def get_mean_envelope(x): y = np.dot(x, spatial) envelope = np.abs(hilbert(fft_filter(y, fs, mu_band))) n_samples = len(envelope) // 10 return [ envelope[k * n_samples:(k + 1) * n_samples].mean() for k in range(10) ]
def preproc(x, fs, rej=None): x = dc_blocker(x) x = fft_filter(x, fs, band=(0, 45)) if rej is not None: x = np.dot(x, rej) return x
elif j > len(p_names) - 3: x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None) print(x.shape) raw_after = add_data_simple(raw_after, name, x) # plot raw data ch_plot = ['C3', 'C4', 'P3', 'P4']#, 'Pz', 'Fp1'] fig1, axes = plt.subplots(len(ch_plot), ncols=1, sharex=True, sharey=True) print(axes) #find median x_all = [] for name, x in list(raw_before.items()) + list(raw_after.items()): x_all.append(np.abs(hilbert(fft_filter(x, fs, (4, 8))))) x_median = np.mean(np.concatenate(x_all), 0) print(x_median) # plot raw t = 0 cm = get_colors() for name, x in list(raw_before.items()) + list(raw_after.items()): if name in ['Closed', 'Opened', 'Baseline', 'Left', 'Right', 'FB']: for j, ch in enumerate(ch_plot): time = np.arange(t, t + len(x)) / fs x_plot = fft_filter(x[:, channels.index(ch)], fs, band=(3, 45)) if ch != 'Fp1' else x[:, channels.index(ch)] axes[j].plot(time, x_plot, c=cm[name], alpha=1) x_plot = fft_filter(x[:, channels.index(ch)], fs, band=(9, 14)) if ch != 'Fp1' else x[:, channels.index(ch)] x_plot = np.abs(hilbert(x_plot))
rejection, alpha, ica = load_rejections(f, reject_alpha=True) raw_before = OrderedDict() for j, name in enumerate(p_names): x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs, rejection if reject else None) raw_before = add_data(raw_before, name, x, j) # plot raw data ch_plot = ['C3', 'C4', 'P3', 'P4'] #, 'Pz', 'Fp1'] fig1, axes = plt.subplots(len(ch_plot), ncols=1, sharex=True, sharey=True) print(axes) #find median x_all = [] for name, x in raw_before.items(): x_all.append(np.abs(hilbert(fft_filter(x, fs, (4, 8))))) x_median = np.mean(np.concatenate(x_all), 0) print(x_median) # plot raw t = 0 cm = get_colors_f for name, x in list(raw_before.items()): for j, ch in enumerate(ch_plot): time = np.arange(t, t + len(x)) / fs x_plot = fft_filter(x[:, channels.index(ch)], fs, band=( 3, 45)) if ch != 'Fp1' else x[:, channels.index(ch)] axes[j].plot(time, x_plot, c=cm(name), alpha=1) x_plot = fft_filter(x[:, channels.index(ch)], fs, band=( 9, 14)) if ch != 'Fp1' else x[:, channels.index(ch)] x_plot = np.abs(hilbert(x_plot))
#plt.show() print('ww', sum(get_outliers_mask(data[channels][data['block_number']<3]))) # spatial filter (SMR detection) try: filt = np.load('ica_{}.npy'.format(experiment)) #filt = np.zeros(len(channels)) #filt[channels.index('C3')] = 1 except FileNotFoundError: a = QApplication([]) (rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection( data[channels][data['block_number'].isin([1, 2])]#.iloc[~get_outliers_mask(data[channels][data['block_number']<12], std=2)] , channels, fs, mode='csp') #(_rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection(np.concatenate(list(x[y=='Left']) + l1ist(x[y=='Legs'])), channels, fs, mode='csp') # filt, topography, bandpass, rejections = SelectSSDFilterWidget.select_filter_and_bandpass(np.concatenate(x), ch_names_to_2d_pos(channels), channels, sampling_freq=fs) np.save('ica_{}.npy'.format(experiment), filt) data['SMR'] = np.dot(data[channels], filt) #data = data.iloc[~get_outliers_mask(data[['C3', 'SMR']], std=3, iter_numb=10)] # temporal filter data['SMR_band_filt'] = fft_filter(data['SMR'], fs, (11, 13)) data['SMR_env'] = np.abs(signal.hilbert(data['SMR_band_filt'])) data.to_pickle('data-{}.pkl'.format(experiment)) data['SMR'].plot() data['SMR_band_filt'].plot() data['SMR_env'].plot() plt.show()
raw = OrderedDict() signal = OrderedDict() for j, name in enumerate(p_names): if name != 'Bci': x = preproc(f['protocol{}/raw_data'.format(j + 1)][:], fs) raw = add_data(raw, name, x, j) signal = add_data(signal, name, f['protocol{}/signals_data'.format(j + 1)][:], j) ch_plot = ['C3', 'P3', 'ICA'] # , 'Pz', 'Fp1'] fig1, axes = plt.subplots(len(ch_plot), ncols=1, sharex=True, sharey=False) # find median x_all = [] x_all_ica = [] for name, x in raw.items(): if 'Baseline' in name: x_all.append(np.abs(hilbert(fft_filter(x, fs, band=mu_band)))) x_all_ica.append( np.abs( hilbert(np.dot(fft_filter(x, fs, band=mu_band), spatial)))) break x_median = np.median(np.concatenate(x_all), 0) x_f_median = np.median(np.concatenate(x_all_ica)) coef = 1 # plot raw t = 0 cm = get_colors_f fff = plt.figure() for name, x in list(raw.items()): for j, ch in enumerate(ch_plot):
print(day, subj, experiment) if subj > 4: with h5py.File('{}\\{}\\{}'.format(dir_, experiment, 'experiment_data.h5')) as f: fs, channels, p_names = get_info(f, settings['drop_channels']) spatial = f['protocol15/signals_stats/left/spatial_filter'][:] band = f['protocol15/signals_stats/left/bandpass'][:] print('band:', band) signals = OrderedDict() raw = OrderedDict() for j, name in enumerate(p_names): if name != 'Bci': x = np.dot(f['protocol{}/raw_data'.format(j + 1)][:], spatial) x = drop_outliers(x) x = fft_filter(x, fs, band=band) #x[x > 0.00005] = np.nan signals = add_data(signals, name, x, j) raw = add_data( raw, name, f['protocol{}/raw_data'.format(j + 1)][:], j) norm_coeff = np.std(signals['16. Baseline']) fb_vars = [] for fb_name in ['18. FB', '20. FB', '22. FB']: for fb_part in np.split( signals[fb_name], range(0, len(signals[fb_name]), 2 * fs))[1:-1]: fb_vars.append(fb_part.std()) fb_vars = np.array(fb_vars) / norm_coeff #sns.regplot(np.arange(len(fb_vars)), fb_vars)
print(mock_ind) 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)]) df['alpha'] = np.dot(df[channels], spat) df['alpha_env'] = df['alpha']*0 df['time'] = np.arange(len(df)) / fs df['signal'] = np.concatenate(signal) fig, axes = plt.subplots(1, 5) eeg = fft_filter(df.loc[df['block_name'].isin(['Open', 'Close']), channels], fs, band) topo = np.dot(np.dot(eeg.T, eeg), spat) axes[2].set_xlabel('Spat. filt.') plot_topomap(spat, montage.get_pos(), axes=axes[2], show=False) axes[3].set_xlabel('Topography') plot_topomap(topo, montage.get_pos(), axes=axes[3], show=False) axes[0].plot(df.loc[df['block_number']==2, 'time'], fft_filter(df.loc[df['block_number']==2, 'alpha'], fs, (2, 100))) axes[0].set_xlim(31, 32) axes[0].set_xlabel('Time, s') axes[0].set_ylabel('Voltage, $\mu V$') #axes[2].plot(fft_filter(df.loc[df['block_number']==2, 'alpha'], fs, (1, 45))) axes[1].plot(*welch(df.loc[df['block_name'].isin(['Close']), 'alpha'], fs, nperseg=fs*4)) axes[1].plot(*welch(df.loc[df['block_name'].isin(['Open']), 'alpha'], fs, nperseg=fs*4))
print(mock_ind) 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)]) df['alpha'] = np.dot(df[channels], spat) df['alpha_env'] = df['alpha']*0 df['time'] = np.arange(len(df)) / fs df['times'] = np.concatenate(times) df['signal'] = np.concatenate(signal)[:, 0] fig, axes = plt.subplots(1, 5) eeg = fft_filter(df.loc[df['block_name'].isin(['Legs', 'Left']), channels], fs, band) topo = np.dot(np.dot(eeg.T, eeg), spat) axes[2].set_xlabel('Spat. filt.') plot_topomap(spat, montage.get_pos(), axes=axes[2], show=False, contours=0) axes[3].set_xlabel('Topography') plot_topomap(topo, montage.get_pos(), axes=axes[3], show=False, contours=0) axes[0].plot(df.loc[df['block_number']==1, 'time'], 1000000*fft_filter(df.loc[df['block_number']==1, 'alpha'], fs, (2, 100))) axes[0].set_xlim(3, 4) axes[0].set_xlabel('Time, s') axes[0].set_ylabel('Voltage, $\mu V$') #axes[2].plot(fft_filter(df.loc[df['block_number']==2, 'alpha'], fs, (1, 45))) axes[1].plot(*welch(df.loc[df['block_name'].isin(['Left']), 'alpha']*1000000, fs, nperseg=fs*4)) axes[1].plot(*welch(df.loc[df['block_name'].isin(['Legs']), 'alpha']*1000000, fs, nperseg=fs*4))
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)]) df['times'] = np.concatenate([np.arange(len(d)) for d in data]) df = df[df['block_name'].isin(['Legs', 'Rest', 'Left', 'Right'])] # eeg = df[channels].as_matrix() try: filters = np.load('filters_ex.npy') topos = np.load('topos_ex.npy') except FileNotFoundError: ica = ICADecomposition(channels, fs, (0.5, 45)) scores, filters, topos = ica.decompose(eeg) np.save('filters_ex.npy', filters) np.save('topos_ex.npy', topos) sources = np.dot(eeg, filters) for state in ['Legs', 'Left', 'Right']: smr_ind = np.argmax(sources[df.block_name == 'Rest'].std(0)/sources[df.block_name == state].std(0)) df[state+'SMR'] = np.dot(df[channels], filters[:, smr_ind]) df[state + 'SMR-env'] = np.abs(hilbert(fft_filter(df[state+'SMR'], fs, [9, 13]))) #plt.plot(np.dot(df[channels], filters[:, smr_ind])) #plt.show() #plot_topomap(topos[:, smr_ind], montage.get_pos()) df.to_csv('wow_ex.csv') sns.tsplot(df, time='times', unit='block_number', value='LegsSMR-env', condition='block_name') plt.show()
def plot_results(pilot_dir, subj, channel, alpha_band=(9, 14), theta_band=(3, 6), drop_channels=None, dc=False, reject_alpha=True, normalize_by='opened'): drop_channels = drop_channels or [] cm = get_colors() fg = plt.figure(figsize=(30, 6)) for j_s, experiment in enumerate(subj): with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment, 'experiment_data.h5')) as f: rejections, top_alpha, top_ica = load_rejections( f, reject_alpha=reject_alpha) fs, channels, p_names = get_info(f, drop_channels) ch = channels.index(channel) #plt.plot(fft_filter(f['protocol6/raw_data'][:, ch], fs, band=(3, 35))) #plt.plot(fft_filter(np.dot(f['protocol6/raw_data'], rejections)[:, ch], fs, band=(3, 35))) #plt.show() #from scipy.signal import welch #plt.plot(*welch(f['protocol1/raw_data'][:60*500//2, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol1/raw_data'][60*500//2:, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol2/raw_data'][:30*500//2, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol2/raw_data'][30*500//2:, channels.index('C3')], fs, nperseg=1000)) #plt.legend(['Close', 'Open', 'Left', 'Right']) #plt.show() # collect powers powers = OrderedDict() raw = OrderedDict() alpha = OrderedDict() pow_theta = [] for j, name in enumerate(p_names): pow, alpha_x, x = get_protocol_power(f, j, fs, rejections, ch, alpha_band, dc=dc) if 'FB' in name: pow_theta.append( get_protocol_power(f, j, fs, rejections, ch, theta_band, dc=dc)[0].mean()) powers = add_data(powers, name, pow, j) raw = add_data(raw, name, x, j) alpha = add_data(alpha, name, alpha_x, j) # plot rejections n_tops = top_ica.shape[1] + top_alpha.shape[1] for j_t in range(top_ica.shape[1]): ax = fg.add_subplot( 4, n_tops * len(subj), n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1) ax.set_xlabel('ICA{}'.format(j_t + 1)) labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) channels = [ label for label in labels if label not in drop_channels ] pos = ch_names_to_2d_pos(channels) plot_topomap(data=top_ica[:, j_t], pos=pos, axes=ax, show=False) for j_t in range(top_alpha.shape[1]): ax = fg.add_subplot( 4, n_tops * len(subj), n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1 + top_ica.shape[1]) ax.set_xlabel('CSP{}'.format(j_t + 1)) labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) channels = [ label for label in labels if label not in drop_channels ] pos = ch_names_to_2d_pos(channels) plot_topomap(data=top_alpha[:, j_t], pos=pos, axes=ax, show=False) # plot powers if normalize_by == 'opened': norm = powers['1. Opened'].mean() elif normalize_by == 'beta': norm = np.mean(pow_theta) else: print('WARNING: norm = 1') print('norm', norm) ax1 = fg.add_subplot(3, len(subj), j_s + 1) ax = fg.add_subplot(3, len(subj), j_s + len(subj) + 1) t = 0 for j_p, ((name, pow), (name, x)) in enumerate(zip(powers.items(), raw.items())): if name == '2228. FB': from scipy.signal import periodogram fff = plt.figure() fff.gca().plot(*periodogram(x, fs, nfft=fs * 3), c=cm[name.split()[1]]) plt.xlim(0, 80) plt.ylim(0, 3e-11) plt.show() print(name) time = np.arange(t, t + len(x)) / fs color = cm[''.join( [i for i in name.split()[1] if not i.isdigit()])] ax1.plot(time, fft_filter(x, fs, (2, 45)), c=color, alpha=0.4) ax1.plot(time, alpha[name], c=color) t += len(x) ax.plot([j_p], [pow.mean() / norm], 'o', c=color, markersize=10) ax.errorbar([j_p], [pow.mean() / norm], yerr=pow.std() / norm, c=color, ecolor=color) fb_x = np.hstack([[j] * len(pows) for j, (key, pows) in enumerate(powers.items()) if 'FB' in key]) fb_y = np.hstack( [pows for key, pows in powers.items() if 'FB' in key]) / norm sns.regplot(x=fb_x, y=fb_y, ax=ax, color=cm['FB'], scatter=False, truncate=True) ax1.set_xlim(0, t / fs) ax1.set_ylim(-40, 40) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70) ax.set_xticks(range(len(powers))) ax.set_xticklabels(powers.keys()) ax.set_ylim(0, 3) ax.set_xlim(-1, len(powers)) ax1.set_title('Day {}'.format(j_s + 1)) return fg
def get_mean_envelope(x): y = np.dot(x, spatial) envelope = np.abs(hilbert(fft_filter(y, fs, mu_band))) n_samples = len(envelope) // 10 return [envelope[k * n_samples: (k+1) * n_samples].mean() for k in range(10)]
axes[j, 1].set_xlabel('Spatial filter ({})'.format('before' if j == 0 else 'after')) #plt.show() # plot raw data ch_plot = ['C3', 'P3', 'ICA']#, 'Pz', 'Fp1'] fig1, axes = plt.subplots(len(ch_plot), ncols=1, sharex=True, sharey=False) print(axes) #find median x_all = [] x_all_ica = [] for name, x in raw.items(): if 'Baseline' in name: x_all.append(np.abs(hilbert(fft_filter(x, fs, band=mu_band)))) x_all_ica.append(np.abs(hilbert(np.dot(fft_filter(x, fs, band=mu_band), spatial)))) break x_median = np.median(np.concatenate(x_all), 0) x_f_median = np.median(np.concatenate(x_all_ica)) coef = 1 # plot raw t = 0 cm = get_colors_f fff = plt.figure() for name, x in list(raw.items()): for j, ch in enumerate(ch_plot): time = np.arange(t, t + len(x)) / fs y = x[:, channels.index(ch)] if ch != 'ICA' else np.dot(x, spatial)
# eeg = df[channels].as_matrix() try: filters = np.load('filters_ex.npy') topos = np.load('topos_ex.npy') except FileNotFoundError: ica = ICADecomposition(channels, fs, (0.5, 45)) scores, filters, topos = ica.decompose(eeg) np.save('filters_ex.npy', filters) np.save('topos_ex.npy', topos) sources = np.dot(eeg, filters) for state in ['Legs', 'Left', 'Right']: smr_ind = np.argmax(sources[df.block_name == 'Rest'].std(0) / sources[df.block_name == state].std(0)) df[state + 'SMR'] = np.dot(df[channels], filters[:, smr_ind]) df[state + 'SMR-env'] = np.abs( hilbert(fft_filter(df[state + 'SMR'], fs, [9, 13]))) #plt.plot(np.dot(df[channels], filters[:, smr_ind])) #plt.show() #plot_topomap(topos[:, smr_ind], montage.get_pos()) df.to_csv('wow_ex.csv') sns.tsplot(df, time='times', unit='block_number', value='LegsSMR-env', condition='block_name') plt.show()
def plot_results(pilot_dir, subj, channel, alpha_band=(9, 14), theta_band=(3, 6), drop_channels=None, dc=False, reject_alpha=True, normalize_by='opened'): drop_channels = drop_channels or [] cm = get_colors() fg = plt.figure(figsize=(30, 6)) for j_s, experiment in enumerate(subj): with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment, 'experiment_data.h5')) as f: rejections, top_alpha, top_ica = load_rejections(f, reject_alpha=reject_alpha) fs, channels, p_names = get_info(f, drop_channels) ch = channels.index(channel) #plt.plot(fft_filter(f['protocol6/raw_data'][:, ch], fs, band=(3, 35))) #plt.plot(fft_filter(np.dot(f['protocol6/raw_data'], rejections)[:, ch], fs, band=(3, 35))) #plt.show() #from scipy.signal import welch #plt.plot(*welch(f['protocol1/raw_data'][:60*500//2, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol1/raw_data'][60*500//2:, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol2/raw_data'][:30*500//2, channels.index('C3')], fs, nperseg=1000)) #plt.plot(*welch(f['protocol2/raw_data'][30*500//2:, channels.index('C3')], fs, nperseg=1000)) #plt.legend(['Close', 'Open', 'Left', 'Right']) #plt.show() # collect powers powers = OrderedDict() raw = OrderedDict() alpha = OrderedDict() pow_theta = [] for j, name in enumerate(p_names): pow, alpha_x, x = get_protocol_power(f, j, fs, rejections, ch, alpha_band, dc=dc) if 'FB' in name: pow_theta.append(get_protocol_power(f, j, fs, rejections, ch, theta_band, dc=dc)[0].mean()) powers = add_data(powers, name, pow, j) raw = add_data(raw, name, x, j) alpha = add_data(alpha, name, alpha_x, j) # plot rejections n_tops = top_ica.shape[1] + top_alpha.shape[1] for j_t in range(top_ica.shape[1]): ax = fg.add_subplot(4, n_tops * len(subj), n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1) ax.set_xlabel('ICA{}'.format(j_t + 1)) labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) channels = [label for label in labels if label not in drop_channels] pos = ch_names_to_2d_pos(channels) plot_topomap(data=top_ica[:, j_t], pos=pos, axes=ax, show=False) for j_t in range(top_alpha.shape[1]): ax = fg.add_subplot(4, n_tops * len(subj), n_tops * len(subj) * 3 + n_tops * j_s + j_t + 1 + top_ica.shape[1]) ax.set_xlabel('CSP{}'.format(j_t + 1)) labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) channels = [label for label in labels if label not in drop_channels] pos = ch_names_to_2d_pos(channels) plot_topomap(data=top_alpha[:, j_t], pos=pos, axes=ax, show=False) # plot powers if normalize_by == 'opened': norm = powers['1. Opened'].mean() elif normalize_by == 'beta': norm = np.mean(pow_theta) else: print('WARNING: norm = 1') print('norm', norm) ax1 = fg.add_subplot(3, len(subj), j_s + 1) ax = fg.add_subplot(3, len(subj), j_s + len(subj) + 1) t = 0 for j_p, ((name, pow), (name, x)) in enumerate(zip(powers.items(), raw.items())): if name == '2228. FB': from scipy.signal import periodogram fff = plt.figure() fff.gca().plot(*periodogram(x, fs, nfft=fs * 3), c=cm[name.split()[1]]) plt.xlim(0, 80) plt.ylim(0, 3e-11) plt.show() print(name) time = np.arange(t, t + len(x)) / fs color = cm[''.join([i for i in name.split()[1] if not i.isdigit()])] ax1.plot(time, fft_filter(x, fs, (2, 45)), c=color, alpha=0.4) ax1.plot(time, alpha[name], c=color) t += len(x) ax.plot([j_p], [pow.mean() / norm], 'o', c=color, markersize=10) ax.errorbar([j_p], [pow.mean() / norm], yerr=pow.std() / norm, c=color, ecolor=color) fb_x = np.hstack([[j] * len(pows) for j, (key, pows) in enumerate(powers.items()) if 'FB' in key]) fb_y = np.hstack([pows for key, pows in powers.items() if 'FB' in key]) / norm sns.regplot(x=fb_x, y=fb_y, ax=ax, color=cm['FB'], scatter=False, truncate=True) ax1.set_xlim(0, t / fs) ax1.set_ylim(-40, 40) plt.setp(ax.xaxis.get_majorticklabels(), rotation=70) ax.set_xticks(range(len(powers))) ax.set_xticklabels(powers.keys()) ax.set_ylim(0, 3) ax.set_xlim(-1, len(powers)) ax1.set_title('Day {}'.format(j_s + 1)) return fg
#plt.show() print('ww', sum(get_outliers_mask(data[channels][data['block_number']<3]))) # spatial filter (SMR detection) try: filt = np.load('ica_{}.npy'.format(experiment)) #filt = np.zeros(len(channels)) #filt[channels.index('C3')] = 1 except FileNotFoundError: a = QApplication([]) (rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection( data[channels][data['block_number'].isin([1, 2])]#.iloc[~get_outliers_mask(data[channels][data['block_number']<12], std=2)] , channels, fs, mode='csp') #(_rej, filt, topo, _unmix, _bandpass, _) = ICADialog.get_rejection(np.concatenate(list(x[y=='Left']) + l1ist(x[y=='Legs'])), channels, fs, mode='csp') # filt, topography, bandpass, rejections = SelectSSDFilterWidget.select_filter_and_bandpass(np.concatenate(x), ch_names_to_2d_pos(channels), channels, sampling_freq=fs) np.save('ica_{}.npy'.format(experiment), filt) data['SMR'] = np.dot(data[channels], filt) #data = data.iloc[~get_outliers_mask(data[['C3', 'SMR']], std=3, iter_numb=10)] # temporal filter data['SMR_band_filt'] = fft_filter(data['SMR'], fs, (11, 13)) data['SMR_env'] = np.abs(signal.hilbert(data['SMR_band_filt'])) data.to_pickle('data-{}.pkl'.format(experiment)) data['SMR'].plot() data['SMR_band_filt'].plot() data['SMR_env'].plot() plt.show()
print(mock_ind) 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)]) df['alpha'] = np.dot(df[channels], spat) df['alpha_env'] = df['alpha'] * 0 df['time'] = np.arange(len(df)) / fs df['times'] = np.concatenate(times) df['signal'] = np.concatenate(signal)[:, 0] fig, axes = plt.subplots(1, 5) eeg = fft_filter(df.loc[df['block_name'].isin(['Legs', 'Left']), channels], fs, band) topo = np.dot(np.dot(eeg.T, eeg), spat) axes[2].set_xlabel('Spat. filt.') plot_topomap(spat, montage.get_pos(), axes=axes[2], show=False, contours=0) axes[3].set_xlabel('Topography') plot_topomap(topo, montage.get_pos(), axes=axes[3], show=False, contours=0) axes[0].plot( df.loc[df['block_number'] == 1, 'time'], 1000000 * fft_filter(df.loc[df['block_number'] == 1, 'alpha'], fs, (2, 100))) axes[0].set_xlim(3, 4) axes[0].set_xlabel('Time, s') axes[0].set_ylabel('Voltage, $\mu V$') #axes[2].plot(fft_filter(df.loc[df['block_number']==2, 'alpha'], fs, (1, 45)))
from scipy.io import loadmat import pandas as pd from scipy.signal import hilbert, welch import pylab as plt from pynfb.postprocessing.utils import fft_filter import numpy as np from pynfb.protocols.ssd.topomap_selector_ica import ICADialog from PyQt4 import QtGui df = pd.read_pickle('p4') fs = 1000 band = (8, 14) df['SMRenv'] = pd.Series(fft_filter(df['SMR'], fs, band)).rolling(fs * 100, center=True).std() df['C3env'] = pd.Series(fft_filter(df['C3'], fs, band)).rolling(fs * 100, center=True).std() f, ax = plt.subplots(2) ax[0].plot(df['C3']) ax[0].plot(-df['SMR']) ax[0].legend(['C3', 'SMR-Right-hand']) ax[1].plot(df['C3env']) ax[1].plot(df['SMRenv']) ax[1].legend(['C3', 'SMR-Right-hand']) #plt.plot(fft_filter(df['SMR'], fs, band), alpha=0.5) plt.show()
def preproc(x, fs, rej=None): x = dc_blocker(x) x = fft_filter(x, fs, band=(3, 45)) if rej is not None: x = np.dot(x, rej) return x
from scipy.io import loadmat import pandas as pd from scipy.signal import hilbert, welch import pylab as plt from pynfb.postprocessing.utils import fft_filter import numpy as np from pynfb.protocols.ssd.topomap_selector_ica import ICADialog df = pd.read_pickle('p4') fs = 1000 band = (8, 14) df['SMRenv'] = pd.Series(fft_filter(df['SMR'], fs, band)).rolling(fs*100, center=True).std() df['C3env'] = pd.Series(fft_filter(df['C3'], fs, band)).rolling(fs*100, center=True).std() f, ax = plt.subplots(2) ax[0].plot(df['C3']) ax[0].plot(-df['SMR']) ax[0].legend(['C3', 'SMR-Right-hand']) ax[1].plot(df['C3env']) ax[1].plot(df['SMRenv']) ax[1].legend(['C3', 'SMR-Right-hand']) #plt.plot(fft_filter(df['SMR'], fs, band), alpha=0.5) plt.show()