Skip to content

Commit 577e395

Browse files
committed
add unit test for variance_threshold condition
1 parent 2743189 commit 577e395

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

nipype/algorithms/tests/test_CompCor.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
22
# vi: set ft=python sts=4 ts=4 sw=4 et:
33
import os
4+
import re
45

56
import nibabel as nb
67
import numpy as np
@@ -62,6 +63,30 @@ def test_compcor(self):
6263
components_file='acc_components_file'), expected_components,
6364
'aCompCor')
6465

66+
def test_compcor_variance_threshold_and_metadata(self):
67+
expected_components = [['-0.2027150345', '-0.4954813834'],
68+
['0.2565929051', '0.7866217875'],
69+
['-0.3550986008', '-0.0089784905'],
70+
['0.7512786244', '-0.3599828482'],
71+
['-0.4500578942', '0.0778209345']]
72+
expected_metadata = {
73+
'component': 'CompCor00',
74+
'mask': '0',
75+
'singular_value': '4.0720553036',
76+
'variance_explained': '0.5527211465',
77+
'cumulative_variance_explained': '0.5527211465'
78+
}
79+
ccinterface = CompCor(
80+
variance_threshold=0.7,
81+
realigned_file=self.realigned_file,
82+
mask_files=self.mask_files,
83+
mask_index=1,
84+
save_metadata=True)
85+
self.run_cc(ccinterface=ccinterface,
86+
expected_components=expected_components,
87+
expected_n_components=2,
88+
expected_metadata=expected_metadata)
89+
6590
def test_tcompcor(self):
6691
ccinterface = TCompCor(num_components=6,
6792
realigned_file=self.realigned_file, percentile_threshold=0.75)
@@ -155,7 +180,9 @@ def test_tcompcor_multi_mask_no_index(self):
155180
def run_cc(self,
156181
ccinterface,
157182
expected_components,
158-
expected_header='CompCor'):
183+
expected_header='CompCor',
184+
expected_n_components=None,
185+
expected_metadata=None):
159186
# run
160187
ccresult = ccinterface.run()
161188

@@ -166,10 +193,12 @@ def run_cc(self,
166193
assert os.path.getsize(expected_file) > 0
167194

168195
with open(ccresult.outputs.components_file, 'r') as components_file:
169-
expected_n_components = min(ccinterface.inputs.num_components,
170-
self.fake_data.shape[3])
196+
if expected_n_components is None:
197+
expected_n_components = min(ccinterface.inputs.num_components,
198+
self.fake_data.shape[3])
171199

172-
components_data = [line.split('\t') for line in components_file]
200+
components_data = [re.sub('\n', '', line).split('\t')
201+
for line in components_file]
173202

174203
# the first item will be '#', we can throw it out
175204
header = components_data.pop(0)
@@ -183,9 +212,24 @@ def run_cc(self,
183212
num_got_timepoints = len(components_data)
184213
assert num_got_timepoints == self.fake_data.shape[3]
185214
for index, timepoint in enumerate(components_data):
186-
assert (len(timepoint) == ccinterface.inputs.num_components
187-
or len(timepoint) == self.fake_data.shape[3])
215+
assert (len(timepoint) == expected_n_components)
188216
assert timepoint[:2] == expected_components[index]
217+
218+
if ccinterface.inputs.save_metadata:
219+
expected_metadata_file = (
220+
ccinterface._list_outputs()['metadata_file'])
221+
assert ccresult.outputs.metadata_file == expected_metadata_file
222+
assert os.path.exists(expected_metadata_file)
223+
assert os.path.getsize(expected_metadata_file) > 0
224+
225+
with open(ccresult.outputs.metadata_file, 'r') as metadata_file:
226+
components_metadata = [re.sub('\n', '', line).split('\t')
227+
for line in metadata_file]
228+
components_metadata = {i: j for i, j in
229+
zip(components_metadata[0],
230+
components_metadata[1])}
231+
assert components_metadata == expected_metadata
232+
189233
return ccresult
190234

191235
@staticmethod

0 commit comments

Comments
 (0)