Beispiel #1
0
        if len(args) == 1:
            dojson = True
        else:
            parser.print_help()
            sys.exit(-1)

    import logging
    logformat = '%(message)s'
    from astrometry.util.util import log_init #, LOG_VERB, LOG_MSG
    loglvl = 2
    if opt.verbose:
        logging.basicConfig(level=logging.DEBUG, format=logformat)
        loglvl += 1
    else:
        logging.basicConfig(level=logging.INFO, format=logformat)
    log_init(loglvl)

    wcsfn = args[0]

    if dojson:
        from astrometry.util.util import anwcs
        wcs = anwcs(wcsfn,0)
        jobjs = get_annotations_for_wcs(wcs, opt)
        import json
        j = json.dumps(jobjs)
        print(j)
        sys.exit(0)

    fmt = PLOTSTUFF_FORMAT_JPG
    s = outfn.split('.')
    if len(s):
Beispiel #2
0
def main():
    import optparse
    import logging
    import sys

    parser = optparse.OptionParser()
    parser.add_option('--threads', dest='threads', default=1, type=int, help='Use this many concurrent processors')
    parser.add_option('-v', '--verbose', dest='verbose', action='count', default=0,
                      help='Make more verbose')

    parser.add_option('--grid', '-g', dest='gridn', type=int, default=5, help='Dust parameter grid size')
    parser.add_option('--steps', '-s', dest='steps', type=int, default=10, help='# Optimization step')
    parser.add_option('--suffix', dest='suffix', default='', help='Output file suffix')

    parser.add_option('--no-100', dest='no100', action='store_true', default=False,
                      help='Omit PACS-100 data?')

    parser.add_option('--callgrind', dest='callgrind', action='store_true', default=False, help='Turn on callgrind around tractor.optimize()')

    parser.add_option('--resume', '-r', dest='resume', type=int, default=-1, help='Resume from a previous run at the given step?')

    parser.add_option('--zoom', dest='zoom', type=float, default=1, help='Scale down the model to only touch the (1/zoom x 1/zoom) central region of the images')

    parser.add_option('--damp', dest='damp', type=float, default=1., help='LSQR damping')

    opt,args = parser.parse_args()

    if opt.verbose == 0:
        lvl = logging.INFO
        log_init(2)
    else:
        lvl = logging.DEBUG
        log_init(3)
    
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

    if opt.threads > 1 and False:
        global dpool
        import debugpool
        dpool = debugpool.DebugPool(opt.threads)
        Time.add_measurement(debugpool.DebugPoolMeas(dpool))
        mp = multiproc(pool=dpool)
    else:
        print('N threads', opt.threads)
        mp = multiproc(opt.threads)#, wrap_all=True)

    if opt.callgrind:
        import callgrind
    else:
        callgrind = None

    np.seterrcall(np_err_handler)
    np.seterr(all='call')
    #np.seterr(all='raise')

    if opt.resume > -1:
        pfn = 'herschel-%02i%s.pickle' % (opt.resume, opt.suffix)
        print('Reading from', pfn)
        tractor = unpickle_from_file(pfn)
        tractor.mp = mp

        ds = tractor.getCatalog()[0]
        print('DustSheet:', ds)

        # derivs = ds.getParamDerivatives(tim)
        # dim = np.zeros(tim.shape)
        # #for k,deriv in enumerate(derivs[:40]):
        # for k,deriv in enumerate(derivs[::10]):
        #   dim[:,:] = 0
        #   deriv.addTo(dim)
        #   plt.clf()
        #   plt.imshow(dim, interpolation='nearest', origin='lower')
        #   plt.savefig('deriv-%04i.png' % k)

        #tim = tractor.getImages()[0]
        # for it,tim in enumerate(tractor.getImages()):
        #   X = ds._getTransformation(tim)
        #   # #print 'X', X
        #   keys = X.keys()
        #   keys.sort()
        #   # for k in keys[::10]:
        #   # for k in keys[:40]:
        #   for k in keys[::202]:
        #       I,G,nil,nil = X[k]
        #       rim = np.zeros_like(tim.getImage())
        #       rim.ravel()[I] = G
        #       plt.clf()
        #       plt.imshow(rim, interpolation='nearest', origin='lower')
        #       plt.colorbar()
        #       plt.savefig('rim-%i-%04i.png' % (it,k))
        #       print 'pix', k
        # sys.exit(0)

        makeplots(tractor, opt.resume, opt.suffix)
        step0 = opt.resume + 1

    else:
        step0 = 0
        tractor = create_tractor(opt)
        tractor.mp = mp

        # zero out invvar outside the model bounds.
        ds = tractor.getCatalog()[0]
        rd = ds.getRaDecCorners(margin=0.5)
        for i,tim in enumerate(tractor.getImages()):
            poly = np.array([tim.getWcs().positionToPixel(
                RaDecPos(rdi[0], rdi[1])) for rdi in rd])
            poly = poly[:-1,:]
            print('Model bounding box in image', tim.name, 'coordinates:')
            #print poly.shape
            print(poly)
            H,W = tim.shape
            xx,yy = np.meshgrid(np.arange(W), np.arange(H))
            inside = point_in_poly(xx, yy, poly)
            tim.inverr[inside == 0] = 0.

        plt.clf()
        for i,tim in enumerate(tractor.images):
            h,w = tim.shape
            rd = [tim.getWcs().pixelToPosition(x,y)
                  for x,y in [(-0.5,-0.5),(w-0.5,-0.5),(w-0.5,h-0.5),
                              (-0.5,h-0.5),(-0.5,-0.5)]]
            plt.plot([p.ra for p in rd], [p.dec for p in rd], '-',
                     label=tim.name)
        rd = ds.getRaDecCorners(margin=0.5)
        plt.plot(rd[:,0], rd[:,1], 'k-', label='Grid')
        mh,mw = ds.shape
        r,d = ds.wcs.pixelxy2radec(1 + np.arange(mw), np.ones(mw))
        plt.plot(r, d, 'k.')
        r,d = ds.wcs.pixelxy2radec(np.ones(mh), 1 + np.arange(mh))
        plt.plot(r, d, 'k.')
        r,d = ds.wcs.pixelxy2radec(1 + np.arange(mw), np.zeros(mw)+mh)
        plt.plot(r, d, 'k.')
        r,d = ds.wcs.pixelxy2radec(np.zeros(mh)+mw, 1 + np.arange(mh))
        plt.plot(r, d, 'k.')
        plt.legend()
        plt.savefig('radec.png')
            
        print('Precomputing transformations...')
        ds = tractor.getCatalog()[0]

        # Split the grid-spread matrix into strips...
        async_results = []
        for im in tractor.getImages():
            args = []
            H,W = ds.shape
            dy = 10
            y = 0
            while y <= H:
                args.append((ds, im, y, min(H, y+dy)))
                y += dy
            async_results.append(mp.map_async(_map_trans, args))
        # Glue to strips back together...
        XX = []
        for ar in async_results:
            Xblocks = ar.get()
            X = Xblocks[0]
            for xi in Xblocks[1:]:
                X.update(xi)
            XX.append(X)
            
        for im,X in zip(tractor.getImages(), XX):
            ds._normalizeTransformation(im, X)
            ds._setTransformation(im, X)
        print('done precomputing.')

        # Plot the grid-spread functions.
        for itim,tim in enumerate(tractor.images):
            T = ds._getTransformation(tim)
            (I,G,nz,NZI) = T[0]
            plt.clf()
            g = np.zeros(tim.shape, np.float32)
            g.flat[I] = G
            plt.imshow(g, interpolation='nearest', origin='lower', cmap='hot')
            plt.colorbar()
            plt.title('Grid-spread function for cell 0, image %s' % tim.name)
            plt.savefig('gsf-%i.png' % itim)

        
        makeplots(tractor, 0, opt.suffix)
        pfn = 'herschel-%02i%s.pickle' % (0, opt.suffix)
        pickle_to_file(tractor, pfn)
        print('Wrote', pfn)

    for im in tractor.getImages():
        im.freezeAllBut('sky')

    for i in range(step0, opt.steps):
        print('Step', i)
        if callgrind:
            callgrind.callgrind_start_instrumentation()

        tractor.optimize(damp=opt.damp, alphas=[1e-3, 1e-2, 0.1, 0.3, 1., 3., 10., 30., 100.])

        if callgrind:
            callgrind.callgrind_stop_instrumentation()

        makeplots(tractor, 1 + i, opt.suffix)
        pfn = 'herschel-%02i%s.pickle' % (1 + i, opt.suffix)
        pickle_to_file(tractor, pfn)
        print('Wrote', pfn)
