diff --git a/autumn/projects/sm_covid2/common_school/runner_tools.py b/autumn/projects/sm_covid2/common_school/runner_tools.py index 5a7f01c40..a87f302db 100644 --- a/autumn/projects/sm_covid2/common_school/runner_tools.py +++ b/autumn/projects/sm_covid2/common_school/runner_tools.py @@ -19,6 +19,7 @@ from estival.utils.parallel import map_parallel from autumn.core.runs import ManagedRun +from autumn.infrastructure.remote import springboard from autumn.settings.folders import PROJECTS_PATH from autumn.projects.sm_covid2.common_school.calibration import get_bcm_object @@ -37,6 +38,19 @@ with countries_path.open() as f: INCLUDED_COUNTRIES = yaml.unsafe_load(f) + +DEFAULT_RUN_CONFIG = { + "N_CHAINS": 8, + "N_OPTI_SEARCHES": 16, + "OPTI_BUDGET": 10000, + "METROPOLIS_TUNE": 5000, + "METROPOLIS_DRAWS": 30000, + "METROPOLIS_METHOD": "DEMetropolisZ", + "FULL_RUNS_SAMPLES": 1000, + "BURN_IN": 20000 +} + + """ Functions related to model calibration """ @@ -312,17 +326,14 @@ def get_quantile_outputs(outputs_df, diff_outputs_df, quantiles=[.025, .25, .5, def run_full_analysis( iso3, analysis="main", - opti_params={'n_searches': 8, 'n_best_retained': 4, 'num_workers': 8, 'parallel_opti_jobs': 4, 'warmup_iterations': 2000, 'search_iterations': 5000, 'init_method': "LHS"}, - mcmc_params={'draws': 10000, 'tune': 1000, 'cores': 32, 'chains': 32, 'method': 'DEMetropolis'}, - full_run_params={'samples': 1000, 'burn_in': 5000}, + opti_params={'n_searches': 8, 'search_iterations': 5000}, + mcmc_params={'draws': 30000, 'tune': 5000, 'chains': 8, 'method': 'DEMetropolisZ'}, + full_run_params={'samples': 1000, 'burn_in': 20000}, output_folder="test_outputs", logger=None ): out_path = Path(output_folder) - - # Check we requested enough searches and number of requested MCMC chains is a multiple of number of retained optimisation searches - assert opti_params['n_best_retained'] <= opti_params['n_searches'] - assert mcmc_params['chains'] % opti_params['n_best_retained'] == 0 + assert mcmc_params['chains'] <= opti_params['n_searches'] # Create BayesianCompartmentalModel object bcm = get_bcm_object(iso3, analysis) @@ -333,13 +344,8 @@ def run_full_analysis( # Sample optimisation starting points with LHS if logger: logger.info("Perform LHS sampling") - if opti_params['init_method'] == "LHS": - sample_as_dicts = sample_with_lhs(opti_params['n_searches'], bcm) - elif opti_params['init_method'] == "midpoint": - sample_as_dicts = [{}] * opti_params['n_searches'] - else: - raise ValueError('init_method optimisation argument not supported') - + sample_as_dicts = sample_with_lhs(opti_params['n_searches'], bcm) + # Store starting points with open(out_path / "LHS_init_points.yml", "w") as f: yaml.dump(sample_as_dicts, f) @@ -347,19 +353,19 @@ def run_full_analysis( # Perform optimisation searches if logger: logger.info(f"Perform optimisation ({opti_params['n_searches']} searches)") - + n_opti_workers = 8 def opti_func(sample_dict): - best_p, _ = optimise_model_fit(bcm, num_workers=opti_params['num_workers'], warmup_iterations=opti_params['warmup_iterations'], search_iterations=opti_params['search_iterations'], suggested_start=sample_dict) + best_p, _ = optimise_model_fit(bcm, num_workers=n_opti_workers, search_iterations=opti_params['search_iterations'], suggested_start=sample_dict) return best_p - best_params = map_parallel(opti_func, sample_as_dicts, n_workers=opti_params['parallel_opti_jobs']) + best_params = map_parallel(opti_func, sample_as_dicts, n_workers=2 * mcmc_params['chains'] / n_opti_workers) # oversubscribing # Store optimal solutions with open(out_path / "best_params.yml", "w") as f: yaml.dump(best_params, f) # Keep only n_best_retained best solutions loglikelihoods = [bcm.loglikelihood(**p) for p in best_params] - ll_cutoff = sorted(loglikelihoods, reverse=True)[opti_params['n_best_retained'] - 1] + ll_cutoff = sorted(loglikelihoods, reverse=True)[mcmc_params['chains'] - 1] retained_init_points, retained_best_params = [], [] for init_sample, best_p, ll in zip(sample_as_dicts, best_params, loglikelihoods): @@ -374,11 +380,11 @@ def opti_func(sample_dict): # Plot optimal solutions and starting points plot_opti_params(retained_init_points, retained_best_params, bcm, output_folder) - # Plot optimal model fits - opt_fits_path = out_path / "optimised_fits" - opt_fits_path.mkdir(exist_ok=True) - for j, best_p in enumerate(retained_best_params): - plot_model_fit(bcm, best_p, iso3, opt_fits_path / f"best_fit_{j}.png") + # # Plot optimal model fits + # opt_fits_path = out_path / "optimised_fits" + # opt_fits_path.mkdir(exist_ok=True) + # for j, best_p in enumerate(retained_best_params): + # plot_model_fit(bcm, best_p, iso3, opt_fits_path / f"best_fit_{j}.png") plot_multiple_model_fits(bcm, retained_best_params, out_path / "optimal_fits.png") @@ -395,57 +401,10 @@ def opti_func(sample_dict): if logger: logger.info(f"Start MCMC for {mcmc_params['tune']} + {mcmc_params['draws']} iterations and {mcmc_params['chains']} chains...") - n_repeat_seed = int(mcmc_params['chains'] / opti_params['n_best_retained']) + n_repeat_seed = 1 init_vals = [[best_p] * n_repeat_seed for i, best_p in enumerate(retained_best_params)] init_vals = [p_dict for sublist in init_vals for p_dict in sublist] - idata = sample_with_pymc(bcm, initvals=init_vals, draws=mcmc_params['draws'], tune=mcmc_params['tune'], cores=mcmc_params['cores'], chains=mcmc_params['chains'], method=mcmc_params['method']) - idata.to_netcdf(out_path / "idata.nc") - make_post_mc_plots(idata, full_run_params['burn_in'], output_folder) - if logger: - logger.info("... MCMC completed") - - """ - Post-MCMC processes - """ - sample_df = extract_sample_subset(idata, full_run_params['samples'], full_run_params['burn_in']) - if logger: - logger.info(f"Perform full runs for {full_run_params['samples']} samples") - outputs_df = run_full_runs(sample_df, iso3, analysis) - if logger: - logger.info("Calculate differential outputs") - diff_outputs_df = calculate_diff_outputs(outputs_df) - if logger: - logger.info("Calculate quantiles") - uncertainty_df, diff_quantiles_df = get_quantile_outputs(outputs_df, diff_outputs_df) - uncertainty_df.to_parquet(out_path / "uncertainty_df.parquet") - diff_quantiles_df.to_parquet(out_path / "diff_quantiles_df.parquet") - - make_country_output_tiling(iso3, uncertainty_df, diff_quantiles_df, output_folder) - - return idata, uncertainty_df, diff_quantiles_df - - - -def run_full_analysis_smc( - iso3, - analysis="main", - smc_params={'draws': 2000, 'cores': 32, 'chains': 4}, - full_run_params={'samples': 1000, 'burn_in': 5000}, - output_folder="test_outputs", - logger=None -): - out_path = Path(output_folder) - - # Create BayesianCompartmentalModel object - bcm = get_bcm_object(iso3, analysis) - - """ - SMC - """ - if logger: - logger.info(f"Start SMC for {smc_params['draws']} draws and {smc_params['chains']} chains...") - - idata = sample_with_pymc_smc(bcm, draws=smc_params['draws'], cores=smc_params['cores'], chains=smc_params['chains']) + idata = sample_with_pymc(bcm, initvals=init_vals, draws=mcmc_params['draws'], tune=mcmc_params['tune'], cores=mcmc_params['chains'], chains=mcmc_params['chains'], method=mcmc_params['method']) idata.to_netcdf(out_path / "idata.nc") make_post_mc_plots(idata, full_run_params['burn_in'], output_folder) if logger: @@ -471,7 +430,6 @@ def run_full_analysis_smc( return idata, uncertainty_df, diff_quantiles_df - """ Helper functions for remote runs """ diff --git a/notebooks/user/rragonnet/project_specific/School_Closure/full_analysis_runner.ipynb b/notebooks/user/rragonnet/project_specific/School_Closure/full_analysis_runner.ipynb index 979220923..8518fd74f 100644 --- a/notebooks/user/rragonnet/project_specific/School_Closure/full_analysis_runner.ipynb +++ b/notebooks/user/rragonnet/project_specific/School_Closure/full_analysis_runner.ipynb @@ -6,11 +6,7 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", - "import pandas as pd\n", - "\n", - "from autumn.projects.sm_covid2.common_school.runner_tools import run_full_analysis\n", - "from autumn.projects.sm_covid2.common_school.output_plots.country_spec import make_country_output_tiling" + "from autumn.projects.sm_covid2.common_school.runner_tools import run_full_analysis, DEFAULT_RUN_CONFIG" ] }, { @@ -22,6 +18,15 @@ "Note that the three returned objects will be dumped automatically into the specified output folder (1 .nc file, 2 .csv files)." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "DEFAULT_RUN_CONFIG" + ] + }, { "cell_type": "code", "execution_count": null, @@ -31,17 +36,16 @@ "ISO3 = \"FRA\"\n", "ANALYSIS = \"main\"\n", "\n", - "N_CHAINS = 8\n", - "N_OPTI_SEARCHES = 2\n", - "N_BEST_RETAINED = 1\n", - "OPTI_BUDGET = 100\n", + "N_CHAINS = 2 # DEFAULT_RUN_CONFIG['N_CHAINS']\n", + "N_OPTI_SEARCHES = 2 # DEFAULT_RUN_CONFIG['N_OPTI_SEARCHES']\n", + "OPTI_BUDGET = 100 # DEFAULT_RUN_CONFIG['OPTI_BUDGET']\n", "\n", - "METROPOLIS_TUNE = 100\n", - "METROPOLIS_DRAWS = 500\n", - "METROPOLIS_METHOD = \"DEMetropolis\"\n", + "METROPOLIS_TUNE = 100 # DEFAULT_RUN_CONFIG['METROPOLIS_TUNE']\n", + "METROPOLIS_DRAWS = 100 # DEFAULT_RUN_CONFIG['METROPOLIS_DRAWS']\n", + "METROPOLIS_METHOD = DEFAULT_RUN_CONFIG['METROPOLIS_METHOD']\n", "\n", - "FULL_RUNS_SAMPLES = 100\n", - "BURN_IN = 50" + "FULL_RUNS_SAMPLES = 100 # DEFAULT_RUN_CONFIG['FULL_RUNS_SAMPLES']\n", + "BURN_IN = 50 # DEFAULT_RUN_CONFIG['BURN_IN']" ] }, { @@ -53,55 +57,12 @@ "idata, uncertainty_df, diff_quantiles_df = run_full_analysis(\n", " ISO3,\n", " analysis=ANALYSIS, \n", - " opti_params={'n_searches': N_OPTI_SEARCHES, \"n_best_retained\": N_BEST_RETAINED, 'num_workers': 8, 'parallel_opti_jobs': 4, 'warmup_iterations': 0, 'search_iterations': OPTI_BUDGET, 'init_method': 'LHS'},\n", - " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'cores': N_CHAINS, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", + " opti_params={'n_searches': N_OPTI_SEARCHES, 'search_iterations': OPTI_BUDGET},\n", + " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", " full_run_params={'samples': FULL_RUNS_SAMPLES, 'burn_in': BURN_IN},\n", " output_folder=\"test_full\",\n", ")" ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Plot analysis outputs" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Load data from previous run if required" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# uncertainty_df = pd.read_parquet(os.path.join(output_folder, \"uncertainty_df.parquet\"))\n", - "# diff_quantiles_df = pd.read_parquet(os.path.join(output_folder, \"diff_quantiles_df.parquet\"))" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Make tiling plot" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "make_country_output_tiling(iso3, uncertainty_df, diff_quantiles_df, output_folder)" - ] } ], "metadata": { diff --git a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis.ipynb b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis.ipynb index 5d65b6816..a18d5462b 100644 --- a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis.ipynb +++ b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis.ipynb @@ -8,7 +8,12 @@ "outputs": [], "source": [ "from autumn.infrastructure.remote import springboard\n", - "from autumn.projects.sm_covid2.common_school.runner_tools import run_full_analysis, print_continuous_status, download_analysis" + "from autumn.projects.sm_covid2.common_school.runner_tools import (\n", + " run_full_analysis, \n", + " print_continuous_status, \n", + " download_analysis,\n", + " DEFAULT_RUN_CONFIG,\n", + ")" ] }, { @@ -32,55 +37,33 @@ { "cell_type": "code", "execution_count": null, - "id": "a5f49d54", + "id": "c4aa031b", "metadata": {}, "outputs": [], "source": [ - "ISO3 = \"FRA\"\n", - "ANALYSIS = \"main\"\n", - "\n", - "N_CHAINS = 32\n", - "N_OPTI_SEARCHES = 4\n", - "N_BEST_RETAINED = 1\n", - "OPTI_BUDGET = 10000\n", - "\n", - "METROPOLIS_TUNE = 2000\n", - "METROPOLIS_DRAWS = 10000\n", - "METROPOLIS_METHOD = \"DEMetropolis\"\n", - "\n", - "FULL_RUNS_SAMPLES = 1000\n", - "BURN_IN = 5000" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "253866ab", - "metadata": {}, - "source": [ - "#### Testing config" + "DEFAULT_RUN_CONFIG" ] }, { "cell_type": "code", "execution_count": null, - "id": "ac0a9fdd", + "id": "a5f49d54", "metadata": {}, "outputs": [], "source": [ - "# ISO3 = \"FRA\"\n", - "# ANALYSIS = \"main\"\n", + "ISO3 = \"FRA\"\n", + "ANALYSIS = \"main\"\n", "\n", - "# N_CHAINS = 32\n", - "# N_OPTI_SEARCHES = 8\n", - "# OPTI_BUDGET = 700\n", + "N_CHAINS = DEFAULT_RUN_CONFIG['N_CHAINS']\n", + "N_OPTI_SEARCHES = DEFAULT_RUN_CONFIG['N_OPTI_SEARCHES']\n", + "OPTI_BUDGET = DEFAULT_RUN_CONFIG['OPTI_BUDGET']\n", "\n", - "# METROPOLIS_TUNE = 200\n", - "# METROPOLIS_DRAWS = 1000\n", - "# METROPOLIS_METHOD = \"DEMetropolis\"\n", + "METROPOLIS_TUNE = DEFAULT_RUN_CONFIG['METROPOLIS_TUNE']\n", + "METROPOLIS_DRAWS = DEFAULT_RUN_CONFIG['METROPOLIS_DRAWS']\n", + "METROPOLIS_METHOD = DEFAULT_RUN_CONFIG['METROPOLIS_METHOD']\n", "\n", - "# FULL_RUNS_SAMPLES = 100\n", - "# BURN_IN = 500" + "FULL_RUNS_SAMPLES = DEFAULT_RUN_CONFIG['FULL_RUNS_SAMPLES']\n", + "BURN_IN = DEFAULT_RUN_CONFIG['BURN_IN']" ] }, { @@ -99,7 +82,6 @@ "metadata": {}, "outputs": [], "source": [ - "assert N_CHAINS % N_OPTI_SEARCHES == 0\n", "assert (METROPOLIS_DRAWS - BURN_IN) * N_CHAINS >= FULL_RUNS_SAMPLES\n", "assert METROPOLIS_METHOD in (\"DEMetropolis\", \"DEMetropolisZ\")" ] @@ -107,7 +89,7 @@ { "cell_type": "code", "execution_count": null, - "id": "47d05a73", + "id": "4810f137", "metadata": {}, "outputs": [], "source": [ @@ -121,8 +103,8 @@ " idata, uncertainty_df, diff_quantiles_df = run_full_analysis(\n", " iso3,\n", " analysis=analysis, \n", - " opti_params={'n_searches': N_OPTI_SEARCHES, \"n_best_retained\": N_BEST_RETAINED, 'num_workers': 8, 'parallel_opti_jobs': 8, 'warmup_iterations': 0, 'search_iterations': OPTI_BUDGET, 'init_method': 'LHS'},\n", - " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'cores': N_CHAINS, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", + " opti_params={'n_searches': N_OPTI_SEARCHES, 'search_iterations': OPTI_BUDGET},\n", + " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", " full_run_params={'samples': FULL_RUNS_SAMPLES, 'burn_in': BURN_IN},\n", " output_folder=bridge.out_path,\n", " logger=bridge.logger\n", @@ -198,7 +180,7 @@ "outputs": [], "source": [ "# wait function with status printing\n", - "print_continuous_status(runner)" + "# print_continuous_status(runner)" ] }, { @@ -208,7 +190,7 @@ "metadata": {}, "outputs": [], "source": [ - "run_path = 'projects/school_project/FRA/2023-08-22T1004-single_start_main_LHS4_opt10000_mc2000n10000'" + "# run_path = 'projects/school_project/FRA/2023-08-22T1004-single_start_main_LHS4_opt10000_mc2000n10000'" ] }, { @@ -220,14 +202,6 @@ "source": [ "download_analysis(run_path)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "170c3a1f", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis_smc.ipynb b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis_smc.ipynb deleted file mode 100644 index 275d64cd2..000000000 --- a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_full_analysis_smc.ipynb +++ /dev/null @@ -1,228 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "f5408fea", - "metadata": {}, - "outputs": [], - "source": [ - "from autumn.infrastructure.remote import springboard\n", - "from autumn.projects.sm_covid2.common_school.runner_tools import run_full_analysis_smc, print_continuous_status, download_analysis" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "e7255011", - "metadata": {}, - "source": [ - "### Define task function" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "2da27a35", - "metadata": {}, - "source": [ - "#### Standard config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a5f49d54", - "metadata": {}, - "outputs": [], - "source": [ - "ISO3 = \"FRA\"\n", - "ANALYSIS = \"main\"\n", - "\n", - "N_CHAINS = 8\n", - "N_CORES = 32\n", - "\n", - "METROPOLIS_DRAWS = 2000\n", - "\n", - "FULL_RUNS_SAMPLES = 1000\n", - "BURN_IN = 5000" - ] - }, - { - "attachments": {}, - "cell_type": "markdown", - "id": "253866ab", - "metadata": {}, - "source": [ - "#### Testing config" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ac0a9fdd", - "metadata": {}, - "outputs": [], - "source": [ - "# ISO3 = \"FRA\"\n", - "# ANALYSIS = \"main\"\n", - "\n", - "# N_CHAINS = 32\n", - "# N_OPTI_SEARCHES = 8\n", - "# OPTI_BUDGET = 700\n", - "\n", - "# METROPOLIS_TUNE = 200\n", - "# METROPOLIS_DRAWS = 1000\n", - "# METROPOLIS_METHOD = \"DEMetropolis\"\n", - "\n", - "# FULL_RUNS_SAMPLES = 100\n", - "# BURN_IN = 500" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "47d05a73", - "metadata": {}, - "outputs": [], - "source": [ - "def remote_full_analysis_smc_task(bridge: springboard.task.TaskBridge, iso3: str = 'FRA', analysis: str = \"main\"):\n", - " \n", - " import multiprocessing as mp\n", - " mp.set_start_method('forkserver')\n", - "\n", - " bridge.logger.info(f\"Running full analysis for {iso3}. Analysis type: {analysis}.\")\n", - "\n", - " idata, uncertainty_df, diff_quantiles_df = run_full_analysis_smc(\n", - " iso3,\n", - " analysis=analysis, \n", - " smc_params={'draws': METROPOLIS_DRAWS, 'cores': N_CORES, 'chains': N_CHAINS},\n", - " full_run_params={'samples': FULL_RUNS_SAMPLES, 'burn_in': BURN_IN},\n", - " output_folder=bridge.out_path,\n", - " logger=bridge.logger\n", - " )\n", - " \n", - " bridge.logger.info(\"Full analysis complete\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1a1207a8", - "metadata": {}, - "outputs": [], - "source": [ - "mspec = springboard.EC2MachineSpec(N_CORES, 4, \"compute\")\n", - "task_kwargs = {\n", - " \"iso3\": ISO3,\n", - " \"analysis\": ANALYSIS\n", - "}\n", - "tspec = springboard.TaskSpec(remote_full_analysis_task, task_kwargs)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e12adbec", - "metadata": {}, - "outputs": [], - "source": [ - "analysis_title = \"SMC\"\n", - "config_str = f\"_{ANALYSIS}_mc{METROPOLIS_DRAWS}\"\n", - "\n", - "run_path = springboard.launch.get_autumn_project_run_path(\"school_project\", ISO3, analysis_title + config_str)\n", - "run_path" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "16657a60", - "metadata": {}, - "outputs": [], - "source": [ - "runner = springboard.launch.launch_synced_autumn_task(tspec, mspec, run_path)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b416f02f", - "metadata": {}, - "outputs": [], - "source": [ - "runner.s3.get_status()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5dfd7dfe", - "metadata": {}, - "outputs": [], - "source": [ - "print(runner.top(\"%CPU\"))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bc0faa03", - "metadata": {}, - "outputs": [], - "source": [ - "# wait function with status printing\n", - "print_continuous_status(runner)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f8019dfb", - "metadata": {}, - "outputs": [], - "source": [ - "run_path = 'projects/school_project/FRA/2023-08-22T1004-single_start_main_LHS4_opt10000_mc2000n10000'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a8d31fe3", - "metadata": {}, - "outputs": [], - "source": [ - "download_analysis(run_path)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "170c3a1f", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.11" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_mcmc.ipynb b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_mcmc.ipynb deleted file mode 100644 index e69de29bb..000000000 diff --git a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_multi_full_analysis.ipynb b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_multi_full_analysis.ipynb index ca4a6fdae..45d3eebc6 100644 --- a/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_multi_full_analysis.ipynb +++ b/notebooks/user/rragonnet/project_specific/School_Closure/remote/sb_multi_full_analysis.ipynb @@ -2,19 +2,36 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\Users\\rrag0004\\.conda\\envs\\summer2\\lib\\site-packages\\summer\\runner\\vectorized_runner.py:363: NumbaDeprecationWarning: \u001b[1mThe 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.\u001b[0m\n", + " def get_strain_infection_values(\n", + "WARNING (pytensor.tensor.blas): Using NumPy C-API based implementation for BLAS functions.\n" + ] + } + ], "source": [ "from autumn.infrastructure.remote import springboard\n", - "from autumn.projects.sm_covid2.common_school.runner_tools import run_full_analysis, print_continuous_status, download_analysis" + "from autumn.projects.sm_covid2.common_school.runner_tools import (\n", + " run_full_analysis, \n", + " print_continuous_status, \n", + " download_analysis,\n", + " DEFAULT_RUN_CONFIG,\n", + ")" ] }, { - "cell_type": "markdown", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ - "#### Standard config" + "DEFAULT_RUN_CONFIG" ] }, { @@ -26,17 +43,26 @@ "ISO3_LIST = [\"AUS\", \"BOL\", \"FRA\", \"MAR\", \"PHL\"]\n", "ANALYSIS = \"main\"\n", "\n", - "N_CHAINS = 32\n", - "N_OPTI_SEARCHES = 32\n", - "N_BEST_RETAINED = 16\n", - "OPTI_BUDGET = 10000\n", + "N_CHAINS = DEFAULT_RUN_CONFIG['N_CHAINS']\n", + "N_OPTI_SEARCHES = DEFAULT_RUN_CONFIG['N_OPTI_SEARCHES']\n", + "OPTI_BUDGET = DEFAULT_RUN_CONFIG['OPTI_BUDGET']\n", "\n", - "METROPOLIS_TUNE = 2000\n", - "METROPOLIS_DRAWS = 10000\n", - "METROPOLIS_METHOD = \"DEMetropolis\"\n", + "METROPOLIS_TUNE = DEFAULT_RUN_CONFIG['METROPOLIS_TUNE']\n", + "METROPOLIS_DRAWS = DEFAULT_RUN_CONFIG['METROPOLIS_DRAWS']\n", + "METROPOLIS_METHOD = DEFAULT_RUN_CONFIG['METROPOLIS_METHOD']\n", "\n", - "FULL_RUNS_SAMPLES = 1000\n", - "BURN_IN = 5000" + "FULL_RUNS_SAMPLES = DEFAULT_RUN_CONFIG['FULL_RUNS_SAMPLES']\n", + "BURN_IN = DEFAULT_RUN_CONFIG['BURN_IN']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "assert (METROPOLIS_DRAWS - BURN_IN) * N_CHAINS >= FULL_RUNS_SAMPLES\n", + "assert METROPOLIS_METHOD in (\"DEMetropolis\", \"DEMetropolisZ\")" ] }, { @@ -55,8 +81,8 @@ " idata, uncertainty_df, diff_quantiles_df = run_full_analysis(\n", " iso3,\n", " analysis=analysis, \n", - " opti_params={'n_searches': N_OPTI_SEARCHES, \"n_best_retained\": N_BEST_RETAINED, 'num_workers': 8, 'parallel_opti_jobs': 8, 'warmup_iterations': 0, 'search_iterations': OPTI_BUDGET, 'init_method': 'LHS'},\n", - " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'cores': N_CHAINS, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", + " opti_params={'n_searches': N_OPTI_SEARCHES, 'search_iterations': OPTI_BUDGET},\n", + " mcmc_params={'draws': METROPOLIS_DRAWS, 'tune': METROPOLIS_TUNE, 'chains': N_CHAINS, 'method': METROPOLIS_METHOD},\n", " full_run_params={'samples': FULL_RUNS_SAMPLES, 'burn_in': BURN_IN},\n", " output_folder=bridge.out_path,\n", " logger=bridge.logger\n", @@ -117,37 +143,9 @@ "metadata": {}, "outputs": [], "source": [ - "runners_dict = {iso3: runners[list(runners.keys())[i]] for i, iso3 in enumerate(ISO3_LIST)}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "runner.instance" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "runners_dict[\"AUS\"].s3.get_status()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "for iso3 in ISO3_LIST:\n", - " runner = runners_dict[iso3]\n", + "for run_path, runner in runners:\n", " print(runner.s3.get_status())\n", - " print(runner.top(\"%MEM\"))" + " # print(runner.top(\"%MEM\"))" ] }, {