def event_info(experiment, data, window): """ """ try: subject = experiment.active_subject if not subject: return "" raw = subject.get_raw() stim_ch = find_stim_channel(subject.get_raw()) if not stim_ch: return "" events = Events(raw, stim_ch=stim_ch).events if events is None: return "" bins = np.bincount(events[:, 2]) event_counts = dict() for event_id in set(events[:, 2]): event_counts[event_id] = bins[event_id] events_string = '' for key, value in event_counts.items(): events_string += 'Trigger %s, %s events\n' % (str(key), str(value)) return events_string except Exception as exc: return ""
def run(self): try: subject = self.experiment.active_subject if not subject: return "" raw = subject.get_raw() stim_ch = find_stim_channel(raw) if not stim_ch: return "" events = find_events(raw, stim_ch=stim_ch) if events is None: return "" bins = np.bincount(events[:,2]) event_counts = dict() for event_id in set(events[:, 2]): event_counts[event_id] = bins[event_id] events_string = "" for key, value in event_counts.items(): events_string += 'Trigger %s, %s events\n' % (str(key), str(value)) return events_string except Exception as exc: return ""
def find_event_times(raw, event_id, mask): stim_ch = find_stim_channel(raw) sfreq = raw.info['sfreq'] events = Events(raw, stim_ch, mask, event_id).events times = [(event[0] - raw.first_samp) / sfreq for event in events] return times
def test_find_stim_channel(): sample_folder = mne.datasets.sample.data_path() sample_fname = os.path.join(sample_folder, 'MEG', 'sample', 'sample_audvis_raw.fif') raw = mne.io.read_raw_fif(sample_fname, preload=True) stim_channel = find_stim_channel(raw) assert (stim_channel == 'STI 014')
def run(self): subject = self.experiment.active_subject raw = subject.get_raw() if not raw: return old_bads = raw.info['bads'].copy() old_annotations = raw.annotations.copy() # find events stim_ch = find_stim_channel(raw) if not stim_ch: events = None else: events = find_events(raw, stim_ch=stim_ch) def handle_close(event): bads_changed = (sorted(raw.info['bads']) != sorted(old_bads)) annotations_changed = False if len(raw.annotations) != len(old_annotations): annotations_changed = True elif not np.allclose(raw.annotations.onset, old_annotations.onset): annotations_changed = True params = {} params['bads'] = raw.info['bads'] params['annotations'] = list(raw.annotations) params['bads_changed'] = bads_changed params['annotations_changed'] = annotations_changed try: self.handler(subject, params) except Exception as exc: exc_messagebox(self.window, exc) fig = raw.plot(events=events, show=False) fig.canvas.mpl_connect('close_event', handle_close) plt.show()
def plot(experiment, data, window): """ """ subject = experiment.active_subject raw = subject.get_raw() if not raw: return old_bads = raw.info['bads'].copy() old_annotations = raw.annotations.copy() def handle_close(event): bads_changed = (sorted(raw.info['bads']) != sorted(old_bads)) annotations_changed = False if len(raw.annotations) != len(old_annotations): annotations_changed = True elif not np.allclose(raw.annotations.onset, old_annotations.onset): annotations_changed = True if bads_changed: logging.getLogger('ui_logger').info('Bads changed!') if annotations_changed: logging.getLogger('ui_logger').info('Annotations changed!') if bads_changed or annotations_changed: subject.save() window.initialize_ui() # find events stim_ch = find_stim_channel(raw) if not stim_ch: events = None else: events = Events(raw, stim_ch=stim_ch).events fig = raw.plot(events=events, show=False) fig.canvas.mpl_connect('close_event', handle_close) plt.show()
def create_epochs_from_events(params, subject): """ """ raw = subject.get_raw() params = copy.deepcopy(params) event_params = params['events'] reject_params = params['reject'] # convert reject params from human readable units to standard units for key in reject_params: reject_params[key] /= get_scaling(key) # remove params that don't match with the channel types present in raw if mne.pick_types(raw.info, meg='grad', eeg=False).size == 0: reject_params.pop('grad', None) if mne.pick_types(raw.info, meg='mag', eeg=False).size == 0: reject_params.pop('mag', None) if mne.pick_types(raw.info, meg=False, eeg=True).size == 0: reject_params.pop('eeg', None) events = [] category = {} stim_channel = find_stim_channel(raw) # event_id should not matter after epochs are created, # so we just add placeholders as 1, 2, 3... if len(event_params) > 0: for idx, item in enumerate(event_params): event_id = item['event_id'] mask = item['mask'] category_id = ('id_' + str(event_id) + '_mask_' + str(mask)) new_events = Events(raw, stim_channel, mask, event_id).events if len(new_events) == 0: logging.warning('No events found with setting ' + str(category_id)) continue category[category_id] = idx + 1 new_events[:, 2] = idx + 1 events.extend([event for event in new_events]) if len(events) == 0: raise ValueError('No events found.') # prepare parameters for pick_types if params['mag'] and params['grad']: meg = True elif params['mag']: meg = 'mag' elif params['grad']: meg = 'grad' else: meg = False eeg = params['eeg'] # find all proper picks, dont exclude bads picks = mne.pick_types(raw.info, meg=meg, eeg=eeg, exclude=[]) if len(picks) == 0: raise ValueError('You should select channel types to go on with') mne_epochs = mne.Epochs(raw, np.array(events), category, params['tmin'], params['tmax'], baseline=(params['bstart'], params['bend']), picks=picks, reject=reject_params) if len(mne_epochs.get_data()) == 0: raise ValueError('Could not find any data. Perhaps the ' + 'rejection thresholds are too strict...') epochs_directory = subject.epochs_directory epochs = Epochs(params['collection_name'], epochs_directory, params, content=mne_epochs) epochs.save_content() subject.add(epochs, 'epochs')