Skip to content

Commit 829007e

Browse files
Sbachmei/mic 6736/vivarium 4 interventions (#130)
1 parent 8e2a387 commit 829007e

File tree

7 files changed

+113
-117
lines changed

7 files changed

+113
-117
lines changed

src/vivarium_gates_nutrition_optimization/components/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from .intervention import MaternalInterventions
33
from .maternal_bmi import MaternalBMIExposure
44
from .maternal_disorders import MaternalDisorders, MaternalHemorrhage
5-
from .morbidity import BackgroundMorbidity
5+
6+
# from .morbidity import BackgroundMorbidity
67
from .mortality import MaternalMortality
78
from .observers import (
89
AnemiaObserver,

src/vivarium_gates_nutrition_optimization/components/disease.py

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -60,49 +60,52 @@ def _probability(self, index) -> pd.Series:
6060
return self.population_view.get_attributes(index, self.pipeline_name)
6161

6262

63-
class ParturitionExclusionState(DiseaseState):
64-
65-
# #####################
66-
# # Lifecycle methods #
67-
# #####################
68-
69-
def get_disability_weight_pipeline(self, builder: Builder) -> None:
70-
builder.value.register_attribute_producer(
71-
f"{self.state_id}.disability_weight",
72-
source=self.compute_disability_weight,
73-
required_resources=[
74-
"is_alive",
75-
"pregnancy",
76-
self.model,
77-
self.lookup_tables["disability_weight"],
78-
],
79-
)
80-
81-
##################################
82-
# Pipeline sources and modifiers #
83-
##################################
84-
85-
def compute_disability_weight(self, index: pd.Index) -> pd.Series:
86-
disability_weight = pd.Series(0, index=index)
87-
raw_disability_weight = pd.Series(0, index=index)
88-
with_condition = self.with_condition(index)
89-
# FIXME: this is broken (self.base_disability_weight is undefined)
90-
raw_disability_weight.loc[with_condition] = self.base_disability_weight(
91-
with_condition
92-
)
93-
94-
dw_map = {
95-
models.NOT_PREGNANT_STATE_NAME: raw_disability_weight,
96-
models.PREGNANT_STATE_NAME: raw_disability_weight,
97-
## Pause YLD accumulation during the parturition state
98-
models.PARTURITION_STATE_NAME: pd.Series(0, index=index),
99-
models.POSTPARTUM_STATE_NAME: raw_disability_weight,
100-
}
101-
102-
pop = self.population_view.get(index)
103-
alive = pop["is_alive"] == True
104-
for state, dw in dw_map.items():
105-
in_state = alive & (pop["pregnancy"] == state)
106-
disability_weight[in_state] = dw.loc[in_state]
107-
108-
return disability_weight
63+
# NOTE: This component is only used by Morbidity which is an exploratory component
64+
# and not fully functional. At the very least, this class has not been updated
65+
# to work with the vivarium v4.0 / vph v5.0. Commenting it out.
66+
# class ParturitionExclusionState(DiseaseState):
67+
68+
# # #####################
69+
# # # Lifecycle methods #
70+
# # #####################
71+
72+
# def get_disability_weight_pipeline(self, builder: Builder) -> None:
73+
# builder.value.register_attribute_producer(
74+
# f"{self.state_id}.disability_weight",
75+
# source=self.compute_disability_weight,
76+
# required_resources=[
77+
# "is_alive",
78+
# "pregnancy",
79+
# self.model,
80+
# self.lookup_tables["disability_weight"],
81+
# ],
82+
# )
83+
84+
# ##################################
85+
# # Pipeline sources and modifiers #
86+
# ##################################
87+
88+
# def compute_disability_weight(self, index: pd.Index) -> pd.Series:
89+
# disability_weight = pd.Series(0, index=index)
90+
# raw_disability_weight = pd.Series(0, index=index)
91+
# with_condition = self.with_condition(index)
92+
# # FIXME: this is broken (self.base_disability_weight is undefined)
93+
# raw_disability_weight.loc[with_condition] = self.base_disability_weight(
94+
# with_condition
95+
# )
96+
97+
# dw_map = {
98+
# models.NOT_PREGNANT_STATE_NAME: raw_disability_weight,
99+
# models.PREGNANT_STATE_NAME: raw_disability_weight,
100+
# ## Pause YLD accumulation during the parturition state
101+
# models.PARTURITION_STATE_NAME: pd.Series(0, index=index),
102+
# models.POSTPARTUM_STATE_NAME: raw_disability_weight,
103+
# }
104+
105+
# pop = self.population_view.get(index)
106+
# alive = pop["is_alive"] == True
107+
# for state, dw in dw_map.items():
108+
# in_state = alive & (pop["pregnancy"] == state)
109+
# disability_weight[in_state] = dw.loc[in_state]
110+
111+
# return disability_weight

src/vivarium_gates_nutrition_optimization/components/intervention.py

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ class MaternalInterventions(Component):
2525
}
2626
}
2727

