4
4
from .....testing import (assert_equal , assert_true , assert_almost_equal ,
5
5
skipif , utils )
6
6
from .....interfaces import fsl , IdentityInterface
7
- from .....pipeline .engine import Node
7
+ from .....pipeline .engine import Node , Workflow
8
8
9
9
from ..resting import create_resting_preproc
10
10
14
14
import nibabel as nb
15
15
import numpy as np
16
16
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
45
42
46
43
class TestResting (unittest .TestCase ):
47
44
48
- in_filenames = {
49
- 'in_file' : 'rsfmrifunc.nii' ,
50
- 'noise_mask_file' : 'rsfmrimask.nii'
51
- }
45
+ in_filenames = {}
52
46
53
47
out_filenames = {
54
48
'noise_mask_file' : '' ,
@@ -57,27 +51,34 @@ class TestResting(unittest.TestCase):
57
51
58
52
def setUp (self ):
59
53
# 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' ))
61
64
62
- @skipif (True )
63
- @mock .patch ('nipype.pipeline.engine.Workflow._write_report_info' )
64
65
@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 ):
71
69
wf = create_resting_preproc ()
70
+
72
71
wf .inputs .inputspec .num_noise_components = 6
72
+ wf .get_node ('slicetimer' ).inputs .slice_time_corrected_file = self .in_filenames ['realigned_file' ]
73
73
wf .get_node ('threshold' ).inputs .out_file = self .in_filenames ['noise_mask_file' ]
74
+
74
75
wf .run ()
75
76
76
77
# assert
77
78
78
79
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 )
81
82
82
83
fake_data = np .array ([[[[2 , 4 , 3 , 9 , 1 ],
83
84
[3 , 6 , 4 , 7 , 4 ]],
0 commit comments