Beispiel #3
0
def check_priors():
    np.seterrcall(np_err_handler)
    np.seterr(all='call')


    import logging
    import sys
    lvl = logging.DEBUG
    log_init(3)
    logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

    if True:
        # Check two-pixel priors: smoothness.
        H,W = 1,2
        logsa = np.zeros((H,W)) + np.log(1e-3)
        logt  = np.zeros((H,W)) + np.log(17.)
        emis  = np.zeros((H,W)) + 2.
        dwcs = Tan(11.2, 41.9, 1, 1, 1e-3, 0, 0, 1e-3, W, H)

        ds = DustSheet(logsa, logt, emis, dwcs)
        cat = Catalog()
        cat.append(ds)
        tractor = Tractor()
        tractor.setCatalog(cat)

        p0 = tractor.getParams()
        print('lnp0', tractor.getLogProb())

        if True:
            # check getLogProb()
            for j,xx in enumerate([
                np.linspace(np.log(1e-5), np.log(1e-1), 20),
                np.linspace(np.log(1e-5), np.log(1e-1), 20),
                np.linspace(np.log(10.), np.log(20.), 20),
                np.linspace(np.log(10.), np.log(20.), 20),
                np.linspace(0., 4., 20),
                np.linspace(0., 4., 20),
                ]):
                pp = []
                for x in xx:
                    tractor.setParam(j, x)
                    p = tractor.getLogProb()
                    pp.append(p)
                tractor.setParam(j, p0[j])
                plt.clf()
                plt.plot(xx, pp, 'ro-')
                plt.title(ds.getParamNames()[j])
            plt.savefig('p%i.png' % (20 + j))
            

            # set the absolute priors to have little effect and repeat.
            ds.prior_logt_std = np.log(100.)
            ds.prior_emis_std = np.log(100.)
            for j,xx in enumerate([
                np.linspace(np.log(1e-5), np.log(1e-1), 20),
                np.linspace(np.log(1e-5), np.log(1e-1), 20),
                np.linspace(np.log(10.), np.log(20.), 20),
                np.linspace(np.log(10.), np.log(20.), 20),
                np.linspace(0., 4., 20),
                np.linspace(0., 4., 20),
                ]):
                pp = []
                for x in xx:
                    tractor.setParam(j, x)
                    p = tractor.getLogProb()
                    pp.append(p)
                tractor.setParam(j, p0[j])
                plt.clf()
                plt.plot(xx, pp, 'ro-')
                plt.title(ds.getParamNames()[j])
                plt.savefig('p%i.png' % (30 + j))



            # revert the priors
            ds.prior_logt_std = np.log(1.2)
            ds.prior_emis_std = np.log(0.5)

        # check getLogPriorChi.
        for j,(ip,val) in enumerate([
            (0, np.log(1e-1)),
            (1, np.log(1e-5)),
            (2, np.log(5.)),
            (3, np.log(5.)),
            (2, np.log(30.)),
            (3, np.log(30.)),
            (4, 1.),
            (5, 1.),
            (4, 3.),
            (5, 3.),
            ]):
            print()
            print()
            print('Setting', ds.getParamNames()[ip], 'from', p0[ip], 'to', val)

            tractor.setParams(p0)
            tractor.setParam(ip, val)
            xx = [val]
            xxall = [tractor.getParams()]
            pp = [tractor.getLogProb()]
            for i in range(10):
                tractor.optimize()#damp=1e-3)
                xx.append(tractor.getParams()[ip])
                pp.append(tractor.getLogProb())
                xxall.append(tractor.getParams())
            plt.clf()
            plt.plot(xx, pp, 'ro-')
            plt.axvline(val, color='r', lw=2, alpha=0.5)
            plt.title(ds.getParamNames()[ip])
            plt.savefig('p%i.png' % (j+40))

            plt.clf()
            xxall = np.vstack(xxall)
            print('xxall', xxall.shape)
            for i in range(6):
                #plt.subplot(1,3,(i/2)+1)
                #plt.plot(xxall[:,i], pp, 'ro-')
                #if i == ip:
                #       plt.axvline(xxall[0,i], color='r', lw=2, alpha=0.5)
                #plt.title(ds.getParamNames()[i])
                plt.subplot(3,1,(i/2)+1)
                c = 'b'
                if i == ip:
                    c = 'r'
                plt.plot(xxall[:,i], 'o-', color=c)
                plt.title(ds.getParamNames()[i])
            plt.savefig('p%i.png' % (j+50))

    if False:
        # Check single-pixel priors: getLogPrior()
        N = 1

        H,W = N,N
        logsa = np.zeros((H,W)) + np.log(1e-3)
        logt  = np.zeros((H,W)) + np.log(17.)
        emis  = np.zeros((H,W)) + 2.
        dwcs = Tan(11.2, 41.9, 1, 1, 1e-3, 0, 0, 1e-3, N, N)

        ds = DustSheet(logsa, logt, emis, dwcs)
        cat = Catalog()
        cat.append(ds)
        tractor = Tractor()
        tractor.setCatalog(cat)

        p0 = tractor.getParams()
        print('lnp0', tractor.getLogProb())

        # no prior on solid angle
        # N(log(17.), log(1.2)) on T
        # N(2, 0.5) on emis
        for j,xx in enumerate([
            np.linspace(np.log(1e-5), np.log(1e-1), 20),
            np.linspace(np.log(10.), np.log(20.), 20),
            np.linspace(0., 4., 20),
            ]):
            pp = []
            for x in xx:
                tractor.setParam(j, x)
                p = tractor.getLogProb()
                pp.append(p)
            tractor.setParam(j, p0[j])
            plt.clf()
            plt.plot(xx, pp, 'ro-')
            plt.title(ds.getParamNames()[j])
            plt.savefig('p%i.png' % j)

        # Check single-pixel priors: getPriorChi()
        for j,(ip,val) in enumerate([
            (0, 1e-2),
            (1, np.log(5.)),
            (1, np.log(30.)),
            (2, 1.),
            (2, 3.),
            ]):
            print()
            print()
            print('Setting', ds.getParamNames()[ip], 'to', val)

            tractor.setParams(p0)
            tractor.setParam(ip, val)
            xx = [val]
            pp = [tractor.getLogProb()]
            for i in range(10):
                tractor.optimize(damp=1e-3)
                xx.append(tractor.getParams()[ip])
                pp.append(tractor.getLogProb())
            plt.clf()
            plt.plot(xx, pp, 'ro-')
            plt.title(ds.getParamNames()[ip])
            plt.savefig('p%i.png' % (j+10))
