fixes #6#7
Conversation
There was a problem hiding this comment.
Pull request overview
This PR attempts to address issue #6 where unrecognized adducts in MGF files lead to dummy formula/mass values, causing a ZeroDivisionError: division by zero. The fix adds validation to skip invalid formulas (mass <= 0 or no carbons) in two key functions.
Changes:
- Added validation in
_gen_arr_from_buddy_datato skip invalid formulas before generating ML feature arrays - Added validation in
_fill_form_feature_arr_in_batch_datato assign zero features to invalid formulas as a fallback
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # skip invalid formulas (mass <= 0 or no carbons) | ||
| if cf.formula.mass <= 0 or cf.formula.array[0] == 0: | ||
| continue |
There was a problem hiding this comment.
The fix prevents ZeroDivisionError in _gen_form_feature by filtering out invalid formulas, but there's an incomplete fix in the call chain. When gen_ml_feature is called from _predict_ml (line 442), it processes all candidate formulas including invalid ones. In gen_ml_feature_single (lines 207-208), if cand_form.charged_formula.mass is zero or negative (which can happen with unrecognized adducts), calculating theo_mass and mz_error will cause a ZeroDivisionError. Since the current approach assigns zero features to invalid formulas and includes them in processing, consider adding a guard in gen_ml_feature_single to handle formulas with mass <= 0, similar to how the H/C ratio is handled at line 214.
Suggested fix for #6