def plot_ratio(df,
               bins=[np.linspace(0, .25, 100),
                     np.linspace(0, 1, 100)],
               alpha=1,
               cmap=None,
               mc_field='mc',
               stdc_field='stdc',
               response_field='response'):
    C, M = plt.meshgrid(*bins)
    resp1 = plt.histogram2d(
        df[df.loc[:, response_field] == 1].loc[:, stdc_field].values,
        df[df.loc[:, response_field] == 1].loc[:, mc_field].values,
        bins=bins)[0] + 1
    #resp1[resp1==1] = np.nan
    resp2 = plt.histogram2d(
        df[df.loc[:, response_field] == -1].loc[:, stdc_field].values,
        df[df.loc[:, response_field] == -1].loc[:, mc_field].values,
        bins=bins)[0] + 1
    #resp2[resp2==1] = np.nan
    resp1 = resp1.astype(float) / np.nansum(resp1)
    resp2 = resp2.astype(float) / np.nansum(resp2)
    plane = np.log(resp1 / resp2)
    #plane[plane==1] = np.nan
    plt.pcolormesh(bins[1],
                   bins[0],
                   np.ma.masked_invalid(plane),
                   cmap=cmap,
                   vmin=-2.4,
                   vmax=2.4)
def hist2d(MC, varname1, varname2, varslice=None,
           percentiles=[0.0027,0.0455,0.3173,0.5,0.75],
           colors=[(0.4,0.4,1,0.2),(1,0.4,1,0.5),(1,0.2,0.2,0.5),(0.7,0.1,0.1,1),(0.5,0.05,0.05,1),(0.4,0.05,0.05,0.5)],
           ticklabels=['3$\\sigma$','2$\\sigma$','1$\\sigma$','50%','25%'],
           axis=None,
           fignum=1,
           contourcmd=pylab.contourf,
           clear=False,
           colorbar=True,
           doerrellipse=False,
           chain=None,
           **kwargs):
    """
    Create a 2D histogram of the MCMC data over some Trace range
    """
    try: # if input is just a dict of arrays
        if varslice is None:
            histvals,xvals,yvals = pylab.histogram2d(MC[varname1].squeeze(),MC[varname2].squeeze(),**kwargs)
        else:
            histvals,xvals,yvals = pylab.histogram2d(MC[varname1][slice(*varslice)].squeeze(),MC[varname2][slice(*varslice)].squeeze(),**kwargs)
    except TypeError:
        if varslice is None:
            histvals,xvals,yvals = pylab.histogram2d(
                MC.trace(varname1,chain=chain)[:].squeeze(),
                MC.trace(varname2,chain=chain)[:].squeeze(),
                **kwargs)
        else:
            histvals,xvals,yvals = pylab.histogram2d(
                MC.trace(varname1,chain=chain)[slice(*varslice)].squeeze(),
                MC.trace(varname2,chain=chain)[slice(*varslice)].squeeze(),
                **kwargs)

    levels = [find_percentile(histvals, p*100) for p in percentiles]

    if axis is None:
        pylab.figure(fignum)
        if clear:
            pylab.clf()
        axis = pylab.gca()

    xax = np.linspace(xvals.min(),xvals.max(),histvals.shape[1])
    yax = np.linspace(yvals.min(),yvals.max(),histvals.shape[0])
    if axis is not None:
        contourcmd = eval('axis.'+contourcmd.__name__)
    cntr = contourcmd(xax, yax, histvals.swapaxes(0,1), np.unique(levels+[histvals.max()]), colors=colors)
    # hack to fix opacity
    axis.set_xlabel(varname1)
    axis.set_ylabel(varname2)
    if colorbar:
        try:
            cb = pylab.colorbar(cntr, ax=axis)
            cb.ax.set_yticks(levels)
            cb.ax.set_yticklabels(ticklabels)
        except Exception as e:
            print("Colorbar failed with exception {0}".format(e))

    if doerrellipse:
        errellipse(MC,varname1,varname2)
    return axis
Beispiel #3
0
def plot_model(
    df,
    model,
    bins=[np.linspace(0, 0.25, 100), np.linspace(0, 1, 100)],
    hyperplane_only=False,
    alpha=1,
    cmap=None,
):
    C, M = np.meshgrid(*bins)
    resp1 = (
        plt.histogram2d(
            df[df.response == 1].stdc.values, df[df.response == 1].mc.values, bins=bins
        )[0]
        + 1
    )
    resp1[resp1 == 1] = np.nan
    resp2 = (
        plt.histogram2d(
            df[df.response == -1].stdc.values,
            df[df.response == -1].mc.values,
            bins=bins,
        )[0]
        + 1
    )
    resp2[resp2 == 1] = np.nan
    resp1 = resp1.astype(float) / np.nansum(resp1)
    resp2 = resp2.astype(float) / np.nansum(resp2)

    p = model.predict(np.vstack([M.ravel(), C.ravel(), 0 * np.ones(M.shape).ravel()]).T)
    p = p.reshape(M.shape)

    decision = (
        lambda x: -(model.params.mc * x + model.params.Intercept) / model.params.stdc
    )
    if not hyperplane_only:
        plane = np.log(resp1 / resp2)
        plane[plane == 1] = np.nan
        pcol = plt.pcolormesh(
            bins[1],
            bins[0],
            np.ma.masked_invalid(plane),
            cmap=cmap,
            vmin=-2.4,
            vmax=2.4,
            rasterized=True,
            linewidth=0,
        )
        pcol.set_edgecolor("face")

    mind, maxd = plt.xlim()
    plt.ylim(bins[0][0], bins[0][-1])
    plt.xlim(bins[1][0], bins[1][-1])
    plt.plot([mind, maxd], [decision(mind), decision(maxd)], "k", lw=2, alpha=alpha)
    plt.plot([0, 0], [bins[0][0], bins[0][-1]], "k--", lw=2)
    # ylim([0, 0.25])
    # xlim([0, 1])
    # contour(bins[1], bins[0], p.T, [0.5])
    return bins, p
def plot_model(df,
               model,
               bins=[np.linspace(0, .25, 100),
                     np.linspace(0, 1, 100)],
               hyperplane_only=False,
               alpha=1,
               cmap=None,
               mc_field='mc',
               stdc_field='stdc',
               response_field='response',
               plot_hyperplane=True):
    C, M = plt.meshgrid(*bins)
    resp1 = plt.histogram2d(
        df[df.loc[:, response_field] == 1].loc[:, stdc_field].values,
        df[df.loc[:, response_field] == 1].loc[:, mc_field].values,
        bins=bins)[0] + 1
    #resp1[resp1==1] = np.nan
    resp2 = plt.histogram2d(
        df[df.loc[:, response_field] == -1].loc[:, stdc_field].values,
        df[df.loc[:, response_field] == -1].loc[:, mc_field].values,
        bins=bins)[0] + 1
    #resp2[resp2==1] = np.nan
    resp1 = resp1.astype(float) / np.nansum(resp1)
    resp2 = resp2.astype(float) / np.nansum(resp2)

    p = model.predict(
        np.vstack([M.ravel(),
                   C.ravel(), 0 * np.ones(M.shape).ravel()]).T)
    p = p.reshape(M.shape)

    decision = lambda x: - \
        (model.params[mc_field] * x + model.params.Intercept) / \
        model.params[stdc_field]
    if not hyperplane_only:

        plane = np.log(resp1 / resp2)
        #plane[plane==1] = np.nan
        plt.pcolormesh(bins[1],
                       bins[0],
                       np.ma.masked_invalid(plane),
                       cmap=cmap,
                       vmin=-2.4,
                       vmax=2.4)

    mind, maxd = plt.xlim()
    plt.ylim(bins[0][0], bins[0][-1])
    plt.xlim(bins[1][0], bins[1][-1])
    if plot_hyperplane:
        plt.plot([mind, maxd], [decision(mind), decision(maxd)],
                 'k',
                 lw=2,
                 alpha=alpha)
    plt.plot([0, 0], [bins[0][0], bins[0][-1]], 'k--', lw=2)
    return bins
