LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
_actions.py
Go to the documentation of this file.
1 from __future__ import annotations
2 
3 __all__ = ("SingleColumnAction", "MultiColumnAction", "CoordColumn", "MagColumnDN", "SumColumns", "AddColumn",
4  "DivideColumns", "SubtractColumns", "MultiplyColumns", "MagColumnNanoJansky",)
5 
6 from typing import Iterable
7 
8 import numpy as np
9 import pandas as pd
10 from astropy import units
11 
12 from ..configurableActions import ConfigurableActionStructField, ConfigurableActionField
13 from ._baseDataFrameActions import DataFrameAction
14 from ._evalColumnExpression import makeColumnExpressionAction
15 
16 from lsst.pex.config import Field
17 
18 
20  column = Field(doc="Column to load for this action", dtype=str, optional=False)
21 
22  @property
23  def columns(self) -> Iterable[str]:
24  return (self.columncolumn, )
25 
26  def __call__(self, df, **kwargs):
27  return df[self.columncolumn]
28 
29 
31  actions = ConfigurableActionStructField(doc="Configurable actions to use in a joint action")
32 
33  @property
34  def columns(self) -> Iterable[str]:
35  yield from (column for action in self.actionsactions for column in action.columns)
36 
37 
39  inRadians = Field(doc="Return the column in radians if true", default=True, dtype=bool)
40 
41  def __call__(self, df):
42  col = super().__call__(df)
43  return col * 180 / np.pi if self.inRadiansinRadians else col
44 
45 
47  coadd_zeropoint = Field(doc="Magnitude zero point", dtype=float, default=27)
48 
49  def __call__(self, df: pd.DataFrame, **kwargs):
50  if not (fluxMag0 := kwargs.get('fluxMag0')):
51  fluxMag0 = 1/np.power(10, -0.4*self.coadd_zeropointcoadd_zeropoint)
52 
53  with np.warnings.catch_warnings():
54  np.warnings.filterwarnings('ignore', r'invalid value encountered')
55  np.warnings.filterwarnings('ignore', r'divide by zero')
56  return -2.5 * np.log10(df[self.columncolumn] / fluxMag0)
57 
58 
60 
61  def __call__(self, df: pd.DataFrame, **kwargs):
62 
63  with np.warnings.catch_warnings():
64  np.warnings.filterwarnings('ignore', r'invalid value encountered')
65  np.warnings.filterwarnings('ignore', r'divide by zero')
66  return -2.5 * np.log10((df[self.columncolumn] * 1e-9) / 3631.0)
67 
68 
70  ab_flux_scale = Field(doc="Scaling of ab flux", dtype=float, default=(0*units.ABmag).to_value(units.nJy))
71  coadd_zeropoint = Field(doc="Magnitude zero point", dtype=float, default=27)
72 
73  def __call__(self, df, **kwargs):
74  dataNumber = super().__call__(df, **kwargs)
75  if not (fluxMag0 := kwargs.get('fluxMag0')):
76  fluxMag0 = 1/np.power(10, -0.4*self.coadd_zeropointcoadd_zeropoint)
77  return self.ab_flux_scaleab_flux_scale * dataNumber / fluxMag0
78 
79  def setDefaults(self):
80  super().setDefaults()
81  self.cachecachecache = True # cache this action for future calls
82 
83 
85  flux_mag_err = Field(doc="Error in the magnitude zeropoint", dtype=float, default=0)
86  flux_action = ConfigurableActionField(doc="Action to use if flux is not provided to the call method",
87  default=NanoJansky, dtype=DataFrameAction)
88 
89  @property
90  def columns(self):
91  yield from zip((self.columncolumn,), self.flux_actionflux_action.columns)
92 
93  def __call__(self, df, flux_column=None, flux_mag_err=None, **kwargs):
94  if flux_column is None:
95  flux_column = self.flux_actionflux_action(df, **kwargs)
96  if flux_mag_err is None:
97  flux_mag_err = self.flux_mag_errflux_mag_err
98 
99 
100 _docs = """This is a `DataFrameAction` that is designed to add two columns
101 together and return the result.
102 """
103 SumColumns = makeColumnExpressionAction("SumColumns", "colA+colB",
104  exprDefaults={"colA": SingleColumnAction,
105  "colB": SingleColumnAction},
106  docstring=_docs)
107 
108 _docs = """This is a `MultiColumnAction` that is designed to subtract two columns
109 together and return the result.
110 """
111 SubtractColumns = makeColumnExpressionAction("SubtractColumns", "colA-colB",
112  exprDefaults={"colA": SingleColumnAction,
113  "colB": SingleColumnAction},
114  docstring=_docs)
115 
116 _docs = """This is a `MultiColumnAction` that is designed to multiply two columns
117 together and return the result.
118 """
119 MultiplyColumns = makeColumnExpressionAction("MultiplyColumns", "colA*colB",
120  exprDefaults={"colA": SingleColumnAction,
121  "colB": SingleColumnAction},
122  docstring=_docs)
123 
124 _docs = """This is a `MultiColumnAction` that is designed to multiply two columns
125 together and return the result.
126 """
127 DivideColumns = makeColumnExpressionAction("DivideColumns", "colA/colB",
128  exprDefaults={"colA": SingleColumnAction,
129  "colB": SingleColumnAction},
130  docstring=_docs)
131 
132 
134  aggregator = ConfigurableActionField(doc="This is an instance of a Dataframe action that will be used "
135  "to create a new column", dtype=DataFrameAction)
136  newColumn = Field(doc="Name of the new column to add", dtype=str)
137 
138  @property
139  def columns(self) -> Iterable[str]:
140  yield from self.aggregatoraggregator.columns
141 
142  def __call__(self, df, **kwargs) -> pd.DataFrame:
143  # do your calculation and and
144  df[self.newColumnnewColumn] = self.aggregatoraggregator(df, kwargs)
145  return df
Type[DataFrameAction] makeColumnExpressionAction(str className, str expr, Optional[Mapping[str, Union[DataFrameAction, Type[DataFrameAction]]]] exprDefaults=None, str docstring=None)