by

## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Oscar Ortega
@ortega2247

Hi,

You can use any of the scipy distributions (https://docs.scipy.org/doc/scipy/reference/stats.html) as a prior in pydream.

To use a uniform and a gaussian distribution you can do something like this:

from scipy.stats import norm, uniform

par1 = SampledParam(uniform, loc=lower_limits, scale=scale)
par2=SampledParam(norm,  loc=mean, scale=std)

sampled_parameters = [par1, par2

saubhagya-gatech
@saubhagya-gatech
oh ok. This makes so much sense. Essentially, we have to give a list of scion distributions. Can we give distributions other than uniform or normal? Like some fitted kernel?
something like this:
Oscar Ortega
@ortega2247
Interesting. I have never done anything like that but in theory it should be doable. Check this example from StackOverFlow to create a new scipy distribution. You just have to make sure that your distribution has a rvs and interval methods
Oscar Ortega
@ortega2247
saubhagya-gatech
@saubhagya-gatech
I see. So, I can give any distribution that is definable in Scipy. That's great.
pietromicheli
@pietromicheli
Hi all, i'm new in the forum.
I have a very quick question: is it somehow possible to pass as input to the simulation a list of values (one for each time point) which is meant to describe how a Parameter (or Expression) entity must vary during the simulation?
Oscar Ortega
@ortega2247

Hi @pietromicheli, if your parameters change over time as a function of the concentration of one or multiples species in your model, you can create an Expression and pass it as rules' rates. For doing something like this, you can check this example:

https://github.com/pysb/pysb/blob/master/pysb/examples/expression_observables.py

If you just want to pass a list parameter values to be used at different time points, I am not aware that there is function like that in pysb. However, you could simulate the first time points with the parameters that you want and then used the simulated results as the initial conditions of the next simulation that has different parameter values. For an example like that, take a look at this function:

https://github.com/LoLab-VU/pydyno/blob/9b93be55f5e6159cc98847e41d1e44290f4dcfcb/pydyno/tools/miscellaneous_analysis.py#L9

@alubbock might have some better ideas :)

Leonard Harris
@lh64
@pietromicheli Can you explain a bit more what you’re trying to do or provide a short example? There are piecewise linear functions that might work but without an example it’s a bit hard for us to determine what the best solution might be for your problem.
pietromicheli
@pietromicheli

Hi @ortega2247 and @lh64, thank you for the answers! :)

@lh64 you're definitely right, I apologize. I'm trying to model a post-synaptic neuron activity:
First, I simulate the gating of post-synaptic ion channels in a Pysb model. Then I use the trajectory of the open channels to calculate, for each time point, the quantity of Calcium ions that flow in the time unit. This post simulation calculation will create an array (of length equal to the time span array used for the first pysb simulation) that basically describes the time course of the post-synaptic calcium influx. What I'm trying to do now is to pass this array to a second Pysb model which contains some Calcium-dependent reactions. The goal here is to use the values of my array (one for each time step) to drive a synthesis-like reaction for a Calcium monomer that can be used by all the Calcium-dependent reactions. I really hope it's clear enough! :)

Thanks a lot @ortega2247 , your function seems super cool for creating a kind of discrete event during the simulation, but in this case I want that my parameter continuously change at each time step :)

saubhagya-gatech
@saubhagya-gatech
Good Morning Forum Members,
I am a PyDream user. I wanted to confirm that there is no strict requirement for the number of chains in Dream(ZS), right? For 19 parameters, can I use 5 chains? For MCMC-DE, we had to use a minimum of D/2 chains where D is the dimension of parameter space. I read that Dream(ZS) does not have any such requirement. Just wanted to double-check.
Thanks
Rodrigo Santibáñez
@glucksfall
Hello everyone. I hope you're well. Is there a way to delete a Rule from the model?
Leonard Harris
@lh64
@glucksfall There's not a simple function for deleting a Rule but you can try manually recreating the model.rules object by excluding the Rule(s) you don't want, using model.reset_equations to clear out the reactions, species, etc. created by BNG, and then regenerating the network. Here's a small example script I put together doing that:
Rodrigo Santibáñez
@glucksfall
Thank you @lh64.
saubhagya-gatech
@saubhagya-gatech

Hello everyone,

I was wondering if it is possible to provide multivariate distribution as prior instead of independent priors for parameters in PyDream?

