def test_plot_results(): "Test plotting test results" ds = datasets.get_uts(True) # ANOVA res = testnd.anova('utsnd', 'A*B*rm', match='rm', ds=ds, samples=0, pmin=0.05) p = plot.Array(res, show=False) p.close() res = testnd.anova('utsnd', 'A*B*rm', match='rm', ds=ds, samples=2, pmin=0.05) p = plot.Array(res, show=False) p.close() # Correlation res = testnd.corr('utsnd', 'Y', 'rm', ds=ds) p = plot.Array(res, show=False) p.close() res = testnd.corr('utsnd', 'Y', 'rm', ds=ds, samples=10, pmin=0.05) p = plot.Array(res, show=False) p.close()
def test_clusters(): "test plot.uts cluster plotting functions" ds = datasets.get_uts() A = ds['A'] B = ds['B'] Y = ds['uts'] # fixed effects model res = testnd.anova(Y, A * B) p = plot.UTSClusters(res, title="Fixed Effects Model", show=False) p.close() # random effects model: subject = Factor(range(15), tile=4, random=True, name='subject') res = testnd.anova(Y, A * B * subject, samples=2) p = plot.UTSClusters(res, title="Random Effects Model", show=False) p.close() # plot UTSStat p = plot.UTSStat(Y, A % B, match=subject, show=False) p.set_clusters(res.clusters) p.close() p = plot.UTSStat(Y, A, Xax=B, match=subject, show=False) p.close()
def test_clusters(): "test plot.uts cluster plotting functions" plot.configure_backend(False, False) ds = datasets.get_rand() A = ds['A'] B = ds['B'] Y = ds['uts'] # fixed effects model res = testnd.anova(Y, A * B) p = plot.UTSClusters(res, title="Fixed Effects Model") p.close() # random effects model: subject = Factor(range(15), tile=4, random=True, name='subject') res = testnd.anova(Y, A * B * subject, samples=2) p = plot.UTSClusters(res, title="Random Effects Model") p.close() # plot UTSStat p = plot.UTSStat(Y, A % B, match=subject) p.set_clusters(res.clusters) p.close() p = plot.UTSStat(Y, A, Xax=B, match=subject) p.close()
def test_ndanova(): ds = datasets.get_uts(nrm=True) ds['An'] = ds['A'].as_var({'a0': 0, 'a1': 1}) with pytest.raises(NotImplementedError): testnd.anova('uts', 'An*B*rm', ds=ds) # nested random effect res = testnd.anova('uts', 'A + A%B + B * nrm(A)', ds=ds, match='nrm', samples=100, pmin=0.05) assert len(res.find_clusters(0.05)) == 8
def test_anova_parc(): "Test ANOVA with parc argument and source space data" set_log_level('warning', 'mne') ds = datasets.get_mne_sample(src='ico', sub="side.isin(('L', 'R'))") y = ds['src'].sub(source=('lateraloccipital-lh', 'cuneus-lh')) y1 = y.sub(source='lateraloccipital-lh') y2 = y.sub(source='cuneus-lh') kwa = dict(ds=ds, tstart=0.2, tstop=0.3, samples=100) resp = testnd.anova(y, "side*modality", pmin=0.05, parc='source', **kwa) c1p = resp.find_clusters(source='lateraloccipital-lh') c2p = resp.find_clusters(source='cuneus-lh') del c1p['p_parc', 'id'] del c2p['p_parc', 'id'] res1 = testnd.anova(y1, "side*modality", pmin=0.05, **kwa) c1 = res1.find_clusters() del c1['id'] res2 = testnd.anova(y2, "side*modality", pmin=0.05, **kwa) c2 = res2.find_clusters() del c2['id'] assert_dataset_equal(c1p, c1) assert_dataset_equal(c2p, c2) assert_array_equal(c2['p'], [ 0.85, 0.88, 0.97, 0.75, 0.99, 0.99, 0.98, 0.0, 0.12, 0.88, 0.25, 0.97, 0.34, 0.96 ]) # without multiprocessing configure(n_workers=0) ress = testnd.anova(y, "side*modality", pmin=0.05, parc='source', **kwa) c1s = ress.find_clusters(source='lateraloccipital-lh') c2s = ress.find_clusters(source='cuneus-lh') del c1s['p_parc', 'id'] del c2s['p_parc', 'id'] assert_dataset_equal(c1s, c1) assert_dataset_equal(c2s, c2) configure(n_workers=True) # parc but single label resp2 = testnd.anova(y2, "side*modality", pmin=0.05, parc='source', **kwa) c2sp = resp2.find_clusters(source='cuneus-lh') del c2sp['p_parc', 'id'] assert_dataset_equal(c2sp, c2) # not defined assert_raises(NotImplementedError, testnd.anova, y, "side*modality", tfce=True, parc='source', **kwa)
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)
def test_anova_parc(): "Test ANOVA with parc argument and source space data" set_log_level('warning', 'mne') ds = datasets.get_mne_sample(src='ico', sub="side.isin(('L', 'R'))") y = ds['src'].sub(source=('lateraloccipital-lh', 'cuneus-lh')) y1 = y.sub(source='lateraloccipital-lh') y2 = y.sub(source='cuneus-lh') kwa = dict(ds=ds, tstart=0.2, tstop=0.3, samples=100) resp = testnd.anova(y, "side*modality", pmin=0.05, parc='source', **kwa) c1p = resp.find_clusters(source='lateraloccipital-lh') c2p = resp.find_clusters(source='cuneus-lh') del c1p['p_parc', 'id'] del c2p['p_parc', 'id'] res1 = testnd.anova(y1, "side*modality", pmin=0.05, **kwa) c1 = res1.find_clusters() del c1['id'] res2 = testnd.anova(y2, "side*modality", pmin=0.05, **kwa) c2 = res2.find_clusters() del c2['id'] assert_dataset_equal(c1p, c1) assert_dataset_equal(c2p, c2) assert_array_equal(c2['p'], [0.85, 0.88, 0.97, 0.75, 0.99, 0.99, 0.98, 0.0, 0.12, 0.88, 0.25, 0.97, 0.34, 0.96]) # without multiprocessing testnd.configure(0) ress = testnd.anova(y, "side*modality", pmin=0.05, parc='source', **kwa) c1s = ress.find_clusters(source='lateraloccipital-lh') c2s = ress.find_clusters(source='cuneus-lh') del c1s['p_parc', 'id'] del c2s['p_parc', 'id'] assert_dataset_equal(c1s, c1) assert_dataset_equal(c2s, c2) testnd.configure(-1) # parc but single label resp2 = testnd.anova(y2, "side*modality", pmin=0.05, parc='source', **kwa) c2sp = resp2.find_clusters(source='cuneus-lh') del c2sp['p_parc', 'id'] assert_dataset_equal(c2sp, c2) # not defined assert_raises(NotImplementedError, testnd.anova, y, "side*modality", tfce=True, parc='source', **kwa)
def run_as_ndanova(y, x, ds): yt = ds.eval(y).x[:, None] y2 = np.concatenate((yt, yt * 2), 1) ndvar = NDVar(y2, ('case', UTS(0, 0.1, 2))) res = testnd.anova(ndvar, x, ds=ds) f1 = [fmap.x[0] for fmap in res.f] f2 = [fmap.x[1] for fmap in res.f] for f1_, f2_ in izip(f1, f2): eq_(f1_, f2_) return f1
def test_plot_results(): "Test plotting test results" ds = datasets.get_uts(True) # ANOVA res = testnd.anova("utsnd", "A*B*rm", ds=ds, samples=0, pmin=0.05) p = plot.Array(res, show=False) p.close() res = testnd.anova("utsnd", "A*B*rm", ds=ds, samples=2, pmin=0.05) p = plot.Array(res, show=False) p.close() # Correlation res = testnd.corr("utsnd", "Y", "rm", ds=ds) p = plot.Array(res, show=False) p.close() res = testnd.corr("utsnd", "Y", "rm", ds=ds, samples=10, pmin=0.05) p = plot.Array(res, show=False) p.close()
def run_as_ndanova(y, x, ds): yt = ds.eval(y).x[:, None] y2 = np.concatenate((yt, yt * 2), 1) ndvar = NDVar(y2, ('case', UTS(0, 0.1, 2))) res = testnd.anova(ndvar, x, ds=ds) f1 = [fmap.x[0] for fmap in res.f] f2 = [fmap.x[1] for fmap in res.f] for f1_, f2_ in zip(f1, f2): assert f1_ == f2_ return f1
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)
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)
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)
def test_anova_incremental(): "Test testnd.anova() with incremental f-tests" ds = datasets.get_uts() testnd.anova('uts', 'A*B', ds=ds[3:], pmin=0.05, samples=10)
def test_anova(): "Test testnd.anova()" ds = datasets.get_uts(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) testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=0, pmin=0.05) repr(res) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=2, pmin=0.05) repr(res) # persistence string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL) res_ = pickle.loads(string) assert_equal(repr(res_), repr(res)) # threshold-free res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10) repr(res) assert_in('A clusters', res.clusters.info) assert_in('B clusters', res.clusters.info) assert_in('A x B clusters', 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_in('v', res.clusters) assert_in('p', res.clusters) # all effects with clusters res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5, pmin=0.05, tstart=0.1, mintime=0.02) assert_equal(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) 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) 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_equal(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 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) testnd.configure(0) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=5) assert_dataset_equal(res.clusters, res0.clusters) testnd.configure(-1) # 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: eq_(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: eq_(len(dist.dist), samples) assert_array_equal(dist.dist, dist.tfce_map.abs().max()) # thresholded res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=samples) clusters = res.find_clusters() for dist, effect in izip(res._cdist, res.effects): effect_idx = clusters.eval("effect == %r" % effect) vmax = clusters[effect_idx, 'v'].abs().max() eq_(len(dist.dist), samples) assert_array_equal(dist.dist, vmax) eelbrain._stats.permutation._YIELD_ORIGINAL = 0 # 1d TFCE testnd.configure(0) res = testnd.anova('utsnd.rms(time=(0.1, 0.3))', 'A*B*rm', ds=ds, tfce=True, samples=samples) testnd.configure(-1)
def test_anova(): "Test testnd.anova()" plot.configure_backend(False, False) ds = datasets.get_uts(True) testnd.anova('utsnd', 'A*B', ds=ds) for samples in (0, 2): logger.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) testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=0, pmin=0.05) repr(res) p = plot.Array(res) p.close() res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=2, pmin=0.05) repr(res) p = plot.Array(res) p.close() # persistence string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL) res_ = pickle.loads(string) assert_equal(repr(res_), repr(res)) # threshold-free res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10) repr(res) assert_in('A clusters', res.clusters.info) assert_in('B clusters', res.clusters.info) assert_in('A x B clusters', 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_in('v', res.clusters) assert_in('p', res.clusters) # all effects with clusters res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5, pmin=0.05, tstart=0.1, mintime=0.02) assert_equal(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) 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) repr(res) string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL) res = pickle.loads(string) tfce_clusters = res.find_clusters(pmin=0.05) peaks = res.find_peaks() assert_equal(tfce_clusters.eval("p.min()"), peaks.eval("p.min()")) unmasked = res.f[0] masked = res.masked_parameter_map(effect=0, pmin=0.05) assert_array_equal(masked.x <= unmasked.x, True)
def test_anova(): "Test testnd.anova()" plot.configure_backend(False, False) ds = datasets.get_rand(True) testnd.anova('utsnd', 'A*B', ds=ds) for samples in (0, 2): logger.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) testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=0, pmin=0.05) repr(res) p = plot.Array(res) p.close() res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=2, pmin=0.05) repr(res) p = plot.Array(res) p.close() # persistence string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL) res_ = pickle.loads(string) assert_equal(repr(res_), repr(res)) # threshold-free res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10) repr(res) assert_in('A clusters', res.clusters.info) assert_in('B clusters', res.clusters.info) assert_in('A x B clusters', 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_in('v', res.clusters) assert_in('p', res.clusters) # all effects with clusters res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5, pmin=0.05, tstart=0.1, mintime=0.02) assert_equal(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) 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) repr(res) string = pickle.dumps(res, pickle.HIGHEST_PROTOCOL) res = pickle.loads(string) tfce_clusters = res._clusters(pmin=0.05) peaks = res.find_peaks() assert_equal(tfce_clusters.eval("p.min()"), peaks.eval("p.min()")) unmasked = res.f[0] masked = res.masked_parameter_map(effect=0, pmin=0.05) assert_array_equal(masked.x <= unmasked.x, True)
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)
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)
def test_anova(): "Test testnd.anova()" ds = datasets.get_uts(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) testnd.anova('utsnd', 'A*B', ds=ds, samples=samples, tfce=True) res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=0, pmin=0.05) eq_( repr(res), "<anova 'utsnd', 'A*B*rm', samples=0, pmin=0.05, " "'A': 17 clusters, 'B': 20 clusters, 'A x B': 22 clusters>") res = testnd.anova('utsnd', 'A*B*rm', match='rm', ds=ds, samples=2, pmin=0.05) eq_( repr(res), "<anova 'utsnd', 'A*B*rm', match='rm', samples=2, pmin=0.05, " "'A': 17 clusters, p >= 0.000, 'B': 20 clusters, p >= 0.000, " "'A x B': 22 clusters, p >= 0.000>") # persistence string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL) res_ = pickle.loads(string) assert_equal(repr(res_), repr(res)) # threshold-free res = testnd.anova('utsnd', 'A*B*rm', ds=ds, samples=10) repr(res) assert_in('A clusters', res.clusters.info) assert_in('B clusters', res.clusters.info) assert_in('A x B clusters', 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_in('v', res.clusters) assert_in('p', res.clusters) # all effects with clusters res = testnd.anova('uts', 'A*B*rm', ds=ds, samples=5, pmin=0.05, tstart=0.1, mintime=0.02) assert_equal(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) 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) 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_equal(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 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) 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) 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: eq_(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: eq_(len(dist.dist), samples) assert_array_equal(dist.dist, dist.tfce_map.abs().max()) # thresholded res = testnd.anova('utsnd', 'A*B*rm', ds=ds, pmin=0.05, samples=samples) clusters = res.find_clusters() for dist, effect in izip(res._cdist, res.effects): effect_idx = clusters.eval("effect == %r" % effect) vmax = clusters[effect_idx, 'v'].abs().max() eq_(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 ds['utsnd'].x[:, 1, 10] = 0. assert_raises(ZeroVariance, testnd.anova, 'utsnd', 'A', ds=ds) assert_raises(ZeroVariance, testnd.anova, 'utsnd', 'A*B*rm', ds=ds)
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