# Path: mimic3benchmark/readers.py
# class LengthOfStayReader(Reader):
#     def __init__(self, dataset_dir, listfile=None):
#         """ Reader for length of stay prediction task.
# 
#         :param dataset_dir: Directory where timeseries files are stored.
#         :param listfile:    Path to a listfile. If this parameter is left `None` then
#                             `dataset_dir/listfile.csv` will be used.
#         """
#         Reader.__init__(self, dataset_dir, listfile)
#         self._data = [line.split(',') for line in self._data]
#         self._data = [(x, float(t), float(y)) for (x, t, y) in self._data]
# 
#     def _read_timeseries(self, ts_filename, time_bound):
#         ret = []
#         with open(os.path.join(self._dataset_dir, ts_filename), "r") as tsfile:
#             header = tsfile.readline().strip().split(',')
#             assert header[0] == "Hours"
#             for line in tsfile:
#                 mas = line.strip().split(',')
#                 t = float(mas[0])
#                 if t > time_bound + 1e-6:
#                     break
#                 ret.append(np.array(mas))
#         return (np.stack(ret), header)
# 
#     def read_example(self, index):
#         """ Reads the example with given index.
# 
#         :param index: Index of the line of the listfile to read (counting starts from 0).
#         :return: Dictionary with the following keys:
#             X : np.array
#                 2D array containing all events. Each row corresponds to a moment.
#                 First column is the time and other columns correspond to different
#                 variables.
#             t : float
#                 Length of the data in hours. Note, in general, it is not equal to the
#                 timestamp of last event.
#             y : float
#                 Remaining time in ICU.
#             header : array of strings
#                 Names of the columns. The ordering of the columns is always the same.
#             name: Name of the sample.
#         """
#         if index < 0 or index >= len(self._data):
#             raise ValueError("Index must be from 0 (inclusive) to number of lines (exclusive).")
# 
#         name = self._data[index][0]
#         t = self._data[index][1]
#         y = self._data[index][2]
#         (X, header) = self._read_timeseries(name, t)
# 
#         return {"X": X,
#                 "t": t,
#                 "y": y,
#                 "header": header,
#                 "name": name}
# 
# Path: mimic3models/common_utils.py
# def convert_to_dict(data, header, channel_info):
# def extract_features_from_rawdata(chunk, header, period, features):
# def read_chunk(reader, chunk_size):
# def sort_and_shuffle(data, batch_size):
# def add_common_arguments(parser):
#     def __init__(self, dataset_dir, listfile=None, small_part=False):
#     def _read_timeseries(self, ts_filename):
# def create_directory(directory):
# def pad_zeros(arr, min_length=None):
# class DeepSupervisionDataLoader:
# 
# Path: mimic3models/metrics.py
# def print_metrics_binary(y_true, predictions, verbose=1):
# def print_metrics_multilabel(y_true, predictions, verbose=1):
# def mean_absolute_percentage_error(y_true, y_pred):
# def print_metrics_regression(y_true, predictions, verbose=1):
# def get_bin_log(x, nbins, one_hot=False):
# def get_estimate_log(prediction, nbins):
# def print_metrics_log_bins(y_true, predictions, verbose=1):
# def get_bin_custom(x, nbins, one_hot=False):
# def get_estimate_custom(prediction, nbins):
# def print_metrics_custom_bins(y_true, predictions, verbose=1):
# class LogBins:
# class CustomBins:
# 
# Path: mimic3models/length_of_stay/utils.py
# def save_results(names, ts, pred, y_true, path):
#     common_utils.create_directory(os.path.dirname(path))
#     with open(path, 'w') as f:
#         f.write("stay,period_length,prediction,y_true\n")
#         for (name, t, x, y) in zip(names, ts, pred, y_true):
#             f.write("{},{:.6f},{:.6f},{:.6f}\n".format(name, t, x, y))


