-
Notifications
You must be signed in to change notification settings - Fork 10
/
Magnifier.py
executable file
·418 lines (304 loc) · 14.9 KB
/
Magnifier.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/usr/bin/env python
# ======================================================================
import pangloss
import sys,getopt,cPickle,numpy,glob
import matplotlib.pyplot as plt
from scipy.stats.kde import gaussian_kde
from math import pi
from astropy.io import ascii
# ======================================================================
def Magnifier(argv):
"""
NAME
Magnifier.py
PURPOSE
Read in a simulation lightcone (or list of lightcones) and compute all
quantities needed to estimate mu, the magnification along the LoS to a
background source. This file create p(mu) for all lines of sight, and
also p(mu) for observed fields which are matched by overdensity (or
simply for LoS with a given overdensity, not necessarily real ones)
Requires extra inputs in the config file, detailed below
COMMENTS
The config file contains the list of lightcones to be
reconstructed, in the form of either a directory or a single
instance. If a directory is specified, one also gives a number
("batchsize") of lightcones to be reconstructed at a time.
FLAGS
-h Print this message [0]
-c, --contributions Plot cumulative contributions
INPUTS
configfile Plain text file containing Pangloss configuration
additional configfile fields:
FieldName Plain text or csv file containing list of field
names, can be None
FieldOverdensity Plain text or csv file containing list of field
overdensities. Must be in same order as field
names, can be None
If FieldName is none, FieldOverdensity can be a list
of overdensities, e.g. [0.75, 1.0, 1.25] for testing
OUTPUTS
stdout Useful information
samples Catalog(s) of samples from Pr(kappah|D)
EXAMPLE
Magnifier.py --contributions example.config
BUGS
Plotting contributions is SLOW
AUTHORS
This file is part of the Pangloss project, distributed under the
GPL v2, by Tom Collett (IoA) and Phil Marshall (Oxford). This
Magnifier code was developed by Charlotte Mason (UCSB)
Please cite: Collett et al. 2013, http://arxiv.org/abs/1303.6564
Mason et al. 2015, http://arxiv.org/abs/1502.03795
HISTORY
2013-03-21 started Collett & Marshall (Oxford)
2014-04-09 modified for BoRG, C Mason (UCSB)
"""
# --------------------------------------------------------------------
try:
opts, args = getopt.getopt(argv,"hc",["help","contributions"])
except getopt.GetoptError, err:
print str(err) # will print something like "option -a not recognized"
print Magnifier.__doc__ # will print the big comment above.
return
for o,a in opts:
if o in ("-h", "--help"):
print "HELP!"
print Magnifier.__doc__
return
elif o in ("-c", "--contributions"):
print "Magnifier: plotting cumulative contributions"
plot_contributions = True
else:
assert False, "unhandled option"
# Check for setup file in array args:
print args
print opts
if len(args) == 1:
configfile = args[0]
print pangloss.doubledashedline
print pangloss.hello
print pangloss.doubledashedline
print "Magnifier: generating magnification PDFs for lightcones of given density"
print "Magnifier: taking instructions from",configfile
else:
print "Magnifier: are you sure you put the options before the config file?"
print Magnifier.__doc__
return
# ==============================================================
# Read in configuration, and extract the ones we need:
# ==============================================================
experiment = pangloss.Configuration(configfile)
# Get the experiment name from the configfile name instead?
EXP_NAME = experiment.parameters['ExperimentName']
CALIB_DIR = experiment.parameters['CalibrationFolder'][0]
mag = experiment.parameters['MagName']
Ncats = 21
field_name = experiment.parameters['FieldName']
field_overdensity = experiment.parameters['FieldOverdensity']
if field_name == None:
field_name = ''
else:
field_name = numpy.genfromtxt(str(field_name), comments='#', usecols=0, dtype='S30')
if len(field_overdensity)==1: field_overdensity = numpy.genfromtxt(str(field_overdensity), comments='#')
Rc = experiment.parameters['LightconeRadius'] # in arcmin
zd = experiment.parameters['StrongLensRedshift']
zs = experiment.parameters['SourceRedshift']
# --------------------------------------------------------------------
# Load the lightcone pickles
calpickles = []
Nc = experiment.parameters['NCalibrationLightcones'] * Ncats ### should be 24
paths = '%s/*_lightcone.pickle' % (CALIB_DIR)
found = glob.glob(paths)
if len(found) > 0: calpickles = found
print "Magnifier: found the lightcones..."
# Ray tracing:
RTscheme = experiment.parameters['RayTracingScheme']
# Reconstruct calibration lines of sight?
DoCal = experiment.parameters['ReconstructCalibrations']
# --------------------------------------------------------------------
# Make redshift grid:
grid = pangloss.Grid(zd,zs,nplanes=100)
# --------------------------------------------------------------------
# Read in lightcones from pickles:
calcones = []
for i in xrange(Nc):
calcones.append(pangloss.readPickle(calpickles[i]))
if i==0: print calpickles[i]
if DoCal=="False": #must be string type
calcones=[]
calpickles=[]
allcones = calcones
allconefiles = calpickles
# ==============================================================
# Find the overdensity of lightcones cut at m<22 in F125W
# ==============================================================
print "Magnifier: finding the distribution of lightcones with density..."
lc_dens = []
lc_galaxies = []
total_galaxies = 0
# Sort into lightcones for each field
for i in xrange(Nc):
lc = pangloss.readPickle(calpickles[i])
num_galaxies = lc.numberWithin(radius=Rc,cut=[16,22],band=mag,units="arcmin")
lc_galaxies.append(num_galaxies)
# Add to the total number of galaxies
total_galaxies += num_galaxies
del lc
lc_dens = [Nc * float(x)/float(total_galaxies) for x in lc_galaxies]
numpy.savetxt(CALIB_DIR+"/lc_density.txt", lc_dens)
print 'Mean overdensity in all fields = %.3f (this should =1)' % numpy.mean(lc_density)
print 'Lightcone overdensities saved to file'
print pangloss.dashedline
del lc_dens
del lc_density
# ==============================================================
# Sample all lightcones to make the pdfs
# ==============================================================
# --------------------------------------------------------------
# Set up overdensity range
density = field_overdensity
drange = 0.02 # ~0.02 is what Zach used
# --------------------------------------------------------------------
# Find contribution to total kappa and mass at redshift intervals
zmax = zs+0.1
zbin = 25
zbins = numpy.linspace(0.0,zmax,zbin)
kappa_cont = numpy.zeros(Nc, zbin)
Mh_cont = numpy.zeros(Nc, zbin)
Mstell_cont = numpy.zeros(Nc, zbin)
# --------------------------------------------------------------------
# Select ALL lightcones and find their convergences
pk = []
pmu =[]
for j in xrange(Nc):
# Get lightcone
lc = pangloss.readPickle(calpickles[j])
# --------------------------------------------------------------------
# Calculate mu and kappa for all lightcones
# Redshift scaffolding:
lc.defineSystem(zd,zs)
lc.loadGrid(grid)
# Figure out data quality etc:
lc.configureForSurvey(experiment)
if j % 1000 == 0 and j !=0:
print ("Magnifier: ...on lightcone %i out of %i..." % (j,Nc))
lc.snapToGrid(grid)
# Draw c from Mhalo:
lc.drawConcentrations(errors=True)
# Compute each halo's contribution to the convergence:
lc.makeKappas(truncationscale=5)
k_add=lc.combineKappas()
mu_add=lc.combineMus(weakapprox=False)
# Add magnification and convergence to global PDF
pmu.append(lc.mu_add_total)
pk.append(lc.kappa_add_total)
if plot_contributions is True:
kappa_cont[j:,] = lc.findContributions('kappa')
Mh_cont[j:,] = lc.findContributions('mass')
Mstell_cont[j:,] = lc.findContributions('stellarmass')
# Make a nice visualisation of one of the lightcones
if j ==0:
lc.plots('kappa', output=CALIB_DIR+"/example_snapshot_kappa_uncalib_z=1.4.png")
lc.plots('mu', output=CALIB_DIR+"/example_snapshot_mu_uncalibz=1.4.png")
del lc
pk = numpy.array(pk)
pmu = numpy.array(pmu)
# --------------------------------------------------------------------
# Write PDFs to pickles
pangloss.writePickle(pk,CALIB_DIR+"/Pofk_z="+str(zs)+".pickle")
pangloss.writePickle(pmu,CALIB_DIR+"/PofMu_z="+str(zs)+".pickle")
del pk
del pmu
print "Magnifier: saved PofMu to "+CALIB_DIR+"/Pofk_z="+str(zs)+".pickle"
pmu = pangloss.readPickle(CALIB_DIR+"/PofMu_z="+str(zs)+".pickle")
pk = pangloss.readPickle(CALIB_DIR+"/Pofk_z="+str(zs)+".pickle")
# --------------------------------------------------------------------
# Plot contributions to total kappa and mass at redshifts
if plot_contributions:
mean_kappa_cont = numpy.mean(kappa_cont, axis=0)
mean_Mh_cont = numpy.mean(Mh_cont, axis=0)
mean_Mstell_cont = numpy.mean(Mstell_cont, axis=0)
plt.clf()
plt.figure()
ax1 = plt.subplot(2,1,1)
ax1.plot(zbins, mean_Mh_cont)
ax1.set_ylabel(r'Cumulative Sum of $M_h/M_{\odot}$')
ax2 = plt.subplot(2,1,2)
ax2.plot(zbins, mean_kappa_cont)
ax2.set_ylabel(r'Cumulative Sum of $\kappa_h$')
plt.tight_layout()
plt.xlabel('Redshift, z')
plt.savefig("figs/"+EXP_NAME+"contribution_z.pdf",dpi=300)
# --------------------------------------------------------------------
# Calculate the smooth components
kappa_smooth = numpy.mean(pk)
mu_smooth = numpy.mean(pmu)
print ' uncalibrated: <kappa> =',kappa_smooth, '<mu> =',mu_smooth
pmu = pmu - mu_smooth + 1.
pk = pk - kappa_smooth + 0.
print ' mean mu now = ', numpy.mean(pmu)
params = [{'param':'Mu', 'name':r'$\mu$', 'lc':pmu, 'smooth':mu_smooth, 'mean':1.0, 'height':30, 'min':0.4, 'max':2.0}]
lc_density = numpy.genfromtxt(CALIB_DIR+"/lc_density.txt", comments="#")
# =====================================================================
# For only <=4 values of density
# =====================================================================
for k in range(len(params)):
var = params[k]
full_pdf = var['lc']
name = var['name']
print 'Magnifier: Old min and max:', full_pdf.min(), full_pdf.max()
# --------------------------------------------------------------------
# Remove outliers !!! I think this is only important for v. high overdensity
if var['param']=='Kappa':
mask = numpy.where(full_pdf==full_pdf)
if var['param']=='Mu':
mask = numpy.where((full_pdf >= 0.) & (full_pdf < 2.))
par = full_pdf[mask]
new_density = lc_density[mask]
print ' Removing Outliers...'
print ' New min and max:', par.min(), par.max()
# Recalculate means and the calibrated pdf
smooth_new = numpy.mean(par)
new_pdf = par - smooth_new + var['mean']
par_mean = numpy.mean(new_pdf)
print ' New mean (this should be',var['mean'],'):', par_mean
# --------------------------------------------------------------------
# Plot all lines of sight
print pangloss.dashedline
print "Magnifier: constructing PDF for", var['param'],"..."
outputfile = CALIB_DIR+"/"+EXP_NAME+"_Pof"+var['param']+"_"+"_z="+str(zs)+"_allLoS.txt"
numpy.savetxt(outputfile, sub_pdf)
print "Magnifier: saved all LoS PDFs to",outputfile
# --------------------------------------------------------------------
# Select only lightcones within certain number density limits
# Need to mask out the outliers
means, fieldname = [], []
for i in range(len(density)):
sub_pdf = []
for j in xrange(len(new_pdf)):
if density[i] - drange <= round(new_density[j],2) <= density[i] + drange:
sub_pdf.append(new_pdf[j])
sub_pdf = numpy.array(sub_pdf)
Nlos = len(sub_pdf)
if Nlos == 0:
print "Magnifier: %s - there are NO LoS with number density ~ %.2f the average" % (field_name[i], density[i])
print "Magnifier: %s - no PDF will be made for this field" % (field_name[i])
else:
sub_mean = numpy.mean(sub_pdf)
print "Magnifier: %s - sampling %i LoS with number density ~ %.2f the average, mean mu=%.2f" % (field_name[i], Nlos, density[i], sub_mean)
if var['param']=='Mu':
numpy.savetxt(CALIB_DIR+"/"+EXP_NAME+str(field_name[i])+"_PofMu.txt", sub_pdf)
means.append(sub_mean)
fieldname.append(field_name[i])
meanmu_table = numpy.array([fieldname, means]).T
ascii.write(meanmu_table, CALIB_DIR+"/"+EXP_NAME+"_table_meanmu.txt", names=['#field','mean_mu'])
print " Mean mu of all the fields = ",numpy.mean(means)
print "Magnifier: saved PDFs to",outputfile
print pangloss.doubledashedline
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
print pangloss.doubledashedline
return
# ======================================================================
if __name__ == '__main__':
Magnifier(sys.argv[1:])
# ======================================================================