Coverage for python/lsst/pipe/tasks/functors.py : 35%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of pipe_tasks.
2#
3# LSST Data Management System
4# This product includes software developed by the
5# LSST Project (http://www.lsst.org/).
6# See COPYRIGHT file at the top of the source tree.
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <https://www.lsstcorp.org/LegalNotices/>.
21#
22import yaml
23import re
24from itertools import product
25import os.path
27import pandas as pd
28import numpy as np
29import astropy.units as u
31from lsst.daf.persistence import doImport
32from lsst.daf.butler import DeferredDatasetHandle
33import lsst.geom as geom
34import lsst.sphgeom as sphgeom
36from .parquetTable import ParquetTable, MultilevelParquetTable
39def init_fromDict(initDict, basePath='lsst.pipe.tasks.functors',
40 typeKey='functor', name=None):
41 """Initialize an object defined in a dictionary
43 The object needs to be importable as
44 f'{basePath}.{initDict[typeKey]}'
45 The positional and keyword arguments (if any) are contained in
46 "args" and "kwargs" entries in the dictionary, respectively.
47 This is used in `functors.CompositeFunctor.from_yaml` to initialize
48 a composite functor from a specification in a YAML file.
50 Parameters
51 ----------
52 initDict : dictionary
53 Dictionary describing object's initialization. Must contain
54 an entry keyed by ``typeKey`` that is the name of the object,
55 relative to ``basePath``.
56 basePath : str
57 Path relative to module in which ``initDict[typeKey]`` is defined.
58 typeKey : str
59 Key of ``initDict`` that is the name of the object
60 (relative to `basePath`).
61 """
62 initDict = initDict.copy()
63 # TO DO: DM-21956 We should be able to define functors outside this module
64 pythonType = doImport(f'{basePath}.{initDict.pop(typeKey)}')
65 args = []
66 if 'args' in initDict:
67 args = initDict.pop('args')
68 if isinstance(args, str):
69 args = [args]
70 try:
71 element = pythonType(*args, **initDict)
72 except Exception as e:
73 message = f'Error in constructing functor "{name}" of type {pythonType.__name__} with args: {args}'
74 raise type(e)(message, e.args)
75 return element
78class Functor(object):
79 """Define and execute a calculation on a ParquetTable
81 The `__call__` method accepts either a `ParquetTable` object or a
82 `DeferredDatasetHandle`, and returns the
83 result of the calculation as a single column. Each functor defines what
84 columns are needed for the calculation, and only these columns are read
85 from the `ParquetTable`.
87 The action of `__call__` consists of two steps: first, loading the
88 necessary columns from disk into memory as a `pandas.DataFrame` object;
89 and second, performing the computation on this dataframe and returning the
90 result.
93 To define a new `Functor`, a subclass must define a `_func` method,
94 that takes a `pandas.DataFrame` and returns result in a `pandas.Series`.
95 In addition, it must define the following attributes
97 * `_columns`: The columns necessary to perform the calculation
98 * `name`: A name appropriate for a figure axis label
99 * `shortname`: A name appropriate for use as a dictionary key
101 On initialization, a `Functor` should declare what band (`filt` kwarg)
102 and dataset (e.g. `'ref'`, `'meas'`, `'forced_src'`) it is intended to be
103 applied to. This enables the `_get_data` method to extract the proper
104 columns from the parquet file. If not specified, the dataset will fall back
105 on the `_defaultDataset`attribute. If band is not specified and `dataset`
106 is anything other than `'ref'`, then an error will be raised when trying to
107 perform the calculation.
109 Originally, `Functor` was set up to expect
110 datasets formatted like the `deepCoadd_obj` dataset; that is, a
111 dataframe with a multi-level column index, with the levels of the
112 column index being `band`, `dataset`, and `column`.
113 It has since been generalized to apply to dataframes without mutli-level
114 indices and multi-level indices with just `dataset` and `column` levels.
115 In addition, the `_get_data` method that reads
116 the dataframe from the `ParquetTable` will return a dataframe with column
117 index levels defined by the `_dfLevels` attribute; by default, this is
118 `column`.
120 The `_dfLevels` attributes should generally not need to
121 be changed, unless `_func` needs columns from multiple filters or datasets
122 to do the calculation.
123 An example of this is the `lsst.pipe.tasks.functors.Color` functor, for
124 which `_dfLevels = ('band', 'column')`, and `_func` expects the dataframe
125 it gets to have those levels in the column index.
127 Parameters
128 ----------
129 filt : str
130 Filter upon which to do the calculation
132 dataset : str
133 Dataset upon which to do the calculation
134 (e.g., 'ref', 'meas', 'forced_src').
136 """
138 _defaultDataset = 'ref'
139 _dfLevels = ('column',)
140 _defaultNoDup = False
142 def __init__(self, filt=None, dataset=None, noDup=None):
143 self.filt = filt
144 self.dataset = dataset if dataset is not None else self._defaultDataset
145 self._noDup = noDup
147 @property
148 def noDup(self):
149 if self._noDup is not None:
150 return self._noDup
151 else:
152 return self._defaultNoDup
154 @property
155 def columns(self):
156 """Columns required to perform calculation
157 """
158 if not hasattr(self, '_columns'):
159 raise NotImplementedError('Must define columns property or _columns attribute')
160 return self._columns
162 def _get_data_columnLevels(self, data, columnIndex=None):
163 """Gets the names of the column index levels
165 This should only be called in the context of a multilevel table.
166 The logic here is to enable this to work both with the gen2 `MultilevelParquetTable`
167 and with the gen3 `DeferredDatasetHandle`.
169 Parameters
170 ----------
171 data : `MultilevelParquetTable` or `DeferredDatasetHandle`
173 columnnIndex (optional): pandas `Index` object
174 if not passed, then it is read from the `DeferredDatasetHandle`
175 """
176 if isinstance(data, DeferredDatasetHandle):
177 if columnIndex is None:
178 columnIndex = data.get(component="columns")
179 if columnIndex is not None:
180 return columnIndex.names
181 if isinstance(data, MultilevelParquetTable):
182 return data.columnLevels
183 else:
184 raise TypeError(f"Unknown type for data: {type(data)}!")
186 def _get_data_columnLevelNames(self, data, columnIndex=None):
187 """Gets the content of each of the column levels for a multilevel table
189 Similar to `_get_data_columnLevels`, this enables backward compatibility with gen2.
191 Mirrors original gen2 implementation within `pipe.tasks.parquetTable.MultilevelParquetTable`
192 """
193 if isinstance(data, DeferredDatasetHandle):
194 if columnIndex is None:
195 columnIndex = data.get(component="columns")
196 if columnIndex is not None:
197 columnLevels = columnIndex.names
198 columnLevelNames = {
199 level: list(np.unique(np.array([c for c in columnIndex])[:, i]))
200 for i, level in enumerate(columnLevels)
201 }
202 return columnLevelNames
203 if isinstance(data, MultilevelParquetTable):
204 return data.columnLevelNames
205 else:
206 raise TypeError(f"Unknown type for data: {type(data)}!")
208 def _colsFromDict(self, colDict, columnIndex=None):
209 """Converts dictionary column specficiation to a list of columns
211 This mirrors the original gen2 implementation within `pipe.tasks.parquetTable.MultilevelParquetTable`
212 """
213 new_colDict = {}
214 columnLevels = self._get_data_columnLevels(None, columnIndex=columnIndex)
216 for i, lev in enumerate(columnLevels):
217 if lev in colDict:
218 if isinstance(colDict[lev], str):
219 new_colDict[lev] = [colDict[lev]]
220 else:
221 new_colDict[lev] = colDict[lev]
222 else:
223 new_colDict[lev] = columnIndex.levels[i]
225 levelCols = [new_colDict[lev] for lev in columnLevels]
226 cols = product(*levelCols)
227 return list(cols)
229 def multilevelColumns(self, data, columnIndex=None, returnTuple=False):
230 """Returns columns needed by functor from multilevel dataset
232 To access tables with multilevel column structure, the `MultilevelParquetTable`
233 or `DeferredDatasetHandle` need to be passed either a list of tuples or a
234 dictionary.
236 Parameters
237 ----------
238 data : `MultilevelParquetTable` or `DeferredDatasetHandle`
240 columnIndex (optional): pandas `Index` object
241 either passed or read in from `DeferredDatasetHandle`.
243 `returnTuple` : bool
244 If true, then return a list of tuples rather than the column dictionary
245 specification. This is set to `True` by `CompositeFunctor` in order to be able to
246 combine columns from the various component functors.
248 """
249 if isinstance(data, DeferredDatasetHandle) and columnIndex is None:
250 columnIndex = data.get(component="columns")
252 # Confirm that the dataset has the column levels the functor is expecting it to have.
253 columnLevels = self._get_data_columnLevels(data, columnIndex)
255 columnDict = {'column': self.columns,
256 'dataset': self.dataset}
257 if self.filt is None:
258 columnLevelNames = self._get_data_columnLevelNames(data, columnIndex)
259 if "band" in columnLevels:
260 if self.dataset == "ref":
261 columnDict["band"] = columnLevelNames["band"][0]
262 else:
263 raise ValueError(f"'filt' not set for functor {self.name}"
264 f"(dataset {self.dataset}) "
265 "and ParquetTable "
266 "contains multiple filters in column index. "
267 "Set 'filt' or set 'dataset' to 'ref'.")
268 else:
269 columnDict['band'] = self.filt
271 if isinstance(data, MultilevelParquetTable):
272 return data._colsFromDict(columnDict)
273 elif isinstance(data, DeferredDatasetHandle):
274 if returnTuple:
275 return self._colsFromDict(columnDict, columnIndex=columnIndex)
276 else:
277 return columnDict
279 def _func(self, df, dropna=True):
280 raise NotImplementedError('Must define calculation on dataframe')
282 def _get_columnIndex(self, data):
283 """Return columnIndex
284 """
286 if isinstance(data, DeferredDatasetHandle):
287 return data.get(component="columns")
288 else:
289 return None
291 def _get_data(self, data):
292 """Retrieve dataframe necessary for calculation.
294 The data argument can be a DataFrame, a ParquetTable instance, or a gen3 DeferredDatasetHandle
296 Returns dataframe upon which `self._func` can act.
298 N.B. while passing a raw pandas `DataFrame` *should* work here, it has not been tested.
299 """
300 if isinstance(data, pd.DataFrame):
301 return data
303 # First thing to do: check to see if the data source has a multilevel column index or not.
304 columnIndex = self._get_columnIndex(data)
305 is_multiLevel = isinstance(data, MultilevelParquetTable) or isinstance(columnIndex, pd.MultiIndex)
307 # Simple single-level parquet table, gen2
308 if isinstance(data, ParquetTable) and not is_multiLevel:
309 columns = self.columns
310 df = data.toDataFrame(columns=columns)
311 return df
313 # Get proper columns specification for this functor
314 if is_multiLevel:
315 columns = self.multilevelColumns(data, columnIndex=columnIndex)
316 else:
317 columns = self.columns
319 if isinstance(data, MultilevelParquetTable):
320 # Load in-memory dataframe with appropriate columns the gen2 way
321 df = data.toDataFrame(columns=columns, droplevels=False)
322 elif isinstance(data, DeferredDatasetHandle):
323 # Load in-memory dataframe with appropriate columns the gen3 way
324 df = data.get(parameters={"columns": columns})
326 # Drop unnecessary column levels
327 if is_multiLevel:
328 df = self._setLevels(df)
330 return df
332 def _setLevels(self, df):
333 levelsToDrop = [n for n in df.columns.names if n not in self._dfLevels]
334 df.columns = df.columns.droplevel(levelsToDrop)
335 return df
337 def _dropna(self, vals):
338 return vals.dropna()
340 def __call__(self, data, dropna=False):
341 try:
342 df = self._get_data(data)
343 vals = self._func(df)
344 except Exception:
345 vals = self.fail(df)
346 if dropna:
347 vals = self._dropna(vals)
349 return vals
351 def difference(self, data1, data2, **kwargs):
352 """Computes difference between functor called on two different ParquetTable objects
353 """
354 return self(data1, **kwargs) - self(data2, **kwargs)
356 def fail(self, df):
357 return pd.Series(np.full(len(df), np.nan), index=df.index)
359 @property
360 def name(self):
361 """Full name of functor (suitable for figure labels)
362 """
363 return NotImplementedError
365 @property
366 def shortname(self):
367 """Short name of functor (suitable for column name/dict key)
368 """
369 return self.name
372class CompositeFunctor(Functor):
373 """Perform multiple calculations at once on a catalog
375 The role of a `CompositeFunctor` is to group together computations from
376 multiple functors. Instead of returning `pandas.Series` a
377 `CompositeFunctor` returns a `pandas.Dataframe`, with the column names
378 being the keys of `funcDict`.
380 The `columns` attribute of a `CompositeFunctor` is the union of all columns
381 in all the component functors.
383 A `CompositeFunctor` does not use a `_func` method itself; rather,
384 when a `CompositeFunctor` is called, all its columns are loaded
385 at once, and the resulting dataframe is passed to the `_func` method of each component
386 functor. This has the advantage of only doing I/O (reading from parquet file) once,
387 and works because each individual `_func` method of each component functor does not
388 care if there are *extra* columns in the dataframe being passed; only that it must contain
389 *at least* the `columns` it expects.
391 An important and useful class method is `from_yaml`, which takes as argument the path to a YAML
392 file specifying a collection of functors.
394 Parameters
395 ----------
396 funcs : `dict` or `list`
397 Dictionary or list of functors. If a list, then it will be converted
398 into a dictonary according to the `.shortname` attribute of each functor.
400 """
401 dataset = None
403 def __init__(self, funcs, **kwargs):
405 if type(funcs) == dict:
406 self.funcDict = funcs
407 else:
408 self.funcDict = {f.shortname: f for f in funcs}
410 self._filt = None
412 super().__init__(**kwargs)
414 @property
415 def filt(self):
416 return self._filt
418 @filt.setter
419 def filt(self, filt):
420 if filt is not None:
421 for _, f in self.funcDict.items():
422 f.filt = filt
423 self._filt = filt
425 def update(self, new):
426 if isinstance(new, dict):
427 self.funcDict.update(new)
428 elif isinstance(new, CompositeFunctor):
429 self.funcDict.update(new.funcDict)
430 else:
431 raise TypeError('Can only update with dictionary or CompositeFunctor.')
433 # Make sure new functors have the same 'filt' set
434 if self.filt is not None:
435 self.filt = self.filt
437 @property
438 def columns(self):
439 return list(set([x for y in [f.columns for f in self.funcDict.values()] for x in y]))
441 def multilevelColumns(self, data, **kwargs):
442 # Get the union of columns for all component functors. Note the need to have `returnTuple=True` here.
443 return list(
444 set(
445 [
446 x
447 for y in [
448 f.multilevelColumns(data, returnTuple=True, **kwargs) for f in self.funcDict.values()
449 ]
450 for x in y
451 ]
452 )
453 )
455 def __call__(self, data, **kwargs):
456 """Apply the functor to the data table
458 Parameters
459 ----------
460 data : `lsst.daf.butler.DeferredDatasetHandle`,
461 `lsst.pipe.tasks.parquetTable.MultilevelParquetTable`,
462 `lsst.pipe.tasks.parquetTable.ParquetTable`,
463 or `pandas.DataFrame`.
464 The table or a pointer to a table on disk from which columns can
465 be accessed
466 """
467 columnIndex = self._get_columnIndex(data)
469 # First, determine whether data has a multilevel index (either gen2 or gen3)
470 is_multiLevel = isinstance(data, MultilevelParquetTable) or isinstance(columnIndex, pd.MultiIndex)
472 # Multilevel index, gen2 or gen3
473 if is_multiLevel:
474 columns = self.multilevelColumns(data, columnIndex=columnIndex)
476 if isinstance(data, MultilevelParquetTable):
477 # Read data into memory the gen2 way
478 df = data.toDataFrame(columns=columns, droplevels=False)
479 elif isinstance(data, DeferredDatasetHandle):
480 # Read data into memory the gen3 way
481 df = data.get(parameters={"columns": columns})
483 valDict = {}
484 for k, f in self.funcDict.items():
485 try:
486 subdf = f._setLevels(
487 df[f.multilevelColumns(data, returnTuple=True, columnIndex=columnIndex)]
488 )
489 valDict[k] = f._func(subdf)
490 except Exception:
491 valDict[k] = f.fail(subdf)
493 else:
494 if isinstance(data, DeferredDatasetHandle):
495 # input if Gen3 deferLoad=True
496 df = data.get(parameters={"columns": self.columns})
497 elif isinstance(data, pd.DataFrame):
498 # input if Gen3 deferLoad=False
499 df = data
500 else:
501 # Original Gen2 input is type ParquetTable and the fallback
502 df = data.toDataFrame(columns=self.columns)
504 valDict = {k: f._func(df) for k, f in self.funcDict.items()}
506 try:
507 valDf = pd.concat(valDict, axis=1)
508 except TypeError:
509 print([(k, type(v)) for k, v in valDict.items()])
510 raise
512 if kwargs.get('dropna', False):
513 valDf = valDf.dropna(how='any')
515 return valDf
517 @classmethod
518 def renameCol(cls, col, renameRules):
519 if renameRules is None:
520 return col
521 for old, new in renameRules:
522 if col.startswith(old):
523 col = col.replace(old, new)
524 return col
526 @classmethod
527 def from_file(cls, filename, **kwargs):
528 # Allow environment variables in the filename.
529 filename = os.path.expandvars(filename)
530 with open(filename) as f:
531 translationDefinition = yaml.safe_load(f)
533 return cls.from_yaml(translationDefinition, **kwargs)
535 @classmethod
536 def from_yaml(cls, translationDefinition, **kwargs):
537 funcs = {}
538 for func, val in translationDefinition['funcs'].items():
539 funcs[func] = init_fromDict(val, name=func)
541 if 'flag_rename_rules' in translationDefinition:
542 renameRules = translationDefinition['flag_rename_rules']
543 else:
544 renameRules = None
546 if 'calexpFlags' in translationDefinition:
547 for flag in translationDefinition['calexpFlags']:
548 funcs[cls.renameCol(flag, renameRules)] = Column(flag, dataset='calexp')
550 if 'refFlags' in translationDefinition:
551 for flag in translationDefinition['refFlags']:
552 funcs[cls.renameCol(flag, renameRules)] = Column(flag, dataset='ref')
554 if 'forcedFlags' in translationDefinition:
555 for flag in translationDefinition['forcedFlags']:
556 funcs[cls.renameCol(flag, renameRules)] = Column(flag, dataset='forced_src')
558 if 'flags' in translationDefinition:
559 for flag in translationDefinition['flags']:
560 funcs[cls.renameCol(flag, renameRules)] = Column(flag, dataset='meas')
562 return cls(funcs, **kwargs)
565def mag_aware_eval(df, expr):
566 """Evaluate an expression on a DataFrame, knowing what the 'mag' function means
568 Builds on `pandas.DataFrame.eval`, which parses and executes math on dataframes.
570 Parameters
571 ----------
572 df : pandas.DataFrame
573 Dataframe on which to evaluate expression.
575 expr : str
576 Expression.
577 """
578 try:
579 expr_new = re.sub(r'mag\((\w+)\)', r'-2.5*log(\g<1>)/log(10)', expr)
580 val = df.eval(expr_new, truediv=True)
581 except Exception: # Should check what actually gets raised
582 expr_new = re.sub(r'mag\((\w+)\)', r'-2.5*log(\g<1>_instFlux)/log(10)', expr)
583 val = df.eval(expr_new, truediv=True)
584 return val
587class CustomFunctor(Functor):
588 """Arbitrary computation on a catalog
590 Column names (and thus the columns to be loaded from catalog) are found
591 by finding all words and trying to ignore all "math-y" words.
593 Parameters
594 ----------
595 expr : str
596 Expression to evaluate, to be parsed and executed by `mag_aware_eval`.
597 """
598 _ignore_words = ('mag', 'sin', 'cos', 'exp', 'log', 'sqrt')
600 def __init__(self, expr, **kwargs):
601 self.expr = expr
602 super().__init__(**kwargs)
604 @property
605 def name(self):
606 return self.expr
608 @property
609 def columns(self):
610 flux_cols = re.findall(r'mag\(\s*(\w+)\s*\)', self.expr)
612 cols = [c for c in re.findall(r'[a-zA-Z_]+', self.expr) if c not in self._ignore_words]
613 not_a_col = []
614 for c in flux_cols:
615 if not re.search('_instFlux$', c):
616 cols.append(f'{c}_instFlux')
617 not_a_col.append(c)
618 else:
619 cols.append(c)
621 return list(set([c for c in cols if c not in not_a_col]))
623 def _func(self, df):
624 return mag_aware_eval(df, self.expr)
627class Column(Functor):
628 """Get column with specified name
629 """
631 def __init__(self, col, **kwargs):
632 self.col = col
633 super().__init__(**kwargs)
635 @property
636 def name(self):
637 return self.col
639 @property
640 def columns(self):
641 return [self.col]
643 def _func(self, df):
644 return df[self.col]
647class Index(Functor):
648 """Return the value of the index for each object
649 """
651 columns = ['coord_ra'] # just a dummy; something has to be here
652 _defaultDataset = 'ref'
653 _defaultNoDup = True
655 def _func(self, df):
656 return pd.Series(df.index, index=df.index)
659class IDColumn(Column):
660 col = 'id'
661 _allow_difference = False
662 _defaultNoDup = True
664 def _func(self, df):
665 return pd.Series(df.index, index=df.index)
668class FootprintNPix(Column):
669 col = 'base_Footprint_nPix'
672class CoordColumn(Column):
673 """Base class for coordinate column, in degrees
674 """
675 _radians = True
677 def __init__(self, col, **kwargs):
678 super().__init__(col, **kwargs)
680 def _func(self, df):
681 # Must not modify original column in case that column is used by another functor
682 output = df[self.col] * 180 / np.pi if self._radians else df[self.col]
683 return output
686class RAColumn(CoordColumn):
687 """Right Ascension, in degrees
688 """
689 name = 'RA'
690 _defaultNoDup = True
692 def __init__(self, **kwargs):
693 super().__init__('coord_ra', **kwargs)
695 def __call__(self, catalog, **kwargs):
696 return super().__call__(catalog, **kwargs)
699class DecColumn(CoordColumn):
700 """Declination, in degrees
701 """
702 name = 'Dec'
703 _defaultNoDup = True
705 def __init__(self, **kwargs):
706 super().__init__('coord_dec', **kwargs)
708 def __call__(self, catalog, **kwargs):
709 return super().__call__(catalog, **kwargs)
712class HtmIndex20(Functor):
713 """Compute the level 20 HtmIndex for the catalog.
714 """
715 name = "Htm20"
716 htmLevel = 20
717 _radians = True
719 def __init__(self, ra, decl, **kwargs):
720 self.pixelator = sphgeom.HtmPixelization(self.htmLevel)
721 self.ra = ra
722 self.decl = decl
723 self._columns = [self.ra, self.decl]
724 super().__init__(**kwargs)
726 def _func(self, df):
728 def computePixel(row):
729 if self._radians:
730 sphPoint = geom.SpherePoint(row[self.ra],
731 row[self.decl],
732 geom.radians)
733 else:
734 sphPoint = geom.SpherePoint(row[self.ra],
735 row[self.decl],
736 geom.degrees)
737 return self.pixelator.index(sphPoint.getVector())
739 return df.apply(computePixel, axis=1)
742def fluxName(col):
743 if not col.endswith('_instFlux'):
744 col += '_instFlux'
745 return col
748def fluxErrName(col):
749 if not col.endswith('_instFluxErr'):
750 col += '_instFluxErr'
751 return col
754class Mag(Functor):
755 """Compute calibrated magnitude
757 Takes a `calib` argument, which returns the flux at mag=0
758 as `calib.getFluxMag0()`. If not provided, then the default
759 `fluxMag0` is 63095734448.0194, which is default for HSC.
760 This default should be removed in DM-21955
762 This calculation hides warnings about invalid values and dividing by zero.
764 As for all functors, a `dataset` and `filt` kwarg should be provided upon
765 initialization. Unlike the default `Functor`, however, the default dataset
766 for a `Mag` is `'meas'`, rather than `'ref'`.
768 Parameters
769 ----------
770 col : `str`
771 Name of flux column from which to compute magnitude. Can be parseable
772 by `lsst.pipe.tasks.functors.fluxName` function---that is, you can pass
773 `'modelfit_CModel'` instead of `'modelfit_CModel_instFlux'`) and it will
774 understand.
775 calib : `lsst.afw.image.calib.Calib` (optional)
776 Object that knows zero point.
777 """
778 _defaultDataset = 'meas'
780 def __init__(self, col, calib=None, **kwargs):
781 self.col = fluxName(col)
782 self.calib = calib
783 if calib is not None:
784 self.fluxMag0 = calib.getFluxMag0()[0]
785 else:
786 # TO DO: DM-21955 Replace hard coded photometic calibration values
787 self.fluxMag0 = 63095734448.0194
789 super().__init__(**kwargs)
791 @property
792 def columns(self):
793 return [self.col]
795 def _func(self, df):
796 with np.warnings.catch_warnings():
797 np.warnings.filterwarnings('ignore', r'invalid value encountered')
798 np.warnings.filterwarnings('ignore', r'divide by zero')
799 return -2.5*np.log10(df[self.col] / self.fluxMag0)
801 @property
802 def name(self):
803 return f'mag_{self.col}'
806class MagErr(Mag):
807 """Compute calibrated magnitude uncertainty
809 Takes the same `calib` object as `lsst.pipe.tasks.functors.Mag`.
811 Parameters
812 col : `str`
813 Name of flux column
814 calib : `lsst.afw.image.calib.Calib` (optional)
815 Object that knows zero point.
816 """
818 def __init__(self, *args, **kwargs):
819 super().__init__(*args, **kwargs)
820 if self.calib is not None:
821 self.fluxMag0Err = self.calib.getFluxMag0()[1]
822 else:
823 self.fluxMag0Err = 0.
825 @property
826 def columns(self):
827 return [self.col, self.col + 'Err']
829 def _func(self, df):
830 with np.warnings.catch_warnings():
831 np.warnings.filterwarnings('ignore', r'invalid value encountered')
832 np.warnings.filterwarnings('ignore', r'divide by zero')
833 fluxCol, fluxErrCol = self.columns
834 x = df[fluxErrCol] / df[fluxCol]
835 y = self.fluxMag0Err / self.fluxMag0
836 magErr = (2.5 / np.log(10.)) * np.sqrt(x*x + y*y)
837 return magErr
839 @property
840 def name(self):
841 return super().name + '_err'
844class NanoMaggie(Mag):
845 """
846 """
848 def _func(self, df):
849 return (df[self.col] / self.fluxMag0) * 1e9
852class MagDiff(Functor):
853 _defaultDataset = 'meas'
855 """Functor to calculate magnitude difference"""
857 def __init__(self, col1, col2, **kwargs):
858 self.col1 = fluxName(col1)
859 self.col2 = fluxName(col2)
860 super().__init__(**kwargs)
862 @property
863 def columns(self):
864 return [self.col1, self.col2]
866 def _func(self, df):
867 with np.warnings.catch_warnings():
868 np.warnings.filterwarnings('ignore', r'invalid value encountered')
869 np.warnings.filterwarnings('ignore', r'divide by zero')
870 return -2.5*np.log10(df[self.col1]/df[self.col2])
872 @property
873 def name(self):
874 return f'(mag_{self.col1} - mag_{self.col2})'
876 @property
877 def shortname(self):
878 return f'magDiff_{self.col1}_{self.col2}'
881class Color(Functor):
882 """Compute the color between two filters
884 Computes color by initializing two different `Mag`
885 functors based on the `col` and filters provided, and
886 then returning the difference.
888 This is enabled by the `_func` expecting a dataframe with a
889 multilevel column index, with both `'band'` and `'column'`,
890 instead of just `'column'`, which is the `Functor` default.
891 This is controlled by the `_dfLevels` attribute.
893 Also of note, the default dataset for `Color` is `forced_src'`,
894 whereas for `Mag` it is `'meas'`.
896 Parameters
897 ----------
898 col : str
899 Name of flux column from which to compute; same as would be passed to
900 `lsst.pipe.tasks.functors.Mag`.
902 filt2, filt1 : str
903 Filters from which to compute magnitude difference.
904 Color computed is `Mag(filt2) - Mag(filt1)`.
905 """
906 _defaultDataset = 'forced_src'
907 _dfLevels = ('band', 'column')
908 _defaultNoDup = True
910 def __init__(self, col, filt2, filt1, **kwargs):
911 self.col = fluxName(col)
912 if filt2 == filt1:
913 raise RuntimeError("Cannot compute Color for %s: %s - %s " % (col, filt2, filt1))
914 self.filt2 = filt2
915 self.filt1 = filt1
917 self.mag2 = Mag(col, filt=filt2, **kwargs)
918 self.mag1 = Mag(col, filt=filt1, **kwargs)
920 super().__init__(**kwargs)
922 @property
923 def filt(self):
924 return None
926 @filt.setter
927 def filt(self, filt):
928 pass
930 def _func(self, df):
931 mag2 = self.mag2._func(df[self.filt2])
932 mag1 = self.mag1._func(df[self.filt1])
933 return mag2 - mag1
935 @property
936 def columns(self):
937 return [self.mag1.col, self.mag2.col]
939 def multilevelColumns(self, parq, **kwargs):
940 return [(self.dataset, self.filt1, self.col), (self.dataset, self.filt2, self.col)]
942 @property
943 def name(self):
944 return f'{self.filt2} - {self.filt1} ({self.col})'
946 @property
947 def shortname(self):
948 return f"{self.col}_{self.filt2.replace('-', '')}m{self.filt1.replace('-', '')}"
951class Labeller(Functor):
952 """Main function of this subclass is to override the dropna=True
953 """
954 _null_label = 'null'
955 _allow_difference = False
956 name = 'label'
957 _force_str = False
959 def __call__(self, parq, dropna=False, **kwargs):
960 return super().__call__(parq, dropna=False, **kwargs)
963class StarGalaxyLabeller(Labeller):
964 _columns = ["base_ClassificationExtendedness_value"]
965 _column = "base_ClassificationExtendedness_value"
967 def _func(self, df):
968 x = df[self._columns][self._column]
969 mask = x.isnull()
970 test = (x < 0.5).astype(int)
971 test = test.mask(mask, 2)
973 # TODO: DM-21954 Look into veracity of inline comment below
974 # are these backwards?
975 categories = ['galaxy', 'star', self._null_label]
976 label = pd.Series(pd.Categorical.from_codes(test, categories=categories),
977 index=x.index, name='label')
978 if self._force_str:
979 label = label.astype(str)
980 return label
983class NumStarLabeller(Labeller):
984 _columns = ['numStarFlags']
985 labels = {"star": 0, "maybe": 1, "notStar": 2}
987 def _func(self, df):
988 x = df[self._columns][self._columns[0]]
990 # Number of filters
991 n = len(x.unique()) - 1
993 labels = ['noStar', 'maybe', 'star']
994 label = pd.Series(pd.cut(x, [-1, 0, n-1, n], labels=labels),
995 index=x.index, name='label')
997 if self._force_str:
998 label = label.astype(str)
1000 return label
1003class DeconvolvedMoments(Functor):
1004 name = 'Deconvolved Moments'
1005 shortname = 'deconvolvedMoments'
1006 _columns = ("ext_shapeHSM_HsmSourceMoments_xx",
1007 "ext_shapeHSM_HsmSourceMoments_yy",
1008 "base_SdssShape_xx", "base_SdssShape_yy",
1009 "ext_shapeHSM_HsmPsfMoments_xx",
1010 "ext_shapeHSM_HsmPsfMoments_yy")
1012 def _func(self, df):
1013 """Calculate deconvolved moments"""
1014 if "ext_shapeHSM_HsmSourceMoments_xx" in df.columns: # _xx added by tdm
1015 hsm = df["ext_shapeHSM_HsmSourceMoments_xx"] + df["ext_shapeHSM_HsmSourceMoments_yy"]
1016 else:
1017 hsm = np.ones(len(df))*np.nan
1018 sdss = df["base_SdssShape_xx"] + df["base_SdssShape_yy"]
1019 if "ext_shapeHSM_HsmPsfMoments_xx" in df.columns:
1020 psf = df["ext_shapeHSM_HsmPsfMoments_xx"] + df["ext_shapeHSM_HsmPsfMoments_yy"]
1021 else:
1022 # LSST does not have shape.sdss.psf. Could instead add base_PsfShape to catalog using
1023 # exposure.getPsf().computeShape(s.getCentroid()).getIxx()
1024 # raise TaskError("No psf shape parameter found in catalog")
1025 raise RuntimeError('No psf shape parameter found in catalog')
1027 return hsm.where(np.isfinite(hsm), sdss) - psf
1030class SdssTraceSize(Functor):
1031 """Functor to calculate SDSS trace radius size for sources"""
1032 name = "SDSS Trace Size"
1033 shortname = 'sdssTrace'
1034 _columns = ("base_SdssShape_xx", "base_SdssShape_yy")
1036 def _func(self, df):
1037 srcSize = np.sqrt(0.5*(df["base_SdssShape_xx"] + df["base_SdssShape_yy"]))
1038 return srcSize
1041class PsfSdssTraceSizeDiff(Functor):
1042 """Functor to calculate SDSS trace radius size difference (%) between object and psf model"""
1043 name = "PSF - SDSS Trace Size"
1044 shortname = 'psf_sdssTrace'
1045 _columns = ("base_SdssShape_xx", "base_SdssShape_yy",
1046 "base_SdssShape_psf_xx", "base_SdssShape_psf_yy")
1048 def _func(self, df):
1049 srcSize = np.sqrt(0.5*(df["base_SdssShape_xx"] + df["base_SdssShape_yy"]))
1050 psfSize = np.sqrt(0.5*(df["base_SdssShape_psf_xx"] + df["base_SdssShape_psf_yy"]))
1051 sizeDiff = 100*(srcSize - psfSize)/(0.5*(srcSize + psfSize))
1052 return sizeDiff
1055class HsmTraceSize(Functor):
1056 """Functor to calculate HSM trace radius size for sources"""
1057 name = 'HSM Trace Size'
1058 shortname = 'hsmTrace'
1059 _columns = ("ext_shapeHSM_HsmSourceMoments_xx",
1060 "ext_shapeHSM_HsmSourceMoments_yy")
1062 def _func(self, df):
1063 srcSize = np.sqrt(0.5*(df["ext_shapeHSM_HsmSourceMoments_xx"]
1064 + df["ext_shapeHSM_HsmSourceMoments_yy"]))
1065 return srcSize
1068class PsfHsmTraceSizeDiff(Functor):
1069 """Functor to calculate HSM trace radius size difference (%) between object and psf model"""
1070 name = 'PSF - HSM Trace Size'
1071 shortname = 'psf_HsmTrace'
1072 _columns = ("ext_shapeHSM_HsmSourceMoments_xx",
1073 "ext_shapeHSM_HsmSourceMoments_yy",
1074 "ext_shapeHSM_HsmPsfMoments_xx",
1075 "ext_shapeHSM_HsmPsfMoments_yy")
1077 def _func(self, df):
1078 srcSize = np.sqrt(0.5*(df["ext_shapeHSM_HsmSourceMoments_xx"]
1079 + df["ext_shapeHSM_HsmSourceMoments_yy"]))
1080 psfSize = np.sqrt(0.5*(df["ext_shapeHSM_HsmPsfMoments_xx"]
1081 + df["ext_shapeHSM_HsmPsfMoments_yy"]))
1082 sizeDiff = 100*(srcSize - psfSize)/(0.5*(srcSize + psfSize))
1083 return sizeDiff
1086class HsmFwhm(Functor):
1087 name = 'HSM Psf FWHM'
1088 _columns = ('ext_shapeHSM_HsmPsfMoments_xx', 'ext_shapeHSM_HsmPsfMoments_yy')
1089 # TODO: DM-21403 pixel scale should be computed from the CD matrix or transform matrix
1090 pixelScale = 0.168
1091 SIGMA2FWHM = 2*np.sqrt(2*np.log(2))
1093 def _func(self, df):
1094 return self.pixelScale*self.SIGMA2FWHM*np.sqrt(
1095 0.5*(df['ext_shapeHSM_HsmPsfMoments_xx'] + df['ext_shapeHSM_HsmPsfMoments_yy']))
1098class E1(Functor):
1099 name = "Distortion Ellipticity (e1)"
1100 shortname = "Distortion"
1102 def __init__(self, colXX, colXY, colYY, **kwargs):
1103 self.colXX = colXX
1104 self.colXY = colXY
1105 self.colYY = colYY
1106 self._columns = [self.colXX, self.colXY, self.colYY]
1107 super().__init__(**kwargs)
1109 @property
1110 def columns(self):
1111 return [self.colXX, self.colXY, self.colYY]
1113 def _func(self, df):
1114 return df[self.colXX] - df[self.colYY] / (df[self.colXX] + df[self.colYY])
1117class E2(Functor):
1118 name = "Ellipticity e2"
1120 def __init__(self, colXX, colXY, colYY, **kwargs):
1121 self.colXX = colXX
1122 self.colXY = colXY
1123 self.colYY = colYY
1124 super().__init__(**kwargs)
1126 @property
1127 def columns(self):
1128 return [self.colXX, self.colXY, self.colYY]
1130 def _func(self, df):
1131 return 2*df[self.colXY] / (df[self.colXX] + df[self.colYY])
1134class RadiusFromQuadrupole(Functor):
1136 def __init__(self, colXX, colXY, colYY, **kwargs):
1137 self.colXX = colXX
1138 self.colXY = colXY
1139 self.colYY = colYY
1140 super().__init__(**kwargs)
1142 @property
1143 def columns(self):
1144 return [self.colXX, self.colXY, self.colYY]
1146 def _func(self, df):
1147 return (df[self.colXX]*df[self.colYY] - df[self.colXY]**2)**0.25
1150class LocalWcs(Functor):
1151 """Computations using the stored localWcs.
1152 """
1153 name = "LocalWcsOperations"
1155 def __init__(self,
1156 colCD_1_1,
1157 colCD_1_2,
1158 colCD_2_1,
1159 colCD_2_2,
1160 **kwargs):
1161 self.colCD_1_1 = colCD_1_1
1162 self.colCD_1_2 = colCD_1_2
1163 self.colCD_2_1 = colCD_2_1
1164 self.colCD_2_2 = colCD_2_2
1165 super().__init__(**kwargs)
1167 def computeDeltaRaDec(self, x, y, cd11, cd12, cd21, cd22):
1168 """Compute the distance on the sphere from x2, y1 to x1, y1.
1170 Parameters
1171 ----------
1172 x : `pandas.Series`
1173 X pixel coordinate.
1174 y : `pandas.Series`
1175 Y pixel coordinate.
1176 cd11 : `pandas.Series`
1177 [1, 1] element of the local Wcs affine transform.
1178 cd11 : `pandas.Series`
1179 [1, 1] element of the local Wcs affine transform.
1180 cd12 : `pandas.Series`
1181 [1, 2] element of the local Wcs affine transform.
1182 cd21 : `pandas.Series`
1183 [2, 1] element of the local Wcs affine transform.
1184 cd22 : `pandas.Series`
1185 [2, 2] element of the local Wcs affine transform.
1187 Returns
1188 -------
1189 raDecTuple : tuple
1190 RA and dec conversion of x and y given the local Wcs. Returned
1191 units are in radians.
1193 """
1194 return (x * cd11 + y * cd12, x * cd21 + y * cd22)
1196 def computeSkySeperation(self, ra1, dec1, ra2, dec2):
1197 """Compute the local pixel scale conversion.
1199 Parameters
1200 ----------
1201 ra1 : `pandas.Series`
1202 Ra of the first coordinate in radians.
1203 dec1 : `pandas.Series`
1204 Dec of the first coordinate in radians.
1205 ra2 : `pandas.Series`
1206 Ra of the second coordinate in radians.
1207 dec2 : `pandas.Series`
1208 Dec of the second coordinate in radians.
1210 Returns
1211 -------
1212 dist : `pandas.Series`
1213 Distance on the sphere in radians.
1214 """
1215 deltaDec = dec2 - dec1
1216 deltaRa = ra2 - ra1
1217 return 2 * np.arcsin(
1218 np.sqrt(
1219 np.sin(deltaDec / 2) ** 2
1220 + np.cos(dec2) * np.cos(dec1) * np.sin(deltaRa / 2) ** 2))
1222 def getSkySeperationFromPixel(self, x1, y1, x2, y2, cd11, cd12, cd21, cd22):
1223 """Compute the distance on the sphere from x2, y1 to x1, y1.
1225 Parameters
1226 ----------
1227 x1 : `pandas.Series`
1228 X pixel coordinate.
1229 y1 : `pandas.Series`
1230 Y pixel coordinate.
1231 x2 : `pandas.Series`
1232 X pixel coordinate.
1233 y2 : `pandas.Series`
1234 Y pixel coordinate.
1235 cd11 : `pandas.Series`
1236 [1, 1] element of the local Wcs affine transform.
1237 cd11 : `pandas.Series`
1238 [1, 1] element of the local Wcs affine transform.
1239 cd12 : `pandas.Series`
1240 [1, 2] element of the local Wcs affine transform.
1241 cd21 : `pandas.Series`
1242 [2, 1] element of the local Wcs affine transform.
1243 cd22 : `pandas.Series`
1244 [2, 2] element of the local Wcs affine transform.
1246 Returns
1247 -------
1248 Distance : `pandas.Series`
1249 Arcseconds per pixel at the location of the local WC
1250 """
1251 ra1, dec1 = self.computeDeltaRaDec(x1, y1, cd11, cd12, cd21, cd22)
1252 ra2, dec2 = self.computeDeltaRaDec(x2, y2, cd11, cd12, cd21, cd22)
1253 # Great circle distance for small separations.
1254 return self.computeSkySeperation(ra1, dec1, ra2, dec2)
1257class ComputePixelScale(LocalWcs):
1258 """Compute the local pixel scale from the stored CDMatrix.
1259 """
1260 name = "PixelScale"
1262 @property
1263 def columns(self):
1264 return [self.colCD_1_1,
1265 self.colCD_1_2,
1266 self.colCD_2_1,
1267 self.colCD_2_2]
1269 def pixelScaleArcseconds(self, cd11, cd12, cd21, cd22):
1270 """Compute the local pixel to scale conversion in arcseconds.
1272 Parameters
1273 ----------
1274 cd11 : `pandas.Series`
1275 [1, 1] element of the local Wcs affine transform in radians.
1276 cd11 : `pandas.Series`
1277 [1, 1] element of the local Wcs affine transform in radians.
1278 cd12 : `pandas.Series`
1279 [1, 2] element of the local Wcs affine transform in radians.
1280 cd21 : `pandas.Series`
1281 [2, 1] element of the local Wcs affine transform in radians.
1282 cd22 : `pandas.Series`
1283 [2, 2] element of the local Wcs affine transform in radians.
1285 Returns
1286 -------
1287 pixScale : `pandas.Series`
1288 Arcseconds per pixel at the location of the local WC
1289 """
1290 return 3600 * np.degrees(np.sqrt(np.fabs(cd11 * cd22 - cd12 * cd21)))
1292 def _func(self, df):
1293 return self.pixelScaleArcseconds(df[self.colCD_1_1],
1294 df[self.colCD_1_2],
1295 df[self.colCD_2_1],
1296 df[self.colCD_2_2])
1299class ConvertPixelToArcseconds(ComputePixelScale):
1300 """Convert a value in units pixels squared to units arcseconds squared.
1301 """
1303 def __init__(self,
1304 col,
1305 colCD_1_1,
1306 colCD_1_2,
1307 colCD_2_1,
1308 colCD_2_2,
1309 **kwargs):
1310 self.col = col
1311 super().__init__(colCD_1_1,
1312 colCD_1_2,
1313 colCD_2_1,
1314 colCD_2_2,
1315 **kwargs)
1317 @property
1318 def name(self):
1319 return f"{self.col}_asArcseconds"
1321 @property
1322 def columns(self):
1323 return [self.col,
1324 self.colCD_1_1,
1325 self.colCD_1_2,
1326 self.colCD_2_1,
1327 self.colCD_2_2]
1329 def _func(self, df):
1330 return df[self.col] * self.pixelScaleArcseconds(df[self.colCD_1_1],
1331 df[self.colCD_1_2],
1332 df[self.colCD_2_1],
1333 df[self.colCD_2_2])
1336class ConvertPixelSqToArcsecondsSq(ComputePixelScale):
1337 """Convert a value in units pixels to units arcseconds.
1338 """
1340 def __init__(self,
1341 col,
1342 colCD_1_1,
1343 colCD_1_2,
1344 colCD_2_1,
1345 colCD_2_2,
1346 **kwargs):
1347 self.col = col
1348 super().__init__(colCD_1_1,
1349 colCD_1_2,
1350 colCD_2_1,
1351 colCD_2_2,
1352 **kwargs)
1354 @property
1355 def name(self):
1356 return f"{self.col}_asArcsecondsSq"
1358 @property
1359 def columns(self):
1360 return [self.col,
1361 self.colCD_1_1,
1362 self.colCD_1_2,
1363 self.colCD_2_1,
1364 self.colCD_2_2]
1366 def _func(self, df):
1367 pixScale = self.pixelScaleArcseconds(df[self.colCD_1_1],
1368 df[self.colCD_1_2],
1369 df[self.colCD_2_1],
1370 df[self.colCD_2_2])
1371 return df[self.col] * pixScale * pixScale
1374class ReferenceBand(Functor):
1375 name = 'Reference Band'
1376 shortname = 'refBand'
1378 @property
1379 def columns(self):
1380 return ["merge_measurement_i",
1381 "merge_measurement_r",
1382 "merge_measurement_z",
1383 "merge_measurement_y",
1384 "merge_measurement_g"]
1386 def _func(self, df):
1387 def getFilterAliasName(row):
1388 # get column name with the max value (True > False)
1389 colName = row.idxmax()
1390 return colName.replace('merge_measurement_', '')
1392 return df[self.columns].apply(getFilterAliasName, axis=1)
1395class Photometry(Functor):
1396 # AB to NanoJansky (3631 Jansky)
1397 AB_FLUX_SCALE = (0 * u.ABmag).to_value(u.nJy)
1398 LOG_AB_FLUX_SCALE = 12.56
1399 FIVE_OVER_2LOG10 = 1.085736204758129569
1400 # TO DO: DM-21955 Replace hard coded photometic calibration values
1401 COADD_ZP = 27
1403 def __init__(self, colFlux, colFluxErr=None, calib=None, **kwargs):
1404 self.vhypot = np.vectorize(self.hypot)
1405 self.col = colFlux
1406 self.colFluxErr = colFluxErr
1408 self.calib = calib
1409 if calib is not None:
1410 self.fluxMag0, self.fluxMag0Err = calib.getFluxMag0()
1411 else:
1412 self.fluxMag0 = 1./np.power(10, -0.4*self.COADD_ZP)
1413 self.fluxMag0Err = 0.
1415 super().__init__(**kwargs)
1417 @property
1418 def columns(self):
1419 return [self.col]
1421 @property
1422 def name(self):
1423 return f'mag_{self.col}'
1425 @classmethod
1426 def hypot(cls, a, b):
1427 if np.abs(a) < np.abs(b):
1428 a, b = b, a
1429 if a == 0.:
1430 return 0.
1431 q = b/a
1432 return np.abs(a) * np.sqrt(1. + q*q)
1434 def dn2flux(self, dn, fluxMag0):
1435 return self.AB_FLUX_SCALE * dn / fluxMag0
1437 def dn2mag(self, dn, fluxMag0):
1438 with np.warnings.catch_warnings():
1439 np.warnings.filterwarnings('ignore', r'invalid value encountered')
1440 np.warnings.filterwarnings('ignore', r'divide by zero')
1441 return -2.5 * np.log10(dn/fluxMag0)
1443 def dn2fluxErr(self, dn, dnErr, fluxMag0, fluxMag0Err):
1444 retVal = self.vhypot(dn * fluxMag0Err, dnErr * fluxMag0)
1445 retVal *= self.AB_FLUX_SCALE / fluxMag0 / fluxMag0
1446 return retVal
1448 def dn2MagErr(self, dn, dnErr, fluxMag0, fluxMag0Err):
1449 retVal = self.dn2fluxErr(dn, dnErr, fluxMag0, fluxMag0Err) / self.dn2flux(dn, fluxMag0)
1450 return self.FIVE_OVER_2LOG10 * retVal
1453class NanoJansky(Photometry):
1454 def _func(self, df):
1455 return self.dn2flux(df[self.col], self.fluxMag0)
1458class NanoJanskyErr(Photometry):
1459 @property
1460 def columns(self):
1461 return [self.col, self.colFluxErr]
1463 def _func(self, df):
1464 retArr = self.dn2fluxErr(df[self.col], df[self.colFluxErr], self.fluxMag0, self.fluxMag0Err)
1465 return pd.Series(retArr, index=df.index)
1468class Magnitude(Photometry):
1469 def _func(self, df):
1470 return self.dn2mag(df[self.col], self.fluxMag0)
1473class MagnitudeErr(Photometry):
1474 @property
1475 def columns(self):
1476 return [self.col, self.colFluxErr]
1478 def _func(self, df):
1479 retArr = self.dn2MagErr(df[self.col], df[self.colFluxErr], self.fluxMag0, self.fluxMag0Err)
1480 return pd.Series(retArr, index=df.index)
1483class LocalPhotometry(Functor):
1484 """Base class for calibrating the specified instrument flux column using
1485 the local photometric calibration.
1487 Parameters
1488 ----------
1489 instFluxCol : `str`
1490 Name of the instrument flux column.
1491 instFluxErrCol : `str`
1492 Name of the assocated error columns for ``instFluxCol``.
1493 photoCalibCol : `str`
1494 Name of local calibration column.
1495 photoCalibErrCol : `str`
1496 Error associated with ``photoCalibCol``
1498 See also
1499 --------
1500 LocalPhotometry
1501 LocalNanojansky
1502 LocalNanojanskyErr
1503 LocalMagnitude
1504 LocalMagnitudeErr
1505 """
1506 logNJanskyToAB = (1 * u.nJy).to_value(u.ABmag)
1508 def __init__(self,
1509 instFluxCol,
1510 instFluxErrCol,
1511 photoCalibCol,
1512 photoCalibErrCol,
1513 **kwargs):
1514 self.instFluxCol = instFluxCol
1515 self.instFluxErrCol = instFluxErrCol
1516 self.photoCalibCol = photoCalibCol
1517 self.photoCalibErrCol = photoCalibErrCol
1518 super().__init__(**kwargs)
1520 def instFluxToNanojansky(self, instFlux, localCalib):
1521 """Convert instrument flux to nanojanskys.
1523 Parameters
1524 ----------
1525 instFlux : `numpy.ndarray` or `pandas.Series`
1526 Array of instrument flux measurements
1527 localCalib : `numpy.ndarray` or `pandas.Series`
1528 Array of local photometric calibration estimates.
1530 Returns
1531 -------
1532 calibFlux : `numpy.ndarray` or `pandas.Series`
1533 Array of calibrated flux measurements.
1534 """
1535 return instFlux * localCalib
1537 def instFluxErrToNanojanskyErr(self, instFlux, instFluxErr, localCalib, localCalibErr):
1538 """Convert instrument flux to nanojanskys.
1540 Parameters
1541 ----------
1542 instFlux : `numpy.ndarray` or `pandas.Series`
1543 Array of instrument flux measurements
1544 instFluxErr : `numpy.ndarray` or `pandas.Series`
1545 Errors on associated ``instFlux`` values
1546 localCalib : `numpy.ndarray` or `pandas.Series`
1547 Array of local photometric calibration estimates.
1548 localCalibErr : `numpy.ndarray` or `pandas.Series`
1549 Errors on associated ``localCalib`` values
1551 Returns
1552 -------
1553 calibFluxErr : `numpy.ndarray` or `pandas.Series`
1554 Errors on calibrated flux measurements.
1555 """
1556 return np.hypot(instFluxErr * localCalib, instFlux * localCalibErr)
1558 def instFluxToMagnitude(self, instFlux, localCalib):
1559 """Convert instrument flux to nanojanskys.
1561 Parameters
1562 ----------
1563 instFlux : `numpy.ndarray` or `pandas.Series`
1564 Array of instrument flux measurements
1565 localCalib : `numpy.ndarray` or `pandas.Series`
1566 Array of local photometric calibration estimates.
1568 Returns
1569 -------
1570 calibMag : `numpy.ndarray` or `pandas.Series`
1571 Array of calibrated AB magnitudes.
1572 """
1573 return -2.5 * np.log10(self.instFluxToNanojansky(instFlux, localCalib)) + self.logNJanskyToAB
1575 def instFluxErrToMagnitudeErr(self, instFlux, instFluxErr, localCalib, localCalibErr):
1576 """Convert instrument flux err to nanojanskys.
1578 Parameters
1579 ----------
1580 instFlux : `numpy.ndarray` or `pandas.Series`
1581 Array of instrument flux measurements
1582 instFluxErr : `numpy.ndarray` or `pandas.Series`
1583 Errors on associated ``instFlux`` values
1584 localCalib : `numpy.ndarray` or `pandas.Series`
1585 Array of local photometric calibration estimates.
1586 localCalibErr : `numpy.ndarray` or `pandas.Series`
1587 Errors on associated ``localCalib`` values
1589 Returns
1590 -------
1591 calibMagErr: `numpy.ndarray` or `pandas.Series`
1592 Error on calibrated AB magnitudes.
1593 """
1594 err = self.instFluxErrToNanojanskyErr(instFlux, instFluxErr, localCalib, localCalibErr)
1595 return 2.5 / np.log(10) * err / self.instFluxToNanojansky(instFlux, instFluxErr)
1598class LocalNanojansky(LocalPhotometry):
1599 """Compute calibrated fluxes using the local calibration value.
1601 See also
1602 --------
1603 LocalNanojansky
1604 LocalNanojanskyErr
1605 LocalMagnitude
1606 LocalMagnitudeErr
1607 """
1609 @property
1610 def columns(self):
1611 return [self.instFluxCol, self.photoCalibCol]
1613 @property
1614 def name(self):
1615 return f'flux_{self.instFluxCol}'
1617 def _func(self, df):
1618 return self.instFluxToNanojansky(df[self.instFluxCol], df[self.photoCalibCol])
1621class LocalNanojanskyErr(LocalPhotometry):
1622 """Compute calibrated flux errors using the local calibration value.
1624 See also
1625 --------
1626 LocalNanojansky
1627 LocalNanojanskyErr
1628 LocalMagnitude
1629 LocalMagnitudeErr
1630 """
1632 @property
1633 def columns(self):
1634 return [self.instFluxCol, self.instFluxErrCol,
1635 self.photoCalibCol, self.photoCalibErrCol]
1637 @property
1638 def name(self):
1639 return f'fluxErr_{self.instFluxCol}'
1641 def _func(self, df):
1642 return self.instFluxErrToNanojanskyErr(df[self.instFluxCol], df[self.instFluxErrCol],
1643 df[self.photoCalibCol], df[self.photoCalibErrCol])
1646class LocalMagnitude(LocalPhotometry):
1647 """Compute calibrated AB magnitudes using the local calibration value.
1649 See also
1650 --------
1651 LocalNanojansky
1652 LocalNanojanskyErr
1653 LocalMagnitude
1654 LocalMagnitudeErr
1655 """
1657 @property
1658 def columns(self):
1659 return [self.instFluxCol, self.photoCalibCol]
1661 @property
1662 def name(self):
1663 return f'mag_{self.instFluxCol}'
1665 def _func(self, df):
1666 return self.instFluxToMagnitude(df[self.instFluxCol],
1667 df[self.photoCalibCol])
1670class LocalMagnitudeErr(LocalPhotometry):
1671 """Compute calibrated AB magnitude errors using the local calibration value.
1673 See also
1674 --------
1675 LocalNanojansky
1676 LocalNanojanskyErr
1677 LocalMagnitude
1678 LocalMagnitudeErr
1679 """
1681 @property
1682 def columns(self):
1683 return [self.instFluxCol, self.instFluxErrCol,
1684 self.photoCalibCol, self.photoCalibErrCol]
1686 @property
1687 def name(self):
1688 return f'magErr_{self.instFluxCol}'
1690 def _func(self, df):
1691 return self.instFluxErrToMagnitudeErr(df[self.instFluxCol],
1692 df[self.instFluxErrCol],
1693 df[self.photoCalibCol],
1694 df[self.photoCalibErrCol])
1697class LocalDipoleMeanFlux(LocalPhotometry):
1698 """Compute absolute mean of dipole fluxes.
1700 See also
1701 --------
1702 LocalNanojansky
1703 LocalNanojanskyErr
1704 LocalMagnitude
1705 LocalMagnitudeErr
1706 LocalDipoleMeanFlux
1707 LocalDipoleMeanFluxErr
1708 LocalDipoleDiffFlux
1709 LocalDipoleDiffFluxErr
1710 """
1711 def __init__(self,
1712 instFluxPosCol,
1713 instFluxNegCol,
1714 instFluxPosErrCol,
1715 instFluxNegErrCol,
1716 photoCalibCol,
1717 photoCalibErrCol,
1718 **kwargs):
1719 self.instFluxNegCol = instFluxNegCol
1720 self.instFluxPosCol = instFluxPosCol
1721 self.instFluxNegErrCol = instFluxNegErrCol
1722 self.instFluxPosErrCol = instFluxPosErrCol
1723 self.photoCalibCol = photoCalibCol
1724 self.photoCalibErrCol = photoCalibErrCol
1725 super().__init__(instFluxNegCol,
1726 instFluxNegErrCol,
1727 photoCalibCol,
1728 photoCalibErrCol,
1729 **kwargs)
1731 @property
1732 def columns(self):
1733 return [self.instFluxPosCol,
1734 self.instFluxNegCol,
1735 self.photoCalibCol]
1737 @property
1738 def name(self):
1739 return f'dipMeanFlux_{self.instFluxPosCol}_{self.instFluxNegCol}'
1741 def _func(self, df):
1742 return 0.5*(np.fabs(self.instFluxToNanojansky(df[self.instFluxNegCol], df[self.photoCalibCol]))
1743 + np.fabs(self.instFluxToNanojansky(df[self.instFluxPosCol], df[self.photoCalibCol])))
1746class LocalDipoleMeanFluxErr(LocalDipoleMeanFlux):
1747 """Compute the error on the absolute mean of dipole fluxes.
1749 See also
1750 --------
1751 LocalNanojansky
1752 LocalNanojanskyErr
1753 LocalMagnitude
1754 LocalMagnitudeErr
1755 LocalDipoleMeanFlux
1756 LocalDipoleMeanFluxErr
1757 LocalDipoleDiffFlux
1758 LocalDipoleDiffFluxErr
1759 """
1761 @property
1762 def columns(self):
1763 return [self.instFluxPosCol,
1764 self.instFluxNegCol,
1765 self.instFluxPosErrCol,
1766 self.instFluxNegErrCol,
1767 self.photoCalibCol,
1768 self.photoCalibErrCol]
1770 @property
1771 def name(self):
1772 return f'dipMeanFluxErr_{self.instFluxPosCol}_{self.instFluxNegCol}'
1774 def _func(self, df):
1775 return 0.5*np.sqrt(
1776 (np.fabs(df[self.instFluxNegCol]) + np.fabs(df[self.instFluxPosCol])
1777 * df[self.photoCalibErrCol])**2
1778 + (df[self.instFluxNegErrCol]**2 + df[self.instFluxPosErrCol]**2)
1779 * df[self.photoCalibCol]**2)
1782class LocalDipoleDiffFlux(LocalDipoleMeanFlux):
1783 """Compute the absolute difference of dipole fluxes.
1785 Value is (abs(pos) - abs(neg))
1787 See also
1788 --------
1789 LocalNanojansky
1790 LocalNanojanskyErr
1791 LocalMagnitude
1792 LocalMagnitudeErr
1793 LocalDipoleMeanFlux
1794 LocalDipoleMeanFluxErr
1795 LocalDipoleDiffFlux
1796 LocalDipoleDiffFluxErr
1797 """
1799 @property
1800 def columns(self):
1801 return [self.instFluxPosCol,
1802 self.instFluxNegCol,
1803 self.photoCalibCol]
1805 @property
1806 def name(self):
1807 return f'dipDiffFlux_{self.instFluxPosCol}_{self.instFluxNegCol}'
1809 def _func(self, df):
1810 return (np.fabs(self.instFluxToNanojansky(df[self.instFluxPosCol], df[self.photoCalibCol]))
1811 - np.fabs(self.instFluxToNanojansky(df[self.instFluxNegCol], df[self.photoCalibCol])))
1814class LocalDipoleDiffFluxErr(LocalDipoleMeanFlux):
1815 """Compute the error on the absolute difference of dipole fluxes.
1817 See also
1818 --------
1819 LocalNanojansky
1820 LocalNanojanskyErr
1821 LocalMagnitude
1822 LocalMagnitudeErr
1823 LocalDipoleMeanFlux
1824 LocalDipoleMeanFluxErr
1825 LocalDipoleDiffFlux
1826 LocalDipoleDiffFluxErr
1827 """
1829 @property
1830 def columns(self):
1831 return [self.instFluxPosCol,
1832 self.instFluxNegCol,
1833 self.instFluxPosErrCol,
1834 self.instFluxNegErrCol,
1835 self.photoCalibCol,
1836 self.photoCalibErrCol]
1838 @property
1839 def name(self):
1840 return f'dipDiffFluxErr_{self.instFluxPosCol}_{self.instFluxNegCol}'
1842 def _func(self, df):
1843 return np.sqrt(
1844 ((np.fabs(df[self.instFluxPosCol]) - np.fabs(df[self.instFluxNegCol]))
1845 * df[self.photoCalibErrCol])**2
1846 + (df[self.instFluxPosErrCol]**2 + df[self.instFluxNegErrCol]**2)
1847 * df[self.photoCalibCol]**2)
1850class Ratio(Functor):
1851 """Base class for returning the ratio of 2 columns.
1853 Can be used to compute a Signal to Noise ratio for any input flux.
1855 Parameters
1856 ----------
1857 numerator : `str`
1858 Name of the column to use at the numerator in the ratio
1859 denominator : `str`
1860 Name of the column to use as the denominator in the ratio.
1861 """
1862 def __init__(self,
1863 numerator,
1864 denominator,
1865 **kwargs):
1866 self.numerator = numerator
1867 self.denominator = denominator
1868 super().__init__(**kwargs)
1870 @property
1871 def columns(self):
1872 return [self.numerator, self.denominator]
1874 @property
1875 def name(self):
1876 return f'ratio_{self.numerator}_{self.denominator}'
1878 def _func(self, df):
1879 with np.warnings.catch_warnings():
1880 np.warnings.filterwarnings('ignore', r'invalid value encountered')
1881 np.warnings.filterwarnings('ignore', r'divide by zero')
1882 return df[self.numerator] / df[self.denominator]