コード例 #1
0
ファイル: plots.py プロジェクト: scarrazza/smpdf
def plot_bindist(obs_table, b, base_pdf=None):

    def _kde_method(X, coords):
            kde = mlab.GaussianKDE(X, None)
            return kde.evaluate(coords)

    obs = obs_table.Observable.unique()
    if len(obs) != 1:
        raise ValueError("Must be only one observable")
    obs = obs[0]
    figure, ax = plt.subplots()

    plt.title("%s [Bin %d]" % (obs, b+1))
    colors  = plotutils.color_names_to_rgb(colorlist)
    alpha = 1
    if base_pdf:
        base = obs_table[obs_table.PDF.get_values()==base_pdf].Result[0]
    else:
        base = None
    results = obs_table.Result.unique(
    )
    for result in results:
        if base is not None:
            cv = base.central_value.as_matrix()
            data = result._violin_data(rel_to=cv)
        else:
            data = data = result._violin_data()

        if isinstance(data, list):
            stats = data[b]
        else:
            stats = violin_stats(data, _kde_method)[b]

        color = next(colors)
        alphacolor = color + (alpha,)
        plt.plot(stats['coords'], stats['vals'], color=color,label=result.pdf.label)
        plt.fill_between(stats['coords'], 0, stats['vals'], color=alphacolor,
                 )

        alpha /= 2


    plt.ylabel("Distribution")
    if base_pdf:
        plt.xlabel('Ratio to %s' % base_pdf.label)
    else:
        plt.xlabel("Observable value")

    ax.yaxis.set_major_locator(MaxNLocator(nbins=10, prune="both"))
    ax.xaxis.set_major_locator(MaxNLocator(nbins=8, prune="both"))

    plt.legend()
    yield (obs, b), figure
コード例 #2
0
ファイル: plots.py プロジェクト: scarrazza/smpdf
def compare_violins(results, base_pdf = None):
    if not isinstance(results, dict):
        combined = aggregate_results(results)
    else:
        combined = results
    for obs in combined:
        figure = plt.figure()
        norms = None
        handles = []
        plt.title(str(obs),  y=1.05)
        colors  = plotutils.color_names_to_rgb(colorlist)
        alpha = 1
        base = combined[obs].get(base_pdf, None)
        results = sorted(combined[obs].values(), key = lambda x: x!=base)
        for result in results:
            if base is not None:
                cv = base.central_value.as_matrix()
                data = result._violin_data(rel_to=cv)
            else:
                data = data = result._violin_data()
            color = next(colors) + (alpha,)
            alpha /= 2
            plot, handle, norms = result.violin_plot(data, color=color,
                                              showextrema=False,
                                              normvalues=norms)
            handles.append(handle)
        plt.xlabel('bins')
        if base_pdf:
            plt.ylabel('Ratio to %s' % base_pdf.label)
        else:
            plt.ylabel("Observable value")
        plt.xticks(range(1,len(result.central_value) + 1))

        #Small style
        dfs = plt.yticks()[0] - 1
        l = len(dfs) // 2  + 1 - ((len(dfs) // 2) % 2)
        mdiff = np.max(dfs)

        plt.yticks(np.linspace(-mdiff, mdiff, l) + 1)
        #

        plt.legend(handles=handles)
        yield (obs,), figure
コード例 #3
0
ファイル: plots.py プロジェクト: scarrazza/smpdf
def compare_cis(results, base_pdf = None):
    if not isinstance(results, dict):
        combined = aggregate_results(results)
    else:
        combined = results
    for obs in combined:
        figure = plt.figure()
        colors  = plotutils.color_names_to_rgb(colorlist)
        plt.title(str(obs), y=1.05)
        base = combined[obs].get(base_pdf, None)
        results = sorted(combined[obs].values(), key = lambda x: x!=base)
        l = len(results)
        if l < 5:
            delta = iter(np.linspace(-0.05*l, 0.05*l, l))
        else:
            delta = iter(np.linspace(-0.25, 0.25, l))
        #x = iter(np.arange(1, len(results) + 1))
        for result in results:
            x = np.arange(1, result.nbins+1) + next(delta)
            #Without copy the /= operator affects the underlying data.
            data_cv = result.central_value.as_matrix().copy()
            data_ci = result.errorbar68.as_matrix().copy()
            if base is not None:
                base_cv = base.central_value.as_matrix()
                data_cv /= base_cv
                data_ci /= base_cv[:,np.newaxis]
            data_ci = np.abs(data_ci)
            color = next(colors)
            plt.errorbar(x, data_cv, yerr=data_ci.T,
                                     linestyle='none',
                                     color=color,
                                     label=result.pdf.label, elinewidth = 2,
                                     capsize=10)
            if isinstance(result, MCResult):
                #Without copy the /= operator affects the underlying data.
                data_std = result.rel_std_interval().as_matrix().copy()
                if base is not None:
                    data_std /= base_cv[:,np.newaxis]
                data_std = np.abs(data_std)
                plt.errorbar(x, data_cv, yerr=data_std.T,
                                         linestyle='none',
                                         color=color,
                                         elinewidth = 2,
                                         capsize=15)

        plt.xlabel('bins')
        if base_pdf:
            plt.ylabel('Ratio to %s' % base_pdf.label)
        else:
            plt.ylabel("Observable value")
        plt.xticks(range(1,len(result.central_value) + 1))
        plt.legend()
        plt.xlim(0.5, results[0].nbins + 0.5)
        plt.grid(axis='x')
        #Small style
        dfs = plt.yticks()[0] - 1
        l = len(dfs) // 2  + 1 - ((len(dfs) // 2) % 2)
        mdiff = np.max(dfs)

        plt.yticks(np.linspace(-mdiff, mdiff, l) + 1)

        yield (obs,), figure