|
1 | 1 | # swarm_it
|
2 |
| -Utility to help setup PSO runs for PySB models. |
| 2 | + |
| 3 | + |
| 4 | +[](LICENSE) |
| 5 | + |
| 6 | +[](https://github.com/LoLab-VU/swarm_it/releases/tag/v0.1.0) |
| 7 | + |
| 8 | +**swarm_it** is a python utility designed to abstract away some of the effort in setting up Particle Swarm Optimization (PSO)-based calibrations of biological models in the [PySB](http://pysb.org/) format using [simplePSO](https://github.com/LoLab-VU/ParticleSwarmOptimization). |
| 9 | + |
| 10 | +------ |
| 11 | + |
| 12 | +# Install |
| 13 | + |
| 14 | +| **! Warning** | |
| 15 | +| :--- | |
| 16 | +| swarm_it is still under heavy development and may rapidly change. | |
| 17 | + |
| 18 | +**swarm_it** installs as the `swarm_it` package. It is compatible (i.e., tested) with Python 3.6. |
| 19 | + |
| 20 | +Note that `swarm_it` has the following core dependencies: |
| 21 | + * [NumPy](http://www.numpy.org/) |
| 22 | + * [SciPy](https://www.scipy.org/) |
| 23 | + * [simplePSO](https://github.com/LoLab-VU/ParticleSwarmOptimization) version 1.0 |
| 24 | + * [PySB](http://pysb.org/) |
| 25 | + |
| 26 | +### pip install |
| 27 | +You can install the latest release of the `swarm_it` package using `pip` sourced from the GitHub repo: |
| 28 | +``` |
| 29 | +pip install -e git+https://github.com/LoLab-VU/[email protected]#egg=swarm_it |
| 30 | +``` |
| 31 | +However, this will not automatically install the core dependencies. You will have to do that separately: |
| 32 | +``` |
| 33 | +pip install numpy scipy simplepso |
| 34 | +``` |
| 35 | +See the [http://pysb.org/download](http://pysb.org/download) page for instructions on installing PySB. |
| 36 | + |
| 37 | +------ |
| 38 | + |
| 39 | +# License |
| 40 | + |
| 41 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details |
| 42 | + |
| 43 | +------ |
| 44 | + |
| 45 | +# Documentation and Usage |
| 46 | + |
| 47 | +### Quick Overview |
| 48 | +swarm_it is a utility that helps generate a simplePSO run script or simplepso.pso.PSO objects for a PySB model. |
| 49 | + |
| 50 | +#### Commmand line use |
| 51 | +swarm_it can be used as a command line utility to generate a template Particle Swarm Optimization run script for a PySB model. swarm_it reads the model file, imports and pulls out all the kinetic parameters, and then writes out a simplepso run script for that model. Users can then edit edit the generated template script to load any data and modify the cost function. |
| 52 | + |
| 53 | +Run swarm_it from the command line with following format: |
| 54 | +``` |
| 55 | +python -m swarm_it model.py output_path |
| 56 | +``` |
| 57 | +where output_path is the directory/folder location where you want the generated script to be saved as "simplepso_model_name.py". |
| 58 | + |
| 59 | +The command line version of swarm_it also has support for setting the calibrated model parameters via an instance of [SwarmParam](https://github.com/LoLab-VU/swarm_it/tree/master#swarmparam) defined along with the model. |
| 60 | + |
| 61 | + |
| 62 | +#### Progammatic use via the SwarmIt class |
| 63 | +The swarm_it utility can be used progammatically via the SwarmIt |
| 64 | +class. It's importable from swarm_it: |
| 65 | +```python |
| 66 | +from swarm_it import SwarmIt |
| 67 | +``` |
| 68 | +The SwarmIt class can build an instance of a simplepso.pso.PSO object. |
| 69 | + Here's a faux minimal example: |
| 70 | +```python |
| 71 | +from my_pysb_model import model as my_model |
| 72 | +from swarm_it import SwarmIt |
| 73 | +import numpy as np |
| 74 | + |
| 75 | +timespan = np.linspace(0., 10., 10) |
| 76 | +data = np.load('my_data.npy') |
| 77 | +data_sd = np.load('my_data_sd.npy') |
| 78 | +observable_data = dict() |
| 79 | +time_idxs = list(range(len(timespan))) |
| 80 | +observable_data['my_observable'] = (data, data_sd, time_idxs) |
| 81 | +# Initialize the SwarmIt instance with the model details. |
| 82 | +swarmit = SwarmIt(my_model, observable_data, timespan) |
| 83 | +# Now build the PSO object. -- All inputs are |
| 84 | +# optional keyword arguments. |
| 85 | +pso = swarmit(pso_kwargs=dict({'save_sampled':False, 'verbose':True}), |
| 86 | + cost_type='norm_logpdf') |
| 87 | +# Then you can run it. |
| 88 | +num_particles = 20 |
| 89 | +num_iterations = 50 |
| 90 | +pso.run(num_particles, num_iterations) |
| 91 | +print("best_theta: ",pso.best) |
| 92 | +``` |
| 93 | + |
| 94 | +SwarmIt constructs the PSO object to sample all of a model's kinetic rate parameters. It assumes that the priors are uniform with size 4 orders of magnitude and centered on the values defined in the model. |
| 95 | + |
| 96 | +In addition, SwarmIt crrently has three pre-defined loglikelihood functions with different estimators, specified with the keyword parameter cost_type: |
| 97 | +```python |
| 98 | +# Now build the PSO object. |
| 99 | +pso = swarmit(cost_type='mse') |
| 100 | +``` |
| 101 | +The options are |
| 102 | + * 'norm_logpdf'=> (default) Compute the cost a the negative sum of normal distribution logpdf's over each data point |
| 103 | + * 'mse'=>Compute the cost using the mean squared error estimator |
| 104 | + * 'sse'=>Compute the cost using the sum of squared errors estimator. |
| 105 | + |
| 106 | +Each of these functions computes the cost estimate using the timecourse output of a model simulation for each observable defined in the `observable_data` dictionary. |
| 107 | +If you want to use a different or more complicated likelihood function with SwarmIt then you'll need to subclass it and either override one of the existing cost functions or define a new one. |
| 108 | + |
| 109 | +#### SwarmParam |
| 110 | +The swarm_it module has a built-in helper class, SwarmParam, which can be used in conjunction of with SwarmIt class. SwarmParam can be used at the level of PySB model definition to log which parameters to include in |
| 111 | +a Particle Swarm Optimization run, or it can be used after model definition at as long as it is defined before defining a SwarmIt object. It can be imported from swarm_it: |
| 112 | +```python |
| 113 | +from swarm_it import SwarmParam |
| 114 | +``` |
| 115 | +It is passed at instantiation to the SwarmIt class, which uses it |
| 116 | +to build the set of parameters to use, their staring PSO position and bounds, as well as the parameter mask for the cost function. |
| 117 | + |
| 118 | +Note that if you flag a parameter for sampling without setting sampling bounds, SwarmParam will by default assign the parameter bounds centered on the parameter's value (as defined in the model) with a width of 4 orders of magnitude. |
| 119 | + |
| 120 | +### Examples |
| 121 | +Additional example scripts that show how to setup and launch PSO runs using **swarm_it** can be found under [examples](./examples). |
| 122 | + |
| 123 | +------ |
| 124 | + |
| 125 | +# Contact |
| 126 | + |
| 127 | +To report problems or bugs please open a |
| 128 | +[GitHub Issue](https://github.com/LoLab-VU/swarm_it/issues). Additionally, any |
| 129 | +comments, suggestions, or feature requests for **swarm_it** can also be submitted as a |
| 130 | +[GitHub Issue](https://github.com/LoLab-VU/swarm_it/issues). |
| 131 | + |
| 132 | +------ |
| 133 | + |
| 134 | +# Citing |
| 135 | + |
| 136 | +If you use the **swarm_it** software in your research, please cite the GitHub repo. |
| 137 | + |
| 138 | +Also, please cite the following references as appropriate for software used with/via **swarm_it**: |
| 139 | + |
| 140 | +#### Packages from the SciPy ecosystem |
| 141 | + |
| 142 | +This includes NumPy and SciPy for which references can be obtained from: |
| 143 | +https://www.scipy.org/citing.html |
| 144 | + |
| 145 | +#### simplePSO |
| 146 | +The simplePSO reference can be exported from its Zenodo DOI entry: |
| 147 | +[10.5281/zenodo.2612912](https://doi.org/10.5281/zenodo.2612912) |
| 148 | + |
| 149 | +#### PySB |
| 150 | + 1. Lopez, C. F., Muhlich, J. L., Bachman, J. A. & Sorger, P. K. Programming biological models in Python using PySB. Mol Syst Biol 9, (2013). doi:[10.1038/msb.2013.1](dx.doi.org/10.1038/msb.2013.1) |
0 commit comments