datafolder = sys.argv[-1]
appendix = sys.argv[-2]
os.environ.keys()
ii = int(os.environ['SLURM_ARRAY_TASK_ID'])
print('ii = %d' % (ii))  # null hypothesis run index

import numpy as np
data = np.load(datafolder + appendix + '/param.npy', allow_pickle=True)
param = data.item()
for key, val in param.items():
    exec(key + '=val')

runs = 400  # runjob series number
STA = np.zeros(L)
for kk in range(1, runs + 1):
    datafile = datafolder + appendix + '/Series' + str(
        kk) + '/STA_null_run%d.npy' % (ii)
    data = np.load(datafile, allow_pickle=True)
    dic = data.item()
    STA_tmp = dic['STA']
    STA = STA + STA_tmp

STA = STA / float(runs)
from transferit import gain, firing_rate_estimate
fr_estimated = firing_rate_estimate(range(1, runs + 1), datafolder, appendix)
[f, gain_filt] = gain(fr_estimated, STA, datafolder, appendix)
transferdata = {'f': f, 'gain': gain_filt}
np.save(
    datafolder + appendix +
    '/nullhypothesis/transferdata_nullhypothesis_run%d' % (ii), transferdata)
datafolder = sys.argv[-1]
stim_type = sys.argv[-2]
os.environ.keys()
ii = int(os.environ['SLURM_ARRAY_TASK_ID'])
print('ii = %d' % (ii))

for tau in (50, ):
    for posNa in (47, ):
        for fr in (5192, ):
            if stim_type == 'OU':
                from transferit import STA_average, gain, firing_rate_estimate
                appendix = 'tau%sfr%sposNa%s_spikedistance50_v3' % (tau, fr,
                                                                    posNa)
                # Randomly select STA with replacement.
                List = [randint(1, runs) for i in range(runs)]
                fr_estimated = firing_rate_estimate(List, datafolder, appendix)
                print('Firing rate is estimated to be %sHz.' % (fr_estimated))
                STA = STA_average(List, datafolder, appendix)  # averaged STA
                [f, gain_filt] = gain(fr_estimated, STA, datafolder, appendix)
                transferdata = {'f': f, 'gain': gain_filt}

            if stim_type == 'step':
                amplitude = 0.1
                appendix = 'tau%sfr%sposNa%samplitude%s' % (tau, fr, posNa,
                                                            amplitude)
                from transferit import gain_step, firing_rate_step
                List = [randint(1, runs) for i in range(runs)]
                print('length of list is %d' % (len(List)))
                [f, gain,
                 firing_rate_smooth] = gain_step(List, datafolder, appendix,
                                                 tau, amplitude)