Example #1
0
def subplots(nrows=1, ncols=1, figsize=(12, 8), dpi=120, num=0, subplot_kw={}):
    """

    Equivalent function of pyplot.subplots(). The difference is that this one
    is not interactive and is used with backend plotting only.

    Parameters
    ----------
    nrows=1, ncols=1, figsize=(12,8), dpi=120

    num : dummy variable for compatibility

    Returns
    -------
    fig, axes


    """

    fig = mpl.figure.Figure(figsize=figsize, dpi=dpi)
    canvas = FigCanvas(fig)
    fig.set_canvas(canvas)
    axes = np.ndarray((nrows, ncols), dtype=np.object)
    plt_nr = 1
    for row in range(nrows):
        for col in range(ncols):
            axes[row, col] = fig.add_subplot(nrows, ncols, plt_nr,
                                             **subplot_kw)
            plt_nr += 1
    return fig, axes
Example #2
0
    def testImproveCluster(self):
        """Make sure scattered r,theta points convert in improvement algorithm"""

        np.random.seed(42)
        
        nx, ny = 512, 512

        r, theta = 300, 0.4
        trail = measArt.SatelliteTrail(r, theta)
        x, y = trail.trace(nx, ny)
        n = len(x)
        t = theta + 0.05*np.random.normal(size=n)

        niter = 2
        rNew, tNew, _r, _x, _y = measArt.improveCluster(t, x, y)
        for i in range(niter-1):
            rNew, tNew, _r, _x, _y = measArt.improveCluster(tNew, x, y)

        rEst = rNew.mean()
        tEst = tNew.mean()

        # if you need to look at it
        if False:
            r0, theta0 = measArt.hesseForm(t-np.pi/2.0, x, y)
            fig = figure.Figure()
            can = FigCanvas(fig)
            ax = fig.add_subplot(111)
            ax.plot(theta0, r0, 'k.')
            ax.plot(tNew, rNew, '.r')
            ax.plot([tEst], [rEst], 'go')
            fig.savefig("improveCluster.png")

        self.assertLess(np.abs(rEst - r), 1.0)
        self.assertLess(np.abs(tEst - theta), 0.001)
def makePortionFigure(deblend, origMimg, origMimgB, pedestal=0.0):

    portions = []
    centers = []
    boxes = []
    for i, peak in enumerate(deblend.peaks):
        # make an image matching the size of the original
        portionedImg = afwImage.ImageF(origMimg.getBBox())

        # get the heavy footprint for the flux aportioned to this peak
        heavyFoot = peak.getFluxPortion()
        footBox = heavyFoot.getBBox()
        pk = peak.peak
        centers.append((pk.getIx(), pk.getIy()))
        boxes.append(
            ((footBox.getMinX(), footBox.getMinY()), footBox.getWidth(),
             footBox.getHeight()))
        print(i, peak, pk.getIx(), pk.getIy(), footBox, "skip:", peak.skip)

        # make a sub-image for this peak, and put the aportioned flux into it
        portionedSubImg = afwImage.ImageF(portionedImg, footBox)
        portionedSubImg += pedestal
        heavyFoot.insert(portionedSubImg)

        portions.append(portionedImg)

    fig = figure.Figure(figsize=(8, 10))
    FigCanvas(fig)

    g = int(numpy.ceil(numpy.sqrt(len(deblend.peaks))))
    gx, gy = g, g + 1

    def makeAx(ax, im, title):
        im = im.getArray()
        a = ax.imshow(im, cmap='gray')
        cb = fig.colorbar(a)
        ax.set_title(title, size='small')
        ax.set_xlim((0, im.shape[0]))
        ax.set_ylim((0, im.shape[1]))
        for t in ax.get_xticklabels() + ax.get_yticklabels(
        ) + cb.ax.get_yticklabels():
            t.set_size('x-small')
        return ax

    # show the originals
    makeAx(fig.add_subplot(gy, gx, 1), origMimg.getImage(), "Orig")
    makeAx(fig.add_subplot(gy, gx, 2), origMimgB.getImage(), "Missing Src")

    # show each aportioned image
    i = gy
    for i_p in range(len(portions)):
        im = portions[i_p]
        ax = makeAx(fig.add_subplot(gy, gx, i), im, "")
        xy, w, h = boxes[i_p]
        ax.add_patch(Rectangle(xy, w, h, fill=False, edgecolor='#ff0000'))
        for x, y in centers:
            ax.plot(x, y, '+', color='#00ff00')
        i += 1

    return fig
Example #4
0
def coordPlot(exposure, finder, pngfile):

    mm = finder._mm
    cmm = finder._mmCals
    img = exposure.getMaskedImage().getImage().getArray()
    bins = 1.0*img.shape[1]/mm.img.shape[1]
    xx, yy = np.meshgrid(np.arange(mm.img.shape[1], dtype=int), np.arange(mm.img.shape[0], dtype=int))
    x = (bins*xx[finder._isCandidate]).astype(int)
    y = (bins*yy[finder._isCandidate]).astype(int)

    cimg = np.zeros(img.shape)
    cimg[y,x] = 1.0


    def small(ax):
        for t in ax.get_xticklabels() + ax.get_yticklabels():
            t.set_size("small")
            
    fig = figure.Figure(figsize=(8.0,4.0))
    fig.subplots_adjust(bottom=0.15)
    can = FigCanvas(fig)
    
    ax = fig.add_subplot(1, 3, 1)
    ax.imshow(np.arcsinh(img), origin='lower', cmap='gray')
    ax.set_xlim([0, img.shape[1]])
    ax.set_ylim([0, img.shape[0]])
    small(ax)
    
    ax = fig.add_subplot(1, 3, 3)
    stride = 1
    #ax.plot(mm.theta[::stride], mm.ellip[::stride], '.k', ms=0.2, alpha=0.2)
    ax.scatter(mm.theta[::stride], mm.ellip[::stride],
               c=np.clip(mm.center[::stride], 0.0, 4.0*finder.centerLimit),
               s=0.2, alpha=0.2, edgecolor='none')
    if len(cmm) == 2:
        i = 0
    else:
        i = 2
    ax.hlines([cmm[i].ellip], -np.pi/2.0, np.pi/2.0, color='k', linestyle='-')
    ax.set_xlim([-np.pi/2.0, np.pi/2.0])
    ax.set_ylim([0.0, 1.0])
    ax.set_xlabel("$\\theta$")
    ax.set_ylabel("$e=1-B/A$")
    ax.yaxis.set_label_position("right")
    ax.yaxis.tick_right()
    small(ax)
    
    ax = fig.add_subplot(1, 3, 2)
    ax.imshow(cimg, origin='lower', cmap='gray_r')
    ax.plot(x, y, 'k.', ms=1.0)
    ax.set_xlim([0, img.shape[1]])
    ax.set_ylim([0, img.shape[0]])
    small(ax)
    
    fig.savefig(pngfile)
    fig.savefig(re.sub("png", "eps", pngfile))
def plot(data, color='r'):
    x = data['x']
    y = data['y']

    fig = figure.Figure(figsize=(4.0, 3.0))
    canvas = FigCanvas(fig)
    ax = fig.add_subplot(111)
    ax.plot(x, y, color + "-")

    return fig
Example #6
0
def main(root, infile):

    butler = dafPersist.Butler(root)

    print "loading"
    detections = collections.defaultdict(list)
    
    if infile.endswith(".pickle"):
        with open(infile, 'r') as fp:
            for bundle in pickle.load(fp):
                (v,c), trailList, runtime = bundle
                for t in trailList:
                    detections[(int(v),int(c))].append(t)
    else:
        lines = []
        with open(infile, 'r') as fp:
            lines = fp.readlines()

        print "parsing"
        for line in lines:
            m = re.match("\((\d+), (\d+)\) SatelliteTrail\(r=(\d+.\d),theta=(\d.\d+),width=(\d+.\d+),.*", line)
            if m:
                v, c, r, t, w = m.groups()
                detections[(int(v),int(c))].append( satTrail.SatelliteTrail(r=float(r),theta=float(t),width=float(w)) )

    print "plotting"
    for i, ((v,c), trails) in enumerate(sorted(detections.items())):
        print v, c
        
        dataId = {'visit': int(v), 'ccd': int(c)}
        cexp = butler.get('calexp', dataId)
        img = cexp.getMaskedImage().getImage().getArray()

        ny, nx = img.shape
        
        fig = figure.Figure()
        can = FigCanvas(fig)
        ax = fig.add_subplot(111)
        ax.imshow(np.arcsinh(img), origin='lower', cmap='gray')

        for trail in trails:
            x1, y1 = trail.trace(nx, ny, offset=40)
            x2, y2 = trail.trace(nx, ny, offset=-40)
            ax.plot(x1, y1, 'r-')
            ax.plot(x2, y2, 'r-')

        ax.set_xlim([0, nx])
        ax.set_ylim([0, ny])
        t0 = trails[0]
        ax.set_title("(%s) %s (%.1f, %.3f, %.1f)" % (str(datetime.datetime.now()),
                                                     str((v,c)), t0.r,t0.theta,t0.width))
        fig.savefig("det-%05d-%03d.png" % (v, c))
Example #7
0
 def __init__(self, size=(4.0, 4.0), dpi=100): # (512, 512), DPI=100):
     """
     @param size  Figure size in inches
     @param dpi   Dots per inch to use.
     """
     
     self.fig         = figure.Figure(figsize=size)
     self.fig.set_dpi(dpi)
     self.canvas      = FigCanvas(self.fig)
     self.map         = {}
     #self.fig.set_size_inches(size[0] / DPI, size[1] / DPI)
     self.mapAreas    = []
     self.mapTransformed = True
def plot(data, dataId):

    n = 100
    x = 2.0 * 3.142 * numpy.arange(n) / n
    y = numpy.sin(x)

    fig = figure.Figure()
    canvas = FigCanvas(fig)

    ax = fig.add_subplot(111)
    ax.plot(x, y)

    fig.savefig("testDyFig.png")
Example #9
0
def dump(img, name):
    '''Dump several slices for the given numpy array.'''
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigCanvas

    figure = Figure()
    canvas = FigCanvas(figure)

    nplot = 9
    for i in range(1, nplot+1):
        ax = figure.add_subplot(3, 3, i)  # pylint: disable=C0103
        ax.imshow(sitk.GetArrayFromImage(img)[i*img.GetDepth()/(nplot+1)])

    canvas.print_figure(name)
