Exemplo n.º 1
0
def test_ttest_rel():
    """Test test.TTestRel()"""
    ds = datasets.get_uv()

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds)
    i1 = ds.eval("logical_and(A=='a1', B=='b1')")
    i2 = ds.eval("logical_and(A=='a2', B=='b1')")
    a1 = ds[i1, 'fltvar'].x
    a2 = ds[i2, 'fltvar'].x
    diff = a1 - a2
    t, p = scipy.stats.ttest_rel(a1, a2)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, 1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t > 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, -1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, -1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t < 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))
Exemplo n.º 2
0
def test_ttest_rel():
    """Test test.TTestRel()"""
    ds = datasets.get_uv()

    res = test.TTestRel("fltvar", "A", "a1", "a2", "rm", "B=='b1'", ds)
    i1 = ds.eval("logical_and(A=='a1', B=='b1')")
    i2 = ds.eval("logical_and(A=='a2', B=='b1')")
    a1 = ds[i1, "fltvar"].x
    a2 = ds[i2, "fltvar"].x
    diff = a1 - a2
    t, p = scipy.stats.ttest_rel(a1, a2)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel("fltvar", "A", "a1", "a2", "rm", "B=='b1'", ds, 1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t > 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel("fltvar", "A", "a1", "a2", "rm", "B=='b1'", ds, -1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, -1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t < 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))
Exemplo n.º 3
0
def source_trfs(
        ress: ResultCollection,
        heading: FMTextArg = None,
        brain_view: Union[str, Sequence[float]] = None,
        axw: float = None,
        surf: str = 'inflated',
        cortex: Any = ((1.00,) * 3, (.4,) * 3),
        vmax: float = None,
        xlim: Tuple[float, float] = None,
        times: Sequence[float] = None,
        cmap: str = None,
        labels: Dict[str, str] = None,
        rasterize: bool = None,
        brain_timewindow: float = 0.050
):
    "Only used for TRFExperiment model-test"
    layout = BrainLayout(brain_view, axw)
    dt = brain_timewindow / 2

    if heading is not None:
        doc = fmtxt.Section(heading)
    else:
        doc = fmtxt.FMText()

    if cmap is None:
        cmap = 'lux-a'

    if labels is None:
        labels = {}

    trf_table = fmtxt.Table('ll')
    for key, res in ress.items():
        trf_resampled = resample(res.masked_difference(), 1000)
        label = labels.get(key, key)
        if rasterize is None:
            rasterize = len(trf_resampled.source) > 500
        # times for anatomical plots
        if times is None:
            trf_tc = abs(trf_resampled).sum('source')
            trf_tc_mask = (~trf_resampled.get_mask()).sum('source') >= 10
            times_ = find_peak_times(trf_tc, trf_tc_mask)
        else:
            times_ = times
        # butterfly-plot
        p = plot.Butterfly(trf_resampled, h=3, w=4, ylabel=False, title=label, vmax=vmax, xlim=xlim, show=False)
        for t in times_:
            p.add_vline(t, color='k')
        trf_table.cell(fmtxt.asfmtext(p, rasterize=rasterize))
        p.close()
        # peak sources
        if not times_:
            trf_table.cell()
        sp = plot.brain.SequencePlotter()
        if layout.brain_view:
            sp.set_parallel_view(*layout.brain_view)
        sp.set_brain_args(surf=surf, cortex=cortex)
        for t in times_:
            yt = trf_resampled.mean(time=(t - dt, t + dt + 0.001))
            if isinstance(cmap, str):
                vmax_ = vmax or max(-yt.min(), yt.max()) or 1
                cmap_ = plot.soft_threshold_colormap(cmap, vmax_ / 10, vmax_)
            else:
                cmap_ = cmap
            sp.add_ndvar(yt, cmap=cmap_, label=f'{t * 1000:.0f} ms', smoothing_steps=10)
        p = sp.plot_table(view='lateral', orientation='vertical', **layout.table_args)
        trf_table.cell(p)
        p.close()
    doc.append(fmtxt.Figure(trf_table))
    return doc
