Skip to content

Instantly share code, notes, and snippets.

@thespacedoctor
Last active May 5, 2021 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thespacedoctor/02f92b359ae7ec95495b1597c6837f25 to your computer and use it in GitHub Desktop.
Save thespacedoctor/02f92b359ae7ec95495b1597c6837f25 to your computer and use it in GitHub Desktop.
[Distribution of number of candidates that go into making ZTF objects] #ztf #lasair

Distribution of number of candidates that go into making ZTF objects

In ZTF speak a candidate is a single detection and an object is a collection of associated candidates. I wanted to visualise (see plot above) the distribution of the number of candidates (ncand) that go into making a ZTF object.

To generate the CANDIDATE table distribution data in candidate_ncand_dist.csv I run the following query on Lasair (April 8, 2020):

SELECT 
    ncand, COUNT(*)
FROM
    (SELECT 
        objectId, COUNT(*) AS ncand
    FROM
        candidates
    GROUP BY objectId) AS a
GROUP BY ncand;

To generate the OBJECT table distribution data in object_ncand_dist.csv I run the following query on Lasair (April 8, 2020):

SELECT 
    ncand, COUNT(*)
FROM
    objects
GROUP BY ncand;

Then to generate this plot run python plot_ncand_histogram.py candidate_ncand_dist.csv object_ncand_dist.csv. There are many source where ncand > 20 (not visualised above) but I'm interested in the low-end to determine which objects we should limit sherlock to run on.

