Test Tools
Baseline vs. result image comparison utilities used by the PhysioMotion4D pytest suite.
Module Reference
Test utilities for comparing images in pytest.
Provides TestTools for baseline vs results comparison with configurable tolerances. All image I/O uses ITK with .mha (compressed); 3D images are passed as itk.Image at the API level.
- physiomotion4d.test_tools.set_create_baseline_if_missing(value)[source]
Set whether to create baseline files when missing (used by pytest conftest).
- Return type:
- class physiomotion4d.test_tools.TestTools(class_name, results_dir=None, baselines_dir=None, *, log_level=20)[source]
Bases:
PhysioMotion4DBaseUtilities for pytest image comparison: baseline directory, result directory, and comparison with configurable tolerances. Inherits from PhysioMotion4DBase for logging. All image I/O uses ITK with compression where supported.
- __init__(class_name, results_dir=None, baselines_dir=None, *, log_level=20)[source]
Initialize test helpers.
- Parameters:
class_name (
str) – Identifier used for the default results/baselines subdirectory and the logger name. Callers that supplyresults_dirorbaselines_direxplicitly are responsible for includingclass_namein the path if they want a per-test subdirectory.results_dir (
Optional[Path]) – Exact directory for written result artifacts. Used as-is. Defaults to<repo>/tests/results/<class_name>whenNone.baselines_dir (
Optional[Path]) – Exact directory for baseline artifacts. Used as-is. Defaults to<repo>/tests/baselines/<class_name>whenNone.log_level (
int) – Logging level.
- static running_as_test()[source]
True when the script is run as a test (e.g. by pytest experiment tests).
Use this to choose fast/small parameters (fewer iterations, fewer files, etc.) so test runs complete in reasonable time. When False, use full parameters for interactive or production runs.
- Return type:
- Returns:
True if PHYSIOMOTION_RUNNING_AS_TEST is set to a truthy value (1, true, yes, case-insensitive); False otherwise.
- image_pass_fail_and_pixels_above_tolerance()[source]
Return (pass, value) for number of pixels above tolerance from the most recent compare_result_to_baseline_image call. pass is True if value <= max_pixels_above_tol that was used in that call.
- image_pass_fail_and_total_absolute_error()[source]
Return (pass, value) for total absolute error from the most recent compare_result_to_baseline_image call. pass is True if value <= total_absolute_error_tol that was used in that call.
- image_difference()[source]
Return the difference image (itk.Image) from the most recent compare_result_to_baseline_image call.
- Return type:
- transform_pass_fail_and_number_of_values_above_tolerance()[source]
Return (pass, value) for number of values above tolerance from the most recent compare_result_to_baseline_transform call. pass is True if value <= max_number_of_values_above_tol that was used in that call.
- transform_pass_fail_and_total_absolute_error()[source]
Return (pass, value) for total absolute error from the most recent compare_result_to_baseline_transform call. pass is True if value <= total_absolute_error_tol that was used in that call.
- transform_difference()[source]
Return the difference transform (itk.Transform) from the most recent compare_result_to_baseline_transform call.
- Return type:
- write_result_image(image, filename)[source]
Write the image to the configured result artifact directory.
- Return type:
- write_result_transform(transform, filename)[source]
Write the transform to the configured result artifact directory.
- Return type:
- compare_result_to_baseline_transform(filename, *, per_value_absolute_error_tol=0.0, max_number_of_values_above_tol=0, total_absolute_error_tol=0.0)[source]
Compare the transform to the baseline transform.
- Return type:
- compare_result_to_baseline_image(filename, *, per_pixel_absolute_error_tol=0.0, max_number_of_pixels_above_tol=0, total_absolute_error_tol=0.0)[source]
Load a 3D result image and a 3D baseline image (.mha), compare the full volumes voxel-by-voxel, save the difference image with “_diff” in the name on failure, and log pass/fail.
If the baseline file does not exist and –create-baselines was given, the 3D result is copied as the new baseline.
Returns True if comparison passed (pixels and total absolute error within tolerance).
- Parameters:
filename (
str) – File name (relative to class results/baselines dirs).per_pixel_absolute_error_tol (
float) – Per-voxel absolute error threshold.max_number_of_pixels_above_tol (
int) – Max allowed voxels exceeding threshold.total_absolute_error_tol (
float) – Max allowed sum of absolute differences.
- Return type:
- save_screenshot_mesh(mesh, filename, *, camera_position='iso', window_size=(800, 600), color='pink', opacity=0.9)[source]
Render a PyVista mesh off-screen and save a PNG.
Saves to the configured result artifact directory. On Linux headless environments, calls pv.start_xvfb() before rendering (no-op when a display is present).
- Parameters:
mesh (
Any) – PyVista PolyData or compatible mesh object.filename (
str) – Output PNG filename, relative to the result artifact dir.camera_position (
Literal['xy','xz','yz','yx','zx','zy','iso']) – PyVista camera preset, e.g.'iso','xy','xz'.window_size (
tuple[int,int]) – Off-screen render size(width, height)in pixels.color (
str) – Mesh color string accepted by PyVista.opacity (
float) – Mesh opacity in [0, 1].
- Return type:
- Returns:
Absolute Path to the saved PNG.
- save_screenshot_openusd(usd_file, filename, *, prim_path='/World', time_code=None)[source]
Render USD mesh geometry off-screen and save a PNG.
The scene is loaded through
USDTools.load_usd_as_vtk()into a PyVista mesh, rendered with a fixed isometric camera and fixed800 x 600window, and centered automatically by PyVista.- Parameters:
- Return type:
- Returns:
Absolute path to the saved PNG.
- save_screenshot_image_slice(image, filename, *, axis=0, slice_fraction=0.5, colormap='gray', vmin=None, vmax=None, overlay_mask=None, overlay_alpha=0.4)[source]
Extract one slice from an ITK image and save a PNG via matplotlib.
The numpy array from
itk.array_view_from_imagehas shape(Z, Y, X)(ITK stores X fastest; numpy reverses the axis order). Axis indices: - axis=0: axial (constant-Z plane) - axis=1: coronal (constant-Y plane) - axis=2: sagittal (constant-X plane)Saves to the configured result artifact directory.
- Parameters:
image (
Any) – 3-Ditk.Imagein RAS world space, axes X Y Z.filename (
str) – Output PNG filename, relative to the result artifact dir.axis (
int) – Numpy axis along which to slice (0=axial, 1=coronal, 2=sagittal).slice_fraction (
float) – Fractional position alongaxisin [0, 1].colormap (
str) – Matplotlib colormap name for the base image.vmin (
Optional[float]) – Lower clamp for display; None means data minimum.vmax (
Optional[float]) – Upper clamp for display; None means data maximum.overlay_mask (
Optional[Any]) – Optional binary ITK mask rendered as a semi-transparent overlay. Must have the same spatial extent asimage.overlay_alpha (
float) – Opacity of the mask overlay in [0, 1].
- Return type:
- Returns:
Absolute Path to the saved PNG.
Navigation