Example #10
0
def debugPlot(finder, pngfile):

    mm     = finder._mm
    cmm    = finder._mmCals
    trails = finder._trails

    img = mm.img

    ###################################
    # a debug figure
    ###################################
    debug = True
    if debug:


        fig = figure.Figure()
        can = FigCanvas(fig)
        fig.suptitle(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

        # pixel plot
        ax = fig.add_subplot(py, px, 1)
        pixelPlot(finder, ax, mm, cmm, trails)
        
        # hough  r vs theta
        ax = fig.add_subplot(py, px, 2)
        houghPlot(finder, ax, mm, cmm, trails)
        
        # e vs theta
        ax = fig.add_subplot(py, px, 3)
        evthetaPlot(finder, ax, mm, cmm, trails)

        # centroid vs flux
        ax = fig.add_subplot(py, px, 4)
        cvfluxPlot(finder, ax, mm, cmm, trails)

        # b versus skew
        ax = fig.add_subplot(py, px, 5)
        bvskewPlot(finder, ax, mm, cmm, trails)

        # trail plots
        for i,trail in enumerate(trails[0:px*py-5]):
            ax = fig.add_subplot(py,px,6+1*i)
            trailPlot(finder, ax, mm, cmm, trail, i)
        
        fig.savefig(pngfile)
def plot(data):

    mrefGmag = data['mrefGmag']
    mimgGmag = data['mimgGmag']
    mimgGmerr = data['mimgGmerr']
    mrefSmag = data['mrefSmag']
    mimgSmag = data['mimgSmag']
    mimgSmerr = data['mimgSmerr']
    urefmag = data['urefmag']
    uimgmag = data['uimgmag']
    zeropt = data['zeropt']
    title = data['title']
    figsize = data['figsize']
    fluxType = data['fluxType']

    # Just to get the histogram results

    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)

    legLines = []
    legLabels = []

    axis = fig.add_axes([0.225, 0.225, 0.675, 0.550])

    ################    --------------------------------  ##################
    # can't fig.savefig() with this Ellipse stuff ??? ... i love matplotlib
    if False:
        for i in range(len(mrefGmag)):
            a = Ellipse(xy=num.array([mimgGmag[i], mrefGmag[i]]),
                        width=mimgGmerr[i] / 2.,
                        height=mimgGmerr[i] / 2.,
                        alpha=0.5,
                        fill=True,
                        ec='g',
                        fc='g',
                        zorder=10)
            axis.add_artist(a)
    else:
        pass
        #axis.plot(mimgGmag, mrefGmag, 'g.', zorder=10, alpha=0.5)
    #########################################################################

    mimgGplot = axis.plot(mimgGmag,
                          mrefGmag,
                          '.',
                          color='g',
                          mfc='g',
                          mec='g',
                          zorder=10,
                          label='Matched Galaxies',
                          ms=2.5)

    legLines.append(mimgGplot[0])
    legLabels.append("Matched Galaxies")

    ################    --------------------------------  ##################
    # can't fig.savefig() with this Ellipse stuff ??? ... i love matplotlib
    if False:
        for i in range(len(mrefSmag)):
            a = Ellipse(xy=num.array([mimgSmag[i], mrefSmag[i]]),
                        width=mimgSmerr[i] / 2.,
                        height=mimgSmerr[i] / 2.,
                        alpha=0.5,
                        fill=True,
                        ec='b',
                        fc='b',
                        zorder=12)
            axis.add_artist(a)
    else:
        pass
        #axis.plot(mimgSmag, mrefSmag, "b.", zorder=12, alpha=0.5)
    ########################################################################

    mimgSplot = axis.plot(mimgSmag,
                          mrefSmag,
                          '.',
                          color='b',
                          mfc='b',
                          mec='b',
                          zorder=12,
                          label='Matched Stars',
                          ms=2.5)

    legLines.append(mimgSplot[0])
    legLabels.append("Matched Stars")

    if len(mrefGmag) == 0 and len(mrefSmag) == 0:
        xmin, xmax, ymin, ymax = -15, -8, 16, 28
    else:
        xmin, xmax, ymin, ymax = axis.axis()

    # Plot zpt
    xzpt = num.array((xmin, xmax))
    pzpt = axis.plot(xzpt, xzpt - zeropt, 'k--', label='Zeropoint')
    legLines.append(pzpt[0])
    legLabels.append("Zeropoint")

    maxN2 = 999

    nu, bu, pu = None, None, None

    # Unmatched & matched reference objects
    ax2 = fig.add_axes([0.1, 0.225, 0.125, 0.550], sharey=axis)
    if len(urefmag) > 0:
        nu, bu, pu = ax2.hist(urefmag,
                              bins=num.arange(ymin, ymax, 0.25),
                              orientation='horizontal',
                              facecolor='r',
                              log=True,
                              alpha=0.5,
                              zorder=1)
        if num.max(nu) > maxN2: maxN2 = num.max(nu)
        legLines.append(pu[0])
        legLabels.append("Unmatched Sources")

    if len(mrefGmag) > 0 and len(mrefSmag) > 0:
        nu, bu, pu = ax2.hist(num.concatenate((mrefGmag, mrefSmag)),
                              bins=num.arange(ymin, ymax, 0.25),
                              orientation='horizontal',
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
    elif len(mrefGmag):
        nu, bu, pu = ax2.hist(mrefGmag,
                              bins=num.arange(ymin, ymax, 0.25),
                              orientation='horizontal',
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
    elif len(mrefSmag) > 0:
        nu, bu, pu = ax2.hist(mrefSmag,
                              bins=num.arange(ymin, ymax, 0.25),
                              orientation='horizontal',
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
    ax2.set_xlabel('N', fontsize=10)
    ax2.set_ylabel('Reference catalog mag', fontsize=10)

    if nu is not None and num.max(nu) > maxN2: maxN2 = num.max(nu)

    maxN3 = 999

    nm, bm, pm = None, None, None

    # Unmatched & matched stellar objects
    ax3 = fig.add_axes([0.225, 0.1, 0.675, 0.125], sharex=axis)
    ax3.get_yaxis().set_ticks_position('right')
    ax3.get_yaxis().set_label_position('right')
    if len(mimgGmag) > 0 and len(mimgSmag) > 0:
        nm, bm, pm = ax3.hist(num.concatenate((mimgGmag, mimgSmag)),
                              bins=num.arange(xmin, xmax, 0.25),
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
        legLines.append(pm[0])
        legLabels.append("Matched Sources")
    elif len(mimgGmag) > 0:
        nm, bm, pm = ax3.hist(mimgGmag,
                              bins=num.arange(xmin, xmax, 0.25),
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
        legLines.append(pm[0])
        legLabels.append("Matched Sources")
    elif len(mimgSmag) > 0:
        nm, bm, pm = ax3.hist(mimgSmag,
                              bins=num.arange(xmin, xmax, 0.25),
                              log=True,
                              facecolor='b',
                              alpha=0.5,
                              zorder=2)
        legLines.append(pm[0])
        legLabels.append("Matched Sources")

    if nm is not None and num.max(nm) > maxN3: maxN3 = num.max(nm)

    if len(uimgmag) > 0:
        try:
            ax3.hist(uimgmag,
                     bins=num.arange(xmin, xmax, 0.25),
                     log=True,
                     facecolor='r',
                     alpha=0.5,
                     zorder=1)
        except:
            pass
    if abs(zeropt) < 1.0e-5:
        ax3.set_xlabel('Image calibrated %s mag' % (fluxType), fontsize=10)
    else:
        ax3.set_xlabel('Image instrumental %s mag' % (fluxType), fontsize=10)
    ax3.set_ylabel('N', rotation=180, fontsize=10)

    # Mag - Zpt
    ax4 = fig.add_axes([0.225, 0.775, 0.675, 0.125], sharex=axis)
    if len(mimgSmag):
        mimgSeb = ax4.errorbar(mimgSmag,
                               mimgSmag - zeropt - mrefSmag,
                               yerr=mimgSmerr,
                               fmt='bo',
                               ms=2,
                               alpha=0.25,
                               capsize=0,
                               elinewidth=0.5)
        mimgSeb[2][0].set_alpha(0.25)  # alpha for error bars

    if len(mimgGmag):
        mimgGeb = ax4.errorbar(mimgGmag,
                               mimgGmag - zeropt - mrefGmag,
                               yerr=mimgGmerr,
                               fmt='go',
                               ms=2,
                               alpha=0.25,
                               capsize=0,
                               elinewidth=0.5)
        mimgGeb[2][0].set_alpha(0.25)  # alpha for error bars

    ax4.get_yaxis().set_ticks_position('right')
    ax4.get_yaxis().set_label_position('right')
    ax4.set_ylabel('Cal-Ref', fontsize=10, rotation=270)
    ax4.axhline(y=0, c='k', linestyle='--', alpha=0.25)

    # Cleaning up figure
    qaPlotUtil.qaSetp(axis.get_xticklabels() + axis.get_yticklabels(),
                      visible=False)
    qaPlotUtil.qaSetp(ax2.get_xticklabels() + ax2.get_yticklabels(),
                      fontsize=8)
    qaPlotUtil.qaSetp(ax2.get_xticklabels(), rotation=90)
    qaPlotUtil.qaSetp(ax3.get_xticklabels() + ax3.get_yticklabels(),
                      fontsize=8)
    qaPlotUtil.qaSetp(ax4.get_xticklabels(), visible=False)
    qaPlotUtil.qaSetp(ax4.get_yticklabels(), fontsize=8)

    fig.legend(legLines,
               legLabels,
               numpoints=1,
               prop=FontProperties(size='x-small'),
               loc='center right')

    fig.suptitle('%s' % (title), fontsize=12)

    numerator = (mimgSmag - zeropt - mrefSmag)
    if len(numerator) == 0:
        numerator = num.array([0.0])
    denominator = mimgSmerr
    med = num.median(numerator)
    ax4.axhline(y=med, c='k', linestyle=':', alpha=0.5)

    # Final axis limits
    ax2.set_xlim(0.75, 3.0 * maxN2)
    ax3.set_ylim(0.75, 3.0 * maxN3)
    ax4.set_ylim(-0.24, 0.24)

    #axis.axis((xmax, xmin, ymax, ymin))
    axis.set_ylim(26, 14)
    axis.set_xlim(26 + zeropt, 14 + zeropt)

    return fig
Example #12
0
def plot(data):

    dmags = data['dmags']
    radii = data['radii']
    ids = data['ids']
    med, std = data['offsetStats']
    magTypes = data['magTypes']
    summary = data['summary']

    ymin, ymax = -0.5, 0.5
    if len(dmags) > 0:
        ymin = num.max([dmags.min(), -0.5])
        ymax = num.min([dmags.max(), 0.5])
    ylim = [ymin, ymax]

    if len(dmags) == 0:
        dmags = num.array([0.0])
        radii = num.array([0.0])
        ids = num.array([0])

    figsize = (4.0, 4.0)
    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)

    if not summary:

        sp1 = fig.add_subplot(111)
        sp1.plot(radii, dmags, 'ro', ms=2.0)
        sp1.set_ylim(ylim)

        ddmag = 0.001
        drad = 0.01 * (max(radii) - min(radii))
        if False:
            for i in range(len(dmags)):
                info = "nolink:sourceId=%s" % (ids[i])
                area = (radii[i] - drad, dmags[i] - ddmag, radii[i] + drad,
                        dmags[i] + ddmag)
                fig.addMapArea("no_label_info", area, info, axes=sp1)

        sp1.axhline(y=0, c='k', linestyle=':', alpha=0.25)
        sp1.axhline(y=med, c='b', linestyle='-')
        sp1.axhspan(ymin=med - std, ymax=med + std, fc='b', alpha=0.15)
        sp1x2 = sp1.twinx()
        ylab = sp1x2.set_ylabel('Delta magnitude (%s-%s)' % tuple(magTypes),
                                fontsize=10)
        ylab.set_rotation(-90)
        sp1.set_xlabel('Dist from focal plane center (pixels)', fontsize=10)
        qaPlotUtil.qaSetp(sp1.get_xticklabels() + sp1.get_yticklabels(),
                          fontsize=8)
        qaPlotUtil.qaSetp(sp1x2.get_xticklabels() + sp1x2.get_yticklabels(),
                          visible=False)

    else:
        ymin = num.max([dmags.min(), -0.5])
        ymax = num.min([dmags.max(), 0.5])
        ylim = [ymin, ymax]
        if ymin == ymax:
            ylim = [ymin - 0.1, ymax + 0.1]

        sp1 = fig.add_subplot(111)
        sp1.plot(radii, dmags, 'ro', ms=2, alpha=0.5)

        sp1.set_ylim(ylim)

        sp1.axhline(y=0, c='k', linestyle=':', alpha=0.25)
        sp1x2 = sp1.twinx()
        ylab = sp1x2.set_ylabel('Delta magnitude (%s-%s)' % tuple(magTypes),
                                fontsize=10)
        ylab.set_rotation(-90)
        sp1.set_xlabel('Dist from focal plane center (pixels)', fontsize=10)
        qaPlotUtil.qaSetp(sp1.get_xticklabels() + sp1.get_yticklabels(),
                          fontsize=8)
        qaPlotUtil.qaSetp(sp1x2.get_xticklabels() + sp1x2.get_yticklabels(),
                          visible=False)

    return fig
def plot(data):

    t = data['t']
    x = data['x']
    y = data['y']
    dx = data['dx']
    dy = data['dy']
    color = data['color']
    limits = data['limits']
    vLen = data['vLen']
    summary = data['summary']
    vlim = data['vlim']
    fwhm = data['fwhm']


    if not summary:
        xbLo, xbHi, ybLo, ybHi = data['bbox']
        x -= xbLo
        y -= ybLo

    #print x, y
    #print limits
    #print data['bbox']
    
    norm = colors.Normalize(vmin=vlim[0], vmax=vlim[1])
    sm = cm.ScalarMappable(norm, cmap=cm.jet)
    
    figsize = (5.0, 4.0)
    xlo, xhi, ylo, yhi = limits

    if len(x) == 0:
        x     = numpy.array([0.0])
        y     = numpy.array([0.0])
        dx    = numpy.array([0.0])
        dy    = numpy.array([0.0])
        color = numpy.array((0.0, 0.0, 0.0))

    #xmax, ymax = x.max(), y.max()
    xlim = [xlo, xhi] #[0, 1024*int(xmax/1024.0 + 0.5)]
    ylim = [ylo, yhi] #[0, 1024*int(ymax/1024.0 + 0.5)]

    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)
    
    fig.subplots_adjust(left=0.15, bottom=0.15)
    ax = fig.add_subplot(111)



    if summary:
        vmin, vmax = sm.get_clim()
        color = fwhm
        q = ax.quiver(x, y, vLen*dx, vLen*dy, color=sm.to_rgba(color),
                      scale=2.0*vLen, angles='xy', pivot='middle',
                      headlength=1.0, headwidth=1.0, width=0.002) 
        ax.quiverkey(q, 0.9, -0.12, 0.1*vLen, "e=0.1", coordinates='axes',
                     fontproperties={'size':"small"}, labelpos='E', color='k')
        q.set_array(color)
        try:
            cb = fig.colorbar(q) #, ax)
            cb.ax.set_xlabel("FWHM$_{\mathrm{xc,yc}}$", size="small")
            cb.ax.xaxis.set_label_position('top')
            for tick in cb.ax.get_yticklabels():
                tick.set_size("x-small")
        except Exception:
            cb = None
        ax.set_title("PSF Shape")
    else:
        q = ax.quiver(x, y, vLen*dx, vLen*dy, color=color, scale=2.0*vLen, angles='xy', pivot='middle',
                      headlength=1.0, headwidth=1.0, width=0.002)
        ax.quiverkey(q, 0.9, -0.12, 0.1*vLen, "e=0.1", coordinates='axes',
                     fontproperties={'size':"small"}, labelpos='E', color='k')
        ax.set_title("PSF Shape (FWHM$_{\mathrm{xc,yc}}$=%.2f)"%(fwhm[0]))

    ax.set_xlabel("x [pixels]") #, position=(0.4, 0.0))

    ax.set_ylabel("y [pixels]")
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    for tic in ax.get_xticklabels() + ax.get_yticklabels():
        tic.set_size("x-small")
    for tic in ax.get_xticklabels():
        tic.set_rotation(22)
    for tic in ax.get_yticklabels():
        tic.set_rotation(45)

    return fig
Example #14
0
    def testFindFake(self):
        """Test all the workings together.  Add a fake and find it."""

        np.random.seed(42)

        # make a fake trail in noise ... detect it.
        nx, ny = 512, 512
        r1, t1 = 300, 0.4
        r2, t2 = 200, 2.0*np.pi-0.2
        trail1 = measArt.SatelliteTrail(r1, t1)
        trail2 = measArt.SatelliteTrail(r2, t2)
        
        # create an exposure with a PSF object
        mimg = afwImage.MaskedImageF(nx, ny)
        exposure = afwImage.ExposureF(mimg)
        seeing = 2.0
        kx, ky = 15, 15
        psf = measAlg.DoubleGaussianPsf(kx, ky, seeing/2.35)
        exposure.setPsf(psf)

        # Add two fake trails
        img  = mimg.getImage().getArray()
        flux = 400.0 
        sigma = seeing
        prof = measArt.DoubleGaussianProfile(flux, sigma)
        width = 8*sigma
        trail1.insert(img, prof, width)
        trail2.insert(img, prof, width)

        # add noise on top of the trail
        rms = 20.0
        noise = rms*np.random.normal(size=(nx, ny))
        img += noise
        
        # create a finder and see what we get
        finder          = measArt.SatelliteFinder()
        trailsFind      = finder.getTrails(exposure, widths=[1.0])

        # try a task
        config               = measArt.HoughSatelliteConfig()
        config.narrow.eRange = 0.02
        task                 = measArt.HoughSatelliteTask(config=config)
        trailsTask, runtime  = task.process(exposure)

        # make sure both trails detected by finder and task
        self.assertEqual(len(trailsFind), 2)
        self.assertEqual(len(trailsTask), 2)

        # make sure we found the right ones!
        drMax = 5
        dThetaMax = 0.1
        for trails in trailsFind, trailsTask:
            found1 = trail1.isNear(trails[0], drMax, dThetaMax) or trail1.isNear(trails[1], drMax, dThetaMax)
            found2 = trail2.isNear(trails[0], drMax, dThetaMax) or trail2.isNear(trails[1], drMax, dThetaMax)
            self.assertTrue(found1)
            self.assertTrue(found2)

            
        fig = figure.Figure()
        can = FigCanvas(fig)
        ax = fig.add_subplot(111)
        ax.imshow(img, origin='lower', cmap='gray')
        for trail in trailsTask:
            x1, y1 = trail.trace(nx, ny, offset=10)
            x2, y2 = trail.trace(nx, ny, offset=-10)
            ax.plot(x1, y1, 'r-')
            ax.plot(x2, y2, 'r-')
        fig.savefig("fake.png")
Example #15
0
def main():
    parser = optparse.OptionParser(usage=__doc__)
    parser.add_option('-n',
                      '--new',
                      help='use with new output',
                      dest='new',
                      action='store_true',
                      default=False)
    parser.add_option('-m',
                      '--model',
                      help='model to use, bulge, disk, exp, dvc, or sersic',
                      dest='model',
                      default='sersic')

    opts, args = parser.parse_args()

    np.seterr(all='ignore')
    if len(args) != 4:
        parser.print_help()
        sys.exit(1)

    #read the data
    data = pyfits.open(args[0])[1].data
    num = {
        'DSERSIC': 0,
        'DDVC': 1 if opts.new else 3,
        'EXPSERSIC': 4,
        'DVC': 2,
        'SERSIC': 1,
        'DVCEXP': 2,
        'DVCSERSIC': 0
    }

    model = str.upper(opts.model)
    if model not in num.keys():
        if opts.new():
            model = 'DVCEXP'
        else:
            model = 'SERSIC'

    #write the n_b=4 b+d page
    s = "<html>\n"
    s += "<body>\n"
    s += "<table border=\"1\">\n"

    for i in range(len(data)):  #range(min(50,len(data))):
        s += "<tr>\n"
        s2 = "<table>\n"
        keys = [
            'IDENT',  #'ALPHA_J2000','DELTA_J2000',
            'CHISQ_%s' % model,
            #'FRACDEV', #'ZEST_TYPE',
            'MAD_SKY'
        ]
        if model in ['DSERSIC', 'DDVC', 'EXPSERSIC', 'DVCEXP', 'DVCSERSIC']:
            keys.append('REFF_' + model)
            keys.append('FLUX_RATIO_' + model)

        keys.append('MAD_%s_MASK' % model)
        #keys=('IDENT','ALPHA_J2000','DELTA_J2000','CHISQ_BULGE',
        #      'FRACDEV')
        for k in keys:
            v = data[i][k]
            s2 += "<tr><td>%s:</td><td>%s</td></tr>\n" % (k, str(v))

        if not opts.new:
            s2 += "<tr><td>%s</td><td>%s</td></tr>\n" % (
                'SERSIC N:', str(data[i]['SERSICFIT'][2]))
        if model == 'DSERSIC':
            s2 += "<tr><td>%s</td><td>%.2f,%.2f</td></tr>\n" % (
                'DSERSIC NS:', data[i]['DSERSICFIT'][2],
                data[i]['DSERSICFIT'][10])
        if model == 'EXPSERSIC':
            s2 += "<tr><td>%s</td><td>%s</td></tr>\n" % (
                'EXPSERSIC N:', str(data[i]['EXPSERSICFIT'][10]))
        if model == 'DVCSERSIC':
            s2 += "<tr><td>%s</td><td>%s</td></tr>\n" % (
                'DVCSERSIC N:', str(data[i]['DVCSERSICFIT'][2]))
#    s2 +=  "<tr><td>%s</td><td>%s</td></tr>\n" % ('B/T:', str(data[i]['SERSICFIT'][2]))
        s2 += "</table>\n"
        s += "<td>%s</td>\n" % (s2)

        #get the images
        try:
            realImage = pyfits.open(args[1]+'images/{0}.fits'. \
                    format(data.FILENAME[i]))[0].data
            modelImage = pyfits.open(args[2]+'M{0:09d}.fits'.\
                     format(int(data.IDENT[i])))[num[model]].data
            maskImage = pyfits.open(
                args[1] +
                'masks_all/{0}_mask.fits'.format(int(data.IDENT[i])))[0].data
            ivarImage = pyfits.open(args[1]+'ivar/{0}.wht.fits'. \
                            format(data.FILENAME[i]))[0].data
            print >> sys.stderr, "opening " + args[
                1] + 'images/' + data.FILENAME[i]

        except IOError:
            s += "<td>couldn't open image files for " + repr(
                data.IDENT[i]) + "</td>\n</tr>\n"
            print >> sys.stderr, "No image " + args[
                1] + 'images/' + data.FILENAME[i]
            continue

        #if data[i]['SERSICFIT'][1] < 1.e-4:
        #    print >>sys.stderr, "invalid sersic fit, skipping "+data.FILENAME[i]


#            continue

        imX, imY = realImage.shape
        x0 = data[i]['XCROP']
        y0 = data[i]['YCROP']
        imX = data[i]['XLEN']
        imY = data[i]['YLEN']
        rs = 3
        if not opts.new:
            offset = max(25, min(data[i]['SERSICFIT'][1] * rs, 300))
            x0 = max(data[i]['SERSICFIT'][5] - offset, x0)
            y0 = max(data[i]['SERSICFIT'][6] - offset, y0)
            imX = min(imX, data[i]['SERSICFIT'][5] + offset - x0)
            imY = min(imY, data[i]['SERSICFIT'][6] + offset - y0)
        else:
            offset = min(data[i]['DDVCFIT'][1] * rs, 300)
            x0 = max(data[i]['DDVCFIT'][5] - offset, x0)
            y0 = max(data[i]['DDVCFIT'][6] - offset, y0)
            imX = min(imX, data[i]['DDVCFIT'][5] + offset - x0)
            imY = min(imY, data[i]['DDVCFIT'][6] + offset - y0)
        realImage[maskImage == 1] = -1000
        showImage = copy.copy(realImage)
        #    showImage[np.abs(ivarImage) < 1.e-13] = 0.0
        showImage = showImage[y0:y0 + imY - 1, x0:x0 + imX - 1]
        origImage = copy.copy(modelImage)
        modelImage[maskImage == 1] = -1000
        modelImage = modelImage[y0:y0 + imY - 1, x0:x0 + imX - 1]

        modelfit = '%sFIT' % model
        if model in ('DDVC', 'EXPSERSIC', 'DSERSIC', 'DVCSERSIC', 'DVCEXP'):
            bulgereff = data[i][modelfit][9]
            bulgeq = data[i][modelfit][11]
            bulgephi = data[i][modelfit][15]
            diskreff = data[i][modelfit][1]
            diskq = data[i][modelfit][3]
            diskphi = data[i][modelfit][7]
            bulgecenX = data[i][modelfit][13]
            bulgecenY = data[i][modelfit][14]
            diskcenX = data[i][modelfit][5]
            diskcenY = data[i][modelfit][6]
            btt = data[i]['FLUX_RATIO_' + model]
        else:
            bulgereff = data[i][modelfit][1]
            bulgeq = data[i][modelfit][3]
            bulgephi = data[i][modelfit][7]
            diskreff = 0.0
            diskq = 0.0
            diskphi = 0.0
            bulgecenX = data[i][modelfit][5]
            bulgecenY = data[i][modelfit][6]
            diskcenX = 0.0
            diskcenY = 0.0
            btt = 1.0
    #bulgereff=data[i]['SERSICFIT'][1]
    #bulgeq=data[i]['SERSICFIT'][3]
    #bulgephi=data[i]['SERSICFIT'][7]
    #bulgecenX=data[i]['SERSICFIT'][5]
    #bulgecenY=data[i]['SERSICFIT'][6]
    #diskcenX=0.0
    #diskcenY=0.0
    #diskreff=0.0
    #diskq=0.0
    #diskphi=0.0
    #btt=1.0

        fig = figure.Figure((8.16, imY * 1.0 / imX * 2.0))
        fig.subplots_adjust(wspace=0,
                            left=0.1,
                            right=0.9,
                            top=1.0,
                            bottom=0.0,
                            hspace=0.0)
        canv = FigCanvas(fig)

        ax1 = fig.add_subplot(141, frameon=True)
        ax1.imshow(
            (np.arcsinh(showImage)),
            aspect='equal',
            interpolation='none',
            vmin=scoreatpercentile(
                np.arcsinh(showImage[showImage > -999].flatten()), 5),
            vmax=scoreatpercentile(np.arcsinh(showImage).flatten(), 99.9))
        ax1.tick_params(labelsize='xx-small')

        ax2 = fig.add_subplot(142, sharex=ax1, sharey=ax1)
        diff = (showImage - modelImage)
        diff[modelImage == 0] = 0
        ax2.imshow(diff,
                   aspect='equal',
                   interpolation='none',
                   vmin=scoreatpercentile(diff.flatten(), 0.5),
                   vmax=scoreatpercentile(diff.flatten(), 99.5))
        ax2.tick_params(labelleft='off', labelbottom='off')
        ax3 = fig.add_subplot(143, sharex=ax1, sharey=ax1)
        plot_mini_model(ax3, bulgereff, diskreff, bulgephi, diskphi, bulgeq,
                        diskq, bulgecenX - x0, bulgecenY - y0, diskcenX - x0,
                        diskcenY - y0, imX, imY, btt)
        ax4 = fig.add_subplot(144)
        realImage = np.asarray(realImage, dtype=np.float64)
        origImage = np.asarray(origImage, dtype=np.float64)
        realImage[maskImage == 1] = -1000

        mappable = ax4.imshow(
            np.arcsinh(ivarImage),
            aspect='equal',
            interpolation='none',
            vmin=scoreatpercentile(
                np.arcsinh(ivarImage[realImage > -999]).flatten(), 0.1),
            vmax=scoreatpercentile(np.arcsinh(ivarImage).flatten(), 99.9))
        cbar = fig.colorbar(mappable)
        cbar.ax.tick_params(labelsize=9)
        ax4.tick_params(labelsize='xx-small',
                        labelleft='off',
                        labelright='off')

        #improf = getProfile(realImage, data[i]['SERSICFIT'][1],
        #                    data[i]['SERSICFIT'][3],
        #                    data[i]['SERSICFIT'][7],
        #                    data[i]['SERSICFIT'][5],
        #                    data[i]['SERSICFIT'][6])
        #modprof = getProfile(origImage, data[i]['SERSICFIT'][1],
        #                     data[i]['SERSICFIT'][3],
        #                     data[i]['SERSICFIT'][7],
        #                     data[i]['SERSICFIT'][5],
        #                     data[i]['SERSICFIT'][6])
        #ax4.errorbar(improf.rad, improf.mnflux,
        #             yerr=improf.stdflux,
        #             fmt='o', mec='k', c='k')
        #ax4.plot(modprof.rad, modprof.mnflux, marker='None',
        #         c='g', ls='solid')
        #ax4.tick_params(labelright='on', labelleft='off',
        #               labelbottom='on', labelsize='xx-small')
        #ax4.set_yscale('log')
        #ax4.imshow(np.arcsinh(psfImage),aspect='equal')
        #ax4.tick_params(labelleft='off',labelbottom='off',labelright='on',
        #    labelsize='xx-small')
        #ax4.set_xlim(0,imX)
        #ax4.set_ylim(imY,0)

        fig.savefig('{2}/{1}fit/{0:09d}.png'.format(int(data.IDENT[i]),
                                                    str.lower(model), args[3]))
        s += "<td><img src=\"{1}fit/{0:09d}.png\"></td>\n".format(
            int(data.IDENT[i]), str.lower(model))
        s += "</tr>\n"

    s += "</table>\n"
    s += "</body>\n"
    s += "</html>\n"

    print s
def derrFigure(*args):
    mag0, diff0, star0, derr0, areaLabel, raft, ccd, figsize, xlim, ylim, xlim2, ylim2, ylimStep, \
          tag1, tag, mode, x, y, trend, magCut = args

    eps = 1.0e-5

    conv = colors.ColorConverter()
    size = 2.0
    red = conv.to_rgba('r')
    black = conv.to_rgba('k')
    mode = "stars"  # this better be the case!
    if len(mag0) == 0:
        mag0 = numpy.array([xlim[1]])
        diff0 = numpy.array([eps])
        derr0 = numpy.array([eps])
        star0 = numpy.array([0])

    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)
    fig.subplots_adjust(left=0.09, right=0.93, bottom=0.125)

    if len(mag0) == 0:
        mag0 = numpy.array([xlim[1]])
        diff0 = numpy.array([eps])
        derr0 = numpy.array([eps])
        star0 = numpy.array([0])

    whereStarGal = numpy.where(star0 > 0)[0]
    mag = mag0[whereStarGal]
    diff = diff0[whereStarGal]
    derr = derr0[whereStarGal]
    star = star0[whereStarGal]

    if len(mag) == 0:
        mag = numpy.array([eps])
        diff = numpy.array([eps])
        derr = numpy.array([eps])
        star = numpy.array([0])

    whereCut = numpy.where((mag < magCut))[0]
    whereOther = numpy.where((mag > magCut))[0]

    xlim = [14.0, 25.0]
    ylim3 = [0.001, 0.99]

    #####

    sp1 = fig.add_subplot(221)
    sp1.plot(mag[whereCut], diff[whereCut], "r.", ms=size, label=ccd)
    sp1.plot(mag[whereOther], diff[whereOther], "k.", ms=size, label=ccd)
    sp1.set_ylabel(tag, fontsize=10)

    #####
    sp2 = fig.add_subplot(222, sharex=sp1)

    def noNeg(xIn):
        x = numpy.array(xIn)
        if len(x) > 1:
            xMax = x.max()
        else:
            xMax = 1.0e-4
        return x.clip(1.0e-5, xMax)

    sp2.plot(mag[whereCut], noNeg(derr[whereCut]), "r.", ms=size, label=ccd)
    sp2.plot(mag[whereOther],
             noNeg(derr[whereOther]),
             "k.",
             ms=size,
             label=ccd)
    sp2.set_ylabel('Error Bars', fontsize=10)

    #####
    sp3 = fig.add_subplot(223, sharex=sp1)

    binmag = []
    binstd = []
    binmerr = []
    xmin = xlim[0]
    xmax = xlim[1]
    bins1 = numpy.arange(xmin, xmax, 0.5)
    for i in range(1, len(bins1)):
        idx = numpy.where((mag > bins1[i - 1]) & (mag <= bins1[i]))[0]
        if len(idx) == 0:
            continue
        #avgMag  = afwMath.makeStatistics(mag[idx], afwMath.MEAN).getValue(afwMath.MEAN)
        #stdDmag = 0.741 * afwMath.makeStatistics(diff[idx], afwMath.IQRANGE).getValue(afwMath.IQRANGE)
        #avgEbar = afwMath.makeStatistics(derr[idx], afwMath.MEAN).getValue(afwMath.MEAN)
        avgMag = mag[idx].mean()
        stdDmag = diff[idx].std()
        avgEbar = derr[idx].mean()
        binmag.append(avgMag)
        binstd.append(stdDmag)
        binmerr.append(avgEbar)
    # Shows the 2 curves
    sp3.plot(binmag, noNeg(binstd), 'r-', label="Phot RMS")
    sp3.plot(binmag, noNeg(binmerr), 'b--', label="Avg Error Bar")
    sp3.set_xlabel(tag1, fontsize=10)

    #####
    sp4 = fig.add_subplot(224, sharex=sp1)

    binmag = numpy.array(binmag)
    binstd = numpy.array(binstd)
    binmerr = numpy.array(binmerr)

    idx = numpy.where((binstd > binmerr))[0]
    errbarmag = binmag[idx]
    errbarresid = numpy.sqrt(binstd[idx]**2 - binmerr[idx]**2)

    whereCut = numpy.where((errbarmag < magCut))[0]
    whereOther = numpy.where((errbarmag > magCut))[0]

    sp4.plot(errbarmag[whereCut],
             noNeg(errbarresid[whereCut]),
             'ro',
             ms=3,
             label="Err Underestimate")
    sp4.plot(errbarmag[whereOther], noNeg(errbarresid[whereOther]), 'ko', ms=3)
    sp4.set_xlabel(tag1, fontsize=10)

    #### CONFIG
    sp2.semilogy()
    sp3.semilogy()
    sp4.semilogy()

    sp2.yaxis.set_label_position('right')
    sp2.yaxis.set_ticks_position('right')
    sp4.yaxis.set_label_position('right')
    sp4.yaxis.set_ticks_position('right')

    sp3.legend(prop=fm.FontProperties(size="xx-small"), loc="upper left")
    sp4.legend(prop=fm.FontProperties(size="xx-small"), loc="upper left")

    qaPlotUtil.qaSetp(sp1.get_xticklabels() + sp1.get_yticklabels(),
                      fontsize=8)
    qaPlotUtil.qaSetp(sp2.get_xticklabels() + sp2.get_yticklabels(),
                      fontsize=8)
    qaPlotUtil.qaSetp(sp3.get_xticklabels() + sp3.get_yticklabels(),
                      fontsize=8)
    qaPlotUtil.qaSetp(sp4.get_xticklabels() + sp4.get_yticklabels(),
                      fontsize=8)

    sp1.set_xlim(xlim)
    sp1.set_ylim(ylim)

    sp2.set_ylim(ylim3)
    sp3.set_ylim(ylim3)
    sp4.set_ylim(ylim3)

    return fig
def summaryFigure(*summaryArgs):

    # many of these are not needed, but for simplicity all the same args get passed
    # to the plot functions standardFigure(), derrFigure(), and summaryFigure()
    mag0, diff0, star0, derr0, areaLabel, raft, ccd, figsize, xlim, ylim, xlim2, ylim2, ylimStep, \
        tag1, tag, mode, x, y, trend, magCut = summaryArgs

    allMags = mag0
    allDiffs = diff0
    allStars = star0
    allDerr = derr0

    size = 2.0

    # dmag vs mag
    fig0 = figure.Figure(figsize=figsize)
    canvan = FigCanvas(fig0)

    fig0.subplots_adjust(left=0.125, bottom=0.125)
    rect0_1 = [0.125, 0.35, 0.478 - 0.125, 0.9 - 0.35]
    rect0_2 = [0.548, 0.35, 0.9 - 0.548, 0.9 - 0.35]
    rect0_1b = [0.125, 0.125, 0.478 - 0.125, 0.2]
    rect0_2b = [0.548, 0.125, 0.9 - 0.548, 0.2]
    ax0_1 = fig0.add_axes(rect0_1)
    ax0_2 = fig0.add_axes(rect0_2)
    ax0_1b = fig0.add_axes(rect0_1b, sharex=ax0_1)
    ax0_2b = fig0.add_axes(rect0_2b, sharex=ax0_2)

    w = numpy.where((allMags < magCut) & (allStars > 0))[0]
    wStar = numpy.where(allStars > 0)[0]
    wGxy = numpy.where(allStars == 0)[0]

    if len(w) > 0:
        lineFit = qaAnaUtil.robustPolyFit(allMags[w], allDiffs[w], 1)
        trendCoeffs = lineFit[0], lineFit[2]
        trendCoeffsLo = lineFit[0] + lineFit[1], lineFit[2] - lineFit[3]
        trendCoeffsHi = lineFit[0] - lineFit[1], lineFit[2] + lineFit[3]
    else:
        trendCoeffs = [0.0, 0.0]
        trendCoeffsLo = [0.0, 0.0]
        trendCoeffsHi = [0.0, 0.0]

    ####################
    # data for all ccds
    haveData = True
    if len(allMags) == 0:
        haveData = False
        allMags = numpy.array([xlim[1]])
        allDiffs = numpy.array([0.0])
        #allColor = [black]
        #allLabels = ["no_valid_data"]
        trendCoeffs = [0.0, 0.0]
        trendCoeffsLo = [0.0, 0.0]
        trendCoeffsHi = [0.0, 0.0]
    else:
        #
        # Lower plots

        mStar = allMags[wStar]
        dStar = allDiffs[wStar]
        eStar = allDerr[wStar]

        binmag = []
        binstd = []
        binmerr = []
        xmin = xlim[0]
        xmax = xlim[1]
        bins1 = numpy.arange(xmin, xmax, 0.5)
        for i in range(1, len(bins1)):
            idx = numpy.where((mStar > bins1[i - 1]) & (mStar < bins1[i]))[0]
            if len(idx) == 0:
                continue
            #avgMag  = afwMath.makeStatistics(mStar[idx], afwMath.MEAN).getValue(afwMath.MEAN)
            avgMag = mStar[idx].mean()
            #stdDmag = 0.741 * afwMath.makeStatistics(dStar[idx], afwMath.IQRANGE).getValue(afwMath.IQRANGE)
            stdDmag = dStar[idx].std()
            #avgEbar = afwMath.makeStatistics(eStar[idx], afwMath.MEAN).getValue(afwMath.MEAN)
            avgEbar = eStar[idx].mean()
            binmag.append(avgMag)
            binstd.append(stdDmag)
            binmerr.append(avgEbar)
        # Shows the 2 curves
        ax0_1b.plot(binmag, binstd, 'r-', label="Phot RMS")
        ax0_1b.plot(binmag, binmerr, 'b--', label="Avg Error Bar")

        binmag = numpy.array(binmag)
        binstd = numpy.array(binstd)
        binmerr = numpy.array(binmerr)

        medresid = 0.0
        idx = numpy.where((binstd > binmerr) & (binmag < magCut))[0]
        if len(idx) == 0:
            medresid = 0.0
        else:
            brightmag = binmag[idx]
            brightresid = numpy.sqrt(binstd[idx]**2 - binmerr[idx]**2)
            medresid = numpy.median(brightresid)
            if numpy.isnan(medresid) or medresid == None:
                medresid = 0.0

        #label       = "Quad error"
        #comment     = "Median value to add in quadrature to star phot error bars (mag < %.2f)" % (magCut)
        #testSet.addTest( testCode.Test(label, medresid, derrLimits, comment, areaLabel="all"))

        # SECOND SUBPANEL

        binmag = []
        binstd = []
        binmerr = []
        xmin2 = xlim2[0]
        xmax2 = xlim2[1]
        bins2 = numpy.arange(xmin2, xmax2, 0.5)
        for i in range(1, len(bins2)):
            idx = numpy.where((mStar > bins2[i - 1]) & (mStar < bins2[i]))[0]
            if len(idx) == 0:
                continue
            #avgMag  = afwMath.makeStatistics(mStar[idx], afwMath.MEAN).getValue(afwMath.MEAN)
            #stdDmag = 0.741 * afwMath.makeStatistics(dStar[idx], afwMath.IQRANGE).getValue(afwMath.IQRANGE)
            #avgEbar = afwMath.makeStatistics(eStar[idx], afwMath.MEAN).getValue(afwMath.MEAN)
            avgMag = mStar[idx].mean()
            stdDmag = dStar[idx].std()
            avgEbar = eStar[idx].mean()
            binmag.append(avgMag)
            binstd.append(stdDmag)
            binmerr.append(avgEbar)

        binmag = numpy.array(binmag)
        binstd = numpy.array(binstd)
        binmerr = numpy.array(binmerr)

        idx = numpy.where((binstd > binmerr))[0]
        errbarmag = binmag[idx]
        errbarresid = numpy.sqrt(binstd[idx]**2 - binmerr[idx]**2)
        ax0_2b.plot(errbarmag,
                    errbarresid,
                    'ko',
                    ms=3,
                    label="Err Underestimate")

        # Lower plots
        #

    #allColor = numpy.array(allColor)
    for ax in [ax0_1, ax0_2]:
        ax.plot(xlim2, [0.0, 0.0], "-k", lw=1.0)  # show an x-axis at y=0
        ax.plot(allMags[wGxy],
                allDiffs[wGxy],
                "bo",
                ms=size,
                label="galaxies",
                alpha=0.5)
        ax.plot(allMags[wStar],
                allDiffs[wStar],
                "ro",
                ms=size,
                label="stars",
                alpha=0.5)
        # 99 is the 'no-data' values
        if abs(trendCoeffs[0] - 99.0) > 1.0e-6:
            ax.plot(xlim2, numpy.polyval(trendCoeffs, xlim2), "-k", lw=1.0)
            ax.plot(xlim2, numpy.polyval(trendCoeffsLo, xlim2), "--k", lw=1.0)
            ax.plot(xlim2, numpy.polyval(trendCoeffsHi, xlim2), "--k", lw=1.0)

    # show outliers railed at the ylims
    wStarOutlier = numpy.where((numpy.abs(allDiffs) > ylim2[1])
                               & (allStars > 0))[0]
    wGxyOutlier = numpy.where((numpy.abs(allDiffs) > ylim2[1])
                              & (allStars == 0))[0]
    clip = 0.99
    ax0_2.plot(allMags[wStarOutlier],
               numpy.clip(allDiffs[wStarOutlier], clip * ylim2[0],
                          clip * ylim2[1]),
               'r.',
               ms=size)
    ax0_2.plot(allMags[wGxyOutlier],
               numpy.clip(allDiffs[wGxyOutlier], clip * ylim2[0],
                          clip * ylim2[1]),
               'g.',
               ms=size)
    ax0_2.legend(prop=fm.FontProperties(size="xx-small"), loc="upper left")

    dmag = 0.1
    ddiff1 = 0.01
    ddiff2 = ddiff1 * (ylim2[1] - ylim2[0]) / (ylim[1] - ylim[0]
                                               )  # rescale for larger y range
    if False:
        for j in xrange(len(allMags)):
            area = (allMags[j] - dmag, allDiffs[j] - ddiff1, allMags[j] + dmag,
                    allDiffs[j] + ddiff1)
            fig0.addMapArea(allLabels[j],
                            area,
                            "%.3f_%.3f" % (allMags[j], allDiffs[j]),
                            axes=ax0_1)
            area = (allMags[j] - dmag, allDiffs[j] - ddiff2, allMags[j] + dmag,
                    allDiffs[j] + ddiff2)
            fig0.addMapArea(allLabels[j],
                            area,
                            "%.3f_%.3f" % (allMags[j], allDiffs[j]),
                            axes=ax0_2)

    del allMags
    del allDiffs
    #del allColor
    #del allLabels

    # move the yaxis ticks/labels to the other side
    ax0_2.yaxis.set_label_position('right')
    ax0_2.yaxis.set_ticks_position('right')
    ax0_2b.yaxis.set_label_position('right')
    ax0_2b.yaxis.set_ticks_position('right')

    ax0_1b.set_xlabel(tag1, fontsize=12)
    ax0_2b.set_xlabel(tag1, fontsize=12)

    ax0_2.plot([xlim[0], xlim[1], xlim[1], xlim[0], xlim[0]],
               [ylim[0], ylim[0], ylim[1], ylim[1], ylim[0]], '-k')

    ax0_1b.semilogy()
    ax0_2b.semilogy()

    if haveData:
        ax0_1b.legend(prop=fm.FontProperties(size="xx-small"),
                      loc="upper left")
        ax0_2b.legend(prop=fm.FontProperties(size="xx-small"),
                      loc="upper left")

    qaPlotUtil.qaSetp(ax0_1.get_xticklabels() + ax0_2.get_xticklabels(),
                      visible=False)
    qaPlotUtil.qaSetp(ax0_1.get_yticklabels() + ax0_2.get_yticklabels(),
                      fontsize=11)
    qaPlotUtil.qaSetp(ax0_1b.get_yticklabels() + ax0_2b.get_yticklabels(),
                      fontsize=10)

    for ax in [ax0_1, ax0_2]:
        ax.set_ylabel(tag)

    ax0_1.set_xlim(xlim)
    ax0_2.set_xlim(xlim2)
    ax0_1.set_ylim(ylim)
    ax0_2.set_ylim(ylim2)

    ax0_1b.set_ylim(0.001, 0.99)
    ax0_2b.set_ylim(0.001, 0.99)

    return fig0
Example #18
0
def main(infile, ccds=None, camera="hsc", cmap="copper", cols=[0,1],
         nsig=3.0, percent=False, textcolor='k', out=None):

    ###########################
    # build the camera
    policy_file = {
        'hsc': os.path.join(os.getenv("OBS_SUBARU_DIR"), "hsc", "hsc_geom.paf"),
        'sc' : os.path.join(os.getenv("OBS_SUBARU_DIR"), "suprimecam", "Full_Suprimecam_geom.paf")
        }
    geomPolicy = afwCGU.getGeomPolicy(policy_file[camera.lower()])
    camera     = afwCGU.makeCamera(geomPolicy)

    
    ###########################
    # load the data
    data = {}
    if infile:
        load = numpy.loadtxt(infile)
        vals = load[:,cols[1]]
        for i in range(len(vals)):
            ccd = int(load[i,cols[0]])
            if len(cols) == 3:
                amp = int(load[i,cols[2]])
            else:
                amp = 0
            if ccd not in data:
                data[ccd] = {}
            data[ccd][amp] = vals[i]
    else:
        vals = []
        for r in camera:
            for c in afwCG.cast_Raft(r):
                ccd = afwCG.cast_Ccd(c)
                ccdId = ccd.getId().getSerial()
                data[ccdId] = {}
                val = 1.0
                if ccdId > 103:
                    val = 0.8
                for a in ccd:
                    amp = afwCG.cast_Amp(a)
                    ampId = amp.getId().getSerial() - 1
                    data[ccdId][ampId] = val
                    vals.append(val)
                if len(data[ccdId]) == 0:
                    data[ccdId][0] = val
                    vals.append(val)
        vals = numpy.array(vals)
        
    mean = vals.mean()
    med  = numpy.median(vals)
    std  = vals.std()
    vmin, vmax = med - nsig*std, med+nsig*std
    

    ###########################
    # make the plot
    fig = figure.Figure(figsize=(7,7))
    canvas = FigCanvas(fig)

    if infile:
        rect = (0.06, 0.12, 0.76, 0.76)
    else:
        rect = (0.06, 0.06, 0.88, 0.88)
        
    fpa_fig = hscUtil.FpaFigure(fig, camera, rect=rect)
    
    for i_ccd, amplist in data.items():

        hide = False
        if ccds and (i_ccd not in ccds):
            hide = True
        
        ax = fpa_fig.getAxes(i_ccd)
        fpa_fig.highlightAmp(i_ccd, 0)
        nq = fpa_fig.detectors[i_ccd].getOrientation().getNQuarter()
        nx, ny = 4, 8
        if nq % 2:
            ny, nx = nx, ny
        firstAmp = sorted(amplist.keys())[0]
        im = numpy.zeros((ny, nx)) + amplist[firstAmp]

        print i_ccd
        for amp, val in amplist.items():
            useVal = val
            if hide:
                useVal = 0.0
            if nq == 0:
                im[:,amp] = useVal
            if nq == 1 or nq == -3:
                im[3-amp,:] = useVal
            if nq == 2:
                im[:,3-amp] = useVal
            if nq == -1 or nq == 3:
                im[amp,:] = useVal
            
        im_ax = ax.imshow(im, cmap=cmap, vmax=vmax, vmin=vmin, interpolation='nearest')

        fpa_fig.addLabel(i_ccd, [str(i_ccd)], color=textcolor)


    #############################
    # the colorbar
    ylo, yhi = vmin, vmax

    if infile:
        rect = (0.91, 0.25, 0.02, 0.6)
        
        # real units
        cax  = fig.add_axes(rect)        
        cax.get_yaxis().get_major_formatter().set_useOffset(False)        
        cax.set_ylim([ylo, yhi])
        cax.get_xaxis().set_ticks([])
        cbar = fig.colorbar(im_ax, cax=cax)

        # mirror the values.  std or in percent (fractional) if requested
        caxp = cax.twinx()
        caxp.get_xaxis().set_ticks([])
        caxp.get_yaxis().get_major_formatter().set_useOffset(False)        
        if percent:
            caxp.set_ylim([(med-ylo)/(yhi-ylo), (yhi-med)/(yhi-ylo)])
        else:
            caxp.set_ylim([-nsig*std, nsig*std])


        for t in cax.get_yticklabels():
            t.set_size('small')
        for t in caxp.get_yticklabels():
            t.set_size("small")


    #################################
    # add stats
    if infile:
        stats = {"Mean":mean, "Med":med, "Std":std, "Max":vals.max(), "Min":vals.min()}
        order = ["Mean", "Med", "Std", "Min", "Max"]
        i = 0
        for k in order:
            v = stats[k]
            ax.text(0.8, 0.03+0.02*i, "%-10s %.3g"%(k+":",v), fontsize=9,
                    horizontalalignment='left', verticalalignment='center', transform=fig.transFigure)
            i += 1

    #################################
    # title and write it
    date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
    title = "File: %s  Cols: %s  %s" % (infile, ":".join([str(x+1) for x in cols]), date) if infile else ""
    fig.suptitle(title)

    outfile = out if out else "camview-%s.png" % infile
    fig.savefig(outfile)
def standardFigure(*args):

    mag0, diff0, star0, derr0, areaLabel, raft, ccd, figsize, xlim, ylim, xlim2, ylim2, ylimStep, \
        tag1, tag, mode, x, y, trend, magCut = args

    eps = 1.0e-5

    conv = colors.ColorConverter()
    size = 2.0

    xlimDefault = [14.0, 25.0]

    red = conv.to_rgba('r')
    black = conv.to_rgba('k')

    x0 = x.copy()
    y0 = y.copy()
    modeIdx = {'all': 0, 'galaxies': 1, 'stars': 2}
    lineFit = copy.copy(trend)[modeIdx[mode]]

    trendCoeffs = lineFit[0], lineFit[2]
    trendCoeffsLo = lineFit[0] + lineFit[1], lineFit[2] - lineFit[3]
    trendCoeffsHi = lineFit[0] - lineFit[1], lineFit[2] + lineFit[3]
    #print trendCoeffs
    if len(mag0) == 0:
        mag0 = numpy.array([xlim[1]])
        diff0 = numpy.array([eps])
        x0 = numpy.array([eps])
        y0 = numpy.array([eps])
        star0 = numpy.array([0])

    #################
    # data for one ccd
    if mode == 'fourPanel':
        figsize = (6.5, 5.0)

    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)
    fig.subplots_adjust(left=0.09, right=0.93, bottom=0.125)

    if mode == 'fourPanel':
        ax_1s = fig.add_subplot(221)
        ax_2s = fig.add_subplot(222)
        ax_1g = fig.add_subplot(223)
        ax_2g = fig.add_subplot(224)
        axSets = [[ax_1s, ax_2s], [ax_1g, ax_2g]]
        starGalLabels = ["stars", "galaxies"]
        whereStarGals = [numpy.where(star0 == 0)[0], numpy.where(star0 > 0)[0]]
    else:
        ax_1 = fig.add_subplot(131)
        ax_2 = fig.add_subplot(132)
        ax_3 = fig.add_subplot(133)
        axSets = [[ax_1, ax_2, ax_3]]
        starGalLabels = [mode]
        if mode == 'stars':
            whereStarGals = [numpy.where(star0 > 0)[0]]
        if mode == 'galaxies':
            whereStarGals = [numpy.where(star0 == 0)[0]]
        if mode == 'all':
            starGalLabels = ["all data"]
            whereStarGals = [numpy.where(star0 > -1)[0]]

    for iSet in range(len(axSets)):
        ax_1, ax_2, ax_3 = axSets[iSet]
        starGalLabel = starGalLabels[iSet]
        whereStarGal = whereStarGals[iSet]

        mag = mag0[whereStarGal]
        diff = diff0[whereStarGal]
        x = x0[whereStarGal]
        y = y0[whereStarGal]
        star = star0[whereStarGal]

        if len(x) == 0:
            mag = numpy.array([eps])
            diff = numpy.array([eps])
            x = numpy.array([eps])
            y = numpy.array([eps])
            star = numpy.array([0])

        whereCut = numpy.where((mag < magCut))[0]
        whereOther = numpy.where((mag > magCut))[0]

        xTrend = numpy.array(xlim)
        ax_1.plot(xTrend, numpy.array([eps, eps]), "-k", lw=1.0)

        ax_1.text(1.02 * xlim[0],
                  0.87 * ylim[1],
                  starGalLabel,
                  size='x-small',
                  horizontalalignment='left')
        for ax in [ax_1, ax_2]:
            ax.plot(mag[whereOther],
                    diff[whereOther],
                    "k.",
                    ms=size,
                    label=ccd)
            ax.plot(mag[whereCut], diff[whereCut], "r.", ms=size, label=ccd)
            ax.set_xlabel(tag1, size='small')

        norm = colors.Normalize(vmin=-0.05, vmax=0.05, clip=True)
        cdict = {
            'red': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)),
            'green': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'blue': ((0.0, 1.0, 1.0), (0.5, 0.0, 0.0), (1.0, 0.0, 0.0))
        }
        my_cmap = colors.LinearSegmentedColormap('my_colormap', cdict, 256)

        midSize = 2.0
        maxSize = 6 * midSize
        minSize = midSize / 2
        magLo = xlim[0] if xlim[0] > mag0.min() else mag0.min()
        magHi = magCut + 2.0  #xlim[1] if xlim[1] < mag0.max() else mag0.max()
        sizes = minSize + (maxSize - minSize) * (magHi - mag) / (magHi - magLo)
        sizes = numpy.clip(sizes, minSize, maxSize)
        xyplot = ax_3.scatter(x,
                              y,
                              s=sizes,
                              c=diff,
                              marker='o',
                              cmap=my_cmap,
                              norm=norm,
                              edgecolors='none')

        if len(x0) > 1:
            ax_3.set_xlim([x0.min(), x0.max()])
            ax_3.set_ylim([y0.min(), y0.max()])
        else:
            ax_3.set_xlim(xlimDefault)
            ax_3.set_ylim(xlimDefault)

        ax_3.set_xlabel("x", size='x-small')
        #ax_3.set_ylabel("y", labelpad=20, y=1.0, size='x-small', rotation=0.0)
        try:
            cb = fig.colorbar(xyplot)
            cb.ax.set_xlabel("$\delta$ mag", size="x-small")
            cb.ax.xaxis.set_label_position('top')
            for tick in cb.ax.get_yticklabels():
                tick.set_size("x-small")
        except Exception:
            cb = None
        for t in ax_3.get_xticklabels() + ax_3.get_yticklabels():
            t.set_rotation(45.0)
            t.set_size('xx-small')
        #for t in ax_3.get_yticklabels():
        #    t.set_size('xx-small')

        lineVals = numpy.lib.polyval(trendCoeffs, xTrend)
        lineValsLo = numpy.lib.polyval(trendCoeffsLo, xTrend)
        lineValsHi = numpy.lib.polyval(trendCoeffsHi, xTrend)

        if abs(trendCoeffs[0] - 99.0) > 1.0e-6:
            lmin, lmax = lineVals.min(), lineVals.max()
            if False:
                if lmin < ylim[0]:
                    ylim[0] = -(int(abs(lmin) / ylimStep) + 1) * ylimStep
                if lmax > ylim[1]:
                    ylim[1] = (int(lmax / ylimStep) + 1) * ylimStep

            ax_1.plot(xTrend, lineVals, "-r", lw=1.0)
            ax_1.plot(xTrend, lineValsLo, "--r", lw=1.0)
            ax_1.plot(xTrend, lineValsHi, "--r", lw=1.0)
            ax_2.plot(xTrend, lineVals, "-r", lw=1.0)
            ax_2.plot(xTrend, lineValsLo, "--r", lw=1.0)
            ax_2.plot(xTrend, lineValsHi, "--r", lw=1.0)

        ax_2.plot([xlim[0], xlim[1], xlim[1], xlim[0], xlim[0]],
                  [ylim[0], ylim[0], ylim[1], ylim[1], ylim[0]], '-k')
        ax_1.set_ylabel(tag, size="small")
        ax_1.set_xlim(xlim if xlim[0] != xlim[1] else xlimDefault)
        ax_2.set_xlim(xlim2 if xlim2[0] != xlim2[1] else xlimDefault)
        ax_1.set_ylim(ylim if ylim[0] != ylim[1] else [-0.1, 0.1])
        ax_2.set_ylim(ylim2 if ylim2[0] != ylim2[1] else [-0.1, 0.1])

        # move the y axis on right panel
        #ax_3.yaxis.set_label_position('right')
        #ax_3.yaxis.set_ticks_position('right')

        dmag = 0.1
        ddiff1 = 0.02
        ddiff2 = ddiff1 * (ylim2[1] - ylim2[0]) / (
            ylim[1] - ylim[0])  # rescale for larger y range

        if False:
            for j in xrange(len(mag)):
                info = "nolink:x:%.2f_y:%.2f" % (x[j], y[j])
                area = (mag[j] - dmag, diff[j] - ddiff1, mag[j] + dmag,
                        diff[j] + ddiff1)
                fig.addMapArea(areaLabel, area, info, axes=ax_1)
                area = (mag[j] - dmag, diff[j] - ddiff2, mag[j] + dmag,
                        diff[j] + ddiff2)
                fig.addMapArea(areaLabel, area, info, axes=ax_2)

        for ax in [ax_1, ax_2]:
            for tick in ax.get_xticklabels() + ax.get_yticklabels():
                tick.set_size("x-small")

    return fig
