示例#1
0
def residual_image(coadd, coadd_col_n, label='all'):
    res = (coadd - coadd_col_n)/(coadd_col_n + 1)  # the constant should fade noise
    for i, band in enumerate(bands):
        pyfits.writeto('stackres_%s_%s.fits'%(label, band), res[i], clobber=True)
    res *= 3.0
    res += 0.5
    colimg = RGBImage(res[0], res[1], res[2], beta=0.0001)
    colimg.save_as('stackres_%s.png'%label)
    colimg = numpy.asarray(colimg.img)
    for i, band in enumerate(bands):
        pyfits.writeto('stackrescol_%s_%s.fits'%(label, band), colimg[:,:,i], clobber=True)
    return res
示例#2
0
def stack_galfit_images(infns, label='all', par='n', debug=False):
    #fns = random.sample(infns, min(len(infns),maxstack))
    fns = infns[:min(len(infns),maxstack)]
    #for j in range(len(fns)):
    #    fns[j] = fns[j].replace('/home/boris/gama/galapagos/galapagos_2.0.3_galfit_0.1.2.1_GAMA_9', '/Users/spb/gala9')
    bandscales = numpy.array([0.06, 0.1, 0.4])/10.0
    beta=2.0
    outsize = size
    n = len(fns)
    if n == 0:
        return None
    if maxstack > 5 and n < 5:
        return None
    print label, len(infns), n
    if n < 5:
        print fns
    nbands = len(bands)
    coadd = []
    modelcoadd = []
    for i, band in enumerate(bands):
        valueoffsets = []
        valuescales = []
        xcentres = []
        ycentres = []
        radii = []
        angles = []
        axisratios = []
        for j, fn in enumerate(fns):
            p = pyfits.open(fn)
            results = p['final_band'].data
            # the following scales to the largest image
            # not suitable when building residual images (need same shape for all stacked images)
            #outsize = max(outsize, max(p[0].shape))
            # index to scale according to the parameters in this band
            bandidx = (results.field('BAND')==band).nonzero()[0][0]
            # index to scale according to r-band parameters
            rbandidx = (results.field('BAND')=='r').nonzero()[0][0]
            valueoffsets.append(-results.field('COMP1_SKY')[bandidx])
            valuescales.append(10**(0.4*(results.field('COMP2_MAG')[rbandidx]-18)))
            xcentres.append(results.field('COMP2_YC')[rbandidx])
            ycentres.append(results.field('COMP2_XC')[rbandidx])
            if par == 're':
                radii.append(results.field('COMP2_RE')[rbandidx])
            elif par == 'n':
                radii.append(results.field('COMP2_RE')[bandidx])
            angles.append(results.field('COMP2_PA')[rbandidx])
            axisratios.append(results.field('COMP2_AR')[rbandidx])
        axisratios = 1.0
        coaddi, stacki = stack_images(fns, outsize, 'input_%s'%band, valueoffsets, valuescales,
                                      xcentres, ycentres, scales, angles, axisratios, clip=3.0)
        coadd.append(coaddi)
        pyfits.writeto('stack_%s_%s.fits'%(label, band), coaddi, clobber=True)
        for si in stacki:
            pyfits.append('stackall_%s_%s.fits'%(label, band), si)
        # stack the models to verify
        coaddi, stacki = stack_images(fns, outsize, 'model_%s'%band, valueoffsets, valuescales,
                                      xcentres, ycentres, scales, angles, axisratios, clip=3.0)
        modelcoadd.append(coaddi)
        pyfits.writeto('stackmodel_%s_%s.fits'%(label, band), coaddi, clobber=True)
        for si in stacki:
            pyfits.append('stackmodelall_%s_%s.fits'%(label, band), si)
    coadd = numpy.array(coadd)
    for i, b in enumerate(bandscales):
        coadd[i] *= b
    colimg = RGBImage(coadd[0], coadd[1], coadd[2], beta=beta)
    colimg.save_as('stack_%s.png'%label)
    coadd = numpy.array(modelcoadd)
    for i, b in enumerate(bandscales):
        coadd[i] *= b
    colimg = RGBImage(coadd[0], coadd[1], coadd[2], beta=beta)
    colimg.save_as('stackmodel_%s.png'%label)
    return coadd
