# Path: cnn/train.py
# def run_trainer():
#     with open('SETTINGS.json') as f:
#         settings_dict = json.load(f)
# 
#     # path
#     data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
#     model_path = settings_dict['path']['model_path'] + '/' + create_cnn_model_name(settings_dict)
#     print data_path
#     print model_path
# 
#     if not os.path.exists(data_path):
#         fft.run_fft_preprocessor()
# 
#     if not os.path.exists(model_path):
#         os.makedirs(model_path)
#     shutil.copy2('SETTINGS.json', model_path + '/SETTINGS.json')
# 
#     # params
#     model_params = settings_dict['model']
#     validation_params = settings_dict['validation']
# 
#     subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
#     for subject in subjects:
#         print '***********************', subject, '***************************'
#         train(subject, data_path, model_path, model_params, validation_params)
# 
# Path: cnn/predict.py
# def run_predictor():
#     with open('SETTINGS.json') as f:
#         settings_dict = json.load(f)
# 
#     model_path = settings_dict['path']['model_path'] + '/' + create_cnn_model_name(settings_dict)
#     data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
#     submission_path = model_path + '/submission'
#     print submission_path
# 
#     if not os.path.exists(data_path):
#         fft.run_fft_preprocessor()
# 
#     if not os.path.exists(submission_path):
#         os.makedirs(submission_path)
#     shutil.copy2('SETTINGS.json', submission_path + '/SETTINGS.json')
# 
#     subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
#     #subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4']
#     for subject in subjects:
#         print '***********************', subject, '***************************'
#         predict(subject, data_path, model_path, submission_path)
# 
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='minmax')
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='softmax')
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='median')
#     merge_csv_data(submission_path, subjects, submission_name='submission')
# 
# Path: utils/averager.py
# def average_submission_files(submission_names, out_submission_name):
#     with open(out_submission_name + '.csv', 'wb') as f:
#         writer = csv.writer(f)
#         writer.writerow(['clip', 'preictal'])
# 
#     dfs = [read_csv(submission_name) for submission_name in submission_names]
#     p1_list = [df['preictal'].values for df in dfs]
#     p1_array = np.vstack(p1_list)
#     print np.cov(p1_array)
#     p1_avg = average(p1_list).squeeze()
#     id = dfs[0]['clip'].values
#     df = DataFrame(data=zip(id, p1_avg), columns=['clip', 'preictal'])
#     with open(out_submission_name + '.csv', 'a') as f:
#         df.to_csv(f, header=False, index=False)
# 
# Path: utils/config_name_creator.py
# def create_cnn_model_name(settings_dict):
#     config_name = ''
#     for key, value in settings_dict['preprocessor'].items():
#         config_name += key[:5] + str(value)
#     for key, value in settings_dict['model'].items():
#         config_name += key[:5] + str(value)
#     for key, value in settings_dict['validation'].items():
#         config_name += key[:5] + str(value)
#     config_name = config_name.replace('\'','')
#     return config_name


import os, shutil, json
from cnn.train import run_trainer
from cnn.predict import run_predictor
from utils.averager import average_submission_files
from utils.config_name_creator import create_cnn_model_name

if __name__ == '__main__':
    settings_files = sorted(os.listdir('settings_dir'))
    for settings_file in settings_files:
        shutil.copy2('settings_dir/'+settings_file, os.getcwd() + '/SETTINGS.json')

# Path: cnn/train.py
# def run_trainer():
#     with open('SETTINGS.json') as f:
#         settings_dict = json.load(f)
# 
#     # path
#     data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
#     model_path = settings_dict['path']['model_path'] + '/' + create_cnn_model_name(settings_dict)
#     print data_path
#     print model_path
# 
#     if not os.path.exists(data_path):
#         fft.run_fft_preprocessor()
# 
#     if not os.path.exists(model_path):
#         os.makedirs(model_path)
#     shutil.copy2('SETTINGS.json', model_path + '/SETTINGS.json')
# 
#     # params
#     model_params = settings_dict['model']
#     validation_params = settings_dict['validation']
# 
#     subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
#     for subject in subjects:
#         print '***********************', subject, '***************************'
#         train(subject, data_path, model_path, model_params, validation_params)
# 
# Path: cnn/predict.py
# def run_predictor():
#     with open('SETTINGS.json') as f:
#         settings_dict = json.load(f)
# 
#     model_path = settings_dict['path']['model_path'] + '/' + create_cnn_model_name(settings_dict)
#     data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
#     submission_path = model_path + '/submission'
#     print submission_path
# 
#     if not os.path.exists(data_path):
#         fft.run_fft_preprocessor()
# 
#     if not os.path.exists(submission_path):
#         os.makedirs(submission_path)
#     shutil.copy2('SETTINGS.json', submission_path + '/SETTINGS.json')
# 
#     subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
#     #subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4']
#     for subject in subjects:
#         print '***********************', subject, '***************************'
#         predict(subject, data_path, model_path, submission_path)
# 
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='minmax')
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='softmax')
#     merge_csv_data(submission_path, subjects, submission_name='submission', scale='median')
#     merge_csv_data(submission_path, subjects, submission_name='submission')
# 
# Path: utils/averager.py
# def average_submission_files(submission_names, out_submission_name):
#     with open(out_submission_name + '.csv', 'wb') as f:
#         writer = csv.writer(f)
#         writer.writerow(['clip', 'preictal'])
# 
#     dfs = [read_csv(submission_name) for submission_name in submission_names]
#     p1_list = [df['preictal'].values for df in dfs]
#     p1_array = np.vstack(p1_list)
#     print np.cov(p1_array)
#     p1_avg = average(p1_list).squeeze()
#     id = dfs[0]['clip'].values
#     df = DataFrame(data=zip(id, p1_avg), columns=['clip', 'preictal'])
#     with open(out_submission_name + '.csv', 'a') as f:
#         df.to_csv(f, header=False, index=False)
# 
# Path: utils/config_name_creator.py
# def create_cnn_model_name(settings_dict):
#     config_name = ''
#     for key, value in settings_dict['preprocessor'].items():
#         config_name += key[:5] + str(value)
#     for key, value in settings_dict['model'].items():
#         config_name += key[:5] + str(value)
#     for key, value in settings_dict['validation'].items():
#         config_name += key[:5] + str(value)
#     config_name = config_name.replace('\'','')
#     return config_name


# Path: cnn/runner.py
import os, shutil, json
from cnn.train import run_trainer
from cnn.predict import run_predictor
from utils.averager import average_submission_files
from utils.config_name_creator import create_cnn_model_name


if __name__ == '__main__':
    settings_files = sorted(os.listdir('settings_dir'))
    for settings_file in settings_files:
        shutil.copy2('settings_dir/'+settings_file, os.getcwd() + '/SETTINGS.json')

        run_trainer()