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

Cleanup in module xcube.core.varexpr #1040

Merged
merged 1 commit into from
Jul 15, 2024
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
58 changes: 27 additions & 31 deletions test/core/varexpr/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,33 @@


class VarExprContextTest(unittest.TestCase):
def test_locals(self):
def test_names(self):
ctx = VarExprContext(dataset)
# noinspection PyShadowingBuiltins
locals = ctx.locals()
self.assertIsInstance(locals.get("A"), ExprVar)
self.assertIsInstance(locals.get("B"), ExprVar)
self.assertIsInstance(locals.get("C"), ExprVar)

def test_globals(self):
# noinspection PyShadowingBuiltins
globals = VarExprContext.globals()

self.assertIsInstance(globals.get("nan"), float)
self.assertIsInstance(globals.get("inf"), float)
self.assertIsInstance(globals.get("e"), float)
self.assertIsInstance(globals.get("pi"), float)

self.assertTrue(callable(globals.get("where")))
self.assertTrue(callable(globals.get("hypot")))
self.assertTrue(callable(globals.get("sin")))
self.assertTrue(callable(globals.get("cos")))
self.assertTrue(callable(globals.get("tan")))
self.assertTrue(callable(globals.get("sqrt")))
self.assertTrue(callable(globals.get("fmin")))
self.assertTrue(callable(globals.get("fmax")))
self.assertTrue(callable(globals.get("minimum")))
self.assertTrue(callable(globals.get("maximum")))

self.assertNotIn("min", globals)
self.assertNotIn("max", globals)
self.assertNotIn("open", globals)
names = ctx.names()

self.assertIsInstance(names.get("A"), ExprVar)
self.assertIsInstance(names.get("B"), ExprVar)
self.assertIsInstance(names.get("C"), ExprVar)

self.assertIsInstance(names.get("nan"), float)
self.assertIsInstance(names.get("inf"), float)
self.assertIsInstance(names.get("e"), float)
self.assertIsInstance(names.get("pi"), float)

self.assertTrue(callable(names.get("where")))
self.assertTrue(callable(names.get("hypot")))
self.assertTrue(callable(names.get("sin")))
self.assertTrue(callable(names.get("cos")))
self.assertTrue(callable(names.get("tan")))
self.assertTrue(callable(names.get("sqrt")))
self.assertTrue(callable(names.get("fmin")))
self.assertTrue(callable(names.get("fmax")))
self.assertTrue(callable(names.get("minimum")))
self.assertTrue(callable(names.get("maximum")))

self.assertNotIn("min", names)
self.assertNotIn("max", names)
self.assertNotIn("open", names)