def plot_phases(in_file, plot_type, plot_log):
    flags = ['histogram','phases']
    plot_flag = 0
    log_flag = 0

    def no_log(x):
        return x

    fig = pylab.figure(1)
    ax = fig.add_subplot(111)

    try:
        img = spimage.sp_image_read(in_file,0)
    except:
        raise IOError("Can't read %s." % in_file)

    values = img.image.reshape(pylab.size(img.image))

    if plot_log:
        log_function = pylab.log
    else:
        log_function = no_log

    if plot_type == PHASES:
        hist = pylab.histogram(pylab.angle(values),bins=500)
        ax.plot((hist[1][:-1]+hist[1][1:])/2.0,log_function(hist[0]))
    elif plot_flag == HISTOGRAM:
        hist = pylab.histogram2d(pylab.real(values),pylab.imag(values),bins=500)
        ax.imshow(log_function(hist[0]),extent=(hist[2][0],hist[2][-1],-hist[1][-1],-hist[1][0]),interpolation='nearest')
    else:
        ax.plot(pylab.real(values),pylab.imag(values),'.')
    return fig
Beispiel #6
0
def plot_phases(in_file, plot_type, plot_log):
    plot_flag = 0

    def no_log(x):
        return x

    fig = pylab.figure(1)
    ax = fig.add_subplot(111)

    try:
        img = spimage.sp_image_read(in_file, 0)
    except IOError:
        raise IOError("Can't read %s." % in_file)

    values = img.image.reshape(pylab.size(img.image))

    if plot_log:
        log_function = pylab.log
    else:
        log_function = no_log

    if plot_type == PHASES:
        hist = pylab.histogram(pylab.angle(values), bins=500)
        ax.plot((hist[1][:-1] + hist[1][1:]) / 2, log_function(hist[0]))
    elif plot_flag == HISTOGRAM:
        hist = pylab.histogram2d(pylab.real(values),
                                 pylab.imag(values),
                                 bins=500)
        ax.imshow(log_function(hist[0]),
                  extent=(hist[2][0], hist[2][-1], -hist[1][-1], -hist[1][0]),
                  interpolation='nearest')
    else:
        ax.plot(pylab.real(values), pylab.imag(values), '.')
    return fig
Beispiel #7
0
def plot_joint_mass_age_pdf(cldata):
    """ plot p(logmass, logage | cluster) """

    grid = cldata['match']['grid']
    pr = np.exp(-0.5 * grid['fit'])
    y = np.unique(grid['age'])
    dlogm = 0.1
    bins = (np.arange(dlogm, 6, dlogm), get_bins_from_center(y))

    n, bx, by = plt.histogram2d(grid['mass'],
                                grid['age'],
                                weights=pr,
                                bins=bins)
    floor = n[n > 0].min()
    plt.imshow(np.log10(n + 1e-3 * floor).T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.magma,
               aspect='auto',
               interpolation='nearest')
    plt.contour(n.T,
                figrc.nice_sigma_levels(n, [1, 2, 3]),
                extent=(bx[0], bx[-1], by[0], by[-1]),
                colors='k')
    plt.xlabel('Log mass')
    plt.ylabel('Log age')
Beispiel #8
0
def probcontour(xarr,yarr,style='black',smooth=2,bins=100,weights=None,label=None):
    from scipy import ndimage
    import numpy
    H,xbins,ybins = pylab.histogram2d(xarr,yarr,bins=bins,weights=weights)

    H = ndimage.gaussian_filter(H,smooth)
    sortH = numpy.sort(H.flatten())
    cumH = sortH.cumsum()
# 1, 2, 3-sigma, for the old school:
    lvl68 = sortH[cumH>cumH.max()*0.32].min()
    lvl95 = sortH[cumH>cumH.max()*0.05].min()
    lvl99 = sortH[cumH>cumH.max()*0.003].min()
    if style == 'black':
        pylab.contour(H.T,[lvl68,lvl95,lvl99],colors=style,\
                          extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))

    else:
        # Shaded areas:
        if type(style) == str:
            pylab.contourf(H.T,[lvl99,lvl95],colors=style,alpha=0.2,\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl95,lvl68],colors=style,alpha=0.5,\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl68,1e8],colors=style,alpha=0.9,\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]),label=label)
        else:
            pylab.contourf(H.T,[lvl99,lvl95],colors=rgb_to_hex(rgb_alpha(style,0.2)),\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl95,lvl68],colors=rgb_to_hex(rgb_alpha(style,0.5)),\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl68,1e8],colors=rgb_to_hex(rgb_alpha(style,0.9)),\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]),label=label)
Beispiel #9
0
def get_surfaces(x, y, filter, bins):
    '''
    Compute how often x and y occured before a specific
    choice.

    Parameters
    ----------
    x, y: array containing mean contrast and std respectively
    filter: array
        True for choice 1, false for choice 2
    bins: 2-list of arrays
        Specifies edges for the 2D histogram. 
    '''
    idx = filter > 0
    resp1 = plt.histogram2d(x[idx], y[idx], bins=bins)[0]
    resp2 = plt.histogram2d(x[~idx], y[~idx], bins=bins)[0]
    return resp1, resp2
def expected_mean(data, bins, levels=[0.5]):
    resp1 = plt.histogram2d(data.trans_stdc.values,
                            data.trans_mc.values,
                            weights=data.mc,
                            bins=bins)[0]
    resp_count = plt.histogram2d(data.trans_stdc.values,
                                 data.trans_mc.values,
                                 bins=bins)[0]
    expected = resp1 / resp_count.astype(float)
    CS = plt.contour(center(bins[1]),
                     center(bins[0]),
                     np.ma.masked_invalid(expected),
                     levels,
                     colors='m')
    zc = CS.collections[0]
    plt.setp(zc, linewidth=2.5)
    return expected
def draw_otf_spectrum(cube, figure=None, subplot=111, title='', grid=True,
                      font_family=None, tick_labels_size=9, xlim=None, ylim=None,
                      show=True, *args, **kwargs):
    import time
    import numpy
    import analyse
    import analyse.plotter
    import matplotlib.figure
    import matplotlib.colors
    import pylab
    t0 = time.time()
    print('[%f] -- %f'%(time.time(), time.time()-t0))
    if not isinstance(figure, matplotlib.figure.Figure):
        if figure is None:
            figure = pylab.figure()
        else:
            figure = pylab.figure(figsize=figure)
    else:
        show = False

    if type(subplot) is int:
        subplot = analyse.plotter.get_subplot(subplot)
    elif type(subplot) in [list, tuple]:
        if len(subplot) == 3:
            subplot = analyse.plotter.get_subplot(subplot)

    nz, ny, nx = cube.data.shape
    spectra = cube.data.T.reshape(nx*ny, nz)
    v = analyse.generate_axis(cube, axis=3) / 1000.

    ax = figure.add_axes(subplot)
    #[ax.plot(v, s, '.b', ms=1, mew=0, alpha=0.1) for s in spectra]
    xd = numpy.array(list(v) * len(spectra))
    yd = spectra.ravel()
    yd[numpy.isnan(yd)] = 0
    histmap, histx, histy = pylab.histogram2d(xd, yd, bins=501)
    histmap = histmap.T
    histmap[numpy.where(histmap==0)] = 0.3
    histmap = numpy.log10(histmap)
    histX, histY = numpy.meshgrid(histx, histy)
    ax.pcolormesh(histX, histY, histmap, vmax=3.3)
    ax.grid(grid)
    ax.set_title(title, size=tick_labels_size+1)
    if xlim is not None: ax.set_xlim(*xlim)
    else: ax.set_xlim(xd.min(), xd.max())
    if ylim is not None: ax.set_ylim(*ylim)
    else: ax.set_ylim(yd.min(), yd.max())
    tx = ax.get_xticks()
    ty = ax.get_yticks()
    ax.set_xticklabels(tx, size=tick_labels_size)
    ax.set_yticklabels(ty, size=tick_labels_size)

    if show:
        pylab.show()

    print('[%f] -- %f'%(time.time(), time.time()-t0))
    return figure
Beispiel #12
0
def mi(x,y, bins=11):
    """Given two arrays x and y of equal length, return their mutual information in bits
    """
    Hxy, xe, ye = pylab.histogram2d(x,y,bins=bins)
    Hx = Hxy.sum(axis=1)
    Hy = Hxy.sum(axis=0)
    Pxy = Hxy/float(x.size)
    Px = Hx/float(x.size)
    Py = Hy/float(x.size)
    pxy = Pxy.ravel()
    px = Px.repeat(Py.size)
    py = pylab.tile(Py, Px.size)
    idx = pylab.find((pxy > 0) & (px > 0) & (py > 0))
    return (pxy[idx]*pylab.log2(pxy[idx]/(px[idx]*py[idx]))).sum()
