def get_some_data(real=False): if not real: channels = [ 'Fp1', 'Fp2', 'F7', 'F3', 'Fz', 'F4', 'F8', 'Ft9', 'Fc5', 'Fc1', 'Fc2', 'Fc6', 'Ft10', 'T7', 'C3', 'Cz', 'C4', 'T8', 'Tp9', 'Cp5', 'Cp1', 'Cp2', 'Cp6', 'Tp10', 'P7', 'P3', 'Pz', 'P4', 'P8', 'O1', 'Oz', 'O2' ] data = np.random.normal(loc=0, scale=0.00001, size=(5000, len(channels))).T fs = 500 else: import h5py from vendor.nfb.pynfb.postprocessing.utils import get_info with h5py.File( r'D:\mu_ica\mu_ica\mu_ica_S1_D3_04-21_18-16-03\experiment_data.h5' ) as f: fs, channels, p_names = get_info(f, []) data = f['protocol{}/raw_data'.format( p_names.index('Baseline') + 1)][:].T from PyQt5.QtWidgets import QApplication a = QApplication([]) rej, spatial, top = ICADialog.get_rejection(data.T, channels, fs, mode='ica', states=None)[:3] data = rej.apply(data.T).T return data, fs, channels
def load_data(file_path): with h5py.File(file_path) as f: fs, channels, p_names = get_info(f, ['A1', 'A2']) data = [ f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names)) ] 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
from vendor.nfb.pynfb.postprocessing.utils import get_info, fft_filter import h5py import numpy as np import pandas as pd import seaborn as sns import pylab as plt from vendor.nfb.pynfb.signal_processing.decompositions import ICADecomposition from mne.viz import plot_topomap from vendor.nfb.pynfb.inlets.montage import Montage from scipy.signal import hilbert, welch from scipy.stats import linregress, ttest_ind, ranksums, ttest_1samp with h5py.File(r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\alpha-nfb-example_01-13_17-37-02\experiment_data.h5') as f: fs, channels, p_names = get_info(f, ['A1', 'A2', 'AUX']) channels = [ch.split('-')[0] for ch in channels] mock_ind = [f['protocol{}'.format(k + 1)].attrs['mock_previous'] for k in range(len(p_names))] data = [f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names))] signal = [f['protocol{}/signals_data'.format(k + 1)][:] for k in range(len(p_names))] spat = f['protocol{}/signals_stats/Alpha/spatial_filter'.format(p_names.index('Baseline')+1)][:] band = f['protocol{}/signals_stats/Alpha/bandpass'.format(p_names.index('Baseline') + 1)][:] #print(p_names) montage = Montage(channels) 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)])
# load raw from vendor.nfb.pynfb.widgets.helpers import ch_names_to_2d_pos mock = False dir_ = r'D:\bci_nfb_bci\bci_nfb_bci' with open(dir_ + '\\info.json', 'r') as f: settings = loads(f.read()) subj = 0 day = 2 experiment = settings['subjects'][subj][day] 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'][:] mu_band = f['protocol15/signals_stats/left/bandpass'][:] max_gap = 1 / min(mu_band) * 2 min_sate_duration = max_gap * 2 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
from vendor.nfb.pynfb.postprocessing.utils import get_info, fft_filter import h5py import numpy as np import pandas as pd import seaborn as sns import pylab as plt from vendor.nfb.pynfb.signal_processing.decompositions import ICADecomposition from mne.viz import plot_topomap from vendor.nfb.pynfb.inlets.montage import Montage from scipy.signal import hilbert with h5py.File( r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\bci-example_12-28_17-57-26\experiment_data.h5' ) as f: fs, channels, p_names = get_info(f, []) channels = [ch.split('-')[0] for ch in channels] data = [ f['protocol{}/raw_data'.format(k + 1)][:] for k in range(len(p_names)) ] print(p_names) montage = Montage(channels) 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'])] #
if __name__ == '__main__': np.random.seed(42) # loading anp pre processing import h5py import pylab as plt file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\BCI_Test_4_06-23_16-20-48\experiment_data.h5' #file = r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\BCI_Test_2_1_06-22_17-01-07\experiment_data.h5' labels_map = {'Open': 0, 'Right': 2, 'Left': 1} with h5py.File(file) as f: fs, ch_names, p_names = get_info(f, []) #['AUX', 'A1', 'A2', 'F4', 'Pz'] before = [] after = [] before_labels = [] after_labels = [] k = 0 for protocol in [ 'protocol{}'.format(j + 1) for j in range(len(f.keys()) - 3) ]: name = f[protocol].attrs['name'] if name in ['Right', 'Open', 'Left']: data = f[protocol + '/raw_data'][:] labels = np.ones(len(data), dtype=int) * labels_map[name] if k < 9: before.append(data) before_labels.append(labels)
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
'111-wed': r'C:\Users\Nikolai\PycharmProjects\nfb\pynfb\results\ghosts-in-111_01-31_21-17-36\experiment_data.h5', '405': r'D:\bipolar-test_02-03_21-26-20\experiment_data.h5', '405-eeg': r'D:\new-lab-eeg-test_02-03_17-09-40\experiment_data.h5'} room = '104' if room == '111-eeg': import mne a = mne.io.read_raw_brainvision(r'D:\bp-ghost-111-01-29-21-00-00\fff.vhdr') x = a.get_data([a.ch_names.index('Aux1')])[0]/1000/10 fs = 1000 else: with h5py.File(rooms[room]) as f: print(list(f.keys()), list(f['protocol1'].keys())) from vendor.nfb.pynfb.postprocessing.utils import get_info get_info(f, []) data = [f['protocol{}/raw_data'.format(k+1)][:] for k in range(len(f.keys())-3)] x = np.concatenate(data)[:500*4*60, 0] fs = 500 fig, axes = plt.subplots(2, 1, sharex=True) t = np.arange(len(x))/fs/60 axes[0].plot(t, x*1000*1000) axes[0].set_ylabel('Voltage [$\mu$V]') axes[0].set_ylim(-50, 50) axes[0].set_title('Room: {} Date: {} Start time: {}'.format(room.split('-')[0], *rooms[room].split('\\')[-2].split('_')[-2:])) f, t, Sxx = signal.spectrogram(x, fs, scaling='spectrum') ax = axes[1].pcolormesh(t/60, f, np.log10(Sxx**0.5), vmin=-7.1, vmax=-4.8, cmap='nipy_spectral') axes[1].set_ylabel('Frequency [Hz]')