def plot(data):

    x = data['x']
    y = data['y']
    dx = data['dx']
    dy = data['dy']
    limits = data['limits']
    gridVectors = data['gridVectors']

    xlo, xhi, ylo, yhi = limits
    xwid, ywid = xhi - xlo, yhi - ylo

    if not gridVectors:
        xbLo, xbHi, ybLo, ybHi = data['bbox']
        x -= xbLo
        y -= ybLo

    #print x
    #print y
    #print limits
    #print data['bbox']

    #
    figsize = (6.5, 3.25)
    conv = colors.ColorConverter()
    black = conv.to_rgb('k')
    red = conv.to_rgb('r')
    green = conv.to_rgb('g')

    # if there's no data, dump a single point at 0,0
    if not len(x) > 0:
        x = numpy.array([0.0])
        y = numpy.array([0.0])
        dx = numpy.array([0.0])
        dy = numpy.array([0.0])

    if len(x) > 1:
        xlim = xlo, xhi
        ylim = ylo, yhi
        #xlim = [xmin, xmin + 1024*round((xmax-xmin)/1024.0 + 0.5)]
        #ylim = [ymin, ymin + 1024*round((ymax-ymin)/1024.0 + 0.5)]
        #xlim = [0.0, 1024*round((xmax-xmin)/1024.0 + 0.5)]
        #ylim = [0.0, 1024*round((ymax-ymin)/1024.0 + 0.5)]
    else:
        xlim = [0, 1.0]
        ylim = [0, 1.0]

    xmin, xmax = xlim
    ymin, ymax = ylim

    #print x, y
    #print xmin, ymin
    r = numpy.sqrt(dx**2 + dy**2)
    rmax = 1.2  # r.max()

    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)
    fig.subplots_adjust(left=0.1)

    ################
    # ccd view
    ax = fig.add_subplot(121)
    box = ax.get_position()
    ax.set_position(
        [box.x0, box.y0 + 0.1 * box.height, box.width, 0.9 * box.height])

    # for the 'all' plot, just show avg vectors in grid cells
    if gridVectors:
        nx, ny = 8, 8
        xstep, ystep = (xlim[1] - xlim[0]) / nx, (ylim[1] - ylim[0]) / ny
        if xstep == 0:
            xstep = 1.0
        if ystep == 0:
            ystep = 1.0

        xgrid = [[0.0] * nx for i in range(ny)]
        ygrid = [[0.0] * nx for i in range(ny)]
        ngrid = [[0] * nx for i in range(ny)]
        for i in range(len(x)):
            ix, iy = int((x[i] - xlim[0]) / xstep), int(
                (y[i] - ylim[0]) / ystep)
            if ix >= 0 and ix < nx and iy >= 0 and iy < ny:
                xgrid[ix][iy] += dx[i]
                ygrid[ix][iy] += dy[i]
                ngrid[ix][iy] += 1

        xt, yt, dxt, dyt, nt = [], [], [], [], []
        for ix in range(nx):
            for iy in range(ny):
                xt.append(xstep * (ix + 0.5))
                yt.append(ystep * (iy + 0.5))
                nt.append("%d" % (ngrid[ix][iy]))
                if ngrid[ix][iy] > 0:
                    xval = xgrid[ix][iy] / ngrid[ix][iy]
                    yval = ygrid[ix][iy] / ngrid[ix][iy]
                    dxt.append(xval)
                    dyt.append(yval)
                else:
                    dxt.append(0.0)
                    dyt.append(0.0)

        #for i in range(len(xt)):
        #    print xt[i], yt[i], dxt[i], dyt[i]
        q = ax.quiver(numpy.array(xt),
                      numpy.array(yt),
                      numpy.array(dxt),
                      numpy.array(dyt),
                      color='k',
                      scale=1.0,
                      angles='xy',
                      pivot='middle',
                      width=0.004)
        ax.quiverkey(q,
                     0.9,
                     -0.2,
                     0.1,
                     "100 mas",
                     coordinates='axes',
                     fontproperties={'size': 'small'})
        for i in range(len(xt)):
            ax.text(xt[i] + 0.1 * xstep, yt[i] + 0.1 * ystep, nt[i], size=5)
        xlim = [0.0, xlim[1] - xlim[0]]
        ylim = [0.0, ylim[1] - ylim[0]]
    else:
        ax.scatter(x, y, 0.5, color='r')
        q = ax.quiver(x, y, dx, dy, color='k', scale=10.0, angles='xy')
        ax.quiverkey(q,
                     0.9,
                     -0.2,
                     1.0,
                     "1 arcsec",
                     coordinates='axes',
                     fontproperties={'size': "small"})

    ax.xaxis.set_major_locator(MaxNLocator(8))
    ax.yaxis.set_major_locator(MaxNLocator(8))

    ax.set_xlabel("x [pixels]")
    ax.set_ylabel("y [pixels]")
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    for tic in ax.get_xticklabels() + ax.get_yticklabels():
        tic.set_size("x-small")

    ################
    # rose view
    xmargin = 0.07
    ymargin = 0.12
    spacer = 0.03
    left, bottom, width, height = 0.5 + spacer, 0.35 + spacer, 0.5 - 2 * xmargin, 0.65 - ymargin - spacer
    ax = fig.add_axes([left, bottom, width, height])

    #get the figure width/heigh in inches to correct
    #aspect ratio
    f_w, f_h = fig.get_size_inches()
    xlimRose = [
        -width * f_w / (height * f_h) * rmax,
        f_w * width / (f_h * height) * rmax
    ]
    limRose = [-rmax, rmax]
    # this is much faster than calling plot() in a loop, and quiver() scale length buggy
    if gridVectors:
        ybin = 50
        xbin = 50  #int(1.0*ybin*f_w*width/(f_h*height))
        qaPlotUtil.make_densityplot(ax,
                                    dx,
                                    dy,
                                    xlims=limRose,
                                    ylims=limRose,
                                    bins=(xbin, ybin),
                                    log=True)
        qaPlotUtil.make_densityContour(ax,
                                       dx,
                                       dy,
                                       xlims=limRose,
                                       ylims=limRose,
                                       bins=(xbin, ybin),
                                       log=True,
                                       percentiles=True,
                                       normed=False,
                                       levels=[0.5])
        c0 = Circle((0.0, 0.0),
                    radius=0.0,
                    facecolor='none',
                    edgecolor=green,
                    zorder=3,
                    label="50%")
        ax.vlines(0.0, limRose[0], limRose[1], linestyle='dashed')
        ax.hlines(0.0, xlimRose[0], xlimRose[1], linestyle='dashed')
        ax.add_patch(c0)
    else:
        z = numpy.zeros(len(dx))
        xy2 = zip(dx, dy)
        xy1 = zip(z, z)
        lines = zip(xy1, xy2)
        p = LineCollection(lines,
                           colors=red * len(lines),
                           zorder=1,
                           label="_nolegend_")
        ax.add_collection(p)
        ax.scatter(dx, dy, s=0.05, color='k', zorder=2, label="_nolegend_")

        #r = numpy.sqrt(dx**2 + dy**2)
        #rmax = r.max()
        isort = r.argsort()
        i50 = isort[len(r) / 2]
        r50 = r[i50]
        c50 = Circle((0.0, 0.0),
                     radius=r50,
                     facecolor='none',
                     edgecolor=green,
                     zorder=3,
                     label="50%")
        ax.add_patch(c50)

        ax.vlines(0.0, limRose[0], limRose[1], linestyle='dashed')
        ax.hlines(0.0, xlimRose[0], xlimRose[1], linestyle='dashed')

    fp = fm.FontProperties(size="xx-small")
    ax.legend(prop=fp)

    ax.xaxis.set_label_position('top')
    ax.yaxis.set_label_position('right')
    ax.xaxis.set_ticks_position('top')
    ax.yaxis.set_ticks_position('right')

    ax.set_xlabel("dRa [arcsec]", size='x-small')
    ax.set_ylabel("dDec [arcsec]", size='x-small')
    ax.set_xlim(xlimRose)
    ax.set_ylim(limRose)
    for tic in ax.get_xticklabels() + ax.get_yticklabels():
        tic.set_size("x-small")

    ################
    # hist view
    left, bottom, width, height = 0.5 + spacer, 0.0 + ymargin, 0.5 - 2 * xmargin, 0.35 - ymargin
    ax0 = fig.add_axes([left, bottom, width, height])

    rNmax = 1.0
    if len(r) > 1:
        binWidth = 0.5 * numpy.std(r) * (20.0 / len(r))**0.2
        nBin = (r.max() - r.min()) / binWidth
        rN, rBin, xx = ax0.hist(r, bins=nBin)

        # add a median arrow
        rmed = numpy.median(r)
        w = numpy.where(rBin > rmed)[0]
        if len(w) > 0 and len(rN) > w[0] - 1:
            histMed = 1.1 * rN[w[0] - 1]
        else:
            histMed = 0.0
        rNmax = rN.max()
        ax0.arrow(rmed,
                  1.2 * rNmax,
                  0.0,
                  histMed - 1.2 * rNmax,
                  facecolor='r',
                  edgecolor='r',
                  lw=0.5)
        ax0.text(1.2 * rmed,
                 0.5 * (histMed + 1.2 * rNmax),
                 "median",
                 verticalalignment="center",
                 color='k',
                 size='x-small')

    ax0.yaxis.set_ticks_position('right')
    ax0.yaxis.set_label_position('right')

    ax0.set_xlabel("r [arcsec]", size='x-small')
    ax0.set_ylabel("N", size='x-small')
    ax0.set_xlim([0, 0.9 * rmax])
    ax0.set_ylim([0, 1.4 * rNmax])

    for tic in ax0.get_xticklabels() + ax0.get_yticklabels(
    ):  # + ax.get_xticklabels():
        tic.set_size("xx-small")

    return fig
