Example #1
0
def adjust_mean_mtslice(tdata_mt, ref=None):
    """Adjust the mean of the mtslice by the given ref
    """
    if ref is None:
        ref = tdata_mt.mean_temp[0]

    study_slices = utils.sizes_to_slices(tdata_mt.study_sizes)
    for i in range(tdata_mt.num_studies):
        obs_mean = tdata_mt.obs_mean[study_slices[i]]
        obs_std = tdata_mt.obs_std[study_slices[i]]
        cov = tdata_mt.daily_temp[study_slices[i]]

        # fit the curve
        if tdata_mt.study_sizes[i] >= 5:
            spline = xspline.xspline(np.array([cov.min(), ref,
                                               cov.max()]),
                                     2,
                                     l_linear=True)
        else:
            spline = xspline.xspline(np.array([cov.min(), cov.max()]), 1)

        beta = utils.fit_spline(obs_mean, obs_std, cov, spline)
        ref_lnrr = spline.designMat(ref).dot(beta)

        # adjust the mean
        tdata_mt.obs_mean[study_slices[i]] -= ref_lnrr

    return tdata_mt
Example #2
0
def offsite_data_at_mean_temp(tdata, mean_temp):
    tdata_at_mean_temp = extract_at_mean_temp(tdata, mean_temp)
    study_slices = utils.sizes_to_slices(tdata_at_mean_temp.study_sizes)
    for i in range(tdata_at_mean_temp.num_studies):
        obs_mean = tdata_at_mean_temp.obs_mean[study_slices[i]]
        obs_std = tdata_at_mean_temp.obs_std[study_slices[i]]
        cov = tdata_at_mean_temp.daily_temp[study_slices[i]]

        # fit the curve
        spline = xspline.xspline(np.array([cov.min(), mean_temp,
                                           cov.max()]),
                                 2,
                                 l_linear=True)
        beta = utils.fit_spline(obs_mean, obs_std, cov, spline)
        ref_lnrr = spline.designMat(mean_temp).dot(beta)

        # shift the data
        tdata_at_mean_temp.obs_mean[study_slices[i]] -= ref_lnrr

        # inflate the std if necessary
        residual = (obs_mean - spline.designMat(cov).dot(beta)) / obs_std
        tdata_at_mean_temp.obs_std[study_slices[i]] *= np.maximum(
            1.0, np.std(residual))

    return tdata_at_mean_temp
Example #3
0
def plot_data_slice(mean_temp, tdata, ylim=None, ax=None, study_range=None):
    """scatter the data at given mean_temp"""
    if ax is None:
        handle = plt
        if ylim is not None: handle.ylim(*ylim)
    else:
        handle = ax
        if ylim is not None: handle.set_ylim(*ylim)

    tdata_amt = process.extract_at_mean_temp(tdata, mean_temp)
    study_slices = utils.sizes_to_slices(tdata_amt.study_sizes)

    if study_range is None:
        study_range = range(0, tdata_amt.num_studies)
    else:
        study_range = range(study_range[0], study_range[1])

    # plot all the points
    for i in study_range:
        s = study_slices[i]
        handle.scatter(tdata_amt.daily_temp[s],
                       tdata_amt.obs_mean[s],
                       s=1.0 / tdata_amt.obs_std[s])

    # plot the trimmed data as red x
    trimming_id = tdata_amt.trimming_weights <= 0.5
    handle.scatter(tdata_amt.daily_temp[trimming_id],
                   tdata_amt.obs_mean[trimming_id],
                   marker='x',
                   color='r')

    return tdata_amt
Example #4
0
def plot_score(trend_result, tdata, mean_temp, outcome, score, 
    dt, shifted_draws, mean_draws, path_to_result_folder):
    """Plot the evidence score and shifted draws."""
    tdata_amt = process.extract_at_mean_temp(tdata, mean_temp)
    study_slices = utils.sizes_to_slices(tdata_amt.study_sizes)
    study_range = range(0, tdata_amt.num_studies)
    
    plt.figure(figsize=((16,8)))
    # plot all the points
    for i in study_range:
        s = study_slices[i]
        plt.scatter(tdata_amt.daily_temp[s],
                    tdata_amt.obs_mean[s] - np.min(mean_draws),
                    s=1.0/tdata_amt.obs_std[s])
    # Trimmed data points
    trimming_id = tdata_amt.trimming_weights <= 0.5
    plt.scatter(tdata_amt.daily_temp[trimming_id],
                   tdata_amt.obs_mean[trimming_id],
                   marker='x',
                   color='r')
    # Upper and lower bound
    plt.fill_between(dt, 
                     np.quantile(shifted_draws, 0.05, axis=1), 
                     np.quantile(shifted_draws, 0.95, axis=1),
                     color='#808080', alpha=0.7)
    # Mean draws after shifted
    plt.plot(dt, np.mean(shifted_draws, axis=1), color='b')
    plt.plot([dt.min(), dt.max()], [0.0, 0.0], "k--")
    plt.xlabel("Daily temperature")
    plt.title(f"Mean temperature at {mean_temp}, score: {np.round(score, 4)}")
    plt.savefig(path_to_result_folder + "/" + outcome + "_score_%i.pdf" % mean_temp,
                bbox_inches="tight")
    plt.close()
Example #5
0
def plot_mtslice(tdata, mt, ylim=None, ax=None, use_colors=True):
    if ax is None:
        ax = plt
        if ylim is not None:
            ax.ylim(ylim[0], ylim[1])
    else:
        if ylim is not None:
            ax.set_ylim(ylim[0], ylim[1])

    tdata_mt = extract_mtslice(tdata, mt)

    if use_colors:
        study_slices = utils.sizes_to_slices(tdata_mt.study_sizes)
        for i in range(tdata_mt.num_studies):
            ax.scatter(tdata_mt.daily_temp[study_slices[i]],
                       tdata_mt.obs_mean[study_slices[i]],
                       s=1.0 / tdata_mt.obs_std[study_slices[i]])
    else:
        ax.scatter(tdata_mt.daily_temp,
                   tdata_mt.obs_mean,
                   s=1.0 / tdata_mt.obs_std)