コード例 #1
0
    def __init__(self,
                 fips,
                 n_years=2,
                 n_samples=250,
                 suppression_policy=(0.35, 0.5, 0.75, 1),
                 skip_plots=False,
                 output_percentiles=(5, 25, 32, 50, 75, 68, 95),
                 generate_report=True):

        self.fips = fips
        self.t_list = np.linspace(0, 365 * n_years, 365 * n_years)
        self.skip_plots = skip_plots

        self.county_metadata = load_data.load_county_metadata_by_fips(fips)
        self.output_percentiles = output_percentiles
        self.n_samples = n_samples
        self.n_years = n_years
        self.t0 = fit_results.load_t0(fips)
        self.date_generated = datetime.datetime.utcnow().isoformat()
        self.suppression_policy = suppression_policy

        self.summary = copy.deepcopy(self.__dict__)
        self.summary.pop('t_list')
        self.generate_report = generate_report

        self.all_outputs = {}
        self.output_file_report = os.path.join(
            OUTPUT_DIR, self.county_metadata['state'], 'reports',
            f"{self.county_metadata['state']}__{self.county_metadata['county']}__{self.fips}__ensemble_projections.pdf"
        )
        self.output_file_data = os.path.join(
            OUTPUT_DIR, self.county_metadata['state'], 'data',
            f"{self.county_metadata['state']}__{self.county_metadata['county']}__{self.fips}__ensemble_projections.json"
        )
コード例 #2
0
    def __init__(
        self,
        fips,
        n_years=0.5,
        n_samples=250,
        suppression_policy=(0.35, 0.5, 0.75, 1),
        skip_plots=False,
        output_percentiles=(5, 25, 32, 50, 75, 68, 95),
        generate_report=True,
        run_mode=RunMode.DEFAULT,
        min_hospitalization_threshold=5,
        hospitalization_to_confirmed_case_ratio=1 / 4,
    ):

        self.fips = fips
        self.agg_level = AggregationLevel.COUNTY if len(fips) == 5 else AggregationLevel.STATE

        self.t_list = np.linspace(0, int(365 * n_years), int(365 * n_years) + 1)
        self.skip_plots = skip_plots
        self.run_mode = RunMode(run_mode)
        self.hospitalizations_for_state = None
        self.min_hospitalization_threshold = min_hospitalization_threshold
        self.hospitalization_to_confirmed_case_ratio = hospitalization_to_confirmed_case_ratio

        if self.agg_level is AggregationLevel.COUNTY:
            self.county_metadata = load_data.load_county_metadata_by_fips(fips)
            self.state_abbr = us.states.lookup(self.county_metadata["state"]).abbr
            self.state_name = us.states.lookup(self.county_metadata["state"]).name

            self.output_file_report = get_run_artifact_path(self.fips, RunArtifact.ENSEMBLE_REPORT)
            self.output_file_data = get_run_artifact_path(self.fips, RunArtifact.ENSEMBLE_RESULT)

        else:
            self.state_abbr = us.states.lookup(self.fips).abbr
            self.state_name = us.states.lookup(self.fips).name

            self.output_file_report = None
            self.output_file_data = get_run_artifact_path(self.fips, RunArtifact.ENSEMBLE_RESULT)

        os.makedirs(os.path.dirname(self.output_file_data), exist_ok=True)
        if self.output_file_report:
            os.makedirs(os.path.dirname(self.output_file_report), exist_ok=True)

        self.output_percentiles = output_percentiles
        self.n_samples = n_samples
        self.n_years = n_years
        # TODO: Will be soon replaced with loaders for all the inferred params.
        # self.t0 = fit_results.load_t0(fips)
        self.date_generated = datetime.datetime.utcnow().isoformat()
        self.suppression_policy = suppression_policy
        self.summary = copy.deepcopy(self.__dict__)
        self.summary.pop("t_list")
        self.generate_report = generate_report

        self.suppression_policies = None
        self.override_params = dict()
        self.init_run_mode()

        self.all_outputs = {}
