import torch


def get_aggregated_metrics(metric_name_list, metric_outputs):
    """Get a dictionary of the mean metric values (to log) from metric names and their values"""
    metric_dict = {}
    for metric_name in metric_name_list:
        metric_dim = len(metric_outputs[0][metric_name].shape)
        if metric_dim == 0:
            metric_value = torch.stack([x[metric_name] for x in metric_outputs]).mean()
        else:
            metric_value = torch.cat([x[metric_name] for x in metric_outputs]).double().mean()
        metric_dict[metric_name] = metric_value.item()
    return metric_dict