コード例 #1
0
    def _load_model_for_region(self, scenario="inferred"):
        """
        Try to load a model for the region, else load the state level model and update parameters
        for the region.
        """
        model = self.regional_input.load_mle_fit_model()
        if model:
            inferred_params = self.regional_input.load_inference_result()
        else:
            _log.info(f"No MLE model found. Reverting to state level.",
                      region=self.regional_input.region)
            model = self.regional_input.load_state_mle_fit_model()
            if model:
                inferred_params = self.regional_input.load_state_inference_result(
                )
            else:
                raise FileNotFoundError(
                    f"Could not locate state result for {self.state_name}")

            # Rescale state values to the county population and replace county
            # specific params.
            # TODO: get_average_seir_parameters should return the analytic solution when available
            # right now it runs an average over the ensemble (with N_samples not consistently set
            # across the code base).
            default_params = ParameterEnsembleGenerator(
                N_samples=500,
                combined_datasets_latest=self.regional_input.latest,
                t_list=model.t_list,
                suppression_policy=model.suppression_policy,
            ).get_average_seir_parameters()
            population_ratio = default_params["N"] / model.N
            model.N *= population_ratio
            model.I_initial *= population_ratio
            model.E_initial *= population_ratio
            model.A_initial *= population_ratio
            model.S_initial = model.N - model.I_initial - model.E_initial - model.A_initial

            for key in {"beds_general", "beds_ICU", "ventilators"}:
                setattr(model, key, default_params[key])

        # Determine the appropriate future suppression policy based on the
        # scenario of interest.

        eps_final = sp.estimate_future_suppression_from_fits(inferred_params,
                                                             scenario=scenario)

        model.suppression_policy = sp.get_epsilon_interpolator(
            eps=inferred_params["eps"],
            t_break=inferred_params["t_break"],
            eps2=inferred_params["eps2"],
            t_delta_phases=inferred_params["t_delta_phases"],
            t_break_final=(datetime.datetime.today() -
                           datetime.datetime.fromisoformat(
                               inferred_params["t0_date"])).days,
            eps_final=eps_final,
        )
        model.run()
        return model
コード例 #2
0
    def run_model(self, R0, eps, t_break, eps2, t_delta_phases,
                  log10_I_initial):
        """
        Generate the model and run.

        Parameters
        ----------
        R0: float
            Basic reproduction number
        eps: float
            Fraction of reduction in contact rates in the second stage.
        t_break: float
            Timing for the switch in suppression policy.
        eps2: float
            Fraction of reduction in contact rates in the third stage
        t_delta_phases: float
            Timing for the switch in from second to third stage.
        log10_I_initial:
            log10 initial infections.

        Returns
        -------
        model: SEIRModel
            The SEIR model that has been run.
        """

        suppression_policy = suppression_policies.get_epsilon_interpolator(
            eps, t_break, eps2, t_delta_phases)

        if self.with_age_structure:
            age_distribution = self.SEIR_kwargs["N"] / self.SEIR_kwargs[
                "N"].sum()
            seir_model = SEIRModelAge
        else:
            age_distribution = 1
            seir_model = SEIRModel

        # Load up some number of initial exposed so the initial flow into
        # infected is stable.
        self.SEIR_kwargs["E_initial"] = (
            self.steady_state_exposed_to_infected_ratio * 10**log10_I_initial *
            age_distribution)

        model = seir_model(
            R0=R0,
            suppression_policy=suppression_policy,
            I_initial=10**log10_I_initial * age_distribution,
            **self.SEIR_kwargs,
        )

        model.run()
        return model
コード例 #3
0
    def _load_model_for_fips(self, scenario="inferred"):
        """
        Try to load a model for the locale, else load the state level model
        and update parameters for the county.
        """
        artifact_path = get_run_artifact_path(self.fips, RunArtifact.MLE_FIT_MODEL)
        if os.path.exists(artifact_path):
            with open(artifact_path, "rb") as f:
                model = pickle.load(f)
            inferred_params = fit_results.load_inference_result(self.fips)

        else:
            _logger.info(
                f"No MLE model found for {self.state_name}: {self.fips}. Reverting to state level."
            )
            artifact_path = get_run_artifact_path(self.fips[:2], RunArtifact.MLE_FIT_MODEL)
            if os.path.exists(artifact_path):
                with open(artifact_path, "rb") as f:
                    model = pickle.load(f)
                inferred_params = fit_results.load_inference_result(self.fips[:2])
            else:
                raise FileNotFoundError(f"Could not locate state result for {self.state_name}")

            # Rescale state values to the county population and replace county
            # specific params.
            # TODO: get_average_seir_parameters should return the analytic solution when available
            # right now it runs an average over the ensemble (with N_samples not consistently set
            # across the code base).
            default_params = ParameterEnsembleGenerator(
                self.fips,
                N_samples=500,
                t_list=model.t_list,
                suppression_policy=model.suppression_policy,
            ).get_average_seir_parameters()
            population_ratio = default_params["N"] / model.N
            model.N *= population_ratio
            model.I_initial *= population_ratio
            model.E_initial *= population_ratio
            model.A_initial *= population_ratio
            model.S_initial = model.N - model.I_initial - model.E_initial - model.A_initial

            for key in {"beds_general", "beds_ICU", "ventilators"}:
                setattr(model, key, default_params[key])

        # Determine the appropriate future suppression policy based on the
        # scenario of interest.

        eps_final = sp.estimate_future_suppression_from_fits(inferred_params, scenario=scenario)

        model.suppression_policy = sp.get_epsilon_interpolator(
            eps=inferred_params["eps"],
            t_break=inferred_params["t_break"],
            eps2=inferred_params["eps2"],
            t_delta_phases=inferred_params["t_delta_phases"],
            t_break_final=(
                datetime.datetime.today()
                - datetime.datetime.fromisoformat(inferred_params["t0_date"])
            ).days,
            eps_final=eps_final,
        )
        model.run()
        return model