示例#1
0
def build_timeseries_for_fips(
    intervention,
    us_latest,
    us_timeseries,
    model_output_dir,
    fips,
) -> Optional[CovidActNowAreaTimeseries]:
    fips_latest = us_latest.get_record_for_fips(fips)

    if intervention is Intervention.SELECTED_INTERVENTION:
        state = fips_latest[CommonFields.STATE]
        intervention = get_can_projection.get_intervention_for_state(state)

    model_output = CANPyseirLocationOutput.load_from_model_output_if_exists(
        fips, intervention, model_output_dir)
    if not model_output and intervention is not Intervention.OBSERVED_INTERVENTION:
        # All model output is currently tied to a specific intervention. However,
        # we want to generate results for areas that don't have a fit result, but we're not
        # duplicating non-model outputs.
        return None

    try:
        area_summary = api.generate_area_summary(fips_latest, model_output)
        fips_timeseries = us_timeseries.get_subset(None, fips=fips)
        area_timeseries = api.generate_area_timeseries(area_summary,
                                                       fips_timeseries,
                                                       model_output)
    except Exception:
        logger.error(f"failed to run output", fips=fips)
        return None

    return area_timeseries
示例#2
0
def test_generate_timeseries_for_fips(
    include_projections,
    nyc_model_output_path,
    nyc_region,
    nyc_rt_dataset,
    nyc_icu_dataset,
):
    us_latest = combined_datasets.load_us_latest_dataset()
    us_timeseries = combined_datasets.load_us_timeseries_dataset()

    nyc_latest = us_latest.get_record_for_fips(nyc_region.fips)
    nyc_timeseries = us_timeseries.get_one_region(nyc_region)
    intervention = Intervention.OBSERVED_INTERVENTION
    model_output = CANPyseirLocationOutput.load_from_path(
        nyc_model_output_path)
    metrics_series, latest_metric = api_pipeline.generate_metrics_and_latest(
        nyc_timeseries, nyc_rt_dataset, nyc_icu_dataset)

    region_summary = generate_api.generate_region_summary(
        nyc_latest, latest_metric, model_output)
    region_timeseries = generate_api.generate_region_timeseries(
        region_summary, nyc_timeseries, metrics_series, model_output)

    summary = generate_api.generate_region_summary(nyc_latest, latest_metric,
                                                   model_output)

    assert summary.dict() == region_timeseries.region_summary.dict()
    # Double checking that serialized json does not contain NaNs, all values should
    # be serialized using the simplejson wrapper.
    assert "NaN" not in region_timeseries.json()
def test_load_from_path(nyc_model_output_path):

    output = CANPyseirLocationOutput.load_from_path(nyc_model_output_path)
    assert output.fips == "36061"
    assert output.intervention == Intervention.STRONG_INTERVENTION
    # manually checked values
    assert output.peak_hospitalizations_date == datetime.datetime(2020, 4, 15)
    assert not output.hospitals_shortfall_date
    assert output.latest_rt == pytest.approx(0.972686)
def test_hospitalization_date():
    rows = [
        _build_row(date="2020-12-11", **{schema.ALL_HOSPITALIZED: 10}),
        _build_row(date="2020-12-12", **{schema.ALL_HOSPITALIZED: 17}),
        _build_row(date="2020-12-13", **{schema.ALL_HOSPITALIZED: 17}),
        _build_row(date="2020-12-14", **{schema.ALL_HOSPITALIZED: 8}),
    ]
    data = _build_input_df(rows)
    model_output = CANPyseirLocationOutput(data)
    # Check that it picks first date of max.
    expected_date = datetime.datetime(year=2020, month=12, day=12)
    assert model_output.peak_hospitalizations_date == expected_date
def test_no_rt_val():
    rows = [
        _build_row(date="2020-12-13",
                   **{
                       schema.RT_INDICATOR: None,
                       schema.RT_INDICATOR_CI90: None
                   }),
    ]
    data = _build_input_df(rows)
    model_output = CANPyseirLocationOutput(data)
    # Check that it picks first date of max.
    assert not model_output.latest_rt
    assert not model_output.latest_rt_ci90
def test_shortfall():
    rows = [
        _build_row(date="2020-12-13", all_hospitalized=10, beds=11),
        _build_row(date="2020-12-14", all_hospitalized=12, beds=11),
        _build_row(date="2020-12-15", all_hospitalized=13, beds=11),
    ]
    data = _build_input_df(rows)
    model_output = CANPyseirLocationOutput(data)
    # Check that it picks first date of max.
    expected_date = datetime.datetime.fromisoformat("2020-12-14")
    assert model_output.hospitals_shortfall_date == expected_date
    assert model_output.peak_hospitalizations_shortfall == 2

    # No shortfall
    rows = [
        _build_row(date="2020-12-13", all_hospitalized=10, beds=11),
        _build_row(date="2020-12-14", all_hospitalized=12, beds=12),
    ]
    data = _build_input_df(rows)
    model_output = CANPyseirLocationOutput(data)
    # Check that it picks first date of max.
    assert not model_output.hospitals_shortfall_date
