@@ -48,20 +48,48 @@ def test_compcor(self):
4848
4949 self .run_cc (
5050 CompCor (
51+ num_components = 6 ,
5152 realigned_file = self .realigned_file ,
5253 mask_files = self .mask_files ,
5354 mask_index = 0 ), expected_components )
5455
5556 self .run_cc (
5657 ACompCor (
58+ num_components = 6 ,
5759 realigned_file = self .realigned_file ,
5860 mask_files = self .mask_files ,
5961 mask_index = 0 ,
6062 components_file = 'acc_components_file' ), expected_components ,
6163 'aCompCor' )
6264
65+ def test_compcor_variance_threshold_and_metadata (self ):
66+ expected_components = [['-0.2027150345' , '-0.4954813834' ],
67+ ['0.2565929051' , '0.7866217875' ],
68+ ['-0.3550986008' , '-0.0089784905' ],
69+ ['0.7512786244' , '-0.3599828482' ],
70+ ['-0.4500578942' , '0.0778209345' ]]
71+ expected_metadata = {
72+ 'component' : 'CompCor00' ,
73+ 'mask' : 'mask' ,
74+ 'singular_value' : '4.0720553036' ,
75+ 'variance_explained' : '0.5527211465' ,
76+ 'cumulative_variance_explained' : '0.5527211465' ,
77+ 'retained' : 'True' ,
78+ }
79+ ccinterface = CompCor (
80+ variance_threshold = 0.7 ,
81+ realigned_file = self .realigned_file ,
82+ mask_files = self .mask_files ,
83+ mask_names = ['mask' ],
84+ mask_index = 1 ,
85+ save_metadata = True )
86+ self .run_cc (ccinterface = ccinterface ,
87+ expected_components = expected_components ,
88+ expected_n_components = 2 ,
89+ expected_metadata = expected_metadata )
90+
6391 def test_tcompcor (self ):
64- ccinterface = TCompCor (
92+ ccinterface = TCompCor (num_components = 6 ,
6593 realigned_file = self .realigned_file , percentile_threshold = 0.75 )
6694 self .run_cc (ccinterface , [['-0.1114536190' , '-0.4632908609' ], [
6795 '0.4566907310' , '0.6983205193'
@@ -70,7 +98,8 @@ def test_tcompcor(self):
7098 ], ['-0.1342351356' , '0.1407855119' ]], 'tCompCor' )
7199
72100 def test_tcompcor_no_percentile (self ):
73- ccinterface = TCompCor (realigned_file = self .realigned_file )
101+ ccinterface = TCompCor (num_components = 6 ,
102+ realigned_file = self .realigned_file )
74103 ccinterface .run ()
75104
76105 mask = nb .load ('mask_000.nii.gz' ).get_data ()
@@ -80,6 +109,7 @@ def test_tcompcor_no_percentile(self):
80109 def test_compcor_no_regress_poly (self ):
81110 self .run_cc (
82111 CompCor (
112+ num_components = 6 ,
83113 realigned_file = self .realigned_file ,
84114 mask_files = self .mask_files ,
85115 mask_index = 0 ,
@@ -151,7 +181,9 @@ def test_tcompcor_multi_mask_no_index(self):
151181 def run_cc (self ,
152182 ccinterface ,
153183 expected_components ,
154- expected_header = 'CompCor' ):
184+ expected_header = 'CompCor' ,
185+ expected_n_components = None ,
186+ expected_metadata = None ):
155187 # run
156188 ccresult = ccinterface .run ()
157189
@@ -160,13 +192,14 @@ def run_cc(self,
160192 assert ccresult .outputs .components_file == expected_file
161193 assert os .path .exists (expected_file )
162194 assert os .path .getsize (expected_file ) > 0
163- assert ccinterface .inputs .num_components == 6
164195
165196 with open (ccresult .outputs .components_file , 'r' ) as components_file :
166- expected_n_components = min (ccinterface .inputs .num_components ,
167- self .fake_data .shape [3 ])
197+ if expected_n_components is None :
198+ expected_n_components = min (ccinterface .inputs .num_components ,
199+ self .fake_data .shape [3 ])
168200
169- components_data = [line .split ('\t ' ) for line in components_file ]
201+ components_data = [line .rstrip ().split ('\t ' )
202+ for line in components_file ]
170203
171204 # the first item will be '#', we can throw it out
172205 header = components_data .pop (0 )
@@ -180,9 +213,24 @@ def run_cc(self,
180213 num_got_timepoints = len (components_data )
181214 assert num_got_timepoints == self .fake_data .shape [3 ]
182215 for index , timepoint in enumerate (components_data ):
183- assert (len (timepoint ) == ccinterface .inputs .num_components
184- or len (timepoint ) == self .fake_data .shape [3 ])
216+ assert (len (timepoint ) == expected_n_components )
185217 assert timepoint [:2 ] == expected_components [index ]
218+
219+ if ccinterface .inputs .save_metadata :
220+ expected_metadata_file = (
221+ ccinterface ._list_outputs ()['metadata_file' ])
222+ assert ccresult .outputs .metadata_file == expected_metadata_file
223+ assert os .path .exists (expected_metadata_file )
224+ assert os .path .getsize (expected_metadata_file ) > 0
225+
226+ with open (ccresult .outputs .metadata_file , 'r' ) as metadata_file :
227+ components_metadata = [line .rstrip ().split ('\t ' )
228+ for line in metadata_file ]
229+ components_metadata = {i : j for i , j in
230+ zip (components_metadata [0 ],
231+ components_metadata [1 ])}
232+ assert components_metadata == expected_metadata
233+
186234 return ccresult
187235
188236 @staticmethod
0 commit comments