-
Notifications
You must be signed in to change notification settings - Fork 16
/
FastLensSim.py
372 lines (306 loc) · 13.8 KB
/
FastLensSim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import indexTricks as iT
import numpy
from pylens import *
from imageSim import profiles,convolve,SBModels
import distances as D
import cPickle
import indexTricks as iT
import numpy,pylab
from imageSim import profiles,convolve,models
import pylab as plt
from Surveys import Survey
from StochasticObserving import SO
from SignaltoNoise import S2N
class FastLensSim(SO,S2N):
def __init__(self,surveyname,fractionofseeing=1):
#-----------------------------------------------------
### Read in survey
self.surveyname=surveyname
survey=Survey(surveyname)#This stores typical survey in Surveys.Survey
self.survey=survey
self.pixelsize=survey.pixelsize
self.side=survey.side
self.readnoise=survey.readnoise
self.nexposures=survey.nexposures
self.f_sky=survey.f_sky
self.bands=survey.bands
self.strategy=survey.strategy
self.strategyx=survey.strategyx
self.exposuretimes={}
self.zeropoints={}
self.stochasticobservingdata={}
self.gains={}
self.seeing={}
self.psfscale={}
self.psf={}
self.psfFFT={}
self.ET={}
self.SB={}
for i in range(len(survey.bands)):
self.exposuretimes[survey.bands[i]]=survey.exposuretimes[i]
self.zeropoints[survey.bands[i]]=survey.zeropoints[i]
self.gains[survey.bands[i]]=survey.gains[i]
self.stochasticobservingdata[survey.bands[i]]=survey.stochasticobservingdata[i]
self.zeroexposuretime=survey.zeroexposuretime
#-----------------------------------------------------
###do some setup
self.xl=(self.side-1.)/2.
self.yl=(self.side-1.)/2.
self.x,self.y = iT.coords((self.side,self.side))
self.r2 = (self.x-self.xl)**2+(self.y-self.yl)**2
self.pixelunits=False
#-----------------------------------------------------
self.Reset()
def Reset(self):
self.sourcenumbers=[]
#Some objects that need pre-defining as dictionaries
self.magnification={}
self.image={}
self.sigma={}
self.residual={}
self.zeromagcounts={}
self.xs={}
self.ys={}
self.ms={}
self.qs={}
self.ps={}
self.rs={}
self.ns={}
self.bl={}
self.src={}
self.galmodel={}
self.sourcemodel={}
self.model={}
self.totallensedsrcmag={}
self.fakeLens={}
self.image={}
self.sigma={}
self.fakeResidual={}
self.fakeResidual[0]={}
self.SN={}
self.SNRF={}
self.convolvedsrc={}
self.convolvedgal={}
#===========================================================================
def trytoconvert(self,par,p):
try:return par/p
except NameError:print "warning one of the parameters is not defined"
#===========================================================================
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 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 lensASource(self,sourcenumber,bands):
src=self.src[sourcenumber]
lens=massmodel.PowerLaw('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'q':self.ql,'pa':90+self.deltap,'b':self.bl[sourcenumber],'eta':1})
es=massmodel.ExtShear('lens',{},{'x':self.xl+self.deltaxl,'y':self.yl+self.deltayl,'pa':self.xp,'b':self.xb})
lenses=[lens,es]
a=51
ox,oy=iT.coords((a,a))
ps=(self.rs[sourcenumber]*(10./a))
ox=(ox-(a-1)/2.)*ps+(self.xs[sourcenumber])
oy=(oy-(a-1)/2.)*ps+(self.ys[sourcenumber])
unlensedsrcmodel=(src.pixeval(ox,oy,csub=5)*(ps**2)).sum()
srcnorm=unlensedsrcmodel.sum()
unlensedsrcmodel/=srcnorm
srcmodel=pylens.lens_images(lenses,src,[self.x,self.y],getPix=True,csub=5)[0]
srcmodel[srcmodel<0]=0
srcmodel/=srcnorm
self.magnification[sourcenumber]=(numpy.sum(numpy.ravel(srcmodel))/numpy.sum(numpy.ravel(unlensedsrcmodel)))
sm={}
for band in bands:
unlensedtotalsrcflux=10**(-(self.ms[sourcenumber][band]-self.zeropoints[band])/2.5)
sm[band]=srcmodel*unlensedtotalsrcflux
if sm[band].max()>0:
self.totallensedsrcmag[sourcenumber][band]=-2.5*numpy.log10(sm[band].sum())+self.zeropoints[band]
else:
self.totallensedsrcmag[sourcenumber][band]=99
return sm
#===========================================================================
def EvaluateGalaxy(self,light,mag,bands):
model={}
lightm=light.pixeval(self.x,self.y,csub=5)
lightm[lightm<0]=0
lightm/=lightm.sum()
for band in bands:
flux=10**(-(mag[band]-self.zeropoints[band])/2.5)
model[band]=lightm*flux
return model
#===========================================================================
def MakeModel(self, bands):
#did you know that self.gal is actually fixed for all bands currently?
self.galmodel=self.EvaluateGalaxy(self.gal,self.ml,bands)
for sourcenumber in self.sourcenumbers:
self.sourcemodel[sourcenumber]=self.lensASource(sourcenumber,bands)
for band in bands:
self.model[band]=self.galmodel[band]*1
for sourcenumber in self.sourcenumbers:
self.model[band]+=self.sourcemodel[sourcenumber][band]
#===========================================================================
def ObserveLens(self,noisy=True,bands=[]):
if bands==[]:bands=self.bands
for band in bands:
if self.seeing[band]!=0:
convolvedgal,self.psfFFT[band] = convolve.convolve(self.galmodel[band],self.psf[band],True)
convolvedgal[convolvedgal<0]=0
self.convolvedgal[band]=convolvedgal
convolvedmodel=convolvedgal*1
convolvedsrc={}
for sourcenumber in self.sourcenumbers:
convolvedsrc[sourcenumber]=convolve.convolve(self.sourcemodel[sourcenumber][band],self.psfFFT[band],False)[0]
convolvedsrc[sourcenumber][convolvedsrc[sourcenumber]<0]=0
self.convolvedsrc[sourcenumber][band]=convolvedsrc[sourcenumber]
convolvedmodel+=convolvedsrc[sourcenumber]
self.zeromagcounts[band]=(10**(-(0-self.zeropoints[band])/2.5))
exposurecorrection=((self.ET[band]*1./self.zeroexposuretime))*self.gains[band]
convolvedmodel*=exposurecorrection
#skybackground per second per square arcsecond
background=(10**(-(self.SB[band]-self.zeropoints[band])/2.5))*(self.pixelsize**2)
tot_bg=background*exposurecorrection
sigma=((convolvedmodel+tot_bg)+self.nexposures*(self.readnoise**0.5)**2)**.5
fakeLens=convolvedmodel*1.
if noisy:fakeLens+=(numpy.random.randn(self.side,self.side)*(sigma))
#convert back to ADU/second:
fakeLens/=exposurecorrection
sigma/=exposurecorrection
self.image[band]=fakeLens*1
self.fakeLens[band]=fakeLens*1
self.sigma[band]=sigma*1
self.fakeResidual[0][band]=fakeLens-convolvedgal
for sourcenumber in self.sourcenumbers:
self.SN[sourcenumber][band]=self.SNfunc(\
convolvedsrc[sourcenumber],sigma)
self.fakeResidual[sourcenumber][band]=\
fakeLens-convolvedmodel+convolvedsrc[sourcenumber]
#===========================================================================
def loadModel(self,ideallens):
self.galmodel,self.sourcemodel,self.model,self.magnification,self.totallensedsrcmag=ideallens
self.image=self.model
#===========================================================================
def loadConvolvedModel(self,ideallens):
self.galmodel,self.sourcemodel,self.model,self.magnification,self.totallensedsrcmag=ideallens
self.image=self.model
#===========================================================================
def makeLens(self,stochastic=True,save=False,noisy=True,stochasticmode="MP",SOdraw=[],bands=[],musthaveallbands=False,MakeModel=True):
if stochastic==True:self.stochasticObserving(mode=stochasticmode,SOdraw=SOdraw,musthaveallbands=musthaveallbands)
if self.seeingtest=="Fail":return None
if bands==[]:bands=self.bands
if MakeModel:
self.MakeModel(bands)
if self.strategy=="resolve":
if stochastic==True:self.stochasticObserving(mode=stochasticmode,SOdraw=SOdraw) #have to rerun stochastic observing now we know the magnification
self.ObserveLens(noisy=noisy)
return [self.galmodel,self.sourcemodel,self.model,self.magnification,self.totallensedsrcmag]
#===========================================================================
def makeColorLens(self,bands=["g_SDSS","r_SDSS","i_SDSS"],recolourize=True):
if self.surveyname=="Euclid" and bands==["g_SDSS","r_SDSS","i_SDSS"]:
bands=["VIS","VIS","VIS"]
import colorImage
goodbands=[]
for band in bands:
try:
self.image[band]
goodbands.append(band)
except KeyError:
pass
bands=goodbands
if len(bands)==1:
bands=[bands[0],bands[0],bands[0]]
if len(bands)==2:
bands=[bands[0],"dummy",bands[1]]
self.ml["dummy"]=(self.ml[bands[0]]+self.ml[bands[2]])/2
self.image["dummy"]=(self.image[bands[0]]+self.image[bands[2]])/2
if recolourize:
self.color = colorImage.ColorImage()
self.color.bMinusr=(self.ml[bands[0]]-self.ml[bands[2]])/4.
self.color.bMinusg=(self.ml[bands[0]]-self.ml[bands[1]])/4.
self.color.nonlin=4.
self.colorimage = self.color.createModel(\
self.image[bands[0]],self.image[bands[1]],self.image[bands[2]])
else:
self.colorimage = self.color.colorize(\
self.image[bands[0]],self.image[bands[1]],self.image[bands[2]])
return self.colorimage
#===========================================================================
def display(self,band="g_SDSS",bands=["g_SDSS","r_SDSS","i_SDSS"]):
if self.surveyname=="Euclid":bands=["VIS","VIS","VIS"]
import pylab as plt
plt.ion()
plt.figure(1)
plt.imshow(self.makeColorLens(bands=bands),interpolation="none")
plt.figure(2)
import colorImage
self.color = colorImage.ColorImage()#sigma-clipped single band residual
plt.imshow(self.color.createModel(self.fakeResidual[0][band],self.fakeResidual[0][band],self.fakeResidual[0][band])[:,:,0],interpolation="none")
plt.figure(3)
plt.imshow(self.fakeResidual[0][band],interpolation="none")
try:
self.fakeResidual[1]["RF"]
plt.figure(4)
plt.imshow(self.fakeResidual[1]["RF"],interpolation="none")
except KeyError: pass
plt.draw()
raw_input()
plt.ioff()
#===========================================================================
def Rank(self,mode,band="g_SDSS",bands=["g_SDSS","r_SDSS","i_SDSS"]):
import pylab as plt
plt.ion()
rank="d"
while rank not in ["0","1","2","3","4","-1","-2","-3"]:
if mode=="colour":
plt.imshow(self.makeColorLens(bands=bands),interpolation="none")
plt.draw()
if mode=="rf":
plt.imshow(self.fakeResidual[0]["RF"],interpolation="none")
plt.draw()
if mode=="best":
plt.imshow(self.fakeResidual[0][band],interpolation="none")
plt.draw()
rank=raw_input()
if rank=="":rank="0"
plt.ioff()
return rank
#===========================================================================