1212import logging
1313import warnings
1414from past .builtins import basestring # Python 2&3 compatibility
15- # import pudb
1615
1716# TODO: Include noise, pixscale
1817
@@ -54,29 +53,33 @@ class ngmixCatalog(object):
5453 _req_params = { 'file_name' : str , 'bands' : str }
5554 _opt_params = { 'dir' : str , 'catalog_type' : str , 'snr_min' : float , 'snr_max' : float ,
5655 't_frac' : float , 't_min' : float , 't_max' : float , 'version' : str ,
57- 'de_redden' : bool }
56+ 'de_redden' : bool , 'TdByTe' : float }
5857 _single_params = []
5958 _takes_rng = False
6059
6160 # Only these ngmix catalog types currently supported
6261 # `gauss`: Single Gaussian fit
6362 # `cm`: Combined bulge+disk model
64- # `mof `: CM model with multi-object fitting
63+ # `bdf `: CM model with buldge-disk ratio fixed
6564 # NOTE: In principle, should be able to handle any type supported by ngmix
66- _valid_catalog_types = ['gauss' ,'cm' ,'mof' ]
65+ # See Issue #79
66+ _valid_catalog_types = ['gauss' ,'cm' ,'bdf' ]
6767
6868 # Only these color bands are currently supported for an ngmix catalog
6969 _valid_band_types = 'griz'
7070
7171 # Dictionary of color band flux to array index in ngmix catalogs
72+ # TODO: This should be grabbed from the fits header rather than be hardcoded
73+ # See Issue #80
7274 _band_index = {'g' : 0 , 'r' : 1 , 'i' : 2 , 'z' : 3 }
7375
74- # The catalog column name prefix doens't always match the catalog type (e.g. 'mof' has a prefix
75- # of 'cm' for most columns). Set this for each new supported catalog type.
76- _cat_col_prefix = {'gauss' : 'gauss' , 'cm' : 'cm' , 'mof' : 'cm' }
76+ # NOTE: In previous versions, the catalog column name prefix didn't always
77+ # match the catalog type (e.g. 'mof' had a prefix of 'cm' for most columns).
78+ # This shouldn't be needed in the future but leaving for now
79+ _cat_col_prefix = {'gauss' : 'gauss' , 'cm' : 'cm' , 'bdf' : 'bdf' }
7780
7881 def __init__ (self , file_name , bands , dir = None , catalog_type = None , snr_min = None , snr_max = None ,
79- t_frac = None , t_min = None , t_max = None , version = None , de_redden = False ,
82+ t_frac = None , t_min = None , t_max = None , version = None , de_redden = False , TdByTe = None ,
8083 _nobjects_only = False ):
8184
8285 if dir :
@@ -102,10 +105,10 @@ def __init__(self, file_name, bands, dir=None, catalog_type=None, snr_min=None,
102105 # Attempt to determine catalog type from filename (this is generally true for DES ngmix
103106 # catalogs)
104107 match = 0
105- for type in self ._valid_catalog_types :
106- if type in self .file_name :
108+ for t in self ._valid_catalog_types :
109+ if t in self .file_name :
107110 match += 1
108- self .cat_type = type
111+ self .cat_type = t
109112 # Reject if multiple matches
110113 if match == 0 :
111114 raise ValueError ("No inputted ngmix catalog type, and no matches in filename! "
@@ -114,8 +117,17 @@ def __init__(self, file_name, bands, dir=None, catalog_type=None, snr_min=None,
114117 raise ValueError ("No inputted ngmix catalog type, and multiple matches in filename!"
115118 " Please set a valid catalog type." )
116119
120+ if TdByTe is not None :
121+ if self .cat_type != 'bdf' :
122+ raise ValueError ('Can only set a constant `TdByTe` for ngmix type `bdf`!' )
123+ if TdByTe < 0 :
124+ raise ValueError ('TdByTe must be non-negative!' )
125+ else :
126+ # This should almost always be 1
127+ TdByTe = 1.
128+ self ._TdByTe = TdByTe
129+
117130 # Catalog column name prefixes don't always match catalog type
118- # (e.g. 'cm' is still used for many 'mof' columns)
119131 self .col_prefix = self ._cat_col_prefix [self .cat_type ]
120132
121133 if isinstance (bands , basestring ):
@@ -242,13 +254,13 @@ def getFlags(self):
242254 # General flags
243255 self .flags = self .catalog ['flags' ]
244256
245- # TODO: Look at these in more detail!
257+ # We don't want to cut on these explicitly anymore:
258+
246259 #self.obj_flags = self.catalog['obj_flags']
247260
248261 # ngmix catalog-specific flags
249262 #self.ngmix_flags = self.catalog[self.col_prefix+'_flags']
250263
251- # TODO: Check for additional flags
252264 # if self.cat_type == 'mof':
253265 # # mof has additional flags
254266 # self.mof_flags = self.catalog[self.col_prefix+'_mof_flags']
@@ -269,7 +281,8 @@ def makeMask(self):
269281
270282 # For now, remove objects with any flags present
271283 mask [self .flags != 0 ] = False
272- # TODO: We probably want to remove these!
284+
285+ # No longer do these explicitly:
273286 # mask[self.obj_flags !=0] = False
274287 # mask[self.ngmix_flags !=0] = False
275288 # Extra flags for 'mof' catalogs
@@ -408,24 +421,43 @@ def ngmix2gs(self, index, band, gsparams=None):
408421
409422 flux = self .catalog [flux_colname ][index ][self ._band_index [band ]]
410423
411- # Gaussian-Mixture parameters are in the format of:
424+ # NOTE: It used to be the case that all Gaussian-Mixture parameters
425+ # were in the format of:
412426 # gm_pars = [centroid1, centroid2, g1, g2, T, flux]
413- # (this is identical to ngmix catalogs, except that flux is a vector
414- # of fluxes in all color bands)
415- gm_pars = [0.0 , 0.0 , g1 , g2 , T , flux ]
416-
417- # Build the appropriate Gaussian mixture for a cm-model
418- fracdev = self .catalog [cp + '_fracdev' ][index ]
419- TdByTe = self .catalog [cp + '_TdByTe' ][index ]
420- gm = ngmix .gmix .GMixCM (fracdev , TdByTe , gm_pars )
427+ # (this is identical to ngmix `pars`, except that flux is a vector
428+ # of fluxes in all bands)
429+ # However, this now depends on the gmix type, so we have to wait
430+ # gm_pars = [0.0, 0.0, g1, g2, T, flux]
431+
432+ # TODO: Implement this once we get a response back from Erin why
433+ # CM isn't included in this function
434+ # https://github.com/esheldon/ngmix/blob/master/ngmix/gmix.py#L39
435+ # This allows us to construct the given gmix type without knowing
436+ # gm.make_gmix_model(pars, model, **kw):
437+
438+ # Build the appropriate Gaussian mixture for a given model
439+ if ct == 'gauss' :
440+ # Uses 'simple' pars scheme
441+ gm_pars = [0.0 , 0.0 , g1 , g2 , T , flux ]
442+ gm = ngmix .gmix .GMixModel (gm_pars , 'gaussian' )
443+
444+ elif ct == 'cm' :
445+ fracdev = self .catalog [cp + '_fracdev' ][index ]
446+ TdByTe = self .catalog [cp + '_TdByTe' ][index ]
447+ # Uses 'simple' pars scheme
448+ gm_pars = [0.0 , 0.0 , g1 , g2 , T , flux ]
449+ gm = ngmix .gmix .GMixCM (fracdev , TdByTe , gm_pars )
450+
451+ elif ct == 'bdf' :
452+ fracdev = self .catalog [cp + '_fracdev' ][index ]
453+ TdByTe = self ._TdByTe
454+ # Uses different 'bdf' pars scheme
455+ gm_pars = [0.0 , 0.0 , g1 , g2 , T , fracdev , flux ]
456+ gm = ngmix .gmix .GMixBDF (pars = gm_pars , TdByTe = TdByTe )
421457
422458 # The majority of the conversion will be handled by `ngmix.gmix.py`
423459 gs_gal = gm .make_galsim_object (gsparams = gsp )
424460
425- # NOTE: Can add any model-specific requirements in future if needed
426- # if ct == 'mof':
427- # gal = ...
428-
429461 return gs_gal
430462
431463 #----------------------------------------------------------------------------------------------
@@ -486,6 +518,11 @@ def selectRandomIndex(self, n_random=1, rng=None, _n_rng_calls=False):
486518
487519 #----------------------------------------------------------------------------------------------
488520
521+ # The catalog type is referred to as a 'subtype' w.r.t BalInput types
522+ # (i.e. the BalInput type is 'ngmix_catalog' with subtype self.catalog_type)
523+ def getSubtype (self ):
524+ return self .cat_type
525+
489526 def getNObjects (self ):
490527 # Used by input/logger methods
491528 return self .nobjects
0 commit comments