88import pytest
99from ...testing import utils
1010from ..confounds import CompCor , TCompCor , ACompCor
11- from ...interfaces .base import Undefined
1211
1312
1413class TestCompCor ():
@@ -48,11 +47,13 @@ def test_compcor(self):
4847 ['-0.1246655485' , '-0.1235705610' ]]
4948
5049 self .run_cc (CompCor (realigned_file = self .realigned_file ,
51- mask_files = self .mask_files ),
50+ mask_files = self .mask_files ,
51+ mask_index = 0 ),
5252 expected_components )
5353
5454 self .run_cc (ACompCor (realigned_file = self .realigned_file ,
5555 mask_files = self .mask_files ,
56+ mask_index = 0 ,
5657 components_file = 'acc_components_file' ),
5758 expected_components , 'aCompCor' )
5859
@@ -64,40 +65,44 @@ def test_tcompcor(self):
6465 ['0.4566907310' , '0.6983205193' ],
6566 ['-0.7132557407' , '0.1340170559' ],
6667 ['0.5022537643' , '-0.5098322262' ],
67- ['-0.1342351356' , '0.1407855119' ]], 'tCompCor' )
68+ ['-0.1342351356' , '0.1407855119' ]],
69+ 'tCompCor' )
6870
6971 def test_tcompcor_no_percentile (self ):
7072 ccinterface = TCompCor (realigned_file = self .realigned_file )
7173 ccinterface .run ()
7274
73- mask = nb .load ('mask .nii' ).get_data ()
75+ mask = nb .load ('mask_000 .nii.gz ' ).get_data ()
7476 num_nonmasked_voxels = np .count_nonzero (mask )
7577 assert num_nonmasked_voxels == 1
7678
7779 def test_compcor_no_regress_poly (self ):
7880 self .run_cc (CompCor (realigned_file = self .realigned_file ,
79- mask_files = self .mask_files ,
80- use_regress_poly = False ), [['0.4451946442' , '-0.7683311482' ],
81- ['-0.4285129505' , '-0.0926034137' ],
82- ['0.5721540256' , '0.5608764842' ],
83- ['-0.5367548139' , '0.0059943226' ],
84- ['-0.0520809054' , '0.2940637551' ]])
81+ mask_files = self .mask_files ,
82+ mask_index = 0 ,
83+ use_regress_poly = False ),
84+ [['0.4451946442' , '-0.7683311482' ],
85+ ['-0.4285129505' , '-0.0926034137' ],
86+ ['0.5721540256' , '0.5608764842' ],
87+ ['-0.5367548139' , '0.0059943226' ],
88+ ['-0.0520809054' , '0.2940637551' ]])
8589
8690 def test_tcompcor_asymmetric_dim (self ):
8791 asymmetric_shape = (2 , 3 , 4 , 5 )
8892 asymmetric_data = utils .save_toy_nii (np .zeros (asymmetric_shape ),
8993 'asymmetric.nii' )
9094
9195 TCompCor (realigned_file = asymmetric_data ).run ()
92- assert nb .load ('mask .nii' ).get_data ().shape == asymmetric_shape [:3 ]
96+ assert nb .load ('mask_000 .nii.gz ' ).get_data ().shape == asymmetric_shape [:3 ]
9397
9498 def test_compcor_bad_input_shapes (self ):
9599 shape_less_than = (1 , 2 , 2 , 5 ) # dim 0 is < dim 0 of self.mask_files (2)
96100 shape_more_than = (3 , 3 , 3 , 5 ) # dim 0 is > dim 0 of self.mask_files (2)
97101
98102 for data_shape in (shape_less_than , shape_more_than ):
99103 data_file = utils .save_toy_nii (np .zeros (data_shape ), 'temp.nii' )
100- interface = CompCor (realigned_file = data_file , mask_files = self .mask_files )
104+ interface = CompCor (realigned_file = data_file ,
105+ mask_files = self .mask_files [0 ])
101106 with pytest .raises (ValueError , message = "Dimension mismatch" ): interface .run ()
102107
103108 def test_tcompcor_bad_input_dim (self ):
@@ -112,19 +117,25 @@ def test_tcompcor_merge_intersect_masks(self):
112117 mask_files = self .mask_files ,
113118 merge_method = method ).run ()
114119 if method == 'union' :
115- assert np .array_equal (nb .load ('mask .nii' ).get_data (),
120+ assert np .array_equal (nb .load ('mask_000 .nii.gz ' ).get_data (),
116121 ([[[0 ,0 ],[0 ,0 ]],[[0 ,0 ],[1 ,0 ]]]))
117122 if method == 'intersect' :
118- assert np .array_equal (nb .load ('mask .nii' ).get_data (),
123+ assert np .array_equal (nb .load ('mask_000 .nii.gz ' ).get_data (),
119124 ([[[0 ,0 ],[0 ,0 ]],[[0 ,1 ],[0 ,0 ]]]))
120125
121126 def test_tcompcor_index_mask (self ):
122127 TCompCor (realigned_file = self .realigned_file ,
123128 mask_files = self .mask_files ,
124129 mask_index = 1 ).run ()
125- assert np .array_equal (nb .load ('mask .nii' ).get_data (),
130+ assert np .array_equal (nb .load ('mask_000 .nii.gz ' ).get_data (),
126131 ([[[0 ,0 ],[0 ,0 ]],[[0 ,1 ],[0 ,0 ]]]))
127132
133+ def test_tcompcor_multi_mask_no_index (self ):
134+ interface = TCompCor (realigned_file = self .realigned_file ,
135+ mask_files = self .mask_files )
136+ with pytest .raises (ValueError , message = 'more than one mask file' ):
137+ interface .run ()
138+
128139 def run_cc (self , ccinterface , expected_components , expected_header = 'CompCor' ):
129140 # run
130141 ccresult = ccinterface .run ()
@@ -137,12 +148,15 @@ def run_cc(self, ccinterface, expected_components, expected_header='CompCor'):
137148 assert ccinterface .inputs .num_components == 6
138149
139150 with open (ccresult .outputs .components_file , 'r' ) as components_file :
140- expected_n_components = min (ccinterface .inputs .num_components , self .fake_data .shape [3 ])
151+ expected_n_components = min (ccinterface .inputs .num_components ,
152+ self .fake_data .shape [3 ])
141153
142154 components_data = [line .split ('\t ' ) for line in components_file ]
143155
144- header = components_data .pop (0 ) # the first item will be '#', we can throw it out
145- expected_header = [expected_header + str (i ) for i in range (expected_n_components )]
156+ # the first item will be '#', we can throw it out
157+ header = components_data .pop (0 )
158+ expected_header = [expected_header + '{:02d}' .format (i ) for i in
159+ range (expected_n_components )]
146160 for i , heading in enumerate (header ):
147161 assert expected_header [i ] in heading
148162
0 commit comments