示例#1
0
def test_annotate_flat(first_samp):
    """Test marking flat segments."""
    # Test if ECG analysis will work on data that is not preloaded
    n_ch, n_times = 11, 1000
    data = np.random.RandomState(0).randn(n_ch, n_times)
    assert not (np.diff(data, axis=-1) == 0).any()  # nothing flat at first
    info = create_info(n_ch, 1000., 'eeg')
    info['meas_date'] = (1, 2)
    # test first_samp != for gh-6295
    raw = RawArray(data, info, first_samp=first_samp)
    raw.info['bads'] = [raw.ch_names[-1]]

    #
    # First make a channel flat the whole time
    #
    raw_0 = raw.copy()
    raw_0._data[0] = 0.
    for kwargs, bads, want_times in [
            # Anything < 1 will mark spatially
        (dict(bad_percent=100.), [], 0),
        (dict(bad_percent=99.9), [raw.ch_names[0]], n_times),
        (dict(), [raw.ch_names[0]], n_times)
    ]:  # default (1)
        raw_time = raw_0.copy()
        annot, got_bads = annotate_flat(raw_0, verbose='debug', **kwargs)
        assert got_bads == bads
        raw_time.set_annotations(raw_time.annotations + annot)
        raw_time.info['bads'] += got_bads
        n_good_times = raw_time.get_data(reject_by_annotation='omit').shape[1]
        assert n_good_times == want_times
        with pytest.deprecated_call(match='annotate_flat'):
            raw_time_2 = mark_flat(raw_0.copy(), verbose='debug', **kwargs)
        _assert_annotations_equal(raw_time_2.annotations, raw_time.annotations)
        assert raw_time_2.info['bads'] == raw_time.info['bads']
    #
    # Now make a channel flat for 20% of the time points
    #
    raw_0 = raw.copy()
    n_good_times = int(round(0.8 * n_times))
    raw_0._data[0, n_good_times:] = 0.
    threshold = 100 * (n_times - n_good_times) / n_times
    for kwargs, bads, want_times in [
            # Should change behavior at bad_percent=20
        (dict(bad_percent=100), [], n_good_times),
        (dict(bad_percent=threshold), [], n_good_times),
        (dict(bad_percent=threshold - 1e-5), [raw.ch_names[0]], n_times),
        (dict(), [raw.ch_names[0]], n_times)
    ]:
        annot, got_bads = annotate_flat(raw_0, verbose='debug', **kwargs)
        assert got_bads == bads
        raw_time = raw_0.copy()
        raw_time.set_annotations(raw_time.annotations + annot)
        raw_time.info['bads'] += got_bads
        n_good_times = raw_time.get_data(reject_by_annotation='omit').shape[1]
        assert n_good_times == want_times

    with pytest.raises(TypeError, match='must be an instance of BaseRaw'):
        annotate_flat(0.)
    with pytest.raises(ValueError, match='not convert string to float'):
        annotate_flat(raw, 'x')
示例#2
0
def test_movement_annotation_head_correction(meas_date):
    """Test correct detection movement artifact and dev_head_t."""
    raw = read_raw_fif(raw_fname, allow_maxshield='yes').load_data()
    pos = read_head_pos(pos_fname)
    if meas_date is None:
        raw.set_meas_date(None)
    else:
        assert meas_date == 'orig'

    # Check 5 rotation segments are detected
    annot_rot, [] = annotate_movement(raw, pos, rotation_velocity_limit=5)
    assert annot_rot.orig_time == raw.info["meas_date"]
    assert annot_rot.duration.size == 5

    # Check 2 translation vel. segments are detected
    annot_tra, [] = annotate_movement(raw, pos, translation_velocity_limit=.05)
    assert annot_tra.duration.size == 2

    # Check 1 movement distance segment is detected
    annot_dis, _ = annotate_movement(raw, pos, mean_distance_limit=.02)
    assert annot_dis.duration.size == 1

    # Check correct trans mat
    annot_all_2 = annotate_movement(
        raw, pos, rotation_velocity_limit=5,
        translation_velocity_limit=.05,
        mean_distance_limit=.02)[0]
    assert (annot_rot.orig_time ==
            annot_tra.orig_time ==
            annot_dis.orig_time ==
            raw.info['meas_date'])
    annot_all = annot_rot + annot_tra + annot_dis
    _assert_annotations_equal(annot_all_2, annot_all)
    assert annot_all.orig_time == raw.info['meas_date']
    raw.set_annotations(annot_all)
    dev_head_t = compute_average_dev_head_t(raw, pos)

    dev_head_t_ori = np.array([
                              [0.9957292, -0.08688804, 0.03120615, 0.00698271],
                              [0.09020767, 0.9875856, -0.12859731, -0.0159098],
                              [-0.01964518, 0.1308631, 0.99120578, 0.07258289],
                              [0., 0., 0., 1.]])

    assert_allclose(dev_head_t_ori, dev_head_t['trans'], rtol=1e-5, atol=0)

    # Smoke test skipping time due to previous annotations.
    raw.set_annotations(Annotations([raw.times[0]], 0.1, 'bad'))
    annot_dis, _ = annotate_movement(raw, pos, mean_distance_limit=.02)
    assert annot_dis.duration.size == 1