def plot_pm_radial():
    """
    Plot a historgram of stars moving in a radial direction (in the
    cluster outskirts and compare to the average histogram in all other
    directions.
    """
    d = atpy.Table(wd1_data)

    idx = np.where(d.P > 0.9)

    # Identify the clusters central position
    # h, xedges, yedges = py.histogram2d(d.x_F814W, d.y_F814W, weights=d.P)
    h, xedges, yedges = py.histogram2d(d.x_F814W[idx], d.y_F814W[idx])
    extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
    py.imshow(h, extent=extent, interpolation=None)
    py.colorbar()
def plot_pm_radial():
    """
    Plot a historgram of stars moving in a radial direction (in the
    cluster outskirts and compare to the average histogram in all other
    directions.
    """
    d = atpy.Table(wd1_data)

    idx = np.where(d.P > 0.9)

    # Identify the clusters central position
    # h, xedges, yedges = py.histogram2d(d.x_F814W, d.y_F814W, weights=d.P)
    h, xedges, yedges = py.histogram2d(d.x_F814W[idx], d.y_F814W[idx])
    extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
    py.imshow(h, extent=extent, interpolation=None)
    py.colorbar()
Beispiel #15
0
    def update_histogram_plt_data(self, histogram_save_data):
        self.hist_data = histogram_save_data
        bins = (self.Gbins, self.Dbins)

        G_breaking = histogram_save_data.get_G_breaking()
        D_breaking = histogram_save_data.get_D_breaking()
        G_making = histogram_save_data.get_G_making()

        break_histogram = pl.histogram(G_breaking, self.Gbins)
        make_histogram = pl.histogram(G_making, self.Gbins)

        self.histo1D_breaking_sum = self.histo1D_breaking_sum + break_histogram[
            0]
        self.histo1D_making_sum = self.histo1D_making_sum + make_histogram[0]

        break_histogram2D = pl.histogram2d(G_breaking, D_breaking, bins)
        self.histo2D_breaking_sum = self.histo2D_breaking_sum + break_histogram2D[
            0]
Beispiel #16
0
def pdf2d(ax,ay,imp,xbins,ybins,smooth,color,style):

  from scipy import ndimage

  pylab.xlim([xbins[0],xbins[-1]])
  pylab.ylim([ybins[0],ybins[-1]])

  # npts = int((ax.size/4)**0.5)
  H,x,y = pylab.histogram2d(ax,ay,weights=imp,bins=[xbins,ybins])

  # Smooth the PDF:
  H = ndimage.gaussian_filter(H,smooth)

  sortH = numpy.sort(H.flatten())
  cumH = sortH.cumsum()
  # 1, 2, 3-sigma, for the old school:
  lvl00 = 2*sortH.max()
  lvl68 = sortH[cumH>cumH.max()*0.32].min()
  lvl95 = sortH[cumH>cumH.max()*0.05].min()
  lvl99 = sortH[cumH>cumH.max()*0.003].min()