Beispiel #4
0
def check_priors():
	np.seterrcall(np_err_handler)
	np.seterr(all='call')


	import logging
	import sys
	lvl = logging.DEBUG
	log_init(3)
	logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

	if True:
		# Check two-pixel priors: smoothness.
		H,W = 1,2
		logsa = np.zeros((H,W)) + np.log(1e-3)
		logt  = np.zeros((H,W)) + np.log(17.)
		emis  = np.zeros((H,W)) + 2.
		dwcs = Tan(11.2, 41.9, 1, 1, 1e-3, 0, 0, 1e-3, W, H)

		ds = DustSheet(logsa, logt, emis, dwcs)
		cat = Catalog()
		cat.append(ds)
		tractor = Tractor()
		tractor.setCatalog(cat)

		p0 = tractor.getParams()
		print 'lnp0', tractor.getLogProb()

		if True:
			# check getLogProb()
			for j,xx in enumerate([
				np.linspace(np.log(1e-5), np.log(1e-1), 20),
				np.linspace(np.log(1e-5), np.log(1e-1), 20),
				np.linspace(np.log(10.), np.log(20.), 20),
				np.linspace(np.log(10.), np.log(20.), 20),
				np.linspace(0., 4., 20),
				np.linspace(0., 4., 20),
				]):
				pp = []
				for x in xx:
					tractor.setParam(j, x)
					p = tractor.getLogProb()
					pp.append(p)
				tractor.setParam(j, p0[j])
				plt.clf()
				plt.plot(xx, pp, 'ro-')
				plt.title(ds.getParamNames()[j])
			plt.savefig('p%i.png' % (20 + j))
			

			# set the absolute priors to have little effect and repeat.
			ds.prior_logt_std = np.log(100.)
			ds.prior_emis_std = np.log(100.)
			for j,xx in enumerate([
				np.linspace(np.log(1e-5), np.log(1e-1), 20),
				np.linspace(np.log(1e-5), np.log(1e-1), 20),
				np.linspace(np.log(10.), np.log(20.), 20),
				np.linspace(np.log(10.), np.log(20.), 20),
				np.linspace(0., 4., 20),
				np.linspace(0., 4., 20),
				]):
				pp = []
				for x in xx:
					tractor.setParam(j, x)
					p = tractor.getLogProb()
					pp.append(p)
				tractor.setParam(j, p0[j])
				plt.clf()
				plt.plot(xx, pp, 'ro-')
				plt.title(ds.getParamNames()[j])
				plt.savefig('p%i.png' % (30 + j))



			# revert the priors
			ds.prior_logt_std = np.log(1.2)
			ds.prior_emis_std = np.log(0.5)

		# check getLogPriorChi.
		for j,(ip,val) in enumerate([
			(0, np.log(1e-1)),
			(1, np.log(1e-5)),
			(2, np.log(5.)),
			(3, np.log(5.)),
			(2, np.log(30.)),
			(3, np.log(30.)),
			(4, 1.),
			(5, 1.),
			(4, 3.),
			(5, 3.),
			]):
			print
			print
			print 'Setting', ds.getParamNames()[ip], 'from', p0[ip], 'to', val

			tractor.setParams(p0)
			tractor.setParam(ip, val)
			xx = [val]
			xxall = [tractor.getParams()]
			pp = [tractor.getLogProb()]
			for i in range(10):
				tractor.optimize()#damp=1e-3)
				xx.append(tractor.getParams()[ip])
				pp.append(tractor.getLogProb())
				xxall.append(tractor.getParams())
			plt.clf()
			plt.plot(xx, pp, 'ro-')
			plt.axvline(val, color='r', lw=2, alpha=0.5)
			plt.title(ds.getParamNames()[ip])
			plt.savefig('p%i.png' % (j+40))

			plt.clf()
			xxall = np.vstack(xxall)
			print 'xxall', xxall.shape
			for i in range(6):
				#plt.subplot(1,3,(i/2)+1)
				#plt.plot(xxall[:,i], pp, 'ro-')
				#if i == ip:
				#		plt.axvline(xxall[0,i], color='r', lw=2, alpha=0.5)
				#plt.title(ds.getParamNames()[i])
				plt.subplot(3,1,(i/2)+1)
				c = 'b'
				if i == ip:
					c = 'r'
				plt.plot(xxall[:,i], 'o-', color=c)
				plt.title(ds.getParamNames()[i])
			plt.savefig('p%i.png' % (j+50))

	if False:
		# Check single-pixel priors: getLogPrior()
		N = 1

		H,W = N,N
		logsa = np.zeros((H,W)) + np.log(1e-3)
		logt  = np.zeros((H,W)) + np.log(17.)
		emis  = np.zeros((H,W)) + 2.
		dwcs = Tan(11.2, 41.9, 1, 1, 1e-3, 0, 0, 1e-3, N, N)

		ds = DustSheet(logsa, logt, emis, dwcs)
		cat = Catalog()
		cat.append(ds)
		tractor = Tractor()
		tractor.setCatalog(cat)

		p0 = tractor.getParams()
		print 'lnp0', tractor.getLogProb()

		# no prior on solid angle
		# N(log(17.), log(1.2)) on T
		# N(2, 0.5) on emis
		for j,xx in enumerate([
			np.linspace(np.log(1e-5), np.log(1e-1), 20),
			np.linspace(np.log(10.), np.log(20.), 20),
			np.linspace(0., 4., 20),
			]):
			pp = []
			for x in xx:
				tractor.setParam(j, x)
				p = tractor.getLogProb()
				pp.append(p)
			tractor.setParam(j, p0[j])
			plt.clf()
			plt.plot(xx, pp, 'ro-')
			plt.title(ds.getParamNames()[j])
			plt.savefig('p%i.png' % j)

		# Check single-pixel priors: getPriorChi()
		for j,(ip,val) in enumerate([
			(0, 1e-2),
			(1, np.log(5.)),
			(1, np.log(30.)),
			(2, 1.),
			(2, 3.),
			]):
			print
			print
			print 'Setting', ds.getParamNames()[ip], 'to', val

			tractor.setParams(p0)
			tractor.setParam(ip, val)
			xx = [val]
			pp = [tractor.getLogProb()]
			for i in range(10):
				tractor.optimize(damp=1e-3)
				xx.append(tractor.getParams()[ip])
				pp.append(tractor.getLogProb())
			plt.clf()
			plt.plot(xx, pp, 'ro-')
			plt.title(ds.getParamNames()[ip])
			plt.savefig('p%i.png' % (j+10))
