# 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 pandas import DataFrame
from sklearn.preprocessing import StandardScaler
from sklearn.lda import LDA
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from merger import merge_csv_files
from commons import reshape_data
from commons import load_test_labels
    fbins = []
    for c in range(n_channels):
        fbins.extend(range(n_fbins))  # 0- delta, 1- theta ...
        channels.extend([c] * n_fbins)

    if plot:
        fig = plt.figure()
        for i in range(n_channels):
            if n_channels == 24:
                fig.add_subplot(4, 6, i)
            else:
                fig.add_subplot(4, 4, i)
            ax = plt.gca()
            ax.set_xlim([0, n_fbins])
            ax.set_xticks(np.arange(0.5, n_fbins + 0.5, 1))
            ax.set_xticklabels(np.arange(0, n_fbins))
            max_y = max(abs(coef)) + 0.01
            ax.set_ylim([0, max_y])
            ax.set_yticks(np.around(np.arange(0, max_y, max_y / 4.0), decimals=1))
            for label in (ax.get_xticklabels() + ax.get_yticklabels()):
                label.set_fontsize(15)
            plt.bar(range(0, n_fbins), abs(coef[i * n_fbins:i * n_fbins + n_fbins]))
        fig.suptitle(subject, fontsize=20)
        plt.show()

    coefs = np.reshape(coef, (n_channels, n_fbins))
    return lda, data_scaler, coefs


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

# 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.py
import numpy as np
import json
import os
import matplotlib.pyplot as plt
import preprocessors.fft as fft
from pandas import DataFrame
from sklearn.preprocessing import StandardScaler
from sklearn.lda import LDA
from utils.loader import load_test_data, load_train_data
from utils.config_name_creator import *
from merger import merge_csv_files
from commons import reshape_data
from commons import load_test_labels

    fbins = []
    for c in range(n_channels):
        fbins.extend(range(n_fbins))  # 0- delta, 1- theta ...
        channels.extend([c] * n_fbins)

    if plot:
        fig = plt.figure()
        for i in range(n_channels):
            if n_channels == 24:
                fig.add_subplot(4, 6, i)
            else:
                fig.add_subplot(4, 4, i)
            ax = plt.gca()
            ax.set_xlim([0, n_fbins])
            ax.set_xticks(np.arange(0.5, n_fbins + 0.5, 1))
            ax.set_xticklabels(np.arange(0, n_fbins))
            max_y = max(abs(coef)) + 0.01
            ax.set_ylim([0, max_y])
            ax.set_yticks(np.around(np.arange(0, max_y, max_y / 4.0), decimals=1))
            for label in (ax.get_xticklabels() + ax.get_yticklabels()):
                label.set_fontsize(15)
            plt.bar(range(0, n_fbins), abs(coef[i * n_fbins:i * n_fbins + n_fbins]))
        fig.suptitle(subject, fontsize=20)
        plt.show()

    coefs = np.reshape(coef, (n_channels, n_fbins))
    return lda, data_scaler, coefs


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

    d = load_test_data(data_path, subject)