Skip to content

Commit 5d37cae

Browse files
authored
Merge pull request #2819 from effigies/enh/compcor_svd_failure
ENH: Add NaN failure mode to CompCor interfaces
2 parents fbed1f3 + 16af71e commit 5d37cae

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

nipype/algorithms/confounds.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,11 @@ class CompCorInputSpec(BaseInterfaceInputSpec):
412412
low=0,
413413
usedefault=True,
414414
desc='Number of volumes at start of series to ignore')
415+
failure_mode = traits.Enum(
416+
'error', 'NaN',
417+
usedefault=True,
418+
desc='When no components are found or convergence fails, raise an error '
419+
'or silently return columns of NaNs.')
415420

416421

417422
class CompCorOutputSpec(TraitedSpec):
@@ -1185,13 +1190,20 @@ def compute_noise_components(imgseries, mask_images, num_components,
11851190

11861191
# "The covariance matrix C = MMT was constructed and decomposed into its
11871192
# principal components using a singular value decomposition."
1188-
u, _, _ = np.linalg.svd(M, full_matrices=False)
1193+
try:
1194+
u, _, _ = np.linalg.svd(M, full_matrices=False)
1195+
except np.linalg.LinAlgError:
1196+
if self.inputs.failure_mode == 'error':
1197+
raise
1198+
u = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
11891199
if components is None:
11901200
components = u[:, :num_components]
11911201
else:
11921202
components = np.hstack((components, u[:, :num_components]))
11931203
if components is None and num_components > 0:
1194-
raise ValueError('No components found')
1204+
if self.inputs.failure_mode == 'error':
1205+
raise ValueError('No components found')
1206+
components = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
11951207
return components, basis
11961208

11971209

0 commit comments

Comments
 (0)