Skip to content

Commit

Permalink
Merge pull request #380 from starsimhub/add-demo
Browse files Browse the repository at this point in the history
Add demo
  • Loading branch information
cliffckerr authored Mar 13, 2024
2 parents 912991f + 6387819 commit 54196a4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ All notable changes to the codebase are documented in this file. Changes that ma
:depth: 1


Version 0.2.8 (2024-03-13)
--------------------------
- Add ``ss.demo()`` to quickly create a default simulation.
- *GitHub info*: PR `380 <https://github.com/amath-idm/stisim/pull/380>`_


Version 0.2.7 (2024-03-09)
--------------------------
- Update ``StaticNet`` with defaults and correct argument passing
Expand Down
34 changes: 32 additions & 2 deletions starsim/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numba as nb


__all__ = ['Sim', 'AlreadyRunError', 'diff_sims']
__all__ = ['Sim', 'AlreadyRunError', 'demo', 'diff_sims']


@nb.njit(cache=True)
Expand Down Expand Up @@ -651,7 +651,7 @@ def get_result(res, func):
summary[key] = entry
self.summary = summary
return summary

def shrink(self, skip_attrs=None, in_place=True):
"""
"Shrinks" the simulation by removing the people and other memory-intensive
Expand Down Expand Up @@ -961,6 +961,36 @@ class AlreadyRunError(RuntimeError):
pass


def demo(run=True, plot=True, summary=True, **kwargs):
"""
Create a simple demo simulation for Starsim
Defaults to using the SIR model with a random network, but these can be configured.
Args:
run (bool): whether to run the sim
plot (bool): whether to plot the results
summary (bool): whether to print a summary of the results
kwargs (dict): passed to ``ss.Sim()``
**Examples**::
ss.demo() # Run, plot, and show results
ss.demo(diseases='hiv', networks='mf') # Run with different defaults
"""
kw = sc.mergedicts(dict(pars=dict(diseases='sir', networks='random')), kwargs)
sim = Sim(**kw)
if run:
sc.heading('Running demo:')
sim.run()
if summary:
sc.heading('Results:')
print(sim.summary)
if plot:
sim.plot()
return sim


def diff_sims(sim1, sim2, skip_key_diffs=False, skip=None, full=False, output=False, die=False):
'''
Compute the difference of the summaries of two simulations, and print any
Expand Down
4 changes: 2 additions & 2 deletions starsim/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

__all__ = ['__version__', '__versiondate__', '__license__']

__version__ = '0.2.7'
__versiondate__ = '2024-03-09'
__version__ = '0.2.8'
__versiondate__ = '2024-03-13'
__license__ = f'Starsim {__version__} ({__versiondate__}) — © 2023-2024 by IDM'
7 changes: 7 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
sc.options(interactive=False) # Assume not running interactively


def test_demo():
""" Test Starsim's demo run """
sim = ss.demo()
return sim


def test_default():
""" Create, run, and plot a sim with default settings """
sim = ss.Sim(n_agents=n_agents).run()
Expand Down Expand Up @@ -146,6 +152,7 @@ def test_parallel():
sc.options(interactive=do_plot)
T = sc.timer()

s0 = test_demo()
s1 = test_default()
s2 = test_simple()
s3a, s3b = test_sir_epi()
Expand Down

0 comments on commit 54196a4

Please sign in to comment.