def buildGal(x0, y0, components=1, gaussian=False, spherical=False, startpars=None, fixamp=False): from imageSim import SBModels GX = {} GY = {} GR = {} GQ = {} GP = {} GN = {} GA = {} gals = [] for i in range(components): GX[i] = pymc.Uniform('GX%i' % i, x0 - 25., x0 + 25., value=x0) GY[i] = pymc.Uniform('GY%i' % i, y0 - 25., y0 + 25., value=y0) GP[i] = pymc.Uniform('GP%i' % i, -180., 180., value=-23) if i == 0: galpars = [GX[i], GY[i], GP[i]] galcov = list(numpy.array([0.1, 0.1, 1.]) / 1e0) else: galpars += [GX[i], GY[i], GP[i]] galcov += list(numpy.array([0.05, 0.05, 1.]) / 1e0) GQ[i] = pymc.Uniform('GQ%i' % i, 0.2, 1, value=0.79) GR[i] = pymc.Uniform('GR%i' % i, 1., 100., value=10) GN[i] = pymc.Uniform('GN%i' % i, 0.25, 8., value=2.34) galpars += [GQ[i], GR[i], GN[i]] galcov += list(numpy.array([0.03, 0.03, 0.07]) / 1e0) if fixamp != True: GA[i] = pymc.Uniform('GA%i' % i, 0, 100., value=0.1) #no good in longrun galpars += [GA[i]] galcov += [0.01] var = { 'x': GX[i], 'y': GY[i], 'q': GQ[i], 'pa': GP[i], 're': GR[i], 'n': GN[i], 'amp': GA[i] } gals.append(SBModels.Sersic('galaxy%i' % i, var)) else: var = { 'x': GX[i], 'y': GY[i], 'q': GQ[i], 'pa': GP[i], 're': GR[i], 'n': GN[i], 'amp': 1 } gals.append(SBModels.Sersic('galaxy%i' % i, var)) return gals, galpars, galcov
def buildSource(x0, y0, components=1, gaussian=False, spherical=False, startpars=None, fixamp=False): from imageSim import SBModels SX = {} SY = {} SR = {} SQ = {} SP = {} SN = {} SA = {} gals = [] for i in range(components): SX[i] = pymc.Uniform('SX%i' % i, x0 - 25., x0 + 25., value=x0) SY[i] = pymc.Uniform('SY%i' % i, y0 - 25., y0 + 25., value=y0) SP[i] = pymc.Uniform('SP%i' % i, -180., 180., value=-23) if i == 0: galpars = [SX[i], SY[i], SP[i]] galcov = list(numpy.array([0.1, 0.1, 1.]) / 1e0) else: galpars += [SX[i], SY[i], SP[i]] galcov += list(numpy.array([0.05, 0.05, 1.]) / 1e0) SQ[i] = pymc.Uniform('SQ%i' % i, 0.2, 1, value=0.79) SR[i] = pymc.Uniform('SR%i' % i, 0.1, 100., value=10) SN[i] = pymc.Uniform('SN%i' % i, 0.25, 8., value=2.34) galpars += [SQ[i], SR[i], SN[i]] galcov += list(numpy.array([0.03, 0.03, 0.07]) / 1e0) if fixamp != True: SA[i] = pymc.Uniform('SA%i' % i, 0, 100., value=0.1) #no good in longrun galpars += [SA[i]] galcov += [0.01] var = { 'x': SX[i], 'y': SY[i], 'q': SQ[i], 'pa': SP[i], 're': SR[i], 'n': SN[i], 'amp': SA[i] } gals.append(SBModels.Sersic('galaxy%i' % i, var)) else: var = { 'x': SX[i], 'y': SY[i], 'q': SQ[i], 'pa': SP[i], 're': SR[i], 'n': SN[i], 'amp': 1 } gals.append(SBModels.Sersic('galaxy%i' % i, var)) return gals, galpars, galcov
def setSourcePars(self, b, m, x, y, q, p, r, n=1, pixelunits=False, sourcenumber=1): if pixelunits == False: x = self.trytoconvert(x, self.pixelsize) y = self.trytoconvert(y, self.pixelsize) r = self.trytoconvert(r, self.pixelsize) b = self.trytoconvert(b, self.pixelsize) self.xs[sourcenumber] = x + self.xl + self.deltaxl + 0.000001 self.ys[sourcenumber] = y + self.yl + self.deltayl + 0.000001 self.ms[sourcenumber] = m self.qs[sourcenumber] = q self.ps[sourcenumber] = p self.rs[sourcenumber] = r self.ns[sourcenumber] = n self.bl[sourcenumber] = b self.src[sourcenumber]=SBModels.Sersic('src%i'%sourcenumber, {'x':self.xs[sourcenumber],'y':self.ys[sourcenumber],\ 'q':self.qs[sourcenumber],'pa':self.ps[sourcenumber],\ 're':self.rs[sourcenumber],'n':self.ns[sourcenumber]}) self.sourcenumbers.append(sourcenumber) self.sourcemodel[sourcenumber] = {} self.totallensedsrcmag[sourcenumber] = {} self.fakeResidual[sourcenumber] = {} self.SN[sourcenumber] = {} self.SNRF[sourcenumber] = {} self.convolvedsrc[sourcenumber] = {}
def setSourcePars(self, b, m, x, y, q, p, r, n=1, pixelunits=False, sourcenumber=1): if pixelunits == False: # if pixelunits doesnt exist x = self.trytoconvert( x, self.pixelsize ) # x is the result of trytoconvert function where par is x, and p is the pixelsize y = self.trytoconvert( y, self.pixelsize ) # y is the result of trytoconvert function where par is y, and p is the pixelsize r = self.trytoconvert( r, self.pixelsize ) # r is the result of trytoconvert function where par is r, and p is the pixelsize b = self.trytoconvert( b, self.pixelsize ) # b is the result of trytoconvert function where par is b, and p is the pixelsize self.xSum[ sourcenumber] = x + self.xl + self.deltaxl + 0.000001 # xsum with a sourcenumber of 1 is the sum of all x self.ySum[ sourcenumber] = y + self.yl + self.deltayl + 0.000001 # ysum with a sourcenumber of 1 is the sum of all y self.ms[sourcenumber] = m self.qs[sourcenumber] = q self.ps[sourcenumber] = p self.rs[sourcenumber] = r self.ns[sourcenumber] = n self.bl[sourcenumber] = b # The identified sourcenumber in the source dictionary is stated. # where x = xSum, y = ySum, q = qs, pa = ps, re =rs, n=ns where sourcenumber is the key self.src[sourcenumber] = SBModels.Sersic( 'src%i' % sourcenumber, { 'x': self.xSum[sourcenumber], 'y': self.ySum[sourcenumber], 'q': self.qs[sourcenumber], 'pa': self.ps[sourcenumber], 're': self.rs[sourcenumber], 'n': self.ns[sourcenumber] }) # sourcenumbers array increases with source number self.sourcenumbers.append(sourcenumber) # sourceModel, totallensedssrcmag, fakeResidual, SN, SNRF, convolvedsrc using the # input of sourcenumber is now a dictonary self.sourceModel[sourcenumber] = {} self.totallensedsrcmag[sourcenumber] = {} self.fakeResidual[sourcenumber] = {} self.SN[sourcenumber] = {} self.SNRF[sourcenumber] = {} self.convolvedsrc[sourcenumber] = {}
def setLensPars(self, m, r, q, n=4, pixelunits=False, reset=True, xb=0, xp=0, jiggle=0): if reset: self.Reset() #if reset is true, reset all parameters self.rl = {} #define rl variable as a dictionary if pixelunits == False: #if there is no pixelunits # loop to fill rl array with bands and pixelsizes for band in r.keys( ): #if band is within the dictionary r keys, then... self.rl[band] = self.trytoconvert( r[band], self.pixelsize ) # the rl band array is set to be the rband captured by the trytoconvert method self.ml = m # defining an array of band, pixelsize where ml is nodel? self.ql = q self.deltaxl = ( numpy.random.rand() - 0.5) * 2 * jiggle #delta xl coordinate = 0, when jiggle = 0 self.deltayl = (numpy.random.rand() - 0.5 ) * 2 * jiggle #delta yl coordinate, when jiggle = 0 if jiggle != 0: self.deltap = 0.0 + ( numpy.random.rand() - 0.5 ) * 180 # if jiggle is not 0, then delta p is numpy.random.rand()-0.5)*180 n = (numpy.random.rand() + 1) * 4 else: self.deltap = 0.0 self.nl = n # defining gal self.gal = SBModels.Sersic( 'gal', { 'x': self.xl + self.deltaxl, 'y': self.yl + self.deltayl, 'q': self.ql, 'pa': 90 + self.deltap, 're': self.rl[band], 'n': self.nl }) #defining xb and xp as themselves in this class self.xb = xb self.xp = xp
def setLensPars(self, m, r, q, n=4, pixelunits=False, reset=True, xb=0, xp=0, jiggle=0): if reset: self.Reset() self.rl = {} if pixelunits == False: for band in r.keys(): self.rl[band] = self.trytoconvert(r[band], self.pixelsize) self.ml = m self.ql = q self.deltaxl = (numpy.random.rand() - 0.5) * 2 * jiggle self.deltayl = (numpy.random.rand() - 0.5) * 2 * jiggle if jiggle != 0: self.deltap = 0. + (numpy.random.rand() - 0.5) * 180 n = (numpy.random.rand() + 1) * 4 else: self.deltap = 0. self.nl = n self.gal = SBModels.Sersic( 'gal', { 'x': self.xl + self.deltaxl, 'y': self.yl + self.deltayl, 'q': self.ql, 'pa': 90 + self.deltap, 're': self.rl[band], 'n': self.nl }) self.xb = xb self.xp = xp
def __init__(self, config): self.pars = [] self.par2index = {} self.index2par = {} self.sci = {} self.err = {} self.convol_matrix = {} self.zp = {} self.light_sb_models = [] self.source_sb_models = [] self.light_sed_models = [] self.source_sed_models = [] self.light_mags = [] self.source_mags = [] self.lens_models = [] self.logp = None # defines bands self.bands = [] self.reference_band = config['reference_band'] for band in config['filters']: self.bands.append(band) self.nbands = len(self.bands) # reads in data filtnames = {} psfdic = {} for i in range(self.nbands): band = self.bands[i] self.zp[band] = config['zeropoints'][i] filtname = config['filter_prefix'] + band + config['filter_suffix'] filtnames[band] = filtname hdu = pyfits.open( os.path.expandvars(config['data_dir']) + '/' + config['filename'] + '_%s' % band + config['science_tag'])[0] img = hdu.data.copy() subimg = img.copy() self.sci[band] = subimg suberr = pyfits.open( os.path.expandvars(config['data_dir']) + '/' + config['filename'] + '_%s' % band + config['err_tag'])[0].data.copy() if config['err_type'] == 'VAR': self.err[band] = suberr**0.5 elif config['err_type'] == 'SIGMA': self.err[band] = suberr.copy() else: df psf_file = pyfits.open( os.path.expandvars(config['data_dir']) + '/' + config['filename'] + '_%s' % band + config['psf_tag']) nhdu = len(psf_file) found = False n = 0 while not found and n < nhdu: if psf_file[n].data is not None: psf = psf_file[n].data.copy() found = True else: n += 1 if not found: df m = (psf[:2].mean() + psf[-2:].mean() + psf[:, :2].mean() + psf[:, -2:].mean()) / 4. psf -= m psf /= psf.sum() psfdic[band] = psf self.convol_matrix[band] = convolve.convolve(self.sci[band], psf)[1] # prepares mask and data array ny, nx = self.sci[self.bands[0]].shape X, Y = np.meshgrid(np.arange(1. * nx), np.arange(1. * ny)) self.ny = ny self.nx = nx self.X = X self.Y = Y self.R = ((X - nx / 2)**2 + (Y - ny / 2)**2)**0.5 if config['mask_dir'] is None: mask_dir = './' else: mask_dir = os.path.expandvars(config['mask_dir']) if config['maskname'] is not None: MASK = pyfits.open(mask_dir + config['maskname'])[0].data.copy() else: MASK = np.ones(X.shape, dtype=int) if config['rmax'] is not None: MASK[self.R > config['rmax']] = 0 mask = MASK > 0 mask_r = mask.ravel() self.modelstack = np.zeros((self.nbands * ny, nx)) self.scistack = 0. * self.modelstack self.errstack = 0. * self.modelstack self.maskstack = np.zeros((self.nbands * ny, nx), dtype=bool) for i in range(self.nbands): self.scistack[i * ny:(i + 1) * ny, :] = self.sci[self.bands[i]] self.errstack[i * ny:(i + 1) * ny, :] = self.err[self.bands[i]] self.maskstack[i * ny:(i + 1) * ny, :] = mask self.maskstack_r = self.maskstack.ravel() #defines model parameters ncomp = 0 npar = 0 for comp in config['light_components']: ncomp += 1 for par in comp['pars']: parpar = comp['pars'][par] if parpar['link'] is None and parpar['var'] == 1: self.pars.append( Par(par + str(ncomp), lower=parpar['low'], upper=parpar['up'], value=parpar['value'], step=parpar['step'])) self.par2index['light' + str(ncomp) + '.' + par] = npar self.index2par[npar] = 'light' + str(ncomp) + '.' + par npar += 1 ncomp = 0 for comp in config['source_components']: ncomp += 1 for par in comp['pars']: parpar = comp['pars'][par] if parpar['link'] is None and parpar['var'] == 1: self.pars.append( Par(par + str(ncomp), lower=parpar['low'], upper=parpar['up'], value=parpar['value'], step=parpar['step'])) self.par2index['source' + str(ncomp) + '.' + par] = npar self.index2par[npar] = 'source' + str(ncomp) + '.' + par npar += 1 ncomp = 0 for comp in config['lens_components']: ncomp += 1 for par in comp['pars']: parpar = comp['pars'][par] if parpar['link'] is None and parpar['var'] == 1: self.pars.append( Par(par + str(ncomp), lower=parpar['low'], upper=parpar['up'], value=parpar['value'], step=parpar['step'])) self.par2index['lens' + str(ncomp) + '.' + par] = npar self.index2par[npar] = 'lens' + str(ncomp) + '.' + par npar += 1 i = 0 filtnames = {} for band in config['filters']: filtname = config['filter_prefix'] + band + config['filter_suffix'] filtnames[band] = filtname ncomp = 1 for comp in config['lens_components']: name = 'lens%d' % ncomp pars_here = {} for par in comp['pars']: if comp['pars'][par]['link'] is None: if comp['pars'][par]['var'] == 1: pars_here[par] = self.pars[self.par2index[name + '.' + par]] elif comp['pars'][par]['var'] == 0: pars_here[par] = Par(par, lower=comp['pars'][par]['value'], upper=comp['pars'][par]['value'], value=comp['pars'][par]['value']) else: df else: pars_here[par] = self.pars[self.par2index[comp['pars'][par] ['link']]] ncomp += 1 lens = MassModels.PowerLaw(name, pars_here) self.lens_models.append(lens) self.nlens = len(self.lens_models) ncomp = 1 for comp in config['light_components']: name = 'light%d' % ncomp pars_here = {} sed_here = {} for par in comp['pars']: if par in SBModels.parlists[comp['class']]: if comp['pars'][par]['link'] is None: if comp['pars'][par]['var'] == 1: pars_here[par] = self.pars[self.par2index[name + '.' + par]] else: pars_here[par] = Par( par, lower=comp['pars'][par]['value'], upper=comp['pars'][par]['value'], value=comp['pars'][par]['value']) else: pars_here[par] = self.pars[self.par2index[ comp['pars'][par]['link']]] else: if comp['pars'][par]['link'] is None: if comp['pars'][par]['var'] == 1: sed_here[par] = self.pars[self.par2index[name + '.' + par]] else: sed_here[par] = Par( par, lower=comp['pars'][par]['value'], upper=comp['pars'][par]['value'], value=comp['pars'][par]['value']) else: sed_here[par] = self.pars[self.par2index[ comp['pars'][par]['link']]] if comp['class'] == 'Sersic': light = SBModels.Sersic('light%d' % ncomp, pars_here) elif comp['class'] == 'PointSource': light = SBModels.PointSource('light%d' % ncomp, psfdic, pars_here) else: df self.light_sb_models.append(light) if comp['sed'] == 'colorpars': sed = SEDModels.Colors('light_sed%d' % ncomp, sed_here, self.bands, self.reference_band, self.zp) elif comp['sed'] == 'template': sed = SEDModels.Template('light_sed%d' % ncomp, sed_here, filtnames, self.zp) elif comp['sed'] == 'sps': modelname = config['sps_model_dir'] + comp['sps_model'] sed = SEDModels.SPS('light_sed%d' % ncomp, sed_here, self.bands, self.zp, modelname) else: df self.light_sed_models.append(sed) self.light_mags.append({}) ncomp += 1 self.nlight = len(self.light_sb_models) ncomp = 1 for comp in config['source_components']: name = 'source%d' % ncomp pars_here = {} sed_here = {} for par in comp['pars']: if par in SBModels.parlists['Sersic']: if comp['pars'][par]['link'] is None: if comp['pars'][par]['var'] == 1: pars_here[par] = self.pars[self.par2index[name + '.' + par]] else: pars_here[par] = comp['pars'][par]['value'] else: pars_here[par] = self.pars[self.par2index[ comp['pars'][par]['link']]] else: if comp['pars'][par]['link'] is None: if comp['pars'][par]['var'] == 1: sed_here[par] = self.pars[self.par2index[name + '.' + par]] else: sed_here[par] = Par( par, lower=comp['pars'][par]['value'], upper=comp['pars'][par]['value'], value=comp['pars'][par]['value']) else: sed_here[par] = self.pars[self.par2index[ comp['pars'][par]['link']]] if comp['class'] == 'Sersic': source = SBModels.Sersic('source%d' % ncomp, pars_here) else: df self.source_sb_models.append(source) if comp['sed'] == 'colorpars': sed = SEDModels.Colors('light_sed%d' % ncomp, sed_here, self.bands, self.reference_band, zp=self.zp) elif comp['sed'] == 'template': sed = SEDModels.Template('light_sed%d' % ncomp, sed_here, filtnames, zp=self.zp) self.source_sed_models.append(sed) self.source_mags.append({}) ncomp += 1 self.nsource = len(self.source_sb_models)