def configure(cb): ModBuilder.metadata.update({'Config_Name': test_name}) test = DTKConfigBuilder.from_files( os.path.join(self.regression_path, test_name, 'config.json'), os.path.join(self.regression_path, test_name, 'campaign.json')) cb.copy_from(test)
def prepare_malariatherapy_configbuilder(config_path, immunity_forcing=True, years=1): # Setup ------------------------------------------------------------------------------------------- cb = DTKConfigBuilder.from_files(config_path) cb.update_params({ 'Vector_Species_Names': [], 'Simulation_Duration': 365 * years, 'Demographics_Filenames': ['Malariatherapy_demographics.json'] }) set_climate_constant(cb) # Add source of infection (challenge bite or forced EIR) ------------------------------------------ add_challenge_trial(cb, start_day=0) # ---- CUSTOM REPORTS ---- add_patient_report(cb) return cb
def test_listed_events(self): from simtools.SetupParser import SetupParser SetupParser.init() # Create a builder based on the files builder = DTKConfigBuilder.from_files( os.path.join(self.input_path, 'customevents', 'configbad.json'), os.path.join(self.input_path, 'customevents', 'campaign.json')) # Call the file writter def fake_write(name, content): return builder.file_writer(fake_write) # Try if the listed events worked self.assertEqual( set(builder.config['parameters']['Listed_Events']), set([ "Received_ITN", "TestedPositive", "Received_Campaign_Drugs", "Received_Treatment", "Received_Test", "Received_Survey", "Diagnostic_Survey_5", "Give_Drugs" ]))
import os from dtk.interventions.larvicides import add_larvicides from dtk.utils.analyzers import TimeseriesAnalyzer, group_by_name from dtk.utils.core.DTKConfigBuilder import DTKConfigBuilder from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory from simtools.ModBuilder import ModBuilder, ModFn from simtools.SetupParser import SetupParser SetupParser.default_block = 'HPC' # Create a config builder from set of input files input_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "inputs", "Sweeps") cb = DTKConfigBuilder.from_files(os.path.join(input_dir, 'config.json'), os.path.join(input_dir, 'campaign.json')) # Change the duration of the simulations to 5 years cb.set_param('Simulation_Duration', 5 * 365) # Define a set_larvicides function in order for the builder to call with different values # Functions passed to the builder needs to take a ConfigBuilder as first parameter # Here we are interested on sweeping on the start date of the intervention. def set_larvicides(cb, start): # Add larvicides to the campaign held in the ConfigBuilder add_larvicides(cb, start) # Returns the tag we want our simulation to have # In order to identify which value of the sweep we used, we are tagging our simulations # with larvicide_start = start date of the larvicides intervention
from dtk.utils.core.DTKConfigBuilder import DTKConfigBuilder from simtools.SetupParser import SetupParser from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory # Run on HPC SetupParser.default_block = "HPC" # Tell this module how to find the config and campaign files to be used. Set campaign_file to None if not used. input_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "input") config_file = os.path.join(input_dir, 'config.json') campaign_file = os.path.join(input_dir, 'campaign.json') kwargs = {} cb = DTKConfigBuilder.from_files(config_name=config_file, campaign_name=campaign_file, **kwargs) cb.set_collection_id('71483975-57dc-e711-9414-f0921c16b9e5') # Name of the experiment exp_name = 'catalyst_comparison-MalariaSandbox' # when run with 'dtk catalyst', run_sim_args['exp_name'] will have additional information appended. run_sim_args = { 'exp_name': exp_name, # 'exp_builder': builder, # users may have created one; this will be overridden in 'dtk catalyst' 'config_builder': cb } if __name__ == "__main__": SetupParser.init()
SetupParser.init('HPC') # Find experiment from whose config/campaigns we want to use (also get sweep params) comparison_exp_id = "9945ae69-3106-e711-9400-f0921c16849c" sim_name = 'Rerun_Rampup_MDA_Better_Diagnostic' expt = retrieve_experiment(comparison_exp_id) df = pd.DataFrame([x.tags for x in expt.simulations]) df['outpath'] = pd.Series([sim.get_path() for sim in expt.simulations]) # generate cb object from the first of these files (the only difference will be in the sweep params) cb_dir = df['outpath'][0] cb = DTKConfigBuilder.from_files(config_name=os.path.join(cb_dir, 'config.json'), campaign_name=os.path.join(cb_dir, 'campaign.json')) CompsDTKOutputParser.sim_dir_map = None #cb.update_params({'Num_Cores': 1}) sites = [ MyanmarCalibSite() ] # Here we are specifying the initial values for the next point data initial_state = [{ 'NodeIDs':[], 'Serialization':2007, 'Prevalence_date':2006, 'Prevalence_threshold':0.4, 'New_Duration': 1643, # Duration of second iteration
run_sets = [] for sweep in SWEEPS: i = 0 for sim_id in os.listdir(path=simulation_input_files_dir): # Create one experiment per base_simulationXsweep pairing. Each exp contains one sim per sweep setting. i += 1 simulation_directory = os.path.join(simulation_input_files_dir, sim_id) if not os.path.isdir(simulation_directory): # skip over Readme file continue # create a config builder for the current experiment, derived from the base simulation input files config_filepath = os.path.join(simulation_directory, 'config.json') campaign_filepath = os.path.join( simulation_directory, 'campaign_Swaziland_v05Sep2018_FINAL_reftracktopup_2011_v2.json') cb = DTKConfigBuilder.from_files(config_name=config_filepath, campaign_name=campaign_filepath) cb.set_input_files_root( path=simulation_directory ) # discoverability of other input files, e.g. PFA_Overlay.json exp_name = EXP_NAME_PATTERN % (sweep['name'], i) # Create a generator of the per-simulation differences from the base simulation builder = ModBuilder.from_combos(sweep['mods']) # keep track of each run_sim_args dictionary, one per experiment run_sets.append({ 'exp_name': exp_name, 'exp_builder': builder, 'config_builder': cb, 'base_simulation_id': sim_id,
import os import json import math import random from dtk.utils.core.DTKConfigBuilder import DTKConfigBuilder from dtk.utils.reports.BaseAgeHistReport import BaseAgeHistReport from simtools.ModBuilder import ModBuilder, ModFn from simtools.SetupParser import SetupParser from simtools.ExperimentManager.ExperimentManagerFactory import ExperimentManagerFactory from SetupFunctions import sample_point_fn #Run on HPC SetupParser.default_block = "HPC" cb = DTKConfigBuilder.from_files(config_name='InputFiles\\config.json', campaign_name='InputFiles\\basecampaign.json') cb.set_param('Num_Cores', 1) cb.set_experiment_executable(path='Executable\\Eradication.exe') cb.add_demog_overlay( name='demographics.json', content=json.load( open('InputFiles\\Nigeria_Ward_smaller_minpop5000_demographics.json'))) cb.experiment_files.add_file( path='InputFiles\\Nigeria_Ward_smaller_minpop5000_air_migration.bin') cb.experiment_files.add_file( path='InputFiles\\Nigeria_Ward_smaller_minpop5000_air_migration.bin.json') cb.experiment_files.add_file( path='InputFiles\\Nigeria_Ward_smaller_minpop5000_local_migration.bin') cb.experiment_files.add_file( path='InputFiles\\Nigeria_Ward_smaller_minpop5000_local_migration.bin.json' )
BaseInputPath = load_input_path() if not os.path.exists(BaseInputPath): os.mkdir(BaseInputPath) OutputPath = os.path.join(load_output_path(), ScenarioName) if not os.path.exists(OutputPath): os.mkdir(OutputPath) if not os.path.exists(os.path.join(OutputPath, 'input_files')): os.mkdir(os.path.join(OutputPath, 'input_files')) #Name the input files DemoFile = os.path.join(BaseInputPath, 'demographics.json') CampaignFile = os.path.join(BaseInputPath, 'campaign.json') ConfigFile = os.path.join(BaseInputPath, 'config.json') SimDurationInYears = 1 TotalPopulation = 5e5 cb = DTKConfigBuilder.from_files(ConfigFile, campaign_name=CampaignFile) cb.set_param('Simulation_Duration', SimDurationInYears * 365.0) demoFile = DemographicsFile.from_file(DemoFile) demoFile.content['Nodes'][0]['NodeAttributes'][ 'InitialPopulation'] = TotalPopulation #Fix this if len(demoFile.nodes) == 1: for node in demoFile.nodes.values(): node.pop = TotalPopulation else: raise ValueError('demographics.json assumed to have only one node') demoFile = SetAgeDistribution( demoFile,
force_immunity = True debug = False def set_immune_forcing_builder(transition_matrix=None, scale_factor_array=[2, 5, 10, 100]): """Creates an experiment builder setting up immunity based on scale factor""" builder = ModBuilder.from_combos([ ModFn(set_transition_matrix, transition_matrix, scale_factor) for scale_factor in scale_factor_array ]) return builder # Setup ------------------------------------------------------------------------------------------- cb = DTKConfigBuilder.from_files(config_filename) cb.update_params({ 'Vector_Species_Names': [], 'Simulation_Duration': 365, 'Demographics_Filenames': ['Malariatherapy_demographics.json'] }) set_climate_constant(cb) # Add source of infection (challenge bite or forced EIR) ------------------------------------------ add_challenge_trial(cb, start_day=0) # ---- CUSTOM REPORTS ---- add_patient_report(cb) if debug: print(f"DEBUG: config builder created")
from malaria.interventions.malaria_challenge import add_challenge_trial from malaria.reports.MalariaReport import add_patient_report from analyze_infection_durations import DurationsAnalyzer from immunity_transitions_configuration import set_transition_matrix # General working_dir = './' exp_name = 'Malariatherapy_2pt0_infections' years = 1 # length of simulation, in years immunity_forcing_on = 1 config_path = os.path.join(working_dir, 'input/from-cfg.json') # Setup ---------------------------------------------------------------------------------------------------------- cb = DTKConfigBuilder.from_files(config_path) cb.update_params({'Vector_Species_Names' : [], 'Simulation_Duration' : 365*years, 'Demographics_Filenames' : ['Malariatherapy_demographics.json'] }) set_climate_constant(cb) analyzers = [DurationsAnalyzer() ] # Specify immune parameters -------------------------------------------------------------------------------------- if immunity_forcing_on == 1: #Pull the naive transition matrix values from the config transition_matrix = cb.config['parameters']['Parasite_Peak_Density_Probabilities'] #Specify the scaling of the transitio matrix to be applied (representing increasing immune pressure) scale_factor_array = [2,5,10,100]