Exemple #1
0
def plot_2d_hist(x,
                 y,
                 z,
                 ax=None,
                 xrange=None,
                 yrange=None,
                 show=True,
                 smooth=None,
                 contour=False):
    """
    """
    if ax is None:
        fig, ax = plt.subplots()

    xsorted = np.unique(x)
    ysorted = np.unique(y)
    dx2 = (xsorted[1] - xsorted[0]) / 2
    dy2 = (ysorted[1] - ysorted[0]) / 2
    extent = (x.min() - dx2, x.max() + dx2, y.min() - dy2, y.max() + dy2
              )  # l r b t

    nx = np.unique(x).size
    ny = np.unique(y).size
    zz = z.reshape(nx, ny).T

    if smooth:
        zz = gaussian_filter(zz, smooth)

    # Display the thresholded image
    ax.imshow(zz, origin="lower", cmap="binary", extent=extent, aspect="auto")

    if contour:
        levels = [
            confidence_threshold(zz, 0.997),
            confidence_threshold(zz, 0.95),
            confidence_threshold(zz, 0.68)
        ]
        ax.contour(zz,
                   colors="b",
                   linewidths=1,
                   zorder=10,
                   levels=levels,
                   extent=extent)

    # Format axes
    if xrange is not None:
        ax.set_xlim(*xrange)
    if yrange is not None:
        ax.set_ylim(*yrange)
    if (xrange is not None) and (yrange is not None):
        ax.set_aspect((xrange[1] - xrange[0]) / (yrange[1] - yrange[0]))

    if show:
        plt.show(block=False)
    return ax
Exemple #2
0
def contour_plot_threshold(ax, df, legend=True, fontsize=15):

    xkey = "rec_phys_alpha"
    ykey = "uae_phys_k"
    zkey = "poisson_likelihood_2d"

    x = df[xkey].values
    y = df[ykey].values
    z = df[zkey].values
    extent = (y.min(), y.max(), x.min(), x.max())

    nx = np.unique(x).size
    ny = np.unique(y).size

    zz = z.reshape(nx, ny)
    zzexp = np.exp(zz+1000)

    levels = (confidence_threshold(zzexp, 0.999999426696856),
              confidence_threshold(zzexp, 0.999936657516334),
              confidence_threshold(zzexp, 0.997),
              confidence_threshold(zzexp, 0.95),
              confidence_threshold(zzexp, 0.68))

    labels = r">$5\sigma$", r"$5\sigma$", r"$4\sigma$", r"$3\sigma$", r"$2\sigma$",  r"$1\sigma$"

    tmap = np.zeros_like(zz)
    for level in levels:
        tmap[zzexp >= level] += 1

    ax.imshow(tmap, origin="lower", cmap="binary", extent=extent, aspect="auto")

    xrange = 0, 1.2
    yrange = 2.4, 5.2

    # ax.plot(xrange, [4.4, 4.4], 'r--')
    # ax.plot(xrange, [3.71, 3.71], 'b--')
    plot_ext(ax)

    bounds = np.array([0, 1, 2, 3, 4, 5, 6])
    cmap = plt.cm.binary
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    axb = inset_axes(ax, width="55%", height="3%", loc=1, borderpad=0.8)
    ticks = bounds+0.5
    cb = mpl.colorbar.ColorbarBase(axb, cmap=cmap, norm=norm, spacing='proportional', ticks=ticks,
                                   boundaries=bounds, format='%1i', orientation='horizontal')
    cb.ax.set_xticklabels(labels, fontsize=fontsize-4)

    ax.set_xlim(*xrange)
    ax.set_ylim(*yrange)
    ax.set_aspect((xrange[1]-xrange[0])/(yrange[1]-yrange[0]))
    ax.set_xlabel("$k$", fontsize=fontsize)
    ax.set_ylabel(r"$\alpha$", fontsize=fontsize)
    if legend:
        ax.legend(loc="lower left", frameon=False, fontsize=fontsize-4)
Exemple #3
0
def contour_plot_gauss(ax, df, fontsize=15):

    xkey = "rec_phys_alpha"
    ykey = "uae_phys_k"
    zkey = "poisson_likelihood_2d"

    x = df[xkey].values
    y = df[ykey].values
    z = np.exp(df[zkey].values + 1000)
    extent = (y.min(), y.max(), x.min(), x.max())

    nx = np.unique(x).size
    ny = np.unique(y).size

    mean = (np.average(x, weights=z), np.average(y, weights=z))
    cov = np.cov(x, y, aweights=z)
    norm = stats.multivariate_normal(mean=mean, cov=cov)

    zz = norm.pdf(np.vstack([x, y]).T).reshape(nx, ny)

    levels = (confidence_threshold(zz, 0.999999426696856),
              confidence_threshold(zz, 0.997),
              confidence_threshold(zz, 0.95),
              confidence_threshold(zz, 0.68))
    colors = "k"
    contour_labels = r"$5\sigma$", r"$3\sigma$", r"$2\sigma$", r"$1\sigma$"

    cs = ax.contour(zz, linewidths=0.8, colors=colors, extent=extent, levels=levels)
    fmt = {}
    for l, s in zip(cs.levels, contour_labels):
        fmt[l] = s
    ax.clabel(cs, cs.levels, inline=True, fmt=fmt, fontsize=10)

    xrange = 0, 1.2
    yrange = 2.5, 5.2

    ax.plot(xrange, [4.4, 4.4], 'b--')
    ax.plot(xrange, [3.71, 3.71], 'r--')
    ax.axhspan(4.4-0.19, 4.4+0.19, color="b", alpha=0.2,
               label="vdB+16 (clusters), Amorisco+16")
    ax.axhspan(3.71-0.33, 3.71+0.33, color="r", alpha=0.2,
               label="vdB+17 (groups)")

    ax.set_xlim(*xrange)
    ax.set_ylim(2.5, 5.2)
    ax.set_aspect((xrange[1]-xrange[0])/(yrange[1]-yrange[0]))
    ax.set_xlabel("$k$", fontsize=fontsize)
    ax.set_ylabel(r"$\alpha$", fontsize=fontsize)
    ax.legend(loc="best")
