Exemplo n.º 1
0
def test_ndvar_binning():
    "Test NDVar.bin()"
    x = np.arange(10)
    time = UTS(-0.1, 0.1, 10)
    x_dst = x.reshape((5, 2)).mean(1)
    time_dst = np.arange(0., 0.9, 0.2)

    # 1-d
    ndvar = NDVar(x, (time,))
    b = ndvar.bin(0.2)
    assert_array_equal(b.x, x_dst, "Binned data")
    assert_array_equal(b.time.x, time_dst, "Bin times")
    b = ndvar.sub(time=(0, 0.8)).bin(0.4)
    eq_(b.shape, (2,))

    # 2-d
    ndvar = NDVar(np.vstack((x, x, x)), ('case', time))
    b = ndvar.bin(0.2)
    assert_array_equal(b.x, np.vstack((x_dst, x_dst, x_dst)), "Binned data")
    assert_array_equal(b.time.x, time_dst, "Bin times")

    # time:
    x = np.ones((5, 70))
    ndvar = NDVar(x, ('case', UTS(0.45000000000000007, 0.005, 70)))
    binned_ndvar = ndvar.bin(0.05)
    assert_array_equal(binned_ndvar.x, 1.)
    eq_(binned_ndvar.shape, (5, 7))
Exemplo n.º 2
0
def test_ndvar_binning():
    "Test NDVar.bin()"
    x = np.arange(10)
    time = UTS(-0.1, 0.1, 10)
    x_dst = x.reshape((5, 2)).mean(1)
    time_dst = np.arange(0., 0.9, 0.2)

    # 1-d
    ndvar = NDVar(x, (time,))
    b = ndvar.bin(0.2)
    assert_array_equal(b.x, x_dst, "Binned data")
    assert_array_equal(b.time.x, time_dst, "Bin times")
    b = ndvar.sub(time=(0, 0.8)).bin(0.4)
    eq_(b.shape, (2,))

    # 2-d
    ndvar = NDVar(np.vstack((x, x, x)), ('case', time))
    b = ndvar.bin(0.2)
    assert_array_equal(b.x, np.vstack((x_dst, x_dst, x_dst)), "Binned data")
    assert_array_equal(b.time.x, time_dst, "Bin times")

    # time:
    x = np.ones((5, 70))
    ndvar = NDVar(x, ('case', UTS(0.45000000000000007, 0.005, 70)))
    binned_ndvar = ndvar.bin(0.05)
    assert_array_equal(binned_ndvar.x, 1.)
    eq_(binned_ndvar.shape, (5, 7))
Exemplo n.º 3
0
def test_mask():
    ds = datasets.get_uts(True)

    x = NDVar([1, 2, 3], Case)
    assert x.mean() == 2.0
    y = x.mask([True, False, False])
    assert y.mean() == 2.5

    # multi-dimensional
    y = ds[:2, 'utsnd'].copy()
    mask_x = y.time.times >= 0.500
    mask_ndvar = NDVar(mask_x, y.time)
    y_masked = y.mask(mask_ndvar)
    assert_array_equal(y_masked.x.mask[:, :, 70:], True)
    assert_array_equal(y_masked.x.mask[:, :, :70], False)
    # mask that is smaller than array
    mask = mask_ndvar.sub(time=(0.100, None))
    with pytest.raises(TypeError):
        y.mask(mask)
    y_masked = y.mask(mask, missing=True)
    assert_array_equal(y_masked.x.mask[:, :, 70:], True)
    assert_array_equal(y_masked.x.mask[:, :, 30:70], False)
    assert_array_equal(y_masked.x.mask[:, :, :30], True)