示例#3
0
def test_read_annot(tmp_path):
    """Test parsing the tal channel."""
    EXPECTED_ANNOTATIONS = [[180.0, 0, 'Lights off'], [180.0, 0, 'Close door'],
                            [180.0, 0, 'Lights off'], [180.0, 0, 'Close door'],
                            [3.14, 4.2, 'nothing'], [1800.2, 25.5, 'Apnea']]

    EXPECTED_ONSET = [180.0, 180.0, 180.0, 180.0, 3.14, 1800.2]
    EXPECTED_DURATION = [0, 0, 0, 0, 4.2, 25.5]
    EXPECTED_DESC = [
        'Lights off', 'Close door', 'Lights off', 'Close door', 'nothing',
        'Apnea'
    ]
    EXPECTED_ANNOTATIONS = Annotations(onset=EXPECTED_ONSET,
                                       duration=EXPECTED_DURATION,
                                       description=EXPECTED_DESC,
                                       orig_time=None)

    annot = (b'+180\x14Lights off\x14Close door\x14\x00\x00\x00\x00\x00'
             b'+180\x14Lights off\x14\x00\x00\x00\x00\x00\x00\x00\x00'
             b'+180\x14Close door\x14\x00\x00\x00\x00\x00\x00\x00\x00'
             b'+3.14\x1504.20\x14nothing\x14\x00\x00\x00\x00'
             b'+1800.2\x1525.5\x14Apnea\x14\x00\x00\x00\x00\x00\x00\x00'
             b'+123\x14\x14\x00\x00\x00\x00\x00\x00\x00')
    annot_file = tmp_path / 'annotations.txt'
    with open(annot_file, "wb") as f:
        f.write(annot)

    onset, duration, desc = _read_annotations_edf(annotations=str(annot_file))
    annotation = Annotations(onset=onset,
                             duration=duration,
                             description=desc,
                             orig_time=None)
    _assert_annotations_equal(annotation, EXPECTED_ANNOTATIONS)

    # Now test when reading from buffer of data
    with open(annot_file, 'rb') as fid:
        ch_data = np.fromfile(fid, dtype='<i2', count=len(annot))
    onset, duration, desc = _read_annotations_edf([ch_data])
    annotation = Annotations(onset=onset,
                             duration=duration,
                             description=desc,
                             orig_time=None)
    _assert_annotations_equal(annotation, EXPECTED_ANNOTATIONS)
示例#4
0
def test_read_ctf_annotations_smoke_test():
    """Test reading CTF marker file.

    `testdata_ctf_mc.ds` has no trials or offsets therefore its a plain reading
    of whatever is in the MarkerFile.mrk.
    """
    EXPECTED_ONSET = [
        0., 0.1425, 0.285, 0.42833333, 0.57083333, 0.71416667, 0.85666667,
        0.99916667, 1.1425, 1.285, 1.4275, 1.57083333, 1.71333333, 1.85666667,
        1.99916667, 2.14166667, 2.285, 2.4275, 2.57083333, 2.71333333,
        2.85583333, 2.99916667, 3.14166667, 3.28416667, 3.4275, 3.57,
        3.71333333, 3.85583333, 3.99833333, 4.14166667, 4.28416667, 4.42666667,
        4.57, 4.7125, 4.85583333, 4.99833333
    ]
    fname = op.join(ctf_dir, 'testdata_ctf_mc.ds')
    annot = read_annotations(fname)
    assert_allclose(annot.onset, EXPECTED_ONSET)

    raw = read_raw_ctf(fname)
    _assert_annotations_equal(raw.annotations, annot)
示例#5
0
def test_no_data_channels():
    """Test that we can load with no data channels."""
    # analog
    raw = read_raw_edf(edf_path, preload=True)
    picks = pick_types(raw.info, stim=True)
    assert list(picks) == [len(raw.ch_names) - 1]
    stim_data = raw[picks][0]
    raw = read_raw_edf(edf_path, exclude=raw.ch_names[:-1])
    stim_data_2 = raw[0][0]
    assert_array_equal(stim_data, stim_data_2)
    raw.plot()  # smoke test
    # annotations
    raw = read_raw_edf(edf_overlap_annot_path)
    picks = pick_types(raw.info, stim=True)
    assert picks.size == 0
    annot = raw.annotations
    raw = read_raw_edf(edf_overlap_annot_path, exclude=raw.ch_names)
    annot_2 = raw.annotations
    _assert_annotations_equal(annot, annot_2)
    # only annotations (should warn)
    with pytest.warns(RuntimeWarning, match='read_annotations'):
        read_raw_edf(edf_annot_only)