[docs]defapplication(filename):# This is fragile but let's try it.try:ei=json.loads(open("config.json").read())["parameters"]["Enable_Interventions"]ifei==0:returnexceptExceptionasex:print("Attempt to get Enable_Interventions from config.json failed.")camp_skel=yaml.load(open(filename).read())event_template=json.loads(""" { "Event_Coordinator_Config": { "Demographic_Coverage": 1.0, "Intervention_Config": { "class": "OutbreakIndividual" }, "Target_Demographic": "Everyone", "class": "StandardInterventionDistributionEventCoordinator" }, "Event_Name": "TBD", "Nodeset_Config": { "class": "NodeSetAll" } } """)events=[]event_counter=1forevent_w5incamp_skel["Events"]:new_event=copy.deepcopy(event_template)# new_event["Event_Name"]="Event #"+str(event_counter)event_counter+=1who=event_w5["Who"]whos=who.split(',')#if len(whos) != 3:#print( "'Who' should be comma-separated version of 'X%, A-B, Male'." )#sys.exit()coverage=float(whos[0].rstrip('%'))/100new_event["Event_Coordinator_Config"]["Demographic_Coverage"]=coverage# TBD: if coverage = 1.0, just omit for defaults?ages=whos[1].strip()ifages!="AllAges":age_lims=ages.split('-')min_age=float(age_lims[0])max_age=float(age_lims[1])# TBD: Check for valid valuesnew_event["Event_Coordinator_Config"]["Target_Demographic"]="ExplicitAgeRanges"new_event["Event_Coordinator_Config"]["Target_Age_Min"]=min_agenew_event["Event_Coordinator_Config"]["Target_Age_Max"]=max_agesex=whos[2].strip()ifsex!="BothSexes":# TBD: Check for valid valuesnew_event["Event_Coordinator_Config"]["Target_Demographic"]="ExplicitAgeRangesAndGender"new_event["Event_Coordinator_Config"]["Target_Gender"]=sexiflen(whos)>=4:ips=whos[3].strip()new_event["Event_Coordinator_Config"]["Property_Restrictions"]=[]new_event["Event_Coordinator_Config"]["Property_Restrictions"].append(ips)where=event_w5["Where"]ifwhere!="Everywhere":print("TBD: Need to implement alternatives to NodeSetAll.")when=event_w5["When"]# Convert 'when' into Start_Day or Start_Yearwhens=[when]ifisinstance(when,str)and","inwhen:whens=when.split(',')start=float(whens[0])if(start%1.0==0.0):# StartTimenew_event["class"]="CampaignEvent"new_event["Start_Day"]=int(start)else:# float# StartYearnew_event["class"]="CampaignEventByYear"new_event["Start_Year"]=startother_whens=[]# now we support two kinds of mutiples: repetitions and 'laundry-list'iflen(whens)==2and'*'inwhens[1]:reps=whens[1].split("*")new_event["Event_Coordinator_Config"]["Timesteps_Between_Repetitions"]=int(reps[0])new_event["Event_Coordinator_Config"]["Number_Repetitions"]=int(reps[1])else:other_whens=whens[1:]what=event_w5["What"]iv_class=what["class"]#new_event["Event_Coordinator_Config"]["Intervention_Config"]["class"] = iv_classnew_iv=new_event["Event_Coordinator_Config"]["Intervention_Config"]new_iv["class"]=iv_class# maybe just copy 'payload' kvp's verbatim except for nasty special cases.ifiv_class=="ScaleLarvalHabitat":habitat=what["Habitat"]level=float(what["Level"])lhm={}lhm[habitat]=levelnew_iv["Larval_Habitat_Multiplier"]=lhmelse:forkeyinwhat.keys():ifkey=="Efficacy_Profile":new_iv["Waning_Config"]={}wanings=what["Efficacy_Profile"].split(',')init=float(wanings[1])dur=int(wanings[2])new_iv["Waning_Config"]["class"]="WaningEffect"+wanings[0]new_iv["Waning_Config"]["Initial_Effect"]=initifwanings[0]=="Box":new_iv["Waning_Config"]["Box_Duration"]=durelse:new_iv[key]=what[key]ifiv_class=="SimpleBoosterVaccine":new_iv["Duplicate_Policy"]="ADD"#why = event_w5["Why"] # for eventsevents.append(new_event)iflen(other_whens)>0:forwheninother_whens:new_event=copy.deepcopy(new_event)if"Start_Day"innew_event:new_event["Start_Day"]=float(when)elif"Start_Year"innew_event:new_event["Start_Year"]=float(when)else:print("Error. Didn't find Start_Day or Start_Year in new_event.")events.append(new_event)top_level={}top_level["Use_Defaults"]=1top_level["Events"]=events#print( json.dumps( top_level,sort_keys=True,indent=4 ))withopen("campaign.json","w")ascamp_file:#camp_file.write( str( json.dumps( top_level,sort_keys=True,indent=4 ) ) )json.dump(top_level,camp_file)config_json=json.loads(open("config.json").read())config_json["parameters"]["Campaign_Filename"]="campaign.json"withopen("config.json","w")asconfig_file:json.dump(config_json,config_file)