BUG: Backport zero-check guard in CumulativeGaussianCostFunction#6067
BUG: Backport zero-check guard in CumulativeGaussianCostFunction#6067hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
Conversation
Guard the division by numberOfElements on line 55 against division-by-zero when m_OriginalDataArray is empty. The existing size-mismatch check did not catch the zero case. Cherry-pick of d7845fe from main, adapted for release-5.4 where the zero check was entirely absent (main had it in the wrong order).
|
| Filename | Overview |
|---|---|
| Modules/Numerics/Optimizers/src/itkCumulativeGaussianCostFunction.cxx | Adds numberOfElements == 0 guard via short-circuit ` |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["CalculateFitError(setTestArray)"] --> B["numberOfElements = m_OriginalDataArray.GetNumberOfElements()"]
B --> C{"numberOfElements == 0?"}
C -- "Yes (NEW guard)" --> D["return 1"]
C -- "No" --> E{"numberOfElements != setTestArray->size?"}
E -- "Yes" --> D
E -- "No" --> F["Sum squared differences in loop"]
F --> G["return sqrt((1/numberOfElements) * fitError)"]
style C fill:#90ee90,stroke:#228B22
style D fill:#ffcccc
Comments Outside Diff (1)
-
Modules/Numerics/Optimizers/src/itkCumulativeGaussianCostFunction.cxx, line 55 (link)Pre-existing integer division yields incorrect RMSE
1 / numberOfElementsperforms integer division because both operands are integer types (intliteral andunsigned int). For anynumberOfElements > 1, this evaluates to0, makingsqrt(0 * fitError) == 0.0— the RMSE is silently wrong for all non-trivial arrays. The PR correctly guards againstnumberOfElements == 0, but since this function is being modified, this is a good opportunity to fix the formula.
Reviews (1): Last reviewed commit: "BUG: Add zero-check guard in CumulativeG..." | Re-trigger Greptile
hjmjohnson
left a comment
There was a problem hiding this comment.
greptile comments need to ensure that upstream/main has already fixed this, if not fix upstream/main and PR.
This is a backport from main -> release-5.4.
Adapted from d7845fe on main. Adds
numberOfElements == 0check before the size-mismatch check inCalculateFitErrorto guard against division-by-zero.On main this was a reorder of an existing compound condition; on release-5.4 the zero check was entirely absent.
Backport for #6051 (Tier 1).