예제 #1
0
def save_predictions(db_file: Union[str, Path],
                     model_version_id: int,
                     gbd_round_id: int,
                     out_dir: Path,
                     locations: Optional[List[int]] = None,
                     sexes: Optional[List[int]] = None,
                     sample: bool = False,
                     predictions: Optional[pd.DataFrame] = None) -> None:
    """
    Save the fit from this dismod database for a specific location and sex to be
    uploaded later on.
    """
    LOG.info("Extracting results from DisMod SQLite Database.")
    da = DismodExtractor(path=db_file)
    predictions = da.format_predictions_for_ihme(locations=locations,
                                                 sexes=sexes,
                                                 gbd_round_id=gbd_round_id,
                                                 samples=sample,
                                                 predictions=predictions)
    LOG.info(f"Saving the results to {out_dir}.")
    rh = ResultsHandler()
    rh.save_draw_files(df=predictions,
                       directory=out_dir,
                       add_summaries=True,
                       model_version_id=model_version_id)
예제 #2
0
def main():
    """
    Takes a dismod database that has had predict run on it and converts the predictions
    into the format needed for the IHME Epi Databases. Also uploads inputs to tier 3 which
    allows us to view those inputs in EpiViz.
    """
    args = get_args()
    logging.basicConfig(level=LEVELS[args.loglevel])

    context = Context(model_version_id=args.model_version_id)
    inputs, alchemy, settings = context.read_inputs()

    if not inputs.csmr.raw.empty:
        LOG.info("Uploading CSMR to t3")
        inputs.csmr.attach_to_model_version_in_db(
            model_version_id=args.model_version_id,
            conn_def=context.model_connection)

    LOG.info("Extracting results from DisMod SQLite Database.")
    dismod_file = context.db_file(location_id=args.parent_location_id,
                                  sex_id=args.sex_id,
                                  make=False)
    da = DismodExtractor(path=dismod_file)
    predictions = da.format_predictions_for_ihme()

    LOG.info("Saving the results.")
    rh = ResultsHandler(model_version_id=args.model_version_id)
    rh.save_draw_files(df=predictions, directory=context.draw_dir)
    rh.upload_summaries(directory=context.draw_dir,
                        conn_def=context.model_connection)
예제 #3
0
def test_format_fit(ihme, dismod, df):
    d = DismodExtractor(path=Path('temp.db'))
    pred = d.format_predictions_for_ihme(gbd_round_id=6, samples=False)
    # This prediction data frame is longer for each demographic group than the prediction
    # dataframe in `test_get_predictions` because there is an extra measure.
    assert len(pred) == 528
    assert all(pred.columns == [
        'location_id', 'year_id', 'age_group_id', 'sex_id', 'measure_id', 'mean'
    ])
예제 #4
0
def test_format_prior(mi, settings, dismod):
    d = DismodExtractor(path=NAME)
    pred = d.format_predictions_for_ihme(locations=[72],
                                         sexes=[2],
                                         gbd_round_id=6,
                                         samples=True)
    assert all(pred.columns == [
        'location_id', 'year_id', 'age_group_id', 'sex_id', 'measure_id',
        'draw_0', 'draw_1'
    ])
예제 #5
0
def test_default_gather_child_draws(mi, settings, dismod):
    de = DismodExtractor(NAME)
    draws = de.gather_draws_for_prior_grid(location_id=72,
                                           sex_id=2,
                                           rates=['iota', 'chi', 'pini'])
    for rate in ['iota', 'chi', 'pini']:
        assert rate in draws
        assert draws[rate]['value'].shape[-1] == 2
        assert 'dage' not in draws[rate].keys()
        assert 'dtime' not in draws[rate].keys()
예제 #6
0
def test_get_predictions(ihme, dismod, df):
    d = DismodExtractor(path=Path('temp.db'))
    pred = d._extract_raw_predictions()
    assert len(pred) == 484
    assert all(pred.columns == [
        'predict_id', 'sample_index', 'avgint_id', 'avg_integrand',
        'integrand_id', 'node_id', 'weight_id', 'subgroup_id', 'age_lower',
        'age_upper', 'time_lower', 'time_upper', 'c_age_group_id',
        'c_location_id', 'c_sex_id', 'c_year_id', 'x_0', 'x_1', 'x_2',
        'integrand_name', 'minimum_meas_cv', 'rate'
    ])