Oscar Ortega
@ortega2247
Hi @saubhagya-gatech , sorry for the late reply. Dream(ZS) has a minimum number of chains requirement of 3. Unfortunately, pydream doesn't support the usage of multivariate distributions as a prior
saubhagya-gatech
@saubhagya-gatech
@ortega2247 Thank you!
Leonard Harris
@lh64
@pietromicheli You should be able to accomplish what you want using the initials or param_values arguments to the run method of the simulator. I'm attaching an example here. I defined two simple rules: X() >> None and A() + X() >> X(). In the code, I first run a simulation of the full system. Then, I run two sequential simulations, the first with only the X() >> None rule and the second with only the A() + X() >> X() rule, where the concentration of X() from the first simulation is fed into the simulator for the second simulation using the initials argument to ScipyOdeSimulator.run. To be clear, the second "simulation" is actually a series of simulations from output time point to output time point, with the final concentration of A() from the previous step and the corresponding value of X() passed into the simulator at each step. I have it loop over three different numbers of output points (10, 100, and 1000) to show how the output of the sequential simulation approaches the true result from the full simulation as the number of output points increases. I'm attaching those plots here as well. Hopefully this helps. If you have any further questions just let us know.
pietromicheli
@pietromicheli

@lh64 Thank you for the answer!

Actually my first approach to this problem was to iteratively run one simulation for each time point just as you suggested, but I wasn't very happy with the computational cost of a such sequential simulation (expecially for a high number of time points). However looking at your code I noticed that (stupidly) I created at each iteration a new simulator object (via ScipyOdeSimulator() function). So it turned out that this unnecessary step tripled the cost of each iteration, compared to the cost that runinning the simulations (via .run() method) using always the same simulator object at each iteration would require. The overall cost is still quite high obviously, but for reasonably small number of time points it is tollerable.

saubhagya-gatech
@saubhagya-gatech
Hello @ortega2247
Was there any change made in the PyDream in the last one month? I installed the package again and the "start" argument is not working. It does not matter what chain initialization I give, the chains are starting at random positions. Any help here would be appreciated. Before I reinstalled PyDream yesterday, I did not have this issue.
saubhagya-gatech
@saubhagya-gatech
Here is the output file. I have printed the list of starting positions of 5 chains, and I also print the parameters received my likelihood function as "proposed parameter values". The first step has totally different proposed values then what is specified in the "start". I think this might be a bug.
Oscar Ortega
@ortega2247
Hi @saubhagya-gatech , I am sorry PyDREAM is having issues. PyDREAM hasn't been updated since March 25 so I am not sure why this is happening. If you used pip to install pydream you can check the version installed by typing: pip show pydream in the terminal. The version should be 2.0.0
If you send me a reproducible example, that would help me to find where the problem is
saubhagya-gatech
@saubhagya-gatech
Thank you Oscar. I was looking at Github and I see pull request accepted on May 14th which had some changes relevant to the start argument. Is "pip install" currently installing version with these changes?
https://github.com/LoLab-VU/PyDREAM/pull/20/files/384866c232f9a16b8da4a7ececea62d665eaf644
I will prepare a reproducible example. I actually manually installed March 25 version using the source code and that is not showing the mentioned issue
saubhagya-gatech
@saubhagya-gatech
I did some more numerical tests and found that if I used this version:( https://github.com/LoLab-VU/PyDREAM/releases/tag/v2.0.0) the start argument is working, but if I use the version installed from "pip install pydream", the start argument is not working. Give me few minute to send an example
Oscar Ortega
@ortega2247
mmm so strange. pip install should be installing this release: https://github.com/LoLab-VU/PyDREAM/releases/tag/v2.0.0 unless you are using python 2
saubhagya-gatech
@saubhagya-gatech
ah ok. I am currently double-checking my python versions and package locations. I will get back to you either today or tomorrow if I figure out what is happening.
On a different note, the version with acceptance-ratio correction is not released yet, right?
Oscar Ortega
@ortega2247
that's correct
i should be releasing it soon
Oscar Ortega
@ortega2247
it fixes the reporting of acceptance rates when one restarts a pydream run. Before this fix, we observed that the acceptance rates were dropping when restarting because the number of accepted points where reset to zero instead of keeping the history of the number of accepted points in the previous run
saubhagya-gatech
@saubhagya-gatech
That makes sense. Thank you. Here is a reproducible example. After running the mcmc_run.py, you can run reading_results.py to print the first step that chains actually took. This is coming out to be different from the first step defined in "start" which is also printed in the python outputs. I could not think of any reason for this. Any help would be appreciated.
^ it might not be related to versions and might not even be an "issue". I am just trying to understand what is happening.
opurcell
@opurcell

Hi, I am having some ongoing trouble with the SBML exporter. Specifically the line 'sbml_output = export(model, 'sbml'). I never had problems before and had been using PySB to build and then export my models for some time, but after starting a new venv (under PyCharm) and I assume then using the latest package versions I started having issues. I solved these by just returning to the exact package versions for everything that I had, which fixed it. However now I am moving from a local instance to AWS, I am seeing the same issues and so far have not been able to solve them with package versions. The initial error is:

Traceback (most recent call last):
File "predict_KOs.py", line 605, in <module>
main()
File "predict_KOs.py", line 591, in main
mode_1_results = run_mode_1()
File "predict_KOs.py", line 375, in run_mode_1
model = build.build_model(networks[network])
File "/home/ubuntu/build.py", line 1177, in build_model
sbml_output = export(model, 'sbml')
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/init.py", line 176, in export
return e.export()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.py", line 315, in export
return libsbml.writeSBMLToString(self.convert(level=level))
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.py", line 125, in convert
pysb.bng.generate_equations(self.model)
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 732, in generate_equations
verbose=verbose, **kwargs).split('\n'))
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 672, in generate_network
bngfile.execute()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 443, in execute
output += self.generator.get_content()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/generator/bng.py", line 26, in get_content
self.generate_content()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/generator/bng.py", line 31, in generate_content
self.generate_parameters()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/generator/bng.py", line 42, in generate_parameters
exprs = self.model.expressions_constant()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/core.py", line 1976, in expressions_constant
cset = ComponentSet(e for e in self.expressions
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/core.py", line 2255, in init
for value in iterable:
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/core.py", line 1977, in <genexpr>
if e.is_constant_expression())
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/core.py", line 1644, in is_constant_expression
for a in self.expr.atoms())
File "/home/ubuntu/.local/lib/python3.7/site-packages/sympy/core/basic.py", line 523, in atoms
result = {node for node in nodes if not node.args}
File "/home/ubuntu/.local/lib/python3.7/site-packages/sympy/core/basic.py", line 523, in <setcomp>
result = {node for node in nodes if not node.args}
AttributeError: 'str' object has no attribute 'args'

