예제 #1
0
def plot_fits(generator,
              prediction_times,
              sharex,
              sharey,
              draw_space,
              plot_obs=None,
              plot_uncertainty=False):
    """
    Plot the result and draws from a model generator at some prediction times.

    Args:
        generator: (curvefit.model_generator.ModelPipeline) that has some draws
        prediction_times: (np.array) of prediction times
        sharex: (bool) fix the x axes
        sharey: (bool) fix the y axes
        draw_space: (callable) which curvefit.functions space to plot the draws in
        plot_obs: (optional str) column of observations to plot
        plot_uncertainty: (optional bool) plot the uncertainty intervals
    """
    fig, ax = plt.subplots(len(generator.groups),
                           1,
                           figsize=(8, 4 * len(generator.groups)),
                           sharex=sharex,
                           sharey=sharey)
    if len(generator.groups) == 1:
        ax = [ax]
    for i, group in enumerate(generator.groups):
        draws = generator.draws[group].copy()
        draws = data_translator(data=draws,
                                input_space=generator.predict_space,
                                output_space=draw_space)
        mean_fit = generator.mean_predictions[group].copy()
        mean_fit = data_translator(data=mean_fit,
                                   input_space=generator.predict_space,
                                   output_space=draw_space)
        mean = draws.mean(axis=0)
        ax[i].plot(prediction_times, mean, c='red', linestyle=':')
        ax[i].plot(prediction_times, mean_fit, c='black')

        if plot_uncertainty:
            lower = np.quantile(draws, axis=0, q=0.025)
            upper = np.quantile(draws, axis=0, q=0.975)
            ax[i].plot(prediction_times, lower, c='red', linestyle=':')
            ax[i].plot(prediction_times, upper, c='red', linestyle=':')

        if plot_obs is not None:
            df_data = generator.all_data.loc[generator.all_data[
                generator.col_group] == group].copy()
            ax[i].scatter(df_data[generator.col_t], df_data[plot_obs])

        ax[i].set_title(f"{group} predictions")
예제 #2
0
def test_data_translator_diff(data, input_space, output_space):
    result = data_translator(data, input_space, output_space)
    result[0, 0] = data[0, 0]
    if input_space.startswith('ln'):
        assert np.allclose(np.exp(data), np.cumsum(np.exp(result), axis=1))
    else:
        assert np.allclose(data, np.cumsum(result, axis=1))
예제 #3
0
 def fit(self, data, x_init=None, options=None):
     if self.assert_solver_defined() is True:
         self.solver.fit(data, x_init, options)
         model = self.get_model_instance()
         self.input_curve_fun = model.curve_fun
         params = effects2params(
             self.solver.x_opt,
             model.data_inputs.group_sizes,
             model.data_inputs.covariates_matrices,
             model.param_set.link_fun,
             model.data_inputs.var_link_fun,
             expand=False,
         )
         self.gm_model.set_params(params[:, 0])
         gm_solver = ScipyOpt(self.gm_model)
         data_inputs_gm = DataInputs(
             t=model.data_inputs.t,
             obs=model.data_inputs.obs,
             obs_se=model.data_inputs.obs_se,
         )
         obs_gau_pdf = data_translator(data_inputs_gm.obs, model.curve_fun,
                                       gaussian_pdf)
         data_inputs_gm.obs = obs_gau_pdf
         gm_solver.fit(data_inputs_gm)
         self.x_opt = gm_solver.x_opt
         self.fun_val_opt = gm_solver.fun_val_opt
예제 #4
0
 def _get_translated_observations(self, group, space):
     values = self._get_df(group=group)[self.col_obs].values
     return data_translator(data=values,
                            input_space=self.obs_space,
                            output_space=space)
예제 #5
0
def test_data_translator_exp(data, input_space, output_space):
    result = data_translator(data, input_space, output_space)
    assert np.allclose(data, result)
예제 #6
0
def truncate_draws(t, draws, draw_space, last_day, last_obs, last_obs_space):
    """Truncating draws to the given last day and last obs.

    Args:
        t (np.ndarray):
            Time variables for the draws.
        draws (np.ndarray):
            Draws matrix.
        draw_space (str | callable):
            Which space is the draw in.
        last_day (int | float):
            From which day, should the draws start.
        last_obs (int | float):
            From which observation value, should the draws start.
        last_obs_space (str | callable):
            Which space is the last observation in.

    Returns:
        np.ndarray:
            Truncated draws.
    """
    draw_ndim = draws.ndim
    if draw_ndim == 1:
        draws = draws[None, :]

    assert draws.shape[1] == t.size

    if callable(draw_space):
        draw_space = draw_space.__name__
    if callable(last_obs_space):
        last_obs_space = last_obs_space.__name__

    assert draw_space in [
        'gaussian_cdf', 'gaussian_pdf', 'ln_gaussian_cdf', 'ln_gaussian_pdf'
    ]
    assert last_obs_space in [
        'gaussian_cdf', 'gaussian_pdf', 'ln_gaussian_cdf', 'ln_gaussian_pdf'
    ]

    if last_obs_space == 'gaussian_cdf':
        assert last_obs >= 0.0
    else:
        last_obs = np.exp(last_obs)

    last_day = int(np.round(last_day))
    assert t.min() <= last_day < t.max()

    gaussian_pdf_draws = data_translator(draws, draw_space, 'gaussian_pdf')
    gaussian_pdf_draws = gaussian_pdf_draws[:, last_day + 1:]

    if draw_space == 'gaussian_pdf':
        final_draws = gaussian_pdf_draws
    elif draw_space == 'ln_gaussian_pdf':
        final_draws = data_translator(gaussian_pdf_draws, 'gaussian_pdf',
                                      'ln_gaussian_pdf')
    elif draw_space == 'gaussian_cdf':
        assert last_obs_space in ['gaussian_cdf', 'ln_gaussian_cdf']
        last_obs = last_obs if last_obs_space == 'gaussian_cdf' else np.exp(
            last_obs)
        final_draws = data_translator(gaussian_pdf_draws, 'gaussian_pdf',
                                      'gaussian_cdf') + last_obs
    else:
        assert last_obs_space in ['gaussian_cdf', 'ln_gaussian_cdf']
        last_obs = last_obs if last_obs_space == 'gaussian_cdf' else np.exp(
            last_obs)
        final_draws = data_translator(gaussian_pdf_draws, 'gaussian_pdf',
                                      'gaussian_cdf') + last_obs
        final_draws = np.log(final_draws)

    if draw_ndim == 1:
        final_draws = final_draws.ravel()

    return final_draws
예제 #7
0
 def predict(self, t, predict_fun=None):
     pred_gau_pdf = self.gm_model.predict(self.x_opt, t)
     if predict_fun is None:
         return data_translator(pred_gau_pdf, gaussian_pdf,
                                self.input_curve_fun)
     return data_translator(pred_gau_pdf, gaussian_pdf, predict_fun)