lsst.pipe.tasks
g2c8cee2ce7+1ac6cedd78
Toggle main menu visibility
Loading...
Searching...
No Matches
python
lsst
pipe
tasks
dataFrameActions
_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"
,
"FractionalDifferenceColumns"
,
5
"MagColumnNanoJansky"
,
"DiffOfDividedColumns"
,
"PercentDiffOfDividedColumns"
,)
6
7
from
typing
import
Iterable
8
9
import
warnings
10
import
numpy
as
np
11
import
pandas
as
pd
12
from
astropy
import
units
13
14
from
lsst.pex.config.configurableActions
import
ConfigurableActionStructField, ConfigurableActionField
15
from
._baseDataFrameActions
import
DataFrameAction
16
from
._evalColumnExpression
import
makeColumnExpressionAction
17
18
from
lsst.pex.config
import
Field
19
20
21
class
SingleColumnAction
(
DataFrameAction
):
22
column = Field(doc=
"Column to load for this action"
, dtype=str, optional=
False
)
23
24
@property
25
def
columns
(self) -> Iterable[str]:
26
return
(self.
column
, )
27
28
def
__call__
(self, df, **kwargs):
29
return
df[self.
column
]
30
31
32
class
MultiColumnAction
(
DataFrameAction
):
33
actions = ConfigurableActionStructField(doc=
"Configurable actions to use in a joint action"
)
34
35
@property
36
def
columns
(self) -> Iterable[str]:
37
yield
from
(column
for
action
in
self.
actions
for
column
in
action.columns)
38
39
40
class
CoordColumn
(
SingleColumnAction
):
41
inRadians = Field(doc=
"Return the column in radians if true"
, default=
True
, dtype=bool)
42
43
def
__call__
(self, df):
44
col = super().
__call__
(df)
45
return
col * 180 / np.pi
if
self.
inRadians
else
col
46
47
48
class
MagColumnDN
(
SingleColumnAction
):
49
coadd_zeropoint = Field(doc=
"Magnitude zero point"
, dtype=float, default=27)
50
51
def
__call__
(self, df: pd.DataFrame, **kwargs):
52
if
not
(fluxMag0 := kwargs.get(
'fluxMag0'
)):
53
fluxMag0 = 1/np.power(10, -0.4*self.
coadd_zeropoint
)
54
55
with
warnings.catch_warnings():
56
warnings.filterwarnings(
'ignore'
,
r'invalid value encountered'
)
57
warnings.filterwarnings(
'ignore'
,
r'divide by zero'
)
58
return
-2.5 * np.log10(df[self.
column
] / fluxMag0)
59
60
61
class
MagColumnNanoJansky
(
SingleColumnAction
):
62
63
def
__call__
(self, df: pd.DataFrame, **kwargs):
64
65
with
warnings.catch_warnings():
66
warnings.filterwarnings(
'ignore'
,
r'invalid value encountered'
)
67
warnings.filterwarnings(
'ignore'
,
r'divide by zero'
)
68
return
-2.5 * np.log10((df[self.
column
] * 1e-9) / 3631.0)
69
70
71
class
NanoJansky
(
SingleColumnAction
):
72
ab_flux_scale = Field(doc=
"Scaling of ab flux"
, dtype=float, default=(0*units.ABmag).to_value(units.nJy))
73
coadd_zeropoint = Field(doc=
"Magnitude zero point"
, dtype=float, default=27)
74
75
def
__call__
(self, df, **kwargs):
76
dataNumber = super().
__call__
(df, **kwargs)
77
if
not
(fluxMag0 := kwargs.get(
'fluxMag0'
)):
78
fluxMag0 = 1/np.power(10, -0.4*self.
coadd_zeropoint
)
79
return
self.
ab_flux_scale
* dataNumber / fluxMag0
80
81
def
setDefaults
(self):
82
super().
setDefaults
()
83
self.
cache
=
True
# cache this action for future calls
84
85
86
class
NanoJanskyErr
(
SingleColumnAction
):
87
flux_mag_err = Field(doc=
"Error in the magnitude zeropoint"
, dtype=float, default=0)
88
flux_action = ConfigurableActionField(doc=
"Action to use if flux is not provided to the call method"
,
89
default=NanoJansky, dtype=DataFrameAction)
90
91
@property
92
def
columns
(self):
93
yield
from
zip((self.
column
,), self.
flux_action
.columns)
94
95
def
__call__
(self, df, flux_column=None, flux_mag_err=None, **kwargs):
96
if
flux_column
is
None
:
97
flux_column = self.
flux_action
(df, **kwargs)
98
if
flux_mag_err
is
None
:
99
flux_mag_err = self.
flux_mag_err
100
101
102
_docs =
"""This is a `DataFrameAction` that is designed to add two columns
103
together and return the result.
104
"""
105
SumColumns =
makeColumnExpressionAction
(
"SumColumns"
,
"colA+colB"
,
106
exprDefaults={
"colA"
: SingleColumnAction,
107
"colB"
: SingleColumnAction},
108
docstring=_docs)
109
110
_docs =
"""This is a `MultiColumnAction` that is designed to subtract two columns
111
together and return the result.
112
"""
113
SubtractColumns =
makeColumnExpressionAction
(
"SubtractColumns"
,
"colA-colB"
,
114
exprDefaults={
"colA"
: SingleColumnAction,
115
"colB"
: SingleColumnAction},
116
docstring=_docs)
117
118
_docs =
"""This is a `MultiColumnAction` that is designed to multiply two columns
119
together and return the result.
120
"""
121
MultiplyColumns =
makeColumnExpressionAction
(
"MultiplyColumns"
,
"colA*colB"
,
122
exprDefaults={
"colA"
: SingleColumnAction,
123
"colB"
: SingleColumnAction},
124
docstring=_docs)
125
126
_docs =
"""This is a `MultiColumnAction` that is designed to divide two columns
127
together and return the result.
128
"""
129
DivideColumns =
makeColumnExpressionAction
(
"DivideColumns"
,
"colA/colB"
,
130
exprDefaults={
"colA"
: SingleColumnAction,
131
"colB"
: SingleColumnAction},
132
docstring=_docs)
133
134
_docs =
"""This is a `MultiColumnAction` that is designed to divide two columns
135
together, subtract one and return the result.
136
"""
137
FractionalDifferenceColumns =
makeColumnExpressionAction
(
"FractionalDifferenceColumns"
,
"(colA-colB)/colB"
,
138
exprDefaults={
"colA"
: SingleColumnAction,
139
"colB"
: SingleColumnAction},
140
docstring=_docs)
141
142
_docs =
"""This is a `MultiColumnAction` that is designed to subtract the division of two columns
143
from the division of two other columns and return the result (i.e. colA1/colB1 - colA2/colB2).
144
"""
145
DiffOfDividedColumns =
makeColumnExpressionAction
(
"DiffOfDividedColumns"
,
"(colA1/colB1)-(colA2/colB2)"
,
146
exprDefaults={
"colA1"
: SingleColumnAction,
147
"colB1"
: SingleColumnAction,
148
"colA2"
: SingleColumnAction,
149
"colB2"
: SingleColumnAction},
150
docstring=_docs)
151
_docs =
"""This is a `MultiColumnAction` that is designed to compute the percent difference
152
between the division of two columns and the division of two other columns and return the result
153
(i.e. 100*((colA1/colB1 - colA2/colB2)/(colA1/colB1))).
154
"""
155
PercentDiffOfDividedColumns =
makeColumnExpressionAction
(
"PercentDiffOfDividedColumns"
,
156
"100*(((colA1/colB1)-(colA2/colB2))/(colA1/colB1))"
,
157
exprDefaults={
"colA1"
: SingleColumnAction,
158
"colB1"
: SingleColumnAction,
159
"colA2"
: SingleColumnAction,
160
"colB2"
: SingleColumnAction},
161
docstring=_docs)
162
163
164
class
AddColumn
(
DataFrameAction
):
165
aggregator = ConfigurableActionField(doc=
"This is an instance of a Dataframe action that will be used "
166
"to create a new column"
, dtype=DataFrameAction)
167
newColumn = Field(doc=
"Name of the new column to add"
, dtype=str)
168
169
@property
170
def
columns
(self) -> Iterable[str]:
171
yield
from
self.
aggregator
.columns
172
173
def
__call__
(self, df, **kwargs) -> pd.DataFrame:
174
# do your calculation and and
175
df[self.
newColumn
] = self.
aggregator
(df, kwargs)
176
return
df
lsst::pex::config::configurableActions
lsst.pipe.tasks.dataFrameActions._actions.AddColumn
Definition
_actions.py:164
lsst.pipe.tasks.dataFrameActions._actions.AddColumn.__call__
pd.DataFrame __call__(self, df, **kwargs)
Definition
_actions.py:173
lsst.pipe.tasks.dataFrameActions._actions.AddColumn.columns
Iterable[str] columns(self)
Definition
_actions.py:170
lsst.pipe.tasks.dataFrameActions._actions.AddColumn.newColumn
newColumn
Definition
_actions.py:167
lsst.pipe.tasks.dataFrameActions._actions.AddColumn.aggregator
aggregator
Definition
_actions.py:165
lsst.pipe.tasks.dataFrameActions._actions.CoordColumn
Definition
_actions.py:40
lsst.pipe.tasks.dataFrameActions._actions.CoordColumn.__call__
__call__(self, df)
Definition
_actions.py:43
lsst.pipe.tasks.dataFrameActions._actions.CoordColumn.inRadians
inRadians
Definition
_actions.py:41
lsst.pipe.tasks.dataFrameActions._actions.MagColumnDN
Definition
_actions.py:48
lsst.pipe.tasks.dataFrameActions._actions.MagColumnDN.coadd_zeropoint
coadd_zeropoint
Definition
_actions.py:49
lsst.pipe.tasks.dataFrameActions._actions.MagColumnDN.__call__
__call__(self, pd.DataFrame df, **kwargs)
Definition
_actions.py:51
lsst.pipe.tasks.dataFrameActions._actions.MagColumnNanoJansky
Definition
_actions.py:61
lsst.pipe.tasks.dataFrameActions._actions.MagColumnNanoJansky.__call__
__call__(self, pd.DataFrame df, **kwargs)
Definition
_actions.py:63
lsst.pipe.tasks.dataFrameActions._actions.MultiColumnAction
Definition
_actions.py:32
lsst.pipe.tasks.dataFrameActions._actions.MultiColumnAction.columns
Iterable[str] columns(self)
Definition
_actions.py:36
lsst.pipe.tasks.dataFrameActions._actions.MultiColumnAction.actions
actions
Definition
_actions.py:33
lsst.pipe.tasks.dataFrameActions._actions.NanoJanskyErr
Definition
_actions.py:86
lsst.pipe.tasks.dataFrameActions._actions.NanoJanskyErr.columns
columns(self)
Definition
_actions.py:92
lsst.pipe.tasks.dataFrameActions._actions.NanoJanskyErr.flux_mag_err
flux_mag_err
Definition
_actions.py:87
lsst.pipe.tasks.dataFrameActions._actions.NanoJanskyErr.flux_action
flux_action
Definition
_actions.py:88
lsst.pipe.tasks.dataFrameActions._actions.NanoJanskyErr.__call__
__call__(self, df, flux_column=None, flux_mag_err=None, **kwargs)
Definition
_actions.py:95
lsst.pipe.tasks.dataFrameActions._actions.NanoJansky
Definition
_actions.py:71
lsst.pipe.tasks.dataFrameActions._actions.NanoJansky.coadd_zeropoint
coadd_zeropoint
Definition
_actions.py:73
lsst.pipe.tasks.dataFrameActions._actions.NanoJansky.ab_flux_scale
ab_flux_scale
Definition
_actions.py:72
lsst.pipe.tasks.dataFrameActions._actions.NanoJansky.setDefaults
setDefaults(self)
Definition
_actions.py:81
lsst.pipe.tasks.dataFrameActions._actions.NanoJansky.__call__
__call__(self, df, **kwargs)
Definition
_actions.py:75
lsst.pipe.tasks.dataFrameActions._actions.SingleColumnAction
Definition
_actions.py:21
lsst.pipe.tasks.dataFrameActions._actions.SingleColumnAction.columns
Iterable[str] columns(self)
Definition
_actions.py:25
lsst.pipe.tasks.dataFrameActions._actions.SingleColumnAction.column
column
Definition
_actions.py:22
lsst.pipe.tasks.dataFrameActions._actions.SingleColumnAction.__call__
__call__(self, df, **kwargs)
Definition
_actions.py:28
lsst.pipe.tasks.dataFrameActions._baseDataFrameActions.DataFrameAction
Definition
_baseDataFrameActions.py:11
lsst::pex::config
lsst.pipe.tasks.dataFrameActions._evalColumnExpression.makeColumnExpressionAction
Type[DataFrameAction] makeColumnExpressionAction(str className, str expr, Optional[Mapping[str, Union[DataFrameAction, Type[DataFrameAction]]]] exprDefaults=None, str docstring=None)
Definition
_evalColumnExpression.py:87
Generated on
for lsst.pipe.tasks by
1.17.0