Skip to content

Commit dcc013b

Browse files
authored
Merge pull request #2838 from feilong/patch-1
ENH/FIX: Resolve LinAlgError during SVD
2 parents b582d53 + 053fd61 commit dcc013b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

nipype/algorithms/confounds.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
IFLOGGER = logging.getLogger('nipype.interface')
2727

2828

29+
def fallback_svd(a, full_matrices=True, compute_uv=True):
30+
try:
31+
return np.linalg.svd(a, full_matrices=full_matrices, compute_uv=compute_uv)
32+
except np.linalg.LinAlgError:
33+
pass
34+
35+
from scipy.linalg import svd
36+
return svd(a, full_matrices=full_matrices, compute_uv=compute_uv, lapack_driver='gesvd')
37+
38+
2939
class ComputeDVARSInputSpec(BaseInterfaceInputSpec):
3040
in_file = File(
3141
exists=True, mandatory=True, desc='functional data, after HMC')
@@ -1191,7 +1201,7 @@ def compute_noise_components(imgseries, mask_images, num_components,
11911201
# "The covariance matrix C = MMT was constructed and decomposed into its
11921202
# principal components using a singular value decomposition."
11931203
try:
1194-
u, _, _ = np.linalg.svd(M, full_matrices=False)
1204+
u, _, _ = fallback_svd(M, full_matrices=False)
11951205
except np.linalg.LinAlgError:
11961206
if self.inputs.failure_mode == 'error':
11971207
raise
@@ -1273,7 +1283,7 @@ def _full_rank(X, cmax=1e15):
12731283
X: array of shape(nrows, ncols) after regularization
12741284
cmax=1.e-15, float tolerance for condition number
12751285
"""
1276-
U, s, V = np.linalg.svd(X, 0)
1286+
U, s, V = fallback_svd(X, full_matrices=False)
12771287
smax, smin = s.max(), s.min()
12781288
c = smax / smin
12791289
if c < cmax:

0 commit comments

Comments
 (0)