# Path: utils/loader.py
# def load_test_data(data_path, subject):
#     read_dir = data_path + '/' + subject
#     data, id = [], []
#     filenames = sorted(os.listdir(read_dir))
#     for filename in sorted(filenames, key=lambda x: int(re.search(r'(\d+).mat', x).group(1))):
#         if 'test' in filename or 'holdout' in filename:
#             data.append(loadmat(read_dir + '/' + filename, squeeze_me=True))
#             id.append(filename)
# 
#     n_test = len(data)
#     x = np.zeros(((n_test,) + data[0]['data'].shape), dtype='float32')
#     for i, datum in enumerate(data):
#         x[i] = datum['data']
# 
#     return {'x': x, 'id': id}
# 
# 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
import os
import matplotlib.pyplot as plt
import preprocessors.fft as fft
from sklearn.preprocessing import StandardScaler
from sklearn.lda import LDA
from sklearn.metrics import confusion_matrix
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from commons import reshape_data
from commons import load_test_labels
from commons import print_cm
from sklearn.metrics import roc_curve




def curve_per_subject(subject, data_path, test_labels):
    d = load_train_data(data_path, subject)
    x, y_10m = d['x'], d['y']
    n_train_examples = x.shape[0]
    n_timesteps = x.shape[-1]
    print 'n_preictal', np.sum(y_10m)
    print 'n_inetrictal', np.sum(y_10m - 1)

    x, y = reshape_data(x, y_10m)
    data_scaler = StandardScaler()
    x = data_scaler.fit_transform(x)

    lda = LDA()
    lda.fit(x, y)

    pred_1m = lda.predict_proba(x)[:, 1]
    pred_10m = np.reshape(pred_1m, (n_train_examples, n_timesteps))
    pred_10m = np.mean(pred_10m, axis=1)
    fpr, tpr, threshold = roc_curve(y_10m, pred_10m)
    c = np.sqrt((1 - tpr) ** 2 + fpr ** 2)
    opt_threshold = threshold[np.where(c == np.min(c))[0]][-1]
    print opt_threshold

    # ------- TEST ---------------


# Path: utils/loader.py
# def load_test_data(data_path, subject):
#     read_dir = data_path + '/' + subject
#     data, id = [], []
#     filenames = sorted(os.listdir(read_dir))
#     for filename in sorted(filenames, key=lambda x: int(re.search(r'(\d+).mat', x).group(1))):
#         if 'test' in filename or 'holdout' in filename:
#             data.append(loadmat(read_dir + '/' + filename, squeeze_me=True))
#             id.append(filename)
# 
#     n_test = len(data)
#     x = np.zeros(((n_test,) + data[0]['data'].shape), dtype='float32')
#     for i, datum in enumerate(data):
#         x[i] = datum['data']
# 
#     return {'x': x, 'id': id}
# 
# 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/lda_snsp_curves.py
import numpy as np
import json
import os
import matplotlib.pyplot as plt
import preprocessors.fft as fft
from sklearn.preprocessing import StandardScaler
from sklearn.lda import LDA
from sklearn.metrics import confusion_matrix
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from commons import reshape_data
from commons import load_test_labels
from commons import print_cm
from sklearn.metrics import roc_curve





def curve_per_subject(subject, data_path, test_labels):
    d = load_train_data(data_path, subject)
    x, y_10m = d['x'], d['y']
    n_train_examples = x.shape[0]
    n_timesteps = x.shape[-1]
    print 'n_preictal', np.sum(y_10m)
    print 'n_inetrictal', np.sum(y_10m - 1)

    x, y = reshape_data(x, y_10m)
    data_scaler = StandardScaler()
    x = data_scaler.fit_transform(x)

    lda = LDA()
    lda.fit(x, y)

    pred_1m = lda.predict_proba(x)[:, 1]
    pred_10m = np.reshape(pred_1m, (n_train_examples, n_timesteps))
    pred_10m = np.mean(pred_10m, axis=1)
    fpr, tpr, threshold = roc_curve(y_10m, pred_10m)
    c = np.sqrt((1 - tpr) ** 2 + fpr ** 2)
    opt_threshold = threshold[np.where(c == np.min(c))[0]][-1]
    print opt_threshold

    # ------- TEST ---------------


    d = load_test_data(data_path, subject)