コード例 #3
0
    def __init__(self, fips, model_ensemble, county_outputs, filename, summary, xlim=(0, 360)):
        self.fips = fips
        self.county_outputs = county_outputs
        self.model_ensemble = model_ensemble
        self.county_metadata = load_data.load_county_metadata_by_fips(fips)
        self.summary = summary

        timeseries = combined_datasets.get_timeseries_for_fips(
            fips, columns=[CommonFields.CASES, CommonFields.DEATHS], min_range_with_some_value=True
        )
        self.county_case_data = timeseries.data
        self.report = PDFReport(filename=filename)
        self.xlim = xlim
コード例 #4
0
 def __init__(self,
              fips,
              model_ensemble,
              county_outputs,
              filename,
              summary,
              xlim=(0, 360)):
     self.fips = fips
     self.county_outputs = county_outputs
     self.model_ensemble = model_ensemble
     self.county_metadata = load_data.load_county_metadata_by_fips(fips)
     self.summary = summary
     _county_case_data = load_data.load_county_case_data()
     self.county_case_data = _county_case_data[_county_case_data['fips'] ==
                                               fips]
     self.report = PDFReport(filename=filename)
     self.xlim = xlim
コード例 #5
0
def get_run_artifact_path(fips, artifact, output_dir=None) -> str:
    """
    Get an artifact path for a given locale and artifact type.

    Parameters
    ----------
    fips: str
        State or county fips code. Can also be a 2 character state abbreviation.
        If arbitrary string (e.g. for tests) then passed through
    artifact: RunArtifact
        The artifact type to retrieve the pointer for.
    output_dir: str or NoneType
        Output directory to obtain the path for.

    Returns
    -------
    path: str
        Location of the artifact.
    """
    state_obj = us.states.lookup(fips[:2])
    if len(fips) == 5:
        agg_level = AggregationLevel.COUNTY
        county = load_data.load_county_metadata_by_fips(fips)["county"]
    elif len(fips) == 2:
        agg_level = AggregationLevel.STATE
    else:
        agg_level = None

    artifact = RunArtifact(artifact)

    output_dir = output_dir or OUTPUT_DIR

    if artifact is RunArtifact.RT_INFERENCE_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f"Rt_results__{state_obj.name}__{county}__{fips}.pdf",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"Rt_results__{state_obj.name}__{fips}.pdf",
            )

    elif artifact is RunArtifact.RT_SMOOTHING_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f"Rt_smoothing__{state_obj.name}__{county}__{fips}.pdf",
            )
        elif agg_level is AggregationLevel.STATE:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"Rt_smoothing__{state_obj.name}__{fips}.pdf",
            )
        else:  # For tests
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"Rt_smoothing__{state_obj.name}__{fips}.pdf",
            )

    elif artifact is RunArtifact.RT_INFERENCE_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f"Rt_results__{state_obj.name}__{county}__{fips}.json",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "data",
                f"Rt_results__{state_obj.name}__{fips}.json",
            )

    elif artifact is RunArtifact.MLE_FIT_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f"mle_fit_results__{state_obj.name}__{county}__{fips}.pdf",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"mle_fit_results__{state_obj.name}__{fips}.pdf",
            )

    elif artifact is RunArtifact.MLE_FIT_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "data",
                f"mle_fit_results__{state_obj.name}_counties.json",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "data",
                f"mle_fit_results__{state_obj.name}_state_only.json",
            )

    elif artifact is RunArtifact.MLE_FIT_MODEL:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f"mle_fit_model__{state_obj.name}__{county}__{fips}.pkl",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "data",
                f"mle_fit_model__{state_obj.name}_state_only.pkl",
            )

    elif artifact is RunArtifact.ENSEMBLE_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f"ensemble_projections__{state_obj.name}__{county}__{fips}.json",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "data",
                f"ensemble_projections__{state_obj.name}__{fips}.json",
            )

    elif artifact is RunArtifact.ENSEMBLE_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f"ensemble_projections__{state_obj.name}__{county}__{fips}.pdf",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"ensemble_projections__{state_obj.name}__{fips}.pdf",
            )

    elif artifact is RunArtifact.WEB_UI_RESULT:
        path = os.path.join(WEB_UI_FOLDER(output_dir), f"{fips}.__INTERVENTION_IDX__.json")

    elif artifact is RunArtifact.WHITELIST_RESULT:
        path = os.path.join(output_dir, "api_whitelist.json")

    elif artifact is RunArtifact.BACKTEST_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f"backtest_results__{state_obj.name}__{county}__{fips}.pdf",
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir),
                "reports",
                f"backtest_results__{state_obj.name}__{fips}.pdf",
            )

    else:
        raise ValueError(f"No paths available for artifact {RunArtifact}")

    os.makedirs(os.path.dirname(path), exist_ok=True)
    return path
