Skip to content
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

Use python dict to support bngl hash type arguments in console actions #539

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ortega2247
Copy link
Contributor

@ortega2247 ortega2247 commented Apr 14, 2021

This adds support to use bngl hash type arguments like max_stoich that are passed to generate_network. I use f-strings because i feel it is easier to read, but most of the code in pysb uses the old string formatting (% operator). Comments, feedback, and requests are more than welcome

from pysb.examples.bax_pore import model
from pysb.bng import generate_equations

generate_equations(model, **{'max_stoich':{"BAX":1}})
model.species

That caps the number of BAX molecules in a complex to 1

Copy link
Member

@alubbock alubbock left a comment

Choose a reason for hiding this comment

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

Looks good - please could you add a small test for this to the BNG test suite? Similar to the example you give on the PR. Thanks!

@ortega2247
Copy link
Contributor Author

Just added the requested test. Also, I change the exporters so that the generate_equation is only called when the model has not generated the equations before. I think that can be good for large models that take a long time to generate equations and so we don't have to generate equations again if we want to export the model to some other format. This also facilitates the export of models' equations that has been generated using arguments like max_stoich

pysb/export/json.py Outdated Show resolved Hide resolved
Copy link
Member

@alubbock alubbock left a comment

Choose a reason for hiding this comment

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

Thanks @ortega2247. I've made a few small suggestions in the inline comments.

pysb/bng.py Outdated Show resolved Hide resolved
pysb/bng.py Outdated Show resolved Hide resolved
pysb/tests/test_bng.py Outdated Show resolved Hide resolved
for k, v in kwargs.items():
bng_param = BngConsole._bng_param(v)
if isinstance(bng_param, collections.Mapping):
par_string = f'{k}' + '=>{' + ','.join(f"{k}=>{v}" for k, v in bng_param.items()) + '}'
Copy link
Member

Choose a reason for hiding this comment

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

This is unwieldy and not really using the benefit of f-strings. Also re-using the local v variable is a bit confusing. How about something like this (not tested) :

args_string = ",".join(f"{k}=>{v}" for k, v in bng_param.items())
par_string = f"{k}=>{args_string}"

for k, v in kwargs.items():
bng_param = BngConsole._bng_param(v)
if isinstance(bng_param, collections.Mapping):
par_string = f'{k}' + '=>{' + ','.join(f"{k}=>{v}" for k, v in bng_param.items()) + '}'
Copy link
Member

Choose a reason for hiding this comment

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

Should we perform some validation on the kwargs keys and values here to make sure we don't emit invalid BNGL? If the BNG parser fails due to invalid BNGL here the error will probably be hard for the user to make sense of. The keys should be validated against the regex that matches whatever BNG accepts there, but I'm not sure what the value syntax is.

@codecov-commenter
Copy link

codecov-commenter commented Jun 21, 2021

Codecov Report

Merging #539 (9a7586f) into master (d58da94) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #539      +/-   ##
==========================================
+ Coverage   80.20%   80.25%   +0.05%     
==========================================
  Files         101      101              
  Lines       10661    10678      +17     
==========================================
+ Hits         8551     8570      +19     
+ Misses       2110     2108       -2     
Impacted Files Coverage Δ
pysb/bng.py 93.10% <100.00%> (+0.11%) ⬆️
pysb/tests/test_bng.py 100.00% <100.00%> (ø)
pysb/generator/bng.py 97.21% <0.00%> (+0.79%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d58da94...9a7586f. Read the comment docs.

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

Successfully merging this pull request may close these issues.

None yet

4 participants