Example #21
0
            except Exception, e:
                raise RuntimeError("Could not get stats on vmax CCD" +
                                   str(vmax) + "  Exiting." + str(e))

        elif re.search(":", vmax):
            vmaxCcd = None
            vmin, vmax = [float(x) for x in vmax.split(":")]
            med = 0.5 * (vmax + vmin)
        else:
            vmaxCcd = None
            vmax = float(vmax)
            med = 0.5 * (vmax + vmin)

    ###########################
    fig = figure.Figure((8, 8))
    canvas = FigCanvas(fig)

    l, b, w, h = 0.08, 0.12, 0.84, 0.78
    rect = l, b, w, h
    if showCbar:
        rect = (0.06, 0.12, 0.76, 0.76)
    fpa_fig = hscUtil.FpaFigure(fig, butler1.get('camera'), rect=rect)

    im_ax = None
    i_show = 0
    for dataId1 in dataIds1:
        if dataId1['ccd'] not in ccdno:
            continue
        print dataId1,

        if visit2:
def plot(data):

    x = data['x']
    y = data['y']
    xmat = data['xmat']
    ymat = data['ymat']
    limits = data['limits']
    summary = data['summary']
    nx, ny = data['nxn']

    xlo, xhi, ylo, yhi = limits
    xwid, ywid = xhi - xlo, yhi - ylo

    if not summary:
        xbLo, xbHi, ybLo, ybHi = data['bbox']
        x -= xbLo
        y -= ybLo
        xmat -= xbLo
        ymat -= ybLo

    # handle no-data possibility
    if len(x) == 0:
        x = numpy.array([0.0])
        y = numpy.array([0.0])

    summaryLabel = "matched"
    if len(xmat) == 0:
        if len(x) == 0 or not summary:
            xmat = numpy.array([0.0])
            ymat = numpy.array([0.0])
        else:
            summaryLabel = "detected"
            xmat = x
            ymat = y

    figsize = (4.0, 4.0)

    ####################
    # create the plot
    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)
    ax = fig.add_subplot(111)
    fig.subplots_adjust(left=0.19)  #, bottom=0.15)

    ncol = None
    if summary:
        ms = 0.1
        if len(xmat) < 10000:
            ms = 0.2
        if len(xmat) < 1000:
            ms = 0.5
        ax.plot(xmat, ymat, "k.", ms=ms, label=summaryLabel)
        ncol = 1
    else:
        ax.plot(x, y, "k.", ms=2.0, label="detected")
        ax.plot(xmat,
                ymat,
                "ro",
                ms=4.0,
                label="matched",
                mfc='None',
                markeredgecolor='r')
        ncol = 2

    ax.set_xlim([xlo, xhi])
    ax.set_ylim([ylo, yhi])
    ax.set_xlabel("x [pixel]", size='x-small')
    ax.set_ylabel("y [pixel]", size='x-small')
    ax.legend(prop=fm.FontProperties(size="xx-small"),
              ncol=ncol,
              loc="upper center")
    for tic in ax.get_xticklabels() + ax.get_yticklabels():
        tic.set_size("x-small")

    # don't bother with this stuff for the final summary plot
    if not summary:
        # show the regions
        for i in range(nx):
            xline = (i + 1) * xwid / nx
            ax.axvline(xline, color="k")
        for i in range(ny):
            yline = (i + 1) * ywid / ny
            ax.axhline(yline, color="k")

        # add map areas to allow mouseover tooltip showing pixel coords
        dx, dy = 20, 20  # on a 4kx4k ccd, < +/-20 pixels is tough to hit with a mouse
        #for i in range(len(x)):
        #    area = x[i]-dx, y[i]-dy, x[i]+dx, y[i]+dy
        #    fig.addMapArea("no_label_info", area, "nolink:%.1f_%.1f"%(x[i],y[i]))

        ax.set_title("Matched Detections by CCD Sector", size='small')
    else:
        ax.set_title("Matched Detections", size='small')

    return fig
