Skip to content

Commit c18a780

Browse files
author
Shoshana Berleant
committed
mocked-up rsfmri workflow runs; use tempfiles
1 parent 402b6b1 commit c18a780

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

nipype/workflows/rsfmri/fsl/tests/test_resting.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .....testing import (assert_equal, assert_true, assert_almost_equal,
55
skipif, utils)
66
from .....interfaces import fsl, IdentityInterface
7-
from .....pipeline.engine import Node
7+
from .....pipeline.engine import Node, Workflow
88

99
from ..resting import create_resting_preproc
1010

@@ -14,41 +14,35 @@
1414
import nibabel as nb
1515
import numpy as np
1616
import os
17-
18-
19-
20-
21-
22-
def mock_node_factory(*args, **kwargs):
23-
''' return mocks for all the nodes except compcor and compcor's neighbors'''
24-
mock = MagicMock()
25-
if 'interface' in kwargs.keys():
26-
mock = mock.create_autospec(kwargs['interface'], instance=True)
27-
if 'name' in kwargs.keys():
28-
name = kwargs['name']
29-
if name == 'compcor':
30-
return Node(*args, **kwargs)
31-
if name in ['threshold', 'inputspec']:
32-
# nodes that provide inputs for compcor
33-
# just give all of them all of the fields
34-
return Node(IdentityInterface(fields=['out_file', 'lowpass_sigma',
35-
'num_noise_components',
36-
'func', 'highpass_sigma']),
37-
name=name)
38-
if name in ('remove_noise'):
39-
# node that takes output from compcor
40-
return Node(IdentityInterface(fields=['design_file', 'out_file']),
41-
name=name)
42-
mock.name = kwargs['name']
43-
mock.iterables = None
44-
return mock
17+
import tempfile
18+
import shutil
19+
20+
all_fields = ['func', 'in_file', 'slice_time_corrected_file', 'stddev_file',
21+
'out_stat', 'thresh', 'num_noise_components', 'detrended_file',
22+
'design_file', 'highpass_sigma', 'lowpass_sigma', 'out_file',
23+
'noise_mask_file', 'filtered_file']
24+
25+
def stub_node_factory(*args, **kwargs):
26+
if 'name' not in kwargs.keys():
27+
raise Exception()
28+
name = kwargs['name']
29+
if name == 'compcor':
30+
return Node(*args, **kwargs)
31+
else: # replace with an IdentityInterface
32+
return Node(IdentityInterface(fields=all_fields),
33+
name=name)
34+
35+
def stub_wf(*args, **kwargs):
36+
wf = Workflow(name='realigner')
37+
inputnode = Node(IdentityInterface(fields=['func']), name='inputspec')
38+
outputnode = Node(interface=IdentityInterface(fields=['realigned_file']),
39+
name='outputspec')
40+
wf.connect(inputnode, 'func', outputnode, 'realigned_file')
41+
return wf
4542

4643
class TestResting(unittest.TestCase):
4744

48-
in_filenames = {
49-
'in_file': 'rsfmrifunc.nii',
50-
'noise_mask_file': 'rsfmrimask.nii'
51-
}
45+
in_filenames = {}
5246

5347
out_filenames = {
5448
'noise_mask_file': '',
@@ -57,27 +51,34 @@ class TestResting(unittest.TestCase):
5751

5852
def setUp(self):
5953
# setup
60-
utils.save_toy_nii(self.fake_data, self.in_filenames['in_file'])
54+
self.orig_dir = os.getcwd()
55+
self.temp_dir = tempfile.mkdtemp()
56+
os.chdir(self.temp_dir)
57+
self.in_filenames['realigned_file'] = utils.save_toy_nii(self.fake_data, os.path.abspath('rsfmrifunc.nii'))
58+
mask = np.zeros(self.fake_data.shape[:3])
59+
for i in range(mask.shape[0]):
60+
for j in range(mask.shape[1]):
61+
if i==j:
62+
mask[i,j] = 1
63+
self.in_filenames['noise_mask_file'] = utils.save_toy_nii(mask, os.path.abspath('rsfmrimask.nii'))
6164

62-
@skipif(True)
63-
@mock.patch('nipype.pipeline.engine.Workflow._write_report_info')
6465
@mock.patch('nipype.workflows.rsfmri.fsl.resting.create_realign_flow',
65-
return_value=Node(name='realigner', interface=IdentityInterface(
66-
fields=['outputspec.realigned_file'])))
67-
@mock.patch('nipype.pipeline.engine.Node', side_effect=mock_node_factory)
68-
def test_create_resting_preproc(self, mock_Node, mock_realign_wf, nothing):
69-
# setup
70-
# run
66+
side_effect=stub_wf)
67+
@mock.patch('nipype.pipeline.engine.Node', side_effect=stub_node_factory)
68+
def test_create_resting_preproc(self, mock_Node, mock_realign_wf):
7169
wf = create_resting_preproc()
70+
7271
wf.inputs.inputspec.num_noise_components = 6
72+
wf.get_node('slicetimer').inputs.slice_time_corrected_file = self.in_filenames['realigned_file']
7373
wf.get_node('threshold').inputs.out_file = self.in_filenames['noise_mask_file']
74+
7475
wf.run()
7576

7677
# assert
7778

7879
def tearDown(self):
79-
utils.remove_nii(self.in_filenames.values())
80-
utils.remove_nii(self.out_filenames.values())
80+
os.chdir(self.orig_dir)
81+
shutil.rmtree(self.temp_dir)
8182

8283
fake_data = np.array([[[[2, 4, 3, 9, 1],
8384
[3, 6, 4, 7, 4]],

0 commit comments

Comments
 (0)