def test_set_bipolar_reference():
    """Test bipolar referencing"""
    raw = Raw(fif_fname, preload=True)
    reref = set_bipolar_reference(
        raw, "EEG 001", "EEG 002", "bipolar", {"kind": FIFF.FIFFV_EOG_CH, "extra": "some extra value"}
    )
    assert_true(reref.info["custom_ref_applied"])

    # Compare result to a manual calculation
    a = raw.pick_channels(["EEG 001", "EEG 002"], copy=True)
    a = a._data[0, :] - a._data[1, :]
    b = reref.pick_channels(["bipolar"], copy=True)._data[0, :]
    assert_allclose(a, b)

    # Original channels should be replaced by a virtual one
    assert_true("EEG 001" not in reref.ch_names)
    assert_true("EEG 002" not in reref.ch_names)
    assert_true("bipolar" in reref.ch_names)

    # Check channel information
    bp_info = reref.info["chs"][reref.ch_names.index("bipolar")]
    an_info = reref.info["chs"][raw.ch_names.index("EEG 001")]
    for key in bp_info:
        if key == "loc":
            assert_array_equal(bp_info[key], 0)
        elif key == "coil_type":
            assert_equal(bp_info[key], FIFF.FIFFV_COIL_EEG_BIPOLAR)
        elif key == "kind":
            assert_equal(bp_info[key], FIFF.FIFFV_EOG_CH)
        else:
            assert_equal(bp_info[key], an_info[key])
    assert_equal(bp_info["extra"], "some extra value")

    # Minimalist call
    reref = set_bipolar_reference(raw, "EEG 001", "EEG 002")
    assert_true("EEG 001-EEG 002" in reref.ch_names)

    # Test creating a bipolar reference that doesn't involve EEG channels:
    # it should not set the custom_ref_applied flag
    reref = set_bipolar_reference(raw, "MEG 0111", "MEG 0112", ch_info={"kind": FIFF.FIFFV_MEG_CH})
    assert_true(not reref.info["custom_ref_applied"])
    assert_true("MEG 0111-MEG 0112" in reref.ch_names)

    # Test a battery of invalid inputs
    assert_raises(ValueError, set_bipolar_reference, raw, "EEG 001", ["EEG 002", "EEG 003"], "bipolar")
    assert_raises(ValueError, set_bipolar_reference, raw, ["EEG 001", "EEG 002"], "EEG 003", "bipolar")
    assert_raises(ValueError, set_bipolar_reference, raw, "EEG 001", "EEG 002", ["bipolar1", "bipolar2"])
    assert_raises(
        ValueError,
        set_bipolar_reference,
        raw,
        "EEG 001",
        "EEG 002",
        "bipolar",
        ch_info=[{"foo": "bar"}, {"foo": "bar"}],
    )
    assert_raises(ValueError, set_bipolar_reference, raw, "EEG 001", "EEG 002", ch_name="EEG 003")
