# Path: mimic3models/metrics.py
# def print_metrics_regression(y_true, predictions, verbose=1):
#     predictions = np.array(predictions)
#     predictions = np.maximum(predictions, 0).flatten()
#     y_true = np.array(y_true)
# 
#     y_true_bins = [get_bin_custom(x, CustomBins.nbins) for x in y_true]
#     prediction_bins = [get_bin_custom(x, CustomBins.nbins) for x in predictions]
#     cf = metrics.confusion_matrix(y_true_bins, prediction_bins)
#     if verbose:
#         print("Custom bins confusion matrix:")
#         print(cf)
# 
#     kappa = metrics.cohen_kappa_score(y_true_bins, prediction_bins,
#                                       weights='linear')
#     mad = metrics.mean_absolute_error(y_true, predictions)
#     mse = metrics.mean_squared_error(y_true, predictions)
#     mape = mean_absolute_percentage_error(y_true, predictions)
# 
#     if verbose:
#         print("Mean absolute deviation (MAD) = {}".format(mad))
#         print("Mean squared error (MSE) = {}".format(mse))
#         print("Mean absolute percentage error (MAPE) = {}".format(mape))
#         print("Cohen kappa score = {}".format(kappa))
# 
#     return {"mad": mad,
#             "mse": mse,
#             "mape": mape,
#             "kappa": kappa}


from mimic3models.metrics import print_metrics_regression
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/length-of-stay/test/listfile.csv'))
    parser.add_argument('--n_iters', type=int, default=1000)
    parser.add_argument('--save_file', type=str, default='los_results.json')
    args = parser.parse_args()

    pred_df = pd.read_csv(args.prediction, index_col=False, dtype={'period_length': np.float32,
                                                                   'y_true': np.float32})
    test_df = pd.read_csv(args.test_listfile, index_col=False, dtype={'period_length': np.float32,
                                                                   'y_true': np.float32})

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

    metrics = [('Kappa', 'kappa'),
               ('MAD', 'mad'),
               ('MSE', 'mse'),
               ('MAPE', 'mape')]

    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_regression(y_true, predictions, verbose=1):
#     predictions = np.array(predictions)
#     predictions = np.maximum(predictions, 0).flatten()
#     y_true = np.array(y_true)
# 
#     y_true_bins = [get_bin_custom(x, CustomBins.nbins) for x in y_true]
#     prediction_bins = [get_bin_custom(x, CustomBins.nbins) for x in predictions]
#     cf = metrics.confusion_matrix(y_true_bins, prediction_bins)
#     if verbose:
#         print("Custom bins confusion matrix:")
#         print(cf)
# 
#     kappa = metrics.cohen_kappa_score(y_true_bins, prediction_bins,
#                                       weights='linear')
#     mad = metrics.mean_absolute_error(y_true, predictions)
#     mse = metrics.mean_squared_error(y_true, predictions)
#     mape = mean_absolute_percentage_error(y_true, predictions)
# 
#     if verbose:
#         print("Mean absolute deviation (MAD) = {}".format(mad))
#         print("Mean squared error (MSE) = {}".format(mse))
#         print("Mean absolute percentage error (MAPE) = {}".format(mape))
#         print("Cohen kappa score = {}".format(kappa))
# 
#     return {"mad": mad,
#             "mse": mse,
#             "mape": mape,
#             "kappa": kappa}


# Path: mimic3benchmark/evaluation/evaluate_los.py
from mimic3models.metrics import print_metrics_regression
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/length-of-stay/test/listfile.csv'))
    parser.add_argument('--n_iters', type=int, default=1000)
    parser.add_argument('--save_file', type=str, default='los_results.json')
    args = parser.parse_args()

    pred_df = pd.read_csv(args.prediction, index_col=False, dtype={'period_length': np.float32,
                                                                   'y_true': np.float32})
    test_df = pd.read_csv(args.test_listfile, index_col=False, dtype={'period_length': np.float32,
                                                                   'y_true': np.float32})

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

    metrics = [('Kappa', 'kappa'),
               ('MAD', 'mad'),
               ('MSE', 'mse'),
               ('MAPE', 'mape')]

    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_regression(data[:, 1], data[:, 0], verbose=0)