# Path: mimic3models/feature_extractor.py
# def extract_features(data_raw, period, features):
#     period = periods_map[period]
#     functions = functions_map[features]
#     return np.array([extract_features_single_episode(x, period, functions)
#                      for x in data_raw])


import numpy as np
import os
import json
import random
from mimic3models.feature_extractor import extract_features
from __future__ import absolute_import
from __future__ import print_function




def convert_to_dict(data, header, channel_info):
    """ convert data from readers output in to array of arrays format """
    ret = [[] for i in range(data.shape[1] - 1)]
    for i in range(1, data.shape[1]):
        ret[i-1] = [(t, x) for (t, x) in zip(data[:, 0], data[:, i]) if x != ""]
        channel = header[i]
        if len(channel_info[channel]['possible_values']) != 0:
            ret[i-1] = list(map(lambda x: (x[0], channel_info[channel]['values'][x[1]]), ret[i-1]))
        ret[i-1] = list(map(lambda x: (float(x[0]), float(x[1])), ret[i-1]))
    return ret


def extract_features_from_rawdata(chunk, header, period, features):
    with open(os.path.join(os.path.dirname(__file__), "resources/channel_info.json")) as channel_info_file:
        channel_info = json.loads(channel_info_file.read())
    data = [convert_to_dict(X, header, channel_info) for X in chunk]

# Path: mimic3models/feature_extractor.py
# def extract_features(data_raw, period, features):
#     period = periods_map[period]
#     functions = functions_map[features]
#     return np.array([extract_features_single_episode(x, period, functions)
#                      for x in data_raw])


# Path: mimic3models/common_utils.py
import numpy as np
import os
import json
import random
from mimic3models.feature_extractor import extract_features

from __future__ import absolute_import
from __future__ import print_function




def convert_to_dict(data, header, channel_info):
    """ convert data from readers output in to array of arrays format """
    ret = [[] for i in range(data.shape[1] - 1)]
    for i in range(1, data.shape[1]):
        ret[i-1] = [(t, x) for (t, x) in zip(data[:, 0], data[:, i]) if x != ""]
        channel = header[i]
        if len(channel_info[channel]['possible_values']) != 0:
            ret[i-1] = list(map(lambda x: (x[0], channel_info[channel]['values'][x[1]]), ret[i-1]))
        ret[i-1] = list(map(lambda x: (float(x[0]), float(x[1])), ret[i-1]))
    return ret


def extract_features_from_rawdata(chunk, header, period, features):
    with open(os.path.join(os.path.dirname(__file__), "resources/channel_info.json")) as channel_info_file:
        channel_info = json.loads(channel_info_file.read())
    data = [convert_to_dict(X, header, channel_info) for X in chunk]

    return extract_features(data, period, features)