Exemplo n.º 1
0
def test_t_contrast():
    ds = datasets.get_uts()

    # simple contrast
    res = testnd.t_contrast_rel('uts', 'A', 'a1>a0', 'rm', ds=ds, samples=10,
                                pmin=0.05)
    repr(res)
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)
    assert_in('samples', repr(res))

    # complex contrast
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a0|b0>a1|b0, a0|b1>a1|b1)',
                                'rm', ds=ds, samples=10, pmin=0.05)
    res_b0 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b0'), ('a1', 'b0'), 'rm',
                              ds=ds)
    res_b1 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b1'), ('a1', 'b1'), 'rm',
                              ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a1|b0>a0|b0, a1|b1>a0|b1)',
                                'rm', ds=ds, tail=1)
Exemplo n.º 2
0
def test_t_contrast():
    ds = datasets.get_uts()

    # simple contrast
    res = testnd.t_contrast_rel('uts', 'A', 'a1>a0', 'rm', ds=ds, samples=10,
                                pmin=0.05)
    repr(res)
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)
    assert_in('samples', repr(res))

    # complex contrast
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a0|b0>a1|b0, a0|b1>a1|b1)',
                                'rm', ds=ds, samples=10, pmin=0.05)
    res_b0 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b0'), ('a1', 'b0'), 'rm',
                              ds=ds)
    res_b1 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b1'), ('a1', 'b1'), 'rm',
                              ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a1|b0>a0|b0, a1|b1>a0|b1)',
                                'rm', ds=ds, tail=1)
Exemplo n.º 3
0
def test_t_contrast():
    ds = datasets.get_uts()

    # simple contrast
    res = testnd.t_contrast_rel('uts', 'A', 'a1>a0', 'rm', ds=ds, samples=10, pmin=0.05)
    assert repr(res) == "<t_contrast_rel 'uts', 'A', 'a1>a0', match='rm', samples=10, pmin=0.05, 7 clusters, p < .001>"
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)

    # complex contrast
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a0|b0>a1|b0, a0|b1>a1|b1)', 'rm', ds=ds, samples=10, pmin=0.05)
    res_b0 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b0'), ('a1', 'b0'), 'rm', ds=ds)
    res_b1 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b1'), ('a1', 'b1'), 'rm', ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a1|b0>a0|b0, a1|b1>a0|b1)',
                                'rm', ds=ds, tail=1)

    # zero variance
    ds['uts'].x[:, 10] = 0.
    with pytest.raises(ZeroVariance):
        testnd.t_contrast_rel('uts', 'A%B', 'min(a1|b0>a0|b0, a1|b1>a0|b1)', 'rm', tail=1, ds=ds)
Exemplo n.º 4
0
def test_t_contrast():
    ds = datasets.get_uts()

    # simple contrast
    res = testnd.t_contrast_rel('uts',
                                'A',
                                'a1>a0',
                                'rm',
                                ds=ds,
                                samples=10,
                                pmin=0.05)
    assert repr(
        res
    ) == "<t_contrast_rel 'uts', 'A', 'a1>a0', match='rm', samples=10, pmin=0.05, 7 clusters, p < .001>"
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)

    # complex contrast
    res = testnd.t_contrast_rel('uts',
                                'A%B',
                                'min(a0|b0>a1|b0, a0|b1>a1|b1)',
                                'rm',
                                ds=ds,
                                samples=10,
                                pmin=0.05)
    res_b0 = testnd.ttest_rel('uts',
                              'A%B', ('a0', 'b0'), ('a1', 'b0'),
                              'rm',
                              ds=ds)
    res_b1 = testnd.ttest_rel('uts',
                              'A%B', ('a0', 'b1'), ('a1', 'b1'),
                              'rm',
                              ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    res = testnd.t_contrast_rel('uts',
                                'A%B',
                                'min(a1|b0>a0|b0, a1|b1>a0|b1)',
                                'rm',
                                ds=ds,
                                tail=1,
                                samples=0)

    # zero variance
    ds['uts'].x[:, 10] = 0.
    with pytest.raises(ZeroVariance):
        testnd.t_contrast_rel('uts',
                              'A%B',
                              'min(a1|b0>a0|b0, a1|b1>a0|b1)',
                              'rm',
                              tail=1,
                              ds=ds,
                              samples=0)
Exemplo n.º 5
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_uts(True)

    # basic
    res = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                           ds=ds, samples=100)
    eq_(repr(res), "<ttest_rel 'uts', 'A x B', ('a1', 'b1'), ('a0', 'b0'), "
                   "'rm' (n=15), samples=100, p >= 0.000>")

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    repr(res_)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_less(res2.p_uncorrected.min(), 0.05)
    assert_equal(res2.n, res.n)

    # reproducibility
    res3 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                            ds=ds, samples=100)
    assert_dataset_equal(res3.find_clusters(maps=True), res.clusters)
    testnd.configure(0)
    res4 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                            ds=ds, samples=100)
    assert_dataset_equal(res4.find_clusters(maps=True), res.clusters)
    testnd.configure(-1)
    sds = ds.sub("B=='b0'")
    # thresholded, UTS
    testnd.configure(0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # thresholded, UTSND
    testnd.configure(0)
    res0 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # TFCE, UTS
    testnd.configure(0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True,
                            samples=10)
    tgt = res0.compute_probability_map()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True,
                            samples=10)
    assert_dataobj_equal(res1.compute_probability_map(), tgt)
Exemplo n.º 6
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_uts(True)

    # basic
    res = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                           ds=ds, samples=100)
    repr(res)

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    repr(res_)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_less(res2.p_uncorrected.min(), 0.05)
    assert_equal(res2.n, res.n)

    # reproducibility
    res3 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                            ds=ds, samples=100)
    assert_dataset_equal(res3.find_clusters(maps=True), res.clusters)
    testnd.configure(0)
    res4 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm',
                            ds=ds, samples=100)
    assert_dataset_equal(res4.find_clusters(maps=True), res.clusters)
    testnd.configure(-1)
    sds = ds.sub("B=='b0'")
    # thresholded, UTS
    testnd.configure(0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # thresholded, UTSND
    testnd.configure(0)
    res0 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # TFCE, UTS
    testnd.configure(0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True,
                            samples=10)
    tgt = res0.compute_probability_map()
    testnd.configure(-1)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True,
                            samples=10)
    assert_dataobj_equal(res1.compute_probability_map(), tgt)
