-
Notifications
You must be signed in to change notification settings - Fork 1
/
cosmolike_libs.py
executable file
·402 lines (336 loc) · 12.8 KB
/
cosmolike_libs.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
import emcee
import ctypes
import os
import numpy as np
# from mpp_blinding import blind_parameters
# from mpp_blinding import seed as blinding_seed
dirname = os.path.dirname(os.path.abspath(__file__))
lib_name = os.path.join(dirname,"./like_fourier.so")
lib=ctypes.cdll.LoadLibrary(lib_name)
double = ctypes.c_double
Double10 = double*10
initcosmo=lib.init_cosmo
initcosmo.argtypes=[]
initbins=lib.init_binning_fourier
initbins.argtypes=[ctypes.c_int, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_double, ctypes.c_int]
initsurvey=lib.init_survey
initsurvey.argtypes=[ctypes.c_char_p]
initgalaxies=lib.init_galaxies
initgalaxies.argtypes=[ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
initclusters=lib.init_clusters
initclusters.argtypes=[]
initia=lib.init_IA
initia.argtypes=[ctypes.c_char_p,ctypes.c_char_p]
initpriors=lib.init_priors
initpriors.argtypes=[ctypes.c_char_p,ctypes.c_char_p,ctypes.c_char_p]
initprobes=lib.init_probes
initprobes.argtypes=[ctypes.c_char_p]
initdatainv=lib.init_data_inv
initdatainv.argtypes=[ctypes.c_char_p,ctypes.c_char_p]
get_N_tomo_shear = lib.get_N_tomo_shear
get_N_tomo_shear.argtypes = []
get_N_tomo_shear.restype = ctypes.c_int
get_N_tomo_clustering = lib.get_N_tomo_clustering
get_N_tomo_clustering.argtypes = []
get_N_tomo_clustering.restype = ctypes.c_int
get_N_ggl = lib.get_N_ggl
get_N_ggl.argtypes = []
get_N_ggl.restype = ctypes.c_int
get_N_ell = lib.get_N_ell
get_N_ell.argtypes = []
get_N_ell.restype = ctypes.c_int
# lib.initialize_all_wrapper.restype = ctypes.c_int
# lib.initialize_all_wrapper.argtypes = [
# ctypes.c_char_p, # const char * base_dir,
# ctypes.c_bool, # bool auborg_prior,
# ctypes.c_bool, # bool photo_bao_prior,
# ctypes.c_bool, # bool ia_datavector,
# ctypes.c_char_p, # char * ia_model, // must be either "none", "NLA_HF"
# ctypes.c_char_p, # char * ia_luminosity_function // should be either "GAMA", "DEEP2"
# ctypes.c_bool, # bool modify_shear_priors,
# Double10, # double shear_m_mean[10],
# Double10, # double shear_m_var[10],
# ctypes.c_bool, # bool modify_photoz_priors,
# Double10, # double photoz_source_bias_mean[10],
# double, # double photoz_source_sigma_mean,
# Double10, # double photoz_source_bias_var[10],
# double, # double photoz_source_sigma_var,
# ctypes.c_char_p, # source_filename
# ctypes.c_char_p, # lens_filename
# ]
# initialize_all_wrapper=lib.initialize_all_wrapper
class IterableStruct(ctypes.Structure):
def names(self):
out = []
for name, obj, length in self.iter_parameters():
if length==0:
out.append(name)
else:
for i in xrange(length):
out.append(name + "_" + str(i))
return out
def iter_parameters(self):
for name,ptype in self._fields_:
obj = getattr(self, name)
if hasattr(ptype, "_length_"):
yield name, obj, ptype._length_
else:
yield name, obj, 0
def iter_parameters_filter(self, used):
for (name, obj, length) in self.iter_parameters():
if name in used:
yield name, obj, 0
def convert_to_vector(self):
p = []
for name, obj, length in self.iter_parameters():
if length==0:
p.append(obj)
else:
for i in xrange(length):
p.append(obj[i])
return p
def convert_to_vector_filter(self, used):
p = []
for name, obj, length in self.iter_parameters():
if length==0:
if name in used:
p.append(obj)
else:
for i in xrange(length):
if name+'_'+str(i) in used:
p.append(obj[i])
return p
def read_from_cosmosis(self, block):
for name,ptype in self._fields_:
obj = getattr(self, name)
if hasattr(ptype, "_length_"):
for i in xrange(ptype._length_):
obj[i] = block[self.section_name, name+"_"+str(i)]
else:
setattr(self, name, block[self.section_name, name])
def print_struct(self):
for name,ptype in self._fields_:
obj = getattr(self, name)
if hasattr(ptype, "_length_"):
for i in xrange(ptype._length_):
print "%s[%d] = %f" % (name, i, obj[i])
else:
print "%s = %f" % (name, obj)
def number_of_doubles(self):
n=0
for name, ptype in self._fields_:
if hasattr(ptype, "_length_"):
n += ptype._length_
else:
n += 1
return n
def set_from_vector(self, p):
i=0
j=0
while i<len(p):
name,ptype = self._fields_[j]
j+=1
if ptype == double:
setattr(self, name, p[i])
i+=1
else:
x = getattr(self, name)
assert x._type_==double
for k in xrange(x._length_):
x[k] = p[i]
i+=1
class InputCosmologyParams(IterableStruct):
section_name = "cosmological_parameters"
_fields_ = [
("omega_m", double),
("sigma_8", double),
("n_s", double),
("w0", double),
("wa", double),
("omega_b", double),
("h0", double),
("MGSigma", double),
("MGmu", double),
]
@classmethod
def fiducial(cls):
c = cls()
c.omega_m = 0.3156
c.sigma_8 = 0.831
c.n_s = 0.9645
c.w0 = -1.0
c.wa = 0.0
c.omega_b = 0.0491685
c.h0 = 0.6727
c.MGSigma = 0.0
c.MGmu = 0.0
return c
@classmethod
def fiducial_sigma(cls):
c = cls()
c.omega_m = 0.01
c.sigma_8 = 0.01
c.n_s = 0.01
c.w0 = .02
c.wa = 0.02
c.omega_b = 0.001
c.h0 = 0.01
c.MGSigma = 0.1
c.MGmu = 0.1
return c
class InputNuisanceParams(IterableStruct):
section_name = "nuisance_parameters"
_fields_ = [
("bias", double*4),
("source_z_bias", double*10),
("source_z_s", double),
("lens_z_bias", double*10),
("lens_z_s", double),
("shear_m", double*10),
("A_ia", double),
("beta_ia", double),
("eta_ia", double),
("eta_ia_highz", double),
("lf", double*6),
("m_lambda", double*4),
("cluster_c", double*4),
]
@classmethod
def fiducial(cls):
c = cls()
c.lens_z_s = 0.01
c.source_z_s = 0.05
c.bias[:] = [1.35, 1.5, 1.65, 1.8]
c.A_ia = 5.92
c.beta_ia = 1.1
c.eta_ia = -0.47
c.m_lambda[:] = [33.6, 1.08, 0.0, 0.25]
c.cluster_c[:] = [0.9, 0.9, 0.9, 0.9]
return c
@classmethod
def fiducial_sigma(cls):
c = cls()
c.lens_z_s = 0.0005
c.source_z_s = 0.001
c.bias[:] = [0.01, 0.01, 0.01, 0.01]
c.shear_m[:] = np.repeat(0.0001, 10)
c.source_z_bias[:] = np.repeat(0.001, 10)
c.lens_z_bias[:] = np.repeat(0.0005, 10)
c.A_ia = 0.05
c.beta_ia = 0.01
c.eta_ia = 0.01
c.eta_ia_highz = 0.01
c.lf[:] = np.repeat(0.005, 6)
c.m_lambda[:] = [0.05, 0.01, 0.01, 0.01]
c.cluster_c[:] = [0.01, 0.01, 0.01, 0.01]
return c
class LikelihoodFunctionWrapper(object):
def __init__(self, varied_parameters):
self.varied_parameters = varied_parameters
def fill_varied(self, icp, inp, x):
assert len(x) == len(self.varied_parameters), "Wrong number of parameters"
i = 0
for s in [icp, inp]:
for name, obj, length in s.iter_parameters():
if length==0:
if name in self.varied_parameters:
setattr(s, name, x[i])
i+=1
else:
for j in xrange(length):
name_i = name + "_" + str(j)
if name_i in self.varied_parameters:
obj[j] = x[i]
i+=1
def __call__(self, x):
icp = InputCosmologyParams.fiducial()
inp = InputNuisanceParams.fiducial()
self.fill_varied(icp, inp, x)
#icp.print_struct()
#inp.print_struct()
#print
like = lib.log_like_wrapper(icp, inp)
return like
lib.log_like_wrapper.argtypes = [InputCosmologyParams, InputNuisanceParams]
lib.log_like_wrapper.restype = double
log_like_wrapper = lib.log_like_wrapper
def sample_cosmology_only(MG = False):
if MG:
varied_parameters = InputCosmologyParams().names()
else:
varied_parameters = ['omega_m']
varied_parameters.append('sigma_8')
varied_parameters.append('n_s')
varied_parameters.append('w0')
varied_parameters.append('wa')
varied_parameters.append('omega_b')
varied_parameters.append('h0')
return varied_parameters
def sample_cosmology_shear_nuisance(tomo_N_shear,MG = False):
varied_parameters = sample_cosmology_only(MG)
varied_parameters += ['shear_m_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['source_z_bias_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters.append('source_z_s')
return varied_parameters
def sample_cosmology_2pt_nuisance(tomo_N_shear,tomo_N_lens,MG = False):
varied_parameters = sample_cosmology_only(MG)
varied_parameters += ['shear_m_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['source_z_bias_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['lens_z_bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters += ['bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters.append('source_z_s')
varied_parameters.append('lens_z_s')
return varied_parameters
def sample_cosmology_2pt_nuisance_IA_marg(tomo_N_shear,tomo_N_lens,MG = False):
varied_parameters = sample_cosmology_only(MG)
varied_parameters += ['shear_m_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['source_z_bias_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['lens_z_bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters += ['bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters.append('source_z_s')
varied_parameters.append('lens_z_s')
varied_parameters.append('A_ia')
varied_parameters.append('beta_ia')
varied_parameters.append('eta_ia')
varied_parameters.append('eta_ia_highz')
varied_parameters += ['lf_%d'%i for i in xrange(6)]
return varied_parameters
def sample_cosmology_2pt_cluster_nuisance(tomo_N_shear,tomo_N_lens,MG = False):
if MG:
print "sample_cosmology_2pt_cluster_nuisance: MG = True not yet supported for clusters"
os.exit()
varied_parameters = sample_cosmology_only(MG)
varied_parameters += ['shear_m_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['source_z_bias_%d'%i for i in xrange(tomo_N_shear)]
varied_parameters += ['lens_z_bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters += ['bias_%d'%i for i in xrange(tomo_N_lens)]
varied_parameters.append('source_z_s')
varied_parameters.append('lens_z_s')
varied_parameters += ['m_lambda_%d'%i for i in xrange(4)]
varied_parameters += ['cluster_c_%d'%i for i in xrange(4)]
return varied_parameters
def sample_main(varied_parameters, iterations, nwalker, nthreads, filename, blind=False):
print varied_parameters
likelihood = LikelihoodFunctionWrapper(varied_parameters)
starting_point = InputCosmologyParams.fiducial().convert_to_vector_filter(varied_parameters)
starting_point += InputNuisanceParams().fiducial().convert_to_vector_filter(varied_parameters)
std = InputCosmologyParams.fiducial_sigma().convert_to_vector_filter(varied_parameters)
std += InputNuisanceParams().fiducial_sigma().convert_to_vector_filter(varied_parameters)
p0 = emcee.utils.sample_ball(starting_point, std, size=nwalker)
ndim = len(starting_point)
print "ndim = ", ndim
print "start = ", starting_point
print "std = ", std
sampler = emcee.EnsembleSampler(nwalker, ndim, likelihood,threads=nthreads)
f = open(filename, 'w')
#write header here
f.write('# ' + ' '.join(varied_parameters)+"\n")
f.write('#blind=%s\n'%blind)
if blind:
f.write('#blinding_seed=%d\n'%blinding_seed)
for (p, loglike, state) in sampler.sample(p0,iterations=iterations):
for row in p:
if blind:
row = blind_parameters(varied_parameters, row)
f.write('%s\n' % (' '.join([str(r) for r in row])))
f.close()