def psf_gen(fit_id, params): gstrat, sfact, mpar = params['G_STRAT'], params['S_FACT'], params[ 'MOF_PARAMS'] psfsize, g_par, g_pos = params['PSF_SIZE'], params['G_PARAMS'], params[ 'G_POS'] show, objsize = params['SHOW'], params['OBJ_SIZE'] out(2, 'Initialization...') # if psfsize is None: psfsize = objsize # nx = ny = psfsize*sfact # if center == 'O': # c1, c2 = nx/2.-0.5, ny/2.-0.5 # elif center == 'NE': # c1, c2 = nx/2., ny/2. # elif center == 'SW': # c1, c2 = nx/2.-1., ny/2.-1. # psf = PSF((nx,ny), (c1,c2)) # mof = mpar[fit_id][:4] # gpos = array(g_pos)[fit_id] # gpar = array(g_par)[fit_id] # out(2, 'Building the PSF...') # psf.set_finalPSF(mof, gpar, gpos, gstrat, (c1, c2, 1.), center=center) # out(2, 'Done. Creating splitted PSF (old style shape)...') # s = fn.switch_psf_shape(psf.array, center) size = psfsize * sfact, psfsize * sfact psf, s = fn.psf_gen(size, zeros(size), mpar[fit_id][:4], array(g_par)[fit_id], array(g_par)[fit_id], gstrat, sfact) fn.array2fits(psf.array, 'results/psf_' + str(fit_id + 1) + '.fits') fn.array2fits(s, 'results/s_' + str(fit_id + 1) + '.fits') if show == True: fn.array2ds9(psf.array, name='psf') fn.array2ds9(s, name='s', frame=2)
def main(argv=None): cfg = 'config.py' if argv is not None: sys.argv = argv opt, args = fn.get_args(sys.argv) p = interactive = False if args is not None: cfg = args[0] if 's' in opt: out.level = 0 if 'v' in opt: out.level = 2 if 'd' in opt: DEBUG = True out.level = 3 out(1, '~~~ DEBUG MODE ~~~') if 'h' in opt: out(1, 'No help page yet!') return 0 if 'i' in opt: interactive = True VAL_BND = (0, 0) USE_MOMENTS = False f = open(cfg, 'r') exec f.read() f.close() vars = [ 'FILENAME', 'SHOW', 'NOBJ', 'SKY_BACKGROUND', 'SIGMA_SKY', 'IMG_GAIN', 'NOWRITE' ] err = fn.check_namespace(vars, locals()) if err > 0: return 1 #TODO: implement option choice files = ws.get_files(FILENAME, directory='img')[0] #@UndefinedVariable out(2, FILENAME, cfg) #@UndefinedVariable data = fn.get_data(files[0], 'img') #- SKY_BACKGROUND if interactive: # import numdisplay fn.array2ds9(data, zscale=True) # numdisplay.readcursor() out( 1, 'Place markers on the desired stars (may not work if several ds9 instances are actives).' ) out(1, '[Press ENTER when you are done]', '-r') _ = raw_input() stars = [] for r in getregions(): stars += [(r.x, r.y)] out(1, stars) else: stars = get_stars(data, NOBJ, SKY_BACKGROUND[0], SIGMA_SKY[0], IMG_GAIN, VAL_BND, True, None, USE_MOMENTS)[0] #@UndefinedVariable if NOWRITE is False: #@UndefinedVariable fn.write_cfg(cfg, {'STARS': stars}) return 0
def get_stars(self, interactive=False): """ Call the procedure getting the stars positions. @type interactive: boolean @param interactive: True to enable the interactive mode (default: False). If the interactive mode is on, the positions of the stars will be acquired \ by prompting the user for ds9 regions. If not, the program will attempt to find stars by looking at different objects\ and choosing those with correlated ellipticities. Those are found by fitting a gaussian \ or by looking at the 2nd order moments (USE_MOMENTS keyword of the configuration file). """ self.refresh() dir = 'images' if self.as_exe: dir = './' files = ws.get_files(self.params['FILENAME'], directory=dir)[0] img = fn.get_data(files[0], dir) if interactive: fn.array2ds9(img, zscale=True) out( 1, 'Place markers on the desired stars (may not work if several ds9 instances are actives).' ) out(1, '[Press ENTER when you are done]', '-r') _ = raw_input() stars = [] for r in _2_get_stars.getregions(): stars += [(r.x, r.y)] out(1, stars) else: stars = _2_get_stars.get_stars(img, self.params, show=self.params['SHOW'])[0] if self.params['NOWRITE'] is False: self.params['STARS'] = stars self.save()
def prompt(file=None): if file is None: file = 'gsig_1.fits' opt, args = fn.get_args(sys.argv) if args is not None: file = args[0] s = string.split(file, '_', 1) maskfile = s[0] + 'mask_' + s[1] try: mask = fn.get_data(maskfile) except: sys.stdout.write('Enter the filename of the mask: ') mask = fn.get_data(raw_input()) data = fn.get_data(file, '') fn.array2ds9(data, frame=1, name='data', zscale=True) fn.array2ds9(mask, frame=2, name='current_mask') print 'Place markers on the desired region (only circles and polygons are supported).' sys.stdout.write( '[Press ENTER when you are done (opt: enter the mask value)]') input = raw_input() if input == 'q' or input == 'quit': return 0 mask = fn.get_ds9_mask(data.shape) try: val = eval(string.strip(input)) except: val = 1.e3 print 'Using default mask value:', val print(data * mask).sum() np.putmask(data, mask, val) fn.array2ds9(mask, frame=2, name='mask') fn.array2ds9(data, frame=3, name='new_data', zscale=False) # fn.array2fits(data, file) fn.array2fits(mask.astype(np.float), maskfile)
def set_ini(data, params, savedir='results/'): #TODO: set to the new norms!! (params) def X2x(X): return X * sfact - 1. / sfact def x2X(x): return x / sfact + 1. / sfact**2. nimg, sfact, gres = params['filenb'], params['S_FACT'], params['G_RES'] show, USE_CUDA, nb_src = params['SHOW'], params['CUDA'], params['NB_SRC'] box_size, src_range, fftdiv = params['BOX_SIZE'], params[ 'SRC_RANGE'], params['FFT_DIV'] obj = [data['objs'][i][0].astype('float64') for i in xrange(nimg)] sigma = [data['objssig'][i][0].astype('float64') for i in xrange(nimg)] psf = [data['psfs'][i][0].astype('float64') for i in xrange(nimg)] bshape = obj[0].shape sshape = (int(bshape[0] * sfact), int(bshape[1] * sfact)) orig_fwhm = 4.5 # 4. #BIG pixels! ~3. if USE_CUDA and fn.has_cuda(): context, plan = fn.cuda_init(sshape) out(2, 'CUDA initializations') def convol(a, b): return fn.cuda_conv(plan, a, b) def deconv(a, b): return fn.cuda_fftdiv(plan, a, b) if not fftdiv: deconv = None else: USE_CUDA = False convol = fn.conv deconv = None out(2, 'Interpolating data...') obji = [] sigmai = [] flux_o = [] flux_s = [] for i in xrange(nimg): d_filt = signal.signaltools.wiener(obj[i], [3, 3]) # psf[i] = signal.signaltools.medfilt2d(psf[i], [3,3]) # psf[i] /= psf[i].sum() obji += [ scipy.ndimage.interpolation.zoom(d_filt, sfact, order=5, mode='reflect').astype('float64') ] sigmai += [ scipy.ndimage.interpolation.zoom(sigma[i], sfact, order=5, mode='reflect').astype('float64') ] flux_o += [obj[i].sum()] flux_s += [sigma[i].sum()] obji[i] *= flux_o[-1] / obji[i].sum() sigmai[i] *= flux_s[-1] / sigmai[i].sum() out(2, 'Setting initial sources parameters...') srcpos, ini = _get_ini(obji[0], sigmai[0], psf[0], nb_src, gres, sfact, nimg, src_range, convol, deconv, pad=box_size) shiftpar = -fn.get_shifts( obj, boxsize=10)[1:] #array([0.1 for i in xrange(2*nimg)]) shiftpar = shiftpar.ravel() fn.array2fits(ini, savedir + 'bkg_ini.fits') ws.drop('INI_PAR', srcpos) if show: fn.array2ds9(obji[0], name='data') fn.array2ds9(ini, name='background', frame=2) if USE_CUDA: out(2, 'Freeing CUDA context...') context.pop() return ini, srcpos, shiftpar
def fitnum(fit_id, data, params, savedir='results/', liveplot=False): starpos, npix, sfactor, mpar = params['STARS'], params['NPIX'], params[ 'S_FACT'], copy.deepcopy(params['MOF_PARAMS']) gres, fitrad, psf_size, itnb = params['G_RES'], params['FIT_RAD'], params[ 'PSF_SIZE'], params['MAX_IT_N'] nbruns, show, _lambda, stepfact = params['NB_RUNS'], params[ 'SHOW'], params['LAMBDA_NUM'], params['BKG_STEP_RATIO_NUM'] cuda, radius, stddev, objsize = params['CUDA'], params['PSF_RAD'], params[ 'SIGMA_SKY'], params['OBJ_SIZE'] minstep, maxstep, thresh = params['MIN_STEP_NUM'], params[ 'MAX_STEP_NUM'], params['WL_THRESHOLD_NUM'] stars = [] mofpar = Param(1, 0) bshape = data['stars'][fit_id][0].shape sshape = (int(bshape[0] * sfactor), int(bshape[1] * sfactor)) ############### Prepare the gaussian PSF ############### r_len = sshape[0] c1, c2 = r_len / 2. - 0.5, r_len / 2. - 0.5 #-0.5 to align on the pixels grid r = fn.gaussian(sshape, gres, c1, c2, 1.) # r = fn.LG_filter(sshape, gres, c1, c2) if cuda and fn.has_cuda(): out(2, 'CUDA initializations') context, plan = fn.cuda_init(sshape) r = fn.switch_psf_shape(r, 'SW') def conv(a, b): return fn.cuda_conv(plan, a, b) else: conv = fn.conv cuda = False r /= r.sum() ############### Set the smoothing ############### _lambda = fn.get_lambda(sshape, radius + 5., _lambda) sm = r.copy() # if not thresh: # thresh = params['SIGMA_SKY'] ############## Initializations ############## for i, pos in enumerate(starpos): #populates the stars array: im = Image(copy.deepcopy(data['stars'][fit_id][i]), noisemap=copy.deepcopy(data['sigs'][fit_id][i]), mask=copy.deepcopy(data['masks'][fit_id][i])) # im.shiftToCenter(mpar[fit_id][4+3*i]/sfactor,mpar[fit_id][4+3*i+1]/sfactor, center_mode='O') # mpar[fit_id][4+3*i:4+3*i+2] = sshape[0]/2., sshape[1]/2. stars += [Star(i + 1, im, mofpar, sfactor, gres, False)] mofpar.fromArray(np.array(mpar[fit_id]), i=-1) img_shifts = [] mof_err = 0. for i, s in enumerate(stars): s.moff_eval() s.build_diffm() s.image.noiseMap /= mpar[fit_id][6 + 3 * i] s.diffm.array /= mpar[fit_id][6 + 3 * i] img_shifts += [((mpar[fit_id][4 + 3 * i] - r_len / 2.), (mpar[fit_id][5 + 3 * i] - r_len / 2.))] mof_err += (np.logical_not(s.image.mask) * abs(s.diffm.array / s.image.noiseMap)).sum() ############ Deconvolution ############ # dec = DecCLEAN([s.diffm.array for s in stars], # dec = DecMC([s.diffm.array for s in stars], # dec = DecWL([s.diffm.array for s in stars], # dec = DecML([s.diffm.array for s in stars], dec = Dec([s.diffm.array for s in stars], [s.image.noiseMap for s in stars], [s.image.mask for s in stars], r, sm, conv, img_shifts, _lambda, gres, thresh) # bak = dec.deconv() dec.ini *= fn.get_circ_mask_exp(dec.ini.shape, radius) bak = dec.deconv(itnb, minstep, maxstep, stepfact, radius) # bak = cyclespin(bak, 3, dec._get_dn_threshold(bak)[0]*10.) bak_conv = conv(r, bak) bak *= fn.get_circ_mask_exp(bak.shape, radius) #, exp=4.) ########## gaus_err = 0. for i, im in enumerate(stars): gaus_err += abs(dec.get_im_resi(bak_conv, i)).sum() out(2, "indicative error gain:", 100 * (1. - gaus_err / mof_err), "%") ########## Set the numerical PSF ########## out(2, 'Building the final PSF...') if psf_size is None: psf_size = objsize size = psf_size * sfactor, psf_size * sfactor psf, s = fn.psf_gen(size, bak, mpar[fit_id][:4], [[]], [[]], 'mixed', sfactor) ############ Prepare output ############ imgs, names = [], [] imgs += [psf.array, s, bak, dec.ini] names += [ 'psf_' + str(fit_id + 1), 's_' + str(fit_id + 1), 'psf_num_' + str(fit_id + 1), 'psfnum_ini_' + str(fit_id + 1) ] for i, s in enumerate(stars): resi = dec.get_im_resi(bak_conv, i, ret_all=True)[1] imgs += [s.diffm.array, resi] names += [ "difmof%(fnb)02d_%(n)02d" % { 'fnb': fit_id + 1, 'n': s.id }, "difnum%(fnb)02d_%(n)02d" % { 'fnb': fit_id + 1, 'n': s.id } ] ############ Save and display ############ if savedir is not None: out(2, 'Writing to disk...') for i, im in enumerate(imgs): fn.array2fits(im, savedir + names[i] + '.fits') # fn.save_img(imgs, names, savedir+'overview.png', min_size=(256,256)) if show == True: out(2, 'Displaying results...') for i, im in enumerate(imgs): fn.array2ds9(im, name=names[i], frame=i + 1) import pylab as p p.figure() trace = np.array(dec.trace) X = np.arange(trace.shape[0]) p.title('Error evolution') p.plot(X, trace) # p.legend() p.draw() p.savefig(savedir + 'trace_fitnum%(fnb)02d.png' % {'fnb': fit_id + 1}) if show == True: p.show() if cuda: out(2, 'Freeing CUDA context...') context.pop() return 0
def deconv(data, params, savedir='results/'): global err, old_bkg, TRACE, TRACE2, err_bk, err_sm err = old_bkg = None sfact, gres, itnb, mpar = params['S_FACT'], params['G_RES'], params[ 'MAX_IT_D'], params['MOF_PARAMS'] gstrat, gpar, gpos = params['G_STRAT'], params['G_PARAMS'], params['G_POS'] show, maxpos_range, stddev = params['SHOW'], params[ 'MAXPOS_RANGE'], params['SIGMA_SKY'] max_iratio_range, force_ini = params['MAX_IRATIO_RANGE'], params[ 'FORCE_INI'] bkg_ini, stepfact, bkg_ratio = params['BKG_INI_CST'], params[ 'BKG_STEP_RATIO'], params['BKG_START_RATIO'] _lambda, nbruns, nb_src = params['LAMBDA'], params['D_NB_RUNS'], params[ 'NB_SRC'] box_size, src_range, cuda = params['BOX_SIZE'], params[ 'SRC_RANGE'], params['CUDA'] srcini, minstep_px, maxstep_px = params['INI_PAR'], params[ 'MIN_STEP_D'], params['MAX_STEP_D'] thresh = params['WL_THRESHOLD_DEC'] out(2, 'Initialization') nimg = params['filenb'] objs = [data['objs'][i][0].astype('float64') for i in xrange(nimg)] sigmas = [data['objssigs'][i][0].astype('float64') for i in xrange(nimg)] masks = [data['objsmasks'][i][0].astype('float64') for i in xrange(nimg)] psfs = [data['psfs'][i][0].astype('float64') for i in xrange(nimg)] dev = stddev[0] mpar = mpar[0] gpar = gpar[0] gpos = gpos[0] bshape = objs[0].shape sshape = (int(bshape[0] * sfact), int(bshape[1] * sfact)) sources = [ PSF(sshape, (sshape[0] / 2., sshape[1] / 2.)) for i in xrange(nimg) ] ############### Prepare the gaussian PSF ############### r_len = sshape[0] c1, c2 = r_len / 2. - 0.5, r_len / 2. - 0.5 #-0.5 to align on the pixels grid r = fn.gaussian(sshape, gres, c1, c2, 1.) # r = fn.LG_filter(sshape, gres, c1, c2) if cuda and fn.has_cuda(): out(2, 'CUDA initializations') context, plan = fn.cuda_init(sshape) r = fn.switch_psf_shape(r, 'SW') def conv(a, b): return fn.cuda_conv(plan, a, b) def div(a, b): return fn.cuda_fftdiv(plan, a, b) psfs = [fn.switch_psf_shape(p, 'SW') for p in psfs] else: conv = fn.conv div = None #fn.div cuda = False r /= r.sum() div = None ########## Initializations ########## img_shifts = fn.get_shifts(objs, 10.) _lambda = fn.get_lambda(sshape, None, _lambda) # if not thresh: # thresh = params['SIGMA_SKY'] dec = DecSrc(objs, sigmas, masks, psfs, r, conv, img_shifts, _lambda, gres, thresh, nb_src, srcini, box_size, src_range, force_ini, bkg_ini, bkg_ratio) ########## Deconvolution ########## bak, src_par = dec.deconv(itnb, minstep_px, maxstep_px, maxpos_range, max_iratio_range, stepfact, nbruns) out(2, 'Initial sources parameters [x,y,I]:', dec.src_ini) out(2, 'Final sources parameters [x,y,I]:', src_par) out(2, 'offsets:', dec.shifts) ############ Prepare output ############ imgs, names = [], [] imgs += [bak, dec.ini] names += ['background', 'bkg_ini'] dec.set_sources(src_par, bak) for i in xrange(len(objs)): bak_conv = conv(dec.psfs[i], bak + dec.sources[i].array) resi = dec.get_im_resi(bak_conv, i) imgs += [ objs[i], resi, dec.sources[i].array, bak + dec.sources[i].array ] names += [ "g_" + str(i + 1), "resi_%(fnb)02d" % { 'fnb': i + 1 }, "sources_%(fnb)02d" % { 'fnb': i + 1 }, "deconv_%(fnb)02d" % { 'fnb': i + 1 } ] ############ Save and display ############ if savedir is not None: out(2, 'Writing to disk...') for i, im in enumerate(imgs): fn.array2fits(im, savedir + names[i] + '.fits') # fn.save_img(imgs, names, savedir+'overview.png', min_size=(256,256)) if show == True: out(2, 'Displaying results...') for i, im in enumerate(imgs): fn.array2ds9(im, name=names[i], frame=i + 1) import pylab as p p.figure(1) trace = array(dec.trace) X = arange(trace.shape[0]) p.title('Error evolution') p.plot(X, trace) # p.legend() p.draw() p.savefig(savedir + 'trace_deconv.png') if show == True: p.show() if cuda: out(2, 'Freeing CUDA context...') context.pop() return src_par, dec.shifts, dec.src_ini