Exemplo n.º 4
0
def test_ttest():
    """Test univariate t-test functions"""
    ds = datasets.get_uv()

    print(test.ttest('fltvar', ds=ds))
    print(test.ttest('fltvar', 'A', ds=ds))
    print(test.ttest('fltvar', 'A%B', ds=ds))
    print(test.ttest('fltvar', 'A', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A', 'a1', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A%B', ('a1', 'b1'), match='rm', ds=ds))

    # Prepare data for scipy
    a1_index = ds.eval("A == 'a1'")
    a2_index = ds.eval("A == 'a2'")
    b1_index = ds.eval("B == 'b1'")
    a1_in_b1_index = np.logical_and(a1_index, b1_index)
    a2_in_b1_index = np.logical_and(a2_index, b1_index)

    # TTest1Samp
    res = test.TTest1Sample('fltvar', ds=ds)
    t, p = scipy.stats.ttest_1samp(ds['fltvar'], 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    res = test.TTest1Sample('fltvar', ds=ds, tail=1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2., 10)

    # TTestInd
    res = test.TTestInd('fltvar', 'A', 'a1', 'a2', ds=ds)
    t, p = scipy.stats.ttest_ind(ds[a1_index, 'fltvar'], ds[a2_index, 'fltvar'])
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)

    # TTestRel
    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds)
    a1 = ds[a1_in_b1_index, 'fltvar'].x
    a2 = ds[a2_in_b1_index, 'fltvar'].x
    difference = a1 - a2
    t, p = scipy.stats.ttest_rel(a1, a2)
    assert_array_equal(res.difference.x, difference)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, 1)
    assert_array_equal(res.difference.x, difference)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t > 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, -1)
    assert_array_equal(res.difference.x, difference)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, -1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t < 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))
    # alternative argspec
    a1 = ds.eval("fltvar[(B == 'b1') & (A == 'a1')]")
    a2 = ds.eval("fltvar[(B == 'b1') & (A == 'a2')]")
    res_alt = test.TTestRel(a1, a2, tail=-1)
    print(res_alt)
    assert res_alt.t == res.t
    assert res_alt.p == res.p