Beispiel #5
0
def main():
	import optparse
	import logging
	import sys

	parser = optparse.OptionParser()
	parser.add_option('--threads', dest='threads', default=1, type=int, help='Use this many concurrent processors')
	parser.add_option('-v', '--verbose', dest='verbose', action='count', default=0,
					  help='Make more verbose')

	parser.add_option('--grid', '-g', dest='gridn', type=int, default=5, help='Dust parameter grid size')
	parser.add_option('--steps', '-s', dest='steps', type=int, default=10, help='# Optimization step')
	parser.add_option('--suffix', dest='suffix', default='', help='Output file suffix')

	parser.add_option('--no-100', dest='no100', action='store_true', default=False,
					  help='Omit PACS-100 data?')

	parser.add_option('--callgrind', dest='callgrind', action='store_true', default=False, help='Turn on callgrind around tractor.optimize()')

	parser.add_option('--resume', '-r', dest='resume', type=int, default=-1, help='Resume from a previous run at the given step?')

	parser.add_option('--zoom', dest='zoom', type=float, default=1, help='Scale down the model to only touch the (1/zoom x 1/zoom) central region of the images')

	parser.add_option('--damp', dest='damp', type=float, default=1., help='LSQR damping')

	opt,args = parser.parse_args()

	if opt.verbose == 0:
		lvl = logging.INFO
		log_init(2)
	else:
		lvl = logging.DEBUG
		log_init(3)
	
	logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout)

	if opt.threads > 1 and False:
		global dpool
		import debugpool
		dpool = debugpool.DebugPool(opt.threads)
		Time.add_measurement(debugpool.DebugPoolMeas(dpool))
		mp = multiproc(pool=dpool)
	else:
		print 'N threads', opt.threads
		mp = multiproc(opt.threads)#, wrap_all=True)

	if opt.callgrind:
		import callgrind
	else:
		callgrind = None

	np.seterrcall(np_err_handler)
	np.seterr(all='call')
	#np.seterr(all='raise')

	if opt.resume > -1:
		pfn = 'herschel-%02i%s.pickle' % (opt.resume, opt.suffix)
		print 'Reading from', pfn
		tractor = unpickle_from_file(pfn)
		tractor.mp = mp

		ds = tractor.getCatalog()[0]
		print 'DustSheet:', ds

		# derivs = ds.getParamDerivatives(tim)
		# dim = np.zeros(tim.shape)
		# #for k,deriv in enumerate(derivs[:40]):
		# for k,deriv in enumerate(derivs[::10]):
		# 	dim[:,:] = 0
		# 	deriv.addTo(dim)
		# 	plt.clf()
		# 	plt.imshow(dim, interpolation='nearest', origin='lower')
		# 	plt.savefig('deriv-%04i.png' % k)

		#tim = tractor.getImages()[0]
		# for it,tim in enumerate(tractor.getImages()):
		# 	X = ds._getTransformation(tim)
		# 	# #print 'X', X
		# 	keys = X.keys()
		# 	keys.sort()
		# 	# for k in keys[::10]:
		# 	# for k in keys[:40]:
		# 	for k in keys[::202]:
		# 		I,G,nil,nil = X[k]
		# 		rim = np.zeros_like(tim.getImage())
		# 		rim.ravel()[I] = G
		# 		plt.clf()
		# 		plt.imshow(rim, interpolation='nearest', origin='lower')
		# 		plt.colorbar()
		# 		plt.savefig('rim-%i-%04i.png' % (it,k))
		# 		print 'pix', k
		# sys.exit(0)

		makeplots(tractor, opt.resume, opt.suffix)
		step0 = opt.resume + 1

	else:
		step0 = 0
		tractor = create_tractor(opt)
		tractor.mp = mp

		# zero out invvar outside the model bounds.
		ds = tractor.getCatalog()[0]
		rd = ds.getRaDecCorners()
		for i,tim in enumerate(tractor.getImages()):
			poly = np.array([tim.getWcs().positionToPixel(RaDecPos(rdi[0], rdi[1])) for rdi in rd])
			poly = poly[:-1,:]
			print 'Model bounding box in image', tim.name, 'coordinates:'
			print poly.shape
			print poly
			H,W = tim.shape
			xx,yy = np.meshgrid(np.arange(W), np.arange(H))
			inside = point_in_poly(xx, yy, poly)
			iv = tim.getInvvar()
			iv[(inside == 0)] = 0.
			tim.setInvvar(iv)

		print 'Precomputing transformations...'
		ds = tractor.getCatalog()[0]

		# Split the grid-spread matrix into strips...
		async_results = []
		for im in tractor.getImages():
			args = []
			H,W = ds.shape
			dy = 10
			y = 0
			while y <= H:
				args.append((ds, im, y, min(H, y+dy)))
				y += dy
			async_results.append(mp.map_async(_map_trans, args))
		# Glue to strips back together...
		XX = []
		for ar in async_results:
			Xblocks = ar.get()
			X = Xblocks[0]
			for xi in Xblocks[1:]:
				X.update(xi)
			XX.append(X)
			
		for im,X in zip(tractor.getImages(), XX):
			ds._normalizeTransformation(im, X)
			ds._setTransformation(im, X)
		print 'done precomputing.'

		makeplots(tractor, 0, opt.suffix)
		pfn = 'herschel-%02i%s.pickle' % (0, opt.suffix)
		pickle_to_file(tractor, pfn)
		print 'Wrote', pfn

	for im in tractor.getImages():
		im.freezeAllBut('sky')

	for i in range(step0, opt.steps):
		if callgrind:
			callgrind.callgrind_start_instrumentation()

		tractor.optimize(damp=opt.damp, alphas=[1e-3, 1e-2, 0.1, 0.3, 1., 3., 10., 30., 100.])

		if callgrind:
			callgrind.callgrind_stop_instrumentation()

		makeplots(tractor, 1 + i, opt.suffix)
		pfn = 'herschel-%02i%s.pickle' % (1 + i, opt.suffix)
		pickle_to_file(tractor, pfn)
		print 'Wrote', pfn
