Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from __future__ import annotations 

2 

3__all__ = ("SingleColumnAction", "MultiColumnAction", "CoordColumn", "MagColumnDN", "SumColumns", "AddColumn", 

4 "DivideColumns", "SubtractColumns", "MultiplyColumns",) 

5 

6from typing import Iterable 

7 

8import numpy as np 

9import pandas as pd 

10from astropy import units 

11 

12from ..configurableActions import ConfigurableActionStructField, ConfigurableActionField 

13from ._baseDataFrameActions import DataFrameAction 

14from ._evalColumnExpression import makeColumnExpressionAction 

15 

16from lsst.pex.config import Field 

17 

18 

19class SingleColumnAction(DataFrameAction): 

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.column, ) 

25 

26 def __call__(self, df, **kwargs): 

27 return df[self.column] 

28 

29 

30class MultiColumnAction(DataFrameAction): 

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.actions for column in action.columns) 

36 

37 

38class CoordColumn(SingleColumnAction): 

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.inRadians else col 

44 

45 

46class MagColumnDN(SingleColumnAction): 

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_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.column] / fluxMag0) 

57 

58 

59class NanoJansky(SingleColumnAction): 

60 ab_flux_scale = Field(doc="Scaling of ab flux", dtype=float, default=(0*units.ABmag).to_value(units.nJy)) 

61 coadd_zeropoint = Field(doc="Magnitude zero point", dtype=float, default=27) 

62 

63 def __call__(self, df, **kwargs): 

64 dataNumber = super().__call__(df, **kwargs) 

65 if not (fluxMag0 := kwargs.get('fluxMag0')): 

66 fluxMag0 = 1/np.power(10, -0.4*self.coadd_zeropoint) 

67 return self.ab_flux_scale * dataNumber / fluxMag0 

68 

69 def setDefaults(self): 

70 super().setDefaults() 

71 self.cache = True # cache this action for future calls 

72 

73 

74class NanoJanskyErr(SingleColumnAction): 

75 flux_mag_err = Field(doc="Error in the magnitude zeropoint", dtype=float, default=0) 

76 flux_action = ConfigurableActionField(doc="Action to use if flux is not provided to the call method", 

77 default=NanoJansky, dtype=DataFrameAction) 

78 

79 @property 

80 def columns(self): 

81 yield from zip((self.column,), self.flux_action.columns) 

82 

83 def __call__(self, df, flux_column=None, flux_mag_err=None, **kwargs): 

84 if flux_column is None: 

85 flux_column = self.flux_action(df, **kwargs) 

86 if flux_mag_err is None: 

87 flux_mag_err = self.flux_mag_err 

88 

89 

90_docs = """This is a `DataFrameAction` that is designed to add two columns 

91together and return the result. 

92""" 

93SumColumns = makeColumnExpressionAction("SumColumns", "colA+colB", 

94 exprDefaults={"colA": SingleColumnAction, 

95 "colB": SingleColumnAction}, 

96 docstring=_docs) 

97 

98_docs = """This is a `MultiColumnAction` that is designed to subtract two columns 

99together and return the result. 

100""" 

101SubtractColumns = makeColumnExpressionAction("SubtractColumns", "colA-colB", 

102 exprDefaults={"colA": SingleColumnAction, 

103 "colB": SingleColumnAction}, 

104 docstring=_docs) 

105 

106_docs = """This is a `MultiColumnAction` that is designed to multiply two columns 

107together and return the result. 

108""" 

109MultiplyColumns = makeColumnExpressionAction("MultiplyColumns", "colA*colB", 

110 exprDefaults={"colA": SingleColumnAction, 

111 "colB": SingleColumnAction}, 

112 docstring=_docs) 

113 

114_docs = """This is a `MultiColumnAction` that is designed to multiply two columns 

115together and return the result. 

116""" 

117DivideColumns = makeColumnExpressionAction("DivideColumns", "colA/colB", 

118 exprDefaults={"colA": SingleColumnAction, 

119 "colB": SingleColumnAction}, 

120 docstring=_docs) 

121 

122 

123class AddColumn(DataFrameAction): 

124 aggregator = ConfigurableActionField(doc="This is an instance of a Dataframe action that will be used " 

125 "to create a new column", dtype=DataFrameAction) 

126 newColumn = Field(doc="Name of the new column to add", dtype=str) 

127 

128 @property 

129 def columns(self) -> Iterable[str]: 

130 yield from self.aggregator.columns 

131 

132 def __call__(self, df, **kwargs) -> pd.DataFrame: 

133 # do your calculation and and 

134 df[self.newColumn] = self.aggregator(df, kwargs) 

135 return df