Source code for ewoksxas.converters.tests.test_resample

import numpy as np
import pytest

from ewoksxas.converters.resample import Alignment, resample_to_common_grid


[docs] def test_identical_grids_are_returned_unchanged(): """Matched, identical grids need no resampling and stay byte-identical.""" x = np.arange(0.0, 2.0, 0.05) y_list = [np.sin(x), np.cos(x)] for mode in (Alignment.INTERPOLATION, Alignment.REBINNING): common_x, matrix = resample_to_common_grid([x, x.copy()], y_list, mode) np.testing.assert_array_equal(common_x, x) np.testing.assert_array_equal(matrix, np.asarray(y_list))
[docs] def test_interpolation_produces_single_overlap_grid_without_nan(): """Interpolation aligns onto the overlap range: one grid, no NaN.""" x_long = np.arange(0.0, 2.0001, 0.05) # 0 .. 2.0 x_short = np.arange(0.0, 1.5001, 0.05) # 0 .. 1.5 y_long, y_short = np.sin(x_long), np.cos(x_short) common_x, matrix = resample_to_common_grid( [x_long, x_short], [y_long, y_short], Alignment.INTERPOLATION ) assert matrix.shape == (2, common_x.size) assert not np.isnan(matrix).any() # Overlap upper bound is the shortest spectrum's max; no extrapolation. assert common_x[0] == pytest.approx(0.0) assert common_x[-1] <= 1.5 + 1e-9 # Interpolation on shared sample points is (near) identity. np.testing.assert_allclose(matrix[1], np.cos(common_x), atol=1e-12)
[docs] def test_rebinning_produces_union_grid_with_nan_gaps(): """Rebinning spans the union; uncovered bins become NaN (not dropped/zeroed).""" x_long = np.arange(0.0, 2.0001, 0.05) # 0 .. 2.0 x_short = np.arange(0.0, 1.5001, 0.05) # 0 .. 1.5 y_long, y_short = np.sin(x_long), np.cos(x_short) common_x, matrix = resample_to_common_grid( [x_long, x_short], [y_long, y_short], Alignment.REBINNING ) assert matrix.shape == (2, common_x.size) assert common_x[-1] >= 2.0 - 1e-9 # union reaches the longest spectrum # The long spectrum covers the whole union; the short one has NaN past 1.5. assert not np.isnan(matrix[0]).any() assert np.isnan(matrix[1]).any() # NaN only appears where the short spectrum had no data (x > 1.5). gap = common_x > 1.5 + 1e-9 assert np.isnan(matrix[1][gap]).all() assert not np.isnan(matrix[1][~gap]).any()
[docs] def test_string_mode_is_accepted(): """The mode may be passed as the enum value string.""" x = np.arange(0.0, 1.0, 0.05) _, matrix = resample_to_common_grid([x, x[:-2]], [x, x[:-2]], "rebinning") assert matrix.shape[0] == 2
[docs] def test_invalid_inputs_raise(): x = np.arange(5.0) with pytest.raises(ValueError, match="at least one spectrum"): resample_to_common_grid([], [], Alignment.INTERPOLATION) with pytest.raises(ValueError, match="same length"): resample_to_common_grid([x], [x, x], Alignment.INTERPOLATION) with pytest.raises(ValueError, match="not-a-mode"): resample_to_common_grid([x], [x], "not-a-mode")