-
Notifications
You must be signed in to change notification settings - Fork 153
/
test_base.py
314 lines (285 loc) · 14.1 KB
/
test_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import unittest
import numpy as np
import pandas as pd
import tempfile
import os
import shutil
from glob import glob
from neurosynth.tests.utils import (get_test_dataset, get_test_data_path,
get_resource_path)
from neurosynth.base.dataset import Dataset
from neurosynth.base import imageutils
from neurosynth.base.mask import Masker
import neurosynth as ns
class TestBase(unittest.TestCase):
def setUp(self):
""" Create a new Dataset and add features. """
self.dataset = get_test_dataset()
self.real_dataset = get_test_dataset(prefix='test_real')
def test_dataset_download(self):
tmpdir = tempfile.mkdtemp()
url = 'https://raw.githubusercontent.com/neurosynth/neurosynth/master/neurosynth/tests/data/test_data.tar.gz'
ns.base.dataset.download(tmpdir, url=url, unpack=True)
files = glob(os.path.join(tmpdir, '*.txt'))
self.assertEqual(len(files), 2)
shutil.rmtree(tmpdir)
def test_dataset_save_and_load(self):
# smoke test of saving and loading
t = tempfile.mktemp()
self.dataset.save(t)
self.assertTrue(os.path.exists(t))
dataset = Dataset.load(t)
self.assertIsNotNone(dataset)
self.assertEqual(len(dataset.image_table.ids), 5)
os.unlink(t)
def test_dataset_initializes(self):
""" Test whether dataset initializes properly. """
Dataset(get_test_data_path() + 'test_dataset.txt',
get_test_data_path() + 'test_features.txt')
self.assertIsNotNone(self.dataset.masker)
self.assertIsNotNone(self.dataset.image_table)
self.assertEqual(len(self.dataset.image_table.ids), 5)
self.assertIsNotNone(self.dataset.masker)
self.assertIsNotNone(self.dataset.r)
self.assertIsNotNone(
self.dataset.activations['extra_field'].iloc[2], 'field')
def test_image_table_loads(self):
""" Test ImageTable initialization. """
self.assertIsNotNone(self.dataset.image_table)
it = self.dataset.image_table
self.assertEqual(len(it.ids), 5)
self.assertIsNotNone(it.masker)
self.assertIsNotNone(it.r)
self.assertEqual(it.data.shape, (228453, 5))
# Add tests for values in table
def test_feature_table_loads(self):
""" Test FeatureTable initialization. """
tt = self.dataset.feature_table
self.assertIsNotNone(tt)
self.assertEqual(len(self.dataset.get_feature_names()), 5)
self.assertEqual(tt.data.shape, (5, 5))
self.assertEqual(tt.data.columns[3], 'f4')
self.assertEqual(tt.data.astype(np.float64).iloc[0, 0], 0.0003)
def test_feature_addition(self):
""" Add feature data from multiple sources to FeatureTable. """
d = self.dataset
new_data = pd.DataFrame(np.array([
[0.001, 0.1, 0.003],
[0.02, 0.0008, 0.0003],
[0.001, 0.002, 0.05]]),
index=['study1', 'study4', 'study6'], columns=['g1', 'g2', 'g3'])
# Replace all features
d.add_features(new_data, append=False)
self.assertEqual(d.feature_table.data.shape, (3, 3))
self.assertAlmostEqual(d.feature_table.data['g2']['study4'], 0.0008)
# Outer merge and ignore duplicate features
d = get_test_dataset()
d.add_features(new_data)
self.assertEqual(d.feature_table.data.shape, (6, 7))
self.assertEqual(d.feature_table.data['g2']['study2'], 0.0)
self.assertEqual(d.feature_table.data['g1']['study1'], 0.02)
# Outer merge but overwrite old features with new ones
d = get_test_dataset()
d.add_features(new_data, duplicates='replace')
self.assertEqual(d.feature_table.data.shape, (6, 7))
self.assertEqual(d.feature_table.data['g1']['study1'], 0.001)
# Left join (i.e., keep only old rows) and rename conflicting features
d = get_test_dataset()
d.add_features(new_data, merge='left', duplicates='merge')
self.assertEqual(d.feature_table.data.shape, (5, 8))
self.assertEqual(d.feature_table.data['g1_y']['study1'], 0.001)
# Apply threshold
d = get_test_dataset()
d.add_features(new_data, min_studies=2, threshold=0.003)
self.assertEqual(d.feature_table.data.shape, (6, 6))
def test_selection_by_features(self):
""" Test feature-based Mappable search. Tests both the FeatureTable method
and the Dataset wrapper. """
tt = self.dataset.feature_table
features = tt.search_features(['f*'])
self.assertEqual(len(features), 4)
d = self.dataset
ids = d.get_studies(features='f4', frequency_threshold=0.001)
self.assertEqual(list(ids), ['study5'])
ids = d.get_studies(features=['f*'], frequency_threshold=0.001)
self.assertEqual(len(ids), 4)
img_data = d.get_studies(
features=['f1', 'f3', 'g1'], activation_threshold=0.001,
func=np.max, return_type='data')
self.assertEqual(img_data.shape, (228453, 5))
d.feature_table.data.columns = [
'f1', 'f2', 'my ngram', 'my ngram reprise', 'g1']
ids = d.get_studies('my ngram reprise', frequency_threshold=0.001)
self.assertEqual(list(ids), ['study5'])
def test_selection_by_expression(self):
""" Tests the expression-based search using the lexer/parser. This
functionality is optional, so only run the test if ply is available.
"""
try:
import ply.lex as lex
have_ply = True
except:
have_ply = False
if have_ply:
ids = self.dataset.get_studies(expression="* &~ (g*)", func=np.sum,
frequency_threshold=0.003)
self.assertEqual(sorted(ids), ['study3', 'study5'])
ids = self.dataset.get_studies(
expression="f* > 0.005", func=np.mean, frequency_threshold=0.0)
self.assertEqual(ids, ['study3'])
ids = self.dataset.get_studies(expression="f* < 0.05", func=np.sum,
frequency_threshold=0.0)
self.assertEqual(sorted(ids), ['study1', 'study2', 'study3',
'study4', 'study5'])
ids = self.dataset.get_studies(expression="f* | g*", func=np.mean,
frequency_threshold=0.003)
self.assertEqual(sorted(ids), ['study1', 'study2', 'study3',
'study4'])
ids = self.dataset.get_studies(expression="(f* & g*)", func=np.sum,
frequency_threshold=0.001)
self.assertEqual(sorted(ids), ['study1', 'study4'])
# test N-gram feature handling
self.dataset.feature_table.data.columns = ['f1', 'f2', 'my ngram',
'my ngram reprise',
'g1']
ids = self.dataset.get_studies(expression="my ngram reprise")
self.assertEqual(ids, ['study5'])
ids = self.dataset.get_studies(expression="my ngram*",
frequency_threshold=0.01)
self.assertEqual(sorted(ids), ['study1', 'study4', 'study5'])
try:
os.unlink('lextab.py')
os.unlink('parser.out')
os.unlink('parsetab.py')
except:
pass
def test_selection_by_mask(self):
""" Test mask-based Mappable selection.
Only one peak in the test dataset (in study5) should be within the
sgACC. """
ids = self.dataset.get_studies(mask=get_test_data_path() +
'sgacc_mask.nii.gz')
self.assertEquals(len(ids), 1)
self.assertEquals('study5', ids[0])
def test_selection_by_peaks(self):
""" Test peak-based Mappable selection. """
ids = self.real_dataset.get_studies(peaks=[[0, 20, 40]])
self.assertEquals(len(ids), 1)
self.assertTrue(9106283 in ids)
peaks = np.array([[0, 20, 40], [-32, 22, 12]])
ids = self.real_dataset.get_studies(peaks=peaks, r=8)
self.assertEquals(len(ids), 11)
def test_selection_by_multiple_criteria(self):
ids = self.dataset.get_studies(
peaks=[[3, 30, -9]], r=40, expression="f* < 0.013", func=np.sum,
frequency_threshold=0.0)
self.assertEquals(sorted(ids), ['study2', 'study5'])
def test_unmask(self):
""" Test unmasking on 1d and 2d vectors (going back to 3d and 4d)
TODO: test directly on Masker class and its functions, and on
some smaller example data. But then it should get into a separate
TestCase to not 'reload' the same Dataset.
So for now let's just reuse loaded Dataset and provide
rudimentary testing
"""
dataset = self.dataset
ids = dataset.get_studies(
mask=get_test_data_path() + 'sgacc_mask.nii.gz')
nvoxels = dataset.masker.n_vox_in_vol
nvols = 2
data2d = np.arange(nvoxels * nvols).reshape((nvoxels, -1))
data2d_unmasked = dataset.masker.unmask(data2d, output='array')
self.assertEqual(data2d_unmasked.shape, (91, 109, 91, 2))
data2d_unmasked = dataset.masker.unmask(data2d, output='image')
self.assertEqual(data2d_unmasked.shape, (91, 109, 91, 2))
self.assertTrue(hasattr(data2d_unmasked, 'get_data'))
def test_get_feature_counts(self):
# If we set threshold too high -- nothing should get through and
# all should be 0s
feature_counts = self.dataset.get_feature_counts(threshold=1.)
self.assertEqual(feature_counts,
dict((f, 0) for f in self.dataset.get_feature_names()))
feature_counts = self.dataset.get_feature_counts()
# all should have some non-0 loading with default threshold,
# otherwise what is the point of having them?
for f, c in feature_counts.items():
self.assertGreater(c, 0, "feature %s has no hits" % f)
# and should be equal to the ones computed directly (we do not do
# any fancy queries atm), assumes default threshold of 0.001
ft = self.dataset.feature_table
feature_counts_ = dict(
(feature, np.sum(ft.data.iloc[:, col].sparse.to_dense() > 0.001))
for col, feature in enumerate(ft.feature_names)
)
self.assertEqual(feature_counts, feature_counts_)
def test_get_feature_data(self):
""" Test retrieval of Mappable x feature data. """
# Retrieve dense
feature_data = self.dataset.get_feature_data(
features=['f1', 'f4', 'g1'])
self.assertEqual(feature_data.shape, (5, 3))
# Retrieve sparse
feature_data = self.dataset.get_feature_data(
ids=['study3', 'study1'], dense=False)
self.assertEqual(feature_data.shape, (2, 5))
# Skip this for now; behaves inconsistently across pandas versions
# self.assertEqual(feature_data.iloc[0,1], 0.02)
self.assertEqual(feature_data['f3']['study1'], 0.012)
def test_get_image_data(self):
""" Test retrieval of voxel x Mappable data. """
image_data = self.dataset.get_image_data(voxels=range(2000, 3000))
self.assertEquals(image_data.shape, (1000, 5))
image_data = self.dataset.get_image_data(
ids=['study1', 'study2', 'study5'], dense=True)
self.assertEquals(image_data.shape, (228453, 3))
# Or directly through the image table
image_data = self.dataset.image_table.get_image_data(
ids=['study1', 'study2', 'study5'], dense=False)
self.assertEquals(image_data.shape, (228453, 3))
def test_trim_image_table(self):
self.dataset.image_table.trim(['study1', 'study2', 'study3'])
self.assertEqual(self.dataset.get_image_data().shape, (228453, 3))
def test_get_features_by_ids(self):
features = self.dataset.feature_table.get_features_by_ids(
['study1', 'study3'], threshold=0.01)
self.assertEquals(len(features), 3)
features = self.dataset.feature_table.get_features_by_ids(
['study2', 'study5'], func=np.sum, threshold=0.0, get_weights=True)
self.assertEquals(len(features), 5)
self.assertEqual(features['f3'], 0.01)
def test_get_feature_names(self):
features = self.dataset.get_feature_names()
self.assertEquals(len(features), 5)
self.assertEquals(features[2], 'f3')
features = self.dataset.get_feature_names(['g1', 'f4', 'f2'])
self.assertEquals(features, ['f2', 'f4', 'g1'])
def test_grid_creation(self):
mask = self.dataset.masker.volume
# Test with mask
grid = imageutils.create_grid(image=mask, scale=4)
self.assertEquals(grid.shape, (91, 109, 91))
self.assertEquals(len(np.unique(grid.get_data())), 4359)
# Test without mask
grid = imageutils.create_grid(image=mask, scale=4, apply_mask=False)
self.assertGreater(len(np.unique(grid.get_data())), 4359)
class TestMasker(unittest.TestCase):
def setUp(self):
""" Create a new Dataset and add features. """
maskfile = get_resource_path() + 'MNI152_T1_2mm_brain.nii.gz'
self.masker = Masker(maskfile)
def test_add_and_remove_masks(self):
self.masker.add(get_test_data_path() + 'sgacc_mask.nii.gz')
self.masker.add(
{'motor': get_test_data_path() + 'medial_motor.nii.gz'})
self.assertEqual(len(self.masker.layers), 2)
self.assertEqual(len(self.masker.stack), 2)
self.assertEqual(
set(self.masker.layers.keys()), set(['layer_0', 'motor']))
self.assertEqual(np.sum(self.masker.layers['motor']), 1419)
self.masker.remove('motor')
self.assertEqual(len(self.masker.layers), 1)
self.assertEqual(len(self.masker.stack), 1)
self.masker.add(get_test_data_path() + 'medial_motor.nii.gz')
self.masker.remove(-1)
self.assertTrue('layer_0' in self.masker.layers.keys())
self.assertEqual(len(self.masker.layers), 1)