Skip to content

Commit

Permalink
merged with master
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffckerr committed Mar 18, 2020
2 parents 770969e + 135a21d commit fa1d255
Show file tree
Hide file tree
Showing 21 changed files with 683 additions and 260 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ubuntu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Covasim CI workflow
on: [push, pull_request]

jobs:
install_and_test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
max-parallel: 3
matrix:
python-version: [ '3.7' ]
name: Install and test
steps:
- name: Checkout sources
uses: actions/checkout@v1
- uses: actions/setup-python@master
with:
python-version: ${{ matrix.python-version }}
architecture: x64
- name: Install Covasim
run: python setup.py develop
- name: Run tests
working-directory: ./tests
run: |
pip install pytest
pytest test_* unittest_* --junitxml=test-results.xml
- name: Upload test results
uses: actions/upload-artifact@v1
if: failure()
with:
name: test_results
path: tests/test-results.xml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Custom
covasim/cova_cdc/results_2020mar14/
covasim/cova_oregon/received/
covasim/cova_oregon/results_2020mar14/
covasim/cova_oregon/results_2020mar15/
covasim/cova_seattle/capacity_analysis
covasim/cova_seattle/school_analysis
covasim/cova_seattle/seattle_analysis
Expand Down
22 changes: 2 additions & 20 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
MIT License
COVASim is licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 License.

Copyright (c) 2020 Institute for Disease Modeling

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
78 changes: 46 additions & 32 deletions covasim/cova_base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,6 @@ def likelihood(self):
def plot(self):
'''
Plot the results -- can supply arguments for both the figure and the plots.
Parameters
----------
do_save : bool or str
Whether or not to save the figure. If a string, save to that filename.
fig_args : dict
Dictionary of kwargs to be passed to pl.figure()
plot_args : dict
Dictionary of kwargs to be passed to pl.plot()
as_days : bool
Whether to plot the x-axis as days or time points
Returns
-------
Figure handle
'''
raise NotImplementedError

Expand All @@ -172,16 +154,19 @@ def plot_people(self):
raise NotImplementedError


def single_run(sim=None, ind=0, noise=0.0, noisepar=None, verbose=None, **kwargs):
def single_run(sim=None, ind=0, noise=0.0, noisepar=None, verbose=None, sim_args=None, **kwargs):
'''
Convenience function to perform a single simulation run. Mostly used for
parallelization, but can also be used directly:
import covid_abm
sim = covid_abm.single_run() # Create and run a default simulation
'''

if sim_args is None:
sim_args = {}

if sim is None:
new_sim = Sim(**kwargs)
new_sim = Sim(**sim_args)
else:
new_sim = sc.dcp(sim) # To avoid overwriting it; otherwise, use

Expand All @@ -191,44 +176,73 @@ def single_run(sim=None, ind=0, noise=0.0, noisepar=None, verbose=None, **kwargs
new_sim['seed'] += ind # Reset the seed, otherwise no point of parallel runs
new_sim.set_seed()

# If the noise parameter is not found, guess what it should be
if noisepar is None:
guesses = ['r_contact', 'r0', 'beta']
found = [guess for guess in guesses if guess in sim.pars.keys()]
if len(found)!=1:
raise KeyError(f'Cound not guess noise parameter since out of {guesses}, {found} were found')
else:
noisepar = found[0]

# Handle noise -- normally distributed fractional error
noiseval = noise*np.random.normal()
if noiseval > 0:
noisefactor = 1 + noiseval
else:
noisefactor = 1/(1-noiseval)
new_sim[noisepar] *= noisefactor

if verbose>=1:
print(f'Running a simulation using {new_sim["seed"]} seed and {noisefactor} noise')

new_sim[noisepar] *= noisefactor
# Handle additional arguments
for key,val in kwargs.items():
print(f'Processing {key}:{val}')
if key in new_sim.pars.keys():
if verbose>=1:
print(f'Setting key {key} from {new_sim[key]} to {val}')
new_sim[key] = val
pass
else:
raise KeyError(f'Could not set key {key}: not a valid parameter name')

# Run
new_sim.run(verbose=verbose)

return new_sim


def multi_run(sim=None, n=4, noise=0.0, noisepar=None, verbose=None, combine=False, **kwargs):
def multi_run(sim=None, n=4, noise=0.0, noisepar=None, iterpars=None, verbose=None, sim_args=None, combine=False, **kwargs):
'''
For running multiple runs in parallel. Example:
import covid_seattle
sim = covid_seattle.Sim()
sims = covid_seattle.multi_run(sim, n=6, noise=0.2)
'''

# Create the sims
if sim_args is None:
sim_args = {}
if sim is None:
sim = Sim(**kwargs)
sim = Sim(**sim_args)

# If the noise parameter is not found, guess what it should be
if noisepar is None:
guesses = ['r_contact', 'r0', 'beta']
found = [guess for guess in guesses if guess in sim.pars.keys()]
if len(found)!=1:
raise KeyError(f'Cound not guess noise parameter since out of {guesses}, {found} were found')
else:
noisepar = found[0]
# Handle iterpars
if iterpars is None:
iterpars = {}
else:
n = None # Reset and get from length of dict instead
for key,val in iterpars.items():
new_n = len(val)
if n is not None and new_n != n:
raise ValueError(f'Each entry in iterpars must have the same length, not {n} and {len(val)}')
else:
n = new_n

# Copy the simulations
iterkwargs = {'ind':np.arange(n)}
kwargs = {'sim':sim, 'noise':noise, 'noisepar':noisepar, 'verbose':verbose}
iterkwargs.update(iterpars)
kwargs = {'sim':sim, 'noise':noise, 'noisepar':noisepar, 'verbose':verbose, 'sim_args':sim_args}
sims = sc.parallelize(single_run, iterkwargs=iterkwargs, kwargs=kwargs)

if not combine:
Expand Down
4 changes: 2 additions & 2 deletions covasim/cova_base/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ['__version__', '__versiondate__']

__version__ = '0.4'
__versiondate__ = '2020-03-12'
__version__ = '0.5'
__versiondate__ = '2020-03-15'
Loading

0 comments on commit fa1d255

Please sign in to comment.