Exemple #4
0
def contour_plot(ax, df, smooth=False):

    xkey = "rec_phys_alpha"
    ykey = "uae_phys_k"
    zkey = "poisson_likelihood_2d"

    x = df[xkey].values
    y = df[ykey].values
    z = df[zkey].values
    extent = (y.min(), y.max(), x.min(), x.max())

    nx = np.unique(x).size
    ny = np.unique(y).size

    zz = z.reshape(nx, ny)
    zzexp = np.exp(zz+1000)

    if smooth:
        zzexp = gaussian_filter(zzexp, 0.5)

    levels = (confidence_threshold(zzexp, 0.999999426696856),
              confidence_threshold(zzexp, 0.997),
              confidence_threshold(zzexp, 0.95),
              confidence_threshold(zzexp, 0.68))
    colors = "k"
    contour_labels = r"$5\sigma$", r"$3\sigma$", r"$2\sigma$", r"$1\sigma$"

    cs = ax.contour(zzexp, linewidths=0.8, colors=colors, extent=extent, levels=levels)
    fmt = {}
    for l, s in zip(cs.levels, contour_labels):
        fmt[l] = s
    ax.clabel(cs, cs.levels, inline=True, fmt=fmt, fontsize=10)

    xrange = 0, 1.2
    yrange = 2.5, 5.2
    fontsize = 15

    ax.plot(xrange, [4.4, 4.4], 'b--', label="van der Burg +16 (clusters), Amorisco +16")
    ax.plot(xrange, [3.71, 3.71], 'r--', label="van der Burg +17 (groups)")
    ax.axhspan(4.4-0.19, 4.4+0.19, color="b", alpha=0.2)
    ax.axhspan(3.71-0.33, 3.71+0.33, color="r", alpha=0.2)

    ax.set_xlim(*xrange)
    ax.set_ylim(2.5, 5.2)
    ax.set_aspect((xrange[1]-xrange[0])/(yrange[1]-yrange[0]))
    ax.set_xlabel("$k$", fontsize=fontsize)
    ax.set_ylabel(r"$\alpha$", fontsize=fontsize)
    ax.legend(loc="best")
Exemple #5
0
    def _identify_confident(self, metric=None, q=0.9):
        """ Identify best models within confidence interval. """
        if metric is None:
            metric = self._default_metric

        df = self.load_metrics()

        values = df[metric].values
        threshold = confidence_threshold(values, q=q)
        cond = values >= threshold

        self.logger.debug(f"Identified {cond.sum()} models for q={q:.2f}.")

        return cond
Exemple #6
0
def threshold_plot(x,
                   y,
                   z,
                   ax=None,
                   legend=True,
                   xrange=None,
                   yrange=None,
                   fontsize=15,
                   show=True,
                   xlabel=None,
                   ylabel=None,
                   smooth=None,
                   **kwargs):
    """
    """
    if ax is None:
        fig, ax = plt.subplots(**kwargs)

    extent = (x.min(), x.max(), y.min(), y.max())  # l r b t

    nx = np.unique(x).size
    ny = np.unique(y).size

    zz = z.reshape(nx, ny).T

    if smooth is not None:
        zz = gaussian_filter(zz, smooth)

    # Identify the thresholds and make thresholded image
    levels = (confidence_threshold(zz, 0.999999426696856),
              confidence_threshold(zz, 0.999936657516334),
              confidence_threshold(zz, 0.997), confidence_threshold(zz, 0.95),
              confidence_threshold(zz, 0.68))
    labels = r">$5\sigma$", r"$5\sigma$", r"$4\sigma$", r"$3\sigma$", r"$2\sigma$", r"$1\sigma$"

    levels = list(levels)[1:]
    labels = list(labels)[1:]
    labels[0] = ">" + labels[1]

    tmap = np.zeros_like(zz)
    for level in levels:
        tmap[zz >= level] += 1

    # Display the thresholded image
    ax.imshow(tmap,
              origin="lower",
              cmap="binary",
              extent=extent,
              aspect="auto")

    # Add the inset discrete threshold colourbar
    # bounds = np.array([0, 1, 2, 3, 4, 5, 6])
    bounds = np.arange(len(levels) + 2)
    cmap = plt.cm.binary
    norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
    axb = inset_axes(ax, width="55%", height="3%", loc=1, borderpad=0.8)
    ticks = bounds + 0.5
    cb = mpl.colorbar.ColorbarBase(axb,
                                   cmap=cmap,
                                   norm=norm,
                                   spacing='proportional',
                                   ticks=ticks,
                                   boundaries=bounds,
                                   format='%1i',
                                   orientation='horizontal')
    cb.ax.set_xticklabels(labels, fontsize=fontsize - 4)

    # Format axes
    if xrange is not None:
        ax.set_xlim(*xrange)
    if yrange is not None:
        ax.set_ylim(*yrange)
    if (xrange is not None) and (yrange is not None):
        ax.set_aspect((xrange[1] - xrange[0]) / (yrange[1] - yrange[0]))
    ax.set_xlabel(xlabel, fontsize=fontsize)
    ax.set_ylabel(ylabel, fontsize=fontsize)

    if legend:
        ax.legend(loc="lower left", frameon=False, fontsize=fontsize - 4)
    if show:
        plt.show(block=False)
    return ax