# Path: utils/config_name_creator.py
# def create_fft_data_name(settings_dict):
#     return 'fft_' + settings_dict['preprocessor']['features'] + '_' + create_time_data_name(settings_dict) \
#            + 'nfreq_bands' + str(settings_dict['preprocessor']['nfreq_bands']) \
#            + 'win_length_sec' + str(settings_dict['preprocessor']['win_length_sec']) \
#            + 'stride_sec' + str(settings_dict['preprocessor']['stride_sec'])


import csv
import json
from pandas import read_csv
from utils.config_name_creator import create_fft_data_name
from commons import softmax_scaler,minmax_scaler, median_scaler


def merge_csv_files(submission_path, subjects, submission_name):
    print subjects
    with open(submission_path + '/' + submission_name + '.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(['clip', 'preictal'])

    for subject in subjects:
        df = read_csv(submission_path + '/' + subject + '.csv')
        if 'softmax' in submission_name:
            df['preictal'] = softmax_scaler(df.drop('clip', axis=1).values)
        elif 'minmax' in submission_name:
            df['preictal'] = minmax_scaler(df.drop('clip', axis=1).values)
        elif 'median' in submission_name:
            df['preictal'] = median_scaler(df.drop('clip', axis=1).values)
        else:
            df['preictal'] = df.drop('clip', axis=1).values
        with open(submission_path + '/' + submission_name + '.csv', 'a') as f:
            df.to_csv(f, header=False, index=False)


if __name__ == '__main__':
    with open('SETTINGS.json') as f:
        settings_dict = json.load(f)


# Path: utils/config_name_creator.py
# def create_fft_data_name(settings_dict):
#     return 'fft_' + settings_dict['preprocessor']['features'] + '_' + create_time_data_name(settings_dict) \
#            + 'nfreq_bands' + str(settings_dict['preprocessor']['nfreq_bands']) \
#            + 'win_length_sec' + str(settings_dict['preprocessor']['win_length_sec']) \
#            + 'stride_sec' + str(settings_dict['preprocessor']['stride_sec'])


# Path: linear_models/merger.py
import csv
import json
from pandas import read_csv
from utils.config_name_creator import create_fft_data_name
from commons import softmax_scaler,minmax_scaler, median_scaler



def merge_csv_files(submission_path, subjects, submission_name):
    print subjects
    with open(submission_path + '/' + submission_name + '.csv', 'wb') as f:
        writer = csv.writer(f)
        writer.writerow(['clip', 'preictal'])

    for subject in subjects:
        df = read_csv(submission_path + '/' + subject + '.csv')
        if 'softmax' in submission_name:
            df['preictal'] = softmax_scaler(df.drop('clip', axis=1).values)
        elif 'minmax' in submission_name:
            df['preictal'] = minmax_scaler(df.drop('clip', axis=1).values)
        elif 'median' in submission_name:
            df['preictal'] = median_scaler(df.drop('clip', axis=1).values)
        else:
            df['preictal'] = df.drop('clip', axis=1).values
        with open(submission_path + '/' + submission_name + '.csv', 'a') as f:
            df.to_csv(f, header=False, index=False)


if __name__ == '__main__':
    with open('SETTINGS.json') as f:
        settings_dict = json.load(f)


    submission_path = submission_path = settings_dict['path']['submission_path'] + '/logreg' + create_fft_data_name(