Skip to content

Commit

Permalink
TransformVolumeData.py: support instance methods as kernels
Browse files Browse the repository at this point in the history
Useful to look at mesh.extents() in ParaView
without having to write a kernel file.
  • Loading branch information
nilsvu committed Oct 31, 2024
1 parent 4d6f679 commit f1d973e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Visualization/Python/TransformVolumeData.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,14 @@ def parse_kernels(kernels, exec_files, map_input_names, interactive=False):
# A module path was specified. Import function from the module
kernel_module_path, kernel_function = kernel.rsplit(".", maxsplit=1)
kernel_module = importlib.import_module(kernel_module_path)
if ":" in kernel_function:
kernel_class, kernel_function = kernel_function.split(":")
kernel_class = getattr(kernel_module, kernel_class)
kernel_function = getattr(kernel_class, kernel_function)
else:
kernel_function = getattr(kernel_module, kernel_function)
yield Kernel(
getattr(kernel_module, kernel_function),
kernel_function,
output_name=None,
map_input_names=map_input_names,
interactive=interactive,
Expand Down Expand Up @@ -717,7 +723,7 @@ def parse_kernels(kernels, exec_files, map_input_names, interactive=False):
multiple=True,
help=(
"Python function to apply to the volume data. "
"Specify as 'path.to.module.function_name', where the "
"Specify as 'path.to.module:function_name', where the "
"module must be available to import. "
"Alternatively, specify just 'function_name' if the "
"function is defined in one of the '--exec' / '-e' "
Expand Down Expand Up @@ -811,16 +817,17 @@ def shift_magnitude(
'parse_kernel_arg' function for all supported argument types, and
'parse_kernel_output' for all supported return types.
The kernels can be loaded from any available Python module. Examples of
useful kernels:
The kernels can be loaded from any available Python module. Specify them as
'path.to.module:function_name' or 'path.to.module.class:function_name'. You
can also load a Python file that defines kernels with the '--exec' / '-e'
option and then just specify the kernel as 'function_name'.
Examples of useful kernels:
- Anything in 'spectre.PointwiseFunctions'
- 'spectre.NumericalAlgorithms.LinearOperators.relative_truncation_error'
- 'spectre.Spectral.Mesh3D:extents'
- 'spectre.NumericalAlgorithms.LinearOperators:relative_truncation_error'
and 'absolute_truncation_error'
You can also execute a Python file that defines kernels with the '--exec' /
'-e' option.
## Input and output dataset names
You will be prompted to specify dataset names for input and output tensors,
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Visualization/Python/Test_TransformVolumeData.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from spectre.Spectral import Mesh
from spectre.Visualization.TransformVolumeData import (
Kernel,
parse_kernels,
parse_pybind11_signatures,
snake_case_to_camel_case,
transform_volume_data,
Expand Down Expand Up @@ -226,6 +227,17 @@ def test_integrate(self):
# particularly precise
npt.assert_allclose(integrals["Sinusoid"], 64.0, rtol=1e-2)

def test_parse_kernels(self):
(mesh_kernel,) = list(
parse_kernels(
["spectre.Spectral.Mesh3D:number_of_grid_points"],
exec_files=[],
map_input_names={},
interactive=False,
)
)
self.assertEqual(mesh_kernel.callable, Mesh[3].number_of_grid_points)

def test_cli(self):
runner = CliRunner()
cli_flags = [
Expand Down

0 comments on commit f1d973e

Please sign in to comment.