def get_info(f, drop_channels): labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) print('fs: {}\nall labels {}: {}'.format(fs, len(labels), labels)) channels = [label for label in labels if label not in drop_channels] print('selected channels {}: {}'.format(len(channels), channels)) n_protocols = len([k for k in f.keys() if ('protocol' in k and k != 'protocol0')]) protocol_names = [f['protocol{}'.format(j+1)].attrs['name'] for j in range(n_protocols)] print('protocol_names:', protocol_names) return fs, channels, protocol_names
def get_info(f, drop_channels): labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) print('fs: {}\nall labels {}: {}'.format(fs, len(labels), labels)) channels = [label for label in labels if label not in drop_channels] print('selected channels {}: {}'.format(len(channels), channels)) n_protocols = len( [k for k in f.keys() if ('protocol' in k and k != 'protocol0')]) protocol_names = [ f['protocol{}'.format(j + 1)].attrs['name'] for j in range(n_protocols) ] print('protocol_names:', protocol_names) return fs, channels, protocol_names
def stream_file_in_a_thread(file_path, reference, stream_name): file_name, file_extension = os.path.splitext(file_path) if file_extension == '.fif': raw = mne.io.read_raw_fif(file_path, verbose='ERROR') start, stop = raw.time_as_index([0, 60]) # read the first 15s of data source_buffer = raw.get_data(start=start, stop=stop) elif file_extension == '.vhdr': raw = read_raw_brainvision(vhdr_fname=file_path, verbose='ERROR') source_buffer = raw.get_data() else: source_buffer = load_h5py_all_samples(file_path=file_path).T try: if file_extension in ('.fif', '.vhdr'): labels = raw.info['ch_names'] fs = raw.info['sfreq'] else: try: labels, fs = load_channels_and_fs(file_path) except ValueError: xml_str = load_xml_str_from_hdf5_dataset( file_path, 'stream_info.xml') labels, fs = get_lsl_info_from_xml(xml_str) exclude = [ ex.upper() for ex in ChannelsSelector.parse_channels_string(reference) ] labels = [label for label in labels if label.upper() not in exclude] print('Using {} channels and fs={}.\n[{}]'.format( len(labels), fs, labels)) except (FileNotFoundError, DatasetNotFound): print( 'Channels labels and fs not found. Using default 32 channels and fs=500Hz.' ) labels = None fs = None thread = Process(target=run_eeg_sim, args=(), kwargs={ 'chunk_size': 0, 'source_buffer': source_buffer, 'name': stream_name, 'labels': labels, 'freq': fs }) thread.start() time.sleep(2) return thread
def stream_file_in_a_thread(file_path, reference, stream_name): file_name, file_extension = os.path.splitext(file_path) if file_extension == '.fif': raw = mne.io.read_raw_fif(file_path, verbose='ERROR') start, stop = raw.time_as_index([0, 60]) # read the first 15s of data source_buffer = raw.get_data(start=start, stop=stop) elif file_extension == '.vhdr': raw = read_raw_brainvision(vhdr_fname=file_path, verbose='ERROR') source_buffer = raw.get_data() else: source_buffer = load_h5py_all_samples(file_path=file_path).T try: if file_extension in ('.fif', '.vhdr'): labels = raw.info['ch_names'] fs = raw.info['sfreq'] else: try: labels, fs = load_channels_and_fs(file_path) except ValueError: xml_str = load_xml_str_from_hdf5_dataset(file_path, 'stream_info.xml') labels, fs = get_lsl_info_from_xml(xml_str) exclude = [ex.upper() for ex in ChannelsSelector.parse_channels_string(reference)] labels = [label for label in labels if label.upper() not in exclude] print('Using {} channels and fs={}.\n[{}]'.format(len(labels), fs, labels)) except (FileNotFoundError, DatasetNotFound): print('Channels labels and fs not found. Using default 32 channels and fs=500Hz.') labels = None fs = None thread = Process(target=run_eeg_sim, args=(), kwargs={'chunk_size': 0, 'source_buffer': source_buffer, 'name': stream_name, 'labels': labels, 'freq': fs}) thread.start() time.sleep(2) return thread
# return vals, vecs and topographics (in descending order) reversed_slice = slice(-1, None, -1) topo = inv(vecs[:,reversed_slice]).T return vals[reversed_slice], vecs[:, reversed_slice], topo if __name__ == '__main__': dir_ = 'D:\\vnd_spbu\\pilot\\mu5days' experiment = 'pilot5days_Rakhmankulov_Day3_03-01_12-51-41' with h5py.File('{}\\{}\\{}'.format(dir_, experiment, 'experiment_data.h5')) as f: ica = f['protocol1/signals_stats/left/rejections/rejection1'][:] x_filters = dc_blocker(np.dot(f['protocol1/raw_data'][:], ica)) x_rotation = dc_blocker(np.dot(f['protocol2/raw_data'][:], ica)) x_dict = { 'closed': x_filters[:x_filters.shape[0] // 2], 'opened': x_filters[x_filters.shape[0] // 2:], 'rotate': x_rotation } drop_channels = ['AUX', 'A1', 'A2'] labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) print('fs: {}\nall labels {}: {}'.format(fs, len(labels), labels)) channels = [label for label in labels if label not in drop_channels] scores, unmixing_matrix, topographies = csp3(x_dict, 250, (9, 14), lambda_=0.5, regularization_coef=0.01) app = QtWidgets.QApplication([]) selector = ICADialog(np.vstack((x_filters, x_rotation)), channels, fs, unmixing_matrix=unmixing_matrix, mode='csp', scores=scores) selector.exec_()
else: with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment, 'experiment_data.h5')) as f: rejections = [ f['protocol1/signals_stats/left/rejections/rejection{}'. format(j + 1)][:] for j in range(2) ] rejection = rejections[0] if reject_alpha: rejection = np.dot(rejection, rejections[1]) # load data with h5py.File('{}\\{}\\{}'.format(pilot_dir, experiment, 'experiment_data.h5')) as f: labels, fs = get_lsl_info_from_xml(f['stream_info.xml'][0]) print('fs: {}\nall labels {}: {}'.format(fs, len(labels), labels)) channels = [ label for label in labels if label not in ['A1', 'A2', 'AUX'] ] pz_index = channels.index('Pz') print('selected channels {}: {}'.format( len(channels) - use_pz, channels)) data = [] for j in PROTOCOLS['ALL']: raw = f['protocol{}/raw_data'.format(j)][:] raw = raw[:raw.shape[0] - raw.shape[0] % fs] if use_pz: raw = raw[:, np.arange(raw.shape[1]) != pz_index] data.append(np.dot(raw, rejection)[:, channels.index(channel)]) assert [
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 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