Skip to content

Commit 09f3b27

Browse files
committed
Refactor try/except to function.
1 parent b60c8ce commit 09f3b27

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

nipype/algorithms/confounds.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
IFLOGGER = logging.getLogger('nipype.interface')
2828

2929

30+
def fallback_svd(a, full_matrices=True, compute_uv=True):
31+
try:
32+
return np.linalg.svd(a, full_matrices=full_matrices, compute_uv=compute_uv)
33+
except np.linalg.LinAlgError:
34+
pass
35+
36+
from scipy.linalg import svd
37+
return svd(a, full_matrices=full_matrices, compute_uv=compute_uv, lapack_driver='gesvd')
38+
39+
3040
class ComputeDVARSInputSpec(BaseInterfaceInputSpec):
3141
in_file = File(
3242
exists=True, mandatory=True, desc='functional data, after HMC')
@@ -1192,14 +1202,11 @@ def compute_noise_components(imgseries, mask_images, num_components,
11921202
# "The covariance matrix C = MMT was constructed and decomposed into its
11931203
# principal components using a singular value decomposition."
11941204
try:
1195-
u, _, _ = np.linalg.svd(M, full_matrices=False)
1205+
u, _, _ = fallback_svd(M, full_matrices=False)
11961206
except np.linalg.LinAlgError:
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
1207+
if self.inputs.failure_mode == 'error':
1208+
raise
1209+
u = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
12031210
if components is None:
12041211
components = u[:, :num_components]
12051212
else:
@@ -1277,7 +1284,7 @@ def _full_rank(X, cmax=1e15):
12771284
X: array of shape(nrows, ncols) after regularization
12781285
cmax=1.e-15, float tolerance for condition number
12791286
"""
1280-
U, s, V = np.linalg.svd(X, 0)
1287+
U, s, V = fallback_svd(X, full_matrices=False)
12811288
smax, smin = s.max(), s.min()
12821289
c = smax / smin
12831290
if c < cmax:

0 commit comments

Comments
 (0)