def main(): import optparse parser = optparse.OptionParser(usage='%prog [options] <NGC-number>') parser.add_option('--threads', dest='threads', type=int, help='use multiprocessing') parser.add_option('--itune1',dest='itune1',type=int,help='Individual tuning, first stage',default=5) parser.add_option('--itune2',dest='itune2',type=int,help='Individual tuning, second stage',default=5) parser.add_option('--ntune',dest='ntune',type=int,help='All objects tuning',default=0) parser.add_option('--nocache',dest='nocache',action='store_true',default=False,help='Disable caching for memory reasons') opt,args = parser.parse_args() if len(args) != 1: parser.print_help() sys.exit(-1) if opt.threads: mp = multiproc(nthreads=opt.threads) else: mp = multiproc() ngc = int(args[0]) j = getNGC(ngc) print j ra = float(j['RA'][0]) dec = float(j['DEC'][0]) itune1 = opt.itune1 itune2 = opt.itune2 ntune = opt.ntune IRLS_scale = 25. radius = (10.**j['LOG_D25'][0])/10. dr8 = True noarcsinh = False print 'Radius', radius print 'RA,Dec', ra, dec sras, sdecs, smags = tychoMatch(ra,dec,(radius*1.5)/60.) for sra,sdec,smag in zip(sras,sdecs,smags): print sra,sdec,smag rcfs = radec_to_sdss_rcf(ra,dec,radius=math.hypot(radius,13./2.),tablefn="dr8fields.fits") print rcfs #fieldPlot(ra,dec,radius,ngc) imkw = dict(psf='dg') if dr8: getim = st.get_tractor_image_dr8 getsrc = st.get_tractor_sources_dr8 imkw.update(zrange=[-3,100]) else: getim = st.get_tractor_image getsrc = st.get_tractor_sources_dr8 imkw.update(useMags=True) bands=['u','g','r','i','z'] #bands=['r'] bandname = 'r' flipBands = ['r'] imsrcs = mp.map(get_ims_and_srcs, [(rcf + (bands, ra, dec, radius*60./0.396, imkw, getim, getsrc)) for rcf in rcfs]) timgs = [] sources = [] allsources = [] for ims,s in imsrcs: if ims is None: continue if s is None: continue timgs.extend(ims) allsources.extend(s) sources.append(s) #rds = [rcf[3:5] for rcf in rcfs] plotarea(ra, dec, radius, ngc, timgs) #, rds) lvl = logging.DEBUG logging.basicConfig(level=lvl,format='%(message)s',stream=sys.stdout) tractor = st.Tractor(timgs, allsources, mp=mp) sa = dict(debug=True, plotAll=False,plotBands=False) if noarcsinh: sa.update(nlscale=0) elif dr8: sa.update(chilo=-8.,chihi=8.) if opt.nocache: tractor.cache = NullCache() sg.disable_galaxy_cache() zr = timgs[0].zr print "zr is: ",zr print bands print "Number of images: ", len(timgs) # for timg,band in zip(timgs,bands): # data = timg.getImage()/np.sqrt(timg.getInvvar()) # plt.hist(data,bins=100) # plt.savefig('hist-%s.png' % (band)) prefix = 'ngc%d' % (ngc) saveAll('initial-'+prefix, tractor,**sa) #plotInvvar('initial-'+prefix,tractor) for sra,sdec,smag in zip(sras,sdecs,smags): print sra,sdec,smag for img in tractor.getImages(): wcs = img.getWcs() starx,stary = wcs.positionToPixel(RaDecPos(sra,sdec)) starr=25*(2**(max(11-smag,0.))) if starx+starr<0. or starx-starr>img.getWidth() or stary+starr <0. or stary-starr>img.getHeight(): continue X,Y = np.meshgrid(np.arange(img.getWidth()), np.arange(img.getHeight())) R2 = (X - starx)**2 + (Y - stary)**2 img.getStarMask()[R2 < starr**2] = 0 #star = [(x,y) for x in range(img.getWidth()) for y in range(img.getHeight()) if (x-starx)**2+(y-stary)**2 <= starr**2] #for (x,y) in star: # img.getStarMask()[y][x] = 0 for timgs,sources in imsrcs: timg = timgs[0] wcs = timg.getWcs() xtr,ytr = wcs.positionToPixel(RaDecPos(ra,dec)) print xtr,ytr xt = xtr yt = ytr r = ((radius*60.))/.396 #radius in pixels for src in sources: xs,ys = wcs.positionToPixel(src.getPosition(),src) if (xs-xt)**2+(ys-yt)**2 <= r**2: print "Removed:", src print xs,ys tractor.removeSource(src) #saveAll('removed-'+prefix, tractor,**sa) newShape = sg.GalaxyShape(30.,1.,0.) newBright = ba.Mags(r=15.0,g=15.0,u=15.0,z=15.0,i=15.0,order=['u','g','r','i','z']) EG = st.ExpGalaxy(RaDecPos(ra,dec),newBright,newShape) print EG tractor.addSource(EG) saveAll('added-'+prefix,tractor,**sa) print 'Tractor has', tractor.getParamNames() for im in tractor.images: im.freezeAllParams() im.thawParam('sky') tractor.catalog.freezeAllBut(EG) print 'Tractor has', tractor.getParamNames() print 'values', tractor.getParams() for i in range(itune1): tractor.optimize() print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] tractor.changeInvvar(IRLS_scale) print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] saveAll('itune1-%d-' % (i+1)+prefix,tractor,**sa) print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] tractor.clearCache() sg.get_galaxy_cache().clear() gc.collect() print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] CGPos = EG.getPosition() CGShape1 = EG.getShape().copy() CGShape2 = EG.getShape().copy() EGBright = EG.getBrightness() CGu = EGBright[0] + 0.75 CGg = EGBright[1] + 0.75 CGr = EGBright[2] + 0.75 CGi = EGBright[3] + 0.75 CGz = EGBright[4] + 0.75 CGBright1 = ba.Mags(r=CGr,g=CGg,u=CGu,z=CGz,i=CGi,order=['u','g','r','i','z']) CGBright2 = ba.Mags(r=CGr,g=CGg,u=CGu,z=CGz,i=CGi,order=['u','g','r','i','z']) print EGBright print CGBright1 CG = st.CompositeGalaxy(CGPos,CGBright1,CGShape1,CGBright2,CGShape2) tractor.removeSource(EG) tractor.addSource(CG) tractor.catalog.freezeAllBut(CG) print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] for i in range(itune2): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('itune2-%d-' % (i+1)+prefix,tractor,**sa) print resource.getpagesize() print "RUsage is: ",resource.getrusage(resource.RUSAGE_SELF)[2] tractor.clearCache() sg.get_galaxy_cache().clear() print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] tractor.catalog.thawAllParams() for i in range(ntune): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('ntune-%d-' % (i+1)+prefix,tractor,**sa) #plotInvvar('final-'+prefix,tractor) sa.update(plotBands=True) saveAll('allBands-' + prefix,tractor,**sa) print CG print CG.getPosition() print CGBright1 print CGBright2 print CGShape1 print CGShape2 print CGBright1+CGBright2 print CG.getBrightness() pfn = 'ngc-%d.pickle' % ngc pickle_to_file(CG,pfn) makeflipbook(prefix,len(tractor.getImages()),itune1,itune2,ntune)
def main(): # In LSST meas-deblend (on lsst6): # python examples/suprimePlot.py --data ~dstn/lsst/ACT-data -v 126969 -c 5 --data-range -100 300 --roi 0 500 0 500 --psf psf.fits --image img.fits --sources srcs.fits from optparse import OptionParser import sys parser = OptionParser(usage=('%prog <img> <psf> <srcs>')) parser.add_option('-v', '--verbose', dest='verbose', action='count', default=0, help='Make more verbose') opt, args = parser.parse_args() if len(args) != 3: parser.print_help() sys.exit(-1) if opt.verbose == 0: lvl = logging.INFO else: lvl = logging.DEBUG logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) imgfn, psffn, srcfn = args pimg = pyfits.open(imgfn) if len(pimg) != 4: print('Image must have 3 extensions') sys.exit(-1) img = pimg[1].data mask = pimg[2].data maskhdr = pimg[2].header var = pimg[3].data del pimg print('var', var.shape) #print var print('mask', mask.shape) #print mask print('img', img.shape) #print img mask = mask.astype(np.int16) for bit in range(16): on = ((mask & (1 << bit)) != 0) print('Bit', bit, 'has', np.sum(on), 'pixels set') ''' MP_BAD = 0 Bit 0 has 2500 pixels set MP_SAT = 1 Bit 1 has 5771 pixels set MP_INTRP= 2 Bit 2 has 11269 pixels set MP_CR = 3 Bit 3 has 136 pixels set MP_EDGE = 4 Bit 4 has 11856 pixels set HIERARCH MP_DETECTED = 5 Bit 5 has 37032 pixels set ''' print('Mask header:', maskhdr) maskplanes = {} print('Mask planes:') for card in maskhdr.ascardlist(): if not card.key.startswith('MP_'): continue print(card.value, card.key) maskplanes[card.key[3:]] = card.value print('Variance range:', var.min(), var.max()) print('Image median:', np.median(img.ravel())) invvar = 1. / var invvar[var == 0] = 0. invvar[var < 0] = 0. sig = np.sqrt(np.median(var)) H, W = img.shape for k, v in maskplanes.items(): plt.clf() I = ((mask & (1 << v)) != 0) rgb = np.zeros((H, W, 3)) clipimg = np.clip((img - (-3. * sig)) / (13. * sig), 0, 1) cimg = clipimg.copy() cimg[I] = 1 rgb[:, :, 0] = cimg cimg = clipimg.copy() cimg[I] = 0 rgb[:, :, 1] = cimg rgb[:, :, 2] = cimg plt.imshow(rgb, interpolation='nearest', origin='lower') plt.title(k) plt.savefig('mask-%s.png' % k.lower()) badmask = sum([(1 << maskplanes[k]) for k in ['BAD', 'SAT', 'INTRP', 'CR']]) # HACK -- left EDGE sucks badmask += (1 << maskplanes['EDGE']) #badmask = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) #badmask |= (1 << 4) print('Masking out: 0x%x' % badmask) invvar[(mask & badmask) != 0] = 0. assert (all(np.isfinite(img.ravel()))) assert (all(np.isfinite(invvar.ravel()))) psf = pyfits.open(psffn)[0].data print('psf', psf.shape) psf /= psf.sum() from tractor.emfit import em_fit_2d from tractor.fitpsf import em_init_params # Create Gaussian mixture model PSF approximation. S = psf.shape[0] # number of Gaussian components K = 3 w, mu, sig = em_init_params(K, None, None, None) II = psf.copy() II /= II.sum() # HIDEOUS HACK II = np.maximum(II, 0) print('Multi-Gaussian PSF fit...') xm, ym = -(S / 2), -(S / 2) em_fit_2d(II, xm, ym, w, mu, sig) print('w,mu,sig', w, mu, sig) mypsf = tractor.GaussianMixturePSF(w, mu, sig) P = mypsf.getPointSourcePatch(S / 2, S / 2) mn, mx = psf.min(), psf.max() ima = dict(interpolation='nearest', origin='lower', vmin=mn, vmax=mx) plt.clf() plt.subplot(1, 2, 1) plt.imshow(psf, **ima) plt.subplot(1, 2, 2) pimg = np.zeros_like(psf) P.addTo(pimg) plt.imshow(pimg, **ima) plt.savefig('psf.png') sig = np.sqrt(np.median(var)) plt.clf() plt.hist(img.ravel(), 100, range=(-3. * sig, 3. * sig)) plt.savefig('imghist.png') srcs = fits_table(srcfn) print('Initial:', len(srcs), 'sources') # Trim sources with x=0 or y=0 srcs = srcs[(srcs.x != 0) * (srcs.y != 0)] print('Trim on x,y:', len(srcs), 'sources left') # Zero out nans & infs for c in ['theta', 'a', 'b']: I = np.logical_not(np.isfinite(srcs.get(c))) srcs.get(c)[I] = 0. # Set sources with flux=NaN to something more sensible... I = np.logical_not(np.isfinite(srcs.flux)) srcs.flux[I] = 1. # Sort sources by flux. srcs = srcs[np.argsort(-srcs.flux)] # Trim sources that are way outside the image. margin = 8. * np.maximum(srcs.a, srcs.b) H, W = img.shape srcs = srcs[(srcs.x > -margin) * (srcs.y > -margin) * (srcs.x < (W + margin) * (srcs.y < (H + margin)))] print('Trim out-of-bounds:', len(srcs), 'sources left') wcs = tractor.FitsWcs(Sip(imgfn, 1)) #wcs = tractor.NullWCS() timg = tractor.Image(data=img, invvar=invvar, psf=mypsf, wcs=wcs, sky=tractor.ConstantSky(0.), photocal=tractor.NullPhotoCal(), name='image') inverr = timg.getInvError() assert (all(np.isfinite(inverr.ravel()))) tsrcs = [] for s in srcs: #pos = tractor.PixPos(s.x, s.y) pos = tractor.RaDecPos(s.ra, s.dec) if s.a > 0 and s.b > 0: eflux = tractor.Flux(s.flux / 2.) dflux = tractor.Flux(s.flux / 2.) re, ab, phi = s.a, s.b / s.a, 90. - s.theta eshape = gal.GalaxyShape(re, ab, phi) dshape = gal.GalaxyShape(re, ab, phi) print('Fluxes', eflux, dflux) tsrc = gal.CompositeGalaxy(pos, eflux, eshape, dflux, dshape) else: flux = tractor.Flux(s.flux) print('Flux', flux) tsrc = tractor.PointSource(pos, flux) tsrcs.append(tsrc) chug = tractor.Tractor([timg]) for src in tsrcs: if chug.getModelPatch(timg, src) is None: print('Dropping non-overlapping source:', src) continue chug.addSource(src) print('Kept a total of', len(chug.catalog), 'sources') ima = dict(interpolation='nearest', origin='lower', vmin=-3. * sig, vmax=10. * sig) chia = dict(interpolation='nearest', origin='lower', vmin=-5., vmax=5.) plt.clf() plt.imshow(img, **ima) plt.colorbar() plt.savefig('img.png') plt.clf() plt.imshow(invvar, interpolation='nearest', origin='lower') plt.colorbar() plt.savefig('invvar.png') mod = chug.getModelImages()[0] plt.clf() plt.imshow(mod, **ima) plt.colorbar() plt.savefig('mod-0.png') chi = chug.getChiImage(0) plt.clf() plt.imshow(chi, **chia) plt.colorbar() plt.savefig('chi-0.png') for step in range(5): cat = chug.getCatalog() for src in cat: if chug.getModelPatch(timg, src) is None: print('Dropping non-overlapping source:', src) chug.removeSource(src) print('Kept a total of', len(chug.catalog), 'sources') #cat = chug.getCatalog() #for i,src in enumerate([]): #for i,src in enumerate(chug.getCatalog()): #for i in range(len(cat)): i = 0 while i < len(cat): src = cat[i] #print 'Step', i #for j,s in enumerate(cat): # x,y = timg.getWcs().positionToPixel(s, s.getPosition()) # print ' ', # if j == i: # print '*', # print '(%6.1f, %6.1f)'%(x,y), s print('Optimizing source', i, 'of', len(cat)) x, y = timg.getWcs().positionToPixel(src.getPosition(), src) print('(%6.1f, %6.1f)' % (x, y), src) # pre = src.getModelPatch(timg) s1 = str(src) print('src1 ', s1) dlnp1, X, a = chug.optimizeCatalogFluxes(srcs=[src]) s2 = str(src) dlnp2, X, a = chug.optimizeCatalogAtFixedComplexityStep(srcs=[src], sky=False) s3 = str(src) #post = src.getModelPatch(timg) print('src1 ', s1) print('src2 ', s2) print('src3 ', s3) print('dlnp', dlnp1, dlnp2) if chug.getModelPatch(timg, src) is None: print('After optimizing, no overlap!') print('Removing source', src) chug.removeSource(src) i -= 1 i += 1 # plt.clf() # plt.subplot(2,2,1) # img = timg.getImage() # (x0,x1,y0,y1) = pre.getExtent() # plt.imshow(img, **ima) # ax = plt.axis() # plt.plot([x0,x0,x1,x1,x0], [y0,y1,y1,y0,y0], 'k-', lw=2) # plt.axis(ax) # plt.subplot(2,2,3) # plt.imshow(pre.getImage(), **ima) # plt.subplot(2,2,4) # plt.imshow(post.getImage(), **ima) # plt.savefig('prepost-s%i-s%03i.png' % (step, i)) # # mod = chug.getModelImages()[0] # plt.clf() # plt.imshow(mod, **ima) # plt.colorbar() # plt.savefig('mod-s%i-s%03i.png' % (step, i)) # chi = chug.getChiImage(0) # plt.clf() # plt.imshow(chi, **chia) # plt.colorbar() # plt.savefig('chi-s%i-s%03i.png' % (step, i)) #dlnp,x,a = chug.optimizeCatalogFluxes() #print 'fluxes: dlnp', dlnp #dlnp,x,a = chug.optimizeCatalogAtFixedComplexityStep() #print 'opt: dlnp', dlnp mod = chug.getModelImages()[0] plt.clf() plt.imshow(mod, **ima) plt.colorbar() plt.savefig('mod-%i.png' % (step + 1)) chi = chug.getChiImage(0) plt.clf() plt.imshow(chi, **chia) plt.colorbar() plt.savefig('chi-%i.png' % (step + 1)) return for step in range(5): chug.optimizeCatalogFluxes() mod = chug.getModelImages()[0] plt.clf() plt.imshow(mod, **ima) plt.colorbar() plt.savefig('mod-s%i.png' % step) chi = chug.getChiImage(0) plt.clf() plt.imshow(chi, **chia) plt.colorbar() plt.savefig('chi-s%i.png' % step)
def main(): ra = 126.925 dec = 21.4833 itune1 = 5 itune2 = 5 ntune = 2 run = [4517, 4576, 4576] field = [103, 99, 100] camcol = [2, 6, 6] bands = ['r'] bandname = 'r' flipBands = ['r'] rerun = 0 TI = [] sources = [] table = pyfits.open("J082742.02+212844.7-r.fits") table.info() header = table[0].header data = table[0].data invvar = table[1].data skyobj = ba.ConstantSky(header['skyval']) psffn = 'J082742.02+212844.7-r-bpsf.fits.gz' psfimg = pyfits.open(psffn)[0].data print('PSF image shape', psfimg.shape) # number of Gaussian components PS = psfimg.shape[0] K = 3 w, mu, sig = em_init_params(K, None, None, None) II = psfimg.copy() II /= II.sum() # HACK II = np.maximum(II, 0) print('Multi-Gaussian PSF fit...') xm, ym = -(PS / 2), -(PS / 2) em_fit_2d(II, xm, ym, w, mu, sig) print('w,mu,sig', w, mu, sig) psf = GaussianMixturePSF(w, mu, sig) sources = [] # for run,camcol,field in zip(run,camcol,field): # sources.append(st.get_tractor_sources(run,camcol,field,bandname,bands=bands)) wcs = Tan("J082742.02+212844.7-r.fits", 0) wcs = FitsWcs(wcs) wcs.setX0Y0(1., 1.) photocal = ba.NasaSloanPhotoCal(bandname) #Also probably not right TI.append( en.Image(data=data, invvar=invvar, sky=skyobj, psf=psf, wcs=wcs, photocal=photocal, name="NASA-Sloan Test")) lvl = logging.DEBUG logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) tims = [TI[0]] tractor = st.SDSSTractor(tims) for source in sources: tractor.addSources(source) zr = np.array([-5., +5.]) # * info['skysig'] print(bands) prefix = 'ngc2595' # saveAll('initial-'+prefix, tractor,zr,flipBands,debug=True) bright = None lowbright = 1000 sources = [] for timg, sources in zip(tims, sources): wcs = timg.getWcs() xtr, ytr = wcs.positionToPixel(RaDecPos(ra, dec)) print(xtr, ytr) xt = xtr yt = ytr r = 250. for src in sources: xs, ys = wcs.positionToPixel(src.getPosition(), src) if (xs - xt)**2 + (ys - yt)**2 <= r**2: print("Removed:", src) print(xs, ys) tractor.removeSource(src) # saveAll('removed-'+prefix, tractor,zr,flipBands,debug=True) newShape = sg.GalaxyShape(30., 1., 0.) newBright = ba.Mags(r=15.0, g=15.0, u=15.0, z=15.0, i=15.0) EG = st.ExpGalaxy(RaDecPos(ra, dec), newBright, newShape) print(EG) tractor.addSource(EG) saveAll('added-' + prefix, tractor, zr, flipBands, debug=True) plotInvvar('added-' + prefix, tractor) for i in range(itune1): if (i % 5 == 0): tractor.optimizeCatalogLoop(nsteps=1, srcs=[EG], sky=True) else: tractor.optimizeCatalogLoop(nsteps=1, srcs=[EG], sky=False) tractor.changeInvvar(9.) tractor.clearCache() saveAll('itune1-%d-' % (i + 1) + prefix, tractor, zr, flipBands, debug=True) plotInvvar('itune1-%d-' % (i + 1) + prefix, tractor) CGPos = EG.getPosition() CGShape = EG.getShape() EGBright = EG.getBrightness() print(EGBright) CGg = EGBright[0] * 1.25 CGi = EGBright[1] * 1.25 CGr = EGBright[2] * 1.25 CGu = EGBright[3] * 1.25 CGz = EGBright[4] * 1.25 CGBright = ba.Mags(r=CGr, g=CGg, u=CGu, z=CGz, i=CGi) print(EGBright) print(CGBright) CG = st.CompositeGalaxy(CGPos, CGBright, CGShape, CGBright, CGShape) tractor.removeSource(EG) tractor.addSource(CG) for i in range(itune2): if (i % 5 == 0): tractor.optimizeCatalogLoop(nsteps=1, srcs=[CG], sky=True) else: tractor.optimizeCatalogLoop(nsteps=1, srcs=[CG], sky=False) tractor.changeInvvar(9.) tractor.clearCache() saveAll('itune2-%d-' % (i + 1) + prefix, tractor, zr, flipBands, debug=True) plotInvvar('itune2-%d-' % (i + 1) + prefix, tractor) for i in range(ntune): tractor.optimizeCatalogLoop(nsteps=1, sky=True) saveAll('ntune-%d-' % (i + 1) + prefix, tractor, zr, flipBands, debug=True) tractor.clearCache() makeflipbook(prefix, len(tractor.getImages()), itune1, itune2, ntune)
def twogalaxies(name1, ra1, dec1, name2, ra2, dec2): name = "%s %s" % (name1, name2) ra = float(ra1) dec = float(dec1) ra2 = float(ra2) dec2 = float(dec2) remradius = 6. fieldradius = 6. threads = None itune1 = 5 itune2 = 5 ntune = 0 nocache = True #Radius should be in arcminutes if threads: mp = multiproc(nthreads=threads) else: mp = multiproc() IRLS_scale = 25. dr9 = True dr8 = False noarcsinh = False print(name) prefix = '%s' % (name.replace(' ', '_')) prefix1 = '%s' % (name1.replace(' ', '_')) prefix2 = '%s' % (name2.replace(' ', '_')) print('Removal Radius', remradius) print('Field Radius', fieldradius) print('RA,Dec', ra, dec) print(os.getcwd()) rcfs = radec_to_sdss_rcf(ra, dec, radius=math.hypot(fieldradius, 13. / 2.), tablefn="dr9fields.fits") print(rcfs) assert (len(rcfs) > 0) assert (len(rcfs) < 15) sras, sdecs, smags = tychoMatch(ra, dec, (fieldradius * 1.5) / 60.) for sra, sdec, smag in zip(sras, sdecs, smags): print(sra, sdec, smag) imkw = dict(psf='dg') if dr9: getim = st.get_tractor_image_dr9 getsrc = st.get_tractor_sources_dr9 imkw.update(zrange=[-3, 100]) elif dr8: getim = st.get_tractor_image_dr8 getsrc = st.get_tractor_sources_dr8 imkw.update(zrange=[-3, 100]) else: getim = st.get_tractor_image getsrc = st.get_tractor_sources_dr8 imkw.update(useMags=True) bands = ['u', 'g', 'r', 'i', 'z'] bandname = 'r' flipBands = ['r'] print(rcfs) imsrcs = mp.map( get_ims_and_srcs, [(rcf + (bands, ra, dec, fieldradius * 60. / 0.396, imkw, getim, getsrc)) for rcf in rcfs]) timgs = [] sources = [] allsources = [] for ims, s in imsrcs: if ims is None: continue if s is None: continue timgs.extend(ims) allsources.extend(s) sources.append(s) #rds = [rcf[3:5] for rcf in rcfs] plotarea(ra, dec, fieldradius, name, prefix, timgs) #, rds) lvl = logging.DEBUG logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) tractor = st.Tractor(timgs, allsources, mp=mp) sa = dict(debug=True, plotAll=False, plotBands=False) if noarcsinh: sa.update(nlscale=0) elif dr8 or dr9: sa.update(chilo=-8., chihi=8.) if nocache: tractor.cache = NullCache() sg.disable_galaxy_cache() zr = timgs[0].zr print("zr is: ", zr) print(bands) print("Number of images: ", len(timgs)) # for timg,band in zip(timgs,bands): # data = timg.getImage()/np.sqrt(timg.getInvvar()) # plt.hist(data,bins=100) # plt.savefig('hist-%s.png' % (band)) saveAll('initial-' + prefix, tractor, **sa) #plotInvvar('initial-'+prefix,tractor) for sra, sdec, smag in zip(sras, sdecs, smags): print(sra, sdec, smag) for img in tractor.getImages(): wcs = img.getWcs() starx, stary = wcs.positionToPixel(RaDecPos(sra, sdec)) starr = 25 * (2**(max(11 - smag, 0.))) if starx + starr < 0. or starx - starr > img.getWidth( ) or stary + starr < 0. or stary - starr > img.getHeight(): continue X, Y = np.meshgrid(np.arange(img.getWidth()), np.arange(img.getHeight())) R2 = (X - starx)**2 + (Y - stary)**2 img.getStarMask()[R2 < starr**2] = 0 for timgs, sources in imsrcs: timg = timgs[0] wcs = timg.getWcs() xtr, ytr = wcs.positionToPixel(RaDecPos(ra, dec)) print(xtr, ytr) xt = xtr yt = ytr r = ((remradius * 60.)) / .396 #radius in pixels for src in sources: xs, ys = wcs.positionToPixel(src.getPosition(), src) if (xs - xt)**2 + (ys - yt)**2 <= r**2: #print "Removed:", src #print xs,ys tractor.removeSource(src) #saveAll('removed-'+prefix, tractor,**sa) newShape = sg.GalaxyShape((remradius * 60.) / 10., 1., 0.) newBright = ba.Mags(r=15.0, g=15.0, u=15.0, z=15.0, i=15.0, order=['u', 'g', 'r', 'i', 'z']) EG = st.ExpGalaxy(RaDecPos(ra, dec), newBright, newShape) newShape2 = sg.GalaxyShape((remradius * 60.) / 10., 1., 0.) newBright2 = ba.Mags(r=15.0, g=15.0, u=15.0, z=15.0, i=15.0, order=['u', 'g', 'r', 'i', 'z']) EG2 = st.ExpGalaxy(RaDecPos(ra2, dec2), newBright2, newShape2) print(EG) print(EG2) tractor.addSource(EG) tractor.addSource(EG2) saveAll('added-' + prefix, tractor, **sa) #print 'Tractor has', tractor.getParamNames() for im in tractor.images: im.freezeAllParams() im.thawParam('sky') tractor.catalog.freezeAllBut(EG) tractor.catalog.thawParams(EG2) #print 'Tractor has', tractor.getParamNames() #print 'values', tractor.getParams() for i in range(itune1): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('itune1-%d-' % (i + 1) + prefix, tractor, **sa) tractor.clearCache() sg.get_galaxy_cache().clear() gc.collect() print(resource.getpagesize()) print(resource.getrusage(resource.RUSAGE_SELF)[2]) CGPos = EG.getPosition() CGShape1 = EG.getShape().copy() CGShape2 = EG.getShape().copy() EGBright = EG.getBrightness() CGu = EGBright[0] + 0.75 CGg = EGBright[1] + 0.75 CGr = EGBright[2] + 0.75 CGi = EGBright[3] + 0.75 CGz = EGBright[4] + 0.75 CGBright1 = ba.Mags(r=CGr, g=CGg, u=CGu, z=CGz, i=CGi, order=['u', 'g', 'r', 'i', 'z']) CGBright2 = ba.Mags(r=CGr, g=CGg, u=CGu, z=CGz, i=CGi, order=['u', 'g', 'r', 'i', 'z']) print(EGBright) print(CGBright1) CG = st.CompositeGalaxy(CGPos, CGBright1, CGShape1, CGBright2, CGShape2) CG2Pos = EG2.getPosition() CG2Shape1 = EG2.getShape().copy() CG2Shape2 = EG2.getShape().copy() EG2Bright = EG2.getBrightness() CG2u = EG2Bright[0] + 0.75 CG2g = EG2Bright[1] + 0.75 CG2r = EG2Bright[2] + 0.75 CG2i = EG2Bright[3] + 0.75 CG2z = EG2Bright[4] + 0.75 CG2Bright1 = ba.Mags(r=CG2r, g=CG2g, u=CG2u, z=CG2z, i=CG2i, order=['u', 'g', 'r', 'i', 'z']) CG2Bright2 = ba.Mags(r=CG2r, g=CG2g, u=CG2u, z=CG2z, i=CG2i, order=['u', 'g', 'r', 'i', 'z']) CG2 = st.CompositeGalaxy(CG2Pos, CG2Bright1, CG2Shape1, CG2Bright2, CG2Shape2) tractor.removeSource(EG) tractor.removeSource(EG2) tractor.addSource(CG) tractor.addSource(CG2) tractor.catalog.freezeAllBut(CG) tractor.catalog.thawParams(CG2) print(resource.getpagesize()) print(resource.getrusage(resource.RUSAGE_SELF)[2]) for i in range(itune2): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('itune2-%d-' % (i + 1) + prefix, tractor, **sa) tractor.clearCache() sg.get_galaxy_cache().clear() print(resource.getpagesize()) print(resource.getrusage(resource.RUSAGE_SELF)[2]) tractor.catalog.thawAllParams() for i in range(ntune): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('ntune-%d-' % (i + 1) + prefix, tractor, **sa) #plotInvvar('final-'+prefix,tractor) sa.update(plotBands=True) saveAll('allBands-' + prefix, tractor, **sa) print(CG) print(CG.getPosition()) print(CGBright1) print(CGBright2) print(CGShape1) print(CGShape2) print(CGBright1 + CGBright2) print(CG.getBrightness()) pfn = '%s.pickle' % prefix pickle_to_file([CG, CG2], pfn) makeflipbook(prefix, len(tractor.getImages()), itune1, itune2, ntune) pickle_to_file(CG, "%s.pickle" % prefix1) pickle_to_file(CG2, "%s.pickle" % prefix2) os.system('cp %s.pickle RC3_Output' % prefix1) os.system('cp %s.pickle RC3_Output' % prefix2)
def general(name, ra, dec, remradius, fieldradius, threads=None, itune1=5, itune2=5, ntune=0, nocache=False, scale=1, ab=1., angle=0.): #Radius should be in arcminutes if threads: mp = multiproc(nthreads=threads) else: mp = multiproc() IRLS_scale = 25. dr9 = True dr8 = False noarcsinh = False print name prefix = 'swapCG_%s' % (name.replace(' ', '_')) print 'Removal Radius', remradius print 'Field Radius', fieldradius print 'RA,Dec', ra, dec print os.getcwd() print ra, dec, math.hypot(fieldradius, 13. / 2.) rcfs = radec_to_sdss_rcf(ra, dec, radius=math.hypot(fieldradius, 13. / 2.), tablefn="dr9fields.fits") print 'RCFS:', rcfs print len(rcfs) assert (len(rcfs) > 0) if 10 <= len(rcfs) < 20: scale = 2 elif 20 <= len(rcfs) < 40: scale = 4 elif 40 <= len(rcfs) < 80: scale = 8 assert (len(rcfs) < 80) sras, sdecs, smags = tychoMatch(ra, dec, (fieldradius * 1.5) / 60.) imkw = dict(psf='kl-gm') if dr9: getim = st.get_tractor_image_dr9 getsrc = st.get_tractor_sources_dr9 imkw.update(zrange=[-3, 100]) elif dr8: getim = st.get_tractor_image_dr8 getsrc = st.get_tractor_sources_dr8 imkw.update(zrange=[-3, 100]) else: getim = st.get_tractor_image getsrc = st.get_tractor_sources_dr8 imkw.update(useMags=True) bands = ['u', 'g', 'r', 'i', 'z'] #bands=['r'] bandname = 'r' flipBands = ['r'] print rcfs imsrcs = mp.map( get_ims_and_srcs, [(rcf + (bands, ra, dec, fieldradius * 60. / 0.396, imkw, getim, getsrc)) for rcf in rcfs]) timgs = [] sources = [] allsources = [] for ims, s in imsrcs: if ims is None: continue if s is None: continue if scale > 1: for im in ims: timgs.append(st.scale_sdss_image(im, scale)) else: timgs.extend(ims) allsources.extend(s) sources.append(s) #rds = [rcf[3:5] for rcf in rcfs] #plotarea(ra, dec, fieldradius, name, prefix, timgs) #, rds) #lvl = logging.DEBUG #logging.basicConfig(level=lvl,format='%(message)s',stream=sys.stdout) tractor = st.Tractor(timgs, allsources, mp=mp) sa = dict(debug=True, plotAll=False, plotBands=False) if noarcsinh: sa.update(nlscale=0) elif dr8 or dr9: sa.update(chilo=-8., chihi=8.) if nocache: tractor.cache = NullCache() sg.disable_galaxy_cache() zr = timgs[0].zr print "zr is: ", zr print bands print "Number of images: ", len(timgs) #for timg,band in zip(timgs,bands): # data = timg.getImage()/np.sqrt(timg.getInvvar()) # plt.hist(data,bins=100) # plt.savefig('hist-%s.png' % (band)) saveAll('initial-' + prefix, tractor, **sa) #plotInvvar('initial-'+prefix,tractor) for sra, sdec, smag in zip(sras, sdecs, smags): for img in tractor.getImages(): wcs = img.getWcs() starx, stary = wcs.positionToPixel(RaDecPos(sra, sdec)) starr = 25 * (2**(max(11 - smag, 0.))) if starx + starr < 0. or starx - starr > img.getWidth( ) or stary + starr < 0. or stary - starr > img.getHeight(): continue X, Y = np.meshgrid(np.arange(img.getWidth()), np.arange(img.getHeight())) R2 = (X - starx)**2 + (Y - stary)**2 img.getStarMask()[R2 < starr**2] = 0 for timgs, sources in imsrcs: timg = timgs[0] wcs = timg.getWcs() xtr, ytr = wcs.positionToPixel(RaDecPos(ra, dec)) xt = xtr yt = ytr r = ((remradius * 60.)) / .396 #radius in pixels for src in sources: xs, ys = wcs.positionToPixel(src.getPosition(), src) if (xs - xt)**2 + (ys - yt)**2 <= r**2: #print "Removed:", src #print xs,ys tractor.removeSource(src) #saveAll('removed-'+prefix, tractor,**sa) newShape = sg.GalaxyShape((remradius * 60.) / 10., ab, angle) newBright = ba.Mags(r=15.0, g=15.0, u=15.0, z=15.0, i=15.0, order=['u', 'g', 'r', 'i', 'z']) EG = st.ExpGalaxy(RaDecPos(ra, dec), newBright, newShape) print EG tractor.addSource(EG) saveAll('added-' + prefix, tractor, **sa) #print 'Tractor has', tractor.getParamNames() for im in tractor.images: im.freezeAllParams() im.thawParam('sky') tractor.catalog.freezeAllBut(EG) #print 'Tractor has', tractor.getParamNames() #print 'values', tractor.getParams() for i in range(itune1): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('itune1-%d-' % (i + 1) + prefix, tractor, **sa) tractor.clearCache() sg.get_galaxy_cache().clear() gc.collect() print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] CGPos = EG.getPosition() CGShape1 = EG.getShape().copy() CGShape2 = EG.getShape().copy() EGBright = EG.getBrightness() CGu = EGBright[0] + 0.75 CGg = EGBright[1] + 0.75 CGr = EGBright[2] + 0.75 CGi = EGBright[3] + 0.75 CGz = EGBright[4] + 0.75 CGBright1 = ba.Mags(r=CGr, g=CGg, u=CGu, z=CGz, i=CGi, order=['u', 'g', 'r', 'i', 'z']) CGBright2 = ba.Mags(r=CGr, g=CGg, u=CGu, z=CGz, i=CGi, order=['u', 'g', 'r', 'i', 'z']) print EGBright print CGBright1 CG = st.CompositeGalaxy(CGPos, CGBright1, CGShape1, CGBright2, CGShape2) tractor.removeSource(EG) tractor.addSource(CG) tractor.catalog.freezeAllBut(CG) print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] for i in range(itune2): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('itune2-%d-' % (i + 1) + prefix, tractor, **sa) tractor.clearCache() sg.get_galaxy_cache().clear() print resource.getpagesize() print resource.getrusage(resource.RUSAGE_SELF)[2] tractor.catalog.thawAllParams() for i in range(ntune): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('ntune-%d-' % (i + 1) + prefix, tractor, **sa) #plotInvvar('final-'+prefix,tractor) sa.update(plotBands=True) saveAll('allBands-' + prefix, tractor, **sa) print "end of first round of optimization:", tractor.getLogLikelihood() print CG print CG.getPosition() print CGBright1 print CGBright2 print CGShape1 print CGShape2 print CGBright1 + CGBright2 print CG.getBrightness() pfn = '%s.pickle' % prefix pickle_to_file(CG, pfn) makeflipbook(prefix, len(tractor.getImages()), itune1, itune2, ntune) # now SWAP exp and dev and DO IT AGAIN newCG = st.CompositeGalaxy(CG.getPosition, CG.brightnessDev.copy(), CG.shapeDev.copy(), CG.brightnessExp.copy(), CG.shapeExp.copy()) tractor.removeSource(CG) tractor.addSource(newCG) tractor.catalog.thawAllParams() for i in range(ntune): tractor.optimize() tractor.changeInvvar(IRLS_scale) saveAll('ntune-swap-%d-' % (i + 1) + prefix, tractor, **sa) #plotInvvar('final-'+prefix,tractor) sa.update(plotBands=True) saveAll('allBands-swap-' + prefix, tractor, **sa) print "end of second (swapped) round of optimization:", tractor.getLogLikelihood( ) print newCG print newCG.getPosition() print newCG.getBrightness() pfn = '%s-swap.pickle' % prefix pickle_to_file(newCG, pfn) makeflipbook(prefix + "-swap", len(tractor.getImages()), itune1, itune2, ntune)