Coverage for python/astro_metadata_translator/observationGroup.py: 29%
74 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-06 03:48 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-04-06 03:48 -0700
1# This file is part of astro_metadata_translator.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the LICENSE 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.
12"""Represent a collection of translated headers."""
14from __future__ import annotations
16__all__ = ("ObservationGroup",)
18import logging
19from collections.abc import Callable, Iterable, Iterator, MutableMapping, MutableSequence
20from typing import TYPE_CHECKING, Any
22from .observationInfo import ObservationInfo
24if TYPE_CHECKING:
25 from .translator import MetadataTranslator
27log = logging.getLogger(__name__)
30class ObservationGroup(MutableSequence):
31 """A collection of `ObservationInfo` headers.
33 Parameters
34 ----------
35 members : iterable of `ObservationInfo` or `dict`-like
36 `ObservationInfo` to seed the group membership. If `dict`-like
37 values are used they will be passed to the `ObservationInfo`
38 constructor.
39 translator_class : `MetadataTranslator`-class, optional
40 If any of the members is not an `ObservationInfo`, translator class
41 to pass to the `ObservationInfo` constructor. If `None` the
42 translation class will be determined automatically.
43 pedantic : `bool`, optional
44 If any of the members is not an `ObservationInfo`, passed to the
45 `ObservationInfo` constructor to control whether
46 a failed translation is fatal or not. `None` indicates that the
47 `ObservationInfo` constructor default should be used.
48 """
50 def __init__(
51 self,
52 members: Iterable[ObservationInfo | MutableMapping[str, Any]],
53 translator_class: type[MetadataTranslator] | None = None,
54 pedantic: bool | None = None,
55 ) -> None:
56 self._members = [
57 self._coerce_value(m, translator_class=translator_class, pedantic=pedantic) for m in members
58 ]
60 # Cache of members in time order
61 self._sorted: list[ObservationInfo] | None = None
63 def __len__(self) -> int:
64 return len(self._members)
66 def __delitem__(self, index: int) -> None: # type: ignore
67 del self._members[index]
68 self._sorted = None
70 def __getitem__(self, index: int) -> ObservationInfo: # type: ignore
71 return self._members[index]
73 def __str__(self) -> str:
74 results = []
75 for obs_info in self._members:
76 results.append(f"({obs_info.instrument}, {obs_info.datetime_begin})")
77 return "[" + ", ".join(results) + "]"
79 def _coerce_value(
80 self,
81 value: ObservationInfo | MutableMapping[str, Any],
82 translator_class: type[MetadataTranslator] | None = None,
83 pedantic: bool | None = None,
84 ) -> ObservationInfo:
85 """Given a value, ensure it is an `ObservationInfo`.
87 Parameters
88 ----------
89 value : `ObservationInfo` or `dict`-like
90 Either an `ObservationInfo` or something that can be passed to
91 an `ObservationInfo` constructor.
92 translator_class : `MetadataTranslator`-class, optional
93 If value is not an `ObservationInfo`, translator class to pass to
94 the `ObservationInfo` constructor. If `None` the
95 translation class will be determined automatically.
96 pedantic : `bool`, optional
97 If value is not an `ObservationInfo`, passed to the
98 `ObservationInfo` constructor to control whether
99 a failed translation is fatal or not. `None` indicates that the
100 `ObservationInfo` constructor default should be used.
102 Raises
103 ------
104 ValueError
105 Raised if supplied value is not an `ObservationInfo` and can
106 not be turned into one.
107 """
108 if value is None:
109 raise ValueError("An ObservationGroup cannot contain 'None'")
111 if not isinstance(value, ObservationInfo):
112 try:
113 kwargs: dict[str, Any] = {"translator_class": translator_class}
114 if pedantic is not None:
115 kwargs["pedantic"] = pedantic
116 value = ObservationInfo(value, **kwargs)
117 except Exception as e:
118 raise ValueError("Could not convert value to ObservationInfo") from e
120 return value
122 def __iter__(self) -> Iterator[ObservationInfo]:
123 return iter(self._members)
125 def __eq__(self, other: Any) -> bool:
126 """Check equality with another group.
128 Compare equal if all the members are equal in the same order.
130 Parameters
131 ----------
132 other : `typing.Any`
133 Thing to compare with current group.
134 """
135 if not isinstance(other, ObservationGroup):
136 return NotImplemented
138 for info1, info2 in zip(self, other):
139 if info1 != info2:
140 return False
141 return True
143 def __setitem__( # type: ignore
144 self, index: int, value: ObservationInfo | MutableMapping[str, Any]
145 ) -> None:
146 """Store item in group.
148 Parameters
149 ----------
150 index : `int`
151 Index to use to store the item.
152 value : `ObservationInfo` or `~collections.abc.MutableMapping`
153 Information to store in group. Item must be an `ObservationInfo`
154 or something that can be passed to an `ObservationInfo`
155 constructor.
156 """
157 value = self._coerce_value(value)
158 self._members[index] = value
159 self._sorted = None
161 def insert(self, index: int, value: ObservationInfo | MutableMapping[str, Any]) -> None:
162 value = self._coerce_value(value)
163 self._members.insert(index, value)
164 self._sorted = None
166 def reverse(self) -> None:
167 self._members.reverse()
169 def sort(self, key: Callable | None = None, reverse: bool = False) -> None:
170 self._members.sort(key=key, reverse=reverse)
171 if key is None and not reverse and self._sorted is None:
172 # Store sorted order in cache
173 self._sorted = self._members.copy()
175 def extremes(self) -> tuple[ObservationInfo, ObservationInfo]:
176 """Return the oldest observation in the group and the newest.
178 If there is only one member of the group, the newest and oldest
179 can be the same observation.
181 Returns
182 -------
183 oldest : `ObservationInfo`
184 Oldest observation.
185 newest : `ObservationInfo`
186 Newest observation.
187 """
188 if self._sorted is None:
189 self._sorted = sorted(self._members)
190 return self._sorted[0], self._sorted[-1]
192 def newest(self) -> ObservationInfo:
193 """Return the newest observation in the group.
195 Returns
196 -------
197 newest : `ObservationInfo`
198 The newest `ObservationInfo` in the `ObservationGroup`.
199 """
200 return self.extremes()[1]
202 def oldest(self) -> ObservationInfo:
203 """Return the oldest observation in the group.
205 Returns
206 -------
207 oldest : `ObservationInfo`
208 The oldest `ObservationInfo` in the `ObservationGroup`.
209 """
210 return self.extremes()[0]
212 def property_values(self, property: str) -> set[Any]:
213 """Return a set of values associated with the specified property.
215 Parameters
216 ----------
217 property : `str`
218 Property of an `ObservationInfo`.
220 Returns
221 -------
222 values : `set`
223 All the distinct values for that property within this group.
224 """
225 return {getattr(obs_info, property) for obs_info in self}
227 def to_simple(self) -> list[MutableMapping[str, Any]]:
228 """Convert the group to simplified form.
230 Returns
231 -------
232 simple : `list` of `dict`
233 Simple form is a list containing the simplified dict form of
234 each `ObservationInfo`.
235 """
236 return [obsinfo.to_simple() for obsinfo in self]
238 @classmethod
239 def from_simple(cls, simple: list[dict[str, Any]]) -> ObservationGroup:
240 """Convert simplified form back to `ObservationGroup`.
242 Parameters
243 ----------
244 simple : `list` of `dict`
245 Object returned by `to_simple`.
247 Returns
248 -------
249 group : `ObservationGroup`
250 Reconstructed group.
251 """
252 return cls(ObservationInfo.from_simple(o) for o in simple)