# Path: utils/config_name_creator.py
# def create_time_data_name(settings_dict):
#     highcut = settings_dict['preprocessor']['highcut']
#     lowcut = settings_dict['preprocessor']['lowcut']
#     config_name = 'lowcut' + str(lowcut) + 'highcut' + str(highcut)
#     return config_name


import scipy as sc
import scipy.signal
import json
import os
import shutil
import numpy as np
from functools import partial
from multiprocessing import Pool
from scipy.io import loadmat, savemat
from utils.config_name_creator import create_time_data_name

def process_file(read_dir, write_dir, lowcut, highcut,  raw_file_path):
    preprocessed_file_path = raw_file_path.replace(read_dir, write_dir)

    d = loadmat(raw_file_path)
    sample = ''
    for key in d.keys():
        if 'segment' in key:
            sample = key
            break
    x = np.array(d[sample][0][0][0], dtype='float32')
    data_length_sec = d[sample][0][0][1][0][0]
    if 'test' in raw_file_path or 'holdout' in raw_file_path:
        sequence = np.Inf
    else:
        sequence = d[sample][0][0][4][0][0]


    new_sampling_frequency = 400
    new_x = filter(x, new_sampling_frequency, data_length_sec, lowcut, highcut)
    data_dict = {'data': new_x, 'data_length_sec': data_length_sec,
                 'sampling_frequency': new_sampling_frequency, 'sequence': sequence}
    savemat(preprocessed_file_path, data_dict, do_compression=True)


def run_filter_preprocessor():
    with open('SETTINGS.json') as f:
        settings_dict = json.load(f)

    raw_data_path = settings_dict['path']['raw_data_path']

# Path: utils/config_name_creator.py
# def create_time_data_name(settings_dict):
#     highcut = settings_dict['preprocessor']['highcut']
#     lowcut = settings_dict['preprocessor']['lowcut']
#     config_name = 'lowcut' + str(lowcut) + 'highcut' + str(highcut)
#     return config_name


# Path: preprocessors/filtering.py
import scipy as sc
import scipy.signal
import json
import os
import shutil
import numpy as np
from functools import partial
from multiprocessing import Pool
from scipy.io import loadmat, savemat
from utils.config_name_creator import create_time_data_name


def process_file(read_dir, write_dir, lowcut, highcut,  raw_file_path):
    preprocessed_file_path = raw_file_path.replace(read_dir, write_dir)

    d = loadmat(raw_file_path)
    sample = ''
    for key in d.keys():
        if 'segment' in key:
            sample = key
            break
    x = np.array(d[sample][0][0][0], dtype='float32')
    data_length_sec = d[sample][0][0][1][0][0]
    if 'test' in raw_file_path or 'holdout' in raw_file_path:
        sequence = np.Inf
    else:
        sequence = d[sample][0][0][4][0][0]


    new_sampling_frequency = 400
    new_x = filter(x, new_sampling_frequency, data_length_sec, lowcut, highcut)
    data_dict = {'data': new_x, 'data_length_sec': data_length_sec,
                 'sampling_frequency': new_sampling_frequency, 'sequence': sequence}
    savemat(preprocessed_file_path, data_dict, do_compression=True)


def run_filter_preprocessor():
    with open('SETTINGS.json') as f:
        settings_dict = json.load(f)

    raw_data_path = settings_dict['path']['raw_data_path']

    processed_data_path = settings_dict['path']['processed_data_path'] + '/'+create_time_data_name(settings_dict)