ncand count(*)
1 25294323
2 3489110
3 1276364
4 594820
5 319585
6 194603
7 130689
8 95216
9 73793
10 59745
11 50152
12 43194
13 37744
14 33512
15 30254
16 27104
17 25451
18 22979
19 21362
20 19934
21 18543
22 17433
23 16332
24 15103
25 14444
26 13878
27 13168
28 12516
29 11779
30 11317
31 10858
32 10404
33 9949
34 9524
35 9068
36 8908
37 8421
38 8044
39 7876
40 7703
41 7349
42 7051
43 6900
44 6631
45 6458
46 6196
47 6099
48 5633
49 5774
50 5410
51 5280
52 5215
53 5013
54 5038
55 4777
56 4694
57 4463
58 4473
59 4271
60 4099
61 4076
62 3998
63 4000
64 3710
65 3630
66 3582
67 3544
68 3447
69 3297
70 3216
71 3293
72 3167
73 3093
74 3004
75 2819
76 2811
77 2771
78 2806
79 2681
80 2675
81 2578
82 2537
83 2429
84 2425
85 2443
86 2348
87 2284
88 2265
89 2131
90 2119
91 2124
92 2041
93 1961
94 1964
95 1933
96 1868
97 1828
98 1798
99 1770
100 1756
101 1686
102 1690
103 1648
104 1583
105 1591
106 1554
107 1641
108 1521
109 1455
110 1431
111 1354
112 1429
113 1368
114 1336
115 1339
116 1289
117 1293
118 1249
119 1299
120 1200
121 1228
122 1212
123 1116
124 1128
125 1109
126 1121
127 1085
128 1063
129 1024
130 1080
131 1031
132 1029
133 986
134 928
135 869
136 861
137 908
138 958
139 900
140 832
141 853
142 863
143 776
144 774
145 766
146 800
147 771
148 746
149 725
150 710
151 727
152 707
153 693
154 682
155 638
156 662
157 621
158 662
159 640
160 676
161 626
162 577
163 588
164 535
165 609
166 544
167 576
168 529
169 526
170 553
171 518
172 445
173 501
174 486
175 471
176 501
177 475
178 463
179 467
180 469
181 449
182 413
183 407
184 420
185 385
186 386
187 376
188 423
189 361
190 379
191 390
192 352
193 327
194 330
195 353
196 348
197 371
198 321
199 304
200 282
201 320
202 287
203 307
204 300
205 301
206 277
207 295
208 284
209 265
210 284
211 280
212 259
213 283
214 257
215 230
216 261
217 263
218 282
219 243
220 254
221 248
222 248
223 209
224 246
225 233
226 242
227 229
228 221
229 223
230 218
231 199
232 205
233 212
234 210
235 198
236 190
237 205
238 181
239 203
240 182
241 189
242 190
243 191
244 165
245 181
246 165
247 190
248 186
249 162
250 176
251 159
252 149
253 156
254 159
255 160
256 148
257 153
258 142
259 155
260 138
261 131
262 130
263 126
264 133
265 113
266 148
267 143
268 134
269 124
270 147
271 156
272 141
273 124
274 124
275 126
276 126
277 123
278 110
279 116
280 135
281 116
282 110
283 115
284 102
285 86
286 104
287 113
288 101
289 108
290 112
291 115
292 95
293 86
294 105
295 97
296 102
297 118
298 97
299 96
300 89
301 78
302 77
303 69
304 90
305 87
306 85
307 92
308 100
309 83
310 77
311 82
312 71
313 74
314 85
315 72
316 80
317 75
318 69
319 78
320 76
321 72
322 81
323 67
324 56
325 68
326 66
327 53
328 61
329 70
330 66
331 59
332 61
333 60
334 64
335 61
336 59
337 52
338 52
339 39
340 62
341 42
342 43
343 41
344 48
345 64
346 51
347 35
348 43
349 44
350 51
351 43
352 44
353 37
354 31
355 44
356 48
357 46
358 34
359 34
360 40
361 43
362 49
363 48
364 37
365 43
366 26
367 31
368 40
369 32
370 37
371 27
372 27
373 28
374 26
375 34
376 29
377 20
378 24
379 32
380 32
381 22
382 29
383 23
384 23
385 31
386 35
387 26
388 28
389 22
390 27
391 26
392 29
393 23
394 17
395 18
396 24
397 22
398 14
399 27
400 26
401 24
402 22
403 19
404 17
405 20
406 13
407 14
408 27
409 16
410 21
411 19
412 17
413 20
414 14
415 21
416 18
417 18
418 10
419 17
420 19
421 11
422 15
423 19
424 23
425 16
426 17
427 18
428 10
429 12
430 16
431 16
432 15
433 14
434 11
435 15
436 14
437 17
438 13
439 6
440 10
441 18
442 10
443 15
444 11
445 12
446 17
447 13
448 17
449 13
450 9
451 13
452 18
453 13
454 16
455 11
456 17
457 15
458 13
459 11
460 11
461 12
462 12
463 6
464 8
465 4
466 12
467 12
468 20
469 9
470 15
471 8
472 12
473 14
474 12
475 6
476 11
477 12
478 6
479 10
480 8
481 8
482 13
483 7
484 12
485 6
486 7
487 6
488 7
489 6
490 4
491 7
492 2
493 10
494 7
495 7
496 5
497 10
498 7
499 10
500 9
501 4
502 6
503 3
504 12
505 11
506 6
507 7
508 6
509 16
510 7
511 6
512 6
513 8
514 10
515 9
516 11
517 10
518 3
519 8
520 8
521 6
522 8
523 5
524 3
525 7
526 10
527 4
528 7
529 4
530 4
531 6
532 8
533 5
534 5
535 3
536 2
537 1
538 3
539 7
540 9
541 9
542 7
543 1
544 4
545 9
546 2
547 3
548 6
549 5
550 9
551 6
552 3
553 2
554 3
555 4
556 11
557 9
558 3
559 7
560 6
561 5
562 6
563 4
564 8
565 2
566 7
567 4
568 4
569 6
570 5
571 4
572 5
573 2
574 5
575 5
576 2
577 2
578 4
579 4
580 7
581 5
582 4
583 2
584 2
585 1
586 2
587 1
588 7
589 4
590 1
591 2
592 5
594 2
595 1
596 6
597 4
598 3
599 2
601 4
602 1
603 3
604 6
605 3
606 4
607 3
608 5
609 5
610 2
611 2
612 4
613 4
614 5
615 3
616 3
617 1
618 1
619 2
620 5
621 2
622 4
623 3
624 2
625 1
626 1
627 2
628 5
629 3
630 1
631 1
632 3
633 2
634 5
635 3
637 2
638 2
639 3
640 1
641 1
642 1
643 3
644 2
645 3
646 4
647 5
648 2
650 4
651 2
653 2
654 2
656 1
657 1
658 2
659 1
660 1
661 1
662 1
664 1
665 3
669 1
670 2
671 1
673 2
674 2
675 2
676 1
678 1
679 1
680 1
684 1
685 2
687 4
689 2
691 1
692 1
695 3
698 1
700 3
702 1
703 1
705 1
706 2
708 2
711 1
712 1
714 3
716 1
717 1
719 2
721 4
722 1
729 1
732 1
733 1
734 2
736 1
737 1
738 1
740 1
743 1
745 1
746 2
748 1
749 3
750 1
753 1
754 2
756 1
759 1
760 2
761 3
762 1
764 1
769 2
771 2
780 1
781 1
787 1
788 2
791 2
795 1
799 1
800 1
807 1
808 1
809 1
812 1
813 1
817 1
819 1
825 1
827 1
831 1
839 1
840 1
843 2
847 1
854 1
855 1
868 1
871 2
872 2
899 1
901 1
902 1
908 1
924 1
931 1
934 1
955 1
1040 1
1112 1
ncand count(*)
0 1973
1 12670138
2 2256906
3 1268298
4 591514
5 318073
6 193684
7 130005
8 94801
9 73572
10 59414
11 49984
12 43051
13 37549
14 33353
15 30094
16 27070
17 25327
18 22808
19 21268
20 19933
21 18422
22 17372
23 16310
24 15088
25 14398
26 13856
27 13135
28 12457
29 11705
30 11283
31 10821
32 10398
33 9912
34 9514
35 9045
36 8907
37 8383
38 8016
39 7863
40 7674
41 7383
42 7041
43 6838
44 6622
45 6436
46 6207
47 6060
48 5626
49 5756
50 5388
51 5269
52 5208
53 5008
54 5041
55 4755
56 4685
57 4458
58 4483
59 4238
60 4090
61 4064
62 4003
63 4011
64 3709
65 3629
66 3566
67 3531
68 3452
69 3296
70 3231
71 3282
72 3161
73 3086
74 3005
75 2813
76 2818
77 2773
78 2787
79 2678
80 2661
81 2579
82 2538
83 2422
84 2433
85 2436
86 2339
87 2302
88 2242
89 2140
90 2121
91 2119
92 2033
93 1958
94 1961
95 1933
96 1873
97 1825
98 1787
99 1772
100 1762
101 1686
102 1698
103 1643
104 1575
105 1594
106 1554
107 1646
108 1511
109 1455
110 1432
111 1351
112 1425
113 1363
114 1328
115 1344
116 1296
117 1292
118 1241
119 1307
120 1205
121 1223
122 1207
123 1110
124 1134
125 1104
126 1123
127 1083
128 1067
129 1026
130 1074
131 1026
132 1034
133 980
134 928
135 865
136 866
137 911
138 954
139 902
140 837
141 848
142 862
143 782
144 767
145 765
146 798
147 771
148 738
149 730
150 713
151 731
152 708
153 691
154 678
155 635
156 669
157 620
158 661
159 636
160 681
161 629
162 571
163 586
164 543
165 606
166 541
167 578
168 531
169 522
170 553
171 518
172 443
173 495
174 488
175 476
176 499
177 473
178 460
179 467
180 472
181 449
182 412
183 406
184 419
185 384
186 385
187 378
188 426
189 362
190 380
191 387
192 351
193 328
194 328
195 351
196 349
197 370
198 322
199 305
200 280
201 321
202 288
203 306
204 297
205 305
206 278
207 292
208 281
209 269
210 284
211 282
212 259
213 284
214 255
215 230
216 262
217 260
218 283
219 241
220 254
221 250
222 252
223 208
224 242
225 233
226 243
227 228
228 222
229 225
230 219
231 199
232 206
233 206
234 209
235 203
236 187
237 203
238 181
239 204
240 184
241 187
242 194
243 192
244 163
245 180
246 166
247 187
248 187
249 161
250 175
251 159
252 150
253 158
254 157
255 161
256 147
257 154
258 143
259 153
260 136
261 134
262 127
263 127
264 136
265 111
266 147
267 144
268 135
269 124
270 148
271 152
272 143
273 124
274 123
275 125
276 129
277 124
278 109
279 117
280 134
281 114
282 110
283 115
284 104
285 86
286 104
287 111
288 101
289 108
290 112
291 114
292 97
293 85
294 106
295 97
296 104
297 117
298 95
299 97
300 89
301 77
302 77
303 69
304 89
305 88
306 85
307 91
308 101
309 85
310 76
311 81
312 70
313 75
314 85
315 73
316 79
317 74
318 69
319 80
320 75
321 71
322 83
323 66
324 55
325 68
326 67
327 55
328 59
329 70
330 65
331 59
332 61
333 61
334 64
335 61
336 58
337 53
338 52
339 39
340 62
341 42
342 44
343 41
344 46
345 64
346 51
347 35
348 44
349 45
350 50
351 42
352 44
353 38
354 31
355 44
356 48
357 45
358 34
359 34
360 40
361 44
362 51
363 46
364 36
365 43
366 27
367 31
368 39
369 32
370 37
371 29
372 25
373 28
374 26
375 34
376 30
377 21
378 23
379 31
380 32
381 22
382 29
383 23
384 23
385 32
386 35
387 26
388 27
389 22
390 27
391 26
392 29
393 23
394 17
395 18
396 24
397 22
398 14
399 27
400 26
401 24
402 22
403 19
404 17
405 20
406 13
407 14
408 27
409 16
410 21
411 19
412 17
413 20
414 14
415 21
416 18
417 18
418 10
419 17
420 20
421 11
422 15
423 18
424 23
425 16
426 17
427 18
428 10
429 12
430 16
431 16
432 15
433 16
434 10
435 14
436 14
437 17
438 13
439 6
440 10
441 18
442 10
443 16
444 11
445 11
446 17
447 13
448 17
449 13
450 9
451 13
452 18
453 13
454 16
455 11
456 17
457 15
458 13
459 11
460 11
461 12
462 12
463 6
464 8
465 4
466 12
467 12
468 20
469 9
470 15
471 8
472 12
473 14
474 12
475 6
476 11
477 12
478 6
479 10
480 8
481 8
482 13
483 7
484 12
485 6
486 7
487 6
488 8
489 5
490 4
491 7
492 2
493 10
494 7
495 7
496 5
497 10
498 7
499 10
500 9
501 4
502 6
503 4
504 12
505 10
506 6
507 8
508 6
509 15
510 7
511 6
512 6
513 8
514 10
515 9
516 11
517 10
518 3
519 8
520 8
521 6
522 8
523 5
524 3
525 7
526 10
527 4
528 7
529 4
530 4
531 6
532 8
533 5
534 5
535 3
536 2
537 1
538 3
539 7
540 9
541 9
542 7
543 1
544 4
545 9
546 2
547 3
548 6
549 5
550 9
551 6
552 3
553 2
554 3
555 4
556 11
557 9
558 3
559 7
560 6
561 5
562 6
563 4
564 8
565 2
566 7
567 4
568 4
569 7
570 4
571 4
572 5
573 2
574 5
575 5
576 2
577 2
578 4
579 4
580 7
581 5
582 4
583 2
584 2
585 1
586 2
587 1
588 7
589 4
590 1
591 2
592 5
594 2
595 1
596 6
597 4
598 3
599 2
601 4
602 1
603 3
604 6
605 3
606 4
607 3
608 5
609 5
610 2
611 2
612 4
613 4
614 5
615 3
616 3
617 1
618 1
619 2
620 5
621 2
622 4
623 3
624 2
625 1
626 1
627 2
628 5
629 3
630 1
631 1
632 3
633 2
634 5
635 3
637 2
638 2
639 3
640 1
641 1
642 1
643 3
644 2
645 3
646 4
647 5
648 2
650 4
651 2
653 2
654 2
656 1
657 1
658 2
659 1
660 1
661 1
662 1
664 1
665 3
669 1
670 2
671 1
673 2
674 2
675 2
676 1
678 1
679 1
680 1
684 1
685 2
687 4
689 2
691 1
692 1
695 3
698 1
700 3
702 1
703 1
705 1
706 2
708 2
711 1
712 1
714 3
716 1
717 1
719 2
721 4
722 1
729 1
732 1
733 1
734 2
736 1
737 1
738 1
740 1
743 1
745 1
746 2
748 1
749 3
750 1
753 1
754 2
756 1
759 1
760 2
761 3
762 1
764 1
769 2
771 2
780 1
781 1
787 1
788 2
791 2
795 1
799 1
800 1
807 1
808 1
809 1
812 1
813 1
817 1
819 1
825 1
827 1
831 1
839 1
840 1
843 2
847 1
854 1
855 1
868 1
871 2
872 2
899 1
901 1
902 1
908 1
924 1
931 1
934 1
955 1
1040 1
1112 1
#!/usr/bin/env python
# encoding: utf-8
"""
*plot a histogram of number of candidates that go into making ZTF objects*
:Author:
David Young
:Date Created:
April 8, 2020
Usage:
plot_ncand_histogram.py <candidate_file> <object_file>
Options:
candidate_file path to the csv export of ZTF ncand distribution - from candidate table
object_file path to the csv export of ZTF ncand distribution - from object table
-h, --help show this help message
-v, --version show version
-s, --settings the settings file
"""
################# GLOBAL IMPORTS ####################
import sys
import os
from fundamentals import tools
import unicodecsv as csv
import matplotlib.pyplot as plt
import numpy as np
def main(arguments=None):
"""
*The main function used when ``plot_ncand_histogram.py`` is run as a single script from the cl*
"""
# SETUP THE COMMAND-LINE UTIL SETTINGS
su = tools(
arguments=arguments,
docString=__doc__,
logLevel="WARNING",
options_first=False,
projectName=False
)
arguments, settings, log, dbConn = su.setup()
# UNPACK REMAINING CL ARGUMENTS USING `EXEC` TO SETUP THE VARIABLE NAMES
# AUTOMATICALLY
a = {}
for arg, val in list(arguments.items()):
if arg[0] == "-":
varname = arg.replace("-", "") + "Flag"
else:
varname = arg.replace("<", "").replace(">", "")
a[varname] = val
if arg == "--dbConn":
dbConn = val
a["dbConn"] = val
log.debug('%s = %s' % (varname, val,))
# OPEN THE OBJECTS TABLE DISTRIBUTION
with open(a["object_file"], 'rb') as csvFile:
csvReader = csv.DictReader(
csvFile, dialect='excel', delimiter=',', quotechar='"')
object_ncand = []
object_count = []
for row in csvReader:
thisCount = int(row["count(*)"])
nc = int(row["ncand"])
if nc <= 20:
object_ncand.append(int(row["ncand"]))
object_count.append(int(row["count(*)"]))
with open(a["candidate_file"], 'rb') as csvFile:
csvReader = csv.DictReader(
csvFile, dialect='excel', delimiter=',', quotechar='"')
cand_ncand = []
cand_count = []
total = 0
moreone = 0
moretwo = 0
morethree = 0
for row in csvReader:
thisCount = int(row["count(*)"])
nc = int(row["ncand"])
if nc:
total += thisCount
if nc > 1:
moreone += thisCount
if nc > 2:
moretwo += thisCount
if nc > 3:
morethree += thisCount
if nc <= 20:
cand_ncand.append(int(row["ncand"]))
cand_count.append(int(row["count(*)"]))
print("Total number of objects = %(total)s" % locals())
print("Objects with ncand > 1 : %(moreone)s" % locals())
print("Objects with ncand > 2 : %(moretwo)s" % locals())
print("Objects with ncand > 3 : %(morethree)s" % locals())
# CANDIDATE DATASET
height = cand_count
bars = cand_ncand
tick_pos = np.arange(len(bars))
bar_pos = np.arange(len(bars)) - 0.2
plt.bar(bar_pos, height, width=0.4, label='Candidates')
# OBJECT DATASET
height = object_count
bars = object_ncand
tick_pos = np.arange(len(bars)) - 1
bar_pos = np.arange(len(bars)) - 0.8
plt.bar(bar_pos, height, width=0.4, label='Objects')
# CREATE LABELS ON THE X-AXIS
plt.xticks(tick_pos, bars)
plt.yscale('log')
plt.ylabel("Number of ZTF Objects (log)")
plt.xlabel("ncand")
legend = plt.legend(loc="upper right", framealpha=1.0)
# Set the legend face color
frame = legend.get_frame()
frame.set_facecolor('#D2D1D1')
# Show graphic
# plt.show()
plt.savefig('ztf_ncand_dist.png', dpi=300)
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment