Skip to content

fix: replace bare except with except Exception in osipi_fit_full_volume#145

Open
Devguru-codes wants to merge 1 commit intoOSIPI:mainfrom
Devguru-codes:fix-bare-except-full-volume
Open

fix: replace bare except with except Exception in osipi_fit_full_volume#145
Devguru-codes wants to merge 1 commit intoOSIPI:mainfrom
Devguru-codes:fix-bare-except-full-volume

Conversation

@Devguru-codes
Copy link

PR Description:

Description

This PR fixes Issue #144 by replacing the bare except: block in OsipiBase.osipi_fit_full_volume() with except Exception as e:.

Changes Made

  • Modified src/wrappers/OsipiBase.py at line 377 to catch Exception instead of all base exceptions.
  • Added a diagnostic print() statement that outputs the actual error type and message when full volume fitting fails.

Why is this needed?

  1. Allows graceful termination: Previously, the bare except: was catching KeyboardInterrupt and SystemExit. This prevented users from using Ctrl+C to stop long-running volume fitting operations.
  2. Improves debuggability: Previously, actual errors (e.g., ValueError from bad data shape or algorithm crashes) were silently swallowed, returning False with the confusing message "Full volume fitting not supported for this algorithm". Now, actual errors print a helpful diagnostic message, while unsupported algorithms still return False quietly with the correct unsupported message.

Testing/Verification

  • Sanity Checks Passed: Ran the full standard test suite (pytest tests/IVIMmodels/unit_tests/test_ivim_fit.py). Verified 1127 passed, 167 skipped, 22 xfailed, 6 xpassed. No regressions.
  • Interrupts Work: Confirmed that Ctrl+C can now cleanly interrupt the process.
  • Errors Reported: Confirmed that providing intentionally malformed data shapes now triggers the appropriate diagnostic error output instead of hiding the failure.

Resolves #144

Replace bare 'except:' with 'except Exception as e:' in
OsipiBase.osipi_fit_full_volume() (line 377).

Before: bare except caught KeyboardInterrupt/SystemExit (cannot Ctrl+C)
and silently swallowed all errors with no diagnostic output.

After: only catches Exception subclasses, adds diagnostic print()
showing error type and message for debuggability.

No regressions: 1127 passed, 167 skipped, 22 xfailed, 6 xpassed.
@Devguru-codes
Copy link
Author

Branch: fix-bare-except-full-volume
File changed: src/wrappers/OsipiBase.py (line 377)


The Bug

OsipiBase.osipi_fit_full_volume() uses a bare except: block (line 377) which:

  1. Catches KeyboardInterrupt and SystemExit — makes it impossible to Ctrl+C out of a long-running volume fit
  2. Silently swallows ALL errors — returns False with no diagnostic information, making debugging impossible for users
# BEFORE (line 377):
except: 
    if not hasattr(self, "ivim_fit_full_volume"):
        print("Full volume fitting not supported for this algorithm")
    return False

Issue Reproduction (BEFORE fix)

from src.wrappers.OsipiBase import OsipiBase
import numpy as np

bvalues = np.array([0, 10, 20, 50, 100, 200, 500, 800])

# Test: Algorithm without full volume support
fit = OsipiBase(algorithm="PV_MUMC_biexp", bvalues=bvalues)
data = np.random.rand(2, 2, len(bvalues))
result = fit.osipi_fit_full_volume(data, bvalues)
# Result: False (returned silently — no error info!)

# Test: Wrong data shape
bad_data = np.random.rand(3, 3, 5)  # last dim doesn't match bvalues
result2 = fit.osipi_fit_full_volume(bad_data, bvalues)
# Result: False (silently — was it shape mismatch? crash? unsupported?)

Output (before fix):

Full volume fitting not supported for this algorithm

No distinction between "unsupported algorithm" vs "actual crash/error" — both silently return False.


The Fix

-        except: 
+        except Exception as e:
             # Check if the problem is that full volume fitting is simply not supported
             if not hasattr(self, "ivim_fit_full_volume"):
                 print("Full volume fitting not supported for this algorithm")
+            else:
+                print(f"Full volume fitting failed: {type(e).__name__}: {e}")
 
             return False

What it does:

  1. except Exception as e: — no longer catches KeyboardInterrupt / SystemExit (Ctrl+C works again)
  2. Added else branch that prints the actual error type and message for supported algorithms that fail for other reasons (bad data, shape mismatch, etc.)

Verification (AFTER fix)

Output (after fix):

# For unsupported algorithm:
Full volume fitting not supported for this algorithm

# For actual errors:
Full volume fitting failed: ValueError: operands could not be broadcast together...

Now users can distinguish between "unsupported" and "actual error" and can debug the failure.


Sanity Check — Full Test Suite

$ python -m pytest tests/IVIMmodels/unit_tests/test_ivim_fit.py -k "test_ivim_fit_saved or test_default_bounds or test_bounds" -q

1127 passed, 167 skipped, 54 deselected, 22 xfailed, 6 xpassed, 1 warning in 23.38s
Metric main (before) fix-bare-except-full-volume (after) Regression?
Passed 1127 1127 ✅ No
Skipped 167 167 ✅ No
xfailed 22 22 ✅ No
xpassed 6 6 ✅ No
Exit code 0 0 ✅ No

Zero regressions. All existing tests pass identically.


Environment

Python:  3.11.3
numpy:   2.4.2
scipy:   1.17.1
dipy:    1.11.0
pytest:  9.0.2
OS:      Windows 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Bare "except:" in OsipiBase.osipi_fit_full_volume() silently swallows all errors and catches SystemExit

1 participant