LSST Applications g070148d5b3+33e5256705,g0d53e28543+25c8b88941,g0da5cf3356+2dd1178308,g1081da9e2a+62d12e78cb,g17e5ecfddb+7e422d6136,g1c76d35bf8+ede3a706f7,g295839609d+225697d880,g2e2c1a68ba+cc1f6f037e,g2ffcdf413f+853cd4dcde,g38293774b4+62d12e78cb,g3b44f30a73+d953f1ac34,g48ccf36440+885b902d19,g4b2f1765b6+7dedbde6d2,g5320a0a9f6+0c5d6105b6,g56b687f8c9+ede3a706f7,g5c4744a4d9+ef6ac23297,g5ffd174ac0+0c5d6105b6,g6075d09f38+66af417445,g667d525e37+2ced63db88,g670421136f+2ced63db88,g71f27ac40c+2ced63db88,g774830318a+463cbe8d1f,g7876bc68e5+1d137996f1,g7985c39107+62d12e78cb,g7fdac2220c+0fd8241c05,g96f01af41f+368e6903a7,g9ca82378b8+2ced63db88,g9d27549199+ef6ac23297,gabe93b2c52+e3573e3735,gb065e2a02a+3dfbe639da,gbc3249ced9+0c5d6105b6,gbec6a3398f+0c5d6105b6,gc9534b9d65+35b9f25267,gd01420fc67+0c5d6105b6,geee7ff78d7+a14128c129,gf63283c776+ede3a706f7,gfed783d017+0c5d6105b6,w.2022.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
_actions.py
Go to the documentation of this file.
1from __future__ import annotations
2
3__all__ = ("SingleColumnAction", "MultiColumnAction", "CoordColumn", "MagColumnDN", "SumColumns", "AddColumn",
4 "DivideColumns", "SubtractColumns", "MultiplyColumns", "FractionalDifferenceColumns",
5 "MagColumnNanoJansky", "DiffOfDividedColumns", "PercentDiffOfDividedColumns",)
6
7from typing import Iterable
8
9import numpy as np
10import pandas as pd
11from astropy import units
12
13from ..configurableActions import ConfigurableActionStructField, ConfigurableActionField
14from ._baseDataFrameActions import DataFrameAction
15from ._evalColumnExpression import makeColumnExpressionAction
16
17from lsst.pex.config import Field
18
19
21 column = Field(doc="Column to load for this action", dtype=str, optional=False)
22
23 @property
24 def columns(self) -> Iterable[str]:
25 return (self.column, )
26
27 def __call__(self, df, **kwargs):
28 return df[self.column]
29
30
32 actions = ConfigurableActionStructField(doc="Configurable actions to use in a joint action")
33
34 @property
35 def columns(self) -> Iterable[str]:
36 yield from (column for action in self.actions for column in action.columns)
37
38
40 inRadians = Field(doc="Return the column in radians if true", default=True, dtype=bool)
41
42 def __call__(self, df):
43 col = super().__call__(df)
44 return col * 180 / np.pi if self.inRadians else col
45
46
48 coadd_zeropoint = Field(doc="Magnitude zero point", dtype=float, default=27)
49
50 def __call__(self, df: pd.DataFrame, **kwargs):
51 if not (fluxMag0 := kwargs.get('fluxMag0')):
52 fluxMag0 = 1/np.power(10, -0.4*self.coadd_zeropoint)
53
54 with np.warnings.catch_warnings():
55 np.warnings.filterwarnings('ignore', r'invalid value encountered')
56 np.warnings.filterwarnings('ignore', r'divide by zero')
57 return -2.5 * np.log10(df[self.column] / fluxMag0)
58
59
61
62 def __call__(self, df: pd.DataFrame, **kwargs):
63
64 with np.warnings.catch_warnings():
65 np.warnings.filterwarnings('ignore', r'invalid value encountered')
66 np.warnings.filterwarnings('ignore', r'divide by zero')
67 return -2.5 * np.log10((df[self.column] * 1e-9) / 3631.0)
68
69
71 ab_flux_scale = Field(doc="Scaling of ab flux", dtype=float, default=(0*units.ABmag).to_value(units.nJy))
72 coadd_zeropoint = Field(doc="Magnitude zero point", dtype=float, default=27)
73
74 def __call__(self, df, **kwargs):
75 dataNumber = super().__call__(df, **kwargs)
76 if not (fluxMag0 := kwargs.get('fluxMag0')):
77 fluxMag0 = 1/np.power(10, -0.4*self.coadd_zeropoint)
78 return self.ab_flux_scale * dataNumber / fluxMag0
79
80 def setDefaults(self):
81 super().setDefaults()
82 self.cachecache = True # cache this action for future calls
83
84
86 flux_mag_err = Field(doc="Error in the magnitude zeropoint", dtype=float, default=0)
87 flux_action = ConfigurableActionField(doc="Action to use if flux is not provided to the call method",
88 default=NanoJansky, dtype=DataFrameAction)
89
90 @property
91 def columns(self):
92 yield from zip((self.column,), self.flux_action.columns)
93
94 def __call__(self, df, flux_column=None, flux_mag_err=None, **kwargs):
95 if flux_column is None:
96 flux_column = self.flux_action(df, **kwargs)
97 if flux_mag_err is None:
98 flux_mag_err = self.flux_mag_err
99
100
101_docs = """This is a `DataFrameAction` that is designed to add two columns
102together and return the result.
103"""
104SumColumns = makeColumnExpressionAction("SumColumns", "colA+colB",
105 exprDefaults={"colA": SingleColumnAction,
106 "colB": SingleColumnAction},
107 docstring=_docs)
108
109_docs = """This is a `MultiColumnAction` that is designed to subtract two columns
110together and return the result.
111"""
112SubtractColumns = makeColumnExpressionAction("SubtractColumns", "colA-colB",
113 exprDefaults={"colA": SingleColumnAction,
114 "colB": SingleColumnAction},
115 docstring=_docs)
116
117_docs = """This is a `MultiColumnAction` that is designed to multiply two columns
118together and return the result.
119"""
120MultiplyColumns = makeColumnExpressionAction("MultiplyColumns", "colA*colB",
121 exprDefaults={"colA": SingleColumnAction,
122 "colB": SingleColumnAction},
123 docstring=_docs)
124
125_docs = """This is a `MultiColumnAction` that is designed to divide two columns
126together and return the result.
127"""
128DivideColumns = makeColumnExpressionAction("DivideColumns", "colA/colB",
129 exprDefaults={"colA": SingleColumnAction,
130 "colB": SingleColumnAction},
131 docstring=_docs)
132
133_docs = """This is a `MultiColumnAction` that is designed to divide two columns
134together, subtract one and return the result.
135"""
136FractionalDifferenceColumns = makeColumnExpressionAction("FractionalDifferenceColumns", "(colA-colB)/colB",
137 exprDefaults={"colA": SingleColumnAction,
138 "colB": SingleColumnAction},
139 docstring=_docs)
140
141_docs = """This is a `MultiColumnAction` that is designed to subtract the division of two columns
142from the division of two other columns and return the result (i.e. colA1/colB1 - colA2/colB2).
143"""
144DiffOfDividedColumns = makeColumnExpressionAction("DiffOfDividedColumns", "(colA1/colB1)-(colA2/colB2)",
145 exprDefaults={"colA1": SingleColumnAction,
146 "colB1": SingleColumnAction,
147 "colA2": SingleColumnAction,
148 "colB2": SingleColumnAction},
149 docstring=_docs)
150_docs = """This is a `MultiColumnAction` that is designed to compute the percent difference
151between the division of two columns and the division of two other columns and return the result
152(i.e. 100*((colA1/colB1 - colA2/colB2)/(colA1/colB1))).
153"""
154PercentDiffOfDividedColumns = makeColumnExpressionAction("PercentDiffOfDividedColumns",
155 "100*(((colA1/colB1)-(colA2/colB2))/(colA1/colB1))",
156 exprDefaults={"colA1": SingleColumnAction,
157 "colB1": SingleColumnAction,
158 "colA2": SingleColumnAction,
159 "colB2": SingleColumnAction},
160 docstring=_docs)
161
162
164 aggregator = ConfigurableActionField(doc="This is an instance of a Dataframe action that will be used "
165 "to create a new column", dtype=DataFrameAction)
166 newColumn = Field(doc="Name of the new column to add", dtype=str)
167
168 @property
169 def columns(self) -> Iterable[str]:
170 yield from self.aggregator.columns
171
172 def __call__(self, df, **kwargs) -> pd.DataFrame:
173 # do your calculation and and
174 df[self.newColumn] = self.aggregator(df, kwargs)
175 return df