def analyze_task(subject, args, hcp_params): flags = {} events = dict(face=1, tools=2) baseline = (-0.5, 0) all_events = collect_events(hcp_params) files_exist = all([ op.isfile(meg.get_cond_fname(meg.EPO, event)) and op.isfile(meg.get_cond_fname(meg.EVO, event)) for event in events.keys() ]) if not files_exist: epochs_hcp, evokeds_from_epochs_hcp = using_preprocessed_epochs( all_events, events, baseline, hcp_params) for event in events.keys(): epochs_hcp[event].save(meg.get_cond_fname(meg.EPO, event)) mne.write_evokeds(meg.get_cond_fname(meg.EVO, event), evokeds_from_epochs_hcp[event]) flags = meg.calc_fwd_inv_wrapper(subject, args, events, flags) flags, stcs_conds, _ = meg.calc_stc_per_condition_wrapper( subject, events, args.inverse_method, args, flags) flags = meg.calc_labels_avg_per_condition_wrapper(subject, events, args.atlas, args.inverse_method, stcs_conds, args, flags)
def calc_evoked(indices, epochs_fname, overwrite_epochs=False, overwrite_evoked=False): epochs = mne.read_epochs(epochs_fname, preload=False) print(epochs.events.shape) for event_name, event_indices in indices.items(): evoked_event_fname = meg.get_cond_fname(meg.EVO, event_name) epochs_event_fname = meg.get_cond_fname(meg.EPO, event_name) if not op.isfile(epochs_event_fname) or overwrite_epochs: print('Saving {} epochs to {}, events num: {}'.format(event_name, epochs_event_fname, len(event_indices))) event_epochs = epochs[event_indices] event_epochs.save(epochs_event_fname) if not op.isfile(evoked_event_fname) or overwrite_evoked: print('Saving {} evoked to {}'.format(event_name, evoked_event_fname)) mne.write_evokeds(evoked_event_fname, event_epochs.average())
def save_evoked_to_blender(mri_subject, events, args, evoked=None): fol = op.join(MMVT_DIR, mri_subject, 'eeg') utils.make_dir(fol) if '{cond}' in meg.EVO: for event_ind, event_id in enumerate(events.keys()): if evoked is None: evo = mne.read_evokeds(meg.get_cond_fname(meg.EVO, event_id)) else: evo = evoked[event_id] if event_ind == 0: ch_names = np.array(evo[0].ch_names) dt = np.diff(evo[0].times[:2])[0] data = np.zeros((evo[0].data.shape[0], evo[0].data.shape[1], 2)) data[:, :, event_ind] = evo[0].data else: if evoked is None: evoked = mne.read_evokeds(meg.EVO) data = evoked[0].data data = data[..., np.newaxis] ch_names = np.array(evoked[0].ch_names) dt = np.diff(evoked[0].times[:2])[0] if 'Event' in ch_names: event_ind = np.where(ch_names == 'Event')[0] ch_names = np.delete(ch_names, event_ind) data = np.delete(data, event_ind, 0) data_max, data_min = utils.get_data_max_min(data, args.norm_by_percentile, args.norm_percs) max_abs = utils.get_max_abs(data_max, data_min) if args.normalize_evoked: data = data / max_abs np.save(op.join(fol, 'eeg_data.npy'), data) np.savez(op.join(fol, 'eeg_data_meta.npz'), names=ch_names, conditions=list(events.keys()), dt=dt, minmax=(-max_abs, max_abs)) return True
def save_evoked_to_blender(mri_subject, events, args, evoked=None): fol = op.join(MMVT_DIR, mri_subject, 'eeg') utils.make_dir(fol) if '{cond}' in meg.EVO: for event_ind, event_id in enumerate(events.keys()): if evoked is None: evo = mne.read_evokeds(meg.get_cond_fname(meg.EVO, event_id)) else: evo = evoked[event_id] if event_ind == 0: ch_names = np.array(evo[0].ch_names) dt = np.diff(evo[0].times[:2])[0] data = np.zeros((evo[0].data.shape[0], evo[0].data.shape[1], 2)) data[:, :, event_ind] = evo[0].data else: if evoked is None: evoked = mne.read_evokeds(meg.EVO) data = evoked[0].data data = data[..., np.newaxis] ch_names = np.array(evoked[0].ch_names) dt = np.diff(evoked[0].times[:2])[0] if 'Event' in ch_names: event_ind = np.where(ch_names == 'Event')[0] ch_names = np.delete(ch_names, event_ind) data = np.delete(data, event_ind, 0) if args.normalize_evoked: data_max, data_min = utils.get_data_max_min(data, args.norm_by_percentile, args.norm_percs) max_abs = utils.get_max_abs(data_max, data_min) data = data / max_abs np.save(op.join(fol, 'eeg_data.npy'), data) np.savez(op.join(fol, 'eeg_data_meta.npz'), names=ch_names, conditions=list(events.keys()), dt=dt) return True
def plot_evoked(indices): for event_name in indices.keys(): evoked_fname = meg.get_cond_fname(meg.EVO, event_name) print('Reading {}'.format(event_name)) evoked = mne.read_evokeds(evoked_fname)[0] evoked.plot()
evoked_hcp.data.ravel()[::10] * 1e15, linestyle='None', marker='o', alpha=0.1, mec='orange', color='orange') plt.annotate("r=%0.3f" % r1, xy=(-300, 250)) plt.ylabel('evoked from scratch with MNE-HCP') plt.xlabel('evoked from HCP evoked file') plt.show() if __name__ == '__main__': tmin, tmax = -1.5, 2.5 decim = 4 events_id = dict(face=1, tools=2) baseline = (-0.5, 0) all_events = collect_events() # all_epochs, evokeds = reprocess_the_data_from_scratch(all_events, event_id, tmin, tmax, baseline, decim) all_epochs_hcp, evokeds_from_epochs_hcp = using_preprocessed_epochs( all_events, events_id) # evoked_hcp = official_ERF() all_epochs_hcp.save(meg.EPO) for event, evoked in zip(events_id.keys(), evokeds_from_epochs_hcp): mne.write_evokeds(meg.get_cond_fname(meg.EVO, event), evoked) mne.write_evokeds(meg.EVO, evokeds_from_epochs_hcp) print('Finish!')