cobrapy
cobrapy copied to clipboard
TypeError: in method 'Reaction_setReversible', argument 2 of type 'bool'
Problem description
Hello guys,
I was playing around when cobra refused to export a model to SBML with a TypeError: TypeError: in method 'Reaction_setReversible', argument 2 of type 'bool'
.
I believe this is because I had one of my Reaction.lower_bound
as a numpy.float64
instead of being a plain float
, which in sbml.py
would yield a numpy.bool_
at line reaction.setReversible((cobra_reaction.lower_bound < 0))
, and ultimately would be refused by libsbml's Reaction_setReversible
that is expecting a bool
.
I would have expected that numpy.float
to be valid bounds types.
I think the test in sbml.py
should be cast to bool
to prevent this error.
HTH, ++
Code Sample
import cobra.test
model = cobra.test.create_test_model("ecoli")
solution = model.optimize()
biomass = solution["BIOMASS_Ec_iJO1366_core_53p95M"]
model.reactions.BIOMASS_Ec_iJO1366_core_53p95M.bounds = (biomass * 0.5, 1000) # this fails
#model.reactions.BIOMASS_Ec_iJO1366_core_53p95M.bounds = (float(biomass * 0.5), 1000) # this works
failing_sbml_test = (model.reactions.BIOMASS_Ec_iJO1366_core_53p95M.lower_bound < 0)
print(type(failing_sbml_test)) # <class 'numpy.bool_'> or <class 'bool'>
cobra.io.write_sbml_model(model, "path.sbml")
Actual Output
Traceback:
TypeError Traceback (most recent call last)
<ipython-input-29-4b9c356149ff> in <module>
13 print(type(model.reactions.BIOMASS_Ec_iJO1366_core_53p95M.lower_bound), type(failing_sbml_test))
14
---> 15 cobra.io.write_sbml_model(model, "path.sbml")
XXX/cobra/io/sbml.py in write_sbml_model(cobra_model, filename, f_replace, **kwargs)
829 f_replace: dict of replacement functions for id replacement
830 """
--> 831 doc = _model_to_sbml(cobra_model, f_replace=f_replace, **kwargs)
832
833 if isinstance(filename, string_types):
XXX/cobra/io/sbml.py in _model_to_sbml(cobra_model, f_replace, units)
1014 reaction.setName(cobra_reaction.name)
1015 reaction.setFast(False)
-> 1016 reaction.setReversible((cobra_reaction.lower_bound < 0))
1017 _sbase_annotations(reaction, cobra_reaction.annotation)
1018 _sbase_notes_dict(reaction, cobra_reaction.notes)
XXX/libsbml/__init__.py in setReversible(self, value)
34480
34481 """
> 34482 return _libsbml.Reaction_setReversible(self, value)
34483
34484
TypeError: in method 'Reaction_setReversible', argument 2 of type 'bool'
Dependency Information
Package Versions
cobra 0.16.0 depinfo 1.5.1 future 0.17.1 numpy 1.16.4 optlang 1.4.4 pandas 0.25.0 pip 19.2.2 python-libsbml-experimental 5.18.0 ruamel.yaml 0.16.5 setuptools 41.0.1 six 1.12.0 swiglpk 4.65.0 wheel 0.33.4
I just came across the same incompatibility; it looks like libSBML will never accept numpy datatypes in bounds. @matthiaskoenig Would there be consequences to overriding this behavior within cobrapy by converting all numpy datatypes to native python floats when writing SBML (under the hood so that users aren't exposed to the issue)? I am guessing this is intentional within libsbml, but if not maybe the adjustment should be made in libsbml instead.
No issue at all. I will just do a cast to float.
On Thu, Nov 14, 2019, 20:22 Greg Medlock [email protected] wrote:
I just came across the same incompatibility; it looks like libSBML will never accept numpy datatypes in bounds. @matthiaskoenig https://github.com/matthiaskoenig Would there be consequences to overriding this behavior within cobrapy by converting all numpy datatypes to native python floats when writing SBML (under the hood so that users aren't exposed to the issue)? I am guessing this is intentional within libsbml, but if not maybe the adjustment should be made in libsbml instead.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/opencobra/cobrapy/issues/905?email_source=notifications&email_token=AAG33OXJCDR2533X2T7LC73QTWQOZA5CNFSM4ITGHMYKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEC7SEI#issuecomment-554039569, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG33OXK62YUFLCHTDHOYATQTWQOZANCNFSM4ITGHMYA .