Exemplo n.º 7
0
def test_t_contrast():
    ds = datasets.get_uts()

    # test aux functions
    y = np.arange(9.).reshape((3, 3))
    indexes = {'a': 0, 'b': 1, 'c': 2}

    contrast = "+sum(a>c, b>c)"
    contrast_ = _testnd._parse_t_contrast(contrast)
    assert_equal(contrast_, ('func', '+', np.sum, [('comp', None, 'a', 'c'),
                                                   ('comp', None, 'b', 'c')]))

    contrast = "+sum(a>*, b>*)"
    contrast_ = _testnd._parse_t_contrast(contrast)
    assert_equal(contrast_, ('func', '+', np.sum, [('comp', None, 'a', '*'),
                                                   ('comp', None, 'b', '*')]))
    _, cells = _testnd._t_contrast_rel_properties(contrast_)
    pc, mc = _testnd._t_contrast_rel_expand_cells(cells, ('a', 'b', 'c'))
    data = _testnd._t_contrast_rel_data(y, indexes, pc, mc)
    assert_array_equal(data['a'], np.arange(3.))
    assert_array_equal(data['*'], y.mean(0))

    assert_raises(ValueError, _testnd._t_contrast_rel_expand_cells, cells,
                  ('a|c', 'b|c', 'c|c'))

    # simple contrast
    res = testnd.t_contrast_rel('uts', 'A', 'a1>a0', 'rm', ds=ds, samples=10,
                                pmin=0.05)
    repr(res)
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)
    assert_in('samples', repr(res))

    # complex contrast
    res = testnd.t_contrast_rel('uts', 'A%B', 'min(a0|b0>a1|b0, a0|b1>a1|b1)',
                                'rm', ds=ds, samples=10, pmin=0.05)
    res_b0 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b0'), ('a1', 'b0'), 'rm',
                              ds=ds)
    res_b1 = testnd.ttest_rel('uts', 'A%B', ('a0', 'b1'), ('a1', 'b1'), 'rm',
                              ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    contrast_star = '+min(a1|b0>a0|*, a1|b1>a0|*)'
    res = testnd.t_contrast_rel('uts', 'A%B', contrast_star, 'rm', ds=ds)
Exemplo n.º 8
0
def test_merged_temporal_cluster_dist():
    "Test use of _MergedTemporalClusterDist with testnd test results"
    ds1 = datasets.get_uts()
    ds2 = datasets.get_uts(seed=42)

    anova_kw = dict(Y='uts', X='A*B*rm', pmin=0.05, samples=10)
    ttest_kw = dict(Y='uts', X='A', c1='a1', c0='a0', pmin=0.05, samples=10)
    contrast_kw = dict(Y='uts', X='A', contrast='a1>a0', pmin=0.05, samples=10)

    def test_merged(res1, res2):
        merged_dist = _MergedTemporalClusterDist([res1._cdist, res2._cdist])
        if isinstance(res1, testnd.anova):
            assert_equal(len(merged_dist.dist), len(res1.effects))
            for effect, dist in merged_dist.dist.iteritems():
                assert_in(effect, res1.effects)
                assert_equal(len(dist), res1.samples)
        else:
            assert_equal(len(merged_dist.dist), res1.samples)
        res1_clusters = merged_dist.correct_cluster_p(res1)
        res2_clusters = merged_dist.correct_cluster_p(res2)
        for clusters in [res1_clusters, res2_clusters]:
            assert_in('p_parc', clusters)
            for cl in clusters.itercases():
                assert_greater_equal(cl['p_parc'], cl['p'])

    # multi-effect
    res1 = testnd.anova(ds=ds1, **anova_kw)
    res2 = testnd.anova(ds=ds2, **anova_kw)
    test_merged(res1, res2)

    # ttest_rel
    res1 = testnd.ttest_rel(ds=ds1, match='rm', **ttest_kw)
    res2 = testnd.ttest_rel(ds=ds2, match='rm', **ttest_kw)
    test_merged(res1, res2)

    # ttest_ind
    res1 = testnd.ttest_ind(ds=ds1, **ttest_kw)
    res2 = testnd.ttest_ind(ds=ds2, **ttest_kw)
    test_merged(res1, res2)

    # ttest_1samp
    res1 = testnd.ttest_1samp('uts', ds=ds1, pmin=0.05, samples=10)
    res2 = testnd.ttest_1samp('uts', ds=ds2, pmin=0.05, samples=10)
    test_merged(res1, res2)

    # t_contrast_rel
    res1 = testnd.t_contrast_rel(ds=ds1, match='rm', **contrast_kw)
    res2 = testnd.t_contrast_rel(ds=ds2, match='rm', **contrast_kw)
    test_merged(res1, res2)
Exemplo n.º 9
0
def test_merged_temporal_cluster_dist():
    "Test use of _MergedTemporalClusterDist with testnd test results"
    ds1 = datasets.get_uts()
    ds2 = datasets.get_uts(seed=42)

    anova_kw = dict(Y='uts', X='A*B*rm', pmin=0.05, samples=10)
    ttest_kw = dict(Y='uts', X='A', c1='a1', c0='a0', pmin=0.05, samples=10)
    contrast_kw = dict(Y='uts', X='A', contrast='a1>a0', pmin=0.05, samples=10)

    def test_merged(res1, res2):
        merged_dist = _MergedTemporalClusterDist([res1._cdist, res2._cdist])
        if isinstance(res1, testnd.anova):
            assert_equal(len(merged_dist.dist), len(res1.effects))
            for effect, dist in merged_dist.dist.iteritems():
                assert_in(effect, res1.effects)
                assert_equal(len(dist), res1.samples)
        else:
            assert_equal(len(merged_dist.dist), res1.samples)
        res1_clusters = merged_dist.correct_cluster_p(res1)
        res2_clusters = merged_dist.correct_cluster_p(res2)
        for clusters in [res1_clusters, res2_clusters]:
            assert_in('p_parc', clusters)
            for cl in clusters.itercases():
                assert_greater_equal(cl['p_parc'], cl['p'])

    # multi-effect
    res1 = testnd.anova(ds=ds1, **anova_kw)
    res2 = testnd.anova(ds=ds2, **anova_kw)
    test_merged(res1, res2)

    # ttest_rel
    res1 = testnd.ttest_rel(ds=ds1, match='rm', **ttest_kw)
    res2 = testnd.ttest_rel(ds=ds2, match='rm', **ttest_kw)
    test_merged(res1, res2)

    # ttest_ind
    res1 = testnd.ttest_ind(ds=ds1, **ttest_kw)
    res2 = testnd.ttest_ind(ds=ds2, **ttest_kw)
    test_merged(res1, res2)

    # ttest_1samp
    res1 = testnd.ttest_1samp('uts', ds=ds1, pmin=0.05, samples=10)
    res2 = testnd.ttest_1samp('uts', ds=ds2, pmin=0.05, samples=10)
    test_merged(res1, res2)

    # t_contrast_rel
    res1 = testnd.t_contrast_rel(ds=ds1, match='rm', **contrast_kw)
    res2 = testnd.t_contrast_rel(ds=ds2, match='rm', **contrast_kw)
    test_merged(res1, res2)
Exemplo n.º 10
0
def test_uts_stat():
    "test plot.UTSStat plotting function"
    ds = datasets.get_uts()
    p = plot.UTSStat('uts', ds=ds, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', ds=ds, show=False)
    p.plot_legend('lower right')
    p.plot_legend(False)
    pl = p.plot_legend('fig')
    p.plot_legend('center')
    pl.close()
    p.close()
    p = plot.UTSStat('uts', 'A', Xax='B', ds=ds, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds,
                     show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds,
                     pool_error=False, show=False)
    p.close()

    # error
    p = plot.UTSStat('uts', 'A', match='rm', ds=ds, error=False, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A', match='rm', ds=ds, error='all', show=False)
    p.close()

    # clusters
    sds = ds.sub("B == 'b0'")
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=0, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', ds=ds, show=False)
    p.set_clusters(res.clusters)
    p.close()
    p = plot.UTSStat('uts', 'A', ds=ds, clusters=res.clusters, show=False)
    p.close()
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=100, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', ds=ds, clusters=res.clusters, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A', 'B', ds=ds, clusters=res.clusters, show=False)
    p.set_clusters(None)
    p.set_clusters(res.clusters, ax=0)
    p.close()
    p = plot.UTSStat('uts', 'A', 'B', ds=ds, show=False)
    p.set_clusters(res.clusters)
    p.set_clusters(None, ax=1)
    p.close()
Exemplo n.º 11
0
def test_uts_stat():
    "test plot.UTSStat plotting function"
    ds = datasets.get_uts()
    p = plot.UTSStat('uts', ds=ds, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', ds=ds, show=False)
    p.plot_legend('lower right')
    p.plot_legend(False)
    pl = p.plot_legend('fig')
    p.plot_legend('center')
    pl.close()
    p.close()
    p = plot.UTSStat('uts', 'A', xax='B', ds=ds, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds,
                     show=False)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds,
                     pool_error=False, show=False)
    p.close()

    # error
    p = plot.UTSStat('uts', 'A', match='rm', ds=ds, error=False, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A', match='rm', ds=ds, error='all', show=False)
    p.close()

    # clusters
    sds = ds.sub("B == 'b0'")
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=0, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', ds=ds, show=False)
    p.set_clusters(res.clusters)
    p.close()
    p = plot.UTSStat('uts', 'A', ds=ds, clusters=res.clusters, show=False)
    p.close()
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=100, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', ds=ds, clusters=res.clusters, show=False)
    p.close()
    p = plot.UTSStat('uts', 'A', 'B', ds=ds, clusters=res.clusters, show=False)
    p.set_clusters(None)
    p.set_clusters(res.clusters, ax=0)
    p.close()
    p = plot.UTSStat('uts', 'A', 'B', ds=ds, show=False)
    p.set_clusters(res.clusters)
    p.set_clusters(None, ax=1)
    p.close()
Exemplo n.º 12
0
def test_t_contrast_testnd():
    ds = datasets.get_uts()

    # binary function
    res = testnd.t_contrast_rel('uts',
                                'A',
                                "a1>a0 - a0>a1",
                                'rm',
                                ds=ds,
                                tmin=4,
                                samples=10)
    assert_equal(res.find_clusters()['p'],
                 np.array([1, 1, 0.9, 0, 0.2, 1, 1, 0]))
    res_t = testnd.ttest_rel('uts',
                             'A',
                             'a1',
                             'a0',
                             match='rm',
                             ds=ds,
                             tmin=2,
                             samples=10)
    assert_array_equal(res.t.x, res_t.t.x * 2)
    assert_array_equal(res.clusters['tstart'], res_t.clusters['tstart'])
    assert_array_equal(res.clusters['tstop'], res_t.clusters['tstop'])
    assert_array_equal(res.clusters['v'], res_t.clusters['v'] * 2)
Exemplo n.º 13
0
def test_cwt():
    "Test tests with wavelet transform"
    ds = datasets.get_uts(True)
    ds['cwt'] = cwt_morlet(ds['utsnd'], np.arange(10, 20))
    res = testnd.ttest_rel('cwt', 'A', match='rm', ds=ds, pmin=0.05, samples=10)
    cluster = res.clusters.sub("p == 0")
    assert_array_equal(cluster['frequency_min'], 10)
    assert_array_equal(cluster['frequency_max'], 19)
Exemplo n.º 14
0
def test_cwt():
    "Test tests with wavelet transform"
    ds = datasets.get_uts(True)
    ds['cwt'] = cwt_morlet(ds['utsnd'], np.arange(10, 20))
    res = testnd.ttest_rel('cwt', 'A', match='rm', ds=ds, pmin=0.05, samples=10)
    cluster = res.clusters.sub("p == 0")
    assert_array_equal(cluster['frequency_min'], 10)
    assert_array_equal(cluster['frequency_max'], 19)
Exemplo n.º 15
0
def test_t_contrast_testnd():
    ds = datasets.get_uts()

    # binary function
    res = testnd.t_contrast_rel("uts", "A", "a1>a0 - a0>a1", "rm", ds=ds, tmin=4, samples=10)
    assert_equal(res.find_clusters()["p"], np.array([1, 1, 0.9, 0, 0.2, 1, 1, 0]))
    res_t = testnd.ttest_rel("uts", "A", "a1", "a0", match="rm", ds=ds, tmin=2, samples=10)
    assert_array_equal(res.t.x, res_t.t.x * 2)
    assert_array_equal(res.clusters["tstart"], res_t.clusters["tstart"])
    assert_array_equal(res.clusters["tstop"], res_t.clusters["tstop"])
    assert_array_equal(res.clusters["v"], res_t.clusters["v"] * 2)
Exemplo n.º 16
0
def test_t_contrast_testnd():
    ds = datasets.get_uts()

    # binary function
    res = testnd.t_contrast_rel('uts', 'A', "a1>a0 - a0>a1", 'rm', ds=ds, tmin=4, samples=10)
    assert_equal(res.find_clusters()['p'], np.array([1, 1, 0.9, 0, 0.2, 1, 1, 0]))
    res_t = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=ds, tmin=2, samples=10)
    assert_array_equal(res.t.x, res_t.t.x * 2)
    assert_array_equal(res.clusters['tstart'], res_t.clusters['tstart'])
    assert_array_equal(res.clusters['tstop'], res_t.clusters['tstop'])
    assert_array_equal(res.clusters['v'], res_t.clusters['v'] * 2)
Exemplo n.º 17
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_rand()

    # basic
    res = testnd.ttest_rel('uts',
                           'A%B', ('a1', 'b1'), ('a0', 'b0'),
                           'rm',
                           ds=ds)
    repr(res)

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    repr(res_)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_less(res2.p_uncorrected.min(), 0.05)
    assert_equal(res2.n, res.n)
Exemplo n.º 18
0
def test_stat():
    "test plot.UTSStat plotting function"
    plot.configure_backend(False, False)
    ds = datasets.get_rand()
    p = plot.UTSStat('uts', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A%B', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A', Xax='B', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds)
    p.close()

    # clusters
    sds = ds.sub("B == 'b0'")
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=0, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', clusters=res.clusters, ds=ds)
    p.close()
    res = testnd.ttest_rel('uts', 'A', 'a1', 'a0', match='rm', ds=sds,
                           samples=100, pmin=0.05, mintime=0.02)
    p = plot.UTSStat('uts', 'A', clusters=res.clusters, ds=ds)
    p.close()
Exemplo n.º 19
0
def test_stat():
    "test plot.UTSStat plotting function"
    plot.configure_backend(False, False)
    ds = datasets.get_rand()
    p = plot.UTSStat('uts', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A%B', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A', Xax='B', ds=ds)
    p.close()
    p = plot.UTSStat('uts', 'A%B', 'rm', sub="rm.isin(('R00', 'R01'))", ds=ds)
    p.close()

    # clusters
    sds = ds.sub("B == 'b0'")
    res = testnd.ttest_rel('uts',
                           'A',
                           'a1',
                           'a0',
                           match='rm',
                           ds=sds,
                           samples=0,
                           pmin=0.05,
                           mintime=0.02)
    p = plot.UTSStat('uts', 'A', clusters=res.clusters, ds=ds)
    p.close()
    res = testnd.ttest_rel('uts',
                           'A',
                           'a1',
                           'a0',
                           match='rm',
                           ds=sds,
                           samples=100,
                           pmin=0.05,
                           mintime=0.02)
    p = plot.UTSStat('uts', 'A', clusters=res.clusters, ds=ds)
    p.close()
Exemplo n.º 20
0
def test_result_report():
    "Test result_report function for different Results"
    ds = datasets.get_uts(True)
    sds = ds.sub("B == 'b0'")

    for y in (
            'uts',  # time
            "utsnd.summary(time=(0.25, 0.35))",  # sensor
            'utsnd',  # sensor x time
    ):
        dims = tuple(dim.name for dim in ds.eval(y).dims[1:])
        logging.info("y=%s %s", y, dims)
        kwargs = dict(pmin=0.1, samples=100)
        if 'time' in dims:
            kwargs['tstart'] = 0.2
            kwargs['tstop'] = 0.4

        for match in (None, 'rm'):
            logging.info("    match=%s", match)
            res = testnd.ttest_1samp(y, match=match, ds=sds, **kwargs)
            rep = result_report(res, ds)
            html(rep)

        res = testnd.ttest_ind(y, 'A', ds=sds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.ttest_rel(y, 'A', ds=sds, match='rm', **kwargs)
        rep = result_report(res, sds)
        html(rep)

        res = testnd.anova(y, 'A * B', ds=ds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.anova(y, 'A * rm', ds=sds, match='rm', **kwargs)
        rep = result_report(res, ds)
        html(rep)
Exemplo n.º 21
0
def test_result_report():
    "Test result_report function for different Results"
    ds = datasets.get_uts(True)
    sds = ds.sub("B == 'b0'")
    ys = [
        'uts',  # time
        "utsnd.summary(time=(0.25, 0.35))",  # sensor
        'utsnd',  # sensor x time
    ]

    for y in ys:
        y_obj = sds.eval(y)
        kwargs = dict(pmin=0.1, samples=100)
        if y_obj.has_dim('time'):
            kwargs['tstart'] = 0.2
            kwargs['tstop'] = 0.4

        for match in (None, 'rm'):
            logging.info("    match=%s", match)
            res = testnd.ttest_1samp(y, match=match, ds=sds, **kwargs)
            rep = result_report(res, ds)
            html(rep)

        res = testnd.ttest_ind(y, 'A', ds=sds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.ttest_rel(y, 'A', ds=sds, match='rm', **kwargs)
        rep = result_report(res, sds)
        html(rep)

        res = testnd.anova(y, 'A * B', ds=ds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.anova(y, 'A * rm', ds=sds, match='rm', **kwargs)
        rep = result_report(res, ds)
        html(rep)
Exemplo n.º 22
0
def reg_clusters_t_src(mod, IV, factor=None, c1=None, c0=None):
    """ Cluster permutation (src x time) t-tests on regression coefficients.
    Example: mod='main-VerbGivenWord', IV='LogFre'
    """

    dr = '/Volumes/Backup/sufAmb/regression/'
    f = dr+'ols_'+mod+'.pickled'
    data = load.unpickle(f)
    cond = "predictor=='%s'" % IV
    data = data.sub(cond)  # smaller ds with only the predictor of interest

    # cluster permutation parameters
    Y='beta'
    match='subject'
    samples=100
    pmin=0.05
    tstart=0.13
    tstop=0.45
    mintime=0.03
    minsource=10

    if factor is None:
        test = testnd.ttest_1samp(Y=Y, ds=data, match=match, samples=samples, pmin=pmin,
                                  tstart=tstart, tstop=tstop, mintime=mintime, minsource=minsource)
    elif factor == 'main':
        test = testnd.ttest_1samp(Y=Y, ds=data.sub("condition=='main'"), match=match, samples=samples, pmin=pmin,
                                  tstart=tstart, tstop=tstop, mintime=mintime, minsource=minsource)
    else:
        test = testnd.ttest_rel(Y=Y, ds=data, X=factor, c1=c1, c0=c0, match=match, samples=samples, pmin=pmin,
                                tstart=tstart, tstop=tstop,  mintime=mintime, minsource=minsource)

    print "Finished cluster test: mod=%s, IV=%s, factor=%s, c1=%s, c0=%s" % (mod, IV, factor, c1, c0)
    path = "/Volumes/BackUp/sufAmb/reg-cluster_time_src/ols_mod-%s_IV-%s_factor-%s_c1-%s_c0-%s.pickled" \
        % (mod, IV, factor, c1, c0)
    save.pickle(test, path)

    return test
Exemplo n.º 23
0
def test_result_report():
    "Test result_report function for different Results"
    ds = datasets.get_uts(True)
    sds = ds.sub("B == 'b0'")

    for y in ('uts',  # time
              "utsnd.summary(time=(0.25, 0.35))",  # sensor
              'utsnd',  # sensor x time
              ):
        dims = tuple(dim.name for dim in ds.eval(y).dims[1:])
        logging.info("y=%s %s", y, dims)
        kwargs = dict(pmin=0.1, samples=100)
        if 'time' in dims:
            kwargs['tstart'] = 0.2
            kwargs['tstop'] = 0.4

        for match in (None, 'rm'):
            logging.info("    match=%s", match)
            res = testnd.ttest_1samp(y, match=match, ds=sds, **kwargs)
            rep = result_report(res, ds)
            html(rep)

        res = testnd.ttest_ind(y, 'A', ds=sds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.ttest_rel(y, 'A',  ds=sds, match='rm', **kwargs)
        rep = result_report(res, sds)
        html(rep)

        res = testnd.anova(y, 'A * B', ds=ds, **kwargs)
        rep = result_report(res, ds)
        html(rep)

        res = testnd.anova(y, 'A * rm', ds=sds, match='rm', **kwargs)
        rep = result_report(res, ds)
        html(rep)
Exemplo n.º 24
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_uts(True)

    # basic
    res = testnd.ttest_rel('uts',
                           'A%B', ('a1', 'b1'), ('a0', 'b0'),
                           'rm',
                           ds=ds,
                           samples=100)
    assert repr(
        res
    ) == "<ttest_rel 'uts', 'A x B', ('a1', 'b1'), ('a0', 'b0'), 'rm' (n=15), samples=100, p < .001>"
    difference = res.masked_difference()
    assert difference.x.mask.sum() == 84
    c1 = res.masked_c1()
    assert c1.x.mask.sum() == 84
    assert_array_equal(c1.x.data, res.c1_mean.x)

    # alternate argspec
    res_ = testnd.ttest_rel("uts[A%B == ('a1', 'b1')]",
                            "uts[A%B == ('a0', 'b0')]",
                            ds=ds,
                            samples=100)
    assert repr(
        res_) == "<ttest_rel 'uts', 'uts' (n=15), samples=100, p < .001>"
    assert_dataobj_equal(res_.t, res.t)
    # alternate argspec 2
    ds1 = Dataset()
    ds1['a1b1'] = ds.eval("uts[A%B == ('a1', 'b1')]")
    ds1['a0b0'] = ds.eval("uts[A%B == ('a0', 'b0')]")
    res1 = testnd.ttest_rel('a1b1', 'a0b0', ds=ds1, samples=100)
    assert_dataobj_equal(res1.t, res.t)
    assert repr(
        res1) == "<ttest_rel 'a1b1', 'a0b0' (n=15), samples=100, p < .001>"

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds, samples=0)
    assert res2.p_uncorrected.min() < 0.05
    assert res2.n == res.n

    # reproducibility
    res3 = testnd.ttest_rel('uts',
                            'A%B', ('a1', 'b1'), ('a0', 'b0'),
                            'rm',
                            ds=ds,
                            samples=100)
    assert_dataset_equal(res3.find_clusters(maps=True), res.clusters)
    configure(n_workers=0)
    res4 = testnd.ttest_rel('uts',
                            'A%B', ('a1', 'b1'), ('a0', 'b0'),
                            'rm',
                            ds=ds,
                            samples=100)
    assert_dataset_equal(res4.find_clusters(maps=True), res.clusters)
    configure(n_workers=True)
    sds = ds.sub("B=='b0'")
    # thresholded, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # thresholded, UTSND
    configure(n_workers=0)
    res0 = testnd.ttest_rel('utsnd',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('utsnd',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # TFCE, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            tfce=True,
                            samples=10)
    tgt = res0.compute_probability_map()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            tfce=True,
                            samples=10)
    assert_dataobj_equal(res1.compute_probability_map(), tgt)

    # zero variance
    ds['utsnd'].x[:, 1, 10] = 0.
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds)
    assert res.t.x[1, 10] == 0

    # argument length
    with pytest.raises(ValueError):
        testnd.ttest_rel('utsnd', 'A[:-1]', match='rm', ds=ds)
    with pytest.raises(ValueError):
        testnd.ttest_rel('utsnd', 'A', match='rm[:-1]', ds=ds)
Exemplo n.º 25
0
def test_t_contrast():
    ds = datasets.get_rand()

    # test aux functions
    y = np.arange(9.).reshape((3, 3))
    indexes = {'a': 0, 'b': 1, 'c': 2}

    contrast = "+sum(a>c, b>c)"
    contrast_ = _testnd._parse_t_contrast(contrast)
    assert_equal(contrast_, ('func', '+', np.sum, [('comp', None, 'a', 'c'),
                                                   ('comp', None, 'b', 'c')]))

    contrast = "+sum(a>*, b>*)"
    contrast_ = _testnd._parse_t_contrast(contrast)
    assert_equal(contrast_, ('func', '+', np.sum, [('comp', None, 'a', '*'),
                                                   ('comp', None, 'b', '*')]))
    _, cells = _testnd._t_contrast_rel_properties(contrast_)
    pc, mc = _testnd._t_contrast_rel_expand_cells(cells, ('a', 'b', 'c'))
    data = _testnd._t_contrast_rel_data(y, indexes, pc, mc)
    assert_array_equal(data['a'], np.arange(3.))
    assert_array_equal(data['*'], y.mean(0))

    assert_raises(ValueError, _testnd._t_contrast_rel_expand_cells, cells,
                  ('a|c', 'b|c', 'c|c'))

    # simple contrast
    res = testnd.t_contrast_rel('uts',
                                'A',
                                'a1>a0',
                                'rm',
                                ds=ds,
                                samples=10,
                                pmin=0.05)
    repr(res)
    res_ = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_array_equal(res.t.x, res_.t.x)
    assert_in('samples', repr(res))

    # complex contrast
    res = testnd.t_contrast_rel('uts',
                                'A%B',
                                'min(a0|b0>a1|b0, a0|b1>a1|b1)',
                                'rm',
                                ds=ds,
                                samples=10,
                                pmin=0.05)
    res_b0 = testnd.ttest_rel('uts',
                              'A%B', ('a0', 'b0'), ('a1', 'b0'),
                              'rm',
                              ds=ds)
    res_b1 = testnd.ttest_rel('uts',
                              'A%B', ('a0', 'b1'), ('a1', 'b1'),
                              'rm',
                              ds=ds)
    assert_array_equal(res.t.x, np.min([res_b0.t.x, res_b1.t.x], axis=0))

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p, res_.p)

    # contrast with "*"
    contrast_star = '+min(a1|b0>a0|*, a1|b1>a0|*)'
    res = testnd.t_contrast_rel('uts', 'A%B', contrast_star, 'rm', ds=ds)
Exemplo n.º 26
0
def test_clusterdist():
    "Test _ClusterDist class"
    shape = (10, 6, 6, 4)
    locs = [[0, 0, 0],
            [1, 0, 0],
            [1, 1, 0],
            [0, 1, 0]]
    x = np.random.normal(0, 1, shape)
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 6), Ordered('dim2', range(6), 'unit'),
            sensor)
    y = NDVar(x, dims)

    # test connecting sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, :2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    print(repr(cdist))
    cdist.add_original(pmap)
    print(repr(cdist))
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))
    assert_equal(cdist.parameter_map.dims, y.dims[1:])

    # test connecting many sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))

    # test keeping sensors separate
    logging.info("TEST:  keeping sensors separate")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, 0] = True
    bin_map[:3, :3, 2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 1, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 2)

    # criteria
    ds = datasets.get_uts(True)
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05)
    assert_less(res.clusters['duration'].min(), 0.01)
    eq_(res.clusters['n_sensors'].min(), 1)
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05,
                           mintime=0.02, minsensor=2)
    assert_greater_equal(res.clusters['duration'].min(), 0.02)
    eq_(res.clusters['n_sensors'].min(), 2)

    # 1d
    res1d = testnd.ttest_rel('utsnd.sub(time=0.1)', 'A', match='rm', ds=ds,
                             samples=0, pmin=0.05)
    assert_dataobj_equal(res1d.p_uncorrected, res.p_uncorrected.sub(time=0.1))

    # TFCE
    logging.info("TEST:  TFCE")
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 4), sensor,
            Ordered('dim2', range(10), 'unit'))
    y = NDVar(np.random.normal(0, 1, (10, 4, 4, 10)), dims)
    cdist = _ClusterDist(y, 3, None)
    cdist.add_original(y.x[0])
    cdist.finalize()
    assert_equal(cdist.dist.shape, (3,))
    # I/O
    string = pickle.dumps(cdist, pickle.HIGHEST_PROTOCOL)
    cdist_ = pickle.loads(string)
    assert_equal(repr(cdist_), repr(cdist))
    # find peaks
    x = np.array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [7, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [5, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 6, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 7, 5, 5, 0, 0],
                   [0, 0, 0, 0, 5, 4, 4, 4, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 4, 0, 0],
                   [0, 0, 0, 0, 7, 0, 0, 3, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
    tgt = np.equal(x, 7)
    peaks = cdist._find_peaks(x)
    logging.debug(' detected: \n%s' % (peaks.astype(int)))
    logging.debug(' target: \n%s' % (tgt.astype(int)))
    assert_array_equal(peaks, tgt)
Exemplo n.º 27
0
def test_vector():
    """Test vector tests"""
    # single vector
    ds = datasets.get_uv(vector=True)
    res = testnd.Vector('v[:40]', ds=ds, samples=10)
    assert res.p == 0.0
    res = testnd.Vector('v[40:]', ds=ds, samples=10)
    assert res.p == 1.0

    # single vector with norm stat
    res_t = testnd.Vector('v[:40]', ds=ds, samples=10, norm=True)
    assert res_t.p == 0.0
    res_t = testnd.Vector('v[40:]', ds=ds, samples=10, norm=True)
    assert res_t.p == 1.0

    # non-space tests should raise error
    with pytest.raises(WrongDimension):
        testnd.ttest_1samp('v', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.ttest_rel('v', 'A', match='rm', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.ttest_ind('v', 'A', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.t_contrast_rel('v', 'A', 'a0 > a1', 'rm', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.corr('v', 'fltvar', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.anova('v', 'A * B', ds=ds)

    # vector in time
    ds = datasets.get_uts(vector3d=True)
    v1 = ds[30:, 'v3d']
    v2 = ds[:30, 'v3d']
    vd = v1 - v2
    res = testnd.Vector(vd, samples=10)
    assert res.p.min() == 0.2
    difference = res.masked_difference(0.5)
    assert difference.x.mask.sum() == 288
    # diff related
    resd = testnd.VectorDifferenceRelated(v1, v2, samples=10)
    assert_dataobj_equal(resd.p, res.p, name=False)
    assert_dataobj_equal(resd.t2, res.t2, name=False)
    # diff independent
    res = VectorDifferenceIndependent(v1, v2, samples=10, norm=True)
    assert_dataobj_equal(res.difference, v1.mean('case') - v2.mean('case'), name=False)
    assert res.p.max() == 1
    assert res.p.min() == 0
    # with mp
    res = testnd.Vector(v1, samples=10)
    assert res.p.min() == 0.4
    # without mp
    configure(n_workers=0)
    res0 = testnd.Vector(v1, samples=10)
    assert_array_equal(np.sort(res0._cdist.dist), np.sort(res._cdist.dist))
    configure(n_workers=True)
    # time window
    res = testnd.Vector(v2, samples=10, tstart=0.1, tstop=0.4)
    assert res.p.min() == 0.3
    difference = res.masked_difference(0.5)
    assert difference.x.mask.sum() == 294

    # vector in time with norm stat
    res = testnd.Vector(vd, samples=10, norm=True)
    assert res.p.min() == 0
    difference = res.masked_difference()
    assert difference.x.mask.sum() == 297
    resd = testnd.VectorDifferenceRelated(v1, v2, samples=10, norm=True)
    assert_dataobj_equal(resd.p, res.p, name=False)
    assert_dataobj_equal(resd.difference, res.difference, name=False)

    v_small = v2 / 100
    res = testnd.Vector(v_small, tfce=True, samples=10, norm=True)
    assert 'WARNING' in repr(res)
    res = testnd.Vector(v_small, tfce=0.1, samples=10)
    assert res.p.min() == 0.0
Exemplo n.º 28
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_uts(True)

    # basic
    res = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm', ds=ds, samples=100)
    assert repr(res) == "<ttest_rel 'uts', 'A x B', ('a1', 'b1'), ('a0', 'b0'), 'rm' (n=15), samples=100, p < .001>"
    difference = res.masked_difference()
    assert difference.x.mask.sum() == 84
    c1 = res.masked_c1()
    assert c1.x.mask.sum() == 84
    assert_array_equal(c1.x.data, res.c1_mean.x)

    # alternate argspec
    res_ = testnd.ttest_rel("uts[A%B == ('a1', 'b1')]", "uts[A%B == ('a0', 'b0')]", ds=ds, samples=100)
    assert repr(res_) == "<ttest_rel 'uts', 'uts' (n=15), samples=100, p < .001>"
    assert_dataobj_equal(res_.t, res.t)
    # alternate argspec 2
    ds1 = Dataset()
    ds1['a1b1'] = ds.eval("uts[A%B == ('a1', 'b1')]")
    ds1['a0b0'] = ds.eval("uts[A%B == ('a0', 'b0')]")
    res1 = testnd.ttest_rel('a1b1', 'a0b0', ds=ds1, samples=100)
    assert_dataobj_equal(res1.t, res.t)
    assert repr(res1) == "<ttest_rel 'a1b1', 'a0b0' (n=15), samples=100, p < .001>"

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert res2.p_uncorrected.min() < 0.05
    assert res2.n == res.n

    # reproducibility
    res3 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm', ds=ds, samples=100)
    assert_dataset_equal(res3.find_clusters(maps=True), res.clusters)
    configure(n_workers=0)
    res4 = testnd.ttest_rel('uts', 'A%B', ('a1', 'b1'), ('a0', 'b0'), 'rm', ds=ds, samples=100)
    assert_dataset_equal(res4.find_clusters(maps=True), res.clusters)
    configure(n_workers=True)
    sds = ds.sub("B=='b0'")
    # thresholded, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1, samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1, samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # thresholded, UTSND
    configure(n_workers=0)
    res0 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1, samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('utsnd', 'A', 'a1', 'a0', 'rm', ds=sds, pmin=0.1, samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # TFCE, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True, samples=10)
    tgt = res0.compute_probability_map()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=sds, tfce=True, samples=10)
    assert_dataobj_equal(res1.compute_probability_map(), tgt)

    # zero variance
    ds['utsnd'].x[:, 1, 10] = 0.
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds)
    assert res.t.x[1, 10] == 0

    # argument length
    with pytest.raises(ValueError):
        testnd.ttest_rel('utsnd', 'A[:-1]', match='rm', ds=ds)
    with pytest.raises(ValueError):
        testnd.ttest_rel('utsnd', 'A', match='rm[:-1]', ds=ds)
Exemplo n.º 29
0
def test_clusterdist():
    "Test NDPermutationDistribution class"
    shape = (10, 6, 6, 4)
    locs = [[0, 0, 0],
            [1, 0, 0],
            [1, 1, 0],
            [0, 1, 0]]
    x = np.random.normal(0, 1, shape)
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 6), Scalar('dim2', range(6), 'unit'),
            sensor)
    y = NDVar(x, dims)

    # test connecting sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, :2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = NDPermutationDistribution(y, 0, 1.5)
    print(repr(cdist))
    cdist.add_original(pmap)
    print(repr(cdist))
    assert cdist.n_clusters == 1
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))
    assert cdist.parameter_map.dims == y.dims[1:]

    # test connecting many sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = NDPermutationDistribution(y, 0, 1.5)
    cdist.add_original(pmap)
    assert cdist.n_clusters == 1
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))

    # test keeping sensors separate
    logging.info("TEST:  keeping sensors separate")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, 0] = True
    bin_map[:3, :3, 2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = NDPermutationDistribution(y, 1, 1.5)
    cdist.add_original(pmap)
    assert cdist.n_clusters == 2

    # criteria
    ds = datasets.get_uts(True)
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05)
    assert res.clusters['duration'].min() < 0.01
    assert res.clusters['n_sensors'].min() == 1
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05,
                           mintime=0.02, minsensor=2)
    assert res.clusters['duration'].min() >= 0.02
    assert res.clusters['n_sensors'].min() == 2

    # 1d
    res1d = testnd.ttest_rel('utsnd.sub(time=0.1)', 'A', match='rm', ds=ds,
                             samples=0, pmin=0.05)
    assert_dataobj_equal(res1d.p_uncorrected, res.p_uncorrected.sub(time=0.1))

    # TFCE
    logging.info("TEST:  TFCE")
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    time = UTS(-0.1, 0.1, 4)
    scalar = Scalar('scalar', range(10), 'unit')
    dims = ('case', time, sensor, scalar)
    np.random.seed(0)
    y = NDVar(np.random.normal(0, 1, (10, 4, 4, 10)), dims)
    cdist = NDPermutationDistribution(y, 3, None)
    cdist.add_original(y.x[0])
    cdist.finalize()
    assert cdist.dist.shape == (3,)
    # I/O
    string = pickle.dumps(cdist, pickle.HIGHEST_PROTOCOL)
    cdist_ = pickle.loads(string)
    assert repr(cdist_) == repr(cdist)
    # find peaks
    x = np.array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [7, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [5, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 6, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 7, 5, 5, 0, 0],
                   [0, 0, 0, 0, 5, 4, 4, 4, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 4, 0, 0],
                   [0, 0, 0, 0, 7, 0, 0, 3, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
    tgt = np.equal(x, 7)
    peaks = find_peaks(x, cdist._connectivity)
    logging.debug(' detected: \n%s' % (peaks.astype(int)))
    logging.debug(' target: \n%s' % (tgt.astype(int)))
    assert_array_equal(peaks, tgt)
    # testnd permutation result
    res = testnd.ttest_1samp(y, tfce=True, samples=3)
    if sys.version_info[0] == 3:
        target = [96.84232967, 205.83207424, 425.65942084]
    else:
        target = [77.5852307, 119.1976153, 217.6270428]
    assert_allclose(np.sort(res._cdist.dist), target)

    # parc with TFCE on unconnected dimension
    configure(False)
    x = np.random.normal(0, 1, (10, 5, 2, 4))
    time = UTS(-0.1, 0.1, 5)
    categorial = Categorial('categorial', ('a', 'b'))
    y = NDVar(x, ('case', time, categorial, sensor))
    y0 = NDVar(x[:, :, 0], ('case', time, sensor))
    y1 = NDVar(x[:, :, 1], ('case', time, sensor))
    res = testnd.ttest_1samp(y, tfce=True, samples=3)
    res_parc = testnd.ttest_1samp(y, tfce=True, samples=3, parc='categorial')
    res0 = testnd.ttest_1samp(y0, tfce=True, samples=3)
    res1 = testnd.ttest_1samp(y1, tfce=True, samples=3)
    # cdist
    assert res._cdist.shape == (4, 2, 5)
    # T-maps don't depend on connectivity
    assert_array_equal(res.t.x[:, 0], res0.t.x)
    assert_array_equal(res.t.x[:, 1], res1.t.x)
    assert_array_equal(res_parc.t.x[:, 0], res0.t.x)
    assert_array_equal(res_parc.t.x[:, 1], res1.t.x)
    # TFCE-maps should always be the same because they're unconnected
    assert_array_equal(res.tfce_map.x[:, 0], res0.tfce_map.x)
    assert_array_equal(res.tfce_map.x[:, 1], res1.tfce_map.x)
    assert_array_equal(res_parc.tfce_map.x[:, 0], res0.tfce_map.x)
    assert_array_equal(res_parc.tfce_map.x[:, 1], res1.tfce_map.x)
    # Probability-maps should depend on what is taken into account
    p_a = res0.compute_probability_map().x
    p_b = res1.compute_probability_map().x
    assert_array_equal(res_parc.compute_probability_map(categorial='a').x, p_a)
    assert_array_equal(res_parc.compute_probability_map(categorial='b').x, p_b)
    p_parc = res_parc.compute_probability_map()
    assert_array_equal(p_parc.x, res.compute_probability_map().x)
    assert np.all(p_parc.sub(categorial='a').x >= p_a)
    assert np.all(p_parc.sub(categorial='b').x >= p_b)
    configure(True)
Exemplo n.º 30
0
def test_vector():
    """Test vector tests"""
    # single vector
    ds = datasets.get_uv(vector=True)
    res = testnd.Vector('v[:40]', ds=ds, samples=10)
    assert res.p == 0.0
    res = testnd.Vector('v[40:]', ds=ds, samples=10)
    assert res.p == 1.0

    # single vector with norm stat
    res_t = testnd.Vector('v[:40]', ds=ds, samples=10, norm=True)
    assert res_t.p == 0.0
    res_t = testnd.Vector('v[40:]', ds=ds, samples=10, norm=True)
    assert res_t.p == 1.0

    # non-space tests should raise error
    with pytest.raises(WrongDimension):
        testnd.ttest_1samp('v', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.ttest_rel('v', 'A', match='rm', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.ttest_ind('v', 'A', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.t_contrast_rel('v', 'A', 'a0 > a1', 'rm', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.corr('v', 'fltvar', ds=ds)
    with pytest.raises(WrongDimension):
        testnd.anova('v', 'A * B', ds=ds)

    # vector in time
    ds = datasets.get_uts(vector3d=True)
    v1 = ds[30:, 'v3d']
    v2 = ds[:30, 'v3d']
    vd = v1 - v2
    res = testnd.Vector(vd, samples=10)
    assert res.p.min() == 0.2
    difference = res.masked_difference(0.5)
    assert difference.x.mask.sum() == 288
    # diff related
    resd = testnd.VectorDifferenceRelated(v1, v2, samples=10)
    assert_dataobj_equal(resd.p, res.p, name=False)
    assert_dataobj_equal(resd.t2, res.t2, name=False)
    # diff independent
    res = VectorDifferenceIndependent(v1, v2, samples=10, norm=True)
    assert_dataobj_equal(res.difference,
                         v1.mean('case') - v2.mean('case'),
                         name=False)
    assert res.p.max() == 1
    assert res.p.min() == 0
    # with mp
    res = testnd.Vector(v1, samples=10)
    assert res.p.min() == 0.4
    # without mp
    configure(n_workers=0)
    res0 = testnd.Vector(v1, samples=10)
    assert_array_equal(np.sort(res0._cdist.dist), np.sort(res._cdist.dist))
    configure(n_workers=True)
    # time window
    res = testnd.Vector(v2, samples=10, tstart=0.1, tstop=0.4)
    assert res.p.min() == 0.3
    difference = res.masked_difference(0.5)
    assert difference.x.mask.sum() == 294

    # vector in time with norm stat
    res = testnd.Vector(vd, samples=10, norm=True)
    assert res.p.min() == 0
    difference = res.masked_difference()
    assert difference.x.mask.sum() == 297
    resd = testnd.VectorDifferenceRelated(v1, v2, samples=10, norm=True)
    assert_dataobj_equal(resd.p, res.p, name=False)
    assert_dataobj_equal(resd.difference, res.difference, name=False)

    v_small = v2 / 100
    res = testnd.Vector(v_small, tfce=True, samples=10, norm=True)
    assert 'WARNING' in repr(res)
    res = testnd.Vector(v_small, tfce=0.1, samples=10)
    assert res.p.min() == 0.0
Exemplo n.º 31
0
def test_clusterdist():
    "Test _ClusterDist class"
    shape = (10, 6, 6, 4)
    locs = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
    x = np.random.normal(0, 1, shape)
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 6), Ordered('dim2', range(6),
                                               'unit'), sensor)
    y = NDVar(x, dims)

    # test connecting sensors
    logger.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, :2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    print repr(cdist)
    cdist.add_original(pmap)
    print repr(cdist)
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))
    assert_equal(cdist.parameter_map.dims, y.dims[1:])

    # test connecting many sensors
    logger.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))

    # test keeping sensors separate
    logger.info("TEST:  keeping sensors separate")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, 0] = True
    bin_map[:3, :3, 2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 1, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 2)

    # criteria
    ds = datasets.get_uts(True)
    res = testnd.ttest_rel('utsnd',
                           'A',
                           match='rm',
                           ds=ds,
                           samples=0,
                           pmin=0.05)
    assert_less(res.clusters['duration'].min(), 0.01)
    eq_(res.clusters['n_sensors'].min(), 1)
    res = testnd.ttest_rel('utsnd',
                           'A',
                           match='rm',
                           ds=ds,
                           samples=0,
                           pmin=0.05,
                           mintime=0.02,
                           minsensor=2)
    assert_greater_equal(res.clusters['duration'].min(), 0.02)
    eq_(res.clusters['n_sensors'].min(), 2)

    # TFCE
    logger.info("TEST:  TFCE")
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1,
                        4), sensor, Ordered('dim2', range(10), 'unit'))
    y = NDVar(np.random.normal(0, 1, (10, 4, 4, 10)), dims)
    cdist = _ClusterDist(y, 3, None)
    cdist.add_original(y.x[0])
    cdist.finalize()
    assert_equal(cdist.dist.shape, (3, ))
    # I/O
    string = pickle.dumps(cdist, pickle.HIGHEST_PROTOCOL)
    cdist_ = pickle.loads(string)
    assert_equal(repr(cdist_), repr(cdist))
    # find peaks
    x = np.array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [7, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [5, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 6, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 7, 5, 5, 0, 0],
                   [0, 0, 0, 0, 5, 4, 4, 4, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 4, 0, 0],
                   [0, 0, 0, 0, 7, 0, 0, 3, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
    tgt = np.equal(x, 7)
    peaks = cdist._find_peaks(x)
    logging.debug(' detected: \n%s' % (peaks.astype(int)))
    logging.debug(' target: \n%s' % (tgt.astype(int)))
    assert_array_equal(peaks, tgt)

    mps = False, True
    thresholds = (None, 'tfce')
    for mp, threshold in product(mps, thresholds):
        logger.info("TEST:  multiprocessing=%r, threshold=%r" %
                    (mp, threshold))
        _testnd.multiprocessing = mp

        # test keeping dimension
        cdist = _ClusterDist(y, 5, threshold, dist_dim='sensor')
        print repr(cdist)
        cdist.add_original(y.x[0])
        print repr(cdist)
        assert_equal(cdist.dist.shape, (5, 4))

        # test keeping time bins
        cdist = _ClusterDist(y, 5, threshold, dist_tstep=0.2)
        cdist.add_original(y.x[0])
        assert_equal(cdist.dist.shape, (5, 2))
        assert_raises(ValueError,
                      _ClusterDist,
                      y,
                      5,
                      threshold,
                      dist_tstep=0.3)

        # test keeping dimension and time bins
        cdist = _ClusterDist(y,
                             5,
                             threshold,
                             dist_dim='sensor',
                             dist_tstep=0.2)
        cdist.add_original(y.x[0])
        assert_equal(cdist.dist.shape, (5, 4, 2))

        # test keeping 2 dimensions and time bins
        cdist = _ClusterDist(y,
                             5,
                             threshold,
                             dist_dim=('sensor', 'dim2'),
                             dist_tstep=0.2)
        cdist.add_original(y.x[0])
        assert_equal(cdist.dist.shape, (5, 4, 2, 10))
Exemplo n.º 32
0
pmin=0.15
tstart=0.13
tstop=0.45
mintime=0.03
minsource=10
X = 'Type'

tests = {}
for v in IVs:
    print v
    testsv = {}
    fil = dr+'corr_'+v+'.pickled'
    data = load.unpickle(fil)
    
    print 'main'
    testsv['main'] = testnd.ttest_1samp(Y=Y, ds=data.sub("Type=='all'"), match=match, samples=samples, pmin=pmin, tstart=tstart, tstop=tstop, mintime=mintime, minsource=minsource)
    print 'test 1'
    testsv['stemVstemS'] = testnd.ttest_rel(Y=Y, ds=data, X=X, c1='stem', c0='stemS', match=match, samples=samples, pmin=pmin, tstart=tstart, tstop=tstop,  mintime=mintime, minsource=minsource)
    print 'test 2'
    testsv['stemVstemEd'] = testnd.ttest_rel(Y=Y, ds=data, X=X, c1='stem', c0='stemEd', match=match, samples=samples, pmin=pmin, tstart=tstart, tstop=tstop, mintime=mintime, minsource=minsource)
    print 'test 3'
    testsv['stemSVstemEd'] = testnd.ttest_rel(Y=Y, ds=data, X=X, c1='stemS', c0='stemEd', match=match, samples=samples, pmin=pmin, tstart=tstart, tstop=tstop, mintime=mintime, minsource=minsource)
     
    for t in testsv:
        name = v+'_'+t
        path = '/Volumes/BackUp/sufAmb/corr_cluster_timespace/corr_%s.pickled' % name
        save.pickle(testsv[t], path)
    
    tests[v] = testsv

Exemplo n.º 33
0
def test_clusterdist():
    "Test _ClusterDist class"
    shape = (10, 6, 6, 4)
    locs = [[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]
    x = np.random.normal(0, 1, shape)
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 6), Scalar('dim2', range(6),
                                              'unit'), sensor)
    y = NDVar(x, dims)

    # test connecting sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, :2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    print(repr(cdist))
    cdist.add_original(pmap)
    print(repr(cdist))
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))
    assert_equal(cdist.parameter_map.dims, y.dims[1:])

    # test connecting many sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))

    # test keeping sensors separate
    logging.info("TEST:  keeping sensors separate")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, 0] = True
    bin_map[:3, :3, 2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 1, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 2)

    # criteria
    ds = datasets.get_uts(True)
    res = testnd.ttest_rel('utsnd',
                           'A',
                           match='rm',
                           ds=ds,
                           samples=0,
                           pmin=0.05)
    assert_less(res.clusters['duration'].min(), 0.01)
    eq_(res.clusters['n_sensors'].min(), 1)
    res = testnd.ttest_rel('utsnd',
                           'A',
                           match='rm',
                           ds=ds,
                           samples=0,
                           pmin=0.05,
                           mintime=0.02,
                           minsensor=2)
    assert_greater_equal(res.clusters['duration'].min(), 0.02)
    eq_(res.clusters['n_sensors'].min(), 2)

    # 1d
    res1d = testnd.ttest_rel('utsnd.sub(time=0.1)',
                             'A',
                             match='rm',
                             ds=ds,
                             samples=0,
                             pmin=0.05)
    assert_dataobj_equal(res1d.p_uncorrected, res.p_uncorrected.sub(time=0.1))

    # TFCE
    logging.info("TEST:  TFCE")
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    time = UTS(-0.1, 0.1, 4)
    scalar = Scalar('scalar', range(10), 'unit')
    dims = ('case', time, sensor, scalar)
    np.random.seed(0)
    y = NDVar(np.random.normal(0, 1, (10, 4, 4, 10)), dims)
    cdist = _ClusterDist(y, 3, None)
    cdist.add_original(y.x[0])
    cdist.finalize()
    assert_equal(cdist.dist.shape, (3, ))
    # I/O
    string = pickle.dumps(cdist, pickle.HIGHEST_PROTOCOL)
    cdist_ = pickle.loads(string)
    assert_equal(repr(cdist_), repr(cdist))
    # find peaks
    x = np.array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [7, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [5, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 6, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 7, 5, 5, 0, 0],
                   [0, 0, 0, 0, 5, 4, 4, 4, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 4, 0, 0],
                   [0, 0, 0, 0, 7, 0, 0, 3, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
    tgt = np.equal(x, 7)
    peaks = find_peaks(x, cdist._connectivity)
    logging.debug(' detected: \n%s' % (peaks.astype(int)))
    logging.debug(' target: \n%s' % (tgt.astype(int)))
    assert_array_equal(peaks, tgt)
    # testnd permutation result
    res = testnd.ttest_1samp(y, tfce=True, samples=3)
    assert_allclose(np.sort(res._cdist.dist),
                    [77.5852307, 119.1976153, 217.6270428])

    # parc with TFCE on unconnected dimension
    configure(False)
    x = np.random.normal(0, 1, (10, 5, 2, 4))
    time = UTS(-0.1, 0.1, 5)
    categorial = Categorial('categorial', ('a', 'b'))
    y = NDVar(x, ('case', time, categorial, sensor))
    y0 = NDVar(x[:, :, 0], ('case', time, sensor))
    y1 = NDVar(x[:, :, 1], ('case', time, sensor))
    res = testnd.ttest_1samp(y, tfce=True, samples=3)
    res_parc = testnd.ttest_1samp(y, tfce=True, samples=3, parc='categorial')
    res0 = testnd.ttest_1samp(y0, tfce=True, samples=3)
    res1 = testnd.ttest_1samp(y1, tfce=True, samples=3)
    # cdist
    eq_(res._cdist.shape, (4, 2, 5))
    # T-maps don't depend on connectivity
    assert_array_equal(res.t.x[:, 0], res0.t.x)
    assert_array_equal(res.t.x[:, 1], res1.t.x)
    assert_array_equal(res_parc.t.x[:, 0], res0.t.x)
    assert_array_equal(res_parc.t.x[:, 1], res1.t.x)
    # TFCE-maps should always be the same because they're unconnected
    assert_array_equal(res.tfce_map.x[:, 0], res0.tfce_map.x)
    assert_array_equal(res.tfce_map.x[:, 1], res1.tfce_map.x)
    assert_array_equal(res_parc.tfce_map.x[:, 0], res0.tfce_map.x)
    assert_array_equal(res_parc.tfce_map.x[:, 1], res1.tfce_map.x)
    # Probability-maps should depend on what is taken into account
    p_a = res0.compute_probability_map().x
    p_b = res1.compute_probability_map().x
    assert_array_equal(res_parc.compute_probability_map(categorial='a').x, p_a)
    assert_array_equal(res_parc.compute_probability_map(categorial='b').x, p_b)
    p_parc = res_parc.compute_probability_map()
    assert_array_equal(p_parc.x, res.compute_probability_map().x)
    ok_(np.all(p_parc.sub(categorial='a').x >= p_a))
    ok_(np.all(p_parc.sub(categorial='b').x >= p_b))
    configure(True)
Exemplo n.º 34
0
def test_ttest_rel():
    "Test testnd.ttest_rel()"
    ds = datasets.get_uts(True)

    # basic
    res = testnd.ttest_rel('uts',
                           'A%B', ('a1', 'b1'), ('a0', 'b0'),
                           'rm',
                           ds=ds,
                           samples=100)
    eq_(
        repr(res), "<ttest_rel 'uts', 'A x B', ('a1', 'b1'), ('a0', 'b0'), "
        "'rm' (n=15), samples=100, p=.000>")

    # alternate argspec
    ds1 = Dataset()
    ds1['a1b1'] = ds.eval("uts[A%B == ('a1', 'b1')]")
    ds1['a0b0'] = ds.eval("uts[A%B == ('a0', 'b0')]")
    res1 = testnd.ttest_rel('a1b1', 'a0b0', ds=ds1, samples=100)
    assert_dataobj_equal(res1.t, res.t)
    eq_(repr(res1), "<ttest_rel 'a1b1', 'a0b0' (n=15), samples=100, p=.000>")

    # persistence
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    repr(res_)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)

    # collapsing cells
    res2 = testnd.ttest_rel('uts', 'A', 'a1', 'a0', 'rm', ds=ds)
    assert_less(res2.p_uncorrected.min(), 0.05)
    assert_equal(res2.n, res.n)

    # reproducibility
    res3 = testnd.ttest_rel('uts',
                            'A%B', ('a1', 'b1'), ('a0', 'b0'),
                            'rm',
                            ds=ds,
                            samples=100)
    assert_dataset_equal(res3.find_clusters(maps=True), res.clusters)
    configure(n_workers=0)
    res4 = testnd.ttest_rel('uts',
                            'A%B', ('a1', 'b1'), ('a0', 'b0'),
                            'rm',
                            ds=ds,
                            samples=100)
    assert_dataset_equal(res4.find_clusters(maps=True), res.clusters)
    configure(n_workers=True)
    sds = ds.sub("B=='b0'")
    # thresholded, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # thresholded, UTSND
    configure(n_workers=0)
    res0 = testnd.ttest_rel('utsnd',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    tgt = res0.find_clusters()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('utsnd',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            pmin=0.1,
                            samples=100)
    assert_dataset_equal(res1.find_clusters(), tgt)
    # TFCE, UTS
    configure(n_workers=0)
    res0 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            tfce=True,
                            samples=10)
    tgt = res0.compute_probability_map()
    configure(n_workers=True)
    res1 = testnd.ttest_rel('uts',
                            'A',
                            'a1',
                            'a0',
                            'rm',
                            ds=sds,
                            tfce=True,
                            samples=10)
    assert_dataobj_equal(res1.compute_probability_map(), tgt)

    # zero variance
    ds['utsnd'].x[:, 1, 10] = 0.
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds)
    eq_(res.t.x[1, 10], 0)