コード例 #6
0
ファイル: utils.py プロジェクト: ashgadala/covid-data-model
def get_run_artifact_path(fips, artifact, output_dir=None):
    """
    Get an artifact path for a given locale and artifact type.

    Parameters
    ----------
    fips: str
        State or county fips code. Can also be a 2 character state abbreviation.
    artifact: RunArtifact
        The artifact type to retrieve the pointer for.
    output_dir: str or NoneType
        Output directory to obtain the path for.

    Returns
    -------
    path: str
        Location of the artifact.
    """
    state_obj = us.states.lookup(fips[:2])
    if len(fips) == 5:
        agg_level = AggregationLevel.COUNTY
        county = load_data.load_county_metadata_by_fips(fips)['county']
    else:
        agg_level = AggregationLevel.STATE

    artifact = RunArtifact(artifact)

    output_dir = output_dir or OUTPUT_DIR

    if artifact is RunArtifact.RT_INFERENCE_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f'Rt_results__{state_obj.name}__{county}__{fips}.pdf')
        else:
            path = os.path.join(STATE_SUMMARY_FOLDER(output_dir), 'reports',
                                f'Rt_results__{state_obj.name}__{fips}.pdf')

    elif artifact is RunArtifact.RT_INFERENCE_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f'Rt_results__{state_obj.name}__{county}__{fips}.json')
        else:
            path = os.path.join(STATE_SUMMARY_FOLDER(output_dir), 'data',
                                f'Rt_results__{state_obj.name}__{fips}.json')

    elif artifact is RunArtifact.MLE_FIT_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f'mle_fit_results__{state_obj.name}__{county}__{fips}.pdf')
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'reports',
                f'mle_fit_results__{state_obj.name}__{fips}.pdf')

    elif artifact is RunArtifact.MLE_FIT_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'data',
                f'mle_fit_results__{state_obj.name}_counties.json')
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'data',
                f'mle_fit_results__{state_obj.name}_state_only.json')

    elif artifact is RunArtifact.MLE_FIT_MODEL:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f'mle_fit_model__{state_obj.name}__{county}__{fips}.pkl')
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'data',
                f'mle_fit_model__{state_obj.name}_state_only.pkl')

    elif artifact is RunArtifact.ENSEMBLE_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                DATA_FOLDER(output_dir, state_obj.name),
                f'ensemble_projections__{state_obj.name}__{county}__{fips}.json'
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'data',
                f'ensemble_projections__{state_obj.name}__{fips}.json')

    elif artifact is RunArtifact.ENSEMBLE_REPORT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f'ensemble_projections__{state_obj.name}__{county}__{fips}.pdf'
            )
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'reports',
                f'ensemble_projections__{state_obj.name}__{fips}.pdf')

    elif artifact is RunArtifact.WEB_UI_RESULT:
        if agg_level is AggregationLevel.COUNTY:

            path = os.path.join(
                WEB_UI_FOLDER(output_dir), 'county',
                f'{state_obj.abbr}.{fips}.__INTERVENTION_IDX__.json')
        else:
            path = os.path.join(WEB_UI_FOLDER(output_dir), 'state',
                                f'{state_obj.abbr}.__INTERVENTION_IDX__.json')

    elif artifact is RunArtifact.WHITELIST_RESULT:
        path = os.path.join(output_dir, 'api_whitelist.json')

    elif artifact is RunArtifact.BACKTEST_RESULT:
        if agg_level is AggregationLevel.COUNTY:
            path = os.path.join(
                REPORTS_FOLDER(output_dir, state_obj.name),
                f'backtest_results__{state_obj.name}__{county}__{fips}.pdf')
        else:
            path = os.path.join(
                STATE_SUMMARY_FOLDER(output_dir), 'reports',
                f'backtest_results__{state_obj.name}__{fips}.pdf')

    else:
        raise ValueError(f'No paths available for artifact {RunArtifact}')

    os.makedirs(os.path.dirname(path), exist_ok=True)
    return path