# 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, os
import preprocessors.fft as fft
from pandas import DataFrame
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from merger import merge_csv_files
from commons import reshape_data


def train(subject, data_path, reg_C=None):
    d = load_train_data(data_path, subject)
    x, y = d['x'], d['y']
    x, y = reshape_data(x, y)
    data_scaler = StandardScaler()
    x = data_scaler.fit_transform(x)
    lda = LogisticRegression(C=reg_C)
    lda.fit(x, y)
    return lda, data_scaler


def predict(subject, model, data_scaler, data_path, submission_path):

# 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/log_reg.py
import numpy as np
import json, os
import preprocessors.fft as fft
from pandas import DataFrame
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from merger import merge_csv_files
from commons import reshape_data



def train(subject, data_path, reg_C=None):
    d = load_train_data(data_path, subject)
    x, y = d['x'], d['y']
    x, y = reshape_data(x, y)
    data_scaler = StandardScaler()
    x = data_scaler.fit_transform(x)
    lda = LogisticRegression(C=reg_C)
    lda.fit(x, y)
    return lda, data_scaler


def predict(subject, model, data_scaler, data_path, submission_path):

    d = load_test_data(data_path, subject)