-
Notifications
You must be signed in to change notification settings - Fork 46
Fix #86: Correct bounds/initial_guess passing in 5 wrapped algorithms #142
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
base: main
Are you sure you want to change the base?
Changes from all commits
b54c929
0038222
aa60e49
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 |
|---|---|---|
|
|
@@ -43,7 +43,7 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non | |
| self.use_bounds = {"f" : True, "D" : True, "Dp" : True, "S0" : True} | ||
| self.use_initial_guess = {"f" : False, "D" : False, "Dp" : False, "S0" : False} | ||
|
|
||
|
|
||
| def ivim_fit(self, signals, bvalues=None): | ||
| """Perform the IVIM fit | ||
|
|
||
|
|
@@ -52,30 +52,51 @@ def ivim_fit(self, signals, bvalues=None): | |
| bvalues (array-like, optional): b-values for the signals. If None, self.bvalues will be used. Default is None. | ||
|
|
||
| Returns: | ||
| _type_: _description_ | ||
| dict: Fitted IVIM parameters f, Dp (D*), and D. | ||
| """ | ||
| if self.bounds is None: | ||
| self.bounds = ([0.9, 0.0001, 0.0, 0.0025], [1.1, 0.003, 1, 0.2]) | ||
| # --- bvalues resolution --- | ||
| # Edge case: bvalues not passed here → fall back to the ones set at __init__ time | ||
| if bvalues is None: | ||
| if self.bvalues is None: | ||
| raise ValueError( | ||
| "PV_MUMC_biexp: bvalues must be provided either at initialization or at fit time." | ||
| ) | ||
| bvalues = self.bvalues | ||
| else: | ||
| bounds = ([self.bounds["S0"][0], self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0]], | ||
| [self.bounds["S0"][1], self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1]]) | ||
|
|
||
| bvalues = np.asarray(bvalues) | ||
|
|
||
| # --- Bounds resolution --- | ||
| # self.bounds is always a dict (OsipiBase force_default_settings=True). | ||
| # The underlying fit_least_squares expects: ([S0min, Dmin, fmin, Dpmin], [S0max, Dmax, fmax, Dpmax]) | ||
| if isinstance(self.bounds, dict): | ||
| bounds = ( | ||
| [self.bounds["S0"][0], self.bounds["D"][0], self.bounds["f"][0], self.bounds["Dp"][0]], | ||
| [self.bounds["S0"][1], self.bounds["D"][1], self.bounds["f"][1], self.bounds["Dp"][1]], | ||
| ) | ||
| else: | ||
| # Fallback: already in list/tuple form (legacy) | ||
| bounds = self.bounds | ||
|
|
||
| if self.thresholds is None: | ||
| self.thresholds = 200 | ||
|
|
||
| DEFAULT_PARAMS = [0.003,0.1,0.05] | ||
| # Default fallback parameters (D, f, Dp) used if the optimizer fails | ||
| DEFAULT_PARAMS = [0.003, 0.1, 0.05] | ||
|
Contributor
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. What happens if we set this to nan? Will the tests fail? Otherwise, I'd suggest 0 so that it's clear that there's something wrong in these voxels.
Contributor
Author
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. @IvanARashid sir,I think 0 is good as fallback to [0,0,0] will clearly tell that optimizer didnt converge fot the voxel. I think 0 is safer as nan can propagate through downstream calculations and potentially break things. If you prefer nan to be there, I can do that. Please tell me. Thank you.
Contributor
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. @oliverchampion I have some vague memory that you've done something like this at some point. Did you use nan? |
||
|
|
||
| try: | ||
| fit_results = self.PV_algorithm(bvalues, signals, bounds=bounds, cutoff=self.thresholds) | ||
| except RuntimeError as e: | ||
| if "maximum number of function evaluations" in str(e): | ||
| fit_results = DEFAULT_PARAMS | ||
| else: | ||
| raise | ||
| # curve_fit raises RuntimeError both for max-evaluations exceeded and other failures | ||
| print(f"PV_MUMC_biexp: optimizer failed ({e}). Returning default parameters.") | ||
| fit_results = DEFAULT_PARAMS | ||
| except Exception as e: | ||
| # Catch any other unexpected error (e.g. all-zero signal, NaNs in input) | ||
| print(f"PV_MUMC_biexp: unexpected error during fit ({type(e).__name__}: {e}). Returning default parameters.") | ||
| fit_results = DEFAULT_PARAMS | ||
|
|
||
| results = {} | ||
| results = {} | ||
| results["f"] = fit_results[1] | ||
| results["Dp"] = fit_results[2] | ||
| results["D"] = fit_results[0] | ||
|
|
||
| return results | ||
Uh oh!
There was an error while loading. Please reload this page.