-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_LOFAR_combined.py
628 lines (517 loc) · 23.4 KB
/
match_LOFAR_combined.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# coding: utf-8
# # ML match for LOFAR and the combined PanSTARRS WISE catalogue: Source catalogue
# ## Configuration
#
# ### Load libraries and setup
import pickle
import os
import numpy as np
from astropy.table import Table
from astropy import units as u
from astropy.coordinates import SkyCoord, search_around_sky
import matplotlib
matplotlib.use('qt5agg')
from matplotlib import pyplot as plt
from mltier1 import (get_center, get_n_m, estimate_q_m, Field, SingleMLEstimator, MultiMLEstimator,
parallel_process, get_sigma, get_q_m, get_threshold, q0_min_level, q0_min_numbers)
# ### General configuration
save_intermediate = True
plot_intermediate = True
idp = "idata/main"
if not os.path.isdir(idp):
os.makedirs(idp)
n_cores = 8
# ### Area limits
field = Field(170.0, 190.0, 46.8, 55.9)
# ## Load data
combined = Table.read("pw.fits")
lofar_all = Table.read("data/LOFAR_HBA_T1_DR1_catalog_v0.1.fits")
# ### Filter catalogues
lofar = field.filter_catalogue(lofar_all, colnames=("RA", "DEC"))
# ### Additional data
combined["colour"] = combined["i"] - combined["W1mag"]
combined_aux_index = np.arange(len(combined))
# ### Sky coordinates
coords_combined = SkyCoord(combined['ra'],
combined['dec'],
unit=(u.deg, u.deg),
frame='icrs')
coords_lofar = SkyCoord(lofar['RA'],
lofar['DEC'],
unit=(u.deg, u.deg),
frame='icrs')
# ### Class of sources in the combined catalogue
#
# The sources are grouped depending on the available photometric data.
combined_matched = (~np.isnan(combined["i"]) & ~np.isnan(combined["W1mag"])) # Matched i-W1 sources
combined_panstarrs = (~np.isnan(combined["i"]) & np.isnan(combined["W1mag"])) # Sources with only i-band
combined_wise =(np.isnan(combined["i"]) & ~np.isnan(combined["W1mag"])) # Sources with only W1-band
combined_i = combined_matched | combined_panstarrs
combined_w1 = combined_matched | combined_wise
# ### Colour categories
#
# The colour categories will be used after the first ML match
colour_limits = [-0.5, 0.0, 0.5, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.5, 4.0]
# Start with the W1-only, i-only and "less than lower colour" bins
colour_bin_def = [{"name":"only W1", "condition": combined_wise},
{"name":"only i", "condition": combined_panstarrs},
{"name":"-inf to {}".format(colour_limits[0]),
"condition": (combined["colour"] < colour_limits[0])}]
# Get the colour bins
for i in range(len(colour_limits)-1):
name = "{} to {}".format(colour_limits[i], colour_limits[i+1])
condition = ((combined["colour"] >= colour_limits[i]) &
(combined["colour"] < colour_limits[i+1]))
colour_bin_def.append({"name":name, "condition":condition})
# Add the "more than higher colour" bin
colour_bin_def.append({"name":"{} to inf".format(colour_limits[-1]),
"condition": (combined["colour"] >= colour_limits[-1])})
combined["category"] = np.nan
for i in range(len(colour_bin_def)):
combined["category"][colour_bin_def[i]["condition"]] = i
# We get the number of sources of the combined catalogue in each colour category. It will be used at a later stage to compute the $Q_0$ values
numbers_combined_bins = np.array([np.sum(a["condition"]) for a in colour_bin_def])
# ## Maximum Likelihood 1st
catalogue_i = combined[combined_i]
bin_list_i = np.linspace(12., 30., 361) # Bins of 0.05
center_i = get_center(bin_list_i)
n_m_i = get_n_m(catalogue_i["i"], bin_list_i, field.area)
q_m_i = estimate_q_m(catalogue_i["i"], bin_list_i, n_m_i, coords_lofar, coords_combined[combined_i], radius=5)
# ### W1-band preparation
catalogue_w1 = combined[combined_w1]
bin_list_w1 = np.linspace(10., 23., 261) # Bins of 0.05
center_w1 = get_center(bin_list_w1)
n_m_w1 = get_n_m(catalogue_w1["W1mag"], bin_list_w1, field.area)
q_m_w1 = estimate_q_m(catalogue_w1["W1mag"],
bin_list_w1,
n_m_w1, coords_lofar,
coords_combined[combined_w1],
radius=5)
# ### $Q_0$ and likelihood estimators
Q0_i = 0.503
Q0_w1 = 0.699
likelihood_ratio_i = SingleMLEstimator(Q0_i, n_m_i, q_m_i, center_i)
likelihood_ratio_w1 = SingleMLEstimator(Q0_w1, n_m_w1, q_m_w1, center_w1)
# ### i-band match
radius = 15
idx_lofar, idx_i, d2d, d3d = search_around_sky(
coords_lofar, coords_combined[combined_i], radius*u.arcsec)
idx_lofar_unique = np.unique(idx_lofar)
lofar["lr_i"] = np.nan # Likelihood ratio
lofar["lr_dist_i"] = np.nan # Distance to the selected source
lofar["lr_index_i"] = np.nan # Index of the PanSTARRS source in combined
total_sources = len(idx_lofar_unique)
combined_aux_index = np.arange(len(combined))
def ml(i):
idx_0 = idx_i[idx_lofar == i]
d2d_0 = d2d[idx_lofar == i]
i_mag = catalogue_i["i"][idx_0]
lofar_ra = lofar[i]["RA"]
lofar_dec = lofar[i]["DEC"]
lofar_pa = lofar[i]["PA"]
lofar_maj_err = lofar[i]["E_Maj"]
lofar_min_err = lofar[i]["E_Min"]
c_ra = catalogue_i["ra"][idx_0]
c_dec = catalogue_i["dec"][idx_0]
c_ra_err = catalogue_i["raErr"][idx_0]
c_dec_err = catalogue_i["decErr"][idx_0]
sigma = get_sigma(lofar_maj_err, lofar_min_err, lofar_pa,
lofar_ra, lofar_dec,
c_ra, c_dec, c_ra_err, c_dec_err)
lr_0 = likelihood_ratio_i(i_mag, d2d_0.arcsec, sigma)
chosen_index = np.argmax(lr_0)
result = [combined_aux_index[combined_i][idx_0[chosen_index]], # Index
(d2d_0.arcsec)[chosen_index], # distance
lr_0[chosen_index]] # LR
return result
res = parallel_process(idx_lofar_unique, ml, n_jobs=n_cores)
(lofar["lr_index_i"][idx_lofar_unique],
lofar["lr_dist_i"][idx_lofar_unique],
lofar["lr_i"][idx_lofar_unique]) = list(map(list, zip(*res)))
# #### Threshold and selection for i-band
lofar["lr_i"][np.isnan(lofar["lr_i"])] = 0
threshold_i = np.percentile(lofar["lr_i"], 100*(1 - Q0_i))
if plot_intermediate:
fig = plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.hist(lofar[lofar["lr_i"] != 0]["lr_i"], bins=200)
plt.vlines([threshold_i], 0, 1000)
plt.ylim([0,1000])
plt.subplot(1,2,2)
plt.hist(np.log10(lofar[lofar["lr_i"] != 0]["lr_i"]+1), bins=200)
plt.vlines(np.log10(threshold_i+1), 0, 1000)
ticks, _ = plt.xticks()
plt.xticks(ticks, ["{:.1f}".format(10**t-1) for t in ticks])
plt.ylim([0,1000])
plt.savefig('{}/lr_i.png'.format(idp))
del fig
lofar["lr_index_sel_i"] = lofar["lr_index_i"]
lofar["lr_index_sel_i"][lofar["lr_i"] < threshold_i] = np.nan
# ### W1-band match
idx_lofar, idx_i, d2d, d3d = search_around_sky(
coords_lofar, coords_combined[combined_w1], radius*u.arcsec)
idx_lofar_unique = np.unique(idx_lofar)
lofar["lr_w1"] = np.nan # Likelihood ratio
lofar["lr_dist_w1"] = np.nan # Distance to the selected source
lofar["lr_index_w1"] = np.nan # Index of the PanSTARRS source in combined
def ml_w1(i):
idx_0 = idx_i[idx_lofar == i]
d2d_0 = d2d[idx_lofar == i]
w1_mag = catalogue_w1["W1mag"][idx_0]
lofar_ra = lofar[i]["RA"]
lofar_dec = lofar[i]["DEC"]
lofar_pa = lofar[i]["PA"]
lofar_maj_err = lofar[i]["E_Maj"]
lofar_min_err = lofar[i]["E_Min"]
c_ra = catalogue_w1["ra"][idx_0]
c_dec = catalogue_w1["dec"][idx_0]
c_ra_err = catalogue_w1["raErr"][idx_0]
c_dec_err = catalogue_w1["decErr"][idx_0]
sigma = get_sigma(lofar_maj_err, lofar_min_err, lofar_pa,
lofar_ra, lofar_dec,
c_ra, c_dec, c_ra_err, c_dec_err)
lr_0 = likelihood_ratio_w1(w1_mag, d2d_0.arcsec, sigma)
chosen_index = np.argmax(lr_0)
result = [combined_aux_index[combined_w1][idx_0[chosen_index]], # Index
(d2d_0.arcsec)[chosen_index], # distance
lr_0[chosen_index]] # LR
return result
res = parallel_process(idx_lofar_unique, ml_w1, n_jobs=n_cores)
(lofar["lr_index_w1"][idx_lofar_unique],
lofar["lr_dist_w1"][idx_lofar_unique],
lofar["lr_w1"][idx_lofar_unique]) = list(map(list, zip(*res)))
# #### Threshold and selection for W1 band
lofar["lr_w1"][np.isnan(lofar["lr_w1"])] = 0
threshold_w1 = np.percentile(lofar["lr_w1"], 100*(1 - Q0_w1))
if plot_intermediate:
fig = plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.hist(lofar[lofar["lr_w1"] != 0]["lr_w1"], bins=200)
plt.vlines([threshold_w1], 0, 1000)
plt.ylim([0,1000])
plt.subplot(1,2,2)
plt.hist(np.log10(lofar[lofar["lr_w1"] != 0]["lr_w1"]+1), bins=200)
plt.vlines(np.log10(threshold_w1+1), 0, 1000)
ticks, _ = plt.xticks()
plt.xticks(ticks, ["{:.1f}".format(10**t-1) for t in ticks])
plt.ylim([0,1000])
plt.savefig('{}/lr_w1.png'.format(idp))
del fig
lofar["lr_index_sel_w1"] = lofar["lr_index_w1"]
lofar["lr_index_sel_w1"][lofar["lr_w1"] < threshold_w1] = np.nan
# ### Final selection of the match
#
# We combine the ML matching done in i-band and W1-band. All the galaxies were the LR is above the selection ratio for the respective band are finally selected.
lr_i_and_w1 = ~np.isnan(lofar["lr_index_sel_i"]) & ~np.isnan(lofar["lr_index_sel_w1"])
lr_only_i = ~np.isnan(lofar["lr_index_sel_i"]) & np.isnan(lofar["lr_index_sel_w1"])
lr_only_w1 = np.isnan(lofar["lr_index_sel_i"]) & ~np.isnan(lofar["lr_index_sel_w1"])
lr_no_match = np.isnan(lofar["lr_index_sel_i"]) & np.isnan(lofar["lr_index_sel_w1"])
print(np.sum(lr_i_and_w1))
print(np.sum(lr_only_i))
print(np.sum(lr_only_w1))
print(np.sum(lr_no_match))
lofar["lr_index_1"] = np.nan
lofar["lr_dist_1"] = np.nan
lofar["lr_1"] = np.nan
lofar["lr_type_1"] = 0
# Only i matches
lofar["lr_1"][lr_only_i] = lofar["lr_i"][lr_only_i]
lofar["lr_index_1"][lr_only_i] = lofar["lr_index_i"][lr_only_i]
lofar["lr_dist_1"][lr_only_i] = lofar["lr_dist_i"][lr_only_i]
lofar["lr_type_1"][lr_only_i] = 1
# Only w1 matches
lofar["lr_1"][lr_only_w1] = lofar["lr_w1"][lr_only_w1]
lofar["lr_index_1"][lr_only_w1] = lofar["lr_index_w1"][lr_only_w1]
lofar["lr_dist_1"][lr_only_w1] = lofar["lr_dist_w1"][lr_only_w1]
lofar["lr_type_1"][lr_only_w1] = 2
# Both matches
lofar["lr_1"][lr_i_and_w1] = np.max([lofar["lr_i"][lr_i_and_w1], lofar["lr_w1"][lr_i_and_w1]], axis=0)
lofar["lr_type_1"][lr_i_and_w1] = np.argmax([lofar["lr_i"][lr_i_and_w1], lofar["lr_w1"][lr_i_and_w1]], axis=0) + 1
c1 = (lofar["lr_type_1"] == 1)
c2 = (lofar["lr_type_1"] == 2)
lofar["lr_index_1"][lr_i_and_w1 & c1] = lofar["lr_index_i"][lr_i_and_w1 & c1]
lofar["lr_index_1"][lr_i_and_w1 & c2] = lofar["lr_index_w1"][lr_i_and_w1 & c2]
lofar["lr_dist_1"][lr_i_and_w1 & c1] = lofar["lr_dist_i"][lr_i_and_w1 & c1]
lofar["lr_dist_1"][lr_i_and_w1 & c2] = lofar["lr_dist_w1"][lr_i_and_w1 & c2]
# Summary of the number of sources matches of each type
print("match sel-i: ", np.sum(lofar["lr_type_1"][lr_i_and_w1] == 1))
print("match sel-W1: ", np.sum(lofar["lr_type_1"][lr_i_and_w1] == 2))
print("match both: ", np.sum(lofar["lr_type_1"][lr_i_and_w1] == 1) +
np.sum(lofar["lr_type_1"][lr_i_and_w1] == 2))
print("match i-only: ", np.sum(lofar["lr_type_1"] == 1) - np.sum(lofar["lr_type_1"][lr_i_and_w1] == 1))
print("match W1-only: ", np.sum(lofar["lr_type_1"] == 2) - np.sum(lofar["lr_type_1"][lr_i_and_w1] == 2))
print("match all: ", np.sum(lofar["lr_type_1"] == 1) +
np.sum(lofar["lr_type_1"] == 2))
print(" Total: ", len(lofar))
# The number of sources for which the match in i-band and W1-band are above the threshold but gives a different match to the combined catalogue.
print(np.sum(lofar["lr_index_i"][lr_i_and_w1] != lofar["lr_index_w1"][lr_i_and_w1]))
# ### Save intermediate data
if save_intermediate:
pickle.dump([bin_list_i, center_i, Q0_i, n_m_i, q_m_i],
open("{}/lofar_params_1i.pckl".format(idp), 'wb'))
pickle.dump([bin_list_w1, center_w1, Q0_w1, n_m_w1, q_m_w1],
open("{}/lofar_params_1w1.pckl".format(idp), 'wb'))
lofar.write("{}/lofar_m1.fits".format(idp), format="fits")
# ## Second iteration using colour
#
# From now on we will take into account the effect of the colour. The sample was distributed in several categories according to the colour of the source and this is considered here.
#
# ### Rusable parameters for all the iterations
#
# These parameters are derived from the underlying population and will not change.
#
# First we compute the number of galaxies in each bin for the combined catalogue
bin_list = [bin_list_w1 if i == 0 else bin_list_i for i in range(len(colour_bin_def))]
centers = [center_w1 if i == 0 else center_i for i in range(len(colour_bin_def))]
numbers_combined_bins = np.array([np.sum(a["condition"]) for a in colour_bin_def])
# Get the colour category and magnitudes for the matched LOFAR sources
n_m = []
# W1 only sources
n_m.append(get_n_m(combined["W1mag"][combined["category"] == 0], bin_list_w1, field.area))
# Rest of the sources
for i in range(1, len(colour_bin_def)):
n_m.append(get_n_m(combined["i"][combined["category"] == i], bin_list_i, field.area))
if plot_intermediate:
fig = plt.figure(figsize=(15,15))
for i, n_m_k in enumerate(n_m):
plt.subplot(5,5,i+1)
plt.plot(centers[i], n_m_k)
plt.savefig('{}/n_m_1.png'.format(idp))
del fig
# ### Parameters of the matched sample
#
# The parameters derived from the matched LOFAR galaxies: $q_0$, q(m) and the number of sources per category.
#
# The columns "category", "W1mag" and "i" will contain the properties of the matched galaxies and will be updated in each iteration to save space.
lofar["category"] = np.nan
lofar["W1mag"] = np.nan
lofar["i"] = np.nan
c = ~np.isnan(lofar["lr_index_1"])
indices = lofar["lr_index_1"][c].astype(int)
lofar["category"][c] = combined[indices]["category"]
lofar["W1mag"][c] = combined[indices]["W1mag"]
lofar["i"][c] = combined[indices]["i"]
# The next parameter represent the number of matched LOFAR sources in each colour category.
numbers_lofar_combined_bins = np.array([np.sum(lofar["category"] == c)
for c in range(len(numbers_combined_bins))])
# The $Q_0$ for each category are obtained by dividing the number of sources in the category by the total number of sources in the sample.
Q_0_colour = numbers_lofar_combined_bins/len(lofar) ### Q_0
q0_total = np.sum(Q_0_colour)
# The q(m) is not estimated with the method of Fleuren et al. but with the most updated distributions and numbers for the matches.
q_m = []
radius = 15.
# W1 only sources
q_m.append(get_q_m(lofar["W1mag"][lofar["category"] == 0],
bin_list_w1,
numbers_lofar_combined_bins[0],
n_m[0],
field.area,
radius=radius))
# Rest of the sources
for i in range(1, len(numbers_lofar_combined_bins)):
q_m.append(get_q_m(lofar["i"][lofar["category"] == i],
bin_list_i,
numbers_lofar_combined_bins[i],
n_m[i],
field.area,
radius=radius))
if plot_intermediate:
fig = plt.figure(figsize=(15,15))
for i, q_m_k in enumerate(q_m):
plt.subplot(5,5,i+1)
plt.plot(centers[i], q_m_k)
plt.savefig('{}/q_m_1.png'.format(idp))
del fig
# ### Save intermediate parameters
if save_intermediate:
pickle.dump([bin_list, centers, Q_0_colour, n_m, q_m],
open("{}/lofar_params_2.pckl".format(idp), 'wb'))
# ### Prepare for ML:
selection = ~np.isnan(combined["category"]) # Avoid the two dreaded sources with no actual data
catalogue = combined[selection]
radius = 15
def apply_ml(i, likelihood_ratio_function):
idx_0 = idx_i[idx_lofar == i]
d2d_0 = d2d[idx_lofar == i]
category = catalogue["category"][idx_0].astype(int)
mag = catalogue["i"][idx_0]
mag[category == 0] = catalogue["W1mag"][idx_0][category == 0]
lofar_ra = lofar[i]["RA"]
lofar_dec = lofar[i]["DEC"]
lofar_pa = lofar[i]["PA"]
lofar_maj_err = lofar[i]["E_Maj"]
lofar_min_err = lofar[i]["E_Min"]
c_ra = catalogue["ra"][idx_0]
c_dec = catalogue["dec"][idx_0]
c_ra_err = catalogue["raErr"][idx_0]
c_dec_err = catalogue["decErr"][idx_0]
sigma = get_sigma(lofar_maj_err, lofar_min_err, lofar_pa,
lofar_ra, lofar_dec,
c_ra, c_dec, c_ra_err, c_dec_err)
lr_0 = likelihood_ratio_function(mag, d2d_0.arcsec, sigma, category)
chosen_index = np.argmax(lr_0)
result = [combined_aux_index[selection][idx_0[chosen_index]], # Index
(d2d_0.arcsec)[chosen_index], # distance
lr_0[chosen_index]] # LR
return result
# ### Run the cross-match
#
# This will not need to be repeated after
idx_lofar, idx_i, d2d, d3d = search_around_sky(
coords_lofar, coords_combined[selection], radius*u.arcsec)
idx_lofar_unique = np.unique(idx_lofar)
# ### Run the ML matching
likelihood_ratio = MultiMLEstimator(Q_0_colour, n_m, q_m, centers)
def ml(i):
return apply_ml(i, likelihood_ratio)
res = parallel_process(idx_lofar_unique, ml, n_jobs=n_cores)
lofar["lr_index_2"] = np.nan
lofar["lr_dist_2"] = np.nan
lofar["lr_2"] = np.nan
(lofar["lr_index_2"][idx_lofar_unique],
lofar["lr_dist_2"][idx_lofar_unique],
lofar["lr_2"][idx_lofar_unique]) = list(map(list, zip(*res)))
# Get the new threshold for the ML matching. FIX THIS
lofar["lr_2"][np.isnan(lofar["lr_2"])] = 0
threshold = np.percentile(lofar["lr_2"], 100*(1 - q0_total))
#manual_q0 = 0.65
#threshold = np.percentile(lofar["lr_2"], 100*(1 - manual_q0))
if plot_intermediate:
fig = plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.hist(lofar[lofar["lr_2"] != 0]["lr_2"], bins=200)
plt.vlines([threshold], 0, 1000)
plt.ylim([0,1000])
plt.subplot(1,2,2)
plt.hist(np.log10(lofar[lofar["lr_2"] != 0]["lr_2"]+1), bins=200)
plt.vlines(np.log10(threshold+1), 0, 1000)
ticks, _ = plt.xticks()
plt.xticks(ticks, ["{:.1f}".format(10**t-1) for t in ticks])
plt.ylim([0,1000])
plt.savefig('{}/lr_2.png'.format(idp))
del fig
lofar["lr_index_sel_2"] = lofar["lr_index_2"]
lofar["lr_index_sel_2"][lofar["lr_2"] < threshold] = np.nan
n_changes = np.sum((lofar["lr_index_sel_2"] != lofar["lr_index_1"]) &
~np.isnan(lofar["lr_index_sel_2"]) &
~np.isnan(lofar["lr_index_1"]))
print("N changes", n_changes)
# Enter the results
# Clear aux columns
lofar["category"] = np.nan
lofar["W1mag"] = np.nan
lofar["i"] = np.nan
c = ~np.isnan(lofar["lr_index_sel_2"])
indices = lofar["lr_index_sel_2"][c].astype(int)
lofar["category"][c] = combined[indices]["category"]
lofar["W1mag"][c] = combined[indices]["W1mag"]
lofar["i"][c] = combined[indices]["i"]
# ### Save intermediate data
if save_intermediate:
lofar.write("{}/lofar_m2.fits".format(idp), format="fits")
# ## Iterate until convergence
radius = 15.
for j in range(10):
iteration = j+3
print("Iteration {}".format(iteration))
print("=============")
## Get new parameters
# Number of matched sources per bin
numbers_lofar_combined_bins = np.array([np.sum(lofar["category"] == c)
for c in range(len(numbers_combined_bins))])
print("numbers_lofar_combined_bins")
print(numbers_lofar_combined_bins)
# q_0
Q_0_colour_est = numbers_lofar_combined_bins/len(lofar) ### Q_0
Q_0_colour = q0_min_numbers(Q_0_colour_est, numbers_combined_bins)
print("Q_0_colour")
print(Q_0_colour)
q0_total = np.sum(Q_0_colour)
print("Q_0_total: ", q0_total)
# q_m
q_m = []
# W1 only sources
q_m.append(get_q_m(lofar["W1mag"][lofar["category"] == 0],
bin_list_w1,
numbers_lofar_combined_bins[0],
n_m[0],
field.area,
radius=radius))
# Rest of the sources
for i in range(1, len(numbers_lofar_combined_bins)):
q_m.append(get_q_m(lofar["i"][lofar["category"] == i],
bin_list_i,
numbers_lofar_combined_bins[i],
n_m[i],
field.area,
radius=radius))
# Save new parameters
if save_intermediate:
pickle.dump([bin_list, centers, Q_0_colour, n_m, q_m],
open("{}/lofar_params_{}.pckl".format(idp, iteration), 'wb'))
if plot_intermediate:
fig = plt.figure(figsize=(15,15))
for i, q_m_k in enumerate(q_m):
plt.subplot(5,5,i+1)
plt.plot(centers[i], q_m_k)
plt.savefig('{}/q0_{}.png'.format(idp, iteration))
del fig
## Define new likelihood_ratio
likelihood_ratio = MultiMLEstimator(Q_0_colour, n_m, q_m, centers)
def ml(i):
return apply_ml(i, likelihood_ratio)
## Run the ML
res = parallel_process(idx_lofar_unique, ml, n_jobs=n_cores)
lofar["lr_index_{}".format(iteration)] = np.nan
lofar["lr_dist_{}".format(iteration)] = np.nan
lofar["lr_{}".format(iteration)] = np.nan
(lofar["lr_index_{}".format(iteration)][idx_lofar_unique],
lofar["lr_dist_{}".format(iteration)][idx_lofar_unique],
lofar["lr_{}".format(iteration)][idx_lofar_unique]) = list(map(list, zip(*res)))
lofar["lr_{}".format(iteration)][np.isnan(lofar["lr_{}".format(iteration)])] = 0
## Get and apply the threshold
threshold = np.percentile(lofar["lr_{}".format(iteration)], 100*(1 - q0_total))
#threshold = get_threshold(lofar[lofar["lr_{}".format(iteration)] != 0]["lr_{}".format(iteration)])
print("Threshold: ", threshold)
if plot_intermediate:
fig = plt.figure(figsize=(15,6))
plt.subplot(1,2,1)
plt.hist(lofar[lofar["lr_{}".format(iteration)] != 0]["lr_{}".format(iteration)], bins=200)
plt.vlines([threshold], 0, 1000)
plt.ylim([0,1000])
plt.subplot(1,2,2)
plt.hist(np.log10(lofar[lofar["lr_{}".format(iteration)] != 0]["lr_{}".format(iteration)]+1), bins=200)
plt.vlines(np.log10(threshold+1), 0, 1000)
ticks, _ = plt.xticks()
plt.xticks(ticks, ["{:.1f}".format(10**t-1) for t in ticks])
plt.ylim([0,1000])
plt.savefig('{}/lr_distribution_{}.png'.format(idp, iteration))
del fig
## Apply the threshold
lofar["lr_index_sel_{}".format(iteration)] = lofar["lr_index_{}".format(iteration)]
lofar["lr_index_sel_{}".format(iteration)][lofar["lr_{}".format(iteration)] < threshold] = np.nan
## Enter changes into the catalogue
# Clear aux columns
lofar["category"] = np.nan
lofar["W1mag"] = np.nan
lofar["i"] = np.nan
# Update data
c = ~np.isnan(lofar["lr_index_sel_{}".format(iteration)])
indices = lofar["lr_index_sel_{}".format(iteration)][c].astype(int)
lofar["category"][c] = combined[indices]["category"]
lofar["W1mag"][c] = combined[indices]["W1mag"]
lofar["i"][c] = combined[indices]["i"]
# Save the data
if save_intermediate:
lofar.write("{}/lofar_m{}.fits".format(idp, iteration), format="fits")
## Compute number of changes
n_changes = np.sum((
lofar["lr_index_sel_{}".format(iteration)] != lofar["lr_index_sel_{}".format(iteration-1)]) &
~np.isnan(lofar["lr_index_sel_{}".format(iteration)]) &
~np.isnan(lofar["lr_index_sel_{}".format(iteration-1)]))
print("N changes: ", n_changes)
## Check changes
if n_changes == 0:
break
else:
print("******** continue **********")