|
3 | 3 |
|
4 | 4 | from .....testing import (assert_equal, assert_true, assert_almost_equal,
|
5 | 5 | skipif, utils)
|
6 |
| -from .....interfaces import fsl, IdentityInterface |
| 6 | +from .....interfaces import fsl, IdentityInterface, utility |
7 | 7 | from .....pipeline.engine import Node, Workflow
|
8 | 8 |
|
9 | 9 | from ..resting import create_resting_preproc
|
@@ -42,39 +42,61 @@ def stub_wf(*args, **kwargs):
|
42 | 42 |
|
43 | 43 | class TestResting(unittest.TestCase):
|
44 | 44 |
|
45 |
| - in_filenames = {} |
| 45 | + in_filenames = { |
| 46 | + 'realigned_file': 'rsfmrifunc.nii', |
| 47 | + 'mask_file': 'rsfmrimask.nii' |
| 48 | + } |
46 | 49 |
|
47 | 50 | out_filenames = {
|
48 |
| - 'noise_mask_file': '', |
49 |
| - 'filtered_file': '' |
| 51 | + 'components_file': 'restpreproc/compcor/noise_components.txt' |
50 | 52 | }
|
51 | 53 |
|
| 54 | + num_noise_components = 6 |
| 55 | + |
52 | 56 | def setUp(self):
|
53 |
| - # setup |
| 57 | + # setup temp folder |
54 | 58 | self.orig_dir = os.getcwd()
|
55 | 59 | self.temp_dir = tempfile.mkdtemp()
|
56 | 60 | os.chdir(self.temp_dir)
|
57 |
| - self.in_filenames['realigned_file'] = utils.save_toy_nii(self.fake_data, os.path.abspath('rsfmrifunc.nii')) |
| 61 | + self.in_filenames = {key: os.path.abspath(value) |
| 62 | + for key, value in self.in_filenames.items()} |
| 63 | + |
| 64 | + # create&save input files |
| 65 | + utils.save_toy_nii(self.fake_data, self.in_filenames['realigned_file']) |
58 | 66 | mask = np.zeros(self.fake_data.shape[:3])
|
59 | 67 | for i in range(mask.shape[0]):
|
60 | 68 | for j in range(mask.shape[1]):
|
61 | 69 | if i==j:
|
62 | 70 | mask[i,j] = 1
|
63 |
| - self.in_filenames['noise_mask_file'] = utils.save_toy_nii(mask, os.path.abspath('rsfmrimask.nii')) |
| 71 | + utils.save_toy_nii(mask, self.in_filenames['mask_file']) |
64 | 72 |
|
65 | 73 | @mock.patch('nipype.workflows.rsfmri.fsl.resting.create_realign_flow',
|
66 | 74 | side_effect=stub_wf)
|
67 | 75 | @mock.patch('nipype.pipeline.engine.Node', side_effect=stub_node_factory)
|
68 | 76 | def test_create_resting_preproc(self, mock_Node, mock_realign_wf):
|
69 |
| - wf = create_resting_preproc() |
| 77 | + wf = create_resting_preproc(base_dir=os.getcwd()) |
70 | 78 |
|
71 |
| - wf.inputs.inputspec.num_noise_components = 6 |
72 |
| - wf.get_node('slicetimer').inputs.slice_time_corrected_file = self.in_filenames['realigned_file'] |
73 |
| - wf.get_node('threshold').inputs.out_file = self.in_filenames['noise_mask_file'] |
| 79 | + wf.inputs.inputspec.num_noise_components = self.num_noise_components |
| 80 | + mask_in = wf.get_node('threshold').inputs |
| 81 | + mask_in.out_file = self.in_filenames['mask_file'] |
| 82 | + func_in = wf.get_node('slicetimer').inputs |
| 83 | + func_in.slice_time_corrected_file = self.in_filenames['realigned_file'] |
74 | 84 |
|
75 | 85 | wf.run()
|
76 | 86 |
|
77 | 87 | # assert
|
| 88 | + expected_file = os.path.abspath(self.out_filenames['components_file']) |
| 89 | + with open(expected_file, 'r') as components_file: |
| 90 | + components_data = [line.split() for line in components_file] |
| 91 | + num_got_components = len(components_data) |
| 92 | + assert_true(num_got_components == self.num_noise_components |
| 93 | + or num_got_components == self.fake_data.shape[3]) |
| 94 | + first_two = [row[:2] for row in components_data] |
| 95 | + assert_equal(first_two, [['-0.5172356654', '-0.6973053243'], |
| 96 | + ['0.2574722644', '0.1645270737'], |
| 97 | + ['-0.0806469590', '0.5156853779'], |
| 98 | + ['0.7187176051', '-0.3235820287'], |
| 99 | + ['-0.3783072450', '0.3406749013']]) |
78 | 100 |
|
79 | 101 | def tearDown(self):
|
80 | 102 | os.chdir(self.orig_dir)
|
|
0 commit comments