示例#7
0
def test_pyseir_end_to_end_idaho():
    # This covers a lot of edge cases.
    # cli._run_all(state='Idaho')
    cli._build_all_for_states(states=["Idaho"], generate_reports=False, fips="16001")
    path = get_run_artifact_path("16001", RunArtifact.WEB_UI_RESULT).replace(
        "__INTERVENTION_IDX__", "2"
    )
    path = pathlib.Path(path)
    assert path.exists()
    output = CANPyseirLocationOutput.load_from_path(path)
    data = output.data
    with_values = data[schema.RT_INDICATOR].dropna()
    assert len(with_values) > 10
    assert (with_values > 0).all()
    assert (with_values < 6).all()
def test_generate_timeseries_for_fips(include_projections,
                                      nyc_model_output_path, nyc_fips):

    us_latest = combined_datasets.build_us_latest_with_all_fields()
    us_timeseries = combined_datasets.build_us_timeseries_with_all_fields()

    nyc_latest = us_latest.get_record_for_fips(nyc_fips)
    nyc_timeseries = us_timeseries.get_subset(None, fips=nyc_fips)
    intervention = Intervention.OBSERVED_INTERVENTION
    model_output = CANPyseirLocationOutput.load_from_path(
        nyc_model_output_path)

    area_summary = generate_api.generate_area_summary(nyc_latest, model_output)
    area_timeseries = generate_api.generate_area_timeseries(
        area_summary, nyc_timeseries, model_output)

    summary = generate_api.generate_area_summary(nyc_latest, model_output)

    assert summary.dict() == area_timeseries.area_summary.dict()
    # Double checking that serialized json does not contain NaNs, all values should
    # be serialized using the simplejson wrapper.
    assert "NaN" not in area_timeseries.json()
def test_build_summary_for_fips(include_projections, nyc_model_output_path,
                                nyc_fips):

    us_latest = combined_datasets.build_us_latest_with_all_fields()
    nyc_latest = us_latest.get_record_for_fips(nyc_fips)
    model_output = None
    expected_projections = None

    intervention = Intervention.OBSERVED_INTERVENTION
    if include_projections:
        model_output = CANPyseirLocationOutput.load_from_path(
            nyc_model_output_path)
        expected_projections = Projections(
            totalHospitalBeds=ResourceUsageProjection(peakShortfall=0,
                                                      peakDate=datetime.date(
                                                          2020, 4, 15),
                                                      shortageStartDate=None),
            ICUBeds=None,
            Rt=model_output.latest_rt,
            RtCI90=model_output.latest_rt_ci90,
        )
        intervention = Intervention.STRONG_INTERVENTION

    summary = generate_api.generate_region_summary(nyc_latest, model_output)

    expected = RegionSummary(
        population=nyc_latest["population"],
        stateName="New York",
        countyName="New York County",
        fips="36061",
        lat=None,
        long=None,
        actuals=Actuals(
            population=nyc_latest["population"],
            intervention="STRONG_INTERVENTION",
            cumulativeConfirmedCases=nyc_latest["cases"],
            cumulativeDeaths=nyc_latest["deaths"],
            cumulativePositiveTests=nyc_latest["positive_tests"],
            cumulativeNegativeTests=nyc_latest["negative_tests"],
            hospitalBeds={
                # Manually calculated from capacity calculation in generate_api.py
                "capacity": 12763,
                "totalCapacity": nyc_latest["max_bed_count"],
                "currentUsageCovid": 0,
                "currentUsageTotal": None,
                "typicalUsageRate": nyc_latest["all_beds_occupancy_rate"],
            },
            ICUBeds={
                "capacity": nyc_latest["icu_beds"],
                "totalCapacity": nyc_latest["icu_beds"],
                "currentUsageCovid": 0,
                "currentUsageTotal": 0,
                "typicalUsageRate": nyc_latest["icu_occupancy_rate"],
            },
            contactTracers=nyc_latest["contact_tracers_count"],
        ),
        lastUpdatedDate=datetime.datetime.utcnow(),
        projections=expected_projections,
    )
    import pprint

    pprint.pprint(expected.actuals.ICUBeds.dict())
    pprint.pprint(summary.actuals.ICUBeds.dict())
    assert expected.dict() == summary.dict()