From b80f7d5fa1d7573e339ba263b185d0838fa319fb Mon Sep 17 00:00:00 2001 From: Ben Toh Date: Mon, 16 Oct 2023 12:45:51 -0500 Subject: [PATCH 1/2] Some changes to make things work plus test.py --- examples/test.py | 71 +++++++++++++++++++ snt/hbhi/set_up_general.py | 36 +++++----- snt/hbhi/set_up_interventions.py | 19 ++--- .../malaria_vaccdrug_campaigns.py | 10 +-- 4 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 examples/test.py diff --git a/examples/test.py b/examples/test.py new file mode 100644 index 0000000..bd69e9a --- /dev/null +++ b/examples/test.py @@ -0,0 +1,71 @@ +import emod_api.campaign as campaign +import numpy as np +import pandas as pd +from snt.hbhi.set_up_interventions import InterventionSuite +from emod_api.interventions.common import BroadcastEvent, DelayedIntervention +from emodpy_malaria.interventions.common import add_triggered_campaign_delay_event +from snt.hbhi.set_up_interventions import add_all_interventions +from emodpy_malaria.interventions.vaccine import add_scheduled_vaccine, add_triggered_vaccine +import manifest + +def non_snt_camp(): + import emod_api.campaign as campaign + campaign.schema_path = manifest.schema_file + + delay_distribution = {"Delay_Period_Distribution": "CONSTANT_DISTRIBUTION", + "Delay_Period_Constant": 274} + event_name = "RTSS_1_eligible" + broadcast_event = BroadcastEvent(campaign, event_name) + delayed_event = DelayedIntervention(campaign, Configs=[broadcast_event], + Delay_Dict=delay_distribution) + add_triggered_campaign_delay_event(campaign, start_day=1, + trigger_condition_list=['Births'], + demographic_coverage=0.504, + individual_intervention=delayed_event) + add_triggered_vaccine(campaign, + start_day=1, + trigger_condition_list=["RTSS_1_eligible"], + intervention_name='RTSS', + broadcast_event='Received_Vaccine', + vaccine_type="AcquisitionBlocking", + vaccine_initial_effect=0.8, + vaccine_box_duration=0, + vaccine_decay_time_constant=592.4067, + efficacy_is_multiplicative=False) + + return campaign + + +def snt_camp(): + import emod_api.campaign as campaign + campaign.schema_path = manifest.schema_file + + int_suite = InterventionSuite() + int_suite.rtss_auto_changeips = False + + rtss_df = pd.DataFrame({ + 'DS_Name': ['Yomou'], + 'coverage_levels': [0.504], + 'round': [3], + 'vaccine': ['simple'], + 'initial_killing': [0.8], + 'decay_time_constant': [592.4067], + 'rtss_touchpoints': [274], + 'decay_class': ['WaningEffectExponential'], + 'RTSS_day': [1], + 'deploy_type': ['EPI'], + 'distribution_name': ['CONSTANT_DISTRIBUTION'], + 'distribution_std': [1] + }) + print(rtss_df) + add_all_interventions(campaign, int_suite, my_ds="Yomou", + rtss_df=rtss_df) + + return campaign + + +camp1 = non_snt_camp() +camp2 = snt_camp() + +camp1.save("camp1.json") +camp2.save("camp2.json") diff --git a/snt/hbhi/set_up_general.py b/snt/hbhi/set_up_general.py index cea4fda..29c9179 100644 --- a/snt/hbhi/set_up_general.py +++ b/snt/hbhi/set_up_general.py @@ -167,8 +167,8 @@ def set_input_files(config, my_ds, archetype_ds=None, demographic_suffix='', else: ds = my_ds - config.parameters['District_Sanitaire'] = 'my_ds' - config.parameters['Archetype'] = 'archetype_ds' + # config.parameters['District_Sanitaire'] = 'my_ds' + # config.parameters['Archetype'] = 'archetype_ds' if demographic_suffix is not None: config.parameters.Demographics_Filenames = [ @@ -196,7 +196,7 @@ def set_input_files(config, my_ds, archetype_ds=None, demographic_suffix='', return {'DS_Name': my_ds} -def add_input_files(task, iopath, my_ds, archetype_ds=None, demographic_suffix='', +def add_input_files(task, inputpath, my_ds, archetype_ds=None, demographic_suffix='', climate_suffix='', climate_prefix=True, use_archetype=True): """ Add assets corresponding to the filename parameters set in set_input_files. @@ -232,58 +232,58 @@ def add_input_files(task, iopath, my_ds, archetype_ds=None, demographic_suffix=' if demographic_suffix is not None: demog_path = os.path.join(ds, f'{ds}_demographics{demographic_suffix}.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', demog_path), + task.common_assets.add_asset(os.path.join(inputpath, demog_path), relative_path=str(Path(demog_path).parent), fail_on_duplicate=False) if climate_suffix is not None: if climate_prefix: file_path = os.path.join(ds, f'{ds}_air_temperature_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'{ds}_air_temperature_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'{ds}_rainfall_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'{ds}_rainfall_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'{ds}_relative_humidity_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'{ds}_relative_humidity_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) else: file_path = os.path.join(ds, f'air_temperature_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'air_temperature_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'rainfall_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'rainfall_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'relative_humidity_daily{climate_suffix}.bin') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) file_path = os.path.join(ds, f'relative_humidity_daily{climate_suffix}.bin.json') - task.common_assets.add_asset(os.path.join(iopath, 'simulation_inputs', file_path), + task.common_assets.add_asset(os.path.join(inputpath, file_path), relative_path=str(Path(file_path).parent), fail_on_duplicate=False) def setup_ds(config, manifest, platform, my_ds, archetype_ds=None, pull_from_serialization=False, burnin_id='', ser_date=50 * 365, - burnin_fname='', + burnin_df=None, rel_abund_df=None, lhdf=None, use_arch_burnin=True, from_arch=None, demographic_suffix='', climate_suffix='', @@ -379,7 +379,7 @@ def setup_ds(config, manifest, platform, my_ds, archetype_ds=None, if burnin_id: ser_df = platform.create_sim_directory_df(burnin_id) # TODO: or we can pass ser_df in else: - ser_df = pd.read_csv(burnin_fname) + ser_df = burnin_df if use_arch_burnin: ser_df = ser_df[ser_df[ds_name] == archetype_ds] diff --git a/snt/hbhi/set_up_interventions.py b/snt/hbhi/set_up_interventions.py index 3aff6aa..e68723f 100644 --- a/snt/hbhi/set_up_interventions.py +++ b/snt/hbhi/set_up_interventions.py @@ -129,7 +129,7 @@ def add_hs_from_file(self, campaign, row, duration): 'rate': rates }) add_treatment_seeking(campaign, start_day=start_day, targets=targets, drug=['Artemether', 'Lumefantrine'], - duration=duration) + duration=duration, broadcast_event_name='Received_Treatment') # Severe targets = [] @@ -677,7 +677,7 @@ def add_ds_vaccpmc(self, campaign, pmc_df, my_ds): num_iiv_groups = 1 pmc_touchpoints_dict = {} - for i, tp in enumerate(df['pmc_touchpoints']): + for i, tp in enumerate(df[self.pmc_touchpoint_col]): pmc_touchpoints_dict[f'{i}'] = tp add_vaccdrug_campaign(campaign, campaign_type='PMC', start_days=list(df[self.pmc_start_col]), @@ -838,13 +838,14 @@ def add_all_interventions(campaign, int_suite, my_ds, hs_df=pd.DataFrame(), if has_irs > 0: event_list.append('Received_IRS') - if not smc_df[smc_df[int_suite.smc_ds_col] == my_ds].empty: - if addtl_smc_func: - addtl_smc = addtl_smc_func(campaign, smc_df, my_ds) - # has_smc = int_suite.add_ds_smc(cb, smc_df, my_ds) # 'Received_Campaign_Drugs' - has_smc = int_suite.add_ds_vaccsmc(campaign, smc_df, my_ds) # per default use vaccsmc - if has_smc > 0: - event_list = event_list + ['Received_SMC_VaccDrug'] # 'Received_Vehicle' + if not smc_df.empty: + if not smc_df[smc_df[int_suite.smc_ds_col] == my_ds].empty: + if addtl_smc_func: + addtl_smc = addtl_smc_func(campaign, smc_df, my_ds) + # has_smc = int_suite.add_ds_smc(cb, smc_df, my_ds) # 'Received_Campaign_Drugs' + has_smc = int_suite.add_ds_vaccsmc(campaign, smc_df, my_ds) # per default use vaccsmc + if has_smc > 0: + event_list = event_list + ['Received_SMC_VaccDrug'] # 'Received_Vehicle' if not itn_df.empty: has_itn = int_suite.add_ds_itns(campaign, itn_df, my_ds) diff --git a/snt/support_files/malaria_vaccdrug_campaigns.py b/snt/support_files/malaria_vaccdrug_campaigns.py index fc76a8f..1c7f91b 100644 --- a/snt/support_files/malaria_vaccdrug_campaigns.py +++ b/snt/support_files/malaria_vaccdrug_campaigns.py @@ -384,7 +384,7 @@ def add_vacc_smc(campaign, start_days, coverages, target_group: dict = None, vaccine_initial_effect=vacc_initial_effect, vaccine_box_duration=vacc_box_duration, vaccine_decay_time_constant=vacc_decay_duration / math.log(2), - efficacy_is_multiplicative=False, + efficacy_is_multiplicative=True, broadcast_event=receiving_vaccine_event) return {'smc_cov': sum(coverages) / len(coverages), @@ -521,7 +521,7 @@ def add_vaccdrug_pmc(campaign, start_days: list, coverages: list, vaccine_initial_effect=vaccine_initial_effect, vaccine_box_duration=vaccine_box_duration, vaccine_decay_time_constant=vaccine_decay_duration / math.log(2), - efficacy_is_multiplicative=False + efficacy_is_multiplicative=True ) else: add_triggered_vaccine(campaign, @@ -535,7 +535,7 @@ def add_vaccdrug_pmc(campaign, start_days: list, coverages: list, vaccine_initial_effect=vaccine_initial_effect, vaccine_box_duration=vaccine_box_duration, vaccine_decay_time_constant=vaccine_decay_duration / math.log(2), - efficacy_is_multiplicative=False + efficacy_is_multiplicative=True ) return {'pmc_cov': sum(coverages) / len(coverages), @@ -628,7 +628,7 @@ def add_vacc_pmc(campaign, start_days: list, coverages: list, target_group: dict vaccine_initial_effect=vaccine_initial_effect, vaccine_box_duration=vaccine_box_duration, vaccine_decay_time_constant=vaccine_decay_duration / math.log(2), - efficacy_is_multiplicative=False + efficacy_is_multiplicative=True ) else: @@ -645,7 +645,7 @@ def add_vacc_pmc(campaign, start_days: list, coverages: list, target_group: dict vaccine_initial_effect=vaccine_initial_effect, vaccine_box_duration=vaccine_box_duration, vaccine_decay_time_constant=vaccine_decay_duration / math.log(2), - efficacy_is_multiplicative=False + efficacy_is_multiplicative=True ) return {'pmc_cov': sum(coverages) / len(coverages), From 464330263332daaec6086d62257df11b84aaa65f Mon Sep 17 00:00:00 2001 From: Ben Toh Date: Tue, 31 Oct 2023 11:44:02 -0500 Subject: [PATCH 2/2] Cast RTSS touchpoint to float to fix json int64 issue --- snt/hbhi/set_up_interventions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snt/hbhi/set_up_interventions.py b/snt/hbhi/set_up_interventions.py index e68723f..f126a4a 100644 --- a/snt/hbhi/set_up_interventions.py +++ b/snt/hbhi/set_up_interventions.py @@ -531,7 +531,7 @@ def add_epi_rtss(self, campaign, rtss_df): for tp_time_trigger, coverage, vtype, event_name, std, init_eff, decay_t in \ zip(rtss_touchpoints, coverage_levels, rtss_types, rtss_event_names, std_dev_list, initial_effect_list, decay_time_constant_list): - + tp_time_trigger = float(tp_time_trigger) if delay_distribution_name == "LOG_NORMAL_DISTRIBUTION": delay_distribution = {"Delay_Period_Distribution": "LOG_NORMAL_DISTRIBUTION", "Delay_Period_Log_Normal_Mu": (