Skip to content

Fix MolecularFormula._calc_kmd ignoring kendrick_rounding_method#29

Open
robertyoung3 wants to merge 1 commit intoEMSL-Computing:masterfrom
robertyoung3:fix/formula-kmd-rounding-method
Open

Fix MolecularFormula._calc_kmd ignoring kendrick_rounding_method#29
robertyoung3 wants to merge 1 commit intoEMSL-Computing:masterfrom
robertyoung3:fix/formula-kmd-rounding-method

Conversation

@robertyoung3
Copy link

Summary

  • MolecularFormulaCalc._calc_kmd() uses int() to derive the nominal Kendrick mass, which always truncates toward zero (equivalent to floor for positive values)
  • This ignores the user-configurable kendrick_rounding_method setting (floor/ceil/round) that MSPeakCalc._calc_kmd() already respects
  • Now reads kendrick_rounding_method from the parent peak's settings (or MSParameters.ms_peak as fallback) and applies the same floor/ceil/rint logic

Before

nominal_km = int(kendrick_mass)  # always truncates (floor)

After

if kendrick_rounding_method == "ceil":
    nominal_km = ceil(kendrick_mass)
elif kendrick_rounding_method == "round":
    nominal_km = rint(kendrick_mass)
elif kendrick_rounding_method == "floor":
    nominal_km = floor(kendrick_mass)

Context

MSPeakCalc._calc_kmd() (for experimental m/z) already respects this setting. MolecularFormulaCalc._calc_kmd() (for theoretical m/z) was inconsistent. With the default kendrick_rounding_method="floor", the behavior is unchanged since int() and floor() are equivalent for positive values.

Test plan

  • Default behavior (floor) is unchanged — int() and floor() produce identical results for positive Kendrick masses
  • Settings access follows the same pattern as kendrick_base (parent peak settings → MSParameters fallback)
  • Includes hasattr guard for safety when _mspeak_parent is not yet set

@robertyoung3 robertyoung3 force-pushed the fix/formula-kmd-rounding-method branch from 227e2f8 to ff63e11 Compare March 3, 2026 00:56
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.

1 participant