def plot(data):

    title = data['title']
    orphan = data['orphan']
    depth = data['depth']
    matchedStar = data['matchedStar']
    blendedStar = data['blendedStar']
    undetectedStar = data['undetectedStar']
    matchedGalaxy = data['matchedGalaxy']
    blendedGalaxy = data['blendedGalaxy']
    undetectedGalaxy = data['undetectedGalaxy']
    bins = data['bins']

    figsize = (4.0, 4.0)
    fig = figure.Figure(figsize=figsize)
    canvas = FigCanvas(fig)

    sp1 = fig.add_subplot(211)
    fig.subplots_adjust(left=0.13)
    sp2 = fig.add_subplot(212, sharex=sp1)

    # Stacked histogram
    orphanHist = num.histogram(orphan, bins=bins)
    matchedStarHist = num.histogram(matchedStar, bins=bins)
    blendedStarHist = num.histogram(blendedStar, bins=bins)
    undetectedStarHist = num.histogram(undetectedStar, bins=bins)
    # For bar, you send the coordinate of the left corner of the bar
    barbins = orphanHist[1][:-1]
    width = 1.0 * (orphanHist[1][1] - orphanHist[1][0])
    orphanBar = sp1.bar(barbins,
                        orphanHist[0],
                        width=width,
                        color='r',
                        alpha=0.5,
                        label='Orphan',
                        capsize=1)
    bottom = orphanHist[0]
    matchedBar = sp1.bar(barbins,
                         matchedStarHist[0],
                         width=width,
                         color='g',
                         alpha=0.5,
                         label='Matched',
                         bottom=bottom,
                         capsize=1)
    bottom += matchedStarHist[0]
    blendedBar = sp1.bar(barbins,
                         blendedStarHist[0],
                         width=width,
                         color='cyan',
                         alpha=0.5,
                         label='Blended',
                         bottom=bottom,
                         capsize=1)
    bottom += blendedStarHist[0]
    unmatBar = sp1.bar(barbins,
                       undetectedStarHist[0],
                       width=width,
                       color='b',
                       alpha=0.5,
                       label='Unmatched',
                       bottom=bottom,
                       capsize=1)

    ymax = num.max(orphanHist[0] + matchedStarHist[0] + blendedStarHist[0] +
                   undetectedStarHist[0])
    sp1.set_ylim([0, 1.4 * ymax])

    sp1x2 = sp1.twinx()
    allStars = num.concatenate((matchedStar, blendedStar, undetectedStar))
    foundStars = num.concatenate((matchedStar, blendedStar))
    histAll = num.histogram(allStars, bins=bins)
    histFound = num.histogram(foundStars, bins=bins)

    magbins = 0.5 * (histAll[1][1:] + histAll[1][:-1])
    w = num.where(histAll[0] != 0)
    x = magbins[w]
    n = 1.0 * histFound[0][w]
    d = 1.0 * histAll[0][w]
    y = n / d
    sp1x2.plot(x, y)
    sp1x2.set_ylim([0.0, 1.4])
    sp1x2.set_ylabel('(Match+Blend)/Tot', fontsize=8)
    sp1x2.axhline(y=0.5, c='k', linestyle='-.', alpha=0.75)
    sp1x2.axhline(y=1.0, c='k', linestyle='-.', alpha=0.75)
    sp1x2.axvline(x=depth, c='k', linestyle='-', alpha=0.75)
    sp1x2.text(depth - 1.0,
               1.2,
               "%.2f" % (depth),
               fontsize=8,
               horizontalalignment='right',
               verticalalignment='center')
    fa = patches.FancyArrow(depth - 0.8,
                            1.2,
                            0.8,
                            0.0,
                            length_includes_head=True,
                            overhang=0.2,
                            head_length=0.2,
                            head_width=0.04)
    sp1x2.add_patch(fa)

    qaPlotUtil.qaSetp(sp1x2.get_xticklabels(), visible=False)
    qaPlotUtil.qaSetp(sp1x2.get_yticklabels(), fontsize=6)

    sp1.set_ylabel('N Stars', fontsize=10)
    if False:  #1.4*ymax > 1000:
        sp1.yaxis.set_major_formatter(ticker.FormatStrFormatter("%.2g"))
        for t in sp1.get_yticklabels():
            print t.get_text()
            t.set_text(re.sub("\+0", "", t.get_text()))
    qaPlotUtil.qaSetp(sp1.get_xticklabels() + sp1.get_yticklabels(),
                      fontsize=6)

    ##############

    orphanHist = num.histogram(orphan, bins=bins)
    matchedGalHist = num.histogram(matchedGalaxy, bins=bins)
    blendedGalHist = num.histogram(blendedGalaxy, bins=bins)
    undetectedGalHist = num.histogram(undetectedGalaxy, bins=bins)
    orphanBar = sp2.bar(barbins,
                        orphanHist[0],
                        width=width,
                        color='r',
                        alpha=0.5,
                        label='Orphan',
                        capsize=1,
                        log=False)
    bottom = orphanHist[0]
    matchedBar = sp2.bar(barbins,
                         matchedGalHist[0],
                         width=width,
                         color='g',
                         alpha=0.5,
                         label='Matched',
                         bottom=bottom,
                         capsize=1,
                         log=False)
    bottom += matchedGalHist[0]
    blendedBar = sp2.bar(barbins,
                         blendedGalHist[0],
                         width=width,
                         color='cyan',
                         alpha=0.5,
                         label='Blended',
                         bottom=bottom,
                         capsize=1,
                         log=False)
    bottom += blendedGalHist[0]
    unmatBar = sp2.bar(barbins,
                       undetectedGalHist[0],
                       width=width,
                       color='b',
                       alpha=0.5,
                       label='Unmatched',
                       bottom=bottom,
                       capsize=1,
                       log=False)

    sp2.set_xlabel('Mag', fontsize=10)
    sp2.set_ylabel('N Gals', fontsize=10)
    qaPlotUtil.qaSetp(sp2.get_xticklabels() + sp2.get_yticklabels(),
                      fontsize=6)
    qaPlotUtil.qaSetp(sp2.get_yticklabels(), rotation=45.0)
    sp2.legend(numpoints=1,
               prop=FontProperties(size='x-small'),
               loc='upper left')
    #sp2.set_ylim(0.75, 999)
    #sp2.semilogy()

    sp1.set_xlim(14, 26)

    fig.suptitle('%s Stacked histogram' % (title), fontsize=11)

    return fig
