Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0fba68d861+83433b07ee,g16d25e1f1b+23bc9e47ac,g1ec0fe41b4+3ea9d11450,g1fd858c14a+9be2b0f3b9,g2440f9efcc+8c5ae1fdc5,g35bb328faa+8c5ae1fdc5,g4a4af6cd76+d25431c27e,g4d2262a081+c74e83464e,g53246c7159+8c5ae1fdc5,g55585698de+1e04e59700,g56a49b3a55+92a7603e7a,g60b5630c4e+1e04e59700,g67b6fd64d1+3fc8cb0b9e,g78460c75b0+7e33a9eb6d,g786e29fd12+668abc6043,g8352419a5c+8c5ae1fdc5,g8852436030+60e38ee5ff,g89139ef638+3fc8cb0b9e,g94187f82dc+1e04e59700,g989de1cb63+3fc8cb0b9e,g9d31334357+1e04e59700,g9f33ca652e+0a83e03614,gabe3b4be73+8856018cbb,gabf8522325+977d9fabaf,gb1101e3267+8b4b9c8ed7,gb89ab40317+3fc8cb0b9e,gc0af124501+57ccba3ad1,gcf25f946ba+60e38ee5ff,gd6cbbdb0b4+1cc2750d2e,gd794735e4e+7be992507c,gdb1c4ca869+be65c9c1d7,gde0f65d7ad+c7f52e58fe,ge278dab8ac+6b863515ed,ge410e46f29+3fc8cb0b9e,gf35d7ec915+97dd712d81,gf5e32f922b+8c5ae1fdc5,gf618743f1b+747388abfa,gf67bdafdda+3fc8cb0b9e,w.2025.18
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
apdb.py
Go to the documentation of this file.
1# This file is part of dax_apdb.
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 COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22from __future__ import annotations
23
24__all__ = ["ApdbConfig", "Apdb"]
25
26from abc import ABC, abstractmethod
27from collections.abc import Iterable, Mapping
28from typing import TYPE_CHECKING
29
30import astropy.time
31import pandas
32from lsst.resources import ResourcePathExpression
33from lsst.sphgeom import Region
34
35from .apdbSchema import ApdbTables
36from .config import ApdbConfig
37from .factory import make_apdb
38from .schema_model import Table
39
40if TYPE_CHECKING:
41 from .apdbMetadata import ApdbMetadata
42
43
44class Apdb(ABC):
45 """Abstract interface for APDB."""
46
47 @classmethod
48 def from_config(cls, config: ApdbConfig) -> Apdb:
49 """Create Ppdb instance from configuration object.
50
51 Parameters
52 ----------
53 config : `ApdbConfig`
54 Configuration object, type of this object determines type of the
55 Apdb implementation.
56
57 Returns
58 -------
59 apdb : `apdb`
60 Instance of `Apdb` class.
61 """
62 return make_apdb(config)
63
64 @classmethod
65 def from_uri(cls, uri: ResourcePathExpression) -> Apdb:
66 """Make Apdb instance from a serialized configuration.
67
68 Parameters
69 ----------
70 uri : `~lsst.resources.ResourcePathExpression`
71 URI or local file path pointing to a file with serialized
72 configuration, or a string with a "label:" prefix. In the latter
73 case, the configuration will be looked up from an APDB index file
74 using the label name that follows the prefix. The APDB index file's
75 location is determined by the ``DAX_APDB_INDEX_URI`` environment
76 variable.
77
78 Returns
79 -------
80 apdb : `apdb`
81 Instance of `Apdb` class, the type of the returned instance is
82 determined by configuration.
83 """
84 config = ApdbConfig.from_uri(uri)
85 return make_apdb(config)
86
87 @abstractmethod
88 def tableDef(self, table: ApdbTables) -> Table | None:
89 """Return table schema definition for a given table.
90
91 Parameters
92 ----------
93 table : `ApdbTables`
94 One of the known APDB tables.
95
96 Returns
97 -------
98 tableSchema : `.schema_model.Table` or `None`
99 Table schema description, `None` is returned if table is not
100 defined by this implementation.
101 """
102 raise NotImplementedError()
103
104 @abstractmethod
105 def getDiaObjects(self, region: Region) -> pandas.DataFrame:
106 """Return catalog of DiaObject instances from a given region.
107
108 This method returns only the last version of each DiaObject,
109 and may return only the subset of the DiaObject columns needed
110 for AP association. Some
111 records in a returned catalog may be outside the specified region, it
112 is up to a client to ignore those records or cleanup the catalog before
113 futher use.
114
115 Parameters
116 ----------
117 region : `lsst.sphgeom.Region`
118 Region to search for DIAObjects.
119
120 Returns
121 -------
122 catalog : `pandas.DataFrame`
123 Catalog containing DiaObject records for a region that may be a
124 superset of the specified region.
125 """
126 raise NotImplementedError()
127
128 @abstractmethod
130 self, region: Region, object_ids: Iterable[int] | None, visit_time: astropy.time.Time
131 ) -> pandas.DataFrame | None:
132 """Return catalog of DiaSource instances from a given region.
133
134 Parameters
135 ----------
136 region : `lsst.sphgeom.Region`
137 Region to search for DIASources.
138 object_ids : iterable [ `int` ], optional
139 List of DiaObject IDs to further constrain the set of returned
140 sources. If `None` then returned sources are not constrained. If
141 list is empty then empty catalog is returned with a correct
142 schema.
143 visit_time : `astropy.time.Time`
144 Time of the current visit.
145
146 Returns
147 -------
148 catalog : `pandas.DataFrame`, or `None`
149 Catalog containing DiaSource records. `None` is returned if
150 ``read_sources_months`` configuration parameter is set to 0.
151
152 Notes
153 -----
154 This method returns DiaSource catalog for a region with additional
155 filtering based on DiaObject IDs. Only a subset of DiaSource history
156 is returned limited by ``read_sources_months`` config parameter, w.r.t.
157 ``visit_time``. If ``object_ids`` is empty then an empty catalog is
158 always returned with the correct schema (columns/types). If
159 ``object_ids`` is `None` then no filtering is performed and some of the
160 returned records may be outside the specified region.
161 """
162 raise NotImplementedError()
163
164 @abstractmethod
166 self, region: Region, object_ids: Iterable[int] | None, visit_time: astropy.time.Time
167 ) -> pandas.DataFrame | None:
168 """Return catalog of DiaForcedSource instances from a given region.
169
170 Parameters
171 ----------
172 region : `lsst.sphgeom.Region`
173 Region to search for DIASources.
174 object_ids : iterable [ `int` ], optional
175 List of DiaObject IDs to further constrain the set of returned
176 sources. If list is empty then empty catalog is returned with a
177 correct schema. If `None` then returned sources are not
178 constrained. Some implementations may not support latter case.
179 visit_time : `astropy.time.Time`
180 Time of the current visit.
181
182 Returns
183 -------
184 catalog : `pandas.DataFrame`, or `None`
185 Catalog containing DiaSource records. `None` is returned if
186 ``read_forced_sources_months`` configuration parameter is set to 0.
187
188 Raises
189 ------
190 NotImplementedError
191 May be raised by some implementations if ``object_ids`` is `None`.
192
193 Notes
194 -----
195 This method returns DiaForcedSource catalog for a region with
196 additional filtering based on DiaObject IDs. Only a subset of DiaSource
197 history is returned limited by ``read_forced_sources_months`` config
198 parameter, w.r.t. ``visit_time``. If ``object_ids`` is empty then an
199 empty catalog is always returned with the correct schema
200 (columns/types). If ``object_ids`` is `None` then no filtering is
201 performed and some of the returned records may be outside the specified
202 region.
203 """
204 raise NotImplementedError()
205
206 @abstractmethod
208 self,
209 visit: int,
210 detector: int,
211 region: Region,
212 visit_time: astropy.time.Time,
213 ) -> bool:
214 """Test whether any sources for a given visit-detector are present in
215 the APDB.
216
217 Parameters
218 ----------
219 visit, detector : `int`
220 The ID of the visit-detector to search for.
221 region : `lsst.sphgeom.Region`
222 Region corresponding to the visit/detector combination.
223 visit_time : `astropy.time.Time`
224 Visit time (as opposed to visit processing time). This can be any
225 timestamp in the visit timespan, e.g. its begin or end time.
226
227 Returns
228 -------
229 present : `bool`
230 `True` if at least one DiaSource or DiaForcedSource record
231 may exist for the specified observation, `False` otherwise.
232 """
233 raise NotImplementedError()
234
235 @abstractmethod
236 def getSSObjects(self) -> pandas.DataFrame:
237 """Return catalog of SSObject instances.
238
239 Returns
240 -------
241 catalog : `pandas.DataFrame`
242 Catalog containing SSObject records, all existing records are
243 returned.
244 """
245 raise NotImplementedError()
246
247 @abstractmethod
248 def store(
249 self,
250 visit_time: astropy.time.Time,
251 objects: pandas.DataFrame,
252 sources: pandas.DataFrame | None = None,
253 forced_sources: pandas.DataFrame | None = None,
254 ) -> None:
255 """Store all three types of catalogs in the database.
256
257 Parameters
258 ----------
259 visit_time : `astropy.time.Time`
260 Time of the visit.
261 objects : `pandas.DataFrame`
262 Catalog with DiaObject records.
263 sources : `pandas.DataFrame`, optional
264 Catalog with DiaSource records.
265 forced_sources : `pandas.DataFrame`, optional
266 Catalog with DiaForcedSource records.
267
268 Notes
269 -----
270 This methods takes DataFrame catalogs, their schema must be
271 compatible with the schema of APDB table:
272
273 - column names must correspond to database table columns
274 - types and units of the columns must match database definitions,
275 no unit conversion is performed presently
276 - columns that have default values in database schema can be
277 omitted from catalog
278 - this method knows how to fill interval-related columns of DiaObject
279 (validityStart, validityEnd) they do not need to appear in a
280 catalog
281 - source catalogs have ``diaObjectId`` column associating sources
282 with objects
283
284 This operation need not be atomic, but DiaSources and DiaForcedSources
285 will not be stored until all DiaObjects are stored.
286 """
287 raise NotImplementedError()
288
289 @abstractmethod
290 def storeSSObjects(self, objects: pandas.DataFrame) -> None:
291 """Store or update SSObject catalog.
292
293 Parameters
294 ----------
295 objects : `pandas.DataFrame`
296 Catalog with SSObject records.
297
298 Notes
299 -----
300 If SSObjects with matching IDs already exist in the database, their
301 records will be updated with the information from provided records.
302 """
303 raise NotImplementedError()
304
305 @abstractmethod
306 def reassignDiaSources(self, idMap: Mapping[int, int]) -> None:
307 """Associate DiaSources with SSObjects, dis-associating them
308 from DiaObjects.
309
310 Parameters
311 ----------
312 idMap : `Mapping`
313 Maps DiaSource IDs to their new SSObject IDs.
314
315 Raises
316 ------
317 ValueError
318 Raised if DiaSource ID does not exist in the database.
319 """
320 raise NotImplementedError()
321
322 @abstractmethod
323 def dailyJob(self) -> None:
324 """Implement daily activities like cleanup/vacuum.
325
326 What should be done during daily activities is determined by
327 specific implementation.
328 """
329 raise NotImplementedError()
330
331 @abstractmethod
332 def countUnassociatedObjects(self) -> int:
333 """Return the number of DiaObjects that have only one DiaSource
334 associated with them.
335
336 Used as part of ap_verify metrics.
337
338 Returns
339 -------
340 count : `int`
341 Number of DiaObjects with exactly one associated DiaSource.
342
343 Notes
344 -----
345 This method can be very inefficient or slow in some implementations.
346 """
347 raise NotImplementedError()
348
349 @property
350 @abstractmethod
351 def metadata(self) -> ApdbMetadata:
352 """Object controlling access to APDB metadata (`ApdbMetadata`)."""
353 raise NotImplementedError()
ApdbMetadata metadata(self)
Definition apdb.py:351
pandas.DataFrame getSSObjects(self)
Definition apdb.py:236
pandas.DataFrame getDiaObjects(self, Region region)
Definition apdb.py:105
pandas.DataFrame|None getDiaSources(self, Region region, Iterable[int]|None object_ids, astropy.time.Time visit_time)
Definition apdb.py:131
Apdb from_uri(cls, ResourcePathExpression uri)
Definition apdb.py:65
None dailyJob(self)
Definition apdb.py:323
Apdb from_config(cls, ApdbConfig config)
Definition apdb.py:48
None store(self, astropy.time.Time visit_time, pandas.DataFrame objects, pandas.DataFrame|None sources=None, pandas.DataFrame|None forced_sources=None)
Definition apdb.py:254
Table|None tableDef(self, ApdbTables table)
Definition apdb.py:88
None storeSSObjects(self, pandas.DataFrame objects)
Definition apdb.py:290
int countUnassociatedObjects(self)
Definition apdb.py:332
None reassignDiaSources(self, Mapping[int, int] idMap)
Definition apdb.py:306
pandas.DataFrame|None getDiaForcedSources(self, Region region, Iterable[int]|None object_ids, astropy.time.Time visit_time)
Definition apdb.py:167
bool containsVisitDetector(self, int visit, int detector, Region region, astropy.time.Time visit_time)
Definition apdb.py:213