def test_format_for_ihme(ihme, dismod):
    d = DismodExtractor(path=Path('temp.db'))
    pred = d.format_predictions_for_ihme()
    assert len(pred) == 36
    assert all(pred.columns == [
        'location_id', 'age_group_id', 'year_id', 'sex_id', 'measure_id',
        'mean', 'upper', 'lower'
    ])
    assert all(pred.location_id == np.tile(list(range(70, 73)), 12))
    assert all(pred.sex_id == 2)
    assert all(pred.age_group_id == 2)
    assert all(pred.year_id == 1990)
예제 #8
0
def test_gather_child_draws(mi, settings, dismod):
    de = DismodExtractor(NAME)
    draws = de.gather_draws_for_prior_grid(location_id=72,
                                           sex_id=2,
                                           rates=['iota', 'chi', 'pini'],
                                           value=True,
                                           dage=True,
                                           dtime=True)
    for rate in ['iota', 'chi', 'pini']:
        assert rate in draws
        assert draws[rate]['value'].shape[-1] == 2

    # pini will not have any dage or dtime draws because it only has one age and time
    for rate in ['iota', 'chi']:
        assert draws[rate]['dage'].shape[-1] == 2
        assert draws[rate]['dtime'].shape[-1] == 2
def test_get_predictions(ihme, dismod):
    d = DismodExtractor(path=Path('temp.db'))
    pred = d.get_predictions()
    assert len(pred) == 33
    assert all(pred.columns == [
        'predict_id', 'sample_index', 'avgint_id', 'avg_integrand',
        'integrand_id', 'node_id', 'weight_id', 'subgroup_id', 'age_lower',
        'age_upper', 'time_lower', 'time_upper', 'c_age_group_id',
        'c_location_id', 'c_sex_id', 'c_year_id', 'x_0', 'x_1', 'x_2',
        'integrand_name', 'minimum_meas_cv', 'rate'
    ])
    assert all(pred.age_lower == 0.0)
    assert all(pred.age_upper == 0.01917808)
    assert all(pred.integrand_id == np.repeat(list(range(0, 11)), 3))
    assert all(pred.time_lower == 1990.)
    assert all(pred.time_upper == 1991.)
    assert all(pred.c_age_group_id == 2)
    assert all(pred.c_sex_id == 2)
예제 #10
0
def get_prior(path: Union[str, Path],
              location_id: int,
              sex_id: int,
              rates: List[str],
              samples: bool = True) -> Dict[str, Dict[str, np.ndarray]]:
    """
    Gets priors from a path to a database for a given location ID and sex ID.
    """

    child_prior = DismodExtractor(path=path).gather_draws_for_prior_grid(
        location_id=location_id, sex_id=sex_id, rates=rates, samples=samples)
    return child_prior
예제 #11
0
def main(args=None):
    """
    Creates a dismod database using the saved inputs and the file
    structure specified in the context.

    Then runs an optional set of commands on the database passed
    in the --commands argument.

    Also passes an optional argument --options as a dictionary to
    the dismod database to fill/modify the options table.
    """
    args = get_args(args=args)
    logging.basicConfig(level=LEVELS[args.loglevel])

    if args.test_dir:
        context = Context(model_version_id=args.model_version_id,
                          configure_application=False,
                          root_directory=args.test_dir)
    else:
        context = Context(model_version_id=args.model_version_id)

    inputs, alchemy, settings = context.read_inputs()

    # If we want to override the rate priors with posteriors from a previous
    # database, pass them in here.
    if args.prior_parent or args.prior_sex:
        if not (args.prior_parent and args.prior_sex):
            raise RuntimeError("Need to pass both prior parent and sex or neither.")
        child_prior = DismodExtractor(path=context.db_file(
            location_id=args.prior_parent,
            sex_id=args.prior_sex
        )).gather_draws_for_prior_grid(
            location_id=args.parent_location_id,
            sex_id=args.sex_id,
            rates=[r.rate for r in settings.rate]
        )
    else:
        child_prior = None

    df = DismodFiller(
        path=context.db_file(location_id=args.parent_location_id, sex_id=args.sex_id),
        settings_configuration=settings,
        measurement_inputs=inputs,
        grid_alchemy=alchemy,
        parent_location_id=args.parent_location_id,
        sex_id=args.sex_id,
        child_prior=child_prior
    )
    df.fill_for_parent_child(**args.options)
    run_dismod_commands(dm_file=df.path.absolute(), commands=args.commands)
예제 #12
0
def test_empty_database():
    with pytest.raises(DismodExtractorError):
        DismodExtractor('temp-2.db')