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_update_all_agents(make_model, make_agent): # make agent 0 model = make_model() assert model.params.agent_zero.interaction_type == "injection" a = make_agent(race="white", DU="Inj") p = make_agent(race="white", DU="Inj") # make sure at least 1 relationship is compatible with agent 0 type Relationship(a, p, 10, bond_type="Inj") model.time = 1 # update all agents passes with agent 0 model.update_all_agents() assert a.knowledge.active is False # remove all bonds compatible with agent 0. agent 0 fails for rel in copy(model.pop.relationships): if rel.bond_type in ["Inj", "SexInj"]: rel.unbond() model.pop.remove_relationship(rel) # no target partners available for agent in model.pop.all_agents: for k in agent.target_partners.keys(): agent.target_partners[k] = 0 model.reset_trackers() with pytest.raises(ValueError) as excinfo: model.update_all_agents() assert "No agent zero!" in str(excinfo)
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_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_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_knolwedge_transmission_probability(make_model, make_agent): model = make_model() a = make_agent() p = make_agent() rel = Relationship(a, p, 10, bond_type="Social") # knowledge transmission assert a.knowledge.get_transmission_probability(model, "pca", p, 0) == 0 assert ( a.knowledge.get_transmission_probability(model, "pca", p, 1) == model.params.knowledge.prob ) assert a.knowledge.get_transmission_probability( model, "pca", p, 2 ) == 1.0 - utils.binom_0(2, model.params.knowledge.prob) # opinion transmission a.knowledge.active = True p.knowledge.active = True assert a.knowledge.get_transmission_probability(model, "pca", p, 0) == 0 assert ( a.knowledge.get_transmission_probability(model, "pca", p, 1) == model.params.knowledge.opinion.prob ) assert a.knowledge.get_transmission_probability( model, "pca", p, 2 ) == 1.0 - utils.binom_0(2, model.params.knowledge.opinion.prob)
def test_incarcerate_not_hiv(make_model, make_agent): model = make_model() a = make_agent(SO="HM", race="white") p = make_agent(SO="HF", race="white") rel = Relationship(a, p, 10, "Sex") a.location.params.demographics.white.sex_type.HM.incar.prob = 1.0 a.incar.update_agent(model) assert a.incar.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_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_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_incarcerate_not_diagnosed(make_model, make_agent): model = make_model() model.time = model.params.partner_tracing.start_time + 1 a = make_agent(SO="HM", race="white") # incarceration only for HM and HF? a.hiv.active = True a.hiv.time = model.time - 1 a.partners["Sex"] = set() p = make_agent(SO="HF") p.partners["Sex"] = set() rel = Relationship(a, p, 10, bond_type="Sex") 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.hiv.dx
def test_initiate_prep_eligible_racial(make_model, make_agent): model = make_model() # make sure there's room to add more prep agents a = make_agent(SO="HF") # model is "CDCwomen" p = make_agent(DU="Inj") a.partners["Sex"] = set() p.partners["Sex"] = set() p.hiv.dx = True p.external_exposure.active = True model.time = 10 a.location.params.prep.cap = 1.0 a.location.params.prep.target_model = ["Racial"] rel = Relationship(a, p, 10, bond_type="Sex") # non-forcing, adherant, inj model.run_random = FakeRandom(-0.1) a.prep.initiate(model) assert a.prep.active assert a.prep.adherent is True assert a.prep.last_dose_time == 10 assert a.prep.time == 10
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_pca_num_acts_dist(make_model, make_agent): model = make_model() model.params.partnership.pca.frequency = ObjMap({ "Social": { "type": "distribution", "distribution": { "dist_type": "set_value", "vars": { 1: { "value": 0, "value_type": "int" } }, }, } }) a = make_agent() p = make_agent() rel = Relationship(a, p, 10, bond_type="Social") model.run_random = FakeRandom(-0.1) assert PCA.get_num_acts(model, rel) == 0
def _make_relationship(id1, id2, bond_type="Sex", duration=2): return Relationship(id1, id2, duration, bond_type)
def test_sex_transmission(make_model, make_agent): model = make_model() model.time = model.params.hiv.start_time a = make_agent() a.sex_role = "insertive" p = make_agent() p.sex_role = "receptive" a.partners["Sex"] = set() p.partners["Sex"] = set() rel = Relationship(a, p, 10, bond_type="Sex") a.hiv.active = True a.hiv.time = model.time # acute rel.total_sex_acts = 0 model.params.calibration.acquisition = 10 model.params.calibration.acquisition = 5 model.params.calibration.sex.act = 10 model.run_random = FakeRandom(0.6) a.location.params.partnership.sex.frequency = ObjMap({ "Sex": { "type": "bins", "bins": { 1: { "prob": 1.0, "min": 10, "max": 37 } } } }) p.location.params.partnership.sex.frequency = ObjMap({ "Sex": { "type": "bins", "bins": { 1: { "prob": 1.0, "min": 10, "max": 37 } } } }) # test partner becomes Sex.interact(model, rel) assert p.hiv.active p.hiv.active = False a.location.params.partnership.sex.frequency = ( p.location.params.partnership.sex.frequency) = ObjMap({ "Sex": { "type": "distribution", "distribution": { "dist_type": "set_value", "vars": { 1: { "value": 1, "value_type": "int" } }, }, } }) Sex.interact(model, rel) assert p.hiv.active # before hiv start time p.hiv.active = False model.time = model.params.hiv.start_time - 1 Sex.interact(model, rel) assert not p.hiv.active