#                 x.min(axis=1), x.max(axis=1),
#                 color='grey', alpha=.5)
#ax.fill_between(np.arange(filter_length), x_min, x_max,
#                color='red', alpha=.5)
#ax.plot(eigvals, 'ok')
#%%
exp, stimnr = '20180710', 1

ff = Stimulus(exp, stimnr)
stimulus = np.array(randpy.gasdev(-1000, ff.frametimings.shape[0])[0])

st = OMB(exp, 8)
ff = st
stimulus = st.bgsteps[0, :]

allspikes = ff.allspikes()

i = 0
spikes = allspikes[i, :]
filter_length = ff.filter_length

rw = asc.rolling_window(stimulus, filter_length, preserve_dim=True)
sta = (spikes @ rw) / spikes.sum()
#%%
# I am not projecting out the STA like Equation 4 in Schwartz et al.2006,J.Vision
precovar = (rw * spikes[:, None]) - sta
stc = (precovar.T @ precovar) / (spikes.sum() - 1)
eigvals, eigvecs = np.linalg.eig(stc)
eigsort = np.argsort(eigvals)
eigvals, eigvecs = eigvals[eigsort], eigvecs[:, eigsort]
fff_stimnr = asc.stimulisorter(exp)['fff'][0]

fff = Stimulus(exp, fff_stimnr)
fff.filter_length = 20

# Get rid of list of numpy arrays
fff_stas = np.array(fff.read_datafile()['stas'])

glmlabel = 'GLM_contrast_xval'

savepath = os.path.join(fff.stim_dir, glmlabel)
os.makedirs(savepath, exist_ok=True)

fff.get_frametimings()
all_spikes = fff.allspikes()

start = dt.datetime.now()

kall = np.zeros((fff.nclusters, xval_splits, fff.filter_length))
muall = np.zeros((fff.nclusters, xval_splits))

frs = np.zeros((all_spikes.shape[0], int(all_spikes.shape[-1] / xval_splits)))

cross_corrs = np.zeros((fff.nclusters, xval_splits))

t = np.linspace(0, fff.filter_length * fff.frame_duration * 1000,
                fff.filter_length)

plotlabels = ['Contrast']