Exemple #2
0
def test_set_bipolar_reference():
    """Test bipolar referencing"""
    raw = Raw(fif_fname, preload=True)
    reref = set_bipolar_reference(raw, 'EEG 001', 'EEG 002', 'bipolar',
                                  {'kind': FIFF.FIFFV_EOG_CH,
                                   'extra': 'some extra value'})
    assert_true(reref.info['custom_ref_applied'])

    # Compare result to a manual calculation
    a = raw.pick_channels(['EEG 001', 'EEG 002'], copy=True)
    a = a._data[0, :] - a._data[1, :]
    b = reref.pick_channels(['bipolar'], copy=True)._data[0, :]
    assert_allclose(a, b)

    # Original channels should be replaced by a virtual one
    assert_true('EEG 001' not in reref.ch_names)
    assert_true('EEG 002' not in reref.ch_names)
    assert_true('bipolar' in reref.ch_names)

    # Check channel information
    bp_info = reref.info['chs'][reref.ch_names.index('bipolar')]
    an_info = reref.info['chs'][raw.ch_names.index('EEG 001')]
    for key in bp_info:
        if key == 'loc':
            assert_array_equal(bp_info[key], 0)
        elif key == 'coil_type':
            assert_equal(bp_info[key], FIFF.FIFFV_COIL_EEG_BIPOLAR)
        elif key == 'kind':
            assert_equal(bp_info[key], FIFF.FIFFV_EOG_CH)
        else:
            assert_equal(bp_info[key], an_info[key])
    assert_equal(bp_info['extra'], 'some extra value')

    # Minimalist call
    reref = set_bipolar_reference(raw, 'EEG 001', 'EEG 002')
    assert_true('EEG 001-EEG 002' in reref.ch_names)

    # Test creating a bipolar reference that doesn't involve EEG channels:
    # it should not set the custom_ref_applied flag
    reref = set_bipolar_reference(raw, 'MEG 0111', 'MEG 0112',
                                  ch_info={'kind': FIFF.FIFFV_MEG_CH})
    assert_true(not reref.info['custom_ref_applied'])
    assert_true('MEG 0111-MEG 0112' in reref.ch_names)

    # Test a battery of invalid inputs
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', ['EEG 002', 'EEG 003'], 'bipolar')
    assert_raises(ValueError, set_bipolar_reference, raw,
                  ['EEG 001', 'EEG 002'], 'EEG 003', 'bipolar')
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', ['bipolar1', 'bipolar2'])
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', 'bipolar',
                  ch_info=[{'foo': 'bar'}, {'foo': 'bar'}])
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', ch_name='EEG 003')
def test_set_bipolar_reference():
    """Test bipolar referencing"""
    raw = Raw(fif_fname, preload=True)
    reref = set_bipolar_reference(raw, 'EEG 001', 'EEG 002', 'bipolar',
                                  {'kind': FIFF.FIFFV_EOG_CH,
                                   'extra': 'some extra value'})
    assert_true(reref.info['custom_ref_applied'])

    # Compare result to a manual calculation
    a = raw.copy().pick_channels(['EEG 001', 'EEG 002'])
    a = a._data[0, :] - a._data[1, :]
    b = reref.copy().pick_channels(['bipolar'])._data[0, :]
    assert_allclose(a, b)

    # Original channels should be replaced by a virtual one
    assert_true('EEG 001' not in reref.ch_names)
    assert_true('EEG 002' not in reref.ch_names)
    assert_true('bipolar' in reref.ch_names)

    # Check channel information
    bp_info = reref.info['chs'][reref.ch_names.index('bipolar')]
    an_info = reref.info['chs'][raw.ch_names.index('EEG 001')]
    for key in bp_info:
        if key == 'loc':
            assert_array_equal(bp_info[key], 0)
        elif key == 'coil_type':
            assert_equal(bp_info[key], FIFF.FIFFV_COIL_EEG_BIPOLAR)
        elif key == 'kind':
            assert_equal(bp_info[key], FIFF.FIFFV_EOG_CH)
        else:
            assert_equal(bp_info[key], an_info[key])
    assert_equal(bp_info['extra'], 'some extra value')

    # Minimalist call
    reref = set_bipolar_reference(raw, 'EEG 001', 'EEG 002')
    assert_true('EEG 001-EEG 002' in reref.ch_names)

    # Test creating a bipolar reference that doesn't involve EEG channels:
    # it should not set the custom_ref_applied flag
    reref = set_bipolar_reference(raw, 'MEG 0111', 'MEG 0112',
                                  ch_info={'kind': FIFF.FIFFV_MEG_CH})
    assert_true(not reref.info['custom_ref_applied'])
    assert_true('MEG 0111-MEG 0112' in reref.ch_names)

    # Test a battery of invalid inputs
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', ['EEG 002', 'EEG 003'], 'bipolar')
    assert_raises(ValueError, set_bipolar_reference, raw,
                  ['EEG 001', 'EEG 002'], 'EEG 003', 'bipolar')
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', ['bipolar1', 'bipolar2'])
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', 'bipolar',
                  ch_info=[{'foo': 'bar'}, {'foo': 'bar'}])
    assert_raises(ValueError, set_bipolar_reference, raw,
                  'EEG 001', 'EEG 002', ch_name='EEG 003')
Exemple #4
0
def set_eog_ecg_channels(raw):
    """Set the HCP ECG and EOG channels

    Operates in place.

    Parameters
    ----------
    raw : instance of Raw
        the hcp raw data.
    """
    for kind in ['ECG', 'VEOG', 'HEOG']:
        set_bipolar_reference(
            raw, anode=kind + '-', cathode=kind + '+', ch_name=kind,
            copy=False)
    raw.set_channel_types({'ECG': 'ecg', 'VEOG': 'eog', 'HEOG': 'eog'})
Exemple #5
0
def set_eog_ecg_channels(raw):
    """Set the HCP ECG and EOG channels

    .. note::
       Operates in place.

    Parameters
    ----------
    raw : instance of Raw
        the hcp raw data.
    """
    for kind in ['ECG', 'VEOG', 'HEOG']:
        set_bipolar_reference(
            raw, anode=kind + '-', cathode=kind + '+', ch_name=kind,
            copy=False)
    raw.set_channel_types({'ECG': 'ecg', 'VEOG': 'eog', 'HEOG': 'eog'})