Example #1
0
    def _get_birth_weight_sum(self, pop: pd.DataFrame, base_filter: QueryString, configuration: Dict,
                              time_spans: List[Tuple[str, Tuple[pd.Timestamp, pd.Timestamp]]],
                              age_bins: pd.DataFrame) -> Dict[str, float]:

        base_key = utilities.get_output_template(**configuration).substitute(measure='birth_weight_sum')

        birth_weight_sum = {}
        for year, (year_start, year_end) in time_spans:
            year_filter = base_filter.format(start_time=year_start, end_time=year_end)
            year_key = base_key.substitute(year=year)
            group_birth_weight_sums = utilities.get_group_counts(pop, year_filter, year_key, configuration, age_bins,
                                                                 lambda df: df[self.birth_weight_pipeline_name].sum())
            birth_weight_sum.update(group_birth_weight_sums)
        return birth_weight_sum
Example #2
0
    def _get_births(self, pop: pd.DataFrame, base_filter: QueryString, configuration: Dict,
                    time_spans: List[Tuple[str, Tuple[pd.Timestamp, pd.Timestamp]]], age_bins: pd.DataFrame,
                    cutoff_weight: float = None) -> Dict[str, float]:
        if cutoff_weight:
            base_filter += (
                QueryString('{column} <= {cutoff}')
                .format(column=f'`{self.birth_weight_pipeline_name}`', cutoff=cutoff_weight)
            )
            measure = 'low_weight_births'
        else:
            measure = 'total_births'

        base_key = utilities.get_output_template(**configuration).substitute(measure=measure)

        births = {}
        for year, (year_start, year_end) in time_spans:
            year_filter = base_filter.format(start_time=year_start, end_time=year_end)
            year_key = base_key.substitute(year=year)
            group_births = utilities.get_group_counts(pop, year_filter, year_key, configuration, age_bins)
            births.update(group_births)
        return births
Example #3
0
    def on_collect_metrics(self, event: 'Event'):
        pop = self.population_view.get(event.index, query='alive == "alive"')
        initial_proportion_reduction = pop[
            'initial_treatment_proportion_reduction']

        fpg = self.fpg(pop.index)
        sbp = self.sbp(pop.index)
        ldlc = self.ldlc(pop.index)
        cvd_score = self.cvd_risk_score(pop.index)
        measure_map = list(
            zip([
                'fpg_person_time', 'sbp_person_time', 'ldlc_person_time',
                'cv_risk_score_person_time'
            ], [fpg, sbp, ldlc, cvd_score]))

        adherent = self.is_adherent(pop.index).astype(int)

        raw_ldlc = ldlc / (1 - initial_proportion_reduction)
        at_target = (ldlc / raw_ldlc <= 0.5).astype(int)

        # noinspection PyTypeChecker
        step_size = to_years(event.step_size)

        age_sex_filter, (ages, sexes) = get_age_sex_filter_and_iterables(
            self.config, self.age_bins)
        base_key = get_output_template(**self.config).substitute(
            year=event.time.year)
        base_filter = QueryString(f'alive == "alive"') + age_sex_filter
        person_time = {}
        for labels, pop_in_group in self.stratifier.group(pop):
            for group, age_group in ages:
                start, end = age_group.age_start, age_group.age_end
                for sex in sexes:
                    filter_kwargs = {
                        'age_start': start,
                        'age_end': end,
                        'sex': sex,
                        'age_group': group
                    }
                    group_key = base_key.substitute(**filter_kwargs)
                    group_filter = base_filter.format(**filter_kwargs)

                    sub_pop = (pop_in_group.query(group_filter) if group_filter
                               and not pop_in_group.empty else pop_in_group)

                    for measure, attribute in measure_map:
                        person_time[group_key.substitute(
                            measure=measure)] = sum(
                                attribute.loc[sub_pop.index] * step_size)

                    adherent_pt = sum(adherent.loc[sub_pop.index] * step_size)
                    person_time[group_key.substitute(
                        measure='adherent_person_time')] = adherent_pt

                    at_target_pt = sum(at_target.loc[sub_pop.index] *
                                       step_size)
                    person_time[group_key.substitute(
                        measure='at_target_person_time')] = at_target_pt

                    treatments = {
                        group_key.substitute(
                            measure=f'{treatment}_person_time'): 0.
                        for treatment in project_globals.TREATMENT
                    }

                    treatments.update(
                        (sub_pop[project_globals.TREATMENT.name].map(
                            lambda x: group_key.substitute(
                                measure=f'{x}_person_time')).value_counts() *
                         step_size).to_dict())
                    person_time.update(treatments)

            self.results.update(
                self.stratifier.update_labels(person_time, labels))