示例#3
0
def plotnonparamcolimg(fns,
                       rgb='Hzg',
                       ids=None,
                       name=None,
                       label=False,
                       desaturate=True,
                       pedestal=0,
                       beta=2.5,
                       scales=np.array([0.04, 0.055, 0.2]),
                       offsets=np.array([75.0, 40.0, 8.0]) * 0.5):
    nbands = len(bands)
    fns = np.atleast_1d(fns)
    nid = len(fns)
    fig = pyplot.figure(figsize=(15.0 / nbands * (1 + nid * 2), 15))
    fig.subplots_adjust(bottom=0.05,
                        top=0.95,
                        left=0.05,
                        right=0.95,
                        hspace=0.0,
                        wspace=0.0)
    original_iid = None
    for i, fn in enumerate(fns):
        if ids is not None:
            iid = ids[i]
        else:
            iid = os.path.splitext(os.path.basename(fn))[0]
        img = fit_images(fn, rgb)
        # Second row, results with nonparam
        img = fit_images(iid, rgb)
        img[0] = [img[0][j] - offsets[j] for j in range(3)]
        img[1] = [img[1][j] - offsets[j] for j in range(3)]
        img[2] = [img[2][j] + scales[j] * 2 * offsets.mean() for j in range(3)]
        ax = fig.add_subplot(nbands, 1 + 2 * nid, 1 + 2 * nid + 2 + i * 2)
        ticksoff(ax)
        colimg = RGBImage(*img[1],
                          scales=scales,
                          beta=beta,
                          desaturate=False,
                          pedestal=pedestal).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
        ax = fig.add_subplot(nbands, 1 + 2 * nid, 1 + 2 * nid + 3 + i * 2)
        ticksoff(ax)
        colimg = RGBImage(*img[2],
                          scales=scales,
                          beta=beta,
                          desaturate=desaturate).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
        # Third row, nonparam diagnostics
        nonparam = nonparam_images(iid, rgb)
        datasub = [img[0][j] - nonparam[j] for j in range(3)]
        nonparam = [nonparam - offsets[j] for j in range(3)]
        datasub = [datasub[j] - offsets[j] for j in range(3)]
        ax = fig.add_subplot(nbands, 1 + 2 * nid, 2 + 4 * nid + 2 + i * 2)
        ticksoff(ax)
        ax.set_xlabel('nonparam %s' % iid, fontsize=labelsize)
        colimg = RGBImage(*nonparam,
                          scales=scales,
                          beta=beta,
                          desaturate=desaturate).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
        ax = fig.add_subplot(nbands, 1 + 2 * nid, 2 + 4 * nid + 3 + i * 2)
        ticksoff(ax)
        ax.set_xlabel('datasub %s' % iid, fontsize=labelsize)
        colimg = RGBImage(*datasub,
                          scales=scales,
                          beta=beta,
                          desaturate=desaturate).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
    fig.savefig('plots/nonparamcolimages_%s.pdf' % name)
    pyplot.close('all')
示例#4
0
def plotcolimg(fns,
               rgb='Hzg',
               ids=None,
               name=None,
               label=False,
               desaturate=True,
               pedestal=0):
    nbands = len(bands)
    fns = np.atleast_1d(fns)
    nid = len(fns)
    beta = 2.5
    scales = np.array((0.04, 0.055, 0.2))
    # offsets not so necessary now have nice desaturation feature working
    offsets = np.array([75.0, 40.0, 8.0]) * 0.5
    rows = 1 + 2 * nid
    fig = pyplot.figure(figsize=(15.0 / nbands * rows, 15))
    fig.subplots_adjust(bottom=0.05,
                        top=0.95,
                        left=0.05,
                        right=0.95,
                        hspace=0.0,
                        wspace=0.0)
    for i, fn in enumerate(fns):
        if ids is not None:
            iid = ids[i]
        else:
            iid = os.path.splitext(os.path.basename(fn))[0]
        img = fit_images(fn, rgb)
        img[0] = [img[0][j] - offsets[j] for j in range(3)]
        img[1] = [img[1][j] - offsets[j] for j in range(3)]
        img[2] = [img[2][j] + scales[j] * 2 * offsets.mean() for j in range(3)]
        if i == 0:
            ax = fig.add_subplot(nbands, rows, 1 + i * 2)
            ticksoff(ax)
            ax.set_xlabel('image', fontsize=labelsize)
            colimg = RGBImage(*img[0],
                              scales=scales,
                              beta=beta,
                              desaturate=desaturate,
                              pedestal=pedestal).img
            pyplot.imshow(colimg, interpolation='nearest', origin='lower')
        ax = fig.add_subplot(nbands, rows, 2 + i * 2)
        ticksoff(ax)
        if label:
            ax.set_xlabel('model {}'.format(iid), fontsize=labelsize)
        colimg = RGBImage(*img[1],
                          scales=scales,
                          beta=beta,
                          desaturate=False,
                          pedestal=pedestal).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
        ax = fig.add_subplot(nbands, rows, 3 + i * 2)
        ticksoff(ax)
        if label:
            ax.set_xlabel('residual {}'.format(iid), fontsize=labelsize)
        colimg = RGBImage(*img[2],
                          scales=scales,
                          beta=beta,
                          desaturate=desaturate).img
        pyplot.imshow(colimg, interpolation='nearest', origin='lower')
    if name is None:
        name = os.path.splitext(os.path.basename(fns[0]))[0]
    fig.savefig('plots/colimages_{}.pdf'.format(name))
    pyplot.close('all')