Exemplo n.º 5
0
def test_anova():
    "Test testnd.anova()"
    ds = datasets.get_uts(True, nrm=True)

    testnd.anova('utsnd', 'A*B', ds=ds)
    for samples in (0, 2):
        logging.info("TEST:  samples=%r" % samples)
        testnd.anova('utsnd', 'A*B', ds=ds, samples=samples)
        testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, pmin=0.05)
        res = testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True)
        assert res._plot_model() == 'A%B'
    asfmtext(res)

    res = testnd.anova('utsnd', 'A*B*rm', match=False, ds=ds, samples=0, pmin=0.05)
    assert repr(res) == "<anova 'utsnd', 'A*B*rm', match=False, samples=0, pmin=0.05, 'A': 17 clusters, 'B': 20 clusters, 'A x B': 22 clusters>"
    assert res._plot_model() == 'A%B'
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=2, pmin=0.05)
    assert res.match == 'rm'
    assert repr(res) == "<anova 'utsnd', 'A*B*rm', match='rm', samples=2, pmin=0.05, 'A': 17 clusters, p < .001, 'B': 20 clusters, p < .001, 'A x B': 22 clusters, p < .001>"
    assert res._plot_model() == 'A%B'

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert res_._plot_model() == 'A%B'

    # threshold-free
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10)
    assert res.match == 'rm'
    assert repr(res) ==  "<anova 'utsnd', 'A*B*rm', match='rm', samples=10, 'A': p < .001, 'B': p < .001, 'A x B': p < .001>"
    assert 'A clusters' in res.clusters.info
    assert 'B clusters' in res.clusters.info
    assert 'A x B clusters' in res.clusters.info

    # no clusters
    res = testnd.anova('uts', 'B', sub="A=='a1'", ds=ds, samples=5, pmin=0.05, mintime=0.02)
    repr(res)
    assert 'v' in res.clusters
    assert 'p' in res.clusters
    assert res._plot_model() == 'B'

    # all effects with clusters
    res = testnd.anova('uts', 'A*B*rm', match=False, ds=ds, samples=5, pmin=0.05, tstart=0.1, mintime=0.02)
    assert set(res.clusters['effect'].cells) == set(res.effects)

    # some effects with clusters, some without
    res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5, pmin=0.05, tstart=0.37, mintime=0.02)
    assert res.match == 'rm'
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_dataobj_equal(res.clusters, res_.clusters)

    # test multi-effect results (with persistence)
    # UTS
    res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5)
    assert res.match == 'rm'
    repr(res)
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    resr = pickle.loads(string)
    tf_clusters = resr.find_clusters(pmin=0.05)
    peaks = resr.find_peaks()
    assert_dataobj_equal(tf_clusters, res.find_clusters(pmin=0.05))
    assert_dataobj_equal(peaks, res.find_peaks())
    assert tf_clusters.eval("p.min()") == peaks.eval("p.min()")
    unmasked = resr.f[0]
    masked = resr.masked_parameter_map(effect=0, pmin=0.05)
    assert_array_equal(masked.x <= unmasked.x, True)

    # reproducibility
    decimal = 12 if IS_WINDOWS else None  # FIXME: why is Windows sometimes different???
    res0 = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    assert_dataset_equal(res.clusters, res0.clusters, decimal=decimal)
    configure(n_workers=0)
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    assert_dataset_equal(res.clusters, res0.clusters, decimal=decimal)
    configure(n_workers=True)

    # permutation
    eelbrain._stats.permutation._YIELD_ORIGINAL = 1
    samples = 4
    # raw
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=samples)
    for dist in res._cdist:
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, dist.parameter_map.abs().max())
    # TFCE
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, tfce=True, samples=samples)
    for dist in res._cdist:
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, dist.tfce_map.abs().max())
    # thresholded
    res1 = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=samples)
    clusters = res1.find_clusters()
    for dist, effect in zip(res1._cdist, res1.effects):
        effect_idx = clusters.eval("effect == %r" % effect)
        vmax = clusters[effect_idx, 'v'].abs().max()
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, vmax)
    eelbrain._stats.permutation._YIELD_ORIGINAL = 0

    # 1d TFCE
    configure(n_workers=0)
    res = testnd.anova('utsnd.rms(time=(0.1, 0.3))', 'A*B*rm', ds=ds, tfce=True, samples=samples)
    configure(n_workers=True)

    # zero variance
    res2 = testnd.anova('utsnd', 'A', ds=ds)
    ds['utsnd'].x[:, 1, 10] = 0.
    zero_var = ds['utsnd'].var('case') == 0
    zv_index = tuple(i[0] for i in zero_var.nonzero())
    res1_zv = testnd.anova('utsnd', 'A*B*rm', ds=ds)
    res2_zv = testnd.anova('utsnd', 'A', ds=ds)
    for res, res_zv in ((res1, res1_zv), (res2, res2_zv)):
        for f, f_zv in zip(res.f, res_zv.f):
            assert_array_equal((f_zv == 0).x, zero_var.x)
            assert f_zv[zv_index] == 0
            f_zv[zv_index] = f[zv_index]
            assert_dataobj_equal(f_zv, f, decimal=decimal)

    # nested random effect
    res = testnd.anova('uts', 'A * B * nrm(A)', ds=ds, samples=10, tstart=.4)
    assert res.match == 'nrm(A)'
    assert [p.min() for p in res.p] == [0.0, 0.6, 0.9]

    # unequal argument length
    with pytest.raises(ValueError):
        testnd.anova('uts', 'A[:-1]', ds=ds)
    with pytest.raises(ValueError):
        testnd.anova('uts[:-1]', 'A * B * nrm(A)', ds=ds)
