예제 #1
0
def plotMeasurementOfParameter(data, parameter, value, agg="mean", color=None, label=None):
    agg_func = {"mean": np.mean, "median": np.median, "mode": get_mode_stats, "logmode": get_mode_stats_log}[agg]
    agg_func_err = {"mean": lambda x: np.std(x)/len(x), "median": lambda x: bootstrap_error(x, np.median),
                    "mode": lambda x: bootstrap_error(x, get_mode_stats, repetitions=100), "logmode": lambda x: bootstrap_error(x, get_mode_stats_log, repetitions=100)}[agg]

    if 0:
        plt.violinplot([meas_data[value].values for measurement_id, meas_data in data.groupby(parameter)], positions=np.log10(data[parameter].unique()))
        #data.groupby(parameter)[value].boxplot()
    if 1:
        #agg = data.groupby([parameter, "measurement_id"])[value].agg(agg_func).reset_index()
        # agg_err = data.groupby([parameter, "measurement_id"])[value].agg(get_mode_stats_err).reset_index()
        # agg = data.groupby([parameter, "measurement_id"])[value].mean().reset_index()
        # agg_err = data.groupby([parameter, "measurement_id"])[value].sem().reset_index()
        agg_mean = data.groupby(parameter)[value].agg(agg_func)
        agg_err_mean = data.groupby(parameter)[value].agg(agg_func_err)  # agg(lambda x: 0.5*np.sqrt((x**2).sum()))
        #l = plt.errorbar(np.log10(agg_mean.index), agg_mean.values, agg_err_mean.values, capsize=3, color=color, zorder=2, label=label)
        l = plt.errorbar(agg_mean.index, agg_mean.values, agg_err_mean.values, capsize=3, color=color, zorder=2, label=label)
    else:
        agg = data.groupby([parameter, "measurement_id"])[value].agg(agg_func).reset_index()
        #agg_err = data.groupby([parameter, "measurement_id"])[value].agg(get_mode_stats_err).reset_index()
        #agg = data.groupby([parameter, "measurement_id"])[value].mean().reset_index()
        #agg_err = data.groupby([parameter, "measurement_id"])[value].sem().reset_index()
        agg_mean = agg.groupby(parameter)[value].mean()
        agg_err_mean = agg.groupby(parameter)[value].sem()#agg(lambda x: 0.5*np.sqrt((x**2).sum()))
        l = plt.errorbar(agg_mean.index, agg_mean.values, agg_err_mean.values, capsize=3, color=color, zorder=2, label=label)
        for measurement_id, meas_data in agg.groupby("measurement_id"):
            plt.plot(meas_data[parameter], meas_data[value], "o", ms=3, color=l[0].get_color(), alpha=0.5)
            plt.plot(meas_data[parameter], meas_data[value], "-", ms=3, color="gray", alpha=0.5)
            #plt.text(meas_data[parameter].values[0], meas_data[value].values[0], measurement_id)
    plt.xlabel(parameter)
    plt.ylabel(value)
예제 #2
0
def get_mode_stats(x):
    from deformationcytometer.evaluation.helper_functions import bootstrap_error
    from scipy import stats

    x = np.array(x)
    x = x[~np.isnan(x)]

    def get_mode(x):
        kde = stats.gaussian_kde(x)
        return x[np.argmax(kde(x))]

    mode = get_mode(x)
    err = bootstrap_error(x, get_mode, repetitions=10)

    def string(x):
        if x > 1:
            return str(round(x))
        else:
            return str(round(x, 2))

    plt.text(0.5,
             1,
             string(mode) + "$\pm$" + string(err),
             transform=plt.gca().transAxes,
             ha="center",
             va="top")
    return mode, err, len(x)
예제 #3
0
def plotBinnedData(x, y, bins, bin_func=np.median, error_func=None, color="black", **kwargs):
    x = np.asarray(x)
    y = np.asarray(y)
    strain_av = []
    stress_av = []
    strain_err = []
    for i in range(len(bins) - 1):
        index = (bins[i] < x) & (x < bins[i + 1])
        yy = y[index]
        yy = yy[~np.isnan(yy)]
        if len(yy) == 0:
            continue
        strain_av.append(bin_func(yy))
        # yy = yy[yy>0]
        # strain_err.append(np.std(np.log(yy)) / np.sqrt(len(yy)))
        if error_func is None:
            strain_err.append(bootstrap_error(yy, bin_func))  # np.quantile(yy, [0.25, 0.75]))
        elif error_func == "quantiles":
            strain_err.append(np.abs(np.quantile(yy, [0.25, 0.75])-bin_func(yy)))  # np.quantile(yy, [0.25, 0.75]))

        stress_av.append(np.median(x[index]))
    plot_kwargs = dict(marker='s', mfc="white", mec=color, ms=7, mew=1, lw=0, ecolor='black', elinewidth=1, capsize=3)
    plot_kwargs.update(kwargs)
    plt.errorbar(stress_av, strain_av, yerr=np.array(strain_err).T, **plot_kwargs)
    x, y = np.array(stress_av), np.array(strain_av)
    index = ~np.isnan(x) & ~np.isnan(y)
    x = x[index]
    y = y[index]
    return x, y
예제 #4
0
def get_mode_stats_err(x):
    from scipy import stats
    from deformationcytometer.evaluation.helper_functions import bootstrap_error
    x = np.array(x)
    x = x[~np.isnan(x)&(x>0)]
    def get_mode(x):
        x = np.log(x)
        kde = stats.gaussian_kde(x)
        return np.exp(x[np.argmax(kde(x))])
    #mode = get_mode(x)
    err = bootstrap_error(x, get_mode, repetitions=2)
    return err