def test_boosting(): "Test boosting NDVars" ds = datasets._get_continuous() # test boosting results configure(n_workers=0) yield run_boosting, ds configure(n_workers=True) yield run_boosting, ds
def test_boosting(n_workers): "Test boosting NDVars" ds = datasets._get_continuous() configure(n_workers=n_workers) y = ds['y'] x1 = ds['x1'] x2 = ds['x2'] y_mean = y.mean() x2_mean = x2.mean() # test values from running function, not verified independently res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025) assert repr( res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>' assert res.r == approx(0.75, abs=0.001) assert res.y_mean is None assert res.h.info['unit'] == 'V' assert res.h_scaled.info['unit'] == 'V' res = boosting(y, x1, 0, 1) assert repr(res) == '<boosting y ~ x1, 0 - 1>' assert res.r == approx(0.83, abs=0.001) assert res.y_mean == y_mean assert res.y_scale == y.std() assert res.x_mean == x1.mean() assert res.x_scale == x1.std() assert res.h.info['unit'] == 'normalized' assert res.h_scaled.info['unit'] == 'V' # inplace res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace') assert_res_equal(res_ip, res) # persistence res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL)) assert_res_equal(res_p, res) res = boosting(y, x2, 0, 1) assert res.r == approx(0.601, abs=0.001) res = boosting(y, x2, 0, 1, error='l1') assert res.r == approx(0.553, abs=0.001) assert res.y_mean == y.mean() assert res.y_scale == (y - y_mean).abs().mean() assert res.x_mean == x2_mean assert res.x_scale == (x2 - x2_mean).abs().mean() # 2 predictors res = boosting(y, [x1, x2], 0, 1) assert res.r == approx(0.947, abs=0.001) # selective stopping res = boosting(y, [x1, x2], 0, 1, selective_stopping=1) assert res.r == approx(0.967, abs=0.001) res = boosting(y, [x1, x2], 0, 1, selective_stopping=2) assert res.r == approx(0.992, abs=0.001)
def test_boosting(n_workers): "Test boosting NDVars" ds = datasets._get_continuous() configure(n_workers=n_workers) y = ds['y'] x1 = ds['x1'] x2 = ds['x2'] y_mean = y.mean() x2_mean = x2.mean() # test values from running function, not verified independently res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025) assert repr(res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>' assert res.r == approx(0.75, abs=0.001) assert res.y_mean is None assert res.h.info['unit'] == 'V' assert res.h_scaled.info['unit'] == 'V' res = boosting(y, x1, 0, 1) assert repr(res) == '<boosting y ~ x1, 0 - 1>' assert res.r == approx(0.83, abs=0.001) assert res.y_mean == y_mean assert res.y_scale == y.std() assert res.x_mean == x1.mean() assert res.x_scale == x1.std() assert res.h.info['unit'] == 'normalized' assert res.h_scaled.info['unit'] == 'V' # inplace res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace') assert_res_equal(res_ip, res) # persistence res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL)) assert_res_equal(res_p, res) res = boosting(y, x2, 0, 1) assert res.r == approx(0.601, abs=0.001) res = boosting(y, x2, 0, 1, error='l1') assert res.r == approx(0.553, abs=0.001) assert res.y_mean == y.mean() assert res.y_scale == (y - y_mean).abs().mean() assert res.x_mean == x2_mean assert res.x_scale == (x2 - x2_mean).abs().mean() # 2 predictors res = boosting(y, [x1, x2], 0, 1) assert res.r == approx(0.947, abs=0.001) # selective stopping res = boosting(y, [x1, x2], 0, 1, selective_stopping=1) assert res.r == approx(0.967, abs=0.001) res = boosting(y, [x1, x2], 0, 1, selective_stopping=2) assert res.r == approx(0.992, abs=0.001)
def run_example(example_path, name): "Run the example script at ``filename``" dirname, example_filename = os.path.split(example_path) # read example with open(example_path) as fid: text = fid.read() # check for explicit skip if re.findall("^# skip test", text, re.MULTILINE): return # check for required modules required_modules = re.findall("^# requires: (\w+)", text, re.MULTILINE) for module in required_modules: try: __import__(module) except ImportError: raise SkipTest("required module not available: %s" % module) # check for required datasets required_datasets = re.findall("^# dataset: (\w+)", text, re.MULTILINE) for dataset in required_datasets: if not DATASETS[dataset]: raise SkipTest("required dataset not available: %s" % dataset) # find required files required_files = re.findall("^# file: (\w+.\w+)", text, re.MULTILINE) # reduce computational load text = text.replace("n_samples = 1000", "n_samples = 2") # copy all files to temporary dir tempdir = mkdtemp() try: logging.info("Tempdir for %s at %s" % (name, tempdir)) for filename in required_files: src = os.path.join(dirname, filename) logging.info(" Copying %s" % (filename, )) shutil.copy(src, tempdir) # execute example os.chdir(tempdir) logging.info(" Executing %s" % (name, )) configure(show=False) exec(text, {}) finally: # delete temporary files # FIXME: on Windows (Appveyor) this raises a WindowsError indicating # that the folder is being used by another process if os.name != 'nt': shutil.rmtree(tempdir)
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_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 with pytest.raises(NotImplementedError): testnd.anova(y, "side*modality", tfce=True, parc='source', **kwa)
def test_example(example_dir): "Run the example script at ``filename``" configure(show=False) with TemporaryDirectory() as temp_dir, working_directory(temp_dir): temp_dir = Path(temp_dir) # link files for file in example_dir.iterdir(): if not file.name.startswith(('.', '_')): os.link(file, temp_dir / file.name) # run *.py files for example in example_dir.glob('*.py'): # check for flags text = example.read_text() if re.findall("^# skip test:", text, re.MULTILINE): return # check for required modules required_modules = re.findall(r"^# requires: (\w+)$", text, re.MULTILINE) for module in required_modules: print(repr(module)) try: import_module(module) except ImportError: pytest.skip(f"required module {module} not available") # check for required datasets required_datasets = re.findall(r"^# dataset: (\w+)$", text, re.MULTILINE) for dataset in required_datasets: if not DATASETS[dataset]: raise pytest.skip( f"required dataset {dataset} not available") # reduce computational load text = text.replace("n_samples = 1000", "n_samples = 2") # execute example logging.info(" Executing %s/%s", example_dir.name, example.name) exec(text, {})
def test_example(tmp_path, path: Path): "Run the example script at ``filename``" # check for flags text = path.read_text() if re.findall("^# skip test:", text, re.MULTILINE): return # check for required modules required_modules = re.findall(r"^# requires: (\w+)$", text, re.MULTILINE) for module in required_modules: try: importlib.import_module(module) except ImportError: pytest.skip(f"required module {module} not available") # check for required datasets required_datasets = re.findall(r"^# dataset: (\w+)$", text, re.MULTILINE) for dataset in required_datasets: if not DATASETS[dataset]: raise pytest.skip(f"required dataset {dataset} not available") # set up context configure(show=False) with working_directory(tmp_path): temp_dir = Path(tmp_path) # link files for file in path.parent.glob('*.*'): if file.name.startswith(('.', '_')): continue elif file.name in text: os.link(file, temp_dir / file.name) # reduce computational load text = text.replace("n_samples = 1000", "n_samples = 2") # prepare example script exa_path = temp_dir / path.name exa_path.write_text(text) logging.info(" Executing %s/%s", path.parent.name, path.name) # execute example spec = importlib.util.spec_from_file_location(exa_path.stem, exa_path) example_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(example_module)
def test_example(example_dir): "Run the example script at ``filename``" configure(show=False) with TemporaryDirectory() as temp_dir, working_directory(temp_dir): temp_dir = Path(temp_dir) # link files for file in example_dir.iterdir(): if not file.name.startswith(('.', '_')): os.link(file, temp_dir / file.name) # run *.py files for example in example_dir.glob('*.py'): # check for flags text = example.read_text() if re.findall("^# skip test:", text, re.MULTILINE): return # check for required modules required_modules = re.findall(r"^# requires: (\w+)$", text, re.MULTILINE) for module in required_modules: print(repr(module)) try: import_module(module) except ImportError: pytest.skip(f"required module {module} not available") # check for required datasets required_datasets = re.findall(r"^# dataset: (\w+)$", text, re.MULTILINE) for dataset in required_datasets: if not DATASETS[dataset]: raise pytest.skip(f"required dataset {dataset} not available") # reduce computational load text = text.replace("n_samples = 1000", "n_samples = 2") # execute example logging.info(" Executing %s/%s", example_dir.name, example.name) exec(text, {})
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
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_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) 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. assert_raises(ZeroVariance, testnd.ttest_rel, 'utsnd', 'A', match='rm', 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_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)
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
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)
def use_pyplot(gallery_conf, fname): eelbrain.configure(frame=False)
def test_boosting(n_workers): "Test boosting NDVars" ds = datasets._get_continuous(ynd=True) configure(n_workers=n_workers) y = ds['y'] ynd = ds['ynd'] x1 = ds['x1'] x2 = ds['x2'] y_mean = y.mean() x2_mean = x2.mean('time') # test values from running function, not verified independently res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025) assert repr( res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>' assert res.r == approx(0.75, abs=0.001) assert res.y_mean is None assert res.h.info['unit'] == 'V' assert res.h_scaled.info['unit'] == 'V' with pytest.raises(NotImplementedError): res.proportion_explained res = boosting(y, x1, 0, 1) assert repr(res) == '<boosting y ~ x1, 0 - 1>' assert res.r == approx(0.83, abs=0.001) assert res.y_mean == y_mean assert res.y_scale == y.std() assert res.x_mean == x1.mean() assert res.x_scale == x1.std() assert res.h.name == 'x1' assert res.h.info['unit'] == 'normalized' assert res.h_scaled.name == 'x1' assert res.h_scaled.info['unit'] == 'V' assert res.proportion_explained == approx(0.506, abs=0.001) # inplace res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace') assert_res_equal(res_ip, res) # persistence res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL)) assert_res_equal(res_p, res) res = boosting(y, x2, 0, 1) assert res.r == approx(0.601, abs=0.001) assert res.proportion_explained == approx(0.273, abs=0.001) res = boosting(y, x2, 0, 1, error='l1') assert res.r == approx(0.553, abs=0.001) assert res.y_mean == y.mean() assert res.y_scale == (y - y_mean).abs().mean() assert_array_equal(res.x_mean.x, x2_mean) assert_array_equal(res.x_scale, (x2 - x2_mean).abs().mean('time')) assert res.proportion_explained == approx(0.123, abs=0.001) # 2 predictors res = boosting(y, [x1, x2], 0, 1) assert res.r == approx(0.947, abs=0.001) # selective stopping res = boosting(y, [x1, x2], 0, 1, selective_stopping=1) assert res.r == approx(0.967, abs=0.001) res = boosting(y, [x1, x2], 0, 1, selective_stopping=2) assert res.r == approx(0.992, abs=0.001) # prefit res_full = boosting(y, [x1, x2], 0, 1) prefit = boosting(y, x1, 0, 1) res = boosting(y, [x1, x2], 0, 1, prefit=prefit) assert correlation_coefficient(res.h, res_full.h[1]) == approx(0.984, 1e-3) prefit = boosting(y, x2, 0, 1) res = boosting(y, [x1, x2], 0, 1, prefit=prefit) assert correlation_coefficient(res.h, res_full.h[0]) == approx(0.995, 1e-3) # ynd res_full = boosting(ynd, [x1, x2], 0, 1) prefit = boosting(ynd, x1, 0, 1) res = boosting(ynd, [x1, x2], 0, 1, prefit=prefit) assert correlation_coefficient(res.h, res_full.h[1]) == approx(0.978, 1e-3) prefit = boosting(ynd, x2, 0, 1) res = boosting(ynd, [x1, x2], 0, 1, prefit=prefit) assert correlation_coefficient(res.h, res_full.h[0]) == approx(0.997, 1e-3)
__version__ = "1.2.0" __email__ = "*****@*****.**" ''' #################################### IMPORT MODULES #################################### ''' import matplotlib matplotlib.use('TkAgg') import autoreject import pickle import eelbrain from eelbrain.gui import select_epochs eelbrain.configure(prompt_toolkit=False) # disable running the GUI main loop in parallel to the Terminal from general_info_general import * from base_funcs_general import * ''' #################################### START PREPROCESSING #################################### ''' raw = mne.io.read_raw_fif (raw_alldata_fileName , preload = True) raw = mne.io.read_raw_fif (raw_alldata_fileName , preload = True) # read fif image
def use_pyplot(gallery_conf, fname): eelbrain.configure(frame=False)
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_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)
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)