diff --git a/src/Visualization/Python/TransformVolumeData.py b/src/Visualization/Python/TransformVolumeData.py index 6edfe63bfb9f..78879a763b30 100644 --- a/src/Visualization/Python/TransformVolumeData.py +++ b/src/Visualization/Python/TransformVolumeData.py @@ -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, @@ -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' " @@ -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, diff --git a/tests/Unit/Visualization/Python/Test_TransformVolumeData.py b/tests/Unit/Visualization/Python/Test_TransformVolumeData.py index 539782ea9977..180daaa727cd 100644 --- a/tests/Unit/Visualization/Python/Test_TransformVolumeData.py +++ b/tests/Unit/Visualization/Python/Test_TransformVolumeData.py @@ -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, @@ -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 = [