Skip to content

bug(calibration): bare except blocks silently swallow all exceptions during optimization #153

Description

@MAfarrag

Context

During PR review of #149 (mkdocs documentation), the silent-failure-hunter
agent flagged three bare except: blocks inside calibration optimization
closures. These are pre-existing bugs not introduced by the PR, but they
hide real errors (including the broken wrapper method names that went
undetected for months).

Related: #146 (unit tests), #149 (docs PR)

Problem / Current Behaviour

Three opt_fun closures in Calibration catch every exception type
(including KeyboardInterrupt, SystemExit, MemoryError), silently
set error = np.nan, and return fail = 1 to the optimizer. During
hours-long calibration runs, any programming error produces garbage
results with zero diagnostic output.

Affected locations

File Symbol Notes
`src/Hapi/calibration.py:~269` `run_calibration.opt_fun` Bare `except:`
`src/Hapi/calibration.py:~415` `FW1Calibration.opt_fun` Bare `except:`
`src/Hapi/calibration.py:~550` `lumpedCalibration.opt_fun` Bare `except:`

Steps to Reproduce

```python

If any model evaluation raises an error (e.g., wrong array shape),

the optimizer silently receives NaN and continues for hours.

from Hapi.calibration import Calibration

... setup calibration with a typo in parameter bounds ...

Calibration runs to completion but every iteration returned NaN.

No error, no traceback, no indication of root cause.

```

Proposed Solution

```python

Minimum fix: catch Exception (not bare except), log the error

except Exception as e:
logger.warning(f"Model evaluation failed: {e}")
error = np.nan
g = []
fail = 1
```

Ideally, distinguish between expected numerical failures (which the
optimizer should handle) and programming errors (which should halt).

Out of Scope

  • Refactoring the optimization loop itself
  • Changing the Harmony Search algorithm interface

Effort Estimate

Size: `S`
Rationale: Mechanical replacement of 3 bare except blocks with
`except Exception as e:` plus a log statement.

Definition of Done

  • All three bare `except:` replaced with `except Exception as e:`
  • Exception is logged with `logger.warning` or `logger.error`
  • `KeyboardInterrupt` and `SystemExit` are no longer caught
  • Existing calibration tests still pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions