Skip to content

ENH/FIX: Resolve LinAlgError during SVD #2838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 22, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions nipype/algorithms/confounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import nibabel as nb
import numpy as np
from numpy.polynomial import Legendre
from scipy import linalg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out!


from .. import config, logging
from ..external.due import BibTeX
Expand Down Expand Up @@ -1193,9 +1194,12 @@ def compute_noise_components(imgseries, mask_images, num_components,
try:
u, _, _ = np.linalg.svd(M, full_matrices=False)
Copy link
Member

@effigies effigies Dec 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested try/except blocks get a little hard to reason about. How about a helper function that will do the fall-back:

# Use the numpy.linalg.svd protocol
def fallback_svd(a, full_matrices=True, compute_uv=True):
    try:
        return np.linalg.svd(a, full_matrices=full_matrices, compute_uv=compute_uv)
    except np.linalg.LinAlgError:
        pass

    from scipy.linalg import svd
    return svd(a, full_matrices=full_matrices, compute_uv=compute_uv, lapack_driver='gesvd')

And then we just replace the above with:

Suggested change
u, _, _ = np.linalg.svd(M, full_matrices=False)
u, _, _ = fallback_svd(M, full_matrices=False)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And to be clear, I'm suggesting we place the above fallback_svd function at the file level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! By putting it at the file level do you mean putting it at the top of the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Anywhere, really, but not nested within a class or another function. The top is fine.

except np.linalg.LinAlgError:
if self.inputs.failure_mode == 'error':
raise
u = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
try:
u, _, _ = linalg.svd(M, full_matrices=False, lapack_driver='gesvd')
except linalg.LinAlgError:
if self.inputs.failure_mode == 'error':
raise
u = np.ones((M.shape[0], num_components), dtype=np.float32) * np.nan
if components is None:
components = u[:, :num_components]
else:
Expand Down