Beispiel #6
0
def plot_wcs_outline(wcsfn,
                     plotfn,
                     W=256,
                     H=256,
                     width=36,
                     zoom=True,
                     zoomwidth=3.6,
                     grid=10,
                     hd=False,
                     hd_labels=False,
                     tycho2=False):
    anutil.log_init(3)
    #anutil.log_set_level(3)

    wcs = anutil.Tan(wcsfn, 0)
    ra, dec = wcs.radec_center()

    plot = ps.Plotstuff(outformat='png', size=(W, H), rdw=(ra, dec, width))
    plot.linestep = 1.
    plot.color = 'verydarkblue'
    plot.plot('fill')

    plot.fontsize = 12
    #plot.color = 'gray'
    # dark gray
    plot.rgb = (0.3, 0.3, 0.3)
    if grid is not None:
        plot.plot_grid(*([grid] * 4))

    plot.rgb = (0.4, 0.6, 0.4)
    ann = plot.annotations
    ann.NGC = ann.bright = ann.HD = 0
    ann.constellations = 1
    ann.constellation_labels = 1
    ann.constellation_labels_long = 1
    plot.plot('annotations')
    plot.stroke()
    ann.constellation_labels = 0
    ann.constellation_labels_long = 0
    ann.constellation_lines = 0

    ann.constellation_markers = 1
    plot.markersize = 3
    plot.rgb = (0.4, 0.6, 0.4)
    plot.plot('annotations')
    plot.fill()
    ann.constellation_markers = 0

    ann.bright_labels = False
    ann.bright = True
    plot.markersize = 2
    if zoom >= 2:
        ann.bright_labels = True
    plot.plot('annotations')
    ann.bright = False
    ann.bright_labels = False
    plot.fill()

    if hd:
        ann.HD = True
        ann.HD_labels = hd_labels
        ps.plot_annotations_set_hd_catalog(ann, settings.HENRY_DRAPER_CAT)
        plot.plot('annotations')
        plot.stroke()
        ann.HD = False
        ann.HD_labels = False

    if tycho2 and settings.TYCHO2_KD:
        from astrometry.libkd.spherematch import tree_open, tree_close, tree_build_radec, tree_free, trees_match
        from astrometry.libkd import spherematch_c
        from astrometry.util.starutil_numpy import deg2dist, xyztoradec
        import numpy as np
        import sys
        kd = tree_open(settings.TYCHO2_KD)
        # this is a bit silly: build a tree with a single point, then do match()
        kd2 = tree_build_radec(np.array([ra]), np.array([dec]))
        r = deg2dist(width * np.sqrt(2.) / 2.)
        #r = deg2dist(wcs.radius())
        I, nil, nil = trees_match(kd, kd2, r, permuted=False)
        del nil
        #print 'Matched', len(I)
        xyz = spherematch_c.kdtree_get_positions(kd, I)
        del I
        tree_free(kd2)
        tree_close(kd)
        #print >>sys.stderr, 'Got', xyz.shape, xyz
        tra, tdec = xyztoradec(xyz)
        #print >>sys.stderr, 'RA,Dec', ra,dec
        plot.apply_settings()
        for r, d in zip(tra, tdec):
            plot.marker_radec(r, d)
        plot.fill()

    ann.NGC = 1
    plot.plot('annotations')
    ann.NGC = 0

    plot.color = 'white'
    plot.lw = 3
    out = plot.outline
    out.wcs_file = wcsfn
    plot.plot('outline')

    if zoom:
        # MAGIC width, height are arbitrary
        zoomwcs = anutil.anwcs_create_box(ra, dec, zoomwidth, 1000, 1000)
        out.wcs = zoomwcs
        plot.lw = 1
        plot.dashed(3)
        plot.plot('outline')

    plot.write(plotfn)
