Skip to content

Commit

Permalink
Component Standardization Refactor (#72)
Browse files Browse the repository at this point in the history
* Component Refactor: Pregnancy + Children (#68)

* update vivarium

* pin VPH

* refactor birth recorder

* refactor LBWSG Distribution

* unrefactor NewChildren for now

* refactor pregnancy.py

* lint

* remove diseasestate refactor

* refactor NewChildren

* lint

* remove a setup super call

* do LB instead of full pin

* remove newchildren columns created

* add new children columns to pregnancy creation

* remove references to new_children.columns_created

* undo comment out

* Refactor Observers (#71)

* refactor observers

* lint

* Refactor Anemia, Hemoglobin, Others (#70)

* refactor hemoglobin

* refactor anemia

* refactor maternal BMI

* refactor intervention

* lint

* Refactor Disorders (#69)

* update vivarium

* pin VPH

* refactor birth recorder

* refactor LBWSG Distribution

* unrefactor NewChildren for now

* refactor pregnancy.py

* lint

* remove diseasestate refactor

* refactor NewChildren

* lint

* remove a setup super call

* do LB instead of full pin

* remove newchildren columns created

* add new children columns to pregnancy creation

* remove references to new_children.columns_created

* update vivarium

* refactor birth recorder

* unrefactor NewChildren for now

* refactor pregnancy.py

* lint

* remove diseasestate refactor

* start mortality refactor

* refactor the exclusionstates

* refactor maternal disorders

* fix bug

* fix imports

* refactor mortality

* outline methods

* lint

* model reflects current status

* include diseaseobservers for free

* refactor morbidity

* add morbidity

* remove newchildren columns created

* remove references to new_children.columns_created

* fix bug

* Revert "Merge branch 'feature/pnast/MIC-4416-pregnancy-refactor' into feature/pnast-MIC-4416-refactor-disorders"

This reverts commit 68cba01, reversing
changes made to 62ce0bf.

* add is_non_zero

* revert erroneous changes

* revert model spec

* comment out diseaseobserver

* lint

* Update CHANGELOG.rst
  • Loading branch information
patricktnast authored Oct 2, 2023
1 parent cf9326c commit d33a4ee
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 714 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
**1.0.1 - 10/02/23**

- Refactor to use vivarium Components

**1.0.0 - 09/18/23**

- Release to 1.0.0 with Wave 1 production runs
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
long_description = f.read()

install_requirements = [
"gbd_mapping==3.1.1",
"vivarium==1.2.2",
"vivarium_public_health==1.0.2",
"gbd_mapping>=3.1.1",
"vivarium>=2.0.0",
"vivarium_public_health>=2.0.0",
"click",
"jinja2",
"loguru",
Expand All @@ -32,8 +32,8 @@

# use "pip install -e .[dev]" to install required components + extra components
data_requires = [
"vivarium_cluster_tools==1.3.10",
"vivarium_inputs[data]==4.1.1",
"vivarium_cluster_tools>=1.3.12",
"vivarium_inputs[data]>=4.1.1",
]

test_requirements = ["pytest"]
Expand Down
66 changes: 28 additions & 38 deletions src/vivarium_gates_nutrition_optimization/components/children.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from datetime import datetime
from pathlib import Path
from typing import Tuple
from typing import List, Tuple

import numpy as np
import pandas as pd
from vivarium import Component
from vivarium.framework.engine import Builder
from vivarium.framework.event import Event
from vivarium_cluster_tools.utilities import mkdir
Expand All @@ -15,21 +16,18 @@
)


class NewChildren:
def __init__(self):
self.lbwsg = LBWSGDistribution()

@property
def name(self):
return "child_status"
class NewChildren(Component):
##############
# Properties #
##############

@property
def sub_components(self):
def sub_components(self) -> List[str]:
return [self.lbwsg]

@property
def columns_created(self):
return ["sex_of_child", "birth_weight", "gestational_age"]
def __init__(self):
super().__init__()
self.lbwsg = LBWSGDistribution()

def setup(self, builder: Builder):
self.randomness = builder.randomness.get_stream(self.name)
Expand All @@ -47,7 +45,7 @@ def empty(self, index: pd.Index) -> pd.DataFrame:
index=index,
)

def generate_children(self, index: pd.Index):
def generate_children(self, index: pd.Index) -> pd.DataFrame:
sex_of_child = self.randomness.choice(
index,
choices=["Male", "Female"],
Expand All @@ -65,11 +63,7 @@ def generate_children(self, index: pd.Index):
)


class LBWSGDistribution:
@property
def name(self):
return "lbwsg_distribution"

class LBWSGDistribution(Component):
def setup(self, builder: Builder):
self.randomness = builder.randomness.get_stream(self.name)
self.exposure = builder.data.load(data_keys.LBWSG.EXPOSURE).set_index("sex")
Expand Down Expand Up @@ -142,23 +136,14 @@ def _parse_description(description: str) -> Tuple:
return *birth_weight, *gestational_age


class BirthRecorder:
@property
def name(self):
return "birth_recorder"

#################
# Setup methods #
#################

# noinspection PyAttributeOutsideInit
def setup(self, builder: Builder) -> None:
self.output_path = self._build_output_path(builder)
self.randomness = builder.randomness.get_stream(self.name)

self.births = []
class BirthRecorder(Component):
##############
# Properties #
##############

required_columns = [
@property
def columns_required(self) -> List[str]:
return [
"pregnancy",
"previous_pregnancy",
"pregnancy_outcome",
Expand All @@ -168,10 +153,15 @@ def setup(self, builder: Builder) -> None:
"maternal_bmi_anemia_category",
"intervention",
]
self.population_view = builder.population.get_view(required_columns)

builder.event.register_listener("collect_metrics", self.on_collect_metrics)
builder.event.register_listener("simulation_end", self.write_output)
#################
# Setup methods #
#################

# noinspection PyAttributeOutsideInit
def setup(self, builder: Builder) -> None:
self.output_path = self._build_output_path(builder)
self.births = []

def on_collect_metrics(self, event: Event):
pop = self.population_view.get(event.index)
Expand Down Expand Up @@ -207,7 +197,7 @@ def on_collect_metrics(self, event: Event):
self.births.append(new_births)

# noinspection PyUnusedLocal
def write_output(self, event: Event) -> None:
def on_simulation_end(self, event: Event) -> None:
births_data = pd.concat(self.births)
births_data.to_hdf(f"{self.output_path}.hdf", key="data")
births_data.to_csv(f"{self.output_path}.csv")
Expand Down
Loading

0 comments on commit d33a4ee

Please sign in to comment.