RuleWorld / bionetgen

Rule-based modeling framework
https://bionetgen.org/
MIT License
56 stars 25 forks source link

Preventing print-outs when simulating a bionetgen model #241

Closed TorkelE closed 2 years ago

TorkelE commented 2 years ago

Hello, I am simulating BioNetGen models in python. The workflow is something like:

import bionetgen
model = bionetgen.bngmodel('model.bngl')
result = bionetgen.run(model);

The "model.bngl" file basically declares the model and then runs a simulate({method=>"ode",t_end=>10,n_steps=>1000}).

Each time I simulate it I get a print-out like:

BioNetGen version 2.7.0
Reading from file /tmp/tmphkgb6zd0/multistate.bngl (level 0)
Read 9 parameters.
Read 3 molecule types.
Read 3 species.
Read 4 observable(s).
Read 4 reaction rule(s).
ACTION: simulate( method=>"cvode" )
ACTION: generate_network( multistate )
Iteration   0:     3 species      0 rxns  0.00e+00 CPU s
Iteration   1:     5 species      2 rxns  0.00e+00 CPU s
Iteration   2:     6 species      6 rxns  0.00e+00 CPU s
Iteration   3:     7 species      9 rxns  0.00e+00 CPU s
Iteration   4:     9 species     12 rxns  0.00e+00 CPU s
Iteration   5:     9 species     18 rxns  1.00e-02 CPU s
Cumulative CPU time for each rule
Rule   1:     6 reactions 1.00e-02 CPU s 1.67e-03 CPU s/rxn
Rule   2:     8 reactions 0.00e+00 CPU s 0.00e+00 CPU s/rxn
Rule   3:     1 reactions 0.00e+00 CPU s 0.00e+00 CPU s/rxn
Rule   4:     3 reactions 0.00e+00 CPU s 0.00e+00 CPU s/rxn
Total   :    18 reactions 1.00e-02 CPU s 5.56e-04 CPU s/rxn
Wrote network in net format to ./multistate.net.
Network simulation using cvode
WARNING: writeFile(): Overwriting existing file ./multistate.net.
Wrote network in net format to ./multistate.net.
Running run_network on slpc385
full command: /home/torkelloman/anaconda3/lib/python3.7/site-packages/bionetgen/bng-linux/bin/run_network -o ./multistate -p cvode -a 1e-08 -r 1e-08 --cdat 1 --fdat 0 -g ./multistate.net ./multistate.net 0.01 1000
[simulation PID is: 20404]
run_network 3.0
Read 9 parameters from ./multistate.net
Read 9 species from ./multistate.net
Read 4 group(s) from ./multistate.net
Read 0 function(s) from ./multistate.net
Read 18 reaction(s) from ./multistate.net
18 reaction(s) have nonzero rate
Initialization took 0.00 CPU seconds
Propagating with cvode using dense LU
Time course of concentrations written to file ./multistate.cdat.
Time course of groups written to file ./multistate.gdat.
Propagation took 0.00e+00 CPU seconds
Program times:  0.00 CPU s 0.00 clock s
Updating species concentrations from ./multistate.cdat
CPU TIME: simulate 0.02 s.
Finished processing file /tmp/tmphkgb6zd0/multistate.bngl.
CPU TIME: total 0.02 s.

when I make several simulations, this generates a lot of text, clogging up my jupyter notebook. Is there some way to prevent this from being generated?

ASinanSaglam commented 2 years ago

Hi,

First, it's not too important but you probably want to post any future python related issues on PyBioNetGen repo instead.

Second, yes there is a way to suppress the output. The documentation is lagging behind development, sorry about the delay. Try doing:

r = bionetgen.run('model.bngl', suppress=True)

run method currently accepts 1 arg, 3 kwargs input, output=None, suppress=False and timeout=None where timeout is in seconds.

Please let me know if it works for you, I'll close the issue after if it does.

TorkelE commented 2 years ago

Thanks, when I add the supress kwarg the run errors. I do

import bionetgen
model = bionetgen.bngmodel('model.bngl')

if I do

result = bionetgen.run(model);

everything is fine, however,

result = bionetgen.run(model, suppress=True);

gives this error:

BioNetGen version 2.7.1
Reading from file /tmp/tmpgcizzo9z/multistate.bngl (level 0)
Read 9 parameters.
Read 3 molecule types.
Read 3 species.
Read 4 reaction rule(s).
Read 4 observable(s).
Wrote model in xml format to ./multistate.xml.
Finished processing file /tmp/tmpgcizzo9z/multistate.bngl.
CPU TIME: total 0.01 s.
Couldn't run the simulation
'NoneType' object has no attribute 'decode'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<timed exec> in <module>

~/anaconda3/lib/python3.7/site-packages/bionetgen/modelapi/runner.py in run(inp, out, suppress, timeout)
     31             cli = BNGCLI(inp, out, conf["bngpath"], suppress=suppress, timeout=timeout)
     32             try:
---> 33                 cli.run()
     34                 os.chdir(cur_dir)
     35             except Exception as e:

~/anaconda3/lib/python3.7/site-packages/bionetgen/tools/cli.py in run(self)
    138             raise RuntimeError(
    139                 "Failed to run your BNGL file, there might be an issue with your model!",
--> 140                 out.stdout.decode("UTF-8"),
    141                 out.stderr.decode("UTF-8"),
    142             )

AttributeError: 'NoneType' object has no attribute 'decode'

(also, thanks for pointing me to https://github.com/RuleWorld/PyBioNetGen/issues, will use that in the future)

ASinanSaglam commented 2 years ago

I'm looking into it now, can you tell me which OS you are using this on? Also can you run bionetgen info in the command line and give me the output?

ASinanSaglam commented 2 years ago

Actually, I figured out the issue, there will be a new PyBioNetGen version with the patch in 10-15 minutes.

ASinanSaglam commented 2 years ago

OK, PyBioNetGen version 0.7.0 is out, Try updating with pip install bionetgen --upgrade and if that doesn't work try pip install bionetgen==0.7.0. Please let me know if that fixes your problem.

TorkelE commented 2 years ago

That worked! Thanks a lot for the quick help :)

ASinanSaglam commented 2 years ago

I'm glad that's solved and that you for posting it as an issue 👍