Skip to content

Commit 227e2f8

Browse files
robertyoung3claude
andcommitted
Fix MolecularFormula._calc_kmd ignoring kendrick_rounding_method
MolecularFormulaCalc._calc_kmd() used int() to derive the nominal Kendrick mass, which always truncates toward zero (equivalent to floor for positive values). This ignored 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 as MSPeakCalc. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0176770 commit 227e2f8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

corems/molecular_formula/calc/MolecularFormulaCalc.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import warnings
66

77
import IsoSpecPy
8-
from numpy import array, exp, isnan, nextafter, power
8+
from numpy import array, ceil, exp, floor, isnan, nextafter, power, rint
99

1010
# this is to handle both versions of IsoSpecPy, 2.0.2 and 2.2.2
1111
# TODO in a future release remove support for legacy isospecpy
@@ -711,13 +711,30 @@ def _calc_kmd(self, dict_base):
711711
tuple
712712
The tuple of the KMD, Kendrick mass, and nominal Kendrick mass.
713713
"""
714+
if hasattr(self, "_mspeak_parent") and self._mspeak_parent:
715+
kendrick_rounding_method = (
716+
self._mspeak_parent._ms_parent.mspeaks_settings.kendrick_rounding_method
717+
)
718+
else:
719+
kendrick_rounding_method = MSParameters.ms_peak.kendrick_rounding_method
720+
714721
mass = 0
715722
for atom in dict_base.keys():
716723
mass = mass + Atoms.atomic_masses.get(atom) * dict_base.get(atom)
717724

718725
kendrick_mass = (int(mass) / mass) * self.mz_calc
719726

720-
nominal_km = int(kendrick_mass)
727+
if kendrick_rounding_method == "ceil":
728+
nominal_km = ceil(kendrick_mass)
729+
elif kendrick_rounding_method == "round":
730+
nominal_km = rint(kendrick_mass)
731+
elif kendrick_rounding_method == "floor":
732+
nominal_km = floor(kendrick_mass)
733+
else:
734+
raise Exception(
735+
"%s method was not implemented, please refer to corems.ms_peak.calc.MSPeakCalc Class"
736+
% kendrick_rounding_method
737+
)
721738

722739
kmd = (nominal_km - kendrick_mass) * 100
723740

0 commit comments

Comments
 (0)