def gather_metrics()

in compile_table.py [0:0]


def gather_metrics(results):
    """
    Gathers up metrics across all the different report fields, and puts them into
    one nice groupable table.
    """
    output = []
    for modeltype, modelset in results.groupby("name"):
        for tablename, metricname, metrickey in REPORT_FIELDS:
            # gather up the best score by validation fold of this model group
            modelset = modelset.copy().reset_index(drop=True)
            valkey = metrickey.replace("{fold}", "val")
            testkey = metrickey.replace("{fold}", "test")

            if valkey not in results.columns:
                continue

            if valkey not in modelset.columns:
                modelset[valkey] = np.nan
            if testkey not in modelset.columns:
                modelset[testkey] = np.nan

            # find the best model on validation
            modelset = modelset.sort_values(valkey, ascending=(tablename == "Ranking"))
            best_on_val = modelset.head(1).iloc[0]
            val_score = best_on_val[valkey]
            test_score = best_on_val[testkey]

            # report results
            output.append(
                {
                    "modeltype": modeltype,
                    "tablename": tablename,
                    "metric": metricname,
                    "fold": "val",
                    "score": val_score,
                }
            )
            output.append(
                {
                    "modeltype": modeltype,
                    "tablename": tablename,
                    "metric": metricname,
                    "fold": "test",
                    "score": test_score,
                }
            )

    df = pd.DataFrame(output)
    return df