Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions msbuddy/ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def _gen_arr_from_buddy_data(buddy_data) -> (np.array, np.array, np.array):
continue
# generate ML features for each candidate formula
for cf in mf.candidate_formula_list:
# skip invalid formulas (mass <= 0 or no carbons)
if cf.formula.mass <= 0 or cf.formula.array[0] == 0:
continue
Comment on lines +46 to +48

Copilot AI Feb 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
# add formula array to all_cand_form_arr
all_cand_form_arr = np.append(all_cand_form_arr, [cf.formula.array], axis=0)
dbe_arr = np.append(dbe_arr, cf.formula.dbe)
Expand Down Expand Up @@ -141,6 +144,11 @@ def _fill_form_feature_arr_in_batch_data(batch_data, feature_arr) -> None:
continue
# fill in ML features for each candidate formula
for cf in mf.candidate_formula_list:
# skip invalid formulas, same as in _gen_arr_from_buddy_data
if cf.formula.mass <= 0 or cf.formula.array[0] == 0:
# fallback: assign zeros so concatenation won’t break
cf.formula_feature_array = np.zeros((26,))
continue
cf.formula_feature_array = feature_arr[cnt, :]
cnt += 1
return
Expand Down
Loading