df = pd.DataFrame(np.concatenate(data), columns=channels) df['block_name'] = np.concatenate([[p]*len(d) for p, d in zip(p_names, data)]) df['block_number'] = np.concatenate([[j + 1]*len(d) for j, d in enumerate(data)]) return df, fs, p_names, channels dir = r'C:\Users\Nikolai\Desktop\vibro-decay' experiment = 'vibro-decay-vb-exp_01-26_17-42-57' file = r'{}\{}\experiment_data.h5'.format(dir, experiment) data, fs, p_names, channels = load_data(file) for ch in []: channels.remove(ch) del data[ch] data = data[~get_outliers_mask(data[channels], iter_numb=25, std=2.5)] plt.plot(data[channels]*10000 + np.arange(len(channels))) plt.show() #data = data.iloc[~get_outliers_mask(data[channels], std=3, iter_numb=40)] #data = data.drop(np.array(channels)[[0, 4, 9, 20]], axis=1) #channels = [ch for j, ch in enumerate(channels) if j not in [0, 4, 9, 20]] #data[channels] = fft_filter(data[channels], fs, (1, 45)) #data = data.iloc[~get_outliers_mask(data[channels], std=3, iter_numb=2)] #plt.plot(data[channels] + 1000*np.arange(len(channels))) #plt.show() print('ww', sum(get_outliers_mask(data[channels][data['block_number']<3]))) # spatial filter (SMR detection) try:
experiments = pd.read_csv(wdir + 'vibro-decay.csv') experiments = experiments[experiments.protocol == 'belt'] print(experiments) n_exp = 20 print(n_exp) exp = experiments.iloc[n_exp] desc = '{}-{}-{}-{}'.format(exp['subject'], exp['protocol'], { 0: 'exp', 1: 'control' }[exp['control']], '-'.join(exp.dataset.split('_')[-2:])) print(exp, '\n*******************', desc, '\n*******************') df, fs, p_names, channels = load_data('{}{}/experiment_data.h5'.format( data_dir, exp.dataset)) df = df[~get_outliers_mask(df[channels], std=3)] right, left = runica2( df.loc[df['block_number'].isin([1, 2, 3, 7, 8, 9]), channels], fs, channels, ['RIGHT', 'LEFT']) np.save(wdir + desc + '-RIGHT.npy', right) np.save(wdir + desc + '-LEFT.npy', left) # load and plot right = np.load(wdir + desc + '-RIGHT.npy') left = np.load(wdir + desc + '-LEFT.npy') f, ax = plt.subplots(1, 4) plot_topomap(right[0], Montage(channels).get_pos(), contours=0, axes=ax[0],
data_dir = '/media/nikolai/D27ECFCB7ECFA697/Users/Nikolai/Desktop/vibro-decay/' experiments = pd.read_csv(wdir + 'vibro-decay.csv') experiments = experiments[experiments.protocol == 'belt'] print(experiments) n_exp = 24 print(n_exp) exp = experiments.iloc[n_exp] desc = '{}-{}-{}-{}'.format(exp['subject'], exp['protocol'], {0: 'exp', 1:'control'}[exp['control']], '-'.join(exp.dataset.split('_')[-2:])) print(exp, '\n*******************', desc, '\n*******************') df, fs, p_names, channels = load_data('{}{}/experiment_data.h5'.format(data_dir, exp.dataset)) channels = channels[:32] df = df[~get_outliers_mask(df[channels], std=3)] right, left = runica2(df.loc[df['block_number'].isin([1, 2, 3, 7, 8, 9]), channels], fs, channels, ['RIGHT', 'LEFT']) np.save(wdir + desc + '-RIGHT.npy', right) np.save(wdir + desc + '-LEFT.npy', left) # load and plot right = np.load(wdir + desc + '-RIGHT.npy') left = np.load(wdir + desc + '-LEFT.npy') f, ax = plt.subplots(1, 4) plot_topomap(right[0], Montage(channels).get_pos(), contours=0, axes=ax[0], show=False) plot_topomap(right[1], Montage(channels).get_pos(), contours=0, axes=ax[1], show=False) plot_topomap(left[0], Montage(channels).get_pos(), contours=0, axes=ax[2], show=False) plot_topomap(left[1], Montage(channels).get_pos(), contours=0, axes=ax[3], show=False) ax[0].set_title('Right spat.')
print(n_exp) exp = experiments.iloc[n_exp] if n_exp == 0: continue if exp['name'] in ['va-ba']: print('\n'.join(['*********************************'] * 10)) continue desc = '{}-{}-{}-{}'.format(exp['subject'], exp['protocol'], { 0: 'exp', 1: 'control' }[exp['control']], '-'.join(exp.dataset.split('_')[-2:])) print(exp, '\n*******************', desc, '\n*******************') df, fs, p_names, channels = load_data('{}{}/experiment_data.h5'.format( data_dir, exp.dataset)) df.iloc[get_outliers_mask(df[channels], std=3, iter_numb=20)] = 0 #channels = channels[:32] channels = channels[:32] right = np.load(wdir + desc + '-RIGHT.npy')[0] left = np.load(wdir + desc + '-LEFT.npy')[0] x = np.dot(df[channels], right) f, t, Sxx = signal.spectrogram(x, fs, scaling='spectrum', nperseg=fs * 10, nfft=fs * 10, noverlap=int(fs * 10 * 0.90))
channels = [ 'Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'Ft9', 'Fc5', 'Fc1', 'Fc2', 'Fc6', 'Ft10', 'T3', 'C3', 'Cz', 'C4', 'T4', 'Tp9', 'Cp5', 'Cp1', 'Cp2', 'Cp6', 'Tp10', 'T5', 'P3', 'Pz', 'P4', 'T6', 'O1', 'Oz', 'O2' ] data = [ loadmat( r'C:\Users\Nikolai\Desktop\Liza_diplom_data\Liza_diplom_data\treatment\p25\day2\proba{}.mat' .format(k))['X1Topo'].T for k in range(1, 16) ] df = pd.DataFrame(data=np.concatenate(data), columns=channels) for ch in ['Pz', 'Cz']: channels.remove(ch) del df[ch] df = df.loc[~get_outliers_mask(df[channels])] #plt.plot(*welch(df['C3'], fs, nperseg=4*fs)) plt.plot(df[channels]) plt.show() #plt.show() #df['C3env'] = np.abs(hilbert(fft_filter(df['C3'], fs, band))) #df['C4env'] = np.abs(hilbert(fft_filter(df['C4'], fs, band))) #plt.plot(df['C3env']+df['C4env']) a = QtGui.QApplication([]) (rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection(df.iloc[:fs * 60 * 3], channels, fs) df['SMR'] = np.dot(df.as_matrix(), filt) df.to_pickle('p4') plt.plot(df['SMR']) plt.show()
for n_exp in list(range(0 , len(experiments)))[::-1]: print(n_exp) exp = experiments.iloc[n_exp] if n_exp == 0: continue if exp['name'] in ['va-ba']: print('\n'.join(['*********************************']*10)) continue desc = '{}-{}-{}-{}'.format(exp['subject'], exp['protocol'], {0: 'exp', 1:'control'}[exp['control']], '-'.join(exp.dataset.split('_')[-2:])) print(exp, '\n*******************', desc, '\n*******************') df, fs, p_names, channels = load_data('{}{}/experiment_data.h5'.format(data_dir, exp.dataset)) df.iloc[get_outliers_mask(df[channels], std=3, iter_numb=20)] = 0 #channels = channels[:32] channels = channels[:32] right = np.load(wdir + desc + '-RIGHT.npy')[0] left = np.load(wdir + desc + '-LEFT.npy')[0] x = np.dot(df[channels], right) f, t, Sxx = signal.spectrogram(x, fs, scaling='spectrum', nperseg=fs*10, nfft=fs*10, noverlap=int(fs*10*0.90)) Sxx = Sxx**0.5 print(f.shape, t.shape, Sxx.shape) norm = np.mean(Sxx[(f >= band[0]) & (f <= band[1])][:, t <= 120])