def test_read_curry_annotations_using_mocked_info(tmpdir, name_part): """Test reading for Curry events file.""" EXPECTED_ONSET = [ 0.484, 0.486, 0.62, 0.622, 1.904, 1.906, 3.212, 3.214, 4.498, 4.5, 5.8, 5.802, 7.074, 7.076, 8.324, 8.326, 9.58, 9.582 ] EXPECTED_DURATION = np.zeros_like(EXPECTED_ONSET) EXPECTED_DESCRIPTION = [ '4', '50000', '2', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000' ] original, fname = _get_read_annotations_mock_info("Curry " + name_part, tmpdir) copyfile(src=original['event'], dst=fname['event']) _msg = 'required files cannot be found' with pytest.raises(FileNotFoundError, match=_msg): read_annotations(fname['event'], sfreq='auto') _mock_info_file(src=original['info'], dst=fname['info'], sfreq=0, time_step=2000) annot = read_annotations(fname['event'], sfreq='auto') assert annot.orig_time is None assert_array_equal(annot.onset, EXPECTED_ONSET) assert_array_equal(annot.duration, EXPECTED_DURATION) assert_array_equal(annot.description, EXPECTED_DESCRIPTION)
def detect_movement(self, thr_mov=.01, plot=True, overwrite=False, save=True): from mne.transforms import read_trans fname = self.subject + '_' + self.experiment + '_mov.txt' out_csv_f = op.join(self.out_annot, fname) fname_t = self.subject + '_' + self.experiment + '_dev2head-trans.fif' out_csv_f_t = op.join(self.out_annot, fname_t) if op.exists(out_csv_f) and not overwrite: mov_annot = read_annotations(out_csv_f) print('Reading from file, mov segments are:', mov_annot) print('Reading from file, dev to head transformation') dev_head_t = read_trans(out_csv_f_t) else: print('Calculating head pos') pos = mne.chpi._calculate_head_pos_ctf(self.raw, gof_limit=-1) mov_annot, hpi_disp, dev_head_t = annotate_motion(self.raw, pos, thr=thr_mov) if plot is True: plt.figure() plt.plot(hpi_disp) plt.axhline(y=thr_mov, color='r') plt.show(block=True) if save is True: mov_annot.save(out_csv_f) dev_head_t.save(out_csv_f_t) #fig.savefig(out_csv_f[:-4]+'.png') old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mov_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.raw.info['dev_head_t_old'] = self.raw.info['dev_head_t'] self.raw.info['dev_head_t'] = dev_head_t self.annot_movement = mov_annot
def test_eeglab_annotations(fname): """Test reading annotations in EEGLAB files.""" _check_h5(fname) annotations = read_annotations(fname) assert len(annotations) == 154 assert set(annotations.description) == {'rt', 'square'} assert np.all(annotations.duration == 0.)
def test_eeglab_annotations(fname): """Test reading annotations in EEGLAB files.""" _check_h5(fname) annotations = read_annotations(fname) assert len(annotations) == 154 assert set(annotations.description) == {'rt', 'square'} assert np.all(annotations.duration == 0.)
def get_trial_info(subject, inputs, outputs, recompute, logpath): """Extract trial information from Presentation log files and BrainVision marker files. 1. trial type {high threat, low threat} 2. start baseline (seconds) 3. cue (seconds) 4. stimulus (seconds) """ save_path = Path(individualize_path(outputs["save_path"], subject, expand_name=True)) if save_path.exists() and not recompute: # only recompute if requested print(f"Not re-computing {save_path}") return log_path = next(Path(".").glob(individualize_path(inputs["log_path"], subject))) marker_path = next(Path(".").glob(individualize_path(inputs["marker_path"], subject))) df_log = pd.read_csv(log_path, sep="\t", usecols=["CSI", "shock"]) markers = read_annotations(marker_path, ECG_SFREQ_ORIGINAL) cues = markers.onset[markers.description == "Stimulus/S 2"] assert len(cues) == df_log.shape[0], ("Unequal number of trials between" " BrainVision and Presentation files" f" for participant {subject}.") trial_types = df_log["shock"] stimuli = cues + df_log["CSI"] / 1000 baselines = cues - 1 # baseline starts 1 second before cue df = pd.DataFrame({"threat": trial_types, "baseline": baselines, "cue": cues, "stimulus": stimuli}) df = df[(df["stimulus"] - df["cue"]) >= 6] # exclude trials with anticipation windows shorter than 6 seconds df.to_csv(save_path, sep="\t", header=True, index=False, float_format="%.4f")
def test_eeglab_read_annotations(): """Test annotations onsets are timestamps (+ validate some).""" annotations = read_annotations(raw_fname_mat) validation_samples = [0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] expected_onset = np.array([1.00, 1.69, 2.08, 4.70, 7.71, 11.30, 17.18, 20.20, 26.12, 29.14, 35.25, 44.30, 47.15]) assert annotations.orig_time is None assert_array_almost_equal(annotations.onset[validation_samples], expected_onset, decimal=2)
def test_eeglab_read_annotations(): """Test annotations onsets are timestamps (+ validate some).""" annotations = read_annotations(raw_fname_mat) validation_samples = [0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] expected_onset = np.array([1.00, 1.69, 2.08, 4.70, 7.71, 11.30, 17.18, 20.20, 26.12, 29.14, 35.25, 44.30, 47.15]) assert annotations.orig_time is None assert_array_almost_equal(annotations.onset[validation_samples], expected_onset, decimal=2)
def detect_muscle(self, thr=1.5, t_min=.5, plot=True, overwrite=False): """Find and annotate mucsle artifacts - by Luke Bloy""" fname = self.subject + '_' + self.experiment + '_mus.txt' out_csv_f = op.join(self.out_annot, fname) if op.exists(out_csv_f) and not overwrite: mus_annot = read_annotations(out_csv_f) print('Reading from file, muscle segments are:', mus_annot) else: print('Calculating muscle artifacts') raw = self.raw.copy().load_data() raw.pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 241, 60), fir_design='firwin') raw.filter(110, 140, fir_design='firwin') raw.apply_hilbert(envelope=True) sfreq = raw.info['sfreq'] art_scores = stats.zscore(raw._data, axis=1) # band pass filter the data art_scores_filt = mne.filter.filter_data(art_scores.mean(axis=0), sfreq, None, 4) art_mask = art_scores_filt > thr # remove artifact free periods shorter than t_min idx_min = t_min * sfreq comps, num_comps = label(art_mask == 0) for l in range(1, num_comps+1): l_idx = np.nonzero(comps == l)[0] if len(l_idx) < idx_min: art_mask[l_idx] = True mus_annot = _annotations_from_mask(raw.times, art_mask, 'Bad-muscle') if plot: del raw print('Plotting data, mark or delete art, by pressing a \n' 'Marked or demarked channels will be saved') old_bd_chns = self.raw.info['bads'] raw = self.raw.copy().load_data().pick_types(meg=True, ref_meg=False) raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin') raw.filter(1, 140) old_annot = raw.annotations # if orig_time cant + none raw.set_annotations(mus_annot) raw.set_annotations(raw.annotations + old_annot) raw.plot(n_channels=140, block=True, bad_color='r') mus_annot = raw.annotations if not (old_bd_chns == raw.info['bads']): bad_chns = raw.info['bads'] print('Saving new bad channels list \n ') print('Bad chans are:', bad_chns) fname = self.subject + '_' + self.experiment + '_bads.csv' csv_save(bad_chns, op.join(self.out_bd_ch, fname)) mus_annot.save(out_csv_f) old_annot = self.raw.annotations # if orig_time cant + with none time self.raw.set_annotations(mus_annot) self.raw.set_annotations(self.raw.annotations + old_annot) self.annot_muscle = mus_annot
def test_compare_events_and_annotations(): """Test comparing annotations and events.""" with pytest.warns(RuntimeWarning, match='Could not parse meas date'): raw = read_raw_cnt(fname) events = np.array([[333, 0, 7], [1010, 0, 7], [1664, 0, 109], [2324, 0, 7], [2984, 0, 109]]) annot = read_annotations(fname) assert len(annot) == 6 assert_array_equal(annot.onset[:-1], events[:, 0] / raw.info['sfreq']) assert 'STI 014' not in raw.info['ch_names']
def test_compare_events_and_annotations(recwarn): """Test comparing annotations and events.""" from mne import find_events from numpy.testing import assert_array_equal raw = read_raw_cnt(input_fname=fname, montage=None, preload=False, stim_channel=True) events = find_events(raw) annot = read_annotations(fname) assert len(annot) == 6 assert_array_equal(annot.onset[:-1], events[:, 0] / raw.info['sfreq'])
def test_eeglab_event_from_annot(): """Test all forms of obtaining annotations.""" raw_fname_mat = op.join(base_dir, 'test_raw.set') raw_fname = raw_fname_mat event_id = {'rt': 1, 'square': 2} raw1 = read_raw_eeglab(input_fname=raw_fname, preload=False) annotations = read_annotations(raw_fname) assert len(raw1.annotations) == 154 raw1.set_annotations(annotations) events_b, _ = events_from_annotations(raw1, event_id=event_id) assert len(events_b) == 154
def test_eeglab_read_annotations(): """Test annotations onsets are timestamps (+ validate some).""" annotations = read_annotations(raw_fname_mat) validation_samples = [0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] expected_onset = np.array([1.00, 1.69, 2.08, 4.70, 7.71, 11.30, 17.18, 20.20, 26.12, 29.14, 35.25, 44.30, 47.15]) assert annotations.orig_time is None assert_array_almost_equal(annotations.onset[validation_samples], expected_onset, decimal=2) # test if event durations are imported correctly raw = read_raw_eeglab(raw_fname_event_duration, preload=True) # file contains 3 annotations with 0.5 s (64 samples) duration each assert_allclose(raw.annotations.duration, np.ones(3) * 0.5)
def test_compare_events_and_annotations(): """Test comparing annotations and events.""" with pytest.warns(RuntimeWarning, match='Could not parse meas date'): raw = read_raw_cnt(fname) events = np.array([[333, 0, 7], [1010, 0, 7], [1664, 0, 109], [2324, 0, 7], [2984, 0, 109]]) annot = read_annotations(fname) assert len(annot) == 6 assert_array_equal(annot.onset[:-1], events[:, 0] / raw.info['sfreq']) assert 'STI 014' not in raw.info['ch_names']
def test_eeglab_read_annotations(): """Test annotations onsets are timestamps (+ validate some).""" annotations = read_annotations(raw_fname_mat) validation_samples = [0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] expected_onset = np.array([1.00, 1.69, 2.08, 4.70, 7.71, 11.30, 17.18, 20.20, 26.12, 29.14, 35.25, 44.30, 47.15]) assert annotations.orig_time is None assert_array_almost_equal(annotations.onset[validation_samples], expected_onset, decimal=2) # test if event durations are imported correctly raw = read_raw_eeglab(raw_fname_event_duration, preload=True) # file contains 3 annotations with 0.5 s (64 samples) duration each assert_allclose(raw.annotations.duration, np.ones(3) * 0.5)
def test_eeglab_event_from_annot(): """Test all forms of obtaining annotations.""" base_dir = op.join(testing.data_path(download=False), 'EEGLAB') raw_fname_mat = op.join(base_dir, 'test_raw.set') raw_fname = raw_fname_mat montage = op.join(base_dir, 'test_chans.locs') event_id = {'rt': 1, 'square': 2} raw1 = read_raw_eeglab(input_fname=raw_fname, montage=montage, preload=False) annotations = read_annotations(raw_fname) assert len(raw1.annotations) == 154 raw1.set_annotations(annotations) events_b, _ = events_from_annotations(raw1, event_id=event_id) assert len(events_b) == 154
def test_eeglab_event_from_annot(): """Test all forms of obtaining annotations.""" base_dir = op.join(testing.data_path(download=False), 'EEGLAB') raw_fname_mat = op.join(base_dir, 'test_raw.set') raw_fname = raw_fname_mat montage = op.join(base_dir, 'test_chans.locs') event_id = {'rt': 1, 'square': 2} raw1 = read_raw_eeglab(input_fname=raw_fname, montage=montage, preload=False) annotations = read_annotations(raw_fname) assert len(raw1.annotations) == 154 raw1.set_annotations(annotations) events_b, _ = events_from_annotations(raw1, event_id=event_id) assert len(events_b) == 154
def test_read_curry_annotations(fname): """Test reading for Curry events file.""" EXPECTED_ONSET = [0.484, 0.486, 0.62, 0.622, 1.904, 1.906, 3.212, 3.214, 4.498, 4.5, 5.8, 5.802, 7.074, 7.076, 8.324, 8.326, 9.58, 9.582] EXPECTED_DURATION = np.zeros_like(EXPECTED_ONSET) EXPECTED_DESCRIPTION = ['4', '50000', '2', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000', '1', '50000'] annot = read_annotations(fname, sfreq='auto') assert annot.orig_time is None assert_array_equal(annot.onset, EXPECTED_ONSET) assert_array_equal(annot.duration, EXPECTED_DURATION) assert_array_equal(annot.description, EXPECTED_DESCRIPTION)
def test_find_events_and_events_from_annot_are_the_same(): """Test that old behaviour and new produce the same events.""" EXPECTED_EVENTS = [[68, 0, 2], [199, 0, 2], [1024, 0, 3], [1280, 0, 2]] raw = read_raw_edf(edf_path, preload=True, stim_channel='auto') raw_shell = _get_empty_raw_with_valid_annot(edf_path) assert raw_shell.info['meas_date'] == raw.info['meas_date'] assert raw_shell.info['sfreq'] == raw.info['sfreq'] assert raw_shell.first_samp == raw.first_samp events_from_find_events = find_events(raw) assert_array_equal(events_from_find_events, EXPECTED_EVENTS) annot = read_annotations(edf_path) raw_shell.set_annotations(annot) event_id = _get_edf_default_event_id(annot.description) event_id.pop('start') events_from_EFA, _ = events_from_annotations(raw_shell, event_id=event_id, use_rounding=False) assert_array_equal(events_from_EFA, events_from_find_events)
def test_find_events_and_events_from_annot_are_the_same(): """Test that old behaviour and new produce the same events.""" EXPECTED_EVENTS = [[68, 0, 2], [199, 0, 2], [1024, 0, 3], [1280, 0, 2]] raw = read_raw_edf(edf_path, preload=True, stim_channel='auto') raw_shell = _get_empty_raw_with_valid_annot(edf_path) assert raw_shell.info['meas_date'] == raw.info['meas_date'] assert raw_shell.info['sfreq'] == raw.info['sfreq'] assert raw_shell.first_samp == raw.first_samp events_from_find_events = find_events(raw) assert_array_equal(events_from_find_events, EXPECTED_EVENTS) annot = read_annotations(edf_path) raw_shell.set_annotations(annot) event_id = _get_edf_default_event_id(annot.description) event_id.pop('start') events_from_EFA, _ = events_from_annotations(raw_shell, event_id=event_id, use_rounding=False) assert_array_equal(events_from_EFA, events_from_find_events)
def test_eeglab_event_from_annot(): """Test all forms of obtaining annotations.""" base_dir = op.join(testing.data_path(download=False), 'EEGLAB') raw_fname_mat = op.join(base_dir, 'test_raw.set') raw_fname = raw_fname_mat montage = op.join(base_dir, 'test_chans.locs') event_id = {'rt': 1, 'square': 2} with pytest.deprecated_call(match='stim_channel'): raw1 = read_raw_eeglab(input_fname=raw_fname, montage=montage, event_id=event_id, preload=False) events_a = find_events(raw1) with pytest.deprecated_call(match='read_events_eeglab'): events_b = read_events_eeglab(raw_fname, event_id=event_id) annotations = read_annotations(raw_fname) assert len(raw1.annotations) == 154 raw1.set_annotations(annotations) events_c, _ = events_from_annotations(raw1, event_id=event_id) assert_array_equal(events_a, events_b) assert_array_equal(events_a, events_c)
def test_read_annotations(fname, recwarn): """Test IO of annotations from edf and bdf files via regexp.""" annot = read_annotations(fname) assert len(annot.onset) == 2
def test_read_annotations(fname, recwarn): """Test IO of annotations from edf and bdf files via regexp.""" annot = read_annotations(fname) assert len(annot.onset) == 2