コード例 #1
0
def test_si_units():
    """Test that our scalings actually produce SI units."""
    scalings = _handle_default('scalings', None)
    units = _handle_default('units', None)
    # Add a bad one to test that we actually detect it
    assert 'csd_bad' not in scalings
    scalings['csd_bad'] = 1e5
    units['csd_bad'] = 'V/m²'
    assert set(scalings) == set(units)

    for key, scale in scalings.items():
        if key == 'csd_bad':
            with pytest.raises(KeyError, match='is not a channel type'):
                want_scale = _get_scaling(key, units[key])
        else:
            want_scale = _get_scaling(key, units[key])
            assert_allclose(scale, want_scale, rtol=1e-12)
コード例 #2
0
def test_get_data_units():
    """Test the "units" argument of get_data method."""
    # Test the unit conversion function
    assert _get_scaling('eeg', 'uV') == 1e6
    assert _get_scaling('eeg', 'dV') == 1e1
    assert _get_scaling('eeg', 'pV') == 1e12
    assert _get_scaling('mag', 'fT') == 1e15
    assert _get_scaling('grad', 'T/m') == 1
    assert _get_scaling('grad', 'T/mm') == 1e-3
    assert _get_scaling('grad', 'fT/m') == 1e15
    assert _get_scaling('grad', 'fT/cm') == 1e13
    assert _get_scaling('csd', 'uV/cm²') == 1e2

    fname = Path(__file__).parent / "data" / "test_raw.fif"
    raw = read_raw_fif(fname)

    last = np.array([4.63803098e-05, 7.66563736e-05, 2.71933595e-04])
    last_eeg = np.array([7.12207023e-05, 4.63803098e-05, 7.66563736e-05])
    last_grad = np.array([-3.85742192e-12,  9.64355481e-13, -1.06079103e-11])

    # None
    data_none = raw.get_data()
    assert data_none.shape == (376, 14400)
    assert_array_almost_equal(data_none[-3:, -1], last)

    # str: unit no conversion
    data_str_noconv = raw.get_data(picks=['eeg'], units='V')
    assert data_str_noconv.shape == (60, 14400)
    assert_array_almost_equal(data_str_noconv[-3:, -1], last_eeg)
    # str: simple unit
    data_str_simple = raw.get_data(picks=['eeg'], units='uV')
    assert data_str_simple.shape == (60, 14400)
    assert_array_almost_equal(data_str_simple[-3:, -1], last_eeg * 1e6)
    # str: fraction unit
    data_str_fraction = raw.get_data(picks=['grad'], units='fT/cm')
    assert data_str_fraction.shape == (204, 14400)
    assert_array_almost_equal(data_str_fraction[-3:, -1],
                              last_grad * (1e15 / 1e2))
    # str: more than one channel type but one with unit
    data_str_simplestim = raw.get_data(picks=['eeg', 'stim'], units='V')
    assert data_str_simplestim.shape == (69, 14400)
    assert_array_almost_equal(data_str_simplestim[-3:, -1], last_eeg)
    # str: too many channels
    with pytest.raises(ValueError, match='more than one channel'):
        raw.get_data(units='uV')
    # str: invalid unit
    with pytest.raises(ValueError, match='is not a valid unit'):
        raw.get_data(picks=['eeg'], units='fV/cm')

    # dict: combination of simple and fraction units
    data_dict = raw.get_data(units=dict(grad='fT/cm', mag='fT', eeg='uV'))
    assert data_dict.shape == (376, 14400)
    assert_array_almost_equal(data_dict[0, -1],
                              -3.857421923113974e-12 * (1e15 / 1e2))
    assert_array_almost_equal(data_dict[2, -1], -2.1478272253525944e-13 * 1e15)
    assert_array_almost_equal(data_dict[-2, -1], 7.665637356879529e-05 * 1e6)
    # dict: channel type not in instance
    data_dict_notin = raw.get_data(units=dict(hbo='uM'))
    assert data_dict_notin.shape == (376, 14400)
    assert_array_almost_equal(data_dict_notin[-3:, -1], last)
    # dict: one invalid unit
    with pytest.raises(ValueError, match='is not a valid unit'):
        raw.get_data(units=dict(grad='fT/cV', mag='fT', eeg='uV'))
    # dict: one invalid channel type
    with pytest.raises(KeyError, match='is not a channel type'):
        raw.get_data(units=dict(bad_type='fT/cV', mag='fT', eeg='uV'))

    # not the good type
    with pytest.raises(TypeError, match='instance of None, str, or dict'):
        raw.get_data(units=['fT/cm', 'fT', 'uV'])