Beispiel #7
0
        else:
            parser.print_help()
            sys.exit(-1)

    import logging

    logformat = "%(message)s"
    from astrometry.util.util import log_init  # , LOG_VERB, LOG_MSG

    loglvl = 2
    if opt.verbose:
        logging.basicConfig(level=logging.DEBUG, format=logformat)
        loglvl += 1
    else:
        logging.basicConfig(level=logging.INFO, format=logformat)
    log_init(loglvl)

    wcsfn = args[0]

    if dojson:
        from astrometry.util.util import anwcs

        wcs = anwcs(wcsfn, 0)
        jobjs = get_annotations_for_wcs(wcs, opt)
        import simplejson

        json = simplejson.dumps(jobjs)
        print json
        sys.exit(0)

    fmt = PLOTSTUFF_FORMAT_JPG
Beispiel #8
0
def plot_wcs_outline(wcsfn, plotfn, W=256, H=256, width=36, zoom=True,
                     zoomwidth=3.6, grid=10, hd=False, hd_labels=False,
                     tycho2=False):
    anutil.log_init(3)
    #anutil.log_set_level(3)

    wcs = anutil.Tan(wcsfn, 0)
    ra,dec = wcs.radec_center()

    plot = ps.Plotstuff(outformat='png', size=(W, H), rdw=(ra,dec,width))
    plot.linestep = 1.
    plot.color = 'verydarkblue'
    plot.plot('fill')

    plot.fontsize = 12
    #plot.color = 'gray'
    # dark gray
    plot.rgb = (0.3,0.3,0.3)
    if grid is not None:
        plot.plot_grid(*([grid]*4))

    plot.rgb = (0.4, 0.6, 0.4)
    ann = plot.annotations
    ann.NGC = ann.bright = ann.HD = 0
    ann.constellations = 1
    ann.constellation_labels = 1
    ann.constellation_labels_long = 1
    plot.plot('annotations')
    plot.stroke()
    ann.constellation_labels = 0
    ann.constellation_labels_long = 0
    ann.constellation_lines = 0

    ann.constellation_markers = 1
    plot.markersize = 3
    plot.rgb = (0.4, 0.6, 0.4)
    plot.plot('annotations')
    plot.fill()
    ann.constellation_markers = 0

    ann.bright_labels = False
    ann.bright = True
    plot.markersize = 2
    if zoom >= 2:
        ann.bright_labels = True
    plot.plot('annotations')
    ann.bright = False
    ann.bright_labels = False
    plot.fill()

    if hd:
        ann.HD = True
        ann.HD_labels = hd_labels
        ps.plot_annotations_set_hd_catalog(ann, settings.HENRY_DRAPER_CAT)
        plot.plot('annotations')
        plot.stroke()
        ann.HD = False
        ann.HD_labels = False

    if tycho2:
        from astrometry.libkd.spherematch import tree_open, tree_close, tree_build_radec, tree_free, trees_match
        from astrometry.libkd import spherematch_c
        from astrometry.util.starutil_numpy import deg2dist, xyztoradec
        import numpy as np
        import sys
        kd = tree_open(settings.TYCHO2_KD)
        # this is a bit silly: build a tree with a single point, then do match()
        kd2 = tree_build_radec(np.array([ra]), np.array([dec]))
        r = deg2dist(width * np.sqrt(2.) / 2.)
        #r = deg2dist(wcs.radius())
        I,J,d = trees_match(kd, kd2, r, permuted=False)
        del J
        del d
        #print 'Matched', len(I)
        xyz = spherematch_c.kdtree_get_positions(kd, I)
        del I
        tree_free(kd2)
        tree_close(kd)
        #print >>sys.stderr, 'Got', xyz.shape, xyz
        tra,tdec = xyztoradec(xyz)
        #print >>sys.stderr, 'RA,Dec', ra,dec
        plot.apply_settings()
        for r,d in zip(tra,tdec):
            plot.marker_radec(r,d)
        plot.fill()

    ann.NGC = 1
    plot.plot('annotations')
    ann.NGC = 0

    plot.color = 'white'
    plot.lw = 3
    out = plot.outline
    out.wcs_file = wcsfn
    plot.plot('outline')

    if zoom:
        # MAGIC width, height are arbitrary
        zoomwcs = anutil.anwcs_create_box(ra, dec, zoomwidth, 1000,1000)
        out.wcs = zoomwcs
        plot.lw = 1
        plot.dashed(3)
        plot.plot('outline')

    plot.write(plotfn)