# Path: utils/loader.py
# def load_grouped_train_data(data_path, subject, files_names_grouped_by_hour):
#     def fill_data_grouped_by_hour(class_label, train_filenames):
#         current_group_idx = 0
#         current_ten_minutes_idx = 0
#         for i, filename in enumerate(train_filenames):
#             if subject + '/' + filename not in files_names_grouped_by_hour[subject][class_label][current_group_idx]:
#                 raise ValueError(
#                     '{}/{} not in group for {}{}{}'.format(subject, filename, subject, class_label, current_group_idx))
#             if current_ten_minutes_idx == 0:
#                 data_grouped_by_hour[class_label].append([])
#             datum = loadmat(read_dir + '/' + filename, squeeze_me=True)
#             data_grouped_by_hour[class_label][-1].append(datum['data'])
#             current_ten_minutes_idx += 1
#             if len(data_grouped_by_hour[class_label][-1]) == len(
#                     files_names_grouped_by_hour[subject][class_label][current_group_idx]):
#                 current_group_idx += 1
#                 current_ten_minutes_idx = 0
# 
#     read_dir = data_path + '/' + subject
#     filenames = sorted(os.listdir(read_dir))
#     train_filenames = [filename for filename in filenames if 'test' not in filename]
#     interictal_train_filenames = [filename for filename in train_filenames if 'interictal' in filename]
#     preictal_train_filenames = [filename for filename in train_filenames if 'preictal' in filename]
# 
#     data_grouped_by_hour = defaultdict(lambda: [])
#     fill_data_grouped_by_hour('interictal', interictal_train_filenames)
#     fill_data_grouped_by_hour('preictal', preictal_train_filenames)
# 
#     return data_grouped_by_hour
# 
# def load_train_data(data_path, subject):
#     read_dir = data_path + '/' + subject
#     filenames = sorted(os.listdir(read_dir))
#     train_filenames = []
#     for filename in filenames:
#         if 'test' not in filename:
#             train_filenames.append(filename)
# 
#     n = len(train_filenames)
#     datum = loadmat(read_dir + '/' + train_filenames[0], squeeze_me=True)
#     x = np.zeros(((n,) + datum['data'].shape), dtype='float32')
#     y = np.zeros(n, dtype='int8')
# 
#     filename_to_idx = {}
#     for i, filename in enumerate(train_filenames):
#         datum = loadmat(read_dir + '/' + filename, squeeze_me=True)
#         x[i] = datum['data']
#         y[i] = 1 if 'preictal' in filename else 0
#         filename_to_idx[subject + '/' + filename] = i
# 
#     return {'x': x, 'y': y, 'filename_to_idx': filename_to_idx}


import numpy as np
import json, os
import preprocessors.fft as fft
import cPickle
from utils.loader import load_grouped_train_data, load_train_data
from utils.config_name_creator import *
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import log_loss, roc_curve, auc
from commons import reshape_data


def predict(model, x_test, n_test_examples, n_timesteps):
    pred_1m = model.predict_proba(x_test)[:, 1]
    pred_10m = np.reshape(pred_1m, (n_test_examples, n_timesteps))
    pred_10m = np.mean(pred_10m, axis=1)
    return pred_10m


def cross_validate(subject, data_path, reg_C, random_cv=False):
    if random_cv:

# Path: utils/loader.py
# def load_grouped_train_data(data_path, subject, files_names_grouped_by_hour):
#     def fill_data_grouped_by_hour(class_label, train_filenames):
#         current_group_idx = 0
#         current_ten_minutes_idx = 0
#         for i, filename in enumerate(train_filenames):
#             if subject + '/' + filename not in files_names_grouped_by_hour[subject][class_label][current_group_idx]:
#                 raise ValueError(
#                     '{}/{} not in group for {}{}{}'.format(subject, filename, subject, class_label, current_group_idx))
#             if current_ten_minutes_idx == 0:
#                 data_grouped_by_hour[class_label].append([])
#             datum = loadmat(read_dir + '/' + filename, squeeze_me=True)
#             data_grouped_by_hour[class_label][-1].append(datum['data'])
#             current_ten_minutes_idx += 1
#             if len(data_grouped_by_hour[class_label][-1]) == len(
#                     files_names_grouped_by_hour[subject][class_label][current_group_idx]):
#                 current_group_idx += 1
#                 current_ten_minutes_idx = 0
# 
#     read_dir = data_path + '/' + subject
#     filenames = sorted(os.listdir(read_dir))
#     train_filenames = [filename for filename in filenames if 'test' not in filename]
#     interictal_train_filenames = [filename for filename in train_filenames if 'interictal' in filename]
#     preictal_train_filenames = [filename for filename in train_filenames if 'preictal' in filename]
# 
#     data_grouped_by_hour = defaultdict(lambda: [])
#     fill_data_grouped_by_hour('interictal', interictal_train_filenames)
#     fill_data_grouped_by_hour('preictal', preictal_train_filenames)
# 
#     return data_grouped_by_hour
# 
# def load_train_data(data_path, subject):
#     read_dir = data_path + '/' + subject
#     filenames = sorted(os.listdir(read_dir))
#     train_filenames = []
#     for filename in filenames:
#         if 'test' not in filename:
#             train_filenames.append(filename)
# 
#     n = len(train_filenames)
#     datum = loadmat(read_dir + '/' + train_filenames[0], squeeze_me=True)
#     x = np.zeros(((n,) + datum['data'].shape), dtype='float32')
#     y = np.zeros(n, dtype='int8')
# 
#     filename_to_idx = {}
#     for i, filename in enumerate(train_filenames):
#         datum = loadmat(read_dir + '/' + filename, squeeze_me=True)
#         x[i] = datum['data']
#         y[i] = 1 if 'preictal' in filename else 0
#         filename_to_idx[subject + '/' + filename] = i
# 
#     return {'x': x, 'y': y, 'filename_to_idx': filename_to_idx}


# Path: linear_models/log_reg_cv.py
import numpy as np
import json, os
import preprocessors.fft as fft
import cPickle
from utils.loader import load_grouped_train_data, load_train_data
from utils.config_name_creator import *
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import StratifiedKFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import log_loss, roc_curve, auc
from commons import reshape_data



def predict(model, x_test, n_test_examples, n_timesteps):
    pred_1m = model.predict_proba(x_test)[:, 1]
    pred_10m = np.reshape(pred_1m, (n_test_examples, n_timesteps))
    pred_10m = np.mean(pred_10m, axis=1)
    return pred_10m


def cross_validate(subject, data_path, reg_C, random_cv=False):
    if random_cv:

        d = load_train_data(data_path,subject)