Skip to content

Commit

Permalink
Merge pull request #234 from pph-collective/dissolve-relationships
Browse files Browse the repository at this point in the history
Dissolve relationships
  • Loading branch information
s-bessey authored May 18, 2023
2 parents 2e88350 + 996fc74 commit 8c7c653
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "titan-model"
version = "3.2.1"
version = "3.3.0"
description = "TITAN Agent Based Model"
license = "GPL-3.0-only"
authors = ["Sam Bessey <[email protected]>", "Mary McGrath <[email protected]>"]
Expand All @@ -12,11 +12,11 @@ classifiers = ["Operating System :: OS Independent"]
packages = [{ include = "titan" }]

[tool.poetry.dependencies]
python = "^3.8"
python = "^3.6"
paraml= "^0.1"
networkx = "^2.4"
nanoid = "^2.0"
numpy = "^1.23"
numpy = "^1.18"
black = {version = "^23.1.0", optional = true}
flake8 = {version = "^3.8", optional = true}
mypy = {version = "^1.0.0", optional = true}
Expand Down
33 changes: 33 additions & 0 deletions tests/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,39 @@ def test_static_network(make_model_integration, tmpdir):
assert rel in curr_rel_ids


@pytest.mark.integration_deterministic
def test_dissolution(params_integration, tmpdir):
model = TITAN(params_integration)
inj_r = 0
for rel in model.pop.relationships:
inj_r += 1
assert inj_r > 0
model.time = 1

model.params.partnership.dissolve.time = 1
model.params.partnership.dissolve.enabled = True

model.params.demographics.white.sex_type.MSM.drug_type.Inj.num_partners = (
model.params.demographics.black.sex_type.MSM.drug_type.Inj.num_partners
)
for agent in model.pop.all_agents:
agent.mean_num_partners["Inj"] = -1
agent.mean_num_partners["Sex"] = -1
agent.mean_num_partners["SexInj"] = -1
agent.mean_num_partners["Social"] = -1
model.pop.update_partner_targets()

model.step(tmpdir)

for rel in model.pop.relationships:
if rel.bond_type == "Inj":
print(rel.agent1.mean_num_partners)
print(rel.agent2.mean_num_partners)
print(rel.agent1.race, rel.agent1.sex_type, rel.agent1.drug_type)
print(rel.agent2.race, rel.agent2.sex_type, rel.agent2.drug_type)
assert rel.bond_type != "Inj"


@pytest.mark.integration_deterministic
def test_incar(params_integration, tmpdir):
# turn on incar - initi is set to 0, so for these purposes, just run time
Expand Down
19 changes: 18 additions & 1 deletion tests/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_model_init(params):


@pytest.mark.unit
def test_update_all_agents(make_model, make_agent):
def test_update_all_agents(make_model, make_agent, make_relationship):
# make agent 0
model = make_model()
assert model.params.agent_zero.interaction_type == "injection"
Expand All @@ -57,6 +57,23 @@ def test_update_all_agents(make_model, make_agent):

assert "No agent zero!" in str(excinfo)

# check that model dissolves relationships
model.params.features.agent_zero = False
model.params.partnership.dissolve.enabled = True
model.params.partnership.dissolve.time = 1
model.time = 1
a = make_agent()
p = make_agent()
r = make_relationship(a, p)
model.pop.add_relationship(r)
a.target_partners["Sex"] = 0
a.target_partners["SexInj"] = 0
a.target_partners["Inj"] = 0

assert a.has_partners() is True
model.update_all_agents()
assert a.has_partners() is False


@pytest.mark.unit
def test_death_none(make_model):
Expand Down
8 changes: 7 additions & 1 deletion titan/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,13 @@ def update_all_agents(self):
# If static network, ignore relationship progression
if not self.params.features.static_network:
for rel in copy(self.pop.relationships):
if rel.progress():
if (
self.params.partnership.dissolve.enabled
and self.time == self.params.partnership.dissolve.time
):
rel.progress(force=True)
self.pop.remove_relationship(rel)
elif rel.progress():
self.pop.remove_relationship(rel)

if self.params.features.exit_enter:
Expand Down
7 changes: 7 additions & 0 deletions titan/params/partnership.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,10 @@ partnership:
description: Probability that for a given partnering attempt, the agent tries to partner with only other agents in their component. Otherwise, the agent tries to partner with agents from any component. Network must be enabled.
min: 0
max: 1
dissolve:
time:
type: int
default: -9999
enabled:
type: bool
default: false

0 comments on commit 8c7c653

Please sign in to comment.