Example #24
0
def main(figname, camera='hsc', output=None):

    # build a camera
    if camera == 'hsc':
        simdir = os.environ['OBS_SUBARU_DIR']
        cameraGeomPaf = os.path.join(simdir, "hsc", "hsc_geom.paf")
        if not os.path.exists(cameraGeomPaf):
            cameraGeomPaf = os.path.join(simdir, "hsc", "description",
                                         "hsc_geom.paf")
            if not os.path.exists(cameraGeomPaf):
                raise Exception("Unable to find cameraGeom Policy file: %s" %
                                (cameraGeomPaf))
        cameraGeomPolicy = camGeomUtils.getGeomPolicy(cameraGeomPaf)
        camera = camGeomUtils.makeCamera(cameraGeomPolicy)

    if not camera:
        raise ValueError("Camera must be ... uhm ... 'hsc'")

    ###########################
    # make RBG arrays with FpaImage objects
    # The FpaImage objects have a method to extract pixels from one CCD
    # So we'll create a dummy image and put the whole user image in it,
    # then we'll copy each CCD into the final image
    ###########################
    bin = 16
    fpa_img = [
        hscUtil.FpaImage(camera, scale=bin),
        hscUtil.FpaImage(camera, scale=bin),
        hscUtil.FpaImage(camera, scale=bin),
    ]
    fpa_dum = [
        hscUtil.FpaImage(camera, scale=bin),
        hscUtil.FpaImage(camera, scale=bin),
        hscUtil.FpaImage(camera, scale=bin),
    ]
    ny, nx = fpa_img[0].image.shape

    ###########################
    # Load the image the user provided.
    # figure out how much to scale things so it fits in our FpaImage
    ###########################
    img0 = PIL.Image.open(figname)
    h, w = img0.size
    if h >= w:
        scale = 1.0 * ny / h
    else:
        scale = 1.0 * nx / w
    img = numpy.array(
        img0.resize((int(scale * h), int(scale * w)), PIL.Image.ANTIALIAS))
    subw = int(scale * w)
    subh = int(scale * h)
    x0 = int(0.5 * (nx - subw))
    y0 = int(0.5 * (ny - subh))

    fpa_dum[0].image += 100
    for i in 0, 1, 2:
        fpa_dum[i].image[y0:y0 + subh, x0:x0 + subw] = img[:, :, i]

    ###########################
    # Now copy each CCD from the dummy to the original
    ###########################
    for i in 0, 1, 2:  #, 1, 2:
        for i_ccd in range(104):
            img2 = fpa_dum[i].getPixels(i_ccd)
            print i_ccd, img2.shape
            fpa_img[i].insert(i_ccd, img2)

    ###########################
    # convert this to an RBG image
    # we don't need to worry about uint8 with range 256, matplotlib will handle normalization.
    ###########################
    color_img = numpy.ones((ny, nx, 3), dtype=numpy.uint8)
    for i in range(3):
        color_img[:, :, i] = fpa_img[i].image

    ###########################
    # plot it
    ###########################
    fig = figure.Figure()
    canvas = FigCanvas(fig)
    ax = fig.add_subplot(111)
    ax.imshow(color_img)
    ax.get_xaxis().set_ticks([])
    ax.get_yaxis().set_ticks([])
    fig.savefig(output)