def read_data_by_draw(artifact_path: str, key: str, draw: int) -> pd.DataFrame: """Reads data from the artifact on a per-draw basis. This is necessary for Low Birthweight Short Gestation (LBWSG) data. Parameters ---------- artifact_path The artifact to read from. key The entity key associated with the data to read. draw The data to retrieve. """ key = key.replace(".", "/") with pd.HDFStore(artifact_path, mode='r') as store: index = store.get(f'{key}/index') draw = store.get(f'{key}/draw_{draw}') draw = draw.rename("value") data = pd.concat([index, draw], axis=1) data = data.drop(columns='location') data = pivot_categorical(data) data[project_globals.LBWSG_MISSING_CATEGORY. CAT] = project_globals.LBWSG_MISSING_CATEGORY.EXPOSURE return data
def _get_excess_shift_source(self, builder: Builder) -> LookupTable: excess_shift_data = builder.data.load( f'{self.risk}.excess_shift', affected_entity=self.target.name, affected_measure=self.target.measure) excess_shift_data = rebin_relative_risk_data(builder, self.risk, excess_shift_data) excess_shift_data = pivot_categorical(excess_shift_data) return builder.lookup.build_table(excess_shift_data, key_columns=['sex'], parameter_columns=['age', 'year'])
def get_relative_risk_data(self, builder): relative_risk_data = read_data_by_draw(builder, f'{self.risk}.relative_risk') correct_target = ((relative_risk_data['affected_entity'] == 'all') & (relative_risk_data['affected_measure'] == 'excess_mortality_rate')) relative_risk_data = (relative_risk_data[correct_target].drop( ['affected_entity', 'affected_measure'], 'columns')) relative_risk_data = pivot_categorical(relative_risk_data) relative_risk_data[project_globals.LBWSG_MISSING_CATEGORY.CAT] = ( relative_risk_data['cat106'] + relative_risk_data['cat116']) / 2 return relative_risk_data
def setup(self, builder: 'Builder'): self.clock = builder.time.clock() self.randomness = builder.randomness.get_stream(f'{self.name}_initial_states') disability_weight_data = builder.data.load(project_globals.VITAMIN_A_DEFICIENCY_DISABILITY_WEIGHT) self.base_disability_weight = builder.lookup.build_table(disability_weight_data, key_columns=['sex'], parameter_columns=['age', 'year']) self.disability_weight = builder.value.register_value_producer( f'{self.name}.disability_weight', source=self.compute_disability_weight, requires_columns=['age', 'sex', 'alive', self.name]) builder.value.register_value_modifier('disability_weight', modifier=self.disability_weight) exposure_data = builder.data.load(project_globals.VITAMIN_A_DEFICIENCY_EXPOSURE) exposure_data = pivot_categorical(exposure_data) exposure_data = exposure_data.drop('cat2', axis=1) self._base_exposure = builder.lookup.build_table(exposure_data, key_columns=['sex'], parameter_columns=['age', 'year']) self.exposure_proportion = builder.value.register_value_producer( f'{self.name}.exposure_parameters', source=self.exposure_proportion_source, requires_values=[f'{self.name}.exposure_parameters.paf'], requires_columns=['age', 'sex'] ) base_paf = builder.lookup.build_table(0) self.joint_paf = builder.value.register_value_producer( f'{self.name}.exposure_parameters.paf', source=lambda index: [base_paf(index)], preferred_combiner=list_combiner, preferred_post_processor=union_post_processor ) self.exposure = builder.value.register_value_producer( f'{self.name}.exposure', source=self.get_current_exposure, requires_columns=['age', 'sex', f'{self.name}_propensity'] ) columns_created = [self.name, project_globals.VITAMIN_A_GOOD_EVENT_TIME, project_globals.VITAMIN_A_GOOD_EVENT_COUNT, project_globals.VITAMIN_A_BAD_EVENT_TIME, project_globals.VITAMIN_A_BAD_EVENT_COUNT, project_globals.VITAMIN_A_PROPENSITY] view_columns = columns_created + ['alive', 'age', 'sex'] self.population_view = builder.population.get_view(view_columns) builder.population.initializes_simulants( self.on_initialize_simulants, creates_columns=columns_created, requires_columns=['age', 'sex'], requires_streams=[f'{self.name}_initial_states'] ) builder.event.register_listener('time_step', self.on_time_step)
def prep_weights(art): weights = art.load( 'risk_factor.high_systolic_blood_pressure.exposure_distribution_weights' ) index_cols = weights.index.names.difference({'location', 'parameter'}) weights = pivot_categorical(weights.reset_index()) weights = weights.drop(columns=index_cols) if 'glnorm' in weights.columns: if np.any(weights['glnorm']): raise NotImplementedError('glnorm distribution is not supported') weights = weights.drop(columns='glnorm') return weights
def load_relative_risk_data(self, builder): relative_risk_data = builder.data.load(f'{self.risk}.relative_risk') correct_target = ((relative_risk_data['affected_entity'] == project_globals.RATE_TARGET_MAP[self.target].name) & (relative_risk_data['affected_measure'] == project_globals.RATE_TARGET_MAP[self.target].measure)) relative_risk_data = (relative_risk_data[correct_target] .drop(['affected_entity', 'affected_measure'], 'columns')) if get_distribution_type(builder, self.risk) in ['dichotomous', 'ordered_polytomous', 'unordered_polytomous']: relative_risk_data = pivot_categorical(relative_risk_data) else: relative_risk_data = relative_risk_data.drop(['parameter'], 'columns') return relative_risk_data
def get_relative_risk_data_by_draw(builder, risk: EntityString, target: TargetString, randomness: RandomnessStream): source_type = validate_relative_risk_data_source(builder, risk, target) relative_risk_data = load_relative_risk_data_by_draw( builder, risk, target, source_type) relative_risk_data = rebin_relative_risk_data(builder, risk, relative_risk_data) if get_distribution_type(builder, risk) in [ 'dichotomous', 'ordered_polytomous', 'unordered_polytomous' ]: relative_risk_data = pivot_categorical(relative_risk_data) else: relative_risk_data = relative_risk_data.drop(['parameter'], 'columns') return relative_risk_data
def get_exposure_data(builder): exposure = read_data_by_draw(builder, project_globals.LBWSG_EXPOSURE) exposure = pivot_categorical(exposure) exposure[project_globals.LBWSG_MISSING_CATEGORY. CAT] = project_globals.LBWSG_MISSING_CATEGORY.EXPOSURE return exposure