Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ExpressionEngine be no Component. Fixes #2065. #2068

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions ctapipe/core/expression_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import astropy.units as u # for use in selection functions
import numpy as np # for use in selection functions

from .component import Component
from .traits import List, Tuple, Unicode

# the following are what are allowed to be used
# in selection functions (passed to eval())
ALLOWED_GLOBALS = {"u": u, "np": np} # astropy units # numpy
Expand All @@ -19,21 +16,14 @@ class ExpressionError(TypeError):
"""Signal a problem with a user-defined selection criteria function"""


class ExpressionEngine(Component):
class ExpressionEngine:
"""
Compile expressions on init, evaluate on call.
"""

expressions = List(
Tuple(Unicode(), Unicode()),
help="List of 2-Tuples of Strings: ('name', 'expression').",
).tag(config=True)

def __init__(self, config=None, parent=None, **kwargs):
super().__init__(config=config, parent=parent, **kwargs)

def __init__(self, expressions):
self.compiled = []
for name, expression in self.expressions:
for name, expression in expressions:
try:
self.compiled.append(compile(expression, __name__, mode="eval"))
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion ctapipe/core/feature_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FeatureGenerator(Component):

def __init__(self, config=None, parent=None, **kwargs):
super().__init__(config=config, parent=parent, **kwargs)
self.engine = ExpressionEngine(parent=self, expressions=self.features)
self.engine = ExpressionEngine(expressions=self.features)
self._feature_names = [name for name, _ in self.features]

def __call__(self, table):
Expand Down
5 changes: 1 addition & 4 deletions ctapipe/core/qualityquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ def __init__(self, config=None, parent=None, **kwargs):
self.criteria_names = [n for (n, _) in self.quality_criteria]
self.expressions = [e for (_, e) in self.quality_criteria]

self.engine = ExpressionEngine(
parent=self,
expressions=self.quality_criteria,
)
self.engine = ExpressionEngine(self.quality_criteria)
for _, expr in self.quality_criteria:
if "lambda" in expr:
raise ValueError(
Expand Down