Skip to content

Commit

Permalink
Rename ApplyPointwise -> TransformVolumeData
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Feb 8, 2023
1 parent c3f056e commit 96d25ed
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Visualization/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ spectre_python_add_module(
Visualization
PYTHON_FILES
__init__.py
ApplyPointwise.py
GenerateXdmf.py
InterpolateToCoords.py
InterpolateToMesh.py
Expand All @@ -16,4 +15,5 @@ spectre_python_add_module(
plots.mplstyle
ReadH5.py
Render1D.py
TransformVolumeData.py
)
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def one_index_constraint(
f"Unknown argument type '{arg.annotation}' for argument "
f"'{arg.name}'. It must be either a Tensor or one of "
"the supported structural types listed in "
"'ApplyPointwise.parse_kernel_arg'.")
"'TransformVolumeData.parse_kernel_arg'.")
return TensorArg(tensor_type=arg.annotation,
dataset_name=map_input_names.get(
arg.name, snake_case_to_camel_case(arg.name)))
Expand All @@ -232,7 +232,7 @@ def __init__(self,
map_input_names: Dict[str, str] = {},
output_name: Optional[str] = None,
elementwise: Optional[bool] = None):
"""Specifies input and output tensor names for a pointwise function
"""Transforms volume data with a Python function
Arguments:
callable: A Python function that takes and returns tensors
Expand Down Expand Up @@ -277,7 +277,7 @@ def __init__(self,
raise ValueError(
f"The function '{callable.__name__}' has no overload "
"with supported arguments. See "
"'ApplyPointwise.parse_kernel_arg' for a list of "
"'TransformVolumeData.parse_kernel_arg' for a list of "
"supported arguments. The overloads are: " +
rich.pretty.pretty_repr(overloads))
# If any argument is not a Tensor then we have to call the kernel
Expand Down Expand Up @@ -309,19 +309,19 @@ def __call__(self, all_tensor_data: Dict[str, Tensor],
return output


def apply_pointwise(
def transform_volume_data(
volfiles: Union[spectre_h5.H5Vol, Iterable[spectre_h5.H5Vol]],
kernels: Sequence[Kernel],
integrate: bool = False) -> Union[None, Dict[str, Sequence[float]]]:
"""Apply pointwise functions to data in the 'volfiles'
"""Transforms data in the 'volfiles' with a sequence of 'kernels'
Arguments:
volfiles: Iterable of open H5 volume files, or a single H5 volume file.
Must be opened in writable mode, e.g. in mode "a". The transformed
data will be written back into these files. All observations in these
files will be transformed.
kernels: List of pointwise transformations to apply to the volume data
in the form of 'Kernel' objects.
kernels: List of transformations to apply to the volume data in the form
of 'Kernel' objects.
integrate: Compute the volume integral over the kernels instead of
writing them back into the volume files. The integral is computed in
inertial coordinates for every tensor component of all kernels and over
Expand Down Expand Up @@ -519,12 +519,12 @@ def parse_kernels(kernels, exec_files, map_input_names):
@click.option("--output-subfile",
help=("Subfile name in the '--output' / '-o' file, if it is "
"an H5 file."))
def apply_pointwise_command(h5files, subfile_name, kernels, exec_files,
map_input_names, integrate, output, output_subfile,
**kwargs):
"""Apply pointwise functions to volume data
def transform_volume_data_command(h5files, subfile_name, kernels, exec_files,
map_input_names, integrate, output,
output_subfile, **kwargs):
"""Transform volume data with Python functions
Run pointwise functions (kernels) over all volume data in the 'H5FILES' and
Run Python functions (kernels) over all volume data in the 'H5FILES' and
write the output data back into the same files. You can use any Python
function as kernel that takes tensors as input arguments and returns a
tensor (from 'spectre.DataStructures.Tensor'). The function must be
Expand Down Expand Up @@ -607,10 +607,10 @@ def shift_magnitude(
task_id = progress.add_task("Applying to files")
volfiles_progress = progress.track(volfiles, task_id=task_id)
with progress:
integrals = apply_pointwise(volfiles_progress,
kernels=kernels,
integrate=integrate,
**kwargs)
integrals = transform_volume_data(volfiles_progress,
kernels=kernels,
integrate=integrate,
**kwargs)
progress.update(task_id, completed=len(volfiles))

# Write integrals to output file or print to terminal
Expand Down Expand Up @@ -644,4 +644,4 @@ def shift_magnitude(


if __name__ == "__main__":
apply_pointwise_command(help_option_names=["-h", "--help"])
transform_volume_data_command(help_option_names=["-h", "--help"])
12 changes: 6 additions & 6 deletions support/Python/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
class Cli(click.MultiCommand):
def list_commands(self, ctx):
return [
"apply-pointwise",
"clean-output",
"extract-dat",
"extract-input",
Expand All @@ -28,14 +27,11 @@ def list_commands(self, ctx):
"plot-power-monitors",
"render-1d",
"simplify-traces",
"transform-volume-data",
]

def get_command(self, ctx, name):
if name == "apply-pointwise":
from spectre.Visualization.ApplyPointwise import (
apply_pointwise_command)
return apply_pointwise_command
elif name == "clean-output":
if name == "clean-output":
from spectre.tools.CleanOutput import clean_output_command
return clean_output_command
elif name == "delete-subfiles":
Expand Down Expand Up @@ -74,6 +70,10 @@ def get_command(self, ctx, name):
from spectre.tools.CharmSimplifyTraces import (
simplify_traces_command)
return simplify_traces_command
elif name in ["transform-volume-data", "transform-vol"]:
from spectre.Visualization.TransformVolumeData import (
transform_volume_data_command)
return transform_volume_data_command
raise NotImplementedError(f"The command '{name}' is not implemented.")


Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Visualization/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

spectre_add_python_bindings_test(
"Unit.Visualization.Python.ApplyPointwise"
Test_ApplyPointwise.py
"unit;visualization;python"
None)

spectre_add_python_bindings_test(
"Unit.Visualization.Python.GenerateXdmf"
Test_GenerateXdmf.py
Expand Down Expand Up @@ -63,3 +57,9 @@ spectre_add_python_bindings_test(
Test_InterpolateToMesh.py
"unit;visualization;python"
None)

spectre_add_python_bindings_test(
"Unit.Visualization.Python.TransformVolumeData"
Test_TransformVolumeData.py
"unit;visualization;python"
None)
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

from spectre.Visualization.ApplyPointwise import (snake_case_to_camel_case,
parse_pybind11_signatures,
Kernel, apply_pointwise,
apply_pointwise_command)
from spectre.Visualization.TransformVolumeData import (
snake_case_to_camel_case, parse_pybind11_signatures, Kernel,
transform_volume_data, transform_volume_data_command)

import inspect
import numpy as np
Expand Down Expand Up @@ -57,10 +56,10 @@ def square_component(component: DataVector) -> Scalar[DataVector]:
return Scalar[DataVector]([component**2])


class TestApplyPointwise(unittest.TestCase):
class TestTransformVolumeData(unittest.TestCase):
def setUp(self):
self.test_dir = os.path.join(unit_test_build_path(),
'Visualization/ApplyPointwise')
'Visualization/TransformVolumeData')
self.h5_filename = os.path.join(self.test_dir, "Test.h5")
os.makedirs(self.test_dir, exist_ok=True)
shutil.copyfile(
Expand All @@ -78,7 +77,7 @@ def test_parse_pybind11_signatures(self):
def test_snake_case_to_camel_case(self):
self.assertEqual(snake_case_to_camel_case("hello_world"), "HelloWorld")

def test_apply_pointwise(self):
def test_transform_volume_data(self):
open_h5_files = [spectre_h5.H5File(self.h5_filename, "a")]
open_volfiles = [
h5file.get_vol("/element_data") for h5file in open_h5_files
Expand All @@ -95,7 +94,7 @@ def test_apply_pointwise(self):
map_input_names={"component": "InertialCoordinates_x"}),
]

apply_pointwise(volfiles=open_volfiles, kernels=kernels)
transform_volume_data(volfiles=open_volfiles, kernels=kernels)

obs_id = open_volfiles[0].list_observation_ids()[0]
result_psisq = open_volfiles[0].get_tensor_component(
Expand Down Expand Up @@ -136,9 +135,9 @@ def test_integrate(self):
Kernel(sinusoid),
]

integrals = apply_pointwise(volfiles=open_volfiles,
kernels=kernels,
integrate=True)
integrals = transform_volume_data(volfiles=open_volfiles,
kernels=kernels,
integrate=True)

npt.assert_allclose(integrals["Volume"], (2 * np.pi)**3)
# The domain has pretty low resolution so the integral is not
Expand All @@ -154,7 +153,7 @@ def test_cli(self):
"-e",
__file__,
]
result = runner.invoke(apply_pointwise_command,
result = runner.invoke(transform_volume_data_command,
cli_flags + [
"-k",
"psi_squared",
Expand Down Expand Up @@ -182,7 +181,7 @@ def test_cli(self):
npt.assert_allclose(np.array(result_radius), radius)

# Test integrals
result = runner.invoke(apply_pointwise_command,
result = runner.invoke(transform_volume_data_command,
cli_flags + [
"-k",
"sinusoid",
Expand All @@ -193,7 +192,7 @@ def test_cli(self):
self.assertIn("63.88", result.output)

output_filename = os.path.join(self.test_dir, "integrals.h5")
result = runner.invoke(apply_pointwise_command,
result = runner.invoke(transform_volume_data_command,
cli_flags + [
"-k",
"sinusoid",
Expand Down

0 comments on commit 96d25ed

Please sign in to comment.