28-
@property
29-
def columns_created(self) -> List[str]:
30-
return ["intervention"]
31-
32-
@property
33-
def columns_required(self) -> List[str]:
34-
return ["maternal_bmi_anemia_category", "tracked"]
35-
36-
@property
37-
def initialization_requirements(self) -> Dict[str, List[str]]:
38-
return {"requires_streams": [self.name], "requires_columns": self.columns_required}
39-
4028
# noinspection PyAttributeOutsideInit
4129
def setup(self, builder: Builder) -> None:
4230
self.clock = builder.time.clock()
@@ -57,42 +45,46 @@ def setup(self, builder: Builder) -> None:
5745
data_keys.MATERNAL_INTERVENTIONS.IFA_EFFECT_SIZE
5846
).value[0]
5947

60-
builder.value.register_value_modifier(
48+
builder.value.register_attribute_modifier(
6149
"hemoglobin.exposure",
6250
self.update_exposure,
63-
requires_columns=self.columns_created,
51+
required_resources=["intervention"],
6452
)
6553

66-
builder.value.register_value_modifier(
54+
builder.value.register_attribute_modifier(
6755
"birth_outcome_probabilities",
6856
self.adjust_stillbirth_probability,
69-
requires_columns=self.columns_created,
57+
required_resources=["intervention"],
7058
)
7159

72-
def on_initialize_simulants(self, pop_data: SimulantData) -> None:
73-
pop = self.population_view.subview(["maternal_bmi_anemia_category"]).get(
74-
pop_data.index
60+
builder.population.register_initializer(
61+
self.initialize_intervention,
62+
"intervention",
63+
required_resources=[self.randomness, "maternal_bmi_anemia_category"],
64+
)
65+
66+
def initialize_intervention(self, pop_data: SimulantData) -> None:
67+
categories = self.population_view.get_attributes(
68+
pop_data.index, "maternal_bmi_anemia_category"
7569
)
7670

7771
if self.scenario == "ifa":
7872
pop_update = pd.DataFrame(
7973
{"intervention": "ifa"},
80-
index=pop.index,
74+
index=categories.index,
8175
)
8276
else:
8377
pop_update = pd.DataFrame(
8478
{"intervention": None},
85-
index=pop.index,
79+
index=categories.index,
8680
)
8781
baseline_ifa = self.randomness.choice(
88-
pop.index,
82+
categories.index,
8983
choices=[models.IFA_SUPPLEMENTATION, models.NO_TREATMENT],
9084
p=[self.ifa_coverage, RESIDUAL_CHOICE],
9185
additional_key="baseline_ifa",
9286
)
93-
low_bmi = pop["maternal_bmi_anemia_category"].isin(
94-
[models.LOW_BMI_NON_ANEMIC, models.LOW_BMI_ANEMIC]
95-
)
87+
low_bmi = categories.isin([models.LOW_BMI_NON_ANEMIC, models.LOW_BMI_ANEMIC])
9688
coverage = data_values.INTERVENTION_SCENARIO_COVERAGE.loc[self.scenario]
9789
pop_update["intervention"] = np.where(
9890
low_bmi, coverage["low_bmi"], coverage["normal_bmi"]
@@ -107,24 +99,24 @@ def update_exposure(self, index, exposure):
10799
if self.clock() - self.start_date >= timedelta(
108100
days=data_values.DURATIONS.INTERVENTION_DELAY_DAYS
109101
):
110-
pop = self.population_view.get(index)
111-
exposure.loc[pop["intervention"] == models.NO_TREATMENT] -= (
102+
intervention = self.population_view.get_attributes(index, "intervention")
103+
exposure.loc[intervention == models.NO_TREATMENT] -= (
112104
self.ifa_coverage * self.ifa_effect_size
113105
)
114-
exposure.loc[pop["intervention"] != models.NO_TREATMENT] += (
106+
exposure.loc[intervention != models.NO_TREATMENT] += (
115107
1 - self.ifa_coverage
116108
) * self.ifa_effect_size
117109

118110
return exposure
119111

120112
def adjust_stillbirth_probability(self, index, birth_outcome_probabilities):
121-
pop = self.population_view.subview(["intervention"]).get(index)
113+
pop = self.population_view.get_attributes(index, "intervention")
122114
rrs = {
123115
models.MMS_SUPPLEMENTATION: self.mms_stillbirth_rr,
124116
models.BEP_SUPPLEMENTATION: self.bep_stillbirth_rr,
125117
}
126118
for intervention, rr in rrs.items():
127-
on_treatment = pop["intervention"] == intervention
119+
on_treatment = pop == intervention
128120
# Add spare probability onto live births first
129121
birth_outcome_probabilities.loc[
130122
on_treatment, models.LIVE_BIRTH_OUTCOME
Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
from vivarium_public_health.disease import DiseaseModel, SusceptibleState
1+
# from vivarium_public_health.disease import DiseaseModel, SusceptibleState
22

3-
from vivarium_gates_nutrition_optimization.components.disease import (
4-
ParturitionExclusionState,
5-
)
3+
# from vivarium_gates_nutrition_optimization.components.disease import (
4+
# ParturitionExclusionState,
5+
# )
66

7+
# NOTE: I have not updated this component to work with the new lookup table
8+
# configuration work so this component is not used in the model spec because
9+
# it is currently broken. This component was an exploratory component and may
10+
# be revisited in the future - albrja
11+
# NOTE: This has also not been updated to work with the vivarium v4.0 / vph v5.0.
12+
# Commenting it out. - sbachmei
713

8-
def BackgroundMorbidity():
9-
# NOTE: I have not updated this component to work with the new lookup table
10-
# configuration work so this component is not used in the model spec because
11-
# it is currently broken. This component was an exploratory component and may
12-
# be revisited in the future - albrja
13-
cause = "other_causes"
14-
susceptible = SusceptibleState(cause)
15-
with_condition = ParturitionExclusionState(
16-
cause,
17-
allow_self_transition=True,
18-
get_data_functions={
19-
"prevalence": lambda *_: 1.0,
20-
"excess_mortality_rate": lambda *_: 0.0,
21-
},
22-
)
23-
return DiseaseModel(
24-
cause,
25-
states=[susceptible, with_condition],
26-
get_data_functions={"cause_specific_mortality_rate": lambda *_: 0.0},
27-
)
14+
15+
# def BackgroundMorbidity():
16+
# cause = "other_causes"
17+
# susceptible = SusceptibleState(cause)
18+
# with_condition = ParturitionExclusionState(
19+
# cause,
20+
# allow_self_transition=True,
21+
# get_data_functions={
22+
# "prevalence": lambda *_: 1.0,
23+
# "excess_mortality_rate": lambda *_: 0.0,
24+
# },
25+
# )
26+
# return DiseaseModel(
27+
# cause,
28+
# states=[susceptible, with_condition],
29+
# get_data_functions={"cause_specific_mortality_rate": lambda *_: 0.0},
30+
# )

src/vivarium_gates_nutrition_optimization/components/observers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def register_observations(self, builder: Builder) -> None:
130130
name=f"person_time_maternal_bmi_anemia",
131131
pop_filter=f"is_alive == True",
132132
when="time_step__prepare",
133-
requires_attributes=["is_alive", "maternal_bmi_anemia_category"],
133+
requires_attributes=["maternal_bmi_anemia_category"],
134134
additional_stratifications=builder.configuration.stratification.maternal_bmi.include,
135135
excluded_stratifications=builder.configuration.stratification.maternal_bmi.exclude,
136136
aggregator=partial(aggregate_state_person_time, builder.time.step_size()()),
@@ -176,11 +176,11 @@ def register_observations(self, builder: Builder) -> None:
176176
builder=builder,
177177
name="intervention_count",
178178
pop_filter=(
179-
'alive == "alive" and tracked == True and '
179+
"is_alive == True and "
180180
f'event_time > "{intervention_date}" and '
181181
f'event_time <= "{intervention_date + builder.time.step_size()()}"'
182182
),
183-
requires_columns=["alive", "intervention", "event_time"],
183+
requires_attributes=["intervention"],
184184
additional_stratifications=builder.configuration.stratification.maternal_intervention.include,
185185
excluded_stratifications=builder.configuration.stratification.maternal_intervention.exclude,
186186
)
@@ -225,7 +225,6 @@ def register_observations(self, builder: Builder) -> None:
225225
self.register_adding_observation(
226226
builder=builder,
227227
name=f"pregnancy_outcome_count",
228-
pop_filter="",
229228
requires_attributes=["pregnancy_outcome"],
230229
additional_stratifications=builder.configuration.stratification.pregnancy_outcome.include,
231230
excluded_stratifications=builder.configuration.stratification.pregnancy_outcome.exclude,
@@ -296,7 +295,7 @@ def register_observations(self, builder: Builder) -> None:
296295
f"and previous_pregnancy == '{models.PREGNANT_STATE_NAME}' "
297296
f"and pregnancy == '{models.PARTURITION_STATE_NAME}'"
298297
),
299-
requires_columns=list(self.COL_MAPPING),
298+
requires_attributes=list(self.COL_MAPPING),
300299
results_formatter=self.format,
301300
)
302301

src/vivarium_gates_nutrition_optimization/components/pregnancy.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ def sample_full_term_durations(self, full_term_pop: pd.Index) -> pd.DataFrame:
134134
)
135135
return child_status
136136

137-
def get_dwell_time_pipeline(self, builder: Builder) -> Pipeline:
138-
return builder.value.register_value_producer(
139-
f"{self.state_id}.dwell_time",
140-
source=lambda index: self.population_view.get(index)["pregnancy_duration"],
141-
requires_columns=["age", "sex", "pregnancy_outcome"],
142-
)
143-
144137
def get_initial_event_times(self, pop_data: SimulantData) -> pd.DataFrame:
145138
"""Overwrite the BaseDiseaseState method"""
146139
return pd.DataFrame(

src/vivarium_gates_nutrition_optimization/model_specifications/model_spec.yaml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ components:
1616
- Hemoglobin()
1717
- Anemia()
1818
- MaternalBMIExposure()
19-
# - MaternalInterventions() # run 3
19+
- MaternalInterventions()
2020
- ResultsStratifier()
2121
- PregnancyObserver()
2222
- PregnancyOutcomeObserver()
23-
# - BirthObserver() # run 4
23+
- BirthObserver()
2424
- MaternalMortalityObserver()
2525
- DisabilityObserver()
2626
- AnemiaObserver()
2727
- MaternalBMIObserver()
28-
# - MaternalInterventionObserver() # run 3
28+
- MaternalInterventionObserver()
2929

3030
configuration:
3131
input_data:
@@ -35,7 +35,12 @@ configuration:
3535
order: 0
3636
extrapolate: True
3737
intervention:
38-
scenario: "baseline"
38+
scenario: baseline
39+
# scenario: zero_coverage
40+
# scenario: mms
41+
# scenario: ifa
42+
# scenario: targeted_bep_ifa
43+
# scenario: targeted_bep_mms
3944
randomness:
4045
map_size: 1_000_000
4146
key_columns: ['entrance_time', 'age']

0 commit comments

Comments
 (0)