from sklearn.preprocessing import Imputer, StandardScaler
from sklearn.linear_model import LogisticRegression
from mimic3benchmark.readers import LengthOfStayReader
from mimic3models import common_utils
from mimic3models import metrics
from mimic3models.length_of_stay.utils import save_results
import numpy as np
import argparse
import os
import json
            logreg.fit(train_X, train_y[:, task_id])

            train_preds = logreg.predict_proba(train_X)
            train_activations[:, task_id] = train_preds[:, 1]

            val_preds = logreg.predict_proba(val_X)
            val_activations[:, task_id] = val_preds[:, 1]

            test_preds = logreg.predict_proba(test_X)
            test_activations[:, task_id] = test_preds[:, 1]

        train_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in train_activations])
        val_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in val_activations])
        test_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in test_activations])

        with open(os.path.join(result_dir, 'train_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(train_actual, train_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)

        with open(os.path.join(result_dir, 'val_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(val_actual, val_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)

        with open(os.path.join(result_dir, 'test_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(test_actual, test_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)


# Path: mimic3benchmark/readers.py
# class LengthOfStayReader(Reader):
#     def __init__(self, dataset_dir, listfile=None):
#         """ Reader for length of stay prediction task.
# 
#         :param dataset_dir: Directory where timeseries files are stored.
#         :param listfile:    Path to a listfile. If this parameter is left `None` then
#                             `dataset_dir/listfile.csv` will be used.
#         """
#         Reader.__init__(self, dataset_dir, listfile)
#         self._data = [line.split(',') for line in self._data]
#         self._data = [(x, float(t), float(y)) for (x, t, y) in self._data]
# 
#     def _read_timeseries(self, ts_filename, time_bound):
#         ret = []
#         with open(os.path.join(self._dataset_dir, ts_filename), "r") as tsfile:
#             header = tsfile.readline().strip().split(',')
#             assert header[0] == "Hours"
#             for line in tsfile:
#                 mas = line.strip().split(',')
#                 t = float(mas[0])
#                 if t > time_bound + 1e-6:
#                     break
#                 ret.append(np.array(mas))
#         return (np.stack(ret), header)
# 
#     def read_example(self, index):
#         """ Reads the example with given index.
# 
#         :param index: Index of the line of the listfile to read (counting starts from 0).
#         :return: Dictionary with the following keys:
#             X : np.array
#                 2D array containing all events. Each row corresponds to a moment.
#                 First column is the time and other columns correspond to different
#                 variables.
#             t : float
#                 Length of the data in hours. Note, in general, it is not equal to the
#                 timestamp of last event.
#             y : float
#                 Remaining time in ICU.
#             header : array of strings
#                 Names of the columns. The ordering of the columns is always the same.
#             name: Name of the sample.
#         """
#         if index < 0 or index >= len(self._data):
#             raise ValueError("Index must be from 0 (inclusive) to number of lines (exclusive).")
# 
#         name = self._data[index][0]
#         t = self._data[index][1]
#         y = self._data[index][2]
#         (X, header) = self._read_timeseries(name, t)
# 
#         return {"X": X,
#                 "t": t,
#                 "y": y,
#                 "header": header,
#                 "name": name}
# 
# Path: mimic3models/common_utils.py
# def convert_to_dict(data, header, channel_info):
# def extract_features_from_rawdata(chunk, header, period, features):
# def read_chunk(reader, chunk_size):
# def sort_and_shuffle(data, batch_size):
# def add_common_arguments(parser):
#     def __init__(self, dataset_dir, listfile=None, small_part=False):
#     def _read_timeseries(self, ts_filename):
# def create_directory(directory):
# def pad_zeros(arr, min_length=None):
# class DeepSupervisionDataLoader:
# 
# Path: mimic3models/metrics.py
# def print_metrics_binary(y_true, predictions, verbose=1):
# def print_metrics_multilabel(y_true, predictions, verbose=1):
# def mean_absolute_percentage_error(y_true, y_pred):
# def print_metrics_regression(y_true, predictions, verbose=1):
# def get_bin_log(x, nbins, one_hot=False):
# def get_estimate_log(prediction, nbins):
# def print_metrics_log_bins(y_true, predictions, verbose=1):
# def get_bin_custom(x, nbins, one_hot=False):
# def get_estimate_custom(prediction, nbins):
# def print_metrics_custom_bins(y_true, predictions, verbose=1):
# class LogBins:
# class CustomBins:
# 
# Path: mimic3models/length_of_stay/utils.py
# def save_results(names, ts, pred, y_true, path):
#     common_utils.create_directory(os.path.dirname(path))
#     with open(path, 'w') as f:
#         f.write("stay,period_length,prediction,y_true\n")
#         for (name, t, x, y) in zip(names, ts, pred, y_true):
#             f.write("{},{:.6f},{:.6f},{:.6f}\n".format(name, t, x, y))


# Path: mimic3models/length_of_stay/logistic/main_cf.py
from sklearn.preprocessing import Imputer, StandardScaler
from sklearn.linear_model import LogisticRegression
from mimic3benchmark.readers import LengthOfStayReader
from mimic3models import common_utils
from mimic3models import metrics
from mimic3models.length_of_stay.utils import save_results
import numpy as np
import argparse
import os
import json

            logreg.fit(train_X, train_y[:, task_id])

            train_preds = logreg.predict_proba(train_X)
            train_activations[:, task_id] = train_preds[:, 1]

            val_preds = logreg.predict_proba(val_X)
            val_activations[:, task_id] = val_preds[:, 1]

            test_preds = logreg.predict_proba(test_X)
            test_activations[:, task_id] = test_preds[:, 1]

        train_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in train_activations])
        val_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in val_activations])
        test_predictions = np.array([metrics.get_estimate_custom(x, n_bins) for x in test_activations])

        with open(os.path.join(result_dir, 'train_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(train_actual, train_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)

        with open(os.path.join(result_dir, 'val_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(val_actual, val_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)

        with open(os.path.join(result_dir, 'test_{}.json'.format(model_name)), 'w') as f:
            ret = metrics.print_metrics_custom_bins(test_actual, test_predictions)
            ret = {k: float(v) for k, v in ret.items()}
            json.dump(ret, f)


        save_results(test_names, test_ts, test_predictions, test_actual,