model not properly returned

22 views
Skip to first unread message

Michiel Karrenbelt

unread,
Oct 19, 2018, 7:57:30 AM10/19/18
to sbml-interoperability
I decided to separate the reading from the rest of the processing. 

def sbml_to_model(filename):
   
   
assert os.path.isfile(filename)  
    doc
= libsbml.readSBMLFromFile(filename)
   
   
if doc.getNumErrors(libsbml.LIBSBML_SEV_ERROR) > 0:
       
print("There were errors while reading, better to fix those first")
        doc
.printErrors()
        sys
.exit(1)    
   
    model
= doc.getModel()
   
if model is None:
       
raise ValueError("document has no model, bailing")
   
   
return model


however, it broke my code

>>> model = sbml_to_model(filename)
>>> model.getListOfReactions()
<ListOfReactions[0]>

VS

>>> doc = libsbml.readSBMLFromFile(filename)
>>> model = doc.getModel()
>>> model.getListOfReactions()
<ListOfReactions[116]>


what evil magic is this?



Brett Olivier

unread,
Oct 19, 2018, 8:40:18 AM10/19/18
to sbml-interoperability
Python is garbage collecting your document when it goes out of the function scope. Try passing the document into the function instead.

In [2]: import libsbml

In [3]: D = libsbml.readSBMLFromFile('BIGG2_iAF1260.xml')

In [4]: def sbml_to_model(doc):
   ...:     M = doc.getModel()
   ...:     return M
   ...:

In [5]: D.getModel().getNumReactions()
Out[5]: 2382

In [6]: sbml_to_model(D).getNumReactions()
Out[6]: 2382

Matthias König

unread,
Oct 19, 2018, 8:42:41 AM10/19/18
to sbml-inter...@googlegroups.com
You always have to keep at least one reference to your SBMLDocument. A reference to the model is not enough. Yes, evel magic.

--
You received this message because you are subscribed to the Google Groups "sbml-interoperability" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sbml-interoperab...@googlegroups.com.
To post to this group, send email to sbml-inter...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sbml-interoperability/98d78293-261c-4c75-9d51-f5a1433bd1e0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Hucka

unread,
Oct 19, 2018, 10:34:10 PM10/19/18
to sbml-interoperability
Ouch.

Is there any way to improve this behavior, specifically by preventing
the document object from being garbage collected if other parts of the
structure are still being used? A Python user would rightly expect that
holding the model would be enough to keep the other pieces around. I
know the SWIG wrapping often gets in the way, but perhaps there's
something that can be done about this?

MH

Thomas Pfau

unread,
Oct 22, 2018, 2:40:36 AM10/22/18
to sbml-inter...@googlegroups.com
Hi,

I'm a little puzzled, but that might be my lack of python.
Why are the contents of the model cleaned, but the model itself is still
valid?

To me, it looks like the SBMLDocument cleans up all its contents
(calling destructors on all its elements), which is the correct
behaviour for C but not really expected in python (or am I wrong).
Shouldn't pythons carbage collection worry about cleaning up the
subelements of the model instead of the SBMLDocument instructing the
model to delete itself? Or am I missing something here?

Best

Thomas




On 2018-10-20 04:34, Michael Hucka wrote:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>                                                 !!!! Unsubscribe
> !!!!                                                 
>                      It appears that you have subscribed to commercial
> messages from this sender.                      To stop receiving such
> messages from this sender, please unsubscribe :
> <http://secure-web.cisco.com/1b3jtedhSLXpk0cL1zXSEk1dth1oqF2BK6HKKebck37uebry5oWYSQEtq9D58FLmcfinrj-WeZEPsqbouKIvMXsqOEjBFV4E6WsFlm6ioulnJFARdaWcjP_c5UWRaEq93K2ft77XgNTo_nimA-_SeHxlJ7PBzBnbXUphgRRGbVmG4ftQR2bm9jnpXTmnzm2B8CWftANlqHygi-ewWugLxHgM9WfWhKXUD_5YOwIRZvlCpWhRczpqjPhJgtFfa2avorJSVj9J-leUxvRguYCuwlzOvfLQtziQw2iYUNDQvS6dVbW7annvR_mSUi2r2ufFNnan7rWN9oDTuoUdYvmoSIFr58dkoC-afWtBxlOO9k5-aMCwPraK-ZpVAlebt6wQgXucKq11Hk4BTYuveW2akfNt_EgcsygxCM27BdZ1cABU/l67%3Amailto%3Agooglegroups-manage%2B14100737528%2Bunsubscribe%40googlegroups.com63%3Ahttps%3A%2F%2Fgroups.google.com%2Fgroup%2Fsbml-interoperability%2Fsubscribe111%3Ahttps%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fsbml-interoperability%2F98d78293-261c-4c75-9d51-f5a1433bd1e0%40googlegroups.com%20%2034%3Ahttps%3A%2F%2Fgroups.google.com%2Fd%2Foptout114%3Ahttps%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fsbml-interoperability%2F1E62CD98-B7E7-40C5-9E09-849DFD21AA45%40library.caltech.edu%20%20e>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> http://secure-web.cisco.com/1kkunTTEEnq0vhv3ErysEor3MKo31fAKs3KLEFKTFnQ8wCotdOSU2w-ofmgI8cyV2TZNr7ONKpN3d6r8ELVLn-qLjUZShXwsi1mDua_iuCdt6kJ7OVPO3MwSIb-iyA73eCFi_zIvMGXn9J2Ti9reQf0dtMK-ciaq5QRfEhhK7zDYDIH9D97yDNMgQWSleSjPTOrsxR9RmGBn0UqcVq-WFRVNIu3F8NNQ1q9pksVQ4xCdYKNT2_fOAJ2MOrq5ABZHZh5JLgLVUL_8VI1LX4tReA1iXbKZSWEtXEH6i8XLgadR_niQohWK3OQBWlcLLHSbP7Cf8yRoJw8erwjydpu8uSAjKNGtoc7fV1CyKzDAGKCa73nAJOLlpUrVTQrq7ASnWB5YQo0AhKhYYEg6MTthW03OB_WD-V5CxWILK1ltegRI/l34%3Ahttps%3A%2F%2Fgroups.google.com%2Fd%2Foptoute.
>

--
Université du Luxembourg
Faculté des Sciences, de la Technologie et de la Communication
Campus Belval, Biotech II 115
6 avenue du Swing
L-4367 Belvaux
Tel: (+352) 46 66 44 5309
Email: thoma...@uni.lu

Michael Hucka

unread,
Oct 23, 2018, 11:13:17 PM10/23/18
to sbml-inter...@googlegroups.com
Hi,

Yeah, this is a frustrating side-effect of using SWIG to create language
bindings for Python and other languages around the C++ base of libSBML.
We're currently discussing internally how to try to improve this
situation. Frank Bergmann has some ideas, and we'll explore further.

MH
> https://groups.google.com/d/msgid/sbml-interoperability/c3936ed4-b1b0-65a4-47e1-41fcbffa0893%40uni.lu.
Reply all
Reply to author
Forward
0 new messages