def get_relative_risks(config: Path, input_draw: int, random_seed: int, age_group_id: int) -> pd.DataFrame:

    sim = InteractiveContext(config, setup=False)

    artifact_path = sim.configuration.input_data.artifact_path
    artifact = Artifact(artifact_path)

    age_bins = artifact.load(data_keys.POPULATION.AGE_BINS).reset_index().set_index('age_group_id')
    age_start = age_bins.loc[age_group_id, 'age_start']
    age_end = age_bins.loc[age_group_id, 'age_end']

    year_start = 2019
    year_end = 2020

    sim.configuration.update({
        'input_data': {
            'input_draw_number': input_draw,
        },
        'randomness': {
            'random_seed': random_seed,
        },
        'population': {
            'age_start': age_start,
            'age_end': age_end
        }
    })
    sim.setup()

    pop = sim.get_population()
    gestational_ages = sim.get_value('short_gestation.exposure')(pop.index)
    birth_weights = sim.get_value('low_birth_weight.exposure')(pop.index)

    interpolators = artifact.load(data_keys.LBWSG.RELATIVE_RISK_INTERPOLATOR)

    def calculate_rr_by_sex(sex: str) -> float:
        sex_mask = pop['sex'] == sex
        row_index = (sex, age_start, age_end, year_start, year_end, 'diarrheal_diseases', 'excess_mortality_rate')
        interpolator = pickle.loads(bytes.fromhex(
            interpolators.loc[row_index, f'draw_{input_draw}']
        ))
        rrs = np.exp(interpolator(gestational_ages[sex_mask], birth_weights[sex_mask], grid=False))
        return rrs

    lbwsg_rrs = pd.DataFrame({'relative_risk': 1.0}, index=pop.index)
    lbwsg_rrs['sex'] = pop['sex']
    lbwsg_rrs.loc[lbwsg_rrs['sex'] == 'Female', 'relative_risk'] = calculate_rr_by_sex('Female')
    lbwsg_rrs.loc[lbwsg_rrs['sex'] == 'Male', 'relative_risk'] = calculate_rr_by_sex('Male')

    return lbwsg_rrs
Example #2
0
def test_acmr():
    sim = InteractiveContext(
        'src/vivarium_conic_lsff/model_specifications/india.yaml')
    sim.step()  # make sure everything is setup

    pop = sim.get_population()
    age_groups = pd.cut(pop.age,
                        bins=[0, 7 / 365, 28 / 365, 1, 5],
                        right=False)

    mr_pipeline = sim.get_value('mortality_rate')

    acmr_orig = mr_pipeline.source(pop.index).sum(axis=1)
    acmr_w_risk = mr_pipeline(
        pop.index).sum(axis=1) * 365  # convert back to "per person-year"

    # confirm that they are *not* identical at the individual level
    assert not np.allclose(
        acmr_orig, acmr_w_risk,
        rtol=.05), 'expect acmr to be quite different for some individuals'

    # but close at pop level
    assert np.allclose(
        acmr_orig.groupby(age_groups).median(),
        acmr_w_risk.groupby(age_groups).median(),
        rtol=.1
    ), 'expect acmr to be within 10% of original at population level'