|
1 | 1 | import numpy as np |
| 2 | +import pytest |
2 | 3 | import xarray as xr |
3 | 4 |
|
4 | | -from extra.utils import imshow2, hyperslicer2, fit_gaussian, gaussian |
| 5 | +from extra.utils import ( |
| 6 | + imshow2, hyperslicer2, fit_gaussian, gaussian, reorder_axes_to_shape, |
| 7 | +) |
5 | 8 |
|
6 | 9 |
|
7 | 10 | def test_imshow2(): |
@@ -43,3 +46,35 @@ def test_fit_gaussian(): |
43 | 46 | data = gaussian(np.arange(100), *params, norm=False) |
44 | 47 | popt = fit_gaussian(data, A_sign=-1) |
45 | 48 | assert np.allclose(popt, params) |
| 49 | + |
| 50 | + |
| 51 | +def test_reorder_axes_to_shape(): |
| 52 | + arr = np.zeros((512, 1024, 16), dtype=np.float32) # E.g. burst mode JUNGFRAU data |
| 53 | + res = reorder_axes_to_shape(arr, (16, 512, 1024)) |
| 54 | + assert res.shape == (16, 512, 1024) |
| 55 | + assert res.base is arr |
| 56 | + |
| 57 | + res = reorder_axes_to_shape(arr, (None, 512, 1024)) |
| 58 | + assert res.shape == (16, 512, 1024) |
| 59 | + assert res.base is arr |
| 60 | + |
| 61 | + with pytest.raises(ValueError): |
| 62 | + reorder_axes_to_shape(arr, (12, 512, 1024)) # Wrong dimension sizes |
| 63 | + |
| 64 | + with pytest.raises(ValueError): |
| 65 | + reorder_axes_to_shape(arr, (16, 1, 512, 1024)) # Wrong number of dimensions |
| 66 | + |
| 67 | + with pytest.raises(ValueError): |
| 68 | + reorder_axes_to_shape(arr[:, :512], (16, 512, 512)) # Ambiguous order |
| 69 | + |
| 70 | + with pytest.raises(ValueError): |
| 71 | + reorder_axes_to_shape(arr, (None, None, 1024)) # Only 1 None allowed |
| 72 | + |
| 73 | + with pytest.raises(ValueError): |
| 74 | + reorder_axes_to_shape(arr, (None, 256, 1024)) # Wildcard & wrong number |
| 75 | + |
| 76 | + # Check we've transposed, not reshaped |
| 77 | + arr = np.arange(15).reshape(3, 5) |
| 78 | + res = reorder_axes_to_shape(arr, (5, 3)) |
| 79 | + assert res.shape == (5, 3) |
| 80 | + np.testing.assert_array_equal(res[0], [0, 5, 10]) |
0 commit comments