#   print "2D histogram: min,max = ",H.min(),H.max()
#   print "Contour levels: ",[lvl00,lvl68,lvl95,lvl99]

  if style == 'shaded':

    # Plot shaded areas first:
    pylab.contourf(H.T,[lvl99,lvl95],colors=color,alpha=0.1,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    pylab.contourf(H.T,[lvl95,lvl68],colors=color,alpha=0.4,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    pylab.contourf(H.T,[lvl68,lvl00],colors=color,alpha=0.7,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
  # endif

  # Always plot outlines:
  pylab.contour(H.T,[lvl68,lvl95,lvl99],colors=color,\
                  extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))

  return
Beispiel #17
0
def pdf2d(ax,ay,imp,xbins,ybins,smooth,color="grey",style='shaded'):

  from scipy import ndimage

  pylab.xlim([xbins[0],xbins[-1]])
  pylab.ylim([ybins[0],ybins[-1]])

  # npts = int((ax.size/4)**0.5)
  H,x,y = pylab.histogram2d(ax,ay,weights=imp,bins=[xbins,ybins])
  
  H = ndimage.gaussian_filter(H,smooth)
  norm = np.sum(H.flatten())
  H = H * (norm > 0.0) / (norm + (norm == 0.0))
  
  sortH = np.sort(H.flatten())
  cumH = sortH.cumsum()
  # Set contours to show number of objects *outside* the contours
  fracs=[0.3, 0.1, 0.03, 0.01, 0.003, 0.001]
  labels=[]
  lvls=[]
  for f in fracs:
    lvls.append( sortH[cumH>cumH.max()*f].min()  )
    labels.append(str(1-f))

  print "2D histogram: min,max = ",H.min(),H.max()
  print "Contour levels: ",lvls

  if style == 'shaded':

    # Plot shaded areas first:
    for num in range(len(lvls)-1):
      pylab.contourf(H.T,[lvls[num],lvls[num+1]],colors=color,alpha=np.sqrt(fracs[num]),\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
  # endif

  # Always plot outlines:
  CS=pylab.contour(H.T,lvls,colors='black',\
                  extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
  plt.clabel(CS,fontsize=10,text=labels)
  return
Beispiel #18
0
def mutual_information(x,y, bins=11):
  """Given two arrays x and y of equal length, return their mutual information in bits

  >>> N = 10000
  >>> xi = pylab.randn(N)
  >>> xi[pylab.find(xi>0)] = 1
  >>> xi[pylab.find(xi<=0)] = 0
  >>> yi = xi
  >>> print round(mutual_information(xi, yi, 1000),2) #One bit of info
  1.0

  >>> N = 10000
  >>> xi = pylab.uniform(size=N)
  >>> yi = pylab.floor(xi*8)
  >>> print round(mutual_information(xi, yi, 1000),2) #Three bits of info
  3.0

  >>> N = 100000
  >>> xi = pylab.randn(N)
  >>> yi = pylab.randn(N)
  >>> print round(mutual_information(xi, yi),2) #Should be zero given enough data and not too sparse binning
  0.0
  """
  Hxy, xe, ye = pylab.histogram2d(x,y,bins=bins)
  Hx = Hxy.sum(axis=1)
  Hy = Hxy.sum(axis=0)

  Pxy = Hxy/float(x.size)
  Px = Hx/float(x.size)
  Py = Hy/float(x.size)

  pxy = Pxy.ravel()
  px = Px.repeat(Py.size)
  py = pylab.tile(Py, Px.size)

  idx = pylab.find((pxy > 0) & (px > 0) & (py > 0))
  mi = (pxy[idx]*pylab.log2(pxy[idx]/(px[idx]*py[idx]))).sum()

  return mi
Beispiel #19
0
def mutual_information(x, y, bins=11):
    """Given two arrays x and y of equal length, return their mutual information in bits

  >>> N = 10000
  >>> xi = pylab.randn(N)
  >>> xi[pylab.find(xi>0)] = 1
  >>> xi[pylab.find(xi<=0)] = 0
  >>> yi = xi
  >>> print round(mutual_information(xi, yi, 1000),2) #One bit of info
  1.0

  >>> N = 10000
  >>> xi = pylab.uniform(size=N)
  >>> yi = pylab.floor(xi*8)
  >>> print round(mutual_information(xi, yi, 1000),2) #Three bits of info
  3.0

  >>> N = 100000
  >>> xi = pylab.randn(N)
  >>> yi = pylab.randn(N)
  >>> print round(mutual_information(xi, yi),2) #Should be zero given enough data and not too sparse binning
  0.0
  """
    Hxy, xe, ye = pylab.histogram2d(x, y, bins=bins)
    Hx = Hxy.sum(axis=1)
    Hy = Hxy.sum(axis=0)

    Pxy = Hxy / float(x.size)
    Px = Hx / float(x.size)
    Py = Hy / float(x.size)

    pxy = Pxy.ravel()
    px = Px.repeat(Py.size)
    py = pylab.tile(Py, Px.size)

    idx = pylab.find((pxy > 0) & (px > 0) & (py > 0))
    mi = (pxy[idx] * pylab.log2(pxy[idx] / (px[idx] * py[idx]))).sum()

    return mi
def probcontour(xarr,
                yarr,
                style='lines',
                color='k',
                smooth=2,
                bins=100,
                weights=None,
                linewidths=1,
                linestyles='solid',
                nlevels=3):
    from scipy import ndimage
    import numpy
    H, xbins, ybins = pylab.histogram2d(xarr, yarr, bins=bins, weights=weights)

    H = ndimage.gaussian_filter(H, smooth)
    sortH = numpy.sort(H.flatten())
    cumH = sortH.cumsum()
    # 1, 2, 3-sigma, for the old school:
    lvl68 = sortH[cumH > cumH.max() * 0.32].min()
    lvl95 = sortH[cumH > cumH.max() * 0.05].min()
    lvl99 = sortH[cumH > cumH.max() * 0.003].min()
    if style == 'lines':
        #pylab.contour(H.T,[lvl99,lvl95,lvl68],colors=color,\
        #                  extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]), linewidths=linewidths, linestyles=linestyles)
        pylab.contour(H.T,[lvl95,lvl68],colors=color,\
                          extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]), linewidths=linewidths, linestyles=linestyles)

    else:
        # Shaded areas:
        if type(color) == str:
            pylab.contourf(H.T,[lvl95,lvl68],colors=color,alpha=0.5,\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl68,1e8],colors=color,alpha=0.9,\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
        else:
            pylab.contourf(H.T,[lvl95,lvl68],colors=rgb_to_hex(rgb_alpha(color,0.5)),\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
            pylab.contourf(H.T,[lvl68,1e8],colors=rgb_to_hex(rgb_alpha(color,0.9)),\
                               extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
Beispiel #21
0
def pdf2d(ax, ay, imp, xbins, ybins, smooth, color, style):

    from scipy import ndimage

    pylab.xlim([xbins[0], xbins[-1]])
    pylab.ylim([ybins[0], ybins[-1]])

    # npts = int((ax.size/4)**0.5)
    H, x, y = pylab.histogram2d(ax, ay, weights=imp, bins=[xbins, ybins])

    # Smooth the PDF:
    H = ndimage.gaussian_filter(H, smooth)

    sortH = numpy.sort(H.flatten())
    cumH = sortH.cumsum()
    # 1, 2, 3-sigma, for the old school:
    lvl00 = 2 * sortH.max()
    lvl68 = sortH[cumH > cumH.max() * 0.32].min()
    lvl95 = sortH[cumH > cumH.max() * 0.05].min()
    lvl99 = sortH[cumH > cumH.max() * 0.003].min()

    #   print "2D histogram: min,max = ",H.min(),H.max()
    #   print "Contour levels: ",[lvl00,lvl68,lvl95,lvl99]

    if style == 'shaded':

        # Plot shaded areas first:
        pylab.contourf(H.T,[lvl99,lvl95],colors=color,alpha=0.1,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
        pylab.contourf(H.T,[lvl95,lvl68],colors=color,alpha=0.4,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
        pylab.contourf(H.T,[lvl68,lvl00],colors=color,alpha=0.7,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    # endif

    # Always plot outlines:
    pylab.contour(H.T,[lvl68,lvl95,lvl99],colors=color,\
                    extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
Beispiel #22
0
def hist2d_with_outliers(x, y, xbins, ybins, nout):
	'''
	Creates a 2D histogram from the given data, and returns a list of
	the indices in the data of points that lie in low-occupancy cells
	(where the histogram counts is < "nout").

	The "xbins" and "ybins" arguments are passed to numpy.histogram2d.

	You probably want to show the histogram with:

	  (H, outliers, xe, ye) = hist2d_with_outliers(x, y, 10, 10, 10)
	  imshow(H, extent=(min(xe), max(xe), min(ye), max(ye)), aspect='auto')
	  plot(x[outliers], y[outliers], 'r.')

	Returns: (H, outliers, xe, ye)

	  H: 2D histogram image
	  outliers: array of integer indices of the outliers
	  xe: x edges chosen by histgram2d
	  ye: y edges chosen by histgram2d
	  
	'''
	# returns (density image, indices of outliers)
	(H,xe,ye) = histogram2d(x, y, (xbins,ybins))
	Out = array([]).astype(int)
	for i in range(len(xe)-1):
		for j in range(len(ye)-1):
			if H[i,j] > nout:
				continue
			if H[i,j] == 0:
				continue
			H[i,j] = 0
			Out = append(Out, flatnonzero((x >= xe[i]) *
										  (x <  xe[i+1]) *
										  (y >= ye[j]) *
										  (y <  ye[j+1])))
	return (H.T, Out, xe, ye)
Beispiel #23
0
def plot_cmd_with_model(cldata):
    cmd = cldata['match']['cmd']
    x = cmd['color']
    y = cmd['mag']
    dx = get_bins_from_center(np.unique(x))
    dy = get_bins_from_center(np.unique(y))
    plt.figure()
    plot_cmd(cldata, c='w', edgecolor='k', alpha=1.)
    n, bx, by = plt.histogram2d(cmd['color'],
                                cmd['mag'],
                                bins=(dx, dy),
                                weights=cmd['n_mod'])
    plt.imshow(n.T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.Blues,
               aspect='auto',
               interpolation='nearest')
    # plt.ylim(plt.ylim()[1], 20)
    plt.xlim(bx[0], bx[-1])
    plt.ylim(by[-1], 20)
    figrc.hide_axis('top right'.split())
    plt.colorbar()
    plt.tight_layout()
Beispiel #24
0
def plot_model(df,
               model=None,
               bins=[np.linspace(0, .3, 26),
                     np.linspace(0, 1, 26)],
               hyperplane_only=False,
               alpha=1,
               mincnt=10,
               cmap='RdBu_r',
               fieldy='stdc',
               fieldx='mc',
               vmin=-2.4,
               vmax=2.4,
               predicted_surface=False,
               contrast=None):
    C, M = np.meshgrid(*bins)
    if model is not None:
        if not predicted_surface:
            N = 50
            resp1 = np.ones((len(bins[0]) - 1, len(bins[1]) - 1))
            resp2 = np.ones((len(bins[0]) - 1, len(bins[1]) - 1))
            if contrast is None:
                contrast = get_contrast(df)
            ps = expit(model(contrast))
            for i in range(N):
                idx = bernoulli.rvs(ps).astype(bool)

                resp1 += plt.histogram2d(df[fieldy].values[idx],
                                         df[fieldx].values[idx],
                                         bins=bins)[0]
                resp2 += plt.histogram2d(df[fieldy].values[~idx],
                                         df[fieldx].values[~idx],
                                         bins=bins)[0]
            resp2[(resp2) <= (mincnt * N)] = np.nan
            resp1[(resp1) <= (mincnt * N)] = np.nan
            resp1, resp2 = resp1 / N, resp2 / N
            resp1 = resp1.astype(float) / np.nansum(resp1)
            resp2 = resp2.astype(float) / np.nansum(resp2)

            plane = np.log(resp1 / resp2)
            plane[plane == 1] = np.nan
        else:
            if contrast is None:
                contrast = get_contrast(df)
            ps = expit(model(contrast))
            plane = get_predicted_surface(contrast.std(1), contrast.mean(1),
                                          ps, bins, mincnt)

    else:
        idx = df.response > 0
        resp1 = plt.histogram2d(
            df[fieldy].values[idx], df[fieldx].values[idx], bins=bins)[0] + 1
        resp1[resp1 == 1] = np.nan
        resp2 = plt.histogram2d(
            df[fieldy].values[~idx], df[fieldx].values[~idx], bins=bins)[0] + 1
        resp2[resp2 == 1] = np.nan
        resp1[resp1 < mincnt] = np.nan
        resp2[resp2 < mincnt] = np.nan
        resp1 = resp1.astype(float) / np.nansum(resp1)
        resp2 = resp2.astype(float) / np.nansum(resp2)

        plane = np.log(resp1 / resp2)
        plane[plane == 1] = np.nan
    pcol = plt.pcolormesh(bins[1],
                          bins[0],
                          np.ma.masked_invalid(plane),
                          cmap=cmap,
                          vmin=vmin,
                          vmax=vmax,
                          rasterized=True,
                          linewidth=0)
    pcol.set_edgecolor('face')
    if predicted_surface:
        pcol = plt.contour(bins[1][1:], bins[0][1:],
                           np.ma.masked_invalid(plane), [0.35, 0.5, 0.65])

    mind, maxd = plt.xlim()
    plt.ylim(bins[0][0], bins[0][-1])
    plt.xlim(bins[1][0], bins[1][-1])
    plt.plot([0, 0], [bins[0][0], bins[0][-1]], 'k--', lw=2)
Beispiel #25
0
            plot_flag = flag
        elif flag in log_flags:
            log_flag = flag
        else:
            print "unknown flag %s" % flag

    if log_flag == 'log':
        log_function = pylab.log
    else:
        log_function = no_log

    if plot_flag == 'phases':
        hist = pylab.histogram(pylab.angle(values),bins=50)
        ax.plot((hist[1][:-1]+hist[1][1:])/2.0,log_function(hist[0]))
    elif plot_flag == 'histogram':
        hist = pylab.histogram2d(pylab.real(values),pylab.imag(values),bins=500)
        ax.imshow(log_function(hist[0]),extent=(hist[2][0],hist[2][-1],-hist[1][-1],-hist[1][0]),interpolation='nearest')
    else:
        ax.plot(pylab.real(values),pylab.imag(values),'.')
    return fig
    


if __name__ == "__main__":
    try:
        plot_phases(sys.argv[1],*sys.argv[2:])
        pylab.show()
    except:
        print """
Usage: plot_phases datafile [flags]
Beispiel #26
0
zspec_master = pylab.array(zspec_master)
zphot_master = pylab.array(zphot_master)
dz1pluszspec_master = (zphot_master - zspec_master) / (1 + zspec_master)

nmad = mypy.nmad(dz1pluszspec_master)
foutlier = len(pylab.find(
    abs(dz1pluszspec_master) >= 0.15)) * 1. / len(dz1pluszspec_master)

###  major panel: all fields

nbins = 120
clo, chi = 0, 1.05  ###  colorbar limits
cmap = pylab.cm.spectral
hist2d, xedges, yedges = pylab.histogram2d(zspec_master,
                                           zphot_master,
                                           bins=(nbins, nbins),
                                           range=([axis1[0], axis1[1]],
                                                  [axis1[2], axis1[3]]))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

nlo, nhi = hist2d.min(), hist2d.max()
hist2d[pylab.where(hist2d == 0)] = pylab.nan
hist2d[pylab.where(hist2d > 0)] = pylab.log10(hist2d[pylab.where(hist2d > 0)])

zphotzspec_map = sp_master.imshow(hist2d.T[::-1],
                                  extent=extent,
                                  interpolation='nearest',
                                  cmap=cmap)
zphotzspec_map.set_clim(0, chi * zphotzspec_map.get_clim()[1])

sp_master.set_aspect('auto')
Beispiel #27
0
def plot_match_cmd_results(cldata):

    cmd = cldata['match']['cmd']
    x = cmd['color']
    y = cmd['mag']
    dx = get_bins_from_center(np.unique(x))
    dy = get_bins_from_center(np.unique(y))
    plt.figure(figsize=(10, 8))

    ax = plt.subplot(221)
    n, bx, by = plt.histogram2d(cmd['color'],
                                cmd['mag'],
                                bins=(dx, dy),
                                weights=cmd['n_obs'])
    plt.imshow(n.T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.gray_r,
               aspect='auto',
               interpolation='nearest')
    figrc.hide_axis('top right'.split())
    plt.colorbar()
    plt.title('observed')

    plt.subplot(222, sharex=ax, sharey=ax)
    n, bx, by = plt.histogram2d(cmd['color'],
                                cmd['mag'],
                                bins=(dx, dy),
                                weights=cmd['n_mod'])
    plt.imshow(n.T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.gray_r,
               aspect='auto',
               interpolation='nearest')
    figrc.hide_axis('top right'.split())
    plt.colorbar()
    plt.title('modeled')

    plt.subplot(223, sharex=ax, sharey=ax)
    n, bx, by = plt.histogram2d(cmd['color'],
                                cmd['mag'],
                                bins=(dx, dy),
                                weights=cmd['n_diff'])
    plt.imshow(n.T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.gray_r,
               aspect='auto',
               interpolation='nearest')
    figrc.hide_axis('top right'.split())
    plt.colorbar()
    plt.title('residuals')

    plt.subplot(224, sharex=ax, sharey=ax)
    n, bx, by = plt.histogram2d(cmd['color'],
                                cmd['mag'],
                                bins=(dx, dy),
                                weights=cmd['sig'])
    plt.imshow(n.T,
               origin='lower',
               extent=(bx[0], bx[-1], by[0], by[-1]),
               cmap=plt.cm.gray_r,
               aspect='auto',
               interpolation='nearest')

    plt.ylim(plt.ylim()[1], 20)
    figrc.hide_axis('top right'.split())
    plt.colorbar()
    plt.title('significance')

    plt.tight_layout()
Beispiel #28
0
def pdf2d(ax, ay, imp, xbins, ybins, smooth, color, style, conditional=False):

    from scipy import ndimage

    pylab.xlim([xbins[0], xbins[-1]])
    pylab.ylim([ybins[0], ybins[-1]])

    # npts = int((ax.size/4)**0.5)
    H, x, y = pylab.histogram2d(ax, ay, weights=imp, bins=[xbins, ybins])

    totalmass = sum(H.flatten())

    # Smooth the histogram into a PDF:
    if conditional:

        ncolumns = len(H[0, :])
        totalstdev = numpy.sqrt(numpy.var(ay))
        for i in range(ncolumns):
            p = H[i, :]
            # Need to choose smoothing scale carefully here! Columns with fewer points need
            # bigger smoothing scales:
            norm = numpy.sum(p)
            if (norm > 0.0):
                yy = ybins[1:]
                mean, stdev, Neff, N95 = pappy.meansd(yy, wht=p)
                blur = (smooth + smooth * (stdev / totalstdev)**2)
                # print "i,norm,totalmass, smooth,blur = ",i,norm,totalmass,smooth,blur
                H[i, :] = ndimage.gaussian_filter1d(p.flatten(), blur)

    else:
        H = ndimage.gaussian_filter(H, smooth)

    # For a conditional PDF Pr(y|x), normalise PDF in columns (constant x):
    if conditional:
        for i in range(len(H[0, :])):
            p = H[i, :]
            norm = numpy.sum(p.flatten())
            # Can only estimate conditional where there are enough points! Rough
            # estimate - focus on 99.9% of the mass:
            norm = norm * (norm > 0.001 * totalmass)
            H[i, :] = p * (norm > 0.0) / (norm + (norm == 0.0))
    else:
        norm = numpy.sum(H.flatten())
        H = H * (norm > 0.0) / (norm + (norm == 0.0))

    sortH = numpy.sort(H.flatten())
    cumH = sortH.cumsum()
    # 1, 2, 3-sigma, for the old school:
    lvl00 = 2 * sortH.max()
    lvl68 = sortH[cumH > cumH.max() * 0.32].min()
    lvl95 = sortH[cumH > cumH.max() * 0.05].min()
    lvl997 = sortH[cumH > cumH.max() * 0.003].min()

    #   print "2D histogram: min,max = ",H.min(),H.max()
    #   print "Contour levels: ",[lvl00,lvl68,lvl95,lvl997]

    if style == 'shaded':

        # Plot shaded areas first:
        pylab.contourf(H.T,[lvl997,lvl95],colors=color,alpha=0.1,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
        pylab.contourf(H.T,[lvl95,lvl68],colors=color,alpha=0.4,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
        pylab.contourf(H.T,[lvl68,lvl00],colors=color,alpha=0.7,\
                       extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    # endif

    # Always plot outlines:
    pylab.contour(H.T,[lvl68,lvl95,lvl997],colors=color,\
                    extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    #   pylab.contour(H.T,[lvl68,lvl95],colors=color,\
    #                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))

    return
Beispiel #29
0
pylab.plot(range(-nburn, nplot), mu1[:nburn+nplot], label='stage 1')
pylab.plot(range(-nburn, nplot), mu2[:nburn+nplot], label='stage 2')
pylab.ylabel('mean')
pylab.xlim(-nburn, nplot)
pylab.axvspan(-nburn, 0, facecolor='r', alpha=0.2)
pylab.legend()
pylab.subplot(2, 1, 2)
pylab.plot(range(-nburn, nplot), M[:nburn+nplot])
pylab.ylabel('transition point')
pylab.xlim(-nburn, nplot)
pylab.axvspan(-nburn, 0, facecolor='r', alpha=0.2)
pylab.xlabel('step')

# plot probability distribution in mu1-mu2 plane
fig = pylab.figure()
(Z, xedges, yedges) = pylab.histogram2d(mu1[nburn:], mu2[nburn:], bins=20,
        normed=True)
X = 0.5*(xedges[:-1]+xedges[1:])  # centers of histogram bins
Y = 0.5*(yedges[:-1]+yedges[1:])  # centers of histogram bins
(Y, X) = pylab.meshgrid(Y, X)
axis = fig.gca(projection='3d', azim=-50, elev=20)
surf = axis.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=pylab.cm.jet)
axis.contour(X, Y, Z, zdir='z', offset=-pylab.amax(Z))
axis.contourf(X, Y, Z, 50, zdir='x', offset=min(mu1[nburn:]), colors='c')
axis.contourf(X, Y, Z, 50, zdir='y', offset=max(mu2[nburn:]), colors='c')
axis.set_xlabel('mu1')
axis.set_ylabel('mu2')
axis.set_zlabel('probability')
axis.set_zlim(-pylab.amax(Z), pylab.amax(Z))
axis.plot([mu1true], [mu2true], [-pylab.amax(Z)], 'r*')
fig.colorbar(surf)
pylab.show()
	sp1.axis(axlims)
	sp1.grid()
	sp1.set_xlabel('%s$_{AUTO}$' % filternames[fiducial_filter])
	sp1.set_ylabel('( %s - %s )$_{APER}$' % (filternames[fiducial_filter], filtername))

	inds = pylab.find((cat.use == 1) & 
		              (fluxes[i][0] > 0) &
		              (fluxes[i][1] > 0) &
		              (fluxes[fiducial_filter][0] > 0) &
		              (fluxes[fiducial_filter][1] > 0))


	color = -2.5 * pylab.log10(fluxes[fiducial_filter][0][inds] / fluxes[i][0][inds])
	magnitude = 25 - 2.5 * pylab.log10(fluxes[fiducial_filter][1][inds])

	hist2d, xedges, yedges = pylab.histogram2d(magnitude, color, bins=(500, 500))
	extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
	hist2d[pylab.where(hist2d > 0)] = pylab.log10(hist2d[pylab.where(hist2d > 0)]) + 0.2
	asdf = sp1.imshow(hist2d.T, extent=extent, interpolation='nearest', cmap=pylab.cm.gray_r)

	pylab.savefig('colorDist%02i_%s-%s_%s.png' % (i+1, filternames[fiducial_filter], filtername, version))
	sp1.clear()

pylab.close()






Beispiel #31
0
def pdf2d(ax,ay,imp,xbins,ybins,smooth,color,style,conditional=False):

  from scipy import ndimage

  pylab.xlim([xbins[0],xbins[-1]])
  pylab.ylim([ybins[0],ybins[-1]])

  # npts = int((ax.size/4)**0.5)
  H,x,y = pylab.histogram2d(ax,ay,weights=imp,bins=[xbins,ybins])
  
  totalmass = sum(H.flatten())

  # Smooth the histogram into a PDF:
  if conditional:
  
    ncolumns = len(H[0,:])
    totalstdev = numpy.sqrt(numpy.var(ay))
    for i in range(ncolumns):
      p = H[i,:]
      # Need to choose smoothing scale carefully here! Columns with fewer points need
      # bigger smoothing scales:
      norm = numpy.sum(p)
      if (norm > 0.0):
        yy = ybins[1:]
        mean,stdev,Neff,N95 = pappy.meansd(yy,wht=p)
        blur = (smooth + smooth*(stdev/totalstdev)**2)
        # print "i,norm,totalmass, smooth,blur = ",i,norm,totalmass,smooth,blur
        H[i,:] = ndimage.gaussian_filter1d(p.flatten(),blur)
        
  else:
    H = ndimage.gaussian_filter(H,smooth)

  # For a conditional PDF Pr(y|x), normalise PDF in columns (constant x):
  if conditional:
    for i in range(len(H[0,:])):
      p = H[i,:]
      norm = numpy.sum(p.flatten())
      # Can only estimate conditional where there are enough points! Rough
      # estimate - focus on 99.9% of the mass:
      norm = norm * (norm > 0.001*totalmass)
      H[i,:] = p * (norm > 0.0) / (norm + (norm == 0.0))
  else:
    norm = numpy.sum(H.flatten())
    H = H * (norm > 0.0) / (norm + (norm == 0.0))
  
  sortH = numpy.sort(H.flatten())
  cumH = sortH.cumsum()
  # 1, 2, 3-sigma, for the old school:
  lvl00 = 2*sortH.max()
  lvl68 = sortH[cumH>cumH.max()*0.32].min()
  lvl95 = sortH[cumH>cumH.max()*0.05].min()
  lvl997 = sortH[cumH>cumH.max()*0.003].min()

#   print "2D histogram: min,max = ",H.min(),H.max()
#   print "Contour levels: ",[lvl00,lvl68,lvl95,lvl997]

  if style == 'shaded':

    # Plot shaded areas first:
    pylab.contourf(H.T,[lvl997,lvl95],colors=color,alpha=0.1,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    pylab.contourf(H.T,[lvl95,lvl68],colors=color,alpha=0.4,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
    pylab.contourf(H.T,[lvl68,lvl00],colors=color,alpha=0.7,\
                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
  # endif

  # Always plot outlines:
  pylab.contour(H.T,[lvl68,lvl95,lvl997],colors=color,\
                  extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))
#   pylab.contour(H.T,[lvl68,lvl95],colors=color,\
#                   extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]))

  return
	#sp.plot(f.fout.lmass[f.inds_spatial][inds], f.overdensities[f.inds_spatial][inds], 
	#	    ls='', marker='o', ms=1, zorder=1)


zspec_master = pylab.array(zspec_master)
z_master = pylab.array(z_master)
v_master = pylab.array(v_master)
m_master = pylab.array(m_master)




cmap = pylab.cm.gray_r
nbins = 5
hist2d, xedges, yedges = pylab.histogram2d(m_master, v_master,
	                                       bins=(len(mlims)*nbins+2*nbins, len(vlims)*nbins+2*nbins), 
	                                       range=([min(mlims)-dm, max(mlims)+dm], [min(vlims)-dv, max(vlims)+dv]))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

nlo, nhi = hist2d.min(), hist2d.max()
hist2d[pylab.where(hist2d == 0)] = pylab.nan
hist2d[pylab.where(hist2d > 0)] = pylab.log10(hist2d[pylab.where(hist2d > 0)])
nhi = hist2d[pylab.where(-pylab.isnan(hist2d))].max()

#colormap = sp.imshow(hist2d.T[::-1,:], extent=extent, vmin=-0.3, vmax=nhi+0.3, interpolation='nearest', cmap=cmap)
colormap = sp.imshow(hist2d.T, extent=extent, vmin=-0.3, vmax=nhi+0.3, interpolation='nearest', cmap=cmap)


"""
font = FontProperties()
font.set_family('sans-serif')
Beispiel #33
0
sp1.axhline(0.25 / 2, color='k', lw=2, ls='--', zorder=2)
sp1.axhline(-0.25 / 2, color='yellow', lw=4, ls='-', zorder=2)
sp1.axhline(-0.25 / 2, color='k', lw=2, ls='--', zorder=2)

sp2.axhline(0.25 / 2, color='yellow', lw=4, ls='-', zorder=2)
sp2.axhline(0.25 / 2, color='k', lw=2, ls='--', zorder=2)
sp2.axhline(-0.25 / 2, color='yellow', lw=4, ls='-', zorder=2)
sp2.axhline(-0.25 / 2, color='k', lw=2, ls='--', zorder=2)

#sp1.plot(lmass_afta_1d, lmass_b4_1d - lmass_afta_1d, ls='', marker='o', mec='gray', ms=1, zorder=1)
#sp2.plot(z_1d, lmass_b4_1d - lmass_afta_1d, ls='', marker='o', mec='gray', ms=1, zorder=1)

nbins1, nbins2 = 70, 70
hist2d, xedges, yedges = pylab.histogram2d(lmass_afta_1d,
                                           lmass_b4_1d - lmass_afta_1d,
                                           bins=(nbins1, nbins2),
                                           range=([ax1[0],
                                                   ax1[1]], [ax1[2], ax1[3]]))
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
asdf = sp1.imshow(pylab.log10(hist2d.T + 1),
                  extent=extent,
                  interpolation='nearest',
                  cmap=pylab.cm.Greens)
asdf.set_clim(0, pylab.log10(hist2d.max()) * 1.)
sp1.set_aspect('auto')

nbins1, nbins2 = 70, 70
hist2d, xedges, yedges = pylab.histogram2d(z_1d,
                                           lmass_b4_1d - lmass_afta_1d,
                                           bins=(nbins1, nbins2),
                                           range=([ax2[0],
vj_zspec, uv_zspec = pylab.array(vj_zspec), pylab.array(uv_zspec)
vj_zphot_err, uv_zphot_err = pylab.array(vj_zphot_err), pylab.array(uv_zphot_err)
vj_zspec_err, uv_zspec_err = pylab.array(vj_zspec_err), pylab.array(uv_zspec_err)





#################################
###  plotting photometric sample
#################################

nbins_zphot = 75
xlims, ylims = [-0.1, 2.1], [0.3, 2.5]
hist2d_zphot, xedges_zphot, yedges_zphot = pylab.histogram2d(vj_zphot, uv_zphot,
	                                       bins=(nbins_zphot, nbins_zphot),
	                                       range=(xlims, ylims))
extent_zphot = [xedges_zphot[0], xedges_zphot[-1], yedges_zphot[0], yedges_zphot[-1]]



nlo, nhi = hist2d_zphot.min(), hist2d_zphot.max()
dn = abs(nlo - nhi)

cmap_colors = [pylab.cm.gray_r(i) for i in pylab.linspace(0, 1, 15)]
cmap = pylab.cm.gray_r
cmap = matplotlib.colors.ListedColormap(cmap_colors, name='custom grays', N=None)

colormap = sp1.imshow(hist2d_zphot.T, extent=extent_zphot, interpolation='nearest', cmap=cmap)
colormap.set_clim(nlo+dn*0.03, nhi-dn*0.03)
colormap.set_clim(nlo+dn*0.02, nhi-dn*0.03)
def plot_pairwise_envelope(imageRoot, comboDir, magCut=100, radCut=10000, 
                           drBinSize=5, dmBinSize=0.5, calcArea=False):
    """
    For all pairs of stars in a starlist, plot up the separation and 
    delta-magnitude. Overplot the Starfinder PSF on the plots.

    imageRoot = e.g. mag06maylgs2_arch_f1_kp
    comboDir = e.g. /u/ghezgroup/data/gc/06maylgs2/combo/
    magCut = Only plot pairs where the primary (brighter star) is brighter
             than a certain magnitude (default=100).
    radCut = Only plot pairs where the radius of the primary (brighter star)
             is less than a certain radius in pixels (default=10,000).
    """
    image_file = comboDir + imageRoot + '.fits'
    psf_file = comboDir + imageRoot + '_psf.fits'
    #psf_file = imageRoot + '_psf2d_2asec.fits'
    lis_file = comboDir + 'starfinder/' + imageRoot + '_rms.lis'

    outSuffix = '%s_m%g_r%d' % (imageRoot.replace('mag', ''), magCut, radCut)

    lis = starTables.StarfinderList(lis_file, hasErrors=True)
    img = pyfits.getdata(image_file)
    psf2D = pyfits.getdata(psf_file)

    # Sort by brightness
    sidx = lis.mag.argsort()
    mag = lis.mag[sidx]
    x = lis.x[sidx]
    y = lis.y[sidx]
    xe = lis.xerr[sidx]
    ye = lis.yerr[sidx]
    asterr = (xe + ye) / 2.0

#     # Make a cut based on the astrometric at a given magnitude.
#     starsPerBin = 30
#     numBins = int( math.ceil(len(x) / starsPerBin) )
#     keep = np.array([], dtype=int)
#     for ii in range(numBins):
#         lo = ii * starsPerBin
#         hi = (ii+1) * starsPerBin

#         asterr_median = np.median( asterr[lo:hi] )
#         asterr_stddev = asterr[lo:hi].std()

#         # Keep only those below median + 3*stddev
#         cutoff = asterr_median + (2.0 * asterr_stddev)
#         tmp = np.where(asterr[lo:hi] < cutoff)[0]
#         tmp += lo
#         keep = np.append(keep, tmp)
#         print 'Stars from %.2f - %.2f have median(asterr) = %.4f   std(asterr) = %.4f' % (mag[lo:hi].min(), mag[lo:hi].max(), asterr_median, asterr_stddev)

#     print 'Throwing out %d of %d stars with outlying astrometric errors' % \
#         (len(x) - len(keep), len(x))

#     mag = mag[keep]
#     x = x[keep]
#     y = y[keep]

    starCount = len(mag)

    magMatrix = np.tile(mag, (starCount, 1))
    xMatrix = np.tile(x, (starCount, 1))
    yMatrix = np.tile(y, (starCount, 1))
    
    # Do the matrix calculation, then take the upper triangule
    dm = util.triu2flat(magMatrix.transpose() - magMatrix)
    dx = util.triu2flat(xMatrix.transpose() - xMatrix)
    dy = util.triu2flat(yMatrix.transpose() - yMatrix)
    dr = np.hypot(dx, dy)

    # Also pull out the x and y positions (and magnitudes) of the
    # primary star in each pair.
    x = util.triu2flat(xMatrix.transpose())
    y = util.triu2flat(yMatrix.transpose())
    m = util.triu2flat(magMatrix.transpose())

    xmid = (x.max() - x.min()) / 2.0
    ymid = (y.max() - y.min()) / 2.0
    r = np.hypot(x - xmid, y - ymid)

    # Calculate the edges of the detectors
    edge_xlo = x.min()
    edge_xhi = x.max()
    edge_ylo = y.min()
    edge_yhi = y.max()

    # Azimuthally average the PSF
    psf_r, psf_f, psf_std, psf_n = radialProfile.azimuthalAverage(psf2D)
    psf = -2.5 * np.log10(psf_f[0] / psf_f)  # convert to magnitudes

    # Trim down to a manageable size
    idx = np.where((dr > 0) & (dr < 500) & (m < magCut) & (r < radCut))[0]
    dm = dm[idx]
    dx = dx[idx]
    dy = dy[idx]
    dr = dr[idx]
    x = x[idx]
    y = y[idx]
    r = r[idx]
    m = m[idx]

    py.close(2)
    py.figure(2, figsize=(8, 6))

    # ##########
    # Plot 2D scatter of positions
    # ##########
    py.clf()
    py.scatter(dx, dy, s=2, c=dm, marker='o', edgecolor='none')
    py.xlabel('X (pixels)')
    py.ylabel('Y (pixels)')
    py.title('MagCut < %g, RadCut < %d' % (magCut, radCut))
    cbar = py.colorbar()
    cbar.set_label('Delta Magnitude')
    print 'Saving plots/pairs_xy_%s.png' % outSuffix
    py.savefig('plots/pairs_xy_%s.png' % outSuffix)

    # ##########
    # Plot 2D histogram of positions
    # ##########
    (xy, x_edges, y_edges) = py.histogram2d(dx, dy, bins=[25, 25])

    dx_bin_size = (x_edges[1] - x_edges[0]) * 0.00995
    dy_bin_size = (y_edges[1] - y_edges[0]) * 0.00995
    xy /= dx_bin_size * dy_bin_size

    py.clf()
    py.imshow(xy.transpose(), 
              extent=[x_edges[0], x_edges[-1], y_edges[0], y_edges[-1]],
              cmap=py.cm.gray_r)
    py.axis('equal')
    py.xlabel('X (pixel)')
    py.ylabel('Y (pixel)')
    py.title('MagCut < %g, RadCut < %d' % (magCut, radCut))
    cbar = py.colorbar()
    cbar.set_label('stars / arcsec^2')
    print 'Saving plots/pairs_xy_hist_%s.png' % outSuffix
    py.savefig('plots/pairs_xy_hist_%s.png' % outSuffix)

    # ##########
    # Grid up into magnitude and distance 2D bins
    # ##########
    dr_bins = np.arange(0, 501, drBinSize)
    dm_bins = np.arange(-11, 0.1, dmBinSize)
    (h, r_edges, m_edges) = py.histogram2d(dr, dm, bins=[dr_bins,dm_bins])


    # Calculate the area covered for each star, taking into account
    # edge limits. Use a dummy grid to calculate this for each star.
    grid_pix_size = 1.0
    grid_side = np.arange(-500, 501, grid_pix_size)
    grid_y, grid_x = np.meshgrid(grid_side, grid_side)
    grid_r = np.hypot(grid_x, grid_y)

    areaFile = 'area_%s_dr%d.dat' % (outSuffix, drBinSize)
    if os.path.isfile(areaFile) and not calcArea:
        print 'Loading area file: %s' % areaFile
        _area = open(areaFile, 'r')
        area_at_r = pickle.load(_area)
        _area.close()

        if len(area_at_r) != len(dr_bins)-1:
            print 'Problem with area_at_r in %s' % areaFile
            print '   len(area_at_r) = %d' % len(area_at_r)
            print '   len(dr_bins)-1 = %d' % (len(dr_bins) - 1)
            
    else:
        print 'Creating area file: %s' % areaFile

        area_at_r = np.zeros(len(dr_bins)-1, dtype=float)
        for rr in range(len(dr_bins)-1):
            idx = np.where((grid_r > r_edges[rr]) & (grid_r <= r_edges[rr+1]))
        
            rgrid_x = grid_x[idx[0], idx[1]]
            rgrid_y = grid_y[idx[0], idx[1]]

            for ss in range(len(dr)):
                sgrid_x = rgrid_x + x[ss]
                sgrid_y = rgrid_y + y[ss]
            
                sdx = np.where((sgrid_x > edge_xlo) & (sgrid_x < edge_xhi) &
                               (sgrid_y > edge_ylo) & (sgrid_y < edge_yhi))[0]

                area_at_r[rr] += len(sdx)
            area_at_r[rr] *= 0.00995**2 * grid_pix_size**2 / len(dr)
        
            area_formula = (r_edges[rr+1]**2 - r_edges[rr]**2)
            area_formula *= math.pi * 0.00995**2
            print '%3d < r <= %3d   %.5f vs. %.5f' % \
                (r_edges[rr], r_edges[rr+1], area_at_r[rr], area_formula)

        _area = open(areaFile, 'w')
        pickle.dump(area_at_r, _area)
        _area.close()


    for ii in range(h.shape[0]):
        h[ii] /= area_at_r[ii] * dmBinSize

    # Calculate a 2nd histogram for a sliding window in magnitude space. This
    # means we have to code our own 2D histogram.
    dr_bins2 = dr_bins
    dm_bins2 = np.arange(-11, 0.1, dmBinSize/5.0)
    h2 = np.zeros((len(dr_bins2)-1, len(dm_bins2)), dtype=float)
    
    for rr in range(len(dr_bins2)-1):
        for mm in range(len(dm_bins2)):
            dm_lo = dm_bins2[mm] - (dmBinSize / 2.0)
            dm_hi = dm_bins2[mm] + (dmBinSize / 2.0)

            idx = np.where((dr >= dr_bins2[rr]) & (dr < dr_bins2[rr+1]) &
                           (dm >= dm_lo) & (dm < dm_hi))[0]

            h2[rr, mm] = len(idx) / (area_at_r[rr] * dmBinSize)

    #m_edges = dm_bins2
    #h = h2
    #r_edges = dr_bins2

    # Now calculate the envelope by taking the average from 
    # outside 2"
    critDist = np.zeros(h.shape[1], dtype=float)
    idx = np.where(r_edges[:-1] > 2.0/0.00995)[0]
    h_avg_hi_r = np.median(h[idx[0]:, :], axis=0)
    h_std_hi_r = h[idx[0]:, :].std(axis=0)
    r_center = r_edges[0:-1] + (drBinSize / 2.0)
    m_center = m_edges[0:-1] + (dmBinSize / 2.0)
    #m_center = m_edges

    for mm in range(len(m_edges)-1):
        # Walk from the inside out until we get to a pixel 
        # that has a density of at least avgDensity / 3.0
        idx = np.where(h[:, mm] > (h_avg_hi_r[mm] / 3.0))[0]
        
        if len(idx) > 0:
            critDist[mm] = r_edges[idx[0]]

        print mm, m_edges[mm], critDist[mm], h_avg_hi_r[mm]

    # ##########
    # Make a 2D version of the envelope (smoothed)
    #   - from 0 > dm > -8, use the PSF
    #   - from dm < -8, use a smoothed version of the envelope.
    # ##########
    # Trim the envelope down to dm < -8
    idx = np.where((m_center < -8) & (critDist > 0))[0]
    int_dist = critDist[idx]
    int_mag = m_center[idx]
    foo = int_dist.argsort()
    int_dist = int_dist[foo]
    int_mag = int_mag[foo]
    tck = interpolate.splrep(int_dist, int_mag, s=0, k=1)
    env_r = np.arange(400)
    env_m = interpolate.splev(env_r, tck)

    # Graft just the tail onto the existing PSF. (past dm < -8)
    npsf_side = np.arange(-200, 201, 1)
    npsf_y, npsf_x = np.meshgrid(npsf_side, npsf_side)
    npsf_r = np.hypot(npsf_x, npsf_y)
    npsf2D = np.zeros(npsf_r.shape, dtype=float)
    npsf1D = np.zeros(npsf_r.shape, dtype=float)
    
    # Set to the 1D azimuthally averaged PSF.
    tck_psf = interpolate.splrep(psf_r, psf_f)
    idx = np.where(npsf_r < 100)
    npsf1D[idx[0], idx[1]] = interpolate.splev(npsf_r[idx[0], idx[1]], tck_psf)

    npsf2D[100:300, 100:300] = psf2D

    # Add in the halo.
    idx = np.where(env_m < -8.25)[0]
    startingDistance = env_r[idx].min()

    idx = np.where(npsf_r >= startingDistance)

    halo = interpolate.splev(npsf_r[idx[0], idx[1]], tck)
    halo = psf_f[0] / 10**(-0.4 * halo)

    npsf1D[idx[0], idx[1]] = np.hypot(npsf1D[idx[0], idx[1]], halo)
    npsf2D[idx[0], idx[1]] = np.hypot(npsf2D[idx[0], idx[1]], halo)


    # Possible choices of the envelope include:
    # azimuthally averaged version
    env_file = imageRoot + '_env1D.fits'
    if os.access(env_file, os.F_OK): os.remove(env_file)
    pyfits.writeto(env_file, npsf1D)

    # full 2D structure inside ~1"
    env_file = imageRoot + '_env2D.fits'
    if os.access(env_file, os.F_OK): os.remove(env_file)
    pyfits.writeto(env_file, npsf2D)

    # the original PSF
    env_file = imageRoot + '_psf2D.fits'
    if os.access(env_file, os.F_OK): os.remove(env_file)
    pyfits.writeto(env_file, psf2D)

    # For plotting purposes get the 1D evelope back out again.
    env_r, env_f, env_std, env_n = radialProfile.azimuthalAverage(npsf2D)
    env_m = -2.5 * np.log10(env_f[0] / env_f)  # convert to magnitudes

    # ##########
    # Plot points of dr vs. dm (unbinned)
    # ##########
    py.clf()
    py.plot(dr, dm, 'k.', ms=2)
    py.plot(psf_r, psf, 'b-')
    py.axis([0, 500, -11, 0])
    py.xlabel('X (pixel)')
    py.ylabel('Y (pixel)')
    py.title('MagCut < %g, RadCut < %d' % (magCut, radCut))
    print 'Saving plots/pairs_rm_%s.png' % outSuffix
    py.savefig('plots/pairs_rm_%s.png' % outSuffix)


    # ##########
    # Plot dr vs. dm in a 2D histogram
    # ##########
    hmask = np.ma.masked_where(h.transpose() == 0, h.transpose())
    r_edges = r_edges * 0.00995

    py.clf()
    py.imshow(hmask,
              extent=[r_edges[0], r_edges[-1], m_edges[0], m_edges[-1]],
              cmap=py.cm.spectral_r)
    py.plot(psf_r * 0.00995, psf, 'b-')
    py.plot(critDist * 0.00995, m_center, 'r.-')
    py.plot(env_r * 0.00995, env_m, 'g.-')
    py.axis('tight')
    py.axis([0, 5.0, -11, 0])
    py.xlabel('Radius (arcsec)')
    py.ylabel('Delta Magnitude')
    py.title('MagCut < %g, RadCut < %d' % (magCut, radCut))
    cbar = py.colorbar()
    cbar.set_label('stars / (mag * arcsec^2)')
    print 'Saving plots/pairs_rm_hist_%s.png' % outSuffix
    py.savefig('plots/pairs_rm_hist_%s.png' % outSuffix)