def test_create_cdf():
    p = [0, 0.2, 0.7, 0.1]
    cdf = create_cdf(p)
    # ----- The first value of CDF should equal the first value of p
    assert p[0] == cdf[0]
    # ----- The final value of the distribution should be 1
    assert cdf[-1] == 1
    # ----- Length of cdf should equal length of p
    assert len(cdf) == len(p)
Пример #2
0
 def create_antibiotics_type_dictionary(self) -> dict:
     """ Create the antibiotics distributions lists for each location
     """
     td = dict()
     for location in self.model.location.locations.enum:
         category = self.model.location.locations.convert_int(
             location.value, "category")
         dist = self.params["distributions"][category]
         td[location.value] = create_cdf(dist)
     return td
def test_random_selection():
    p = [0.1, 0.1, 0.7, 0.1]
    options = [0, 1, 2, 3]
    cdf = create_cdf(p)
    rs = random_selection(0.95, cdf, options)
    # ----- .95 is between .9 and 1.0, so 2 is selected
    assert rs == 3
    rs = random_selection(0.5, cdf, options)
    # ----- .5 is between .2 and .7, so 2 is selected
    assert rs == 2
    rs = random_selection(0.11, cdf, options)
    # ----- .1 is between 0.1 and .2, so 1 is selected
    assert rs == 1
    rs = random_selection(0.09, cdf, options)
    # ----- .09 is between 0 and .1, so 0 is selected
    assert rs == 0
Пример #4
0
    def simulate_cre_transitions(self):
        """ Simulate all agents CRE Transitions
            1: SUSCEPTIBLE TO CRE: Community & Facility
            2: CRE RECOVERY
        """
        living = self.model.life.values == LifeState.ALIVE
        susceptible_agents = (living) & (self.cre.values
                                         == CREState.SUSCEPTIBLE)
        cre_agents = (living) & (self.cre.values == CREState.CRE)

        # ----- 1a: Community
        community_id = self.model.location.locations.community
        in_community = self.model.unique_ids[
            (self.model.location.location.values == community_id)
            & susceptible_agents]
        probabilities = np.zeros(len(in_community))
        probabilities.fill(self.params["cre"]["betas"]["base"])  # TODO
        selected_agents = probabilities > self.model.rng.rand(
            len(probabilities))
        for unique_id in in_community[selected_agents]:
            self.cre_update(unique_id, CREState.CRE)

        # ----- 1b: Facility
        in_facility = self.model.unique_ids[
            (self.model.location.location.values != community_id)
            & susceptible_agents]
        probabilities = self.find_cre_probability(in_facility)
        selected_agents = probabilities > self.model.rng.rand(
            len(probabilities))
        for unique_id in in_facility[selected_agents]:
            self.cre_update(unique_id, CREState.CRE)

        # ----- 2: CRE Recovery
        probabilities = np.array([self.params["recovery"]] * len(cre_agents))
        selected_agents = probabilities > self.model.rng.rand(
            len(probabilities))
        for unique_id in cre_agents[selected_agents]:
            # --- Create recovery options (Susceptible, Colonized, Death)
            to_death = self.params["death"]
            to_susceptible = 1 - to_death
            dist = [to_susceptible, to_death]
            options = [CREState.SUSCEPTIBLE, CREState.DEAD]
            end_state = random_selection(self.model.rng.rand(1),
                                         create_cdf(dist), options)
            self.cre_update(unique_id, end_state)
