From 56ab243f6726052149d92913e93557b8e37d26f1 Mon Sep 17 00:00:00 2001 From: cliffckerr Date: Mon, 17 May 2021 23:02:22 -0700 Subject: [PATCH] update docs --- CHANGELOG.rst | 1 + covasim/analysis.py | 14 +++++++++++--- docs/tutorials/tut_calibration.ipynb | 3 +-- examples/t07_optuna_calibration.py | 3 +-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c721194d4..04a97ebef 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -29,6 +29,7 @@ Version 3.0.3 (2021-05-17) -------------------------- - Added a new class, ``cv.Calibration``, that can perform automatic calibration. Simplest usage is ``sim.calibrate(calib_pars)``. Note: this requires Optuna, which is not installed by default; please install separately via ``pip install optuna``. See the updated calibration tutorial for more information. - Added a new result, ``known_deaths``, which counts only deaths among people who have been diagnosed. +- Updated several vaccine and variant parameters (e.g., B1.351 and B117 cross-immunity). - ``sim.compute_fit()`` now returns the fit by default, and creates ``sim.fit`` (previously, this was stored in ``sim.results.fit``). - *Regression information*: Calls to ``sim.results.fit`` should be replaced with ``sim.fit``. The ``output`` parameter for ``sim.compute_fit()`` has been removed since it now always outputs the ``Fit`` object. - *GitHub info*: PR `1047 `__ diff --git a/covasim/analysis.py b/covasim/analysis.py index 37572c21a..624fea5f1 100644 --- a/covasim/analysis.py +++ b/covasim/analysis.py @@ -1288,6 +1288,13 @@ def __init__(self, sim, calib_pars=None, custom_fn=None, n_trials=None, n_worker self.custom_fn = custom_fn self.verbose = verbose self.calibrated = False + + # Handle if the sim has already been run + if self.sim.complete: + print('Warning: sim has already been run; re-initializing, but in future, use a sim that has not been run') + self.sim = self.sim.copy() + self.sim.initialize() + return @@ -1316,7 +1323,7 @@ def run_trial(self, trial): ''' Define the objective for Optuna ''' pars = {} for key, (best,low,high) in self.calib_pars.items(): - pars[key] = trial.suggest_uniform(key, low, high) # Sample from beta values within this range + pars[key] = trial.suggest_uniform(key, low, high) # Sample from values within this range mismatch = self.run_sim(pars) return mismatch @@ -1353,6 +1360,7 @@ def calibrate(self, calib_pars=None, verbose=True, **kwargs): Args: calib_pars (dict): if supplied, overwrite stored calib_pars + verbose (bool): whether to print output from each trial kwargs (dict): if supplied, overwrite stored run_args (n_trials, n_workers, etc.) ''' @@ -1368,8 +1376,8 @@ def calibrate(self, calib_pars=None, verbose=True, **kwargs): t0 = sc.tic() self.make_study() self.run_workers() - study = op.load_study(storage=self.run_args.storage, study_name=self.run_args.name) - self.best_pars = sc.objdict(study.best_params) + self.study = op.load_study(storage=self.run_args.storage, study_name=self.run_args.name) + self.best_pars = sc.objdict(self.study.best_params) self.elapsed = sc.toc(t0, output=True) # Compare the results diff --git a/docs/tutorials/tut_calibration.ipynb b/docs/tutorials/tut_calibration.ipynb index c43e1e734..e50f70e5c 100644 --- a/docs/tutorials/tut_calibration.ipynb +++ b/docs/tutorials/tut_calibration.ipynb @@ -131,7 +131,7 @@ " mismatch = np.mean(mismatches)\n", " return mismatch\n", "\n", - "guess = [0.01, 1] # Initial guess of parameters -- beta and relative death probability\n", + "guess = [0.015, 1] # Initial guess of parameters -- beta and relative death probability\n", "pars = scipy.optimize.minimize(objective, x0=guess, method='nelder-mead') # Run the optimization\n", "```" ] @@ -216,7 +216,6 @@ "outputs": [], "source": [ "# Plot the results\n", - "calib.summarize()\n", "calib.plot(to_plot=['cum_tests', 'cum_diagnoses', 'cum_deaths'])" ] }, diff --git a/examples/t07_optuna_calibration.py b/examples/t07_optuna_calibration.py index 6768272f5..82cc39979 100644 --- a/examples/t07_optuna_calibration.py +++ b/examples/t07_optuna_calibration.py @@ -30,6 +30,5 @@ n_workers = 4 calib = sim.calibrate(calib_pars=calib_pars, n_trials=n_trials, n_workers=n_workers) - # Summarize and plot the results - calib.summarize() + # Plot the results calib.plot(to_plot=['cum_tests', 'cum_diagnoses', 'cum_deaths']) \ No newline at end of file