Exemplo n.º 35
0
def test_clusterdist():
    "Test _ClusterDist class"
    shape = (10, 6, 6, 4)
    locs = [[0, 0, 0],
            [1, 0, 0],
            [1, 1, 0],
            [0, 1, 0]]
    x = np.random.normal(0, 1, shape)
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 6), Ordered('dim2', list(range(6)), 'unit'),
            sensor)
    y = NDVar(x, dims)

    # test connecting sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, :2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    print(repr(cdist))
    cdist.add_original(pmap)
    print(repr(cdist))
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))
    assert_equal(cdist.parameter_map.dims, y.dims[1:])

    # test connecting many sensors
    logging.info("TEST:  connecting sensors")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 0, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 1)
    assert_array_equal(cdist._original_cluster_map == cdist._cids[0],
                       cdist._crop(bin_map).swapaxes(0, cdist._nad_ax))

    # test keeping sensors separate
    logging.info("TEST:  keeping sensors separate")
    bin_map = np.zeros(shape[1:], dtype=np.bool8)
    bin_map[:3, :3, 0] = True
    bin_map[:3, :3, 2] = True
    pmap = np.random.normal(0, 1, shape[1:])
    np.clip(pmap, -1, 1, pmap)
    pmap[bin_map] = 2
    cdist = _ClusterDist(y, 1, 1.5)
    cdist.add_original(pmap)
    assert_equal(cdist.n_clusters, 2)

    # criteria
    ds = datasets.get_uts(True)
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05)
    assert_less(res.clusters['duration'].min(), 0.01)
    eq_(res.clusters['n_sensors'].min(), 1)
    res = testnd.ttest_rel('utsnd', 'A', match='rm', ds=ds, samples=0, pmin=0.05,
                           mintime=0.02, minsensor=2)
    assert_greater_equal(res.clusters['duration'].min(), 0.02)
    eq_(res.clusters['n_sensors'].min(), 2)

    # 1d
    res1d = testnd.ttest_rel('utsnd.sub(time=0.1)', 'A', match='rm', ds=ds,
                             samples=0, pmin=0.05)
    assert_dataobj_equal(res1d.p_uncorrected, res.p_uncorrected.sub(time=0.1))

    # TFCE
    logging.info("TEST:  TFCE")
    sensor = Sensor(locs, ['0', '1', '2', '3'])
    sensor.set_connectivity(connect_dist=1.1)
    dims = ('case', UTS(-0.1, 0.1, 4), sensor,
            Ordered('dim2', list(range(10)), 'unit'))
    y = NDVar(np.random.normal(0, 1, (10, 4, 4, 10)), dims)
    cdist = _ClusterDist(y, 3, None)
    cdist.add_original(y.x[0])
    cdist.finalize()
    assert_equal(cdist.dist.shape, (3,))
    # I/O
    string = pickle.dumps(cdist, pickle.HIGHEST_PROTOCOL)
    cdist_ = pickle.loads(string)
    assert_equal(repr(cdist_), repr(cdist))
    # find peaks
    x = np.array([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [7, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [5, 7, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 6, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 7, 5, 5, 0, 0],
                   [0, 0, 0, 0, 5, 4, 4, 4, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],

                  [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 4, 0, 0],
                   [0, 0, 0, 0, 7, 0, 0, 3, 0, 0],
                   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]])
    tgt = np.equal(x, 7)
    peaks = cdist._find_peaks(x)
    logging.debug(' detected: \n%s' % (peaks.astype(int)))
    logging.debug(' target: \n%s' % (tgt.astype(int)))
    assert_array_equal(peaks, tgt)