Skip to content

Commit b60c8ce

Browse files
authored
Resolve LinAlgError during SVD
The default `lapack_driver` uses randomized SVD, which occasionally will cause `LinAlgError` even if the data are completely fine. Switching `lapack_driver` to `gesvd` will solve this issue.
1 parent b582d53 commit b60c8ce

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

nipype/algorithms/confounds.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import nibabel as nb
1515
import numpy as np
1616
from numpy.polynomial import Legendre
17+
from scipy import linalg
1718

1819
from .. import config, logging
1920
from ..external.due import BibTeX
@@ -1193,9 +1194,12 @@ def compute_noise_components(imgseries, mask_images, num_components,
11931194
try:
11941195
u, _, _ = np.linalg.svd(M, full_matrices=False)
11951196
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
1197+
try:
1198+
u, _, _ = linalg.svd(M, full_matrices=False, lapack_driver='gesvd')
1199+
except linalg.LinAlgError:
1200+
if self.inputs.failure_mode == 'error':
1201+
raise
1202+
u = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
11991203
if components is None:
12001204
components = u[:, :num_components]
12011205
else:

0 commit comments

Comments
 (0)