def test_capabilities(self):
c_list = VarExprContext.get_constants()
Expand Down Expand Up @@ -241,7 +237,7 @@ def test_intentional_disintegration(self):
# This is intentional disintegration
ev = ExprVar(xr.DataArray(var_a_data, dims=("y", "x")))
ev.__dict__["_ExprVar__da"] = True
ctx._locals["my_ev"] = ev
ctx._names["my_ev"] = ev
with pytest.raises(
RuntimeError,
match="internal error: 'DataArray' object expected, but got type 'bool'",
Expand Down
64 changes: 31 additions & 33 deletions xcube/core/varexpr/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Permissions are hereby granted under the terms of the MIT License:
# https://opensource.org/licenses/MIT.

import ast
import inspect
from typing import Any, Callable, Union

Expand All @@ -12,7 +11,7 @@
from .error import VarExprError
from .exprvar import ExprVar
from .names import (
GLOBALS,
_GLOBAL_NAMES,
get_xarray_funcs,
get_numpy_ufuncs,
get_constants,
Expand All @@ -30,41 +29,25 @@ class VarExprContext:
"""

def __init__(self, dataset: xr.Dataset):
self._locals = dict({str(k): ExprVar(v) for k, v in dataset.data_vars.items()})

def locals(self) -> dict[str, Any]:
return dict(self._locals)
self._array_variables = dict(
{str(k): ExprVar(v) for k, v in dataset.data_vars.items()}
)
self._names = dict(_GLOBAL_NAMES)
self._names.update(self._array_variables)

@classmethod
def globals(cls) -> dict[str, Any]:
return dict(GLOBALS)
def names(self) -> dict[str, Any]:
return dict(self._names)

@classmethod
def _format_callable(cls, name: str, fn: Union[np.ufunc, Callable]):
if name == "where":
return "where(C,X,Y)"
if isinstance(fn, np.ufunc):
num_args = fn.nargs - 1
else:
signature = inspect.signature(fn)
num_args = len(signature.parameters.values())
if num_args == 0:
return f"{name}()"
elif num_args == 1:
return f"{name}(X)"
elif num_args == 2:
return f"{name}(X,Y)"
elif num_args == 3:
return f"{name}(X,Y,Z)"
else:
return f"{name}({','.join(map(lambda i: f'X{i + 1}', range(num_args)))})"
def get_array_variables(self) -> list[str]:
"""Get array functions."""
return sorted(self._array_variables.keys())

@classmethod
def get_array_functions(cls) -> list[str]:
"""Get array functions."""
# noinspection PyTypeChecker
return sorted(
cls._format_callable(name, fn)
_format_callable(name, fn)
for name, fn in dict(**get_xarray_funcs(), **get_numpy_ufuncs()).items()
)

Expand Down Expand Up @@ -148,7 +131,7 @@ def evaluate(self, var_expr: str) -> xr.DataArray:
A newly computed variable of type `xarray.DataArray`.
"""
try:
result = evaluate(var_expr, dict(**GLOBALS, **self._locals))
result = evaluate(var_expr, self._names)
except BaseException as e:
# Do not report the name 'ExprVar'
raise VarExprError(f"{e}".replace("ExprVar", "DataArray")) from e
Expand All @@ -171,6 +154,21 @@ def evaluate(self, var_expr: str) -> xr.DataArray:
return result


class VarExprValidator(ast.NodeTransformer):
def visit_Lambda(self, node):
raise VarExprError("lambda expressions are not supported")
def _format_callable(name: str, fn: Union[np.ufunc, Callable]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for putting this outside of the class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is was a private static method and now it is a private function, calling is shorter as it does not require context object.

if name == "where":
return "where(C,X,Y)"
if isinstance(fn, np.ufunc):
num_args = fn.nargs - 1
else:
signature = inspect.signature(fn)
num_args = len(signature.parameters.values())
if num_args == 0:
return f"{name}()"
elif num_args == 1:
return f"{name}(X)"
elif num_args == 2:
return f"{name}(X,Y)"
elif num_args == 3:
return f"{name}(X,Y,Z)"
else:
return f"{name}({','.join(map(lambda i: f'X{i + 1}', range(num_args)))})"
3 changes: 1 addition & 2 deletions xcube/core/varexpr/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ def get_xarray_funcs() -> dict[str, Callable]:


# noinspection PyProtectedMember
GLOBALS = {
_GLOBAL_NAMES = {
**get_constants(),
**{k: ExprVar._wrap_fn(fn) for k, fn in get_numpy_ufuncs().items()},
**{k: ExprVar._wrap_fn(fn) for k, fn in get_xarray_funcs().items()},
"__builtins__": {},
}

# noinspection PyProtectedMember
Expand Down
4 changes: 2 additions & 2 deletions xcube/core/varexpr/varexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
Names = Mapping[str, Any]
UnaryFunction = Callable[[Any], Any]
BinaryFunction = Callable[[Any, Any], Any]
ComparisonFunction = Callable[[Any, Any], bool]

_UNARY_OPS: Mapping[str, UnaryFunction] = {
"UAdd": lambda x: +x,
"USub": lambda x: -x,
"Invert": lambda x: ~x,
"Not": lambda x: not x,
}

_BINARY_OPS: Mapping[str, BinaryFunction] = {
"Add": lambda x, y: x + y,
"Sub": lambda x, y: x - y,
Expand All @@ -35,7 +35,7 @@
"BitOr": lambda x, y: x | y,
}

_COMPARISON_OPS: Mapping[str, ComparisonFunction] = {
_COMPARISON_OPS: Mapping[str, BinaryFunction] = {
"Eq": lambda x, y: x == y,
"NotEq": lambda x, y: x != y,
"Lt": lambda x, y: x < y,
Expand Down
Loading