Coverage for python / lsst / images / _concrete_bounds.py: 23%
24 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 09:16 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-26 09:16 +0000
1# This file is part of lsst-images.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12from __future__ import annotations
14__all__ = ("SerializableBounds", "deserialize_bounds")
17from ._geom import Bounds, Box, NoOverlapError
19# This is expected to become a union of concrete Bounds types that we can
20# serialize via pydantic. Right now that's only Box.
21type SerializableBounds = Box
24def deserialize_bounds(serialized: SerializableBounds) -> Bounds:
25 """Convert a serialized bounds object into its in-memory form."""
26 match serialized:
27 case Box():
28 return serialized # type: ignore[return-value]
29 raise RuntimeError(f"Cannot deserialize {serialized!r}.")
32def _intersect_box(box: Box, other: Bounds) -> Bounds:
33 """Return the intersection between a Box and an arbitrary Bounds object.
35 When there is no overlap, `NoOverlapError` is raised.
36 """
37 match other:
38 case Box():
39 return _intersect_box_box(box, other)
40 case _:
41 raise TypeError(f"Unrecognized bounds type: {other}.")
44def _intersect_box_box(box: Box, other: Box) -> Box:
45 """Return the intersection of two boxes.
47 When there is no overlap between the boxes, `NoOverlapError` is raised.
48 """
49 intervals = []
50 for a, b in zip(box._intervals, other._intervals, strict=True):
51 try:
52 intervals.append(a.intersection(b))
53 except NoOverlapError as err:
54 err.add_note(f"In intersection between {a} and {b}.")
55 raise
56 return Box(*intervals)