|
| 1 | +"""Index-array bounds are validated against raw coordinates during loading.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | +from zarr_indexing import ConstantMap, IndexTransform, normalize_ndsel |
| 11 | +from zarr_indexing.messages import NdselError |
| 12 | +from zarr_indexing.output_map import output_index_map_from_json |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from zarr_indexing.json import IndexTransformJSON, OutputIndexMapJSON |
| 16 | + |
| 17 | + |
| 18 | +def load(values: Any, bounds: Any, entry: str, offset: int, stride: int) -> Any: |
| 19 | + output: OutputIndexMapJSON = {"index_array": values, "offset": offset, "stride": stride} |
| 20 | + if bounds is not None: |
| 21 | + output["index_array_bounds"] = bounds |
| 22 | + if entry == "output_map": |
| 23 | + return output_index_map_from_json(output) |
| 24 | + body: IndexTransformJSON = {"input_shape": list(np.asarray(values).shape), "output": [output]} |
| 25 | + return IndexTransform.from_json(body) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 29 | +@pytest.mark.parametrize(("offset", "stride"), [(0, 1), (10, -2), (7, 0)]) |
| 30 | +@pytest.mark.parametrize( |
| 31 | + ("values", "bounds"), |
| 32 | + [ |
| 33 | + ([-3, 0, 4], [-3, 4]), |
| 34 | + ([-3, 0, 4], ["-inf", 4]), |
| 35 | + ([-3, 0, 4], [-3, "+inf"]), |
| 36 | + ([-3, 0, 4], ["-inf", "+inf"]), |
| 37 | + ([-3, 0, 4], None), |
| 38 | + ([2], [2, 2]), |
| 39 | + ([], [2, 2]), |
| 40 | + ([], ["+inf", "+inf"]), |
| 41 | + ([], ["-inf", "-inf"]), |
| 42 | + ([[0, 2], [2, 0]], [0, 2]), |
| 43 | + ], |
| 44 | +) |
| 45 | +def test_valid_bounds_preserve_values_and_roundtrip( |
| 46 | + values: Any, bounds: Any, entry: str, offset: int, stride: int |
| 47 | +) -> None: |
| 48 | + model = np.asarray(values, dtype=np.intp) |
| 49 | + loaded = load(values, bounds, entry, offset, stride) |
| 50 | + expected = offset + stride * model |
| 51 | + if entry == "transform": |
| 52 | + points = np.array(list(np.ndindex(model.shape)), dtype=np.intp).reshape(-1, model.ndim) |
| 53 | + restored = IndexTransform.from_json(loaded.to_json()) |
| 54 | + np.testing.assert_array_equal(loaded.apply_many(points).reshape(model.shape), expected) |
| 55 | + np.testing.assert_array_equal(restored.apply_many(points).reshape(model.shape), expected) |
| 56 | + # Further selection and affine adjustment operate on validated coordinates. |
| 57 | + if model.ndim == 1 and model.size > 1: |
| 58 | + selected = loaded.oindex[[model.size - 1, 0]].translate((3,)) |
| 59 | + np.testing.assert_array_equal( |
| 60 | + selected.apply_many([[0], [1]]).ravel(), expected[[-1, 0]] + 3 |
| 61 | + ) |
| 62 | + else: |
| 63 | + restored = output_index_map_from_json(loaded.to_json()) |
| 64 | + if isinstance(restored, ConstantMap): |
| 65 | + np.testing.assert_array_equal(np.full(model.shape, restored.offset), expected) |
| 66 | + else: |
| 67 | + assert restored == loaded |
| 68 | + np.testing.assert_array_equal(loaded.offset + loaded.stride * loaded.index_array, expected) |
| 69 | + if bounds is not None: |
| 70 | + canonical = normalize_ndsel( |
| 71 | + { |
| 72 | + "kind": "transform", |
| 73 | + "input_shape": list(model.shape), |
| 74 | + "output": [{"index_array": values, "index_array_bounds": bounds}], |
| 75 | + } |
| 76 | + ) |
| 77 | + assert canonical["output"][0]["index_array_bounds"] == bounds |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 81 | +@pytest.mark.parametrize(("offset", "stride"), [(0, 1), (10, -2), (7, 0)]) |
| 82 | +@pytest.mark.parametrize( |
| 83 | + ("values", "bounds"), |
| 84 | + [ |
| 85 | + ([-1, 0], [0, 2]), |
| 86 | + ([0, 3], [0, 2]), |
| 87 | + ([3], [2, 2]), |
| 88 | + ([-1], [0, "+inf"]), |
| 89 | + ([3], ["-inf", 2]), |
| 90 | + ([0], ["+inf", "+inf"]), |
| 91 | + ([0], ["-inf", "-inf"]), |
| 92 | + ([[0, 1], [1, 3]], [0, 2]), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_out_of_bounds_raw_values_are_rejected( |
| 96 | + values: Any, bounds: Any, entry: str, offset: int, stride: int |
| 97 | +) -> None: |
| 98 | + with pytest.raises(NdselError, match="index_array_bounds") as exc: |
| 99 | + load(values, bounds, entry, offset, stride) |
| 100 | + assert exc.value.reason == "invalid_json" |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 104 | +@pytest.mark.parametrize("values", [[], [1]]) |
| 105 | +@pytest.mark.parametrize("bounds", [[], [0], [0, 1, 2], [False, 2], [0.5, 2], ["bad", 2]]) |
| 106 | +def test_malformed_bounds_are_rejected(values: Any, bounds: Any, entry: str) -> None: |
| 107 | + with pytest.raises(NdselError) as exc: |
| 108 | + load(values, bounds, entry, 0, 1) |
| 109 | + assert exc.value.reason == "invalid_json" |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 113 | +@pytest.mark.parametrize("values", [[], [1]]) |
| 114 | +def test_reversed_bounds_are_rejected(values: Any, entry: str) -> None: |
| 115 | + with pytest.raises(NdselError) as exc: |
| 116 | + load(values, [2, 0], entry, 0, 1) |
| 117 | + assert exc.value.reason == "bounds_out_of_order" |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize("entry", ["transform", "output_map"]) |
| 121 | +@pytest.mark.parametrize("value", [int(np.iinfo(np.intp).min), int(np.iinfo(np.intp).max)]) |
| 122 | +def test_integer_extreme_bounds_are_exact(entry: str, value: int) -> None: |
| 123 | + result = load([value], [value, value], entry, 0, 1) |
| 124 | + if entry == "transform": |
| 125 | + assert result.apply((0,)) == (value,) |
| 126 | + else: |
| 127 | + assert int(result.index_array[0]) == value |
| 128 | + bounds = [value + 1, "+inf"] if value < 0 else ["-inf", value - 1] |
| 129 | + with pytest.raises(NdselError, match="index_array_bounds"): |
| 130 | + load([value], bounds, entry, 0, 1) |
0 commit comments