|
| 1 | +import sys |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +sf = pytest.importorskip('surfa') |
| 8 | + |
| 9 | + |
| 10 | +def _non_native_float32(data): |
| 11 | + byteorder = '>' if sys.byteorder == 'little' else '<' |
| 12 | + return np.asarray(data, dtype=np.float32).astype(np.dtype(np.float32).newbyteorder(byteorder), copy=True) |
| 13 | + |
| 14 | + |
| 15 | +def _assert_machine_precision_equal(a, b): |
| 16 | + assert a.shape == b.shape |
| 17 | + assert a.dtype == b.dtype |
| 18 | + assert np.issubdtype(a.dtype, np.floating) |
| 19 | + |
| 20 | + scale = max( |
| 21 | + float(np.max(np.abs(a))), |
| 22 | + float(np.max(np.abs(b))), |
| 23 | + 1.0, |
| 24 | + ) |
| 25 | + atol = np.finfo(a.dtype).eps * scale |
| 26 | + assert np.allclose(a, b, rtol=0.0, atol=atol), f'max abs diff={np.max(np.abs(a - b))}, atol={atol}' |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize('method', ['linear', 'nearest']) |
| 30 | +def test_resize_non_native_endian_matches_native(method): |
| 31 | + rng = np.random.default_rng(123456) |
| 32 | + data_native = rng.normal(loc=0.1, scale=1.7, size=(9, 7, 5, 2)).astype(np.float32) |
| 33 | + data_non_native = _non_native_float32(data_native) |
| 34 | + |
| 35 | + vol_native = sf.Volume(data_native) |
| 36 | + vol_non_native = sf.Volume(data_non_native) |
| 37 | + |
| 38 | + assert vol_non_native.framed_data.dtype.byteorder not in ('=', '|') |
| 39 | + |
| 40 | + voxsize = np.array([1.3, 0.9, 1.7], dtype=np.float32) |
| 41 | + resized_native = vol_native.resize(voxsize, method=method) |
| 42 | + resized_non_native = vol_non_native.resize(voxsize, method=method) |
| 43 | + |
| 44 | + assert resized_native.framed_data.shape == resized_non_native.framed_data.shape |
| 45 | + assert resized_native.framed_data.dtype == resized_non_native.framed_data.dtype |
| 46 | + _assert_machine_precision_equal(resized_native.framed_data, resized_non_native.framed_data) |
0 commit comments