-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Albrja/mic 5371/maternal sepsis tree #11
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from __future__ import annotations | ||
|
||
import pandas as pd | ||
from vivarium import Component | ||
from vivarium.framework.engine import Builder | ||
from vivarium.framework.event import Event | ||
from vivarium.framework.population import SimulantData | ||
|
||
from vivarium_gates_mncnh.constants import data_keys | ||
from vivarium_gates_mncnh.constants.data_values import ( | ||
COLUMNS, | ||
PREGNANCY_OUTCOMES, | ||
SIMULATION_EVENT_NAMES, | ||
) | ||
from vivarium_gates_mncnh.constants.metadata import ARTIFACT_INDEX_COLUMNS | ||
from vivarium_gates_mncnh.utilities import get_location | ||
|
||
|
||
class MaternalSepsis(Component): | ||
@property | ||
def configuration_defaults(self) -> dict: | ||
return {self.name: {"data_sources": {"incidence_risk": self.load_incidence_risk}}} | ||
|
||
@property | ||
def columns_created(self): | ||
return [COLUMNS.MATERNAL_SEPSIS] | ||
|
||
@property | ||
def columns_required(self): | ||
return [COLUMNS.PREGNANCY_OUTCOME] | ||
|
||
def setup(self, builder: Builder) -> None: | ||
self._sim_step_name = builder.time.simulation_event_name() | ||
self.randomness = builder.randomness.get_stream(self.name) | ||
self.location = get_location(builder) | ||
|
||
def on_initialize_simulants(self, pop_data: SimulantData) -> None: | ||
anc_data = pd.DataFrame( | ||
{ | ||
COLUMNS.MATERNAL_SEPSIS: False, | ||
}, | ||
index=pop_data.index, | ||
) | ||
|
||
self.population_view.update(anc_data) | ||
|
||
def on_time_step(self, event: Event) -> None: | ||
if self._sim_step_name() != SIMULATION_EVENT_NAMES.MATERNAL_SEPSIS: | ||
return | ||
|
||
pop = self.population_view.get(event.index) | ||
full_term = pop.loc[ | ||
pop[COLUMNS.PREGNANCY_OUTCOME].isin( | ||
[PREGNANCY_OUTCOMES.STILLBIRTH_OUTCOME, PREGNANCY_OUTCOMES.LIVE_BIRTH_OUTCOME] | ||
) | ||
] | ||
sepsis_risk = self.lookup_tables["incidence_risk"](full_term.index) | ||
got_sepsis = self.randomness.filter_for_probability( | ||
full_term.index, | ||
sepsis_risk, | ||
"got_sepsis_choice", | ||
) | ||
pop.loc[got_sepsis, COLUMNS.MATERNAL_SEPSIS] = True | ||
self.population_view.update(pop) | ||
|
||
def load_incidence_risk(self, builder: Builder) -> pd.DataFrame: | ||
raw_incidence = builder.data.load( | ||
data_keys.MATERNAL_SEPSIS.RAW_INCIDENCE_RATE | ||
).set_index(ARTIFACT_INDEX_COLUMNS) | ||
asfr = builder.data.load(data_keys.PREGNANCY.ASFR).set_index(ARTIFACT_INDEX_COLUMNS) | ||
sbr = ( | ||
builder.data.load(data_keys.PREGNANCY.SBR) | ||
.set_index("year_start") | ||
.drop(columns=["year_end"]) | ||
.reindex(asfr.index, level="year_start") | ||
) | ||
birth_rate = (sbr + 1) * asfr | ||
incidence_risk = (raw_incidence / birth_rate).fillna(0.0) | ||
return incidence_risk.reset_index() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,6 @@ def setup(self, builder: Builder): | |
Interface to several simulation tools. | ||
""" | ||
self.time_step = builder.time.step_size() | ||
self._sim_step_name = builder.time.simulation_event_name() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just wasn't being used for the pregnancy component. |
||
self.randomness = builder.randomness.get_stream(self.name) | ||
self.birth_outcome_probabilities = builder.value.register_value_producer( | ||
"birth_outcome_probabilities", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import annotations | ||
|
||
import pandas as pd | ||
from vivarium.framework.engine import Builder | ||
from vivarium.framework.event import Event | ||
from vivarium.framework.state_machine import Machine, State, TransientState | ||
from vivarium.types import ClockTime | ||
|
||
from vivarium_gates_mncnh.constants.data_values import SIMULATION_EVENT_NAMES | ||
|
||
|
||
class TreeMachine(Machine): | ||
def __init__( | ||
self, | ||
state_column: str, | ||
states: list[State], | ||
initial_state=None, | ||
time_step_name: str = "", | ||
): | ||
super().__init__(state_column, states, initial_state) | ||
# Time step name where the simulants will go through the decision tree | ||
self._time_step_trigger = time_step_name | ||
|
||
def setup(self, builder: Builder) -> None: | ||
super().setup(builder) | ||
self._sim_step_name = builder.time.simulation_event_name() | ||
|
||
def on_time_step(self, event: Event) -> None: | ||
if self._sim_step_name() == self._time_step_trigger: | ||
super().on_time_step(event) | ||
|
||
|
||
class DecisionTreeState(TransientState): | ||
def __init__( | ||
self, | ||
state_id: str, | ||
update_col: str, | ||
update_value: str | bool, | ||
) -> None: | ||
super().__init__(state_id) | ||
self.update_column = update_col | ||
self.update_value = update_value | ||
|
||
@property | ||
def columns_required(self) -> list[str]: | ||
return [self.update_column] | ||
|
||
def transition_side_effect(self, index: pd.Index, _event_time: ClockTime) -> None: | ||
pop = self.population_view.get(index) | ||
pop[self.update_column] = self.update_value | ||
self.population_view.update(pop) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These classes got moved to tree.py and were made more configurable for future decision trees.