def process_best()

in prediction_generation/old-code/summarize_metrics_old.py [0:0]


def process_best(method):
    hyperparams = dict()
    stripped_method = method.replace("best_", "")
    for dataset_metrics in datasets_metrics:
        for unit_method in dataset_metrics["results"][method]:
            if unit_method["status"] == "SUCCESS":
                conf = unit_method["args"]
                metrics = unit_method["scores"]
                f1 = metrics["f1"]
                precision = metrics["precision"]
                recall = metrics["recall"]
                conf_str = json.dumps(conf, sort_keys=True)
                if conf_str in hyperparams:
                    hyperparams[conf_str]["f1"].append(f1)
                    hyperparams[conf_str]["precision"].append(precision)
                    hyperparams[conf_str]["recall"].append(recall)
                else:
                    metrics_dict = {
                        "f1": [f1],
                        "precision": [precision],
                        "recall": [recall]
                    }
                    hyperparams[conf_str] = metrics_dict
    dict_f1 = {key: sum(value['f1']) / len(value['f1']) if len(value['f1']) > 0 else None for key, value in hyperparams.items()}
    dict_precision = {key: sum(value['precision']) / len(value['precision']) if len(value['precision']) > 0 else None for key, value in hyperparams.items()}
    dict_recall = {key: sum(value['recall']) / len(value['recall']) if len(value['recall']) > 0 else None for key, value in hyperparams.items()}
    '''
    print("--------- " + method + " ---------")
    print('################# dict_f1 (' + method + ') #################')
    for key, value in dict_f1.items():
        print(f"{key}: {value}")
    print('################# dict_precision (' + method + ') #################')
    for key, value in dict_precision.items():
        print(f"{key}: {value}")
    print('################# dict_recall (' + method + ') #################')
    for key, value in dict_recall.items():
        print(f"{key}: {value}")
    '''
    try:
        max_f1 = dict_f1[max(dict_f1, key=dict_f1.get)]
    except:
        max_f1 = None
    try:
        precision_max_f1 = dict_precision[max(dict_f1, key=dict_f1.get)]
    except:
        precision_max_f1 = None
    try:
        recall_max_f1 = dict_recall[max(dict_f1, key=dict_f1.get)]
    except:
        recall_max_f1 = None
    try:
        max_precision = dict_precision[max(dict_precision, key=dict_precision.get)]
    except:
        max_precision = None
    try:
        max_recall = dict_recall[max(dict_recall, key=dict_recall.get)]
    except:
        max_recall = None
    MethodsMeasurements[stripped_method].f1_best = max_f1
    MethodsMeasurements[stripped_method].precision_best = max_precision
    MethodsMeasurements[stripped_method].recall_best = max_recall
    MethodsMeasurements[stripped_method].precision_f1_best = precision_max_f1
    MethodsMeasurements[stripped_method].recall_f1_best = recall_max_f1