Skip to content

[BUG] check_mass_balance fails for floating point coefficients #1435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
jedforrest opened this issue Apr 15, 2025 · 0 comments · May be fixed by #1436
Open
1 task done

[BUG] check_mass_balance fails for floating point coefficients #1435

jedforrest opened this issue Apr 15, 2025 · 0 comments · May be fixed by #1436
Labels

Comments

@jedforrest
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Problem description

The check_mass_balance function for reactions gives an incorrect result for reactions with floating point coefficients.

Specifically, scaling the coefficients by a float (like in the example below) returns a dictionary of near-zero values. A non-empty dictionary implies the reaction is unbalanced, when it's really just a floating-point error.

A simple fix is to filter out near-zero values with something like numpy.isclose.

Code sample

Code run:

from cobra import Model, Metabolite, Reaction

# setup
model = Model("model")

adp = Metabolite("adp", formula="C10H12N5O10P2", charge=-3)
atp = Metabolite("atp", formula="C10H12N5O13P3", charge=-4)
h = Metabolite("h", formula="H", charge=1)
h2o = Metabolite("h2o", formula="H2O", charge=0)
pi = Metabolite("pi", formula="HO4P", charge=-2)
model.add_metabolites([adp, atp, h, h2o, pi])

reaction = Reaction("reaction")
reaction.add_metabolites({atp: -1, h2o: -1, adp: 1, pi: 1, h: 1})
model.add_reactions([reaction])

print(reaction.check_mass_balance())
# {}

# float coefficients return a mass balance dict of near-zero values
reaction_scaled = reaction * 1.3
print(reaction_scaled.check_mass_balance())
# {'charge': -2.220446049250313e-16, 'H': -1.3322676295501878e-15, 'O': -2.6645352591003757e-15, 'P': -2.220446049250313e-16}

Environment

Anything else?

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant