Source code for ewoksxrdct.tests.test_read_aeroystepscan

import shutil
from pathlib import Path

import h5py
import numpy as np
import pytest
from ewoksutils.exceptions import TaskExecutionError

from ..tasks.read_aeroystepscan import ReadAeroystepscan
from .conftest import write_integration_output as _write_integration_output

BLISS_MASTER = Path(__file__).parent / "data" / "test_aeroystepscan.h5"

# BLISS_MASTER records an aeroystepscan with 36 rotation points (x_range=360 in
# steps of 10 from 0, recorded as step centers) at each of 3 translation points
# (y_npoints_arr=[3] from -0.1 in steps of 0.01).
N_POINTS = 108

# Parameters to use to generate test ai file
N_RADIAL = 1500
RADIAL_AXIS = np.linspace(0.003, 5.477, N_RADIAL, dtype=np.float32)
INTENSITY = np.arange(N_POINTS * N_RADIAL, dtype=np.float32).reshape(N_POINTS, N_RADIAL)


def _write_ai_file(tmp_path):
    """Write an azimuthal-integration output with one frame per scan point."""
    ai_file = tmp_path / "ai.h5"
    _write_integration_output(
        ai_file,
        radial_axis=RADIAL_AXIS,
        intensity=INTENSITY,
    )
    return ai_file


[docs] @pytest.fixture(scope="module") def ai_file(tmp_path_factory): """An azimuthal-integration output with one frame per scan point, built once and shared across tests since the tests only ever read it.""" return _write_ai_file(tmp_path_factory.mktemp("ai"))
def _copy_master_with_overrides(tmp_path, overrides): """Copy BLISS_MASTER, replacing or adding the datasets named by the HDF5 paths in `overrides`, to build a master that differs from the recorded scan.""" master = tmp_path / "master.h5" shutil.copy(BLISS_MASTER, master) with h5py.File(master, "r+") as f: for path, value in overrides.items(): if path in f: del f[path] f[path] = value return master def _make_task(ai_file, master=BLISS_MASTER, **input_overrides): inputs = { "bliss_master_path": str(master), "integration_output_path": str(ai_file), "scan_number": 1, "translation_motor_name": "hry", "rotation_motor_name": "hrrz", "integration_intensity_path": "/intensities", "integration_radial_axis_path": "/2th_deg", } inputs.update(input_overrides) return ReadAeroystepscan(inputs=inputs)
[docs] def test_run_reads_raw_data(ai_file): """Raw arrays pass through unreshaped and unsorted, and grid info is derived from fscan_parameters using the bin-centered formula for rotation and the plain inclusive-endpoint formula for translation.""" task = _make_task(ai_file) task.execute() with h5py.File(BLISS_MASTER, "r") as f: recorded_rotations = f["1.1/measurement/hrrz"][()] recorded_translations = f["1.1/measurement/hry"][()] np.testing.assert_array_equal(task.outputs.rotation_angles, recorded_rotations) np.testing.assert_array_equal( task.outputs.translation_values, recorded_translations ) assert task.outputs.rotation_angles.shape == (N_POINTS,) np.testing.assert_array_equal( task.outputs.integration_intensity_values, np.arange(N_POINTS * N_RADIAL, dtype=np.float32).reshape(N_POINTS, N_RADIAL), ) assert task.outputs.integration_radial_axis == "2th_deg" np.testing.assert_array_equal( task.outputs.integration_radial_axis_values, RADIAL_AXIS ) # Grid values derived from the fscan_parameters of BLISS_MASTER. assert task.outputs.rotation_grid_params == pytest.approx((5, 355, 36)) assert task.outputs.translation_grid_params == pytest.approx((-0.1, -0.08, 3))
[docs] def test_run_wrong_motor_name_raises(ai_file): """A motor name that doesn't exist in the bliss master file raises ValueError.""" task = _make_task(ai_file, rotation_motor_name="does_not_exist") with pytest.raises(TaskExecutionError, match="does_not_exist") as excinfo: task.execute() assert isinstance(excinfo.value.__cause__, ValueError)
[docs] def test_run_wrong_integration_path_raises(ai_file): """A radial axis or intensity HDF5 path that does not exist raises ValueError.""" task = _make_task(ai_file, integration_intensity_path="/does/not/exist") with pytest.raises(TaskExecutionError, match="/does/not/exist") as excinfo: task.execute() assert isinstance(excinfo.value.__cause__, ValueError)
[docs] def test_run_frame_count_mismatch_raises(tmp_path): """An integration output covering only part of the scan raises ValueError.""" partial_ai_file = tmp_path / "partial_ai.h5" _write_integration_output( partial_ai_file, radial_axis=RADIAL_AXIS, intensity=INTENSITY[: N_POINTS // 2], ) task = _make_task(partial_ai_file) with pytest.raises(TaskExecutionError, match="integrated frames") as excinfo: task.execute() assert isinstance(excinfo.value.__cause__, ValueError)
[docs] def test_run_point_count_mismatch_raises(tmp_path, ai_file): """The recorded array length not matching the point count derived from fscan_parameters raises a ValueError.""" # x_range=400 with x_step_size=10 gives N_rot=40, so 3*40=120 points are # expected against the 108 actually recorded. master = _copy_master_with_overrides( tmp_path, {"1.1/instrument/fscan_parameters/x_range": 400} ) task = _make_task(ai_file, master=master) with pytest.raises( TaskExecutionError, match="Expected 120 translation positions" ) as excinfo: task.execute() assert isinstance(excinfo.value.__cause__, ValueError)