@@ -20,9 +20,9 @@ def get_correlation_length_matrix(size, e1, e2):
20
20
:param size: Correlation lenght of the kernel.
21
21
:param e1, e2: Shear applied to isotropic kernel.
22
22
"""
23
- if abs (e1 )> 1 or abs (e2 )> 1 :
24
- raise ValueError ('abs value of e1 and e2 must be lower than one' )
25
23
e = np .sqrt (e1 ** 2 + e2 ** 2 )
24
+ if e > 1 :
25
+ raise ValueError ('magnitude of e must be lower than one' )
26
26
q = (1 - e ) / (1 + e )
27
27
phi = 0.5 * np .arctan2 (e2 ,e1 )
28
28
rot = np .array ([[np .cos (phi ), np .sin (phi )],
@@ -92,15 +92,15 @@ def _model_skl(self, sigma, corr_length, g1, g2):
92
92
from sklearn kernel.
93
93
94
94
:param sigma: Standard deviation of the gaussian random field.
95
- :param corr_length: Correlation lenght of the kernel.
95
+ :param corr_length: Correlation length of the kernel.
96
96
:param g1, g2: Shear applied to isotropic kernel.
97
97
"""
98
- if abs (g1 ) > 1 or abs ( g2 )> 1 :
98
+ if (g1 ** 2 + g2 ** 2 )> 1 :
99
99
return None
100
100
else :
101
101
L = get_correlation_length_matrix (corr_length , g1 , g2 )
102
102
invLam = np .linalg .inv (L )
103
- kernel_used = sigma ** 2 * self .kernel_class (invLam = invLam )
103
+ kernel_used = sklearn . gaussian_process . kernels . ConstantKernel ( sigma ** 2 , constant_value_bounds = "fixed" ) * self .kernel_class (invLam = invLam )
104
104
pcf = kernel_used .__call__ (self .coord ,Y = np .zeros_like (self .coord ))[:,0 ]
105
105
self .kernel_fit = kernel_used
106
106
return pcf
@@ -140,7 +140,6 @@ def _minimize_minuit(self, p0 = [3000., 0.2, 0.2]):
140
140
141
141
:param p0: List of starting points.
142
142
"""
143
- print ('start minimize_minuit' )
144
143
with warnings .catch_warnings ():
145
144
warnings .simplefilter ("ignore" )
146
145
if int (iminuit .__version__ [0 ])>= 2 :
@@ -153,13 +152,11 @@ def _minimize_minuit(self, p0 = [3000., 0.2, 0.2]):
153
152
self .m .migrad ()
154
153
results = [self .m .values [key ] for key in self .m .values .keys ()]
155
154
self ._fit_ok = self .m .migrad_ok ()
156
- print ('results = ' ,results )
157
155
158
156
self ._minuit_result = results
159
157
self .result = [np .sqrt (self .alpha [0 ][0 ]), results [0 ],
160
158
results [1 ], results [2 ],
161
159
self .alpha [1 ][0 ]]
162
- print ('done minimize_minuit' )
163
160
164
161
165
162
def minimize_minuit (self , p0 = [3000. , 0.2 , 0.2 ]):
@@ -190,11 +187,10 @@ def minimize_minuit(self, p0 = [3000., 0.2, 0.2]):
190
187
break
191
188
pcf = self ._model_skl (self .result [0 ], self .result [1 ],
192
189
self .result [2 ], self .result [3 ])
193
- print ('pcf = ' ,pcf )
194
190
195
191
class two_pcf (object ):
196
192
"""
197
- Fit statistical uncertaintie on two-point correlation function using bootstraping.
193
+ Fit statistical uncertainty on two-point correlation function using bootstraping.
198
194
199
195
:param X: Coordinates of the field. (n_samples, 1 or 2)
200
196
:param y: Values of the field. (n_samples)
@@ -252,29 +248,22 @@ def comp_2pcf(self, X, y, y_err):
252
248
:param y: Values of the field. (n_samples)
253
249
:param y_err: Error of y. (n_samples)
254
250
"""
255
- print ("start comp_2pcf" )
256
251
if np .sum (y_err ) == 0 :
257
252
w = None
258
253
else :
259
254
w = 1. / y_err ** 2
260
255
261
256
if self .anisotropic :
262
- print ("anisotropic" )
263
257
cat = treecorr .Catalog (x = X [:,0 ], y = X [:,1 ], k = (y - np .mean (y )), w = w )
264
- print ("KK: " ,self .min_sep ,self .max_sep ,self .nbins )
265
258
kk = treecorr .KKCorrelation (min_sep = self .min_sep , max_sep = self .max_sep , nbins = self .nbins ,
266
- bin_type = 'TwoD' , bin_slop = 0 , verbose = 3 )
267
- print ("made kk" )
259
+ bin_type = 'TwoD' , bin_slop = 0 )
268
260
kk .process (cat )
269
- print ("done process" )
270
- #print("kk.xi = ",kk.xi)
271
261
# Need a mask in the case of the 2D correlation function, to compute
272
262
# the covariance matrix using the bootstrap. The 2D correlation
273
263
# function is symmetric, so just half of the correlation function
274
264
# is useful to compute the covariance matrix. If it is not done,
275
265
# the covariance matrix is consequently not positive definite.
276
266
npixels = len (kk .xi )** 2
277
- #print("npixels = ",npixels)
278
267
mask = np .ones_like (kk .xi , dtype = bool )
279
268
mask = mask .reshape ((int (np .sqrt (npixels )), int (np .sqrt (npixels ))))
280
269
@@ -290,20 +279,16 @@ def comp_2pcf(self, X, y, y_err):
290
279
dx = dy .T
291
280
292
281
distance = np .array ([dx .reshape (npixels ), dy .reshape (npixels )]).T
293
- #print("distance = ",distance)
294
282
Coord = distance
295
283
xi = kk .xi .reshape (npixels )
296
- print ("xi = " ,xi )
297
284
else :
298
285
cat = treecorr .Catalog (x = X [:,0 ], y = X [:,1 ], k = (y - np .mean (y )), w = w )
299
- print ("KK: " ,self .min_sep ,self .max_sep ,self .nbins )
300
286
kk = treecorr .KKCorrelation (min_sep = self .min_sep , max_sep = self .max_sep , nbins = self .nbins )
301
287
kk .process (cat )
302
288
distance = kk .meanr
303
289
mask = np .ones_like (kk .xi , dtype = bool )
304
290
Coord = np .array ([distance ,np .zeros_like (distance )]).T
305
291
xi = kk .xi
306
- print ("xi = " ,xi )
307
292
308
293
return xi , distance , Coord , mask
309
294
@@ -334,9 +319,7 @@ def return_2pcf(self, seed=610639139):
334
319
335
320
:param seed: seed of the random generator.
336
321
"""
337
- print ('start return_2pcf' )
338
322
xi , distance , coord , mask = self .comp_2pcf (self .X , self .y , self .y_err )
339
- #print('xi = ',xi)
340
323
if self .anisotropic :
341
324
# Choice done from Andy Taylor et al. 2012
342
325
# see https://doi.org/10.1093/mnras/stt270
@@ -346,16 +329,12 @@ def f_bias(x, npixel=len(xi[mask])):
346
329
bottom = x - npixel - 2.
347
330
return (top / bottom ) - 2.
348
331
results = optimize .fsolve (f_bias , len (xi [mask ]) + 10 )
349
- #print('results = ',results)
350
332
xi_cov = self .comp_xi_covariance (n_bootstrap = int (results [0 ]), mask = mask , seed = seed )
351
- #print('xi_cov = ',xi_cov)
352
333
bias_factor = (int (results [0 ]) - 1. ) / (int (results [0 ]) - len (xi [mask ]) - 2. )
353
334
xi_weight = np .linalg .inv (xi_cov ) * bias_factor
354
- #print('xi_wt = ',xi_weight)
355
335
else :
356
336
# let like developed initialy for the moment
357
337
xi_weight = np .eye (len (xi )) * 1. / np .var (self .y )
358
- print ('done return_2pcf' )
359
338
return xi , xi_weight , distance , coord , mask
360
339
361
340
def optimizer (self , kernel ):
@@ -365,15 +344,13 @@ def optimizer(self, kernel):
365
344
366
345
:param kernel: sklearn.gaussian_process kernel.
367
346
"""
368
- print ('start optimizer' )
369
347
size_x = np .max (self .X [:,0 ]) - np .min (self .X [:,0 ])
370
348
if self .ndim == 2 :
371
349
size_y = np .max (self .X [:,1 ]) - np .min (self .X [:,1 ])
372
350
rho = float (len (self .X [:,0 ])) / (size_x * size_y )
373
351
if self .ndim == 1 :
374
352
size_y = 0.
375
353
rho = float (len (self .X [:,0 ])) / size_x
376
- print ('rho = ' ,rho )
377
354
# if min_sep is None and isotropic GP, set min_sep to the average separation
378
355
# between data.
379
356
if self .min_sep is not None :
@@ -383,45 +360,36 @@ def optimizer(self, kernel):
383
360
min_sep = 0.
384
361
else :
385
362
min_sep = np .sqrt (1. / rho )
386
- print ('min_sep = ' ,min_sep )
387
363
# if max_sep is None, set max_sep to half of the size of the
388
364
# given field.
389
365
if self .max_sep is not None :
390
366
max_sep = self .max_sep
391
367
else :
392
368
max_sep = np .sqrt (size_x ** 2 + size_y ** 2 )/ 2.
393
- print ('max_sep = ' ,min_sep )
394
369
395
370
self .min_sep = min_sep
396
371
self .max_sep = max_sep
397
372
398
373
xi , xi_weight , distance , coord , mask = self .return_2pcf ()
399
- print ('xi = ' ,xi )
400
374
401
375
def PCF (param , k = kernel ):
402
376
kernel = k .clone_with_theta (param )
403
377
pcf = kernel .__call__ (coord ,Y = np .zeros_like (coord ))[:,0 ]
404
378
return pcf
405
379
406
380
xi_mask = xi [mask ]
407
- print ('xi_mask = ' ,xi_mask )
408
381
def chi2 (param ):
409
382
residual = xi_mask - PCF (param )[mask ]
410
383
return residual .dot (xi_weight .dot (residual ))
411
384
412
- print ('robust_fit? ' ,self .robust_fit )
413
385
if self .robust_fit :
414
386
robust = robust_2dfit (kernel , xi ,
415
387
coord [:,0 ], coord [:,1 ],
416
388
xi_weight , mask = mask )
417
- print ('made robust' )
418
389
robust .minimize_minuit (p0 = self .p0_robust_fit )
419
- print ('after minimize_minuit' )
420
390
kernel = copy .deepcopy (robust .kernel_fit )
421
- print ('after copy kernel' )
422
391
cst = robust .result [- 1 ]
423
392
self ._results_robust = robust .result
424
- print ('results_robust ' ,robust .result )
425
393
else :
426
394
p0 = kernel .theta
427
395
results_fmin = optimize .fmin (chi2 ,p0 ,disp = False )
@@ -432,13 +400,11 @@ def chi2(param):
432
400
results = results [ind_min ]
433
401
kernel = kernel .clone_with_theta (results )
434
402
cst = 0
435
- print ('results ' ,results )
436
403
437
404
self ._2pcf = xi
438
405
self ._2pcf_weight = xi_weight
439
406
self ._2pcf_dist = distance
440
407
self ._2pcf_fit = PCF (kernel .theta ) + cst
441
408
self ._2pcf_mask = mask
442
409
self ._kernel = copy .deepcopy (kernel )
443
- print ('done making kernel' )
444
410
return kernel
0 commit comments