nimare 0.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- benchmarks/__init__.py +0 -0
- benchmarks/bench_cbma.py +57 -0
- nimare/__init__.py +45 -0
- nimare/_version.py +21 -0
- nimare/annotate/__init__.py +21 -0
- nimare/annotate/cogat.py +213 -0
- nimare/annotate/gclda.py +924 -0
- nimare/annotate/lda.py +147 -0
- nimare/annotate/text.py +75 -0
- nimare/annotate/utils.py +87 -0
- nimare/base.py +217 -0
- nimare/cli.py +124 -0
- nimare/correct.py +462 -0
- nimare/dataset.py +685 -0
- nimare/decode/__init__.py +33 -0
- nimare/decode/base.py +115 -0
- nimare/decode/continuous.py +462 -0
- nimare/decode/discrete.py +753 -0
- nimare/decode/encode.py +110 -0
- nimare/decode/utils.py +44 -0
- nimare/diagnostics.py +510 -0
- nimare/estimator.py +139 -0
- nimare/extract/__init__.py +19 -0
- nimare/extract/extract.py +466 -0
- nimare/extract/utils.py +295 -0
- nimare/generate.py +331 -0
- nimare/io.py +667 -0
- nimare/meta/__init__.py +39 -0
- nimare/meta/cbma/__init__.py +6 -0
- nimare/meta/cbma/ale.py +951 -0
- nimare/meta/cbma/base.py +947 -0
- nimare/meta/cbma/mkda.py +1361 -0
- nimare/meta/cbmr.py +970 -0
- nimare/meta/ibma.py +1683 -0
- nimare/meta/kernel.py +501 -0
- nimare/meta/models.py +1199 -0
- nimare/meta/utils.py +494 -0
- nimare/nimads.py +492 -0
- nimare/reports/__init__.py +24 -0
- nimare/reports/base.py +664 -0
- nimare/reports/default.yml +123 -0
- nimare/reports/figures.py +651 -0
- nimare/reports/report.tpl +160 -0
- nimare/resources/__init__.py +1 -0
- nimare/resources/atlases/Harvard-Oxford-LICENSE +93 -0
- nimare/resources/atlases/HarvardOxford-cort-maxprob-thr25-2mm.nii.gz +0 -0
- nimare/resources/database_file_manifest.json +142 -0
- nimare/resources/english_spellings.csv +1738 -0
- nimare/resources/filenames.json +32 -0
- nimare/resources/neurosynth_laird_studies.json +58773 -0
- nimare/resources/neurosynth_stoplist.txt +396 -0
- nimare/resources/nidm_pain_dset.json +1349 -0
- nimare/resources/references.bib +541 -0
- nimare/resources/semantic_knowledge_children.txt +325 -0
- nimare/resources/semantic_relatedness_children.txt +249 -0
- nimare/resources/templates/MNI152_2x2x2_brainmask.nii.gz +0 -0
- nimare/resources/templates/tpl-MNI152NLin6Asym_res-01_T1w.nii.gz +0 -0
- nimare/resources/templates/tpl-MNI152NLin6Asym_res-01_desc-brain_mask.nii.gz +0 -0
- nimare/resources/templates/tpl-MNI152NLin6Asym_res-02_T1w.nii.gz +0 -0
- nimare/resources/templates/tpl-MNI152NLin6Asym_res-02_desc-brain_mask.nii.gz +0 -0
- nimare/results.py +225 -0
- nimare/stats.py +276 -0
- nimare/tests/__init__.py +1 -0
- nimare/tests/conftest.py +229 -0
- nimare/tests/data/amygdala_roi.nii.gz +0 -0
- nimare/tests/data/data-neurosynth_version-7_coordinates.tsv.gz +0 -0
- nimare/tests/data/data-neurosynth_version-7_metadata.tsv.gz +0 -0
- nimare/tests/data/data-neurosynth_version-7_vocab-terms_source-abstract_type-tfidf_features.npz +0 -0
- nimare/tests/data/data-neurosynth_version-7_vocab-terms_vocabulary.txt +100 -0
- nimare/tests/data/neurosynth_dset.json +2868 -0
- nimare/tests/data/neurosynth_laird_studies.json +58773 -0
- nimare/tests/data/nidm_pain_dset.json +1349 -0
- nimare/tests/data/nimads_annotation.json +1 -0
- nimare/tests/data/nimads_studyset.json +1 -0
- nimare/tests/data/test_baseline.txt +2 -0
- nimare/tests/data/test_pain_dataset.json +1278 -0
- nimare/tests/data/test_pain_dataset_multiple_contrasts.json +1242 -0
- nimare/tests/data/test_sleuth_file.txt +18 -0
- nimare/tests/data/test_sleuth_file2.txt +10 -0
- nimare/tests/data/test_sleuth_file3.txt +5 -0
- nimare/tests/data/test_sleuth_file4.txt +5 -0
- nimare/tests/data/test_sleuth_file5.txt +5 -0
- nimare/tests/test_annotate_cogat.py +32 -0
- nimare/tests/test_annotate_gclda.py +86 -0
- nimare/tests/test_annotate_lda.py +27 -0
- nimare/tests/test_dataset.py +99 -0
- nimare/tests/test_decode_continuous.py +132 -0
- nimare/tests/test_decode_discrete.py +92 -0
- nimare/tests/test_diagnostics.py +168 -0
- nimare/tests/test_estimator_performance.py +385 -0
- nimare/tests/test_extract.py +46 -0
- nimare/tests/test_generate.py +247 -0
- nimare/tests/test_io.py +294 -0
- nimare/tests/test_meta_ale.py +298 -0
- nimare/tests/test_meta_cbmr.py +295 -0
- nimare/tests/test_meta_ibma.py +240 -0
- nimare/tests/test_meta_kernel.py +209 -0
- nimare/tests/test_meta_mkda.py +234 -0
- nimare/tests/test_nimads.py +21 -0
- nimare/tests/test_reports.py +110 -0
- nimare/tests/test_stats.py +101 -0
- nimare/tests/test_transforms.py +272 -0
- nimare/tests/test_utils.py +200 -0
- nimare/tests/test_workflows.py +221 -0
- nimare/tests/utils.py +126 -0
- nimare/transforms.py +907 -0
- nimare/utils.py +1367 -0
- nimare/workflows/__init__.py +14 -0
- nimare/workflows/base.py +189 -0
- nimare/workflows/cbma.py +165 -0
- nimare/workflows/ibma.py +108 -0
- nimare/workflows/macm.py +77 -0
- nimare/workflows/misc.py +65 -0
- nimare-0.4.2.dist-info/LICENSE +21 -0
- nimare-0.4.2.dist-info/METADATA +124 -0
- nimare-0.4.2.dist-info/RECORD +119 -0
- nimare-0.4.2.dist-info/WHEEL +5 -0
- nimare-0.4.2.dist-info/entry_points.txt +2 -0
- nimare-0.4.2.dist-info/top_level.txt +2 -0
@@ -0,0 +1,18 @@
|
|
1
|
+
// Reference = MNI
|
2
|
+
// StudyName: ExperimentName
|
3
|
+
// Subjects = 50
|
4
|
+
-16 -58 -22
|
5
|
+
26 -64 -50
|
6
|
+
12 -70 -42
|
7
|
+
-10 12 50
|
8
|
+
|
9
|
+
// StudyName2: ExperimentName
|
10
|
+
// Subjects = 46
|
11
|
+
-9 -72 57
|
12
|
+
-15 9 -3
|
13
|
+
|
14
|
+
// StudyName3: ExperimentName
|
15
|
+
// StudyName3: ExperimentName2
|
16
|
+
// StudyName3: ExperimentName3
|
17
|
+
// Subjects = 20
|
18
|
+
-9 -72 57
|
@@ -0,0 +1,32 @@
|
|
1
|
+
"""Test nimare.annotate.cogat (Cognitive Atlas extraction methods)."""
|
2
|
+
|
3
|
+
import pandas as pd
|
4
|
+
|
5
|
+
from nimare import annotate, extract, utils
|
6
|
+
|
7
|
+
|
8
|
+
def test_cogat(testdata_laird):
|
9
|
+
"""A smoke test for CogAt-related functions."""
|
10
|
+
# A small test dataset with abstracts
|
11
|
+
ns_dset_laird = testdata_laird.copy()
|
12
|
+
cogat = extract.download_cognitive_atlas(data_dir=utils.get_resource_path(), overwrite=False)
|
13
|
+
id_df = pd.read_csv(cogat["ids"])
|
14
|
+
rel_df = pd.read_csv(cogat["relationships"])
|
15
|
+
weights = {"isKindOf": 1, "isPartOf": 1, "inCategory": 1}
|
16
|
+
counts_df, rep_text_df = annotate.cogat.extract_cogat(
|
17
|
+
ns_dset_laird.texts, id_df, text_column="abstract"
|
18
|
+
)
|
19
|
+
assert "id" in ns_dset_laird.texts.columns
|
20
|
+
expanded_df = annotate.cogat.expand_counts(counts_df, rel_df, weights)
|
21
|
+
assert isinstance(expanded_df, pd.DataFrame)
|
22
|
+
|
23
|
+
|
24
|
+
def test_CogAtLemmatizer():
|
25
|
+
"""A smoke test for CogAtLemmatizer."""
|
26
|
+
cogat = extract.download_cognitive_atlas(data_dir=utils.get_resource_path(), overwrite=False)
|
27
|
+
id_df = pd.read_csv(cogat["ids"])
|
28
|
+
id_df = id_df.loc[id_df["id"] == "trm_4aae62e4ad209"]
|
29
|
+
lem = annotate.cogat.CogAtLemmatizer(id_df)
|
30
|
+
true_text = "trm_4aae62e4ad209 is great"
|
31
|
+
test_text = "Cognitive control is great"
|
32
|
+
assert lem.transform(test_text) == true_text
|
@@ -0,0 +1,86 @@
|
|
1
|
+
"""Test nimare.annotate.gclda (GCLDA)."""
|
2
|
+
|
3
|
+
import nibabel as nib
|
4
|
+
import numpy as np
|
5
|
+
import pandas as pd
|
6
|
+
import pytest
|
7
|
+
|
8
|
+
from nimare import annotate, decode
|
9
|
+
|
10
|
+
|
11
|
+
def test_gclda_symmetric(testdata_laird):
|
12
|
+
"""A smoke test for GCLDA with symmetric regions."""
|
13
|
+
counts_df = annotate.text.generate_counts(
|
14
|
+
testdata_laird.texts,
|
15
|
+
text_column="abstract",
|
16
|
+
tfidf=False,
|
17
|
+
min_df=1,
|
18
|
+
max_df=1.0,
|
19
|
+
)
|
20
|
+
|
21
|
+
with pytest.raises(ValueError):
|
22
|
+
annotate.gclda.GCLDAModel(
|
23
|
+
counts_df,
|
24
|
+
testdata_laird.coordinates,
|
25
|
+
mask=testdata_laird.masker.mask_img,
|
26
|
+
n_regions=3,
|
27
|
+
symmetric=True,
|
28
|
+
)
|
29
|
+
|
30
|
+
model = annotate.gclda.GCLDAModel(
|
31
|
+
counts_df,
|
32
|
+
testdata_laird.coordinates,
|
33
|
+
mask=testdata_laird.masker.mask_img,
|
34
|
+
n_regions=2,
|
35
|
+
symmetric=True,
|
36
|
+
)
|
37
|
+
model.fit(n_iters=5, loglikely_freq=5)
|
38
|
+
|
39
|
+
# Create ROI to decode
|
40
|
+
arr = np.zeros(testdata_laird.masker.mask_img.shape, np.int32)
|
41
|
+
arr[40:44, 45:49, 40:44] = 1
|
42
|
+
mask_img = nib.Nifti1Image(arr, testdata_laird.masker.mask_img.affine)
|
43
|
+
decoded_df, _ = decode.discrete.gclda_decode_roi(model, mask_img)
|
44
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
45
|
+
|
46
|
+
# Decode the ROI as a continuous map
|
47
|
+
decoded_df, _ = decode.continuous.gclda_decode_map(model, mask_img)
|
48
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
49
|
+
|
50
|
+
# Encode text
|
51
|
+
encoded_img, _ = decode.encode.gclda_encode(model, "fmri activation")
|
52
|
+
assert isinstance(encoded_img, nib.Nifti1Image)
|
53
|
+
|
54
|
+
|
55
|
+
def test_gclda_asymmetric(testdata_laird):
|
56
|
+
"""A smoke test for GCLDA with three asymmetric regions."""
|
57
|
+
counts_df = annotate.text.generate_counts(
|
58
|
+
testdata_laird.texts,
|
59
|
+
text_column="abstract",
|
60
|
+
tfidf=False,
|
61
|
+
min_df=1,
|
62
|
+
max_df=1.0,
|
63
|
+
)
|
64
|
+
model = annotate.gclda.GCLDAModel(
|
65
|
+
counts_df,
|
66
|
+
testdata_laird.coordinates,
|
67
|
+
mask=testdata_laird.masker.mask_img,
|
68
|
+
n_regions=3,
|
69
|
+
symmetric=False,
|
70
|
+
)
|
71
|
+
model.fit(n_iters=5, loglikely_freq=5)
|
72
|
+
|
73
|
+
# Create ROI to decode
|
74
|
+
arr = np.zeros(testdata_laird.masker.mask_img.shape, np.int32)
|
75
|
+
arr[40:44, 45:49, 40:44] = 1
|
76
|
+
mask_img = nib.Nifti1Image(arr, testdata_laird.masker.mask_img.affine)
|
77
|
+
decoded_df, _ = decode.discrete.gclda_decode_roi(model, mask_img)
|
78
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
79
|
+
|
80
|
+
# Decode the ROI as a continuous map
|
81
|
+
decoded_df, _ = decode.continuous.gclda_decode_map(model, mask_img)
|
82
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
83
|
+
|
84
|
+
# Encode text
|
85
|
+
encoded_img, _ = decode.encode.gclda_encode(model, "fmri activation")
|
86
|
+
assert isinstance(encoded_img, nib.Nifti1Image)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
"""Test nimare.annotate.lda (LDA)."""
|
2
|
+
|
3
|
+
import numpy as np
|
4
|
+
import pandas as pd
|
5
|
+
|
6
|
+
from nimare import annotate
|
7
|
+
|
8
|
+
|
9
|
+
def test_lda(testdata_laird):
|
10
|
+
"""A smoke test for LDA."""
|
11
|
+
N_TOPICS = 5
|
12
|
+
model = annotate.lda.LDAModel(
|
13
|
+
n_topics=N_TOPICS,
|
14
|
+
max_iter=100,
|
15
|
+
text_column="abstract",
|
16
|
+
)
|
17
|
+
new_dset = model.fit(testdata_laird)
|
18
|
+
topic_columns = [c for c in new_dset.annotations.columns if c.startswith("LDA")]
|
19
|
+
assert len(topic_columns) == N_TOPICS
|
20
|
+
|
21
|
+
assert hasattr(model, "distributions_")
|
22
|
+
assert "p_topic_g_word" in model.distributions_.keys()
|
23
|
+
assert isinstance(model.distributions_["p_topic_g_word"], np.ndarray)
|
24
|
+
assert model.distributions_["p_topic_g_word"].shape[0] == N_TOPICS
|
25
|
+
assert "p_topic_g_word_df" in model.distributions_.keys()
|
26
|
+
assert isinstance(model.distributions_["p_topic_g_word_df"], pd.DataFrame)
|
27
|
+
assert model.distributions_["p_topic_g_word_df"].shape[0] == N_TOPICS
|
@@ -0,0 +1,99 @@
|
|
1
|
+
"""Test nimare.dataset (Dataset IO/transformations)."""
|
2
|
+
|
3
|
+
import copy
|
4
|
+
import json
|
5
|
+
import os.path as op
|
6
|
+
import warnings
|
7
|
+
|
8
|
+
import nibabel as nib
|
9
|
+
import numpy as np
|
10
|
+
import pytest
|
11
|
+
|
12
|
+
import nimare
|
13
|
+
from nimare import dataset
|
14
|
+
from nimare.tests.utils import get_test_data_path
|
15
|
+
|
16
|
+
|
17
|
+
def test_dataset_smoke():
|
18
|
+
"""Smoke test for nimare.dataset.Dataset initialization and get methods."""
|
19
|
+
db_file = op.join(get_test_data_path(), "neurosynth_dset.json")
|
20
|
+
dset = dataset.Dataset(db_file)
|
21
|
+
dset.update_path(get_test_data_path())
|
22
|
+
assert isinstance(dset, nimare.dataset.Dataset)
|
23
|
+
# Test that Dataset.masker is portable
|
24
|
+
assert not nib.is_proxy(dset.masker.mask_img_.dataobj)
|
25
|
+
|
26
|
+
methods = [dset.get_images, dset.get_labels, dset.get_metadata, dset.get_texts]
|
27
|
+
for method in methods:
|
28
|
+
assert isinstance(method(), list)
|
29
|
+
assert isinstance(method(ids=dset.ids[:5]), list)
|
30
|
+
assert isinstance(method(ids=dset.ids[0]), list)
|
31
|
+
|
32
|
+
assert isinstance(dset.get_images(imtype="beta"), list)
|
33
|
+
assert isinstance(dset.get_metadata(field="sample_sizes"), list)
|
34
|
+
assert isinstance(dset.get_studies_by_label("cogat_cognitive_control"), list)
|
35
|
+
assert isinstance(dset.get_studies_by_coordinate(np.array([[20, 20, 20]])), list)
|
36
|
+
|
37
|
+
# If label is not available, raise ValueError
|
38
|
+
with pytest.raises(ValueError):
|
39
|
+
dset.get_studies_by_label("dog")
|
40
|
+
|
41
|
+
mask_data = np.zeros(dset.masker.mask_img.shape, np.int32)
|
42
|
+
mask_data[40, 40, 40] = 1
|
43
|
+
mask_img = nib.Nifti1Image(mask_data, dset.masker.mask_img.affine)
|
44
|
+
assert isinstance(dset.get_studies_by_mask(mask_img), list)
|
45
|
+
|
46
|
+
dset1 = dset.slice(dset.ids[:5])
|
47
|
+
dset2 = dset.slice(dset.ids[5:])
|
48
|
+
assert isinstance(dset1, dataset.Dataset)
|
49
|
+
dset_merged = dset1.merge(dset2)
|
50
|
+
assert isinstance(dset_merged, dataset.Dataset)
|
51
|
+
|
52
|
+
|
53
|
+
def test_empty_dset():
|
54
|
+
"""Smoke test for initialization with an empty Dataset."""
|
55
|
+
# dictionary with no information
|
56
|
+
minimal_dict = {"study-0": {"contrasts": {"1": {}}}}
|
57
|
+
dataset.Dataset(minimal_dict)
|
58
|
+
|
59
|
+
|
60
|
+
def test_posneg_warning():
|
61
|
+
"""Smoke test for nimare.dataset.Dataset initialization with positive and negative z_stat."""
|
62
|
+
db_file = op.join(get_test_data_path(), "neurosynth_dset.json")
|
63
|
+
with open(db_file, "r") as f_obj:
|
64
|
+
data = json.load(f_obj)
|
65
|
+
|
66
|
+
data_pos_zstats = copy.deepcopy(data)
|
67
|
+
data_neg_zstats = copy.deepcopy(data)
|
68
|
+
for pid in data.keys():
|
69
|
+
for expid in data[pid]["contrasts"].keys():
|
70
|
+
exp = data[pid]["contrasts"][expid]
|
71
|
+
|
72
|
+
if "coords" not in exp.keys():
|
73
|
+
continue
|
74
|
+
|
75
|
+
if "z_stat" not in exp["coords"].keys():
|
76
|
+
continue
|
77
|
+
|
78
|
+
n_zstats = len(exp["coords"]["z_stat"])
|
79
|
+
rand_arr = np.random.randn(n_zstats)
|
80
|
+
rand_pos_arr = np.abs(rand_arr)
|
81
|
+
rand_neg_arr = np.abs(rand_arr) * -1
|
82
|
+
|
83
|
+
data[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_arr.tolist()
|
84
|
+
data_neg_zstats[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_neg_arr.tolist()
|
85
|
+
data_pos_zstats[pid]["contrasts"][expid]["coords"]["z_stat"] = rand_pos_arr.tolist()
|
86
|
+
|
87
|
+
# Test Warning is raised if there are positive and negative z-stat
|
88
|
+
with pytest.warns(UserWarning, match=r"positive and negative z_stats"):
|
89
|
+
dset_posneg = dataset.Dataset(data)
|
90
|
+
|
91
|
+
# Test Warning is not raised if there are only positive or negative z-stat
|
92
|
+
with warnings.catch_warnings():
|
93
|
+
warnings.simplefilter("error")
|
94
|
+
dset_pos = dataset.Dataset(data_pos_zstats)
|
95
|
+
dset_neg = dataset.Dataset(data_neg_zstats)
|
96
|
+
|
97
|
+
assert isinstance(dset_posneg, nimare.dataset.Dataset)
|
98
|
+
assert isinstance(dset_pos, nimare.dataset.Dataset)
|
99
|
+
assert isinstance(dset_neg, nimare.dataset.Dataset)
|
@@ -0,0 +1,132 @@
|
|
1
|
+
"""Test nimare.decode.continuous.
|
2
|
+
|
3
|
+
Tests for nimare.decode.continuous.gclda_decode_map are in test_annotate_gclda.
|
4
|
+
"""
|
5
|
+
|
6
|
+
import os
|
7
|
+
|
8
|
+
import numpy as np
|
9
|
+
import pandas as pd
|
10
|
+
import pytest
|
11
|
+
|
12
|
+
from nimare.decode import continuous
|
13
|
+
from nimare.meta import kernel, mkda
|
14
|
+
|
15
|
+
|
16
|
+
def test_CorrelationDecoder_smoke(testdata_laird, tmp_path_factory):
|
17
|
+
"""Smoke test for continuous.CorrelationDecoder."""
|
18
|
+
testdata_laird = testdata_laird.copy()
|
19
|
+
features = testdata_laird.get_labels(ids=testdata_laird.ids[0])[:5]
|
20
|
+
|
21
|
+
# Make an image to decode
|
22
|
+
meta = mkda.KDA(null_method="approximate")
|
23
|
+
res = meta.fit(testdata_laird)
|
24
|
+
img = res.get_map("stat")
|
25
|
+
|
26
|
+
# Test: train decoder with Dataset object
|
27
|
+
decoder = continuous.CorrelationDecoder(features=features)
|
28
|
+
decoder.fit(testdata_laird)
|
29
|
+
decoded_df = decoder.transform(img)
|
30
|
+
|
31
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
32
|
+
|
33
|
+
# Get features and images to compare with other methods
|
34
|
+
features = list(decoder.results_.maps.keys())
|
35
|
+
images = np.array(list(decoder.results_.maps.values()))
|
36
|
+
|
37
|
+
# Save images to disk to test load_imgs method
|
38
|
+
tmpdir = tmp_path_factory.mktemp("test_CorrelationDecoder")
|
39
|
+
decoder.results_.save_maps(tmpdir)
|
40
|
+
|
41
|
+
# Test: load pregenerated maps using a dictionary of feature names and paths
|
42
|
+
img_dict = {feature: os.path.join(tmpdir, f"{feature}.nii.gz") for feature in features}
|
43
|
+
decoder2 = continuous.CorrelationDecoder()
|
44
|
+
decoder2.load_imgs(img_dict, mask=testdata_laird.masker)
|
45
|
+
decoded2_df = decoder2.transform(img)
|
46
|
+
|
47
|
+
features2 = list(decoder2.results_.maps.keys())
|
48
|
+
images2 = np.array(list(decoder2.results_.maps.values()))
|
49
|
+
|
50
|
+
assert isinstance(decoded2_df, pd.DataFrame)
|
51
|
+
assert np.array_equal(features, features2)
|
52
|
+
assert np.array_equal(images, images2)
|
53
|
+
assert decoded_df.equals(decoded2_df)
|
54
|
+
|
55
|
+
# Test: load pregenerated maps from a directory
|
56
|
+
decoder3 = continuous.CorrelationDecoder()
|
57
|
+
decoder3.load_imgs(tmpdir.as_posix(), mask=testdata_laird.masker)
|
58
|
+
decoded3_df = decoder3.transform(img)
|
59
|
+
|
60
|
+
features3 = list(decoder3.results_.maps.keys())
|
61
|
+
images3 = np.array(list(decoder3.results_.maps.values()))
|
62
|
+
|
63
|
+
assert isinstance(decoded3_df, pd.DataFrame)
|
64
|
+
assert np.array_equal(features, features3)
|
65
|
+
assert np.array_equal(images, images3)
|
66
|
+
assert decoded_df.equals(decoded3_df)
|
67
|
+
|
68
|
+
# Test: passing a dataset to load_imgs
|
69
|
+
decoder4 = continuous.CorrelationDecoder()
|
70
|
+
with pytest.raises(ValueError):
|
71
|
+
decoder4.load_imgs(testdata_laird, mask=testdata_laird.masker)
|
72
|
+
|
73
|
+
# Test: try loading pregenerated maps without a masker
|
74
|
+
decoder5 = continuous.CorrelationDecoder()
|
75
|
+
with pytest.raises(ValueError):
|
76
|
+
decoder5.load_imgs(img_dict)
|
77
|
+
|
78
|
+
# Test: try transforming an image without fitting the decoder
|
79
|
+
decoder6 = continuous.CorrelationDecoder()
|
80
|
+
with pytest.raises(AttributeError):
|
81
|
+
decoder6.transform(img)
|
82
|
+
|
83
|
+
|
84
|
+
def test_CorrelationDistributionDecoder_smoke(testdata_laird, tmp_path_factory):
|
85
|
+
"""Smoke test for continuous.CorrelationDistributionDecoder."""
|
86
|
+
tmpdir = tmp_path_factory.mktemp("test_CorrelationDistributionDecoder")
|
87
|
+
|
88
|
+
testdata_laird = testdata_laird.copy()
|
89
|
+
dset = testdata_laird.copy()
|
90
|
+
features = testdata_laird.get_labels(ids=testdata_laird.ids[0])[:5]
|
91
|
+
|
92
|
+
decoder = continuous.CorrelationDistributionDecoder(features=features)
|
93
|
+
|
94
|
+
# No images of the requested type
|
95
|
+
with pytest.raises(ValueError):
|
96
|
+
decoder.fit(testdata_laird)
|
97
|
+
|
98
|
+
# Let's add the path
|
99
|
+
testdata_laird.update_path(tmpdir)
|
100
|
+
|
101
|
+
# Then let's make some images to decode
|
102
|
+
kern = kernel.MKDAKernel(r=10, value=1)
|
103
|
+
kern._infer_names() # Determine MA map filenames
|
104
|
+
|
105
|
+
imgs = kern.transform(testdata_laird, return_type="image")
|
106
|
+
for i_img, img in enumerate(imgs):
|
107
|
+
id_ = testdata_laird.ids[i_img]
|
108
|
+
out_file = os.path.join(testdata_laird.basepath, kern.filename_pattern.format(id=id_))
|
109
|
+
|
110
|
+
# Add file names to dset.images DataFrame
|
111
|
+
img.to_filename(out_file)
|
112
|
+
dset.images.loc[testdata_laird.images["id"] == id_, kern.image_type] = out_file
|
113
|
+
|
114
|
+
# And now we have images we can use for decoding!
|
115
|
+
decoder = continuous.CorrelationDistributionDecoder(
|
116
|
+
features=features,
|
117
|
+
target_image=kern.image_type,
|
118
|
+
)
|
119
|
+
decoder.fit(dset)
|
120
|
+
|
121
|
+
# Make an image to decode
|
122
|
+
meta = mkda.KDA(null_method="approximate")
|
123
|
+
res = meta.fit(testdata_laird)
|
124
|
+
img = res.get_map("stat")
|
125
|
+
decoded_df = decoder.transform(img)
|
126
|
+
|
127
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
128
|
+
|
129
|
+
# Test: try transforming an image without fitting the decoder
|
130
|
+
decoder2 = decoder = continuous.CorrelationDistributionDecoder()
|
131
|
+
with pytest.raises(AttributeError):
|
132
|
+
decoder2.transform(img)
|
@@ -0,0 +1,92 @@
|
|
1
|
+
"""Test nimare.decode.discrete.
|
2
|
+
|
3
|
+
Tests for nimare.decode.discrete.gclda_decode_roi are in test_annotate_gclda.
|
4
|
+
"""
|
5
|
+
|
6
|
+
import pandas as pd
|
7
|
+
import pytest
|
8
|
+
|
9
|
+
from nimare.decode import discrete
|
10
|
+
|
11
|
+
|
12
|
+
def test_neurosynth_decode(testdata_laird):
|
13
|
+
"""Smoke test for discrete.neurosynth_decode."""
|
14
|
+
ids = testdata_laird.ids[:5]
|
15
|
+
features = testdata_laird.annotations.columns.tolist()[5:10]
|
16
|
+
decoded_df = discrete.neurosynth_decode(
|
17
|
+
testdata_laird.coordinates,
|
18
|
+
testdata_laird.annotations,
|
19
|
+
ids=ids,
|
20
|
+
features=features,
|
21
|
+
correction=None,
|
22
|
+
)
|
23
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
24
|
+
|
25
|
+
|
26
|
+
def test_brainmap_decode(testdata_laird):
|
27
|
+
"""Smoke test for discrete.brainmap_decode."""
|
28
|
+
ids = testdata_laird.ids[:5]
|
29
|
+
features = testdata_laird.annotations.columns.tolist()[5:10]
|
30
|
+
decoded_df = discrete.brainmap_decode(
|
31
|
+
testdata_laird.coordinates,
|
32
|
+
testdata_laird.annotations,
|
33
|
+
ids=ids,
|
34
|
+
features=features,
|
35
|
+
correction=None,
|
36
|
+
)
|
37
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
38
|
+
|
39
|
+
|
40
|
+
def test_NeurosynthDecoder(testdata_laird):
|
41
|
+
"""Smoke test for discrete.NeurosynthDecoder."""
|
42
|
+
ids = testdata_laird.ids[:5]
|
43
|
+
labels = testdata_laird.get_labels(ids=testdata_laird.ids)
|
44
|
+
decoder = discrete.NeurosynthDecoder(features=labels)
|
45
|
+
decoder.fit(testdata_laird)
|
46
|
+
decoded_df = decoder.transform(ids=ids)
|
47
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
48
|
+
assert decoded_df.shape == (len(labels), 6)
|
49
|
+
|
50
|
+
|
51
|
+
def test_NeurosynthDecoder_featuregroup(testdata_laird):
|
52
|
+
"""Smoke test for discrete.NeurosynthDecoder with feature group selection."""
|
53
|
+
ids = testdata_laird.ids[:5]
|
54
|
+
decoder = discrete.NeurosynthDecoder(feature_group="Neurosynth_TFIDF")
|
55
|
+
decoder.fit(testdata_laird)
|
56
|
+
decoded_df = decoder.transform(ids=ids)
|
57
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
58
|
+
|
59
|
+
|
60
|
+
def test_NeurosynthDecoder_featuregroup_failure(testdata_laird):
|
61
|
+
"""Smoke test for NeurosynthDecoder with feature group selection and no detected features."""
|
62
|
+
decoder = discrete.NeurosynthDecoder(feature_group="Neurosynth_TFIDF", features=["01", "05"])
|
63
|
+
with pytest.raises(Exception):
|
64
|
+
decoder.fit(testdata_laird)
|
65
|
+
|
66
|
+
|
67
|
+
def test_BrainMapDecoder(testdata_laird):
|
68
|
+
"""Smoke test for discrete.BrainMapDecoder."""
|
69
|
+
ids = testdata_laird.ids[:5]
|
70
|
+
labels = testdata_laird.get_labels(ids=testdata_laird.ids)
|
71
|
+
decoder = discrete.BrainMapDecoder(features=labels)
|
72
|
+
decoder.fit(testdata_laird)
|
73
|
+
decoded_df = decoder.transform(ids=ids)
|
74
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
75
|
+
assert decoded_df.shape == (len(labels), 6)
|
76
|
+
|
77
|
+
|
78
|
+
def test_BrainMapDecoder_failure(testdata_laird):
|
79
|
+
"""Smoke test for discrete.BrainMapDecoder where there are no features left."""
|
80
|
+
decoder = discrete.BrainMapDecoder(features=["doggy"])
|
81
|
+
with pytest.raises(Exception):
|
82
|
+
decoder.fit(testdata_laird)
|
83
|
+
|
84
|
+
|
85
|
+
def test_ROIAssociationDecoder(testdata_laird, roi_img):
|
86
|
+
"""Smoke test for discrete.ROIAssociationDecoder."""
|
87
|
+
labels = testdata_laird.get_labels(ids=testdata_laird.ids)
|
88
|
+
decoder = discrete.ROIAssociationDecoder(masker=roi_img, features=labels)
|
89
|
+
decoder.fit(testdata_laird)
|
90
|
+
decoded_df = decoder.transform()
|
91
|
+
assert isinstance(decoded_df, pd.DataFrame)
|
92
|
+
assert decoded_df.shape == (len(labels), 1)
|