-
Notifications
You must be signed in to change notification settings - Fork 0
/
catls_utils.py
1078 lines (940 loc) · 38.1 KB
/
catls_utils.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Victor Calderon
# Created : 2018-05-08
# Last Modified: 2018-05-08
from __future__ import print_function, division, absolute_import
__author__ =['Victor Calderon']
__copyright__ =["Copyright 2018 Victor Calderon"]
__email__ =['victor.calderon@vanderbilt.edu']
__maintainer__ =['Victor Calderon']
__all__ =[ "catl_keys",
"catl_keys_prop",
"catl_sdss_dir",
"extract_catls",
"sdss_catl_clean",
"sdss_catl_clean_nmin",
"catl_sdss_merge"]
## Import modules
import os
import numpy as np
import pandas as pd
from collections import Counter
from cosmo_utils.utils import file_utils as fd
from cosmo_utils.utils import work_paths as wp
from cosmo_utils.utils import file_readers as fr
from cosmo_utils.custom_exceptions import LSSUtils_Error
## Functions
## Catalogue Keys - Main
def catl_keys(catl_kind, perf_opt=False, return_type='list'):
"""
Dictionary keys for the different types of catalogues
Parameters
----------
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
perf_opt : boolean, optional
Option for using a `perfect` mock catalogue.
return_type : {'list', 'dict'} str, optional
Type of output to the be returned. This variable is set to `list`
by default.
Options:
- 'list' : Returns the values as part of a list
- 'dict' : Returns the values as part of a python dictionary
Returns
----------
catl_keys : python dictionary or array_like
Dictionary/array with the proper keys for the catalogue(s).
Order : 1) `gm_key`, 2) `id_key`, 3) `galtype_key`
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
Examples
----------
>>> catl_keys('data', perf_opt=False, return_type='list')
['M_h', 'groupid', 'galtype']
>>> catl_keys('mocks', perf_opt=True, return_type='list')
['M_h', 'haloid', 'galtype']
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
# `catl_kind`
if not (catl_kind in ['data', 'mocks']):
msg = '{0} `catl_kind` ({1}) is not a valid input parameter!'.format(
file_msg, catl_kind)
raise LSSUtils_Error(msg)
# `return_type`
if not (return_type in ['list', 'dict']):
msg = '{0} `return_type` ({1}) is not a valid input parameter'.format(
file_msg, return_type)
raise LSSUtils_Error(msg)
# `perf_opt`
if not (isinstance(perf_opt, bool)):
msg = '{0} `perf_opt` ({1}) must be a boolean object!'.format(
file_msg, type(perf_opt))
raise LSSUtils_Error(msg)
##
## Perfect Catalogue
if catl_kind == 'data':
perf_opt = False
##
## Property keys
if catl_kind == 'data':
gm_key, id_key, galtype_key = ['M_h', 'groupid', 'galtype']
elif catl_kind == 'mocks':
if perf_opt:
gm_key, id_key, galtype_key = ['M_h', 'haloid', 'galtype']
else:
gm_key, id_key, galtype_key = ['M_group', 'groupid', 'g_galtype']
##
## Saving values
if return_type == 'dict':
catl_objs = { 'gm_key' : gm_key,
'id_key' : id_key,
'galtype_key' : galtype_key}
elif return_type == 'list':
catl_objs = [ gm_key, id_key, galtype_key]
return catl_objs
## Catalogue Keys - Properties
def catl_keys_prop(catl_kind, catl_info='members', return_type='list'):
"""
Dictionary keys for the diffeent galaxy and group properties of
catalogues.
Parameters
------------
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_info : {'members', 'groups'} str, optional
Option for which kind of catalogues to use.
Options:
- `members` : Member galaxies of group catalogues
- `groups` : Catalogues with `group` information.
return_type : {'list', 'dict'} str, optional
Type of output to the be returned. This variable is set to `list`
by default.
Options:
- 'list' : Returns the values as part of a list
- 'dict' : Returns the values as part of a python dictionary
Return
------------
catl_objs : python dictionary or array_like
Dictionary/array with the proper keys for the catalogue(s).
Order : 1) `ssfr_key`, 2) `mstar_key`
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
Examples
------------
>>> catl_keys_prop('data')
['logssfr', 'logMstar_JHU']
>>> catl_keys_prop('mocks', catl_info='groups', return_type='list')
['logssfr', 'logMstar']
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_kind_valid = ['data' , 'mocks' ]
catl_info_valid = ['members', 'groups']
return_type_valid = ['list' , 'dict' ]
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(
file_msg, catl_kind)
raise LSSUtils_Error(msg)
# `catl_info`
if not (catl_info in catl_info_valid):
msg = '{0} `catl_info` ({1}) is not a valid input!'.format(
file_msg, catl_info)
raise LSSUtils_Error(msg)
# `return_type`
if not (return_type in return_type_valid):
msg = '{0} `return_type` ({1}) is not a valid input!'.format(
file_msg, return_type)
raise LSSUtils_Error(msg)
##
## Property keys
##
## Data
if (catl_kind == 'data'):
## Members
if catl_info == 'members':
# SSFR and Stellar mass
logssfr_key, logmstar_key = ['logssfr', 'logMstar_JHU']
## Groups
if catl_info == 'groups':
# SSFR and Stellar mass
logssfr_key, logmstar_key = ['logssfr_tot', 'logMstar_tot']
##
## Mocks
if (catl_kind == 'mocks'):
## Members
if catl_info == 'members':
# SSFR and Stellar mass
logssfr_key, logmstar_key = ['logssfr', 'logMstar']
## Groups
if catl_info == 'groups':
# SSFR and Stellar mass
logssfr_key, logmstar_key = ['logssfr', 'logMstar']
##
## Saving values
if return_type == 'dict':
catl_objs = { 'logssfr_key' : logssfr_key ,
'logmstar_key': logmstar_key}
elif return_type == 'list':
catl_objs = [ logssfr_key, logmstar_key]
return catl_objs
## Extracting path of synthetic catalogues
def catl_sdss_dir(catl_kind='data', catl_type='mr', sample_s='19',
catl_info='members', halotype='fof', clf_method=3, hod_n=0, clf_seed=1235,
perf_opt=False, print_filedir=True):
"""
Extracts the path to the synthetic catalogues.
Parameters
-----------
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_type : {'mr', 'mstar'} str, optional
Type of catalogue to use. It shows which abundance matching method
was used for the CLF when assigning halo masses. This variable is
set to 'mr' by default.
Options:
- `mr` : Uses r-band absolute magnitude
- `mstar` : Uses stellar masses
sample_s : {'19', '20', '21'} str, optional
Volume-limited sample to use. This variable is set to '19' by default.
Options:
- '19' : Uses the Mr19 volume-limited sample, i.e. 'Consuelo'
- '20' : Uses the Mr20 volume-limited sample, i.e. 'Esmeralda'
- '21' : Uses the Mr21 volume-limited sample, i.e. 'Carmen'
catl_info : {'members', 'groups'} str, optional
Option for which kind of catalogues to use.
Options:
- `members` : Member galaxies of group catalogues
- `groups` : Catalogues with `group` information.
halotype : {'fof', 'so'} str, optional
Type of the dark matter halo of the simulation used to create the
synthetic catalogues. This variable is set to `fof` by default.
Options:
- 'fof': Friends-of-Friends halos.
- 'so' : Spherical overdensity halos.
clf_method : {1, 2, 3} int, optional
Method for assigning galaxy properties to mock galaxies.
This variable is set to `3` by default.
Options:
- `1` : Independent assigment of (g-r) color, sersic, and log(ssfr)
- `2` : (g-r) decides active/passive designation and draw values
independently.
- `3` : (g-r) decides active/passive designations, and
assigns other galaxy properties for that given galaxy.
hod_n : {0, 1} int, optional
HOD model to use. Only relevant when `catl_kind == mocks`.
clf_seed : int, optional
Seed used for the `CLF` random seed. This variable is set to `1235`
by default.
perf_opt : boolean, optional
If True, it chooses to analyze the `perfect` set of synthetic
catalogues. This variable is set to `False` by default.
print_filedir : boolean, optional
If True, the output directory is printed onto the screen.
Returns
-----------
catls_path : str
Path to the desired set of synthetic catalogues.
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_kind_valid = ['data', 'mocks' ]
catl_type_valid = ['mr', 'mstar']
sample_s_valid = ['19', '20', '21']
catl_info_valid = ['members', 'groups']
halotype_valid = ['fof', 'so']
clf_method_valid = [1, 2, 3]
hod_n_valid = [0, 1]
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
catl_kind)
raise LSSUtils_Error(msg)
# `catl_type`
if not (catl_type in catl_type_valid):
msg = '{0} `catl_type` ({1}) is not a valid input!'.format(file_msg,
catl_type)
raise LSSUtils_Error(msg)
# `sample_s`
if not (sample_s in sample_s_valid):
msg = '{0} `sample_s` ({1}) is not a valid input!'.format(file_msg,
sample_s)
raise LSSUtils_Error(msg)
# `catl_info`
if not (catl_info in catl_info_valid):
msg = '{0} `catl_info` ({1}) is not a valid input!'.format(file_msg,
catl_info)
raise LSSUtils_Error(msg)
# `halotype`
if not (halotype in halotype_valid):
msg = '{0} `halotype` ({1}) is not a valid input!'.format(file_msg,
halotype)
raise LSSUtils_Error(msg)
# `clf_method`
if not (clf_method in clf_method_valid):
msg = '{0} `clf_method` ({1}) is not a valid input!'.format(file_msg,
clf_method)
raise LSSUtils_Error(msg)
# `hod_n`
if not (hod_n in hod_n_valid):
msg = '{0} `hod_n` ({1}) is not a valid input!'.format(file_msg,
hod_n)
raise LSSUtils_Error(msg)
# `perf_opt`
if not (isinstance(perf_opt, bool)):
msg = '{0} `perf_opt` ({1}) is not a valid type!'.format(file_msg,
type(perf_opt))
raise LSSUtils_Error(msg)
# `print_filedir`
if not (isinstance(print_filedir, bool)):
msg = '{0} `print_filedir` ({1}) is not a valid type!'.format(file_msg,
type(print_filedir))
raise LSSUtils_Error(msg)
##
## Type of catalogue
if catl_info == 'members':
catl_info_str = 'member_galaxy_catalogues'
elif catl_info == 'groups':
catl_info_str = 'group_galaxy_catalogues'
##
## Perfect catalogue
if perf_opt:
# Data
if catl_kind == 'data':
msg = '{0} Invalid `catl_kind` ({1}) for when `perf_opt == True'
msg = msg.format(file_msg, catl_kind)
raise LSSUtils_Error(msg)
# Mocks
catl_info_perf_str = 'perfect_{0}'.format(catl_info_str)
else:
# Mocks
catl_info_perf_str = catl_info_str
##
## Extracting path of the files
# Data
if catl_kind == 'data':
# Joining paths
filedir = os.path.join( wp.get_output_path(),
'SDSS',
catl_kind,
catl_type,
'Mr' + sample_s,
catl_info_perf_str)
# Mocks
if catl_kind == 'mocks':
# Joining paths
filedir = os.path.join( wp.get_output_path(),
'SDSS',
catl_kind,
'halos_{0}'.format(halotype),
'hod_model_{0}'.format(hod_n),
'clf_seed_{0}'.format(clf_seed),
'clf_method_{0}'.format(clf_method),
catl_type,
'Mr' + sample_s,
catl_info_perf_str)
##
## Making sure `filedir` exists
if not (os.path.exists(filedir)):
msg = '{0} `filedir` ({1}) does NOT exist! Check input variables'
msg = msg.format(file_msg, filedir)
raise LSSUtils_Error(msg)
##
## Printing out paths
if print_filedir:
print('{0} `filedir`: {1}'.format(file_msg, filedir))
return filedir
## Extracting list of synthetic catalogues given input parameters
def extract_catls(catl_kind='data', catl_type='mr', sample_s='19',
datatype='.hdf5', catl_info='members', halotype='fof', clf_method=3,
hod_n=0, clf_seed=1235, perf_opt=False, return_len=False,
print_filedir=True):
"""
Extracts a list of synthetic catalogues given input parameters
Parameters
------------
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_type : {'mr', 'mstar'} str, optional
Type of catalogue to use. It shows which abundance matching method
was used for the CLF when assigning halo masses. This variable is
set to 'mr' by default.
Options:
- `mr` : Uses r-band absolute magnitude
- `mstar` : Uses stellar masses
sample_s : {'19', '20', '21'} str, optional
Volume-limited sample to use. This variable is set to '19' by default.
Options:
- '19' : Uses the Mr19 volume-limited sample, i.e. 'Consuelo'
- '20' : Uses the Mr20 volume-limited sample, i.e. 'Esmeralda'
- '21' : Uses the Mr21 volume-limited sample, i.e. 'Carmen'
datatype : {'.hdf5'} str, optional
Data type of the files to be indexed in the folder. This variable
is set to '.hdf5' by default.
catl_info : {'members', 'groups'} str, optional
Option for which kind of catalogues to use.
Options:
- `members` : Member galaxies of group catalogues
- `groups` : Catalogues with `group` information.
halotype : {'fof', 'so'} str, optional
Type of the dark matter halo of the simulation used to create the
synthetic catalogues. This variable is set to `fof` by default.
Options:
- 'fof': Friends-of-Friends halos.
- 'so' : Spherical overdensity halos.
clf_method : {1, 2, 3} int, optional
Method for assigning galaxy properties to mock galaxies.
This variable is set to `3` by default.
Options:
- `1` : Independent assigment of (g-r) color, sersic, and log(ssfr)
- `2` : (g-r) decides active/passive designation and draw values
independently.
- `3` : (g-r) decides active/passive designations, and
assigns other galaxy properties for that given galaxy.
hod_n : {0, 1} int, optional
HOD model to use. Only relevant when `catl_kind == mocks`.
clf_seed : int, optional
Seed used for the `CLF` random seed. This variable is set to `1235`
by default.
perf_opt : boolean, optional
If True, it chooses to analyze the `perfect` set of synthetic
catalogues. This variable is set to `False` by default.
return_len : boolean, optional
If True, the function returns the total number of elements in
the folder that match the criteria.
print_filedir : boolean, optional
If True, the output directory is printed onto the screen.
Returns
------------
catl_arr : `numpy.ndarray`
Array of elements/files matching the `datatype` type in the directory.
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_kind_valid = ['data', 'mocks' ]
catl_type_valid = ['mr', 'mstar']
sample_s_valid = ['19', '20', '21']
catl_info_valid = ['members', 'groups']
halotype_valid = ['fof', 'so']
clf_method_valid = [1, 2, 3]
hod_n_valid = [0, 1]
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
catl_kind)
raise LSSUtils_Error(msg)
# `catl_type`
if not (catl_type in catl_type_valid):
msg = '{0} `catl_type` ({1}) is not a valid input!'.format(file_msg,
catl_type)
raise LSSUtils_Error(msg)
# `sample_s`
if not (sample_s in sample_s_valid):
msg = '{0} `sample_s` ({1}) is not a valid input!'.format(file_msg,
sample_s)
raise LSSUtils_Error(msg)
# `catl_info`
if not (catl_info in catl_info_valid):
msg = '{0} `catl_info` ({1}) is not a valid input!'.format(file_msg,
catl_info)
raise LSSUtils_Error(msg)
# `halotype`
if not (halotype in halotype_valid):
msg = '{0} `halotype` ({1}) is not a valid input!'.format(file_msg,
halotype)
raise LSSUtils_Error(msg)
# `clf_method`
if not (clf_method in clf_method_valid):
msg = '{0} `clf_method` ({1}) is not a valid input!'.format(file_msg,
clf_method)
raise LSSUtils_Error(msg)
# `hod_n`
if not (hod_n in hod_n_valid):
msg = '{0} `hod_n` ({1}) is not a valid input!'.format(file_msg,
hod_n)
raise LSSUtils_Error(msg)
# `perf_opt`
if not (isinstance(perf_opt, bool)):
msg = '{0} `perf_opt` ({1}) is not a valid type!'.format(file_msg,
type(perf_opt))
raise LSSUtils_Error(msg)
# `print_filedir`
if not (isinstance(print_filedir, bool)):
msg = '{0} `print_filedir` ({1}) is not a valid type!'.format(file_msg,
type(print_filedir))
raise LSSUtils_Error(msg)
# `return_len`
if not (isinstance(return_len, bool)):
msg = '{0} `return_len` ({1}) is not a valid type!'.format(file_msg,
type(return_len))
raise LSSUtils_Error(msg)
# `datatype`
if not (isinstance(datatype, str)):
msg = '{0} `datatype` ({1}) is not a valid type!'.format(file_msg,
type(datatype))
raise LSSUtils_Error(msg)
##
## Extracting the path of the catalogues
filedir = catl_sdss_dir( catl_kind=catl_kind,
catl_type=catl_type,
sample_s=sample_s,
catl_info=catl_info,
halotype=halotype,
clf_method=clf_method,
hod_n=hod_n,
clf_seed=clf_seed,
perf_opt=perf_opt,
print_filedir=print_filedir)
##
## Convertint to array
catl_arr = np.sort(fd.Index(filedir, datatype))
# Checking number of elements
if len(catl_arr) == 0:
msg = '{0} `catl_arr` contains 0 entries!'.format(file_msg)
raise LSSUtils_Error(msg)
##
## Returning elements
if return_len:
return catl_arr, len(catl_arr)
else:
return catl_arr
## Cleaning the catalogue removing `failed` values
def sdss_catl_clean(catl_pd, catl_kind, catl_info='members', reindex=True):
"""
Cleans the catalogue removing `failed` values.
Parameters
-----------
catl_pd : `pandas.DataFrame`
Dataset with the catalogue information.
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_info : {'members', 'groups'} str, optional
Option for which kind of catalogues to use.
Options:
- `members` : Member galaxies of group catalogues
- `groups` : Catalogues with `group` information.
reindex : boolean, optional
If True, the output catalogue is re-indexed.
Return
-----------
catl_pd_clean : `pandas.DataFrame`
Cleaned version of `catl_pd`, after having removed `failed` values.
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_kind_valid = ['data', 'mocks' ]
catl_info_valid = ['members', 'groups']
# `catl_pd`
if not (isinstance(catl_pd, pd.DataFrame)):
msg = '{0} `catl_pd` ({1}) is not a valid type!'.format(file_msg,
catl_pd)
raise LSSUtils_Error(msg)
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
catl_kind)
raise LSSUtils_Error(msg)
# `catl_info`
if not (catl_info in catl_info_valid):
msg = '{0} `catl_info` ({1}) is not a valid input!'.format(file_msg,
catl_info)
raise LSSUtils_Error(msg)
# `reindex
if not (isinstance(reindex, bool)):
msg = '{0} `reindex` ({1}) is not a valid type!'.format(file_msg,
type(reindex))
raise LSSUtils_Error(msg)
##
## Defining `failed` values
ssfr_fail_arr = [0, -99, -999, np.nan]
mstar_fail_arr = [-1, 0, np.nan]
##
## Getting keys for catalogues
( logssfr_key ,
logmstar_key ) = catl_keys_prop( catl_kind=catl_kind,
catl_info=catl_info,
return_type='list')
##
## Cleaning catalogue entries
# Data
if catl_kind == 'data':
# Clean version
catl_pd_clean = catl_pd[~catl_pd[logssfr_key].isin(ssfr_fail_arr) &\
~catl_pd[logmstar_key].isin(mstar_fail_arr)]
# Mocks
if catl_kind == 'mocks':
# Clean version
catl_pd_clean = catl_pd[~catl_pd[logssfr_key].isin(ssfr_fail_arr)]
##
## Reindexing
if reindex:
catl_pd_clean.reset_index(inplace=True, drop=True)
return catl_pd_clean
## Cleans dataset and includes only groups above a number of galaxy threshold
def sdss_catl_clean_nmin(catl_pd, catl_kind, catl_info='members', nmin=1,
perf_opt=False):
"""
Cleans the catalogue removing `failed` values, and only includes
galaxies that are in groups/halos above a `nmin` threshold.
Parameters
-----------
catl_pd : `pandas.DataFrame`
Dataset with the catalogue information.
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_info : {'members', 'groups'} str, optional
Option for which kind of catalogues to use.
Options:
- `members` : Member galaxies of group catalogues
- `groups` : Catalogues with `group` information.
nmin : int, optional
Minimum group richness to have in the (galaxy) group catalogue.
This variable is set to `1` by default.
perf_opt : boolean, optional
Option for using a `perfect` mock catalogue.
Return
-----------
catl_pd_clean : `pandas.DataFrame`
Cleaned version of `catl_pd` after having removed `failed` values,
and having choosen only galaxies within groups above a group richness
threshold of `nmin`.
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_kind_valid = ['data', 'mocks' ]
catl_info_valid = ['members', 'groups']
# `catl_pd`
if not (isinstance(catl_pd, pd.DataFrame)):
msg = '{0} `catl_pd` ({1}) is not a valid type!'.format(file_msg,
catl_pd)
raise LSSUtils_Error(msg)
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
catl_kind)
raise LSSUtils_Error(msg)
# `catl_info`
if not (catl_info in catl_info_valid):
msg = '{0} `catl_info` ({1}) is not a valid input!'.format(file_msg,
catl_info)
raise LSSUtils_Error(msg)
# `nmin`
if not ((nmin > 0) and (isinstance(nmin, int))):
msg = '{0} `nmin` must be an integer and have a value above `0`'
msg = msg.format(file_msg)
raise LSSUtils_Error(msg)
# `perf_opt`
if not (isinstance(perf_opt, bool)):
msg = '{0} `perf_opt` ({1}) is not a valid type!'.format(file_msg,
type(perf_opt))
raise LSSUtils_Error(msg)
##
## Types of galaxies
cens = int(1)
nmin = int(nmin)
##
## Getting keys for catalogue
( gm_key,
id_key,
galtype_key) = catl_keys( catl_kind,
return_type='list',
perf_opt=perf_opt)
##
## Cleaning catalogue entries
catl_pd_clean_all = sdss_catl_clean( catl_pd,
catl_kind=catl_kind,
catl_info=catl_info,
reindex=True)
## Choosing only galaxies in groups of richness >= `nmin`
# Member galaxies
if catl_info == 'members':
# Centrals
catl_pd_cens = catl_pd_clean_all.loc[(catl_pd_clean_all[galtype_key] == cens), id_key]
catl_pd_cl = catl_pd_clean_all[(catl_pd_clean_all[id_key].isin(catl_pd_cens))]
# Group counts
group_counts = Counter(catl_pd_cl[id_key])
group_ngals = np.array([xx for xx in group_counts.keys() if
group_counts[xx] >= nmin])
# Cleaned version
catl_pd_clean = catl_pd_cl[catl_pd_cl[id_key].isin(group_ngals)]
catl_pd_clean.reset_index(inplace=True, drop=True)
# Group catalogue
if catl_info == 'groups':
if ('ngals' in catl_pd_clean_all.columns.tolist()):
catl_pd_clean = catl_pd_clean_all.loc[catl_pd_clean_all['ngals'] >= nmin]
catl_pd_clean.reset_index(inplace=True, drop=True)
else:
msg = '{0} Key `ngals` not found in DataFrame ... Exiting!'
msg = msg.format(file_msg)
raise LSSUtils_Error(msg)
return catl_pd_clean
## Merges the member and group catalogues for a given set of input parameters
def catl_sdss_merge(catl_pd_ii, catl_kind='data', catl_type='mr',
sample_s='19', halotype='fof', clf_method=3, hod_n=0, clf_seed=1235,
perf_opt=False, return_memb_group=False, print_filedir=False):
"""
Merges the member and group catalogues for a given set of input parameters,
and returns a modified version of the galaxy group catalogues with added
info about the galaxy groups.
Parameters
------------
catl_pd_ii : int
Index of the catalogue to match,
from :func:`~cosmo_utils.mock_catalogues.catls_utils.extract_catls`
function.
catl_kind : {'data', 'mocks'} str, optional
Type of catalogue to use. This variable is set to `data` by default.
Options:
- `data` : catalogues come from SDSS `real` catalogue
- `mocks` : catalogue come from SDSS `mock` catalogues
catl_type : {'mr', 'mstar'} str, optional
Type of catalogue to use. It shows which abundance matching method
was used for the CLF when assigning halo masses. This variable is
set to 'mr' by default.
Options:
- `mr` : Uses r-band absolute magnitude
- `mstar` : Uses stellar masses
sample_s : {'19', '20', '21'} str, optional
Volume-limited sample to use. This variable is set to '19' by default.
Options:
- '19' : Uses the Mr19 volume-limited sample, i.e. 'Consuelo'
- '20' : Uses the Mr20 volume-limited sample, i.e. 'Esmeralda'
- '21' : Uses the Mr21 volume-limited sample, i.e. 'Carmen'
halotype : {'fof', 'so'} str, optional
Type of the dark matter halo of the simulation used to create the
synthetic catalogues. This variable is set to `fof` by default.
Options:
- 'fof': Friends-of-Friends halos.
- 'so' : Spherical overdensity halos.
clf_method : {1, 2, 3} int, optional
Method for assigning galaxy properties to mock galaxies.
This variable is set to `3` by default.
Options:
- `1` : Independent assigment of (g-r) color, sersic, and log(ssfr)
- `2` : (g-r) decides active/passive designation and draw values
independently.
- `3` : (g-r) decides active/passive designations, and
assigns other galaxy properties for that given galaxy.
hod_n : {0, 1} int, optional
HOD model to use. Only relevant when `catl_kind == mocks`.
clf_seed : int, optional
Seed used for the `CLF` random seed. This variable is set to `1235`
by default.
perf_opt : boolean, optional
If True, it chooses to analyze the `perfect` set of synthetic
catalogues. This variable is set to `False` by default.
return_memb_group : `bool`, optional
If True, the function returns the member and group catalogues,
along with the merged catalogue.
It returns ``<memb_group_pd, memb_pd, group_pd>``
print_filedir : boolean, optional
If True, the output directory is printed onto the screen.
Return
------------
memb_group_pd : `pandas.DataFrame`
Combined version of the i-th member and group catalogues.
It contains both galaxy and group information.
memb_pd : `pandas.DataFrame`
Catalogue of the member galaxies of the i-th catalogue.
This catalogue contains information of the `member galaxies`.
group_pd : `pandas.DataFrame`
Catalogue of the groups of the i-th catalogue.
This catalogue contains information of the `galaxy groups`.
Raises
------------
LSSUtils_Error : Exception from `LSSUtils_Error`
Program exception if input parameters are accepted.
"""
file_msg = fd.Program_Msg(__file__)
## Checking input parameters
catl_pd_ii_valid = (float, int)
catl_kind_valid = ['data', 'mocks' ]
catl_type_valid = ['mr', 'mstar']
sample_s_valid = ['19', '20', '21']
catl_info_valid = ['members', 'groups']
halotype_valid = ['fof', 'so']
clf_method_valid = [1, 2, 3]
hod_n_valid = [0, 1]
# `catl_pd_ii`
if (isinstance(catl_pd_ii, catl_pd_ii_valid)):
catl_pd_ii = int(catl_pd_ii)
else:
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
type(catl_kind))
raise LSSUtils_Error(msg)
# `catl_kind`
if not (catl_kind in catl_kind_valid):
msg = '{0} `catl_kind` ({1}) is not a valid input!'.format(file_msg,
catl_kind)
raise LSSUtils_Error(msg)
# `catl_type`
if not (catl_type in catl_type_valid):
msg = '{0} `catl_type` ({1}) is not a valid input!'.format(file_msg,
catl_type)
raise LSSUtils_Error(msg)
# `sample_s`
if not (sample_s in sample_s_valid):
msg = '{0} `sample_s` ({1}) is not a valid input!'.format(file_msg,
sample_s)
raise LSSUtils_Error(msg)
# `halotype`
if not (halotype in halotype_valid):
msg = '{0} `halotype` ({1}) is not a valid input!'.format(file_msg,
halotype)
raise LSSUtils_Error(msg)
# `clf_method`
if not (clf_method in clf_method_valid):
msg = '{0} `clf_method` ({1}) is not a valid input!'.format(file_msg,
clf_method)
raise LSSUtils_Error(msg)
# `hod_n`
if not (hod_n in hod_n_valid):
msg = '{0} `hod_n` ({1}) is not a valid input!'.format(file_msg,
hod_n)
raise LSSUtils_Error(msg)
# `perf_opt`
if not (isinstance(perf_opt, bool)):
msg = '{0} `perf_opt` ({1}) is not a valid type!'.format(file_msg,
type(perf_opt))
raise LSSUtils_Error(msg)
# `return_memb_group`
if not (isinstance(return_memb_group, bool)):
msg = '{0} `return_memb_group` ({1}) is not a valid type!'.format(file_msg,
type(return_memb_group))
raise LSSUtils_Error(msg)
# `print_filedir`
if not (isinstance(print_filedir, bool)):
msg = '{0} `print_filedir` ({1}) is not a valid type!'.format(file_msg,
type(print_filedir))
raise LSSUtils_Error(msg)
##
## Extracting catalogues given input parameters
( memb_arr,
memb_len) = extract_catls( catl_kind=catl_kind,
catl_type=catl_type,
sample_s=sample_s,
halotype=halotype,
clf_method=clf_method,
hod_n=hod_n,
clf_seed=clf_seed,
perf_opt=perf_opt,
catl_info='members',
return_len=True,
print_filedir=print_filedir)
# Checking number of catalogues
if catl_pd_ii > (memb_len - 1):
msg = '{0} `catl_pd_ii` ({1}) is OUT of range ({2})!'.format(
file_msg, catl_pd_ii, memb_len)
raise LSSUtils_Error(msg)
##
## Extracting group catalogue
# i-th Galaxy catalogue
memb_path = memb_arr[catl_pd_ii]
# i-th Galaxy Group catalogue
group_path = catl_sdss_dir( catl_kind=catl_kind,
catl_type=catl_type,
sample_s=sample_s,
halotype=halotype,
clf_method=clf_method,
hod_n=hod_n,
clf_seed=clf_seed,
perf_opt=perf_opt,
catl_info='groups',
print_filedir=print_filedir)
##
## Paths to catalogue
# Mocks
if catl_kind == 'mocks':
group_path += os.path.basename(memb_path).replace('memb', 'group')
# Data