-
Notifications
You must be signed in to change notification settings - Fork 532
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,7 @@ | |||||
import nibabel as nb | ||||||
import numpy as np | ||||||
from numpy.polynomial import Legendre | ||||||
from scipy import linalg | ||||||
|
||||||
from .. import config, logging | ||||||
from ..external.due import BibTeX | ||||||
|
@@ -1193,9 +1194,12 @@ def compute_noise_components(imgseries, mask_images, num_components, | |||||
try: | ||||||
u, _, _ = np.linalg.svd(M, full_matrices=False) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested # 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And to be clear, I'm suggesting we place the above There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unneeded import.
There was a problem hiding this comment.
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!