which is obtained using sympy==1.6.1. If I instead use sympy 1.5.1 (which is the version I had it working locally with), I get a an error about the BNG2.pl file, which I have confirmed is where it should be:

Traceback (most recent call last):
File "predict_KOs.py", line 605, in <module>
main()
File "predict_KOs.py", line 591, in main
mode_1_results = run_mode_1()
File "predict_KOs.py", line 375, in run_mode_1
model = build.build_model(networks[network])
File "/home/ubuntu/build.py", line 1177, in build_model
sbml_output = export(model, 'sbml')
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/init.py", line 176, in export
return e.export()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.py", line 315, in export
return libsbml.writeSBMLToString(self.convert(level=level))
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.

the end of the second error was missed off:

Traceback (most recent call last):
File "predict_KOs.py", line 605, in <module>
main()
File "predict_KOs.py", line 591, in main
mode_1_results = run_mode_1()
File "predict_KOs.py", line 375, in run_mode_1
model = build.build_model(networks[network])
File "/home/ubuntu/build.py", line 1177, in build_model
sbml_output = export(model, 'sbml')
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/init.py", line 176, in export
return e.export()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.py", line 315, in export
return libsbml.writeSBMLToString(self.convert(level=level))
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/export/sbml.py", line 125, in convert
pysb.bng.generate_equations(self.model)
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 732, in generate_equations
verbose=verbose, **kwargs).split('\n'))
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 672, in generate_network
bngfile.execute()
File "/home/ubuntu/.local/lib/python3.7/site-packages/pysb/bng.py", line 487, in execute
p_err.rstrip())
pysb.bng.BngInterfaceError:
Can't open perl script "BioNetGen-2.5.0/BNG2.pl": No such file or directory

Rodrigo Santibáñez
@glucksfall
use
from pysb.pathfinder import set_path
set_path('bng', path_to_BNG2.pl)
opurcell
@opurcell
Thanks, that fixed it. I'm curious tho - why is there the sympy package dependency? i.e. why does it work with 1.5.1, but not with 1.6.1?
Rodrigo Santibáñez
@glucksfall
I'm just a pySB user...
opurcell
@opurcell
I see. Well thanks for the fix. If anyone in the pySB dev community is able to shed any light on the dependency issue that would be great. Im using python 3.7 and pySB 1.11. BioNetGen 2-5-0.
Alex Lubbock
@alubbock
sympy 1.6.1 was only recently released. We've added support for it in the PySB master branch on Github, which will be included in the next PySB release.
If you install using an anaconda environment conda install -c alubbock pysb, the dependency versions should be pinned correctly (please let me know if not).
opurcell
@opurcell
OK thanks for the explanation. Good to know.