LSST Applications g0265f82a02+0e5473021a,g02d81e74bb+0dd8ce4237,g1470d8bcf6+3ea6592b6f,g2079a07aa2+86d27d4dc4,g2305ad1205+5ca4c0b359,g295015adf3+d10818ec9d,g2a9a014e59+6f9be1b9cd,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g3ddfee87b4+703ba97ebf,g487adcacf7+4fa16da234,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+ffa42b374e,g5a732f18d5+53520f316c,g64a986408d+0dd8ce4237,g858d7b2824+0dd8ce4237,g8a8a8dda67+585e252eca,g99cad8db69+d39438377f,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+f1d96605c8,gb0e22166c9+60f28cb32d,gb6a65358fc+0e5473021a,gba4ed39666+c2a2e4ac27,gbb8dafda3b+e5339d463f,gc120e1dc64+da31e9920e,gc28159a63d+0e5473021a,gcf0d15dbbd+703ba97ebf,gdaeeff99f8+f9a426f77a,ge6526c86ff+889fc9d533,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gf18bd8381d+7268b93478,gff1a9f87cc+0dd8ce4237,w.2024.16
LSST Data Management Base Package
Loading...
Searching...
No Matches
Classes | Functions | Variables
lsst.pipe.tasks.dataFrameActions._evalColumnExpression Namespace Reference

Classes

class  ExpressionParser
 

Functions

Type[DataFrameActionmakeColumnExpressionAction (str className, str expr, Optional[Mapping[str, Union[DataFrameAction, Type[DataFrameAction]]]] exprDefaults=None, str docstring=None)
 

Variables

dict OPERATORS
 
dict EXTRA_MATH = {"cos": cos, "sin": sin, "cosh": cosh, "sinh": sinh, "log": log}
 

Function Documentation

◆ makeColumnExpressionAction()

Type[DataFrameAction] lsst.pipe.tasks.dataFrameActions._evalColumnExpression.makeColumnExpressionAction ( str className,
str expr,
Optional[Mapping[str, Union[DataFrameAction, Type[DataFrameAction]]]] exprDefaults = None,
str docstring = None )
Factory function for producing ConfigurableAction classes which are
realizations of arithmetic operations.

Parameters
----------
className : `str`
    The name of the class that will be produced
expr : `str`
    An arithmetic expression that will be parsed to produce the output
    ConfigurableAction. Individual variable names will be the name of
    individual `ConfigActions` inside the expression (i.e. "x+y" will
    produce an action with configAction.actions.x and
    configAction.actions.y). Expression can contain arithmatic python
    operators as well as; sin, cos, sinh, cosh, log (which is base 10).
exprDefaults : `Mapping` of `str` to `DataFrameAction` optional
    A mapping of strings which correspond to the names in the expression to
    values which are default `ConfigurableActions` to assign in the
    expression. If no default for a action is supplied `SingleColumnAction`
    is set as the default.
docstring : `str`
    A string that is assigned as the resulting classes docstring

Returns
-------
action : `Type` of `DataFrameAction`
    A `DataFrameAction` class that was programatically constructed from the
    input expression.

Definition at line 83 of file _evalColumnExpression.py.

87 ) -> Type[DataFrameAction]:
88 """Factory function for producing ConfigurableAction classes which are
89 realizations of arithmetic operations.
90
91 Parameters
92 ----------
93 className : `str`
94 The name of the class that will be produced
95 expr : `str`
96 An arithmetic expression that will be parsed to produce the output
97 ConfigurableAction. Individual variable names will be the name of
98 individual `ConfigActions` inside the expression (i.e. "x+y" will
99 produce an action with configAction.actions.x and
100 configAction.actions.y). Expression can contain arithmatic python
101 operators as well as; sin, cos, sinh, cosh, log (which is base 10).
102 exprDefaults : `Mapping` of `str` to `DataFrameAction` optional
103 A mapping of strings which correspond to the names in the expression to
104 values which are default `ConfigurableActions` to assign in the
105 expression. If no default for a action is supplied `SingleColumnAction`
106 is set as the default.
107 docstring : `str`
108 A string that is assigned as the resulting classes docstring
109
110 Returns
111 -------
112 action : `Type` of `DataFrameAction`
113 A `DataFrameAction` class that was programatically constructed from the
114 input expression.
115 """
116 # inspect is used because this is a factory function used to produce classes
117 # and it is desireable that the classes generated appear to be in the
118 # module of the calling frame, instead of something defined within the
119 # scope of this function call.
120 import inspect
121 new_module = inspect.stack()[1].frame.f_locals['__name__']
122 node = ast.parse(expr, mode='eval')
123
124 # gather the specified names
125 names: Set[str] = set()
126 for elm in ast.walk(node):
127 if isinstance(elm, ast.Name):
128 names.add(elm.id)
129
130 # remove the known Math names
131 names -= EXTRA_MATH.keys()
132
133 fields: Mapping[str, ConfigurableActionField] = {}
134 for name in sorted(names):
135 if exprDefaults is not None and (value := exprDefaults.get(name)) is not None:
136 kwargs = {"default": value}
137 else:
138 kwargs = {}
139 fields[name] = ConfigurableActionField(doc=f"expression action {name}", **kwargs)
140
141 # skip flake8 on N807 because this is a stand alone function, but it is
142 # intended to be patched in as a method on a dynamically generated class
143 def __call__(self, df: pd.DataFrame, **kwargs) -> pd.Series: # noqa: N807
144 values_map = {}
145 for name in fields:
146 values_map[name] = getattr(self, name)(df, **kwargs)
147
148 parser = ExpressionParser(**values_map)
149 return parser.visit(node.body)
150
151 # create the function to look up the columns for the dynamically created action
152 def columns(self) -> Iterable[str]:
153 for name in fields:
154 yield from getattr(self, name).columns
155
156 dct: MutableMapping[str, Any] = {"__call__": __call__, "columns": property(columns)}
157 if docstring is not None:
158 dct['__doc__'] = docstring
159 dct.update(**fields)
160 dct['__module__'] = new_module
161
162 return type(className, (DataFrameAction, ), dct)
daf::base::PropertySet * set
Definition fits.cc:931

Variable Documentation

◆ EXTRA_MATH

dict lsst.pipe.tasks.dataFrameActions._evalColumnExpression.EXTRA_MATH = {"cos": cos, "sin": sin, "cosh": cosh, "sinh": sinh, "log": log}

Definition at line 43 of file _evalColumnExpression.py.

◆ OPERATORS

dict lsst.pipe.tasks.dataFrameActions._evalColumnExpression.OPERATORS
Initial value:
1= {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
2 ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor,
3 ast.USub: op.neg}

Definition at line 39 of file _evalColumnExpression.py.