Пример #5
0
def select_facility(hospital: dict,
                    discharge_data: pd.DataFrame,
                    county_age_dict: dict,
                    fill_percent=.7):
    """ Append the number of hospital patients required for a given county/age dictionary.
        This will help fill the beds with agents
    """
    hospital_id = hospital['ID']
    rows = discharge_data[discharge_data[hospital_id] > 0].copy()
    rows.loc[:, 'Percentage'] = rows[hospital_id] / rows[hospital_id].sum()

    for bed in range(0, int(hospital['beds'] * fill_percent)):
        p = create_cdf(rows.Percentage.values)
        # --- Randomly select a county based on the percentage
        county = random_selection(
            np.random.rand(1, 1)[0], p, rows.County_Code.values)
        # --- Randomly select an age based on 40% <50, 20% 50-65, and 40% 65+
        age = random_selection(
            np.random.rand(1, 1)[0], [.4, .6, 1.0], np.array([0, 1, 2]))
        county_age_dict[(county, age)].append(hospital['int'])
Пример #6
0
    def simulate_cdi_transitions(self):
        """ Simulate all agents CDI Transitions
            Find which patients are susceptible, colonized, or currently have CDI

            1: Susecptible to Colonized (a: those in facilities, b: those in community)
            2: Colonization Recovery
            3: Colonized to CDI
            4: CDI Recovery
        """
        living = self.model.life.values == LifeState.ALIVE
        susceptible_agents = (living) & (self.cdi.values == CDIState.SUSCEPTIBLE)
        colonized_agents = self.model.unique_ids[
            (living) & (self.cdi.values == CDIState.COLONIZED)
        ]
        cdi_agents = self.model.unique_ids[(living) & (self.cdi.values == CDIState.CDI)]

        # ----- 1a:
        community = self.model.location.locations.community
        in_facility = self.model.unique_ids[
            (self.model.location.location.values != community) & susceptible_agents
        ]
        probabilities = self.find_colonization_probability(in_facility)
        selected_agents = probabilities > self.model.rng.rand(len(probabilities))
        for unique_id in in_facility[selected_agents]:
            self.cdi_update(unique_id, CDIState.COLONIZED)

        # ----- 1b:
        in_community = self.model.unique_ids[
            (self.model.location.location.values == community) & susceptible_agents
        ]
        probabilities = np.zeros(len(in_community))
        probabilities.fill(self.colonization_dict[community])
        selected_agents = probabilities > self.model.rng.rand(len(probabilities))
        for unique_id in in_community[selected_agents]:
            self.cdi_update(unique_id, CDIState.COLONIZED)

        # ----- 2:
        probabilities = np.array(
            [self.params["colonization"]["clearance"]] * len(colonized_agents)
        )
        selected_agents = probabilities > self.model.rng.rand(len(probabilities))
        for unique_id in colonized_agents[selected_agents]:
            self.cdi_update(unique_id, CDIState.SUSCEPTIBLE)

        # ----- 3:
        probabilities = self.find_cdi_probability(colonized_agents)
        selected_agents = probabilities > self.model.rng.rand(len(probabilities))
        for unique_id in colonized_agents[selected_agents]:
            self.cdi_update(unique_id, CDIState.CDI)

        # ----- 4:
        probabilities = np.array([self.params["cdi"]["recovery"]] * len(cdi_agents))
        selected_agents = probabilities > self.model.rng.rand(len(probabilities))
        for unique_id in cdi_agents[selected_agents]:
            # --- Create recovery options (Susceptible, Colonized, Death)
            to_death = self.params["cdi"]["death"]["age"][
                str(int(self.model.age_groups[unique_id]))
            ]
            to_col = self.params["cdi"]["recurrence"]["probability_with_recent_CDI"][
                str(self.recent_cdi_count.get(unique_id, 0))
            ]
            dist = [1 - to_death - to_col, to_col, to_death]
            options = [CDIState.SUSCEPTIBLE, CDIState.COLONIZED, CDIState.DEAD]
            end_state = random_selection(
                self.model.rng.rand(1), create_cdf(dist), options
            )
            self.cdi_update(unique_id, end_state)

        self.update_cdi_variables()
Пример #7
0
 def create_cdf_dict(self):
     for k, v in self.transition_dict.items():
         self.cdf_dict[k] = create_cdf(v)