Skip to content

Commit b11c5f5

Browse files
author
Shoshana Berleant
committed
refactoring & testing in preparation for 4d label files; pep8
1 parent 4f79c18 commit b11c5f5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

nipype/algorithms/stats.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
'''
1414
from __future__ import (print_function, division, unicode_literals,
1515
absolute_import)
16-
from builtins import str
1716

1817
import numpy as np
1918

@@ -71,11 +70,7 @@ def _run_interface(self, runtime):
7170
ins = self.inputs
7271

7372
if ins.stat == 'mean': # always true for now
74-
nlmasker = nl.NiftiLabelsMasker(ins.label_file,
75-
detrend=ins.detrend)
76-
nlmasker.fit()
77-
region_signals = nlmasker.transform_single_imgs(ins.in_file)
78-
73+
region_signals = self._3d_label_handler(nl, ins)
7974
num_labels_found = region_signals.shape[1]
8075
if len(ins.class_labels) != num_labels_found:
8176
raise ValueError('The length of class_labels {} does not '
@@ -90,6 +85,13 @@ def _run_interface(self, runtime):
9085
np.savetxt(ins.out_file, output, fmt=b'%s', delimiter='\t')
9186
return runtime
9287

88+
def _3d_label_handler(self, nl, ins):
89+
nlmasker = nl.NiftiLabelsMasker(ins.label_file,
90+
detrend=ins.detrend)
91+
nlmasker.fit()
92+
region_signals = nlmasker.transform_single_imgs(ins.in_file)
93+
return region_signals
94+
9395
def _list_outputs(self):
9496
outputs = self._outputs().get()
9597
outputs['out_file'] = self.inputs.out_file

nipype/algorithms/tests/test_stats.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
no_nilearn = True
1515
try:
16-
import nilearn
16+
__import__('nilearn')
1717
no_nilearn = False
1818
except ImportError:
1919
pass
@@ -23,6 +23,7 @@ class TestSignalExtraction(unittest.TestCase):
2323
filenames = {
2424
'in_file': 'fmri.nii',
2525
'label_file': 'labels.nii',
26+
'4d_label_file': '4dlabels.nii',
2627
'out_file': 'signals.tsv'
2728
}
2829

@@ -70,6 +71,11 @@ def test_signal_extraction_bad_class_labels(self):
7071
label_file=self.filenames['label_file'],
7172
class_labels=['bad']).run()
7273

74+
@skipif(no_nilearn)
75+
def test_signal_extraction_4d_label(self):
76+
# setup
77+
utils.save_toy_nii(self.fake_4d_label_data, self.filenames['4d_label_file'])
78+
7379
def tearDown(self):
7480
os.chdir(self.orig_dir)
7581
shutil.rmtree(self.temp_dir)
@@ -92,3 +98,16 @@ def tearDown(self):
9298

9399
[[2, 0],
94100
[1, 3]]])
101+
102+
fake_4d_label_data = np.array([[[[0.2, 0.3, 0.5],
103+
[0.1, 0.1, 0.8]],
104+
105+
[[0.1, 0.3, 0.6],
106+
[0.3, 0.4, 0.3]]],
107+
108+
109+
[[[0.2, 0.2, 0.6],
110+
[0., 0.3, 0.7]],
111+
112+
[[0.3, 0.3, 0.4],
113+
[0.3, 0.4, 0.3]]]])

0 commit comments

Comments
 (0)