예제 #1
0
def test__assign_demography_with_initial_age_zero(config):
    pop_data = dt.assign_demographic_proportions(
        make_uniform_pop_data(age_bin_midpoint=True))
    pop_data = pop_data[pop_data.year_start == 1990]
    simulants = make_base_simulants()
    initial_age = 0
    r = {
        k: get_randomness()
        for k in ['general_purpose', 'bin_selection', 'age_smoothing']
    }
    step_size = utilities.to_time_delta(config.time.step_size)

    simulants = bp._assign_demography_with_initial_age(
        simulants, pop_data, initial_age, step_size, r,
        lambda *args, **kwargs: None)

    assert len(simulants) == len(simulants.age.unique())
    assert simulants.age.min() > initial_age
    assert simulants.age.max() < initial_age + utilities.to_years(step_size)
    assert math.isclose(len(simulants[simulants.sex == 'Male']) /
                        len(simulants),
                        0.5,
                        abs_tol=0.01)
    for location in simulants.location.unique():
        assert math.isclose(len(simulants[simulants.location == location]) /
                            len(simulants),
                            1 / len(simulants.location.unique()),
                            abs_tol=0.01)
 def adjust_coverage_level(self, index, coverage):
     """Adjust the coverage level of iron fortification."""
     time_since_intervention_start = max(
         to_years(self.clock() - self.intervention_start), 0)
     if time_since_intervention_start > 0:
         c_start, c_end = self.coverage_start(index), self.coverage_end(
             index)
         # noinspection PyTypeChecker
         new_coverage = (
             c_end - (c_end - c_start) *
             (1 - project_globals.IRON_ANNUAL_PROPORTION_INCREASE)**
             time_since_intervention_start)
         coverage = new_coverage
     return coverage
예제 #3
0
def get_lived_in_span(pop: pd.DataFrame, t_start: pd.Timestamp, t_end: pd.Timestamp) -> pd.DataFrame:
    """Gets a subset of the population that lived in the time span.

    Parameters
    ----------
    pop
        A table representing the population with columns for 'entrance_time',
        'exit_time' and 'age'.
    t_start
        The date and time at the start of the span.
    t_end
        The date and time at the end of the span.

    Returns
    -------
    lived_in_span
        A table representing the population who lived some amount of time
        within the time span with all columns provided in the original
        table and additional columns 'age_at_span_start' and 'age_at_span_end'
        indicating the age of the individual at the start and end of the time
        span, respectively. 'age_at_span_start' will never be lower than the
        age at the simulant's entrance time and 'age_at_span_end' will never
        be greater than the age at the simulant's exit time.

    """
    lived_in_span = pop.loc[(t_start < pop['exit_time']) & (pop['entrance_time'] < t_end)]

    span_entrance_time = lived_in_span.entrance_time.copy()
    span_entrance_time.loc[t_start > span_entrance_time] = t_start
    span_exit_time = lived_in_span.exit_time.copy()
    span_exit_time.loc[t_end < span_exit_time] = t_end

    lived_in_span.loc[:, 'age_at_span_end'] = lived_in_span.age - to_years(lived_in_span.exit_time
                                                                           - span_exit_time)
    lived_in_span.loc[:, 'age_at_span_start'] = lived_in_span.age - to_years(lived_in_span.exit_time
                                                                             - span_entrance_time)
    return lived_in_span
 def adjust_effective_coverage_level(self, index, coverage):
     """Adjust the effective coverage of folic acid fortification."""
     time_since_intervention_start = max(
         to_years(self.clock() - (self.intervention_start +
                                  project_globals.FOLIC_ACID_DELAY)), 0)
     if time_since_intervention_start > 0:
         c_start, c_end = self.coverage_start(index), self.coverage_end(
             index)
         # noinspection PyTypeChecker
         new_coverage = (
             c_end - (c_end - c_start) *
             (1 - project_globals.FOLIC_ACID_ANNUAL_PROPORTION_INCREASE)**
             time_since_intervention_start)
         coverage = new_coverage
     return coverage
예제 #5
0
    def _get_severity(self, exposure):
        age = self.population_view.subview(['age']).get(exposure.index).age
        severity = pd.Series('none',
                             index=exposure.index,
                             name='anemia_severity')

        neonatal = age < to_years(pd.Timedelta(days=28))
        mild = ((neonatal & (130 <= exposure) & (exposure < 150))
                | (~neonatal & (100 <= exposure) & (exposure < 110)))
        moderate = ((neonatal & (90 <= exposure) & (exposure < 130))
                    | (~neonatal & (70 <= exposure) & (exposure < 100)))
        severe = ((neonatal & (exposure < 90)) | (~neonatal & (exposure < 70)))
        severity.loc[mild] = 'mild'
        severity.loc[moderate] = 'moderate'
        severity.loc[severe] = 'severe'
        return severity
    def on_time_step(self, event):
        """Adds a set number of simulants to the population each time step.

        Parameters
        ----------
        event
            The event that triggered the function call.
        """
        # Assume births are uniformly distributed throughout the year.
        step_size = utilities.to_years(event.step_size)
        simulants_to_add = self.simulants_per_year * step_size + self.fractional_new_births

        self.fractional_new_births = simulants_to_add % 1
        simulants_to_add = int(simulants_to_add)

        if simulants_to_add > 0:
            self.simulant_creator(simulants_to_add,
                                  population_configuration={
                                      'age_start': 0,
                                      'age_end': 0,
                                      'sim_state': 'time_step',
                                  })
    def on_time_step(self, event):
        """Adds new simulants every time step based on the Crude Birth Rate
        and an assumption that birth is a Poisson process
        Parameters
        ----------
        event
            The event that triggered the function call.
        """
        birth_rate = self.birth_rate.at[self.clock().year]
        step_size = utilities.to_years(event.step_size)

        mean_births = birth_rate * step_size
        # Assume births occur as a Poisson process
        r = np.random.RandomState(seed=self.randomness.get_seed())
        simulants_to_add = r.poisson(mean_births)

        if simulants_to_add > 0:
            self.simulant_creator(simulants_to_add,
                                  population_configuration={
                                      'age_start': 0,
                                      'age_end': 0,
                                      'sim_state': 'time_step',
                                  })
예제 #8
0
 def count_ylds(sub_group):
     """Counts ylds attributable to a cause in the time step."""
     return sum(disability_weights[cause](sub_group.index) * to_years(step_size))
예제 #9
0
def get_susceptible_person_time(pop, config, disease, current_year, step_size, age_bins):
    base_key = get_output_template(**config).substitute(measure=f'{disease}_susceptible_person_time', year=current_year)
    base_filter = QueryString(f'alive == "alive" and {disease} == "susceptible_to_{disease}"')
    person_time = get_group_counts(pop, base_filter, base_key, config, age_bins,
                                   aggregate=lambda x: len(x) * to_years(step_size))
    return person_time