def run(cfg, state=mp.Value('i', 1), queue=None): """ Training protocol for Alpha/Theta neurofeedback. """ redirect_stdout_to_queue(logger, queue, 'INFO') # add tdef object cfg.tdef = trigger_def(cfg.TRIGGER_FILE) # Extract features if not state.value: sys.exit(-1) raw = mne.io.read_raw_brainvision(cfg.DATA_PATH, preload=True) outfile = cfg.OUT_MICROSTATES_FILE ch_names = [ 'P3', 'C3', 'F3', 'Fz', 'F4', 'C4', 'P4', 'Cz', 'Pz', 'Fp1', 'Fp2', 'T3', 'T5', 'O1', 'O2', 'F7', 'F8', 'T6', 'T4' ] raw.pick_channels(ch_names) raw.set_montage('standard_1005') raw.set_eeg_reference('average') raw.filter(1, 30) maps, segmentation = microstates.segment(raw.get_data(), n_states=4, max_n_peaks=10000000, max_iter=5000, normalize=True) np.savetxt(outfile, maps, delimiter=" ")
from mne.datasets import sample fname = sample.data_path() + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' raw = mne.io.read_raw_fif(fname, preload=True) events = mne.find_events(raw) raw.info['bads'] = ['MEG 2443', 'EEG 053'] raw.set_eeg_reference('average') raw.pick_types(meg='mag', eeg=True, eog=True, ecg=True) raw.filter(1, 40) # Clean EOG with ICA ica = mne.preprocessing.ICA(0.99).fit(raw) bads_eog, _ = ica.find_bads_eog(raw) bads_ecg, _ = ica.find_bads_ecg(raw) ica.exclude = bads_eog[:2] + bads_ecg[:2] raw = ica.apply(raw) # Select sensor type # raw.pick_types(meg=False, eeg=True) raw.pick_types(meg='mag', eeg=False) # Segment the data in 6 microstates maps, segmentation, _, _ = microstates.segment(raw.get_data(), n_states=6, random_state=0) # Plot the topographic maps of the microstates and the segmentation microstates.viz.plot_maps(maps, raw.info) microstates.viz.plot_segmentation(segmentation[:500], raw.get_data()[:, :500], raw.times[:500])
raw.set_eeg_reference('average') #raw.plot_psd() plt.show() # Highpass filter the data a little bit raw.filter(0.2, None) # Selecting the sensor types to use in the analysis. In this example, we # use only EEG channels raw.pick_types(meg=False, eeg=True) # Segment the data into 6 microstates maps, segmentation = microstates.segment(raw.get_data(), n_states=nstates, max_n_peaks=10000000000, max_iter=5000, normalize=normalize) # Plot the topographic maps of the found microstates #microstates.plot_maps(maps, raw.info) myplot_maps(maps, raw.info) # Plot the segmentation of the first 500 samples cutpoint = 5000 microstates.plot_segmentation(segmentation[cutpoint - 1000:cutpoint], raw.get_data()[:, cutpoint - 1000:cutpoint], raw.times[cutpoint - 1000:cutpoint]) plt.show() np.savetxt(outfile, maps, delimiter=" ")
n_states = 4 n_inits = 10 EGI256 = True if EGI256 == True: n_channels = 256 - len(chan_to_drop) grouped_maps = np.array([], dtype=np.int64).reshape(0, n_channels) for i, f in enumerate(subj_folder): fname = HC_RS_path + f + '/' + f +'_clean-epo.fif' epochs = mne.read_epochs(fname, preload=True) if EGI256 == True: epochs.drop_channels(chan_to_drop) data = epochs.get_data() # Segment the data in microstates maps, segmentation, gev, gfp_peaks = mst.segment(data, n_states, n_inits) grouped_maps = np.concatenate((grouped_maps, maps), axis=0) # Transpose the maps from maps(n_maps, n_channels) to maps(n_channels, n_maps) # and treat the n_maps as a sample in time. grouped_maps_T = grouped_maps.transpose() # Find the group maps using k-means clustering group_maps, group_gev = mst.segment(grouped_maps_T, n_states, n_inits, use_peaks=False) # Plot the maps mst.viz.plot_maps(group_maps, epochs.info) # Fitting the maps back to the original epoched data by subject grouped_segment, all_p = [], [] for i, f in enumerate(subj_folder):
if EGI256 == True: epochs.drop_channels([ 'E67', 'E73', 'E247', 'E251', 'E256', 'E243', 'E246', 'E250', 'E255', 'E82', 'E91', 'E254', 'E249', 'E245', 'E242', 'E253', 'E252', 'E248', 'E244', 'E241', 'E92', 'E102', 'E103', 'E111', 'E112', 'E120', 'E121', 'E133', 'E134', 'E145', 'E146', 'E156', 'E165', 'E166', 'E174', 'E175', 'E187', 'E188', 'E199', 'E200', 'E208', 'E209', 'E216', 'E217', 'E228', 'E229', 'E232', 'E233', 'E236', 'E237', 'E240', 'E218', 'E227', 'E231', 'E235', 'E239', 'E219', 'E225', 'E226', 'E230', 'E234', 'E238' ]) n_epochs, n_chans, n_samples = epochs.get_data().shape # Segment the data in microstates maps, segmentation, gev, gfp_peaks = mst.segment(epochs.get_data(), n_states, n_inits, normalize=True, min_peak_dist=10, max_n_peaks=10000) # Smoothen the segmentation segmentation_smooth = mst.seg_smoothing(data=epochs.get_data(), maps=maps) # Mark each epoch at a beginning and at an end of an epoch w/ the value 88 seg_w_borders = mst.mark_border_msts(segmentation, n_epochs, n_samples, n_states) # Remove the values 88 of the segmentation seg_wo_borders = seg_w_borders[seg_w_borders != 88] # Plot the topographic maps of the microstates and the segmentation mst.viz.plot_maps(maps, epochs.info) # plot the whole segmentation