Exemplo n.º 4
0
def test_ndvar_indexing():
    ds = datasets.get_uts(utsnd=True)
    x = ds['utsnd']

    # case
    test_ndvar_index(x, 'case', 1, 1)
    test_ndvar_index(x, 'case', [0, 3], [0, 3])
    test_ndvar_index(x, 'case', slice(0, 10, 2), slice(0, 10, 2))

    # sensor
    test_ndvar_index(x, 'sensor', '0', 0)
    test_ndvar_index(x, 'sensor', ['0', '2'], [0, 2])
    test_ndvar_index(x, 'sensor', slice('0', '2'), slice(0, 2))
    test_ndvar_index(x, 'sensor', 0, 0, False)
    test_ndvar_index(x, 'sensor', [0, 2], [0, 2], False)
    test_ndvar_index(x, 'sensor', slice(0, 2), slice(0, 2), False)

    # time
    test_ndvar_index(x, 'time', 0, 20)
    test_ndvar_index(x, 'time', 0.1, 30)
    test_ndvar_index(x, 'time', 0.102, 30, False)
    test_ndvar_index(x, 'time', [0, 0.1, 0.2], [20, 30, 40])
    test_ndvar_index(x, 'time', slice(0.1, None), slice(30, None))
    test_ndvar_index(x, 'time', slice(0.2), slice(40))
    test_ndvar_index(x, 'time', slice(0.202), slice(41), False)
    test_ndvar_index(x, 'time', slice(0.1, 0.2), slice(30, 40))
    test_ndvar_index(x, 'time', slice(0.102, 0.2), slice(31, 40), False)
    test_ndvar_index(x, 'time', slice(0.1, None, 0.1), slice(30, None, 10))
    test_ndvar_index(x, 'time', slice(0.1, None, 1), slice(30, None, 100))

    # Ordered
    x = cwt_morlet(ds['uts'], [8, 10, 13, 17])
    assert_raises(IndexError, x.__getitem__, (full_slice, 9))
    assert_raises(IndexError, x.__getitem__, (full_slice, 6))
    test_ndvar_index(x, 'frequency', 10, 1)
    test_ndvar_index(x, 'frequency', 10.1, 1, False)
    test_ndvar_index(x, 'frequency', 9.9, 1, False)
    test_ndvar_index(x, 'frequency', [8.1, 10.1], [0, 1], False)
    test_ndvar_index(x, 'frequency', slice(8, 13), slice(0, 2))
    test_ndvar_index(x, 'frequency', slice(8, 13.1), slice(0, 3), False)
    test_ndvar_index(x, 'frequency', slice(8, 13.1, 2), slice(0, 3, 2), False)

    # Categorial
    x = NDVar(x.x, ('case', Categorial('cat', ['8', '10', '13', '17']), x.time))
    assert_raises(TypeError, x.__getitem__, (full_slice, 9))
    assert_raises(IndexError, x.__getitem__, (full_slice, '9'))
    test_ndvar_index(x, 'cat', '13', 2)
    test_ndvar_index(x, 'cat', ['8', '13'], [0, 2])
    test_ndvar_index(x, 'cat', slice('8', '13'), slice(0, 2))
    test_ndvar_index(x, 'cat', slice('8', None, 2), slice(0, None, 2))

    # SourceSpace
    x = datasets.get_mne_stc(True)
    assert_raises(TypeError, x.__getitem__, slice('insula-rh'))
    assert_raises(TypeError, x.__getitem__, slice('insula-lh', 'insula-rh'))
    assert_raises(TypeError, x.__getitem__, ('insula-lh', 'insula-rh'))
    test_ndvar_index(x, 'source', 'L90', 90)
    test_ndvar_index(x, 'source', 'R90', 642 + 90)
    test_ndvar_index(x, 'source', ['L90', 'R90'], [90, 642 + 90])
    test_ndvar_index(x, 'source', slice('L90', 'R90'), slice(90, 642 + 90))
    test_ndvar_index(x, 'source', 90, 90, False)
    test_ndvar_index(x, 'source', [90, 95], [90, 95], False)
    test_ndvar_index(x, 'source', slice(90, 95), slice(90, 95), False)
    test_ndvar_index(x, 'source', 'insula-lh', x.source.parc == 'insula-lh', False)
    test_ndvar_index(x, 'source', ('insula-lh', 'insula-rh'),
                     x.source.parc.isin(('insula-lh', 'insula-rh')), False)
    n_lh = x.source.parc.endswith('lh').sum()
    test_ndvar_index(x, 'source', 'lh', slice(n_lh), False)
    test_ndvar_index(x, 'source', 'rh', slice(n_lh, None), False)

    # argmax
    x.x[10, 10] = 20
    eq_(x.argmax(), ('L10', 0.1))
    eq_(x[('L10', 0.1)], 20)
    eq_(x.sub(source='L10').argmax(), 0.1)
    eq_(x.sub(time=0.1).argmax(), 'L10')