Exemplo n.º 6
0
def test_anova():
    "Test testnd.anova()"
    ds = datasets.get_uts(True, nrm=True)

    testnd.anova('utsnd', 'A*B', ds=ds)
    for samples in (0, 2):
        logging.info("TEST:  samples=%r" % samples)
        testnd.anova('utsnd', 'A*B', ds=ds, samples=samples)
        testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, pmin=0.05)
        res = testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True)
        assert res._plot_model() == 'A%B'
    asfmtext(res)

    res = testnd.anova('utsnd',
                       'A*B*rm',
                       match=False,
                       ds=ds,
                       samples=0,
                       pmin=0.05)
    assert repr(
        res
    ) == "<anova 'utsnd', 'A*B*rm', match=False, samples=0, pmin=0.05, 'A': 17 clusters, 'B': 20 clusters, 'A x B': 22 clusters>"
    assert res._plot_model() == 'A%B'
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=2, pmin=0.05)
    assert res.match == 'rm'
    assert repr(
        res
    ) == "<anova 'utsnd', 'A*B*rm', match='rm', samples=2, pmin=0.05, 'A': 17 clusters, p < .001, 'B': 20 clusters, p < .001, 'A x B': 22 clusters, p < .001>"
    assert res._plot_model() == 'A%B'

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert repr(res_) == repr(res)
    assert res_._plot_model() == 'A%B'

    # threshold-free
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10)
    assert res.match == 'rm'
    assert repr(
        res
    ) == "<anova 'utsnd', 'A*B*rm', match='rm', samples=10, 'A': p < .001, 'B': p < .001, 'A x B': p < .001>"
    assert 'A clusters' in res.clusters.info
    assert 'B clusters' in res.clusters.info
    assert 'A x B clusters' in res.clusters.info

    # no clusters
    res = testnd.anova('uts',
                       'B',
                       sub="A=='a1'",
                       ds=ds,
                       samples=5,
                       pmin=0.05,
                       mintime=0.02)
    repr(res)
    assert 'v' in res.clusters
    assert 'p' in res.clusters
    assert res._plot_model() == 'B'

    # all effects with clusters
    res = testnd.anova('uts',
                       'A*B*rm',
                       match=False,
                       ds=ds,
                       samples=5,
                       pmin=0.05,
                       tstart=0.1,
                       mintime=0.02)
    assert set(res.clusters['effect'].cells) == set(res.effects)

    # some effects with clusters, some without
    res = testnd.anova('uts',
                       'A*B*rm',
                       ds=ds,
                       samples=5,
                       pmin=0.05,
                       tstart=0.37,
                       mintime=0.02)
    assert res.match == 'rm'
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_dataobj_equal(res.clusters, res_.clusters)

    # test multi-effect results (with persistence)
    # UTS
    res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5)
    assert res.match == 'rm'
    repr(res)
    string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL)
    resr = pickle.loads(string)
    tf_clusters = resr.find_clusters(pmin=0.05)
    peaks = resr.find_peaks()
    assert_dataobj_equal(tf_clusters, res.find_clusters(pmin=0.05))
    assert_dataobj_equal(peaks, res.find_peaks())
    assert tf_clusters.eval("p.min()") == peaks.eval("p.min()")
    unmasked = resr.f[0]
    masked = resr.masked_parameter_map(effect=0, pmin=0.05)
    assert_array_equal(masked.x <= unmasked.x, True)

    # reproducibility
    decimal = 12 if IS_WINDOWS else None  # FIXME: why is Windows sometimes different???
    res0 = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    assert_dataset_equal(res.clusters, res0.clusters, decimal=decimal)
    configure(n_workers=0)
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5)
    assert_dataset_equal(res.clusters, res0.clusters, decimal=decimal)
    configure(n_workers=True)

    # permutation
    eelbrain._stats.permutation._YIELD_ORIGINAL = 1
    samples = 4
    # raw
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=samples)
    for dist in res._cdist:
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, dist.parameter_map.abs().max())
    # TFCE
    res = testnd.anova('utsnd', 'A*B*rm', ds=ds, tfce=True, samples=samples)
    for dist in res._cdist:
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, dist.tfce_map.abs().max())
    # thresholded
    res1 = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=samples)
    clusters = res1.find_clusters()
    for dist, effect in zip(res1._cdist, res1.effects):
        effect_idx = clusters.eval("effect == %r" % effect)
        vmax = clusters[effect_idx, 'v'].abs().max()
        assert len(dist.dist) == samples
        assert_array_equal(dist.dist, vmax)
    eelbrain._stats.permutation._YIELD_ORIGINAL = 0

    # 1d TFCE
    configure(n_workers=0)
    res = testnd.anova('utsnd.rms(time=(0.1, 0.3))',
                       'A*B*rm',
                       ds=ds,
                       tfce=True,
                       samples=samples)
    configure(n_workers=True)

    # zero variance
    res2 = testnd.anova('utsnd', 'A', ds=ds)
    ds['utsnd'].x[:, 1, 10] = 0.
    zero_var = ds['utsnd'].var('case') == 0
    zv_index = tuple(i[0] for i in zero_var.nonzero())
    res1_zv = testnd.anova('utsnd', 'A*B*rm', ds=ds)
    res2_zv = testnd.anova('utsnd', 'A', ds=ds)
    for res, res_zv in ((res1, res1_zv), (res2, res2_zv)):
        for f, f_zv in zip(res.f, res_zv.f):
            assert_array_equal((f_zv == 0).x, zero_var.x)
            assert f_zv[zv_index] == 0
            f_zv[zv_index] = f[zv_index]
            assert_dataobj_equal(f_zv, f, decimal=decimal)

    # nested random effect
    res = testnd.anova('uts', 'A * B * nrm(A)', ds=ds, samples=10, tstart=.4)
    assert res.match == 'nrm(A)'
    assert [p.min() for p in res.p] == [0.0, 0.6, 0.9]

    # unequal argument length
    with pytest.raises(ValueError):
        testnd.anova('uts', 'A[:-1]', ds=ds)
    with pytest.raises(ValueError):
        testnd.anova('uts[:-1]', 'A * B * nrm(A)', ds=ds)
