def test_generic_sweep(self): def verify(b): md = [(0.05, 100, 'Namawala'), (0.05, 100, 'Matsari'), (0.1, 100, 'Namawala'), (0.1, 100, 'Matsari')] ngenerated = 0 for i, ml in enumerate(b.mod_generator): for m in ml: m(self.cb) mdd = dict( zip(('x_Temporary_Larval_Habitat', 'Simulation_Duration', '_site_'), md[i])) mdd.update({'population_scale': 1}) self.assertEqual(b.metadata, mdd) self.assertEqual( self.cb.get_param('x_Temporary_Larval_Habitat'), md[i][0]) ngenerated += 1 self.assertEqual(ngenerated, 4) b = ModBuilder.from_combos([ ModFn(DTKConfigBuilder.set_param, 'x_Temporary_Larval_Habitat', v) for v in [0.05, 0.1] ], [ModFn(DTKConfigBuilder.set_param, 'Simulation_Duration', 100) ], [ModFn(configure_site, s) for s in ['Namawala', 'Matsari']]) verify(b) b = GenericSweepBuilder.from_dict({ 'x_Temporary_Larval_Habitat': [0.05, 0.1], 'Simulation_Duration': [100], '_site_': ['Namawala', 'Matsari'] }) verify(b)
from dtk.generic.climate import set_climate_constant from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory from simtools.SetupParser import SetupParser if __name__ == "__main__": expname = 'Serialized_file_drug_testing' sim_duration = 365 * 70 num_seeds = 50 SetupParser('HPC') cb = DTKConfigBuilder.from_defaults('MALARIA_SIM') builder = GenericSweepBuilder.from_dict({'Run_Number': range(num_seeds)}) set_climate_constant(cb) set_species_param(cb, 'gambiae', 'Larval_Habitat_Types', {"LINEAR_SPLINE": { "Capacity_Distribution_Per_Year": { "Times": [0.0, 30.417, 60.833, 91.25, 121.667, 152.083, 182.5, 212.917, 243.333, 273.75, 304.167, 334.583], # "Values": [1, 0.25, 0.1, 1, 1, 0.5, 12, 5, 3, 2, 1.5, 1.5] "Values": [3, 0.8, 1.25, 0.1, 2.7, 8, 4, 35, 6.8, 6.5, 2.6, 2.1] }, "Max_Larval_Capacity": 1e9 }}) set_species_param(cb, "gambiae", "Indoor_Feeding_Fraction", 0.9) cb.update_params({"Demographics_Filenames": ['Calibration/single_node_demographics.json'],
from simtools.SetupParser import SetupParser from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory # Run on HPC SetupParser.default_block = "HPC" # Configure a default 5 years simulation cb = DTKConfigBuilder.from_defaults('MALARIA_SIM', Simulation_Duration=365 * 5) # Set it in Namawala configure_site(cb, 'Namawala') # Name of the experiment exp_name = 'ExampleSweep' # Create a builder to sweep over the birth rate multiplier builder = GenericSweepBuilder.from_dict({'x_Birth': np.arange(1, 1.5, .1)}) run_sim_args = { 'exp_name': exp_name, 'exp_builder': builder, 'config_builder': cb } if __name__ == "__main__": SetupParser.init() exp_manager = ExperimentManagerFactory.init() exp_manager.run_simulations(**run_sim_args) exp_manager.wait_for_finished(verbose=True) assert (exp_manager.succeeded())
'CONSTANT': 1.0 * pop_multiplier, 'WATER_VEGETATION': 1.0 * pop_multiplier, 'PIECEWISE_MONTHLY': fun_multiplier } return demographics location = 'HPC' #'LOCAL' geography = 'Zambia/Gwembe_Sinazongwe_115_nodes' sites = ['Gwembe_Sinazongwe_115_nodes'] SetupParser.init(location) builder = GenericSweepBuilder.from_dict({ '_site_': sites, # study sites 'x_Local_Migration': [1e-2], 'Run_Number': range(1) # random seeds }) cb = DTKConfigBuilder.from_defaults('MALARIA_SIM', Num_Cores=24, Simulation_Duration=365 * 5) # migration cb.update_params(single_roundtrip_params) # set demographics file name cb.update_params({ 'Demographics_Filenames': [ os.path.join(geography, "Zambia_Gwembe_Sinazongwe_115_nodes_demographics.json")
from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory from simtools.SetupParser import SetupParser # This block will be used unless overridden on the command-line SetupParser.default_block = 'HPC' cb = DTKConfigBuilder.from_defaults('VECTOR_SIM') configure_site(cb, 'Namawala') cb.set_param('Simulation_Duration',365) analyzers = (TimeseriesAnalyzer(), VectorSpeciesAnalyzer()) builder = GenericSweepBuilder.from_dict({'Run_Number': range(5)}) run_sim_args = { 'exp_name': 'testrunandanalyze', 'exp_builder': builder, 'config_builder':cb } if __name__ == "__main__": SetupParser.init(selected_block=SetupParser.default_block) exp_manager = ExperimentManagerFactory.from_cb(config_builder=cb) exp_manager.run_simulations(**run_sim_args) exp_manager.wait_for_finished(verbose=True) am = AnalyzeManager(exp_manager.experiment) for a in analyzers:
def catalyst(args, unknownArgs): """ Catalyst run-and-analyze process as ported from the test team. Programmatic-only arguments: args.mode : used by FidelityReportExperimentDefinition, default: 'prod' args.report_label : attached to the experiment name args.debug : True/False, passed into FidelityReportAnalyzer, default: False :param args: :param unknownArgs: :return: """ from dtk.utils.builders.sweep import GenericSweepBuilder from catalyst_report.fidelity_report_analyzer import FidelityReportAnalyzer from catalyst_report.fidelity_report_experiment_definition import FidelityReportExperimentDefinition import catalyst_report.utils as catalyst_utils from simtools.Analysis.AnalyzeManager import AnalyzeManager # we're going to do a dtk run, then a set-piece analysis. But first we need to do some overrides # to get the run part to do the desired parameter sweep. mod = args.loaded_module # when run with 'dtk catalyst', run_sim_args['exp_name'] will have additional information appended. mod.run_sim_args[ 'exp_name'] = mod.run_sim_args['exp_name'] + '-development' # lining up the arguments expected by FidelityReportExperimentDefinition args.sweep = args.sweep_method # hidden, programmatic arguments args.mode = args.mode if hasattr(args, 'mode') else 'prod' args.report_label = args.report_label if hasattr(args, 'report_label') else None args.debug = args.debug if hasattr(args, 'debug') else False # determine which report is being asked for. If not specified, default to what the config.json file says # ck4, this should go somewhere else, on a Config object of some sort? (prob not the builder, though) report_type_mapping = { 'DENGUE_SIM': 'dengue', 'GENERIC_SIM': 'generic', 'HIV_SIM': 'hiv', 'MALARIA_SIM': 'malaria', 'POLIO_SIM': 'polio', 'STI_SIM': 'sti', 'TB_SIM': 'tb', 'TYPHOID_SIM': 'typhoid', 'VECTOR_SIM': 'generic' } if args.report_type: report_type = args.report_type else: sim_type = mod.run_sim_args['config_builder'].config['parameters'][ 'Simulation_Type'] report_type = report_type_mapping.get(sim_type, None) if not report_type: raise KeyError( 'Default report type could not be determined for sim_type: %s. Report type must be specified' ' via -r flag.' % sim_type) # Create and set a builder to sweep over population scaling or model timestep reports = catalyst_utils.load_report_definitions( definitions_filename=args.report_definitions) if report_type in reports: args.report_channel_list = reports[report_type]['inset_channel_names'] else: raise Exception('Invalid report: %s. Available reports: %s' % (report_type, sorted(reports.keys()))) catalyst_config = catalyst_utils.load_sweep_configs( sweep_type=args.sweep_type, config_filename=args.sweep_definitions) defn = FidelityReportExperimentDefinition(catalyst_config, args) # redefine the experiment name so it doesn't conflict with the likely follow-up non-catalyst experiment mod.run_sim_args['exp_name'] = 'Catalyst-' + mod.run_sim_args['exp_name'] # define the sweep to perform sweep_dict = { 'Run_Number': range(1, int(defn['nruns']) + 1), defn['sweep_param']: defn['sweep_values'] } mod.run_sim_args['exp_builder'] = GenericSweepBuilder.from_dict(sweep_dict) # overwrite spatial output channels to those used in the catalyst report spatial_channel_names = defn['spatial_channel_names'] if len(spatial_channel_names) > 0: mod.run_sim_args['config_builder'].enable('Spatial_Output') mod.run_sim_args['config_builder'].params[ 'Spatial_Output_Channels'] = spatial_channel_names else: mod.run_sim_args['config_builder'].disable('Spatial_Output') mod.run_sim_args['config_builder'].params[ 'Spatial_Output_Channels'] = [] # now run if no preexisting experiment id was provided if not args.experiment_id: # we must always block so that we can run the analysis at the end; run and analyze! args.blocking = True experiment_manager = run(args, unknownArgs) experiment = experiment_manager.experiment print('Done running experiment: %s' % experiment.exp_id) else: experiment = retrieve_experiment(args.experiment_id) # Create an analyze manager am = AnalyzeManager(exp_list=[experiment], verbose=False) # Add the TimeSeriesAnalyzer to the manager and do analysis # ck4, is there a better way to specify the first 4 arguments? The DTKCase from Test-land might be nicer. # After all, the names COULD be different analyzer = FidelityReportAnalyzer( 'catalyst_report', 'config.json', mod.run_sim_args['config_builder'].get_param( 'Demographics_Filenames')[0], experiment_definition=defn, experiment_id=experiment.exp_id, experiment_name=experiment.exp_name, label=args.report_label, time_series_step_from=defn['step_from'], time_series_step_to=defn['step_to'], time_series_equal_step_count=True, raw_data=True, debug=args.debug) am.add_analyzer(analyzer) am.analyze() import webbrowser webbrowser.open_new("file:///{}".format( os.path.join(os.getcwd(), "catalyst_report", "summary_report.html")))
from dtk.interventions.outbreakindividual import recurring_outbreak from dtk.utils.builders.sweep import GenericSweepBuilder from dtk.utils.core.DTKConfigBuilder import DTKConfigBuilder from malaria.reports.MalariaReport import add_summary_report, add_immunity_report exp_name = 'burnin' builder = GenericSweepBuilder.from_dict({ 'Run_Number': range(1), 'x_Temporary_Larval_Habitat': (0.1, 0.2, 0.3, 0.4, 0.5), '_site_': ('Sinazongwe.static', ) }) nyears = 50 # for sim duration AND reporting interval cb = DTKConfigBuilder.from_defaults('MALARIA_SIM') cb.update_params({ 'Num_Cores': 1, 'Simulation_Duration': nyears * 365, 'New_Diagnostic_Sensitivity': 0.025 # 40/uL }) # recurring outbreak to avoid fadeout recurring_outbreak(cb, outbreak_fraction=0.001, tsteps_btwn=180) # custom reporting add_summary_report(cb, start=0, interval=365, nreports=nyears, description="AnnualAverage")