LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
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.