Exemplo n.º 7
0
def test_ttest():
    """Test univariate t-test functions"""
    ds = datasets.get_uv()

    print(test.ttest('fltvar', ds=ds))
    print(test.ttest('fltvar', 'A', ds=ds))
    print(test.ttest('fltvar', 'A%B', ds=ds))
    print(test.ttest('fltvar', 'A', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A', 'a1', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A%B', ('a1', 'b1'), match='rm', ds=ds))

    # Prepare data for scipy
    a1_index = ds.eval("A == 'a1'")
    a2_index = ds.eval("A == 'a2'")
    b1_index = ds.eval("B == 'b1'")
    a1_in_b1_index = np.logical_and(a1_index, b1_index)
    a2_in_b1_index = np.logical_and(a2_index, b1_index)

    # TTest1Samp
    res = test.TTest1Sample('fltvar', ds=ds)
    t, p = scipy.stats.ttest_1samp(ds['fltvar'], 0)
    assert res.t == pytest.approx(t, 10)
    assert res.p == pytest.approx(p, 10)
    res = test.TTest1Sample('fltvar', ds=ds, tail=1)
    assert res.t == pytest.approx(t, 10)
    assert res.p == pytest.approx(p / 2., 10)

    # TTestInd
    res = test.TTestInd('fltvar', 'A', 'a1', 'a2', ds=ds)
    t, p = scipy.stats.ttest_ind(ds[a1_index, 'fltvar'], ds[a2_index,
                                                            'fltvar'])
    assert res.t == pytest.approx(t, 10)
    assert res.p == pytest.approx(p, 10)

    # TTestRel
    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds)
    a1 = ds[a1_in_b1_index, 'fltvar'].x
    a2 = ds[a2_in_b1_index, 'fltvar'].x
    difference = a1 - a2
    t, p = scipy.stats.ttest_rel(a1, a2)
    assert_array_equal(res.difference.x, difference)
    assert res.df == len(a1) - 1
    assert res.tail == 0
    assert res.t == pytest.approx(t)
    assert res.p == pytest.approx(p)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, 1)
    assert_array_equal(res.difference.x, difference)
    assert res.df == len(a1) - 1
    assert res.tail == 1
    assert res.t == pytest.approx(t)
    assert res.p == pytest.approx(p / 2)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a2', 'a1', 'rm', "B=='b1'", ds, 1)
    assert_array_equal(res.difference.x, -difference)
    assert res.df == len(a1) - 1
    assert res.tail == 1
    assert res.t == pytest.approx(-t)
    assert res.p == pytest.approx(1 - p / 2)

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, -1)
    assert_array_equal(res.difference.x, difference)
    assert res.df == len(a1) - 1
    assert res.tail == -1
    assert res.t == pytest.approx(t)
    assert res.p == pytest.approx(1 - p / 2)
    print(res)
    print(asfmtext(res))
    # alternative argspec
    a1 = ds.eval("fltvar[(B == 'b1') & (A == 'a1')]")
    a2 = ds.eval("fltvar[(B == 'b1') & (A == 'a2')]")
    res_alt = test.TTestRel(a1, a2, tail=-1)
    print(res_alt)
    assert res_alt.t == res.t
    assert res_alt.p == res.p
