def test_init_agent(make_agent, make_population, make_model): model = make_model() pop = make_population(n=1) # test allcomers (no prep) pop.pop_random = FakeRandom(0.9) a1 = make_agent(race="black") a2 = make_agent(race="white") assert not a1.prep.active and not a2.prep.active # test allcomers pop.pop_random = FakeRandom(0.4) a1.prep.init_agent(pop, 10) a2.prep.init_agent(pop, 10) assert a1.prep.active assert a2.prep.active # reset prep, change to racial model between black and white init prob a1.prep.active = a2.prep.active = False a1.location.params.prep.target_model = a2.location.params.prep.target_model = [ "Racial" ] a1.prep.init_agent(pop, 10) a2.prep.init_agent(pop, 10) assert a1.prep.active assert not a2.prep.active
def test_get_cumulative_bin(params): bin_def = params.partnership.sex.frequency.Sex.bins rand_gen = FakeRandom(-0.1) assert utils.get_independent_bin(rand_gen, bin_def) == 1 rand_gen = FakeRandom(1.1) assert utils.get_independent_bin(rand_gen, bin_def) == len(bin_def)
def test_partnership_duration(params): # test duration with randint assert get_partnership_duration(params, FakeRandom(1.0), "Inj", "white") == 3 # test duration with bins assert get_partnership_duration(params, FakeRandom(0.1), "Sex", "white") == 1 # test duration with second race assert get_partnership_duration(params, FakeRandom(0.1), "Sex", "black") == 3
def test_update_haart_t1(make_model, make_agent): model = make_model() model.time = 1 a = make_agent(race="white") a.hiv.active = True # nothing happens, not tested a.haart.update_agent(model) assert a.haart.adherent is False assert a.haart.active is False # t0 agent initialized HAART a.hiv.dx = True a.hiv.dx_time = model.time # go on haart model.run_random = FakeRandom( -0.1 ) # means this will always be less than params even though not possible in reality a.haart.update_agent(model) assert a.haart.adherent is True assert a.haart.active # go off haart a.haart.update_agent(model) assert a.haart.adherent is False assert a.haart.active is False # Try haart cap with low cap, one agent on haart and one diagnosed agent. Nothing happens a.location.params.haart.use_cap = True a.haart.counts[a.race][a.sex_type] = 1 a.hiv.dx_counts[a.race][a.sex_type] = 5 a.location.params.demographics[a.race].sex_type[a.sex_type].drug_type[ a.drug_type].haart.prob = 0.1 a.haart.update_agent(model) assert a.haart.active is False assert a.haart.adherent is False # Increase cap. Agent goes on haart a.location.params.demographics[a.race].sex_type[a.sex_type].drug_type[ a.drug_type].haart.cap = 2.0 a.haart.update_agent(model) assert a.haart.active assert a.haart.adherent is True # falls off adherence model.run_random = FakeRandom(1.0) a.location.params.demographics[a.race].sex_type[a.sex_type].drug_type[ a.drug_type].haart.adherence.discontinue = 5.0 a.haart.update_agent(model) assert a.haart.active assert a.haart.adherent is False
def test_injection_get_num_acts_do_nothing(make_model, make_agent): model = make_model() model.time = model.params.hiv.start_time + 2 a = make_agent(race="white", DU="Inj", SO="HM") p_inj = make_agent(race="white", DU="Inj", SO="HF") rel_Inj = Relationship(a, p_inj, 10, bond_type="Inj") model.run_random = FakeRandom(-0.1) assert Injection.get_num_acts(model, rel_Inj) > 0 model.run_random = FakeRandom(1.1) assert Injection.get_num_acts(model, rel_Inj) == 0
def test_knowledge_influence(make_model, make_agent): model = make_model() model.run_random = FakeRandom(-0.1) rel = next(iter(model.pop.relationships)) # get a relationship from the pop rel.agent1.prep.active = False rel.agent2.prep.active = False rel.agent1.hiv.active = False rel.agent2.hiv.active = False rel.agent1.knowledge.opinion = 2 rel.agent2.knowledge.opinion = 5 influence(model, rel) # did agent1's opinion change? otherwise, try the other way if rel.agent1.knowledge.opinion > 2: assert rel.agent1.prep.active else: rel.agent1.knowledge.opinion = 5 rel.agent2.knowledge.opinion = 2 influence(model, rel) assert rel.agent2.prep.active
def test_update_high_risk_incar(make_model, make_agent, make_relationship): model = make_model() model.run_random = FakeRandom(-0.1) a = make_agent() p = make_agent() r = make_relationship(a, p) # try to update when not high risk and not incar assert a.high_risk.update_agent(model) is None # agent incarcerated last time step, partners become high risk a.incar.active = True a.incar.time = model.time - 1 a.incar.release_time = model.time a.high_risk.update_agent(model) assert p.high_risk.active # agent released, becomes high risk model.time += 1 a.high_risk.update_agent(model) assert a.high_risk.active assert a.high_risk.duration == 6
def test_injection_num_acts(make_model, make_agent): model = make_model() model.np_random = FakeRandom(0.5) a = make_agent() p = make_agent() a.drug_type = "Inj" p.drug_type = "Inj" rel = Relationship(a, p, 10, bond_type="Inj") # set to a high number to ensure above zero a.location.params.demographics[a.race].sex_type[ a.sex_type].injection.num_acts = 100 p.location.params.demographics[p.race].sex_type[ p.sex_type].injection.num_acts = 200 assert Injection.get_num_acts(model, rel) > 0 a.syringe_services.active = True SyringeServices.enrolled_risk = 0.0 assert Injection.get_num_acts(model, rel) == 0 a.syringe_services.active = False a.hiv.active = True a.hiv.dx = True model.params.hiv.dx.risk_reduction.injection = 1.0 assert Injection.get_num_acts(model, rel) == 0 assert p.hiv
def test_vaccine_update_agent(make_model, make_agent): model = make_model() model.time = model.params.vaccine.start_time model.run_random = FakeRandom(-0.1) a = make_agent() a.prep.active = True a.vaccine.update_agent(model) assert a.vaccine.active is False a.prep.active = False a.hiv.active = True a.vaccine.update_agent(model) assert a.vaccine.active is False a.hiv.active = False a.vaccine.update_agent(model) assert a.vaccine.active is True assert a.vaccine.time == model.time model.time += (a.location.params.demographics[a.race].sex_type[ a.sex_type].vaccine.booster.interval) a.vaccine.update_agent(model) assert a.vaccine.active assert a.vaccine.time == model.time
def test_hiv_expose(make_model, make_agent): model = make_model() model.run_random = FakeRandom(0.0) # always less than param a = make_agent() p = make_agent() a.partners["Sex"] = set() p.partners["Sex"] = set() rel = Relationship(a, p, 10, bond_type="Sex") HIV.expose(model, "sex", rel, 10) assert a.hiv.active is False assert p.hiv.active is False a.hiv.active = True p.hiv.active = True # test nothing happens HIV.expose(model, "sex", rel, 10) assert a.hiv.active assert p.hiv.active p.hiv.active = False # test conversion happens HIV.expose(model, "sex", rel, 10) assert a.hiv.active assert p.hiv.active
def test_get_random_pwid_partner_w_PWID(make_population, make_agent, params): empty_pop = make_population() idu_agent = make_agent(DU="Inj") idu_partner = make_agent(DU="Inj") for bond in params.classes.bond_types: idu_agent.target_partners[bond] = 0 idu_partner.target_partners[bond] = 0 idu_agent.partners[bond] = set() idu_partner.partners[bond] = set() idu_agent.target_partners["Inj"] = 10 idu_partner.target_partners["Inj"] = 10 empty_pop.add_agent(idu_agent) empty_pop.add_agent(idu_partner) for race in params.classes.races: for bond in copy(params.classes.bond_types): if bond != "Sex": params.demographics[ race].sex_type.MSM.drug_type.Inj.num_partners[bond].vars[ 1].value = 0.0 partner = select_partner( idu_agent, empty_pop.all_agents.members, empty_pop.sex_partners, empty_pop.pwid_agents, params, FakeRandom(1.0), "Inj", ) assert partner == idu_partner
def test_pca_interaction(make_model, make_agent): model = make_model() a = make_agent() p = make_agent() a.knowledge.opinion = 4 p.knowledge.opinion = 2 a.knowledge.active = True a.partners["SexInj"] = set() p.partners["SexInj"] = set() rel = Relationship(a, p, 10, bond_type="SexInj") model.run_random = FakeRandom(-0.1) model.pop.graph.add_edge(a, p) model.pop.graph.add_edge(a, "edge") model.time = 5 # make partner aware via dissemination PCA.interact(model, rel) assert p.knowledge.active # influence partner's opinion model.time += 1 PCA.interact(model, rel) assert p.knowledge.opinion == 3
def test_incarcerate_unincarcerate(make_model, make_agent): model = make_model() model.run_random = FakeRandom(-0.1) a = make_agent() a.target_partners = { bond: 0 for bond in model.params.classes.bond_types.keys() } a.mean_num_partners = copy(a.target_partners) a.incar.active = True a.incar.release_time = model.time + 2 a.hiv.active = True a.haart.active = True model.time += 1 a.incar.update_agent(model) assert a.incar.active model.time += 1 a.incar.update_agent(model) assert a.incar.active is False assert a.haart.active is False assert a.haart.adherent is False
def test_sex_num_acts(make_model, make_agent, make_relationship, params): params.hiv.dx.risk_reduction.sex = 1.0 model = make_model() model.time = model.params.hiv.start_time model.np_random = FakeRandom(1.0) a = make_agent() p = make_agent() a.partners["Sex"] = set() p.partners["Sex"] = set() rel_Sex = Relationship(a, p, 10, bond_type="Sex") assert Sex.get_num_acts(model, rel_Sex) > 0 a.hiv.active = True a.hiv.dx = True # test nothing happens assert Sex.get_num_acts(model, rel_Sex) == 0 a.location.params.partnership.sex.frequency = ( p.location.params.partnership.sex.frequency) = ObjMap({ "Sex": { "type": "distribution", "distribution": { "dist_type": "set_value", "vars": { 1: { "value": 0, "value_type": "int" } }, }, } }) assert Sex.get_num_acts(model, rel_Sex) == 0
def test_incarcerate_diagnosed(make_model, make_agent): model = make_model() model.time = 10 a = make_agent(SO="HM", race="white") # incarceration only for HM and HF? a.hiv.active = True a.hiv.dx = True a.partners["Sex"] = set() model.run_random = FakeRandom(0.0) # always less than params a.incar.update_agent(model) assert a.incar.active assert a.incar.release_time == model.time + 1 assert a.haart.active assert a.haart.adherent is True # Goes on haart but nonadherent a = make_agent(SO="HM", race="white") a.location.params.incar.haart.adherence = -1.0 a.hiv.active = True a.hiv.dx = True a.partners["Sex"] = set() a.incar.update_agent(model) assert a.incar.active assert a.incar.release_time == model.time + 1 assert a.haart.active assert not a.haart.adherent
def test_die_and_replace_all(make_model, params): params.features.incar = False model = make_model(params) model.run_random = FakeRandom(0.0000001) # always lower than death rate baseline_pop = copy(model.pop.all_agents.members) old_ids = [a.id for a in baseline_pop] num_hm = len([x for x in baseline_pop if x.sex_type == "HM"]) num_white = len([x for x in baseline_pop if x.race == "white"]) num_pwid = len([x for x in baseline_pop if x.drug_type == "Inj"]) model.die_and_replace() assert num_hm == len( [x for x in model.pop.all_agents.members if x.sex_type == "HM"]) assert num_white == len( [x for x in model.pop.all_agents.members if x.race == "white"]) assert num_pwid == len( [x for x in model.pop.all_agents.members if x.drug_type == "Inj"]) new_ids = [a.id for a in model.pop.all_agents.members] death_ids = [a.id for a in model.deaths] for agent in model.pop.all_agents.members: assert agent.id not in old_ids assert agent in model.pop.graph.nodes() for agent in baseline_pop: assert agent.id not in new_ids assert agent not in model.pop.graph.nodes() assert agent.id in death_ids
def test_vaccine_init_agent(make_population): pop = make_population(n=0) pop.params.hiv.start_time = 10 loc = pop.geography.locations["world"] loc.params.vaccine.on_init = True pop.pop_random = FakeRandom(-0.1) a = pop.create_agent(loc, "white", 0) assert a.vaccine.active assert a.vaccine.time == 0 assert a.vaccine.type != "" pop.pop_random = FakeRandom(1.0) b = pop.create_agent(loc, "white", 0) assert b.vaccine.active is False assert b.vaccine.time is None assert b.vaccine.type == ""
def test_initiate_prep_force_non_adh(make_model, make_agent): model = make_model() a = make_agent() # forcing, non-adherant, inj model.run_random = FakeRandom(1.0) a.prep.initiate(model, True) assert a.prep.active assert a.prep.adherent is False assert a.prep.last_dose_time == model.time
def test_knowledge_convert(make_model, make_agent): model = make_model() a = make_agent() a.knowledge.opinion = 0 model.run_random = FakeRandom(1) a.knowledge.convert(model) assert a.knowledge.active assert not a.prep.active a.knowledge.active = False a.knowledge.opinion = 5 model.run_random = FakeRandom(-0.1) a.knowledge.convert(model) assert a.knowledge.active assert a.prep.active
def test_die_and_replace_none(make_model): model = make_model() model.run_random = FakeRandom(0.999) # always greater than death rate baseline_pop = copy(model.pop.all_agents.members) model.die_and_replace() ids = [a.id for a in baseline_pop] for agent in model.pop.all_agents.members: assert agent.id in ids
def test_injection_transmission(make_model, make_agent): model = make_model() model.np_random = FakeRandom(1.0) model.run_random = FakeRandom(-0.1) model.time = model.params.hiv.start_time + 2 a = make_agent(race="black", DU="Inj", SO="HM") p = make_agent(race="black", DU="Inj", SO="HF") rel = Relationship(a, p, 10, bond_type="Inj") Injection.interact(model, rel) assert a.hiv.active is False assert p.hiv.active is False a.hiv.active = True a.hiv.time = model.time - 1 # acute Injection.interact(model, rel) assert p.hiv.active
def test_pca_num_acts_bins(make_model, make_agent): model = make_model() model.run_random = FakeRandom(-0.1) a = make_agent() p = make_agent() rel = Relationship(a, p, 10, bond_type="Social") assert (PCA.get_num_acts( model, rel) == model.params.partnership.pca.frequency.Social.bins[1].min)
def test_safe_dist(): rand_gen = np.random.RandomState(123) dist_info = ObjMap( {"dist_type": "poisson", "vars": {1: {"value": 20, "value_type": "int"}}} ) # numpy dist assert utils.safe_dist(dist_info, rand_gen) > 0 # custom dist dist_info["dist_type"] = "set_value" assert utils.safe_dist(dist_info, rand_gen) == 20 # not a real distribution dist_info["dist_type"] = "fake_dist" with pytest.raises(AttributeError) as excinfo: utils.safe_dist(dist_info, rand_gen) assert "Distribution type fake_dist not found!" in str(excinfo) # pert low = 2 peak = 5 high = 100 temp = 4 pert_info = ObjMap( { "dist_type": "pert", "vars": { 1: {"value": low, "value_type": "int"}, 2: {"value": peak, "value_type": "int"}, 3: {"value": high, "value_type": "int"}, 4: {"value": temp, "value_type": "int"}, }, } ) val = utils.safe_dist(pert_info, rand_gen) assert val > low assert val < high # weibull_modified (2-param weibull) rand_gen = FakeRandom(0.5) shape = 0.5 scale = 11 weibull_info = ObjMap( { "dist_type": "weibull_modified", "vars": { 1: {"value": shape, "value_type": "float"}, 2: {"value": scale, "value_type": "float"}, }, } ) assert utils.safe_dist(weibull_info, rand_gen) == 5.284983153100216
def test_enroll_prep_one(make_agent, params): rand_gen = FakeRandom(1.1) a = make_agent() a.location.params.prep.type = ["Oral"] a.prep.enroll(rand_gen, 0) assert a.prep.active assert a.prep.last_dose_time == 0 assert a.prep.adherent is False assert a.prep.type == "Oral"
def test_enroll_prep_choice(make_agent, params): rand_gen = FakeRandom(-0.1) a = make_agent() a.location.params.prep.type = ["Oral", "Inj"] a.prep.enroll(rand_gen, 0) assert a.prep.active assert a.prep.last_dose_time == 0 assert a.prep.adherent is True assert a.prep.type == "Inj"
def test_get_random_sex_partner_valid(make_population, make_agent, params): empty_pop = make_population() hm_agent = make_agent(SO="HM") hf_partner = make_agent(SO="HF") for bond in params.classes.bond_types: hm_agent.target_partners[bond] = 0 hf_partner.target_partners[bond] = 0 hm_agent.partners[bond] = set() hf_partner.partners[bond] = set() empty_pop.add_agent(hm_agent) empty_pop.add_agent(hf_partner) hm_agent.target_partners = 10 hf_partner.target_partners = 10 partner = select_partner( hm_agent, empty_pop.all_agents.members, empty_pop.sex_partners, empty_pop.pwid_agents, params, FakeRandom(1.0), "Sex", ) assert partner == hf_partner rel = Relationship(partner, hm_agent, 10, "Sex") empty_pop.add_relationship(rel) # no match after bonded partner = select_partner( hm_agent, empty_pop.all_agents.members, empty_pop.sex_partners, empty_pop.pwid_agents, params, FakeRandom(1.0), "Sex", ) assert partner is None
def test_external_exposure(make_model, make_agent, params): params.features.external_exposure = True model = make_model(params) external_exposure_agent = make_agent() external_exposure_agent.external_exposure.active = True model.pop.add_agent(external_exposure_agent) model.run_random = FakeRandom(-0.1) assert external_exposure_agent.hiv.active is False external_exposure_agent.external_exposure.update_agent(model) assert external_exposure_agent.hiv.active
def test_hiv_convert(make_model, make_agent): model = make_model() a = make_agent() a.prep.active = True model.run_random = FakeRandom(-0.1) a.hiv.convert(model) assert a.hiv.active assert a.hiv.time == model.time assert a in HIV.agents assert a.prep.active is False
def test_initialize_random_trial_prep_all(make_model, params): params.features.prep = True params.vaccine.on_init = False params.prep.cap = 0 params.random_trial.choice = "all" model = make_model(params) model.run_random = FakeRandom(-0.1) model.time = model.params.random_trial.start_time RandomTrial.update_pop(model) for agent in model.pop.all_agents: assert agent.random_trial.active if not agent.hiv.active: assert agent.random_trial.treated assert agent.prep.active
def test_diagnose_hiv(make_model, make_agent): model = make_model() model.params.partner_tracing.prob = 1.0 model.time = 1 a = make_agent() p = make_agent() p.hiv.active = True a.partners["Sex"].add(p) model.run_random = FakeRandom(-0.1) # always less than param a.hiv.diagnose(model) assert a.hiv.dx assert a.hiv.dx_time == model.time