lsst.pipe.tasks g13b7538eaf+caabbbbd3a
_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", "FractionalResidualColumns",
5 "MagColumnNanoJansky",)
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.columncolumn, )
26
27 def __call__(self, df, **kwargs):
28 return df[self.columncolumn]
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.actionsactions 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.inRadiansinRadians 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_zeropointcoadd_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.columncolumn] / 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.columncolumn] * 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_zeropointcoadd_zeropoint)
78 return self.ab_flux_scaleab_flux_scale * dataNumber / fluxMag0
79
80 def setDefaults(self):
81 super().setDefaults()
82 self.cachecachecache = 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.columncolumn,), self.flux_actionflux_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_actionflux_action(df, **kwargs)
97 if flux_mag_err is None:
98 flux_mag_err = self.flux_mag_errflux_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"""
136FractionalResidualColumns = makeColumnExpressionAction("FractionalResidualColumns", "(colA-colB)/colB",
137 exprDefaults={"colA": SingleColumnAction,
138 "colB": SingleColumnAction},
139 docstring=_docs)
140
141
143 aggregator = ConfigurableActionField(doc="This is an instance of a Dataframe action that will be used "
144 "to create a new column", dtype=DataFrameAction)
145 newColumn = Field(doc="Name of the new column to add", dtype=str)
146
147 @property
148 def columns(self) -> Iterable[str]:
149 yield from self.aggregatoraggregator.columns
150
151 def __call__(self, df, **kwargs) -> pd.DataFrame:
152 # do your calculation and and
153 df[self.newColumnnewColumn] = self.aggregatoraggregator(df, kwargs)
154 return df
Type[DataFrameAction] makeColumnExpressionAction(str className, str expr, Optional[Mapping[str, Union[DataFrameAction, Type[DataFrameAction]]]] exprDefaults=None, str docstring=None)