Exemplo n.º 8
0
def test_ttest():
    """Test univariate t-test functions"""
    ds = datasets.get_uv()

    print(test.ttest('fltvar', ds=ds))
    print(test.ttest('fltvar', 'A', ds=ds))
    print(test.ttest('fltvar', 'A%B', ds=ds))
    print(test.ttest('fltvar', 'A', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A', 'a1', match='rm', ds=ds))
    print(test.ttest('fltvar', 'A%B', ('a1', 'b1'), match='rm', ds=ds))

    # Prepare data for scipy
    a1_index = ds.eval("A == 'a1'")
    a2_index = ds.eval("A == 'a2'")
    b1_index = ds.eval("B == 'b1'")
    a1_in_b1_index = np.logical_and(a1_index, b1_index)
    a2_in_b1_index = np.logical_and(a2_index, b1_index)

    # TTest1Samp
    res = test.TTest1Sample('fltvar', ds=ds)
    t, p = scipy.stats.ttest_1samp(ds['fltvar'], 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    res = test.TTest1Sample('fltvar', ds=ds, tail=1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2., 10)

    # TTestInd
    res = test.TTestInd('fltvar', 'A', 'a1', 'a2', ds=ds)
    t, p = scipy.stats.ttest_ind(ds[a1_index, 'fltvar'], ds[a2_index,
                                                            'fltvar'])
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)

    # TTestRel
    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds)
    a1 = ds[a1_in_b1_index, 'fltvar'].x
    a2 = ds[a2_in_b1_index, 'fltvar'].x
    diff = a1 - a2
    t, p = scipy.stats.ttest_rel(a1, a2)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 0)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, 1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, 1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t > 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))

    res = test.TTestRel('fltvar', 'A', 'a1', 'a2', 'rm', "B=='b1'", ds, -1)
    assert_array_equal(res.diff.x, diff)
    eq_(res.df, len(a1) - 1)
    eq_(res.tail, -1)
    assert_almost_equal(res.t, t, 10)
    assert_almost_equal(res.p, p / 2 if t < 0 else 1 - p / 2, 10)
    print(res)
    print(asfmtext(res))