LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
_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",)
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"""
136FractionalDifferenceColumns = makeColumnExpressionAction("FractionalDifferenceColumns", "(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)