# Path: mimic3models/metrics.py
# def print_metrics_binary(y_true, predictions, verbose=1):
#     predictions = np.array(predictions)
#     if len(predictions.shape) == 1:
#         predictions = np.stack([1 - predictions, predictions]).transpose((1, 0))
# 
#     cf = metrics.confusion_matrix(y_true, predictions.argmax(axis=1))
#     if verbose:
#         print("confusion matrix:")
#         print(cf)
#     cf = cf.astype(np.float32)
# 
#     acc = (cf[0][0] + cf[1][1]) / np.sum(cf)
#     prec0 = cf[0][0] / (cf[0][0] + cf[1][0])
#     prec1 = cf[1][1] / (cf[1][1] + cf[0][1])
#     rec0 = cf[0][0] / (cf[0][0] + cf[0][1])
#     rec1 = cf[1][1] / (cf[1][1] + cf[1][0])
#     auroc = metrics.roc_auc_score(y_true, predictions[:, 1])
# 
#     (precisions, recalls, thresholds) = metrics.precision_recall_curve(y_true, predictions[:, 1])
#     auprc = metrics.auc(recalls, precisions)
#     minpse = np.max([min(x, y) for (x, y) in zip(precisions, recalls)])
# 
#     if verbose:
#         print("accuracy = {}".format(acc))
#         print("precision class 0 = {}".format(prec0))
#         print("precision class 1 = {}".format(prec1))
#         print("recall class 0 = {}".format(rec0))
#         print("recall class 1 = {}".format(rec1))
#         print("AUC of ROC = {}".format(auroc))
#         print("AUC of PRC = {}".format(auprc))
#         print("min(+P, Se) = {}".format(minpse))
# 
#     return {"acc": acc,
#             "prec0": prec0,
#             "prec1": prec1,
#             "rec0": rec0,
#             "rec1": rec1,
#             "auroc": auroc,
#             "auprc": auprc,
#             "minpse": minpse}


from mimic3models.metrics import print_metrics_binary
import sklearn.utils as sk_utils
import numpy as np
import pandas as pd
import argparse
import json
import os



def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('prediction', type=str)
    parser.add_argument('--test_listfile', type=str,
                        default=os.path.join(os.path.dirname(__file__),
                                             '../../data/in-hospital-mortality/test/listfile.csv'))
    parser.add_argument('--n_iters', type=int, default=10000)
    parser.add_argument('--save_file', type=str, default='ihm_results.json')
    args = parser.parse_args()

    pred_df = pd.read_csv(args.prediction, index_col=False)
    test_df = pd.read_csv(args.test_listfile, index_col=False)

    df = test_df.merge(pred_df, left_on='stay', right_on='stay', how='left', suffixes=['_l', '_r'])
    assert (df['prediction'].isnull().sum() == 0)
    assert (df['y_true_l'].equals(df['y_true_r']))

    metrics = [('AUC of ROC', 'auroc'),
               ('AUC of PRC', 'auprc'),
               ('min(+P, Se)', 'minpse')]

    data = np.zeros((df.shape[0], 2))
    data[:, 0] = np.array(df['prediction'])
    data[:, 1] = np.array(df['y_true_l'])

    results = dict()
    results['n_iters'] = args.n_iters

# Path: mimic3models/metrics.py
# def print_metrics_binary(y_true, predictions, verbose=1):
#     predictions = np.array(predictions)
#     if len(predictions.shape) == 1:
#         predictions = np.stack([1 - predictions, predictions]).transpose((1, 0))
# 
#     cf = metrics.confusion_matrix(y_true, predictions.argmax(axis=1))
#     if verbose:
#         print("confusion matrix:")
#         print(cf)
#     cf = cf.astype(np.float32)
# 
#     acc = (cf[0][0] + cf[1][1]) / np.sum(cf)
#     prec0 = cf[0][0] / (cf[0][0] + cf[1][0])
#     prec1 = cf[1][1] / (cf[1][1] + cf[0][1])
#     rec0 = cf[0][0] / (cf[0][0] + cf[0][1])
#     rec1 = cf[1][1] / (cf[1][1] + cf[1][0])
#     auroc = metrics.roc_auc_score(y_true, predictions[:, 1])
# 
#     (precisions, recalls, thresholds) = metrics.precision_recall_curve(y_true, predictions[:, 1])
#     auprc = metrics.auc(recalls, precisions)
#     minpse = np.max([min(x, y) for (x, y) in zip(precisions, recalls)])
# 
#     if verbose:
#         print("accuracy = {}".format(acc))
#         print("precision class 0 = {}".format(prec0))
#         print("precision class 1 = {}".format(prec1))
#         print("recall class 0 = {}".format(rec0))
#         print("recall class 1 = {}".format(rec1))
#         print("AUC of ROC = {}".format(auroc))
#         print("AUC of PRC = {}".format(auprc))
#         print("min(+P, Se) = {}".format(minpse))
# 
#     return {"acc": acc,
#             "prec0": prec0,
#             "prec1": prec1,
#             "rec0": rec0,
#             "rec1": rec1,
#             "auroc": auroc,
#             "auprc": auprc,
#             "minpse": minpse}


# Path: mimic3benchmark/evaluation/evaluate_ihm.py
from mimic3models.metrics import print_metrics_binary
import sklearn.utils as sk_utils
import numpy as np
import pandas as pd
import argparse
import json
import os




def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('prediction', type=str)
    parser.add_argument('--test_listfile', type=str,
                        default=os.path.join(os.path.dirname(__file__),
                                             '../../data/in-hospital-mortality/test/listfile.csv'))
    parser.add_argument('--n_iters', type=int, default=10000)
    parser.add_argument('--save_file', type=str, default='ihm_results.json')
    args = parser.parse_args()

    pred_df = pd.read_csv(args.prediction, index_col=False)
    test_df = pd.read_csv(args.test_listfile, index_col=False)

    df = test_df.merge(pred_df, left_on='stay', right_on='stay', how='left', suffixes=['_l', '_r'])
    assert (df['prediction'].isnull().sum() == 0)
    assert (df['y_true_l'].equals(df['y_true_r']))

    metrics = [('AUC of ROC', 'auroc'),
               ('AUC of PRC', 'auprc'),
               ('min(+P, Se)', 'minpse')]

    data = np.zeros((df.shape[0], 2))
    data[:, 0] = np.array(df['prediction'])
    data[:, 1] = np.array(df['y_true_l'])

    results = dict()
    results['n_iters'] = args.n_iters

    ret = print_metrics_binary(data[:, 1], data[:, 0], verbose=0)