LSST Applications g04e9c324dd+8c5ae1fdc5,g134cb467dc+b203dec576,g18429d2f64+358861cd2c,g199a45376c+0ba108daf9,g1fd858c14a+dd066899e3,g262e1987ae+ebfced1d55,g29ae962dfc+72fd90588e,g2cef7863aa+aef1011c0b,g35bb328faa+8c5ae1fdc5,g3fd5ace14f+b668f15bc5,g4595892280+3897dae354,g47891489e3+abcf9c3559,g4d44eb3520+fb4ddce128,g53246c7159+8c5ae1fdc5,g67b6fd64d1+abcf9c3559,g67fd3c3899+1f72b5a9f7,g74acd417e5+cb6b47f07b,g786e29fd12+668abc6043,g87389fa792+8856018cbb,g89139ef638+abcf9c3559,g8d7436a09f+bcf525d20c,g8ea07a8fe4+9f5ccc88ac,g90f42f885a+6054cc57f1,g97be763408+06f794da49,g9dd6db0277+1f72b5a9f7,ga681d05dcb+7e36ad54cd,gabf8522325+735880ea63,gac2eed3f23+abcf9c3559,gb89ab40317+abcf9c3559,gbf99507273+8c5ae1fdc5,gd8ff7fe66e+1f72b5a9f7,gdab6d2f7ff+cb6b47f07b,gdc713202bf+1f72b5a9f7,gdfd2d52018+8225f2b331,ge365c994fd+375fc21c71,ge410e46f29+abcf9c3559,geaed405ab2+562b3308c0,gf9a733ac38+8c5ae1fdc5,w.2025.35
LSST Data Management Base Package
Loading...
Searching...
No Matches
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__ = ["Apdb", "ApdbConfig"]
25
26from abc import ABC, abstractmethod
27from collections.abc import Iterable, Mapping
28from typing import TYPE_CHECKING
29
30import astropy.time
31import pandas
32
33from lsst.resources import ResourcePathExpression
34from lsst.sphgeom import Region
35
36from .apdbSchema import ApdbTables
37from .config import ApdbConfig
38from .factory import make_apdb
39from .schema_model import Table
40
41if TYPE_CHECKING:
42 from .apdbAdmin import ApdbAdmin
43 from .apdbMetadata import ApdbMetadata
44
45
46class Apdb(ABC):
47 """Abstract interface for APDB."""
48
49 @classmethod
50 def from_config(cls, config: ApdbConfig) -> Apdb:
51 """Create Ppdb instance from configuration object.
52
53 Parameters
54 ----------
55 config : `ApdbConfig`
56 Configuration object, type of this object determines type of the
57 Apdb implementation.
58
59 Returns
60 -------
61 apdb : `apdb`
62 Instance of `Apdb` class.
63 """
64 return make_apdb(config)
65
66 @classmethod
67 def from_uri(cls, uri: ResourcePathExpression) -> Apdb:
68 """Make Apdb instance from a serialized configuration.
69
70 Parameters
71 ----------
72 uri : `~lsst.resources.ResourcePathExpression`
73 URI or local file path pointing to a file with serialized
74 configuration, or a string with a "label:" prefix. In the latter
75 case, the configuration will be looked up from an APDB index file
76 using the label name that follows the prefix. The APDB index file's
77 location is determined by the ``DAX_APDB_INDEX_URI`` environment
78 variable.
79
80 Returns
81 -------
82 apdb : `apdb`
83 Instance of `Apdb` class, the type of the returned instance is
84 determined by configuration.
85 """
86 config = ApdbConfig.from_uri(uri)
87 return make_apdb(config)
88
89 @abstractmethod
90 def getConfig(self) -> ApdbConfig:
91 """Return APDB configuration for this instance, including any updates
92 that may be read from database.
93
94 Returns
95 -------
96 config : `ApdbConfig`
97 APDB configuration.
98 """
99 raise NotImplementedError()
100
101 @abstractmethod
102 def tableDef(self, table: ApdbTables) -> Table | None:
103 """Return table schema definition for a given table.
104
105 Parameters
106 ----------
107 table : `ApdbTables`
108 One of the known APDB tables.
109
110 Returns
111 -------
112 tableSchema : `.schema_model.Table` or `None`
113 Table schema description, `None` is returned if table is not
114 defined by this implementation.
115 """
116 raise NotImplementedError()
117
118 @abstractmethod
119 def getDiaObjects(self, region: Region) -> pandas.DataFrame:
120 """Return catalog of DiaObject instances from a given region.
121
122 This method returns only the last version of each DiaObject,
123 and may return only the subset of the DiaObject columns needed
124 for AP association. Some
125 records in a returned catalog may be outside the specified region, it
126 is up to a client to ignore those records or cleanup the catalog before
127 futher use.
128
129 Parameters
130 ----------
131 region : `lsst.sphgeom.Region`
132 Region to search for DIAObjects.
133
134 Returns
135 -------
136 catalog : `pandas.DataFrame`
137 Catalog containing DiaObject records for a region that may be a
138 superset of the specified region.
139 """
140 raise NotImplementedError()
141
142 @abstractmethod
144 self, region: Region, object_ids: Iterable[int] | None, visit_time: astropy.time.Time
145 ) -> pandas.DataFrame | None:
146 """Return catalog of DiaSource instances from a given region.
147
148 Parameters
149 ----------
150 region : `lsst.sphgeom.Region`
151 Region to search for DIASources.
152 object_ids : iterable [ `int` ], optional
153 List of DiaObject IDs to further constrain the set of returned
154 sources. If `None` then returned sources are not constrained. If
155 list is empty then empty catalog is returned with a correct
156 schema.
157 visit_time : `astropy.time.Time`
158 Time of the current visit.
159
160 Returns
161 -------
162 catalog : `pandas.DataFrame`, or `None`
163 Catalog containing DiaSource records. `None` is returned if
164 ``read_sources_months`` configuration parameter is set to 0.
165
166 Notes
167 -----
168 This method returns DiaSource catalog for a region with additional
169 filtering based on DiaObject IDs. Only a subset of DiaSource history
170 is returned limited by ``read_sources_months`` config parameter, w.r.t.
171 ``visit_time``. If ``object_ids`` is empty then an empty catalog is
172 always returned with the correct schema (columns/types). If
173 ``object_ids`` is `None` then no filtering is performed and some of the
174 returned records may be outside the specified region.
175 """
176 raise NotImplementedError()
177
178 @abstractmethod
180 self, region: Region, object_ids: Iterable[int] | None, visit_time: astropy.time.Time
181 ) -> pandas.DataFrame | None:
182 """Return catalog of DiaForcedSource instances from a given region.
183
184 Parameters
185 ----------
186 region : `lsst.sphgeom.Region`
187 Region to search for DIASources.
188 object_ids : iterable [ `int` ], optional
189 List of DiaObject IDs to further constrain the set of returned
190 sources. If list is empty then empty catalog is returned with a
191 correct schema. If `None` then returned sources are not
192 constrained. Some implementations may not support latter case.
193 visit_time : `astropy.time.Time`
194 Time of the current visit.
195
196 Returns
197 -------
198 catalog : `pandas.DataFrame`, or `None`
199 Catalog containing DiaSource records. `None` is returned if
200 ``read_forced_sources_months`` configuration parameter is set to 0.
201
202 Raises
203 ------
204 NotImplementedError
205 May be raised by some implementations if ``object_ids`` is `None`.
206
207 Notes
208 -----
209 This method returns DiaForcedSource catalog for a region with
210 additional filtering based on DiaObject IDs. Only a subset of DiaSource
211 history is returned limited by ``read_forced_sources_months`` config
212 parameter, w.r.t. ``visit_time``. If ``object_ids`` is empty then an
213 empty catalog is always returned with the correct schema
214 (columns/types). If ``object_ids`` is `None` then no filtering is
215 performed and some of the returned records may be outside the specified
216 region.
217 """
218 raise NotImplementedError()
219
220 @abstractmethod
222 self,
223 visit: int,
224 detector: int,
225 region: Region,
226 visit_time: astropy.time.Time,
227 ) -> bool:
228 """Test whether any sources for a given visit-detector are present in
229 the APDB.
230
231 Parameters
232 ----------
233 visit, detector : `int`
234 The ID of the visit-detector to search for.
235 region : `lsst.sphgeom.Region`
236 Region corresponding to the visit/detector combination.
237 visit_time : `astropy.time.Time`
238 Visit time (as opposed to visit processing time). This can be any
239 timestamp in the visit timespan, e.g. its begin or end time.
240
241 Returns
242 -------
243 present : `bool`
244 `True` if at least one DiaSource or DiaForcedSource record
245 may exist for the specified observation, `False` otherwise.
246 """
247 raise NotImplementedError()
248
249 @abstractmethod
250 def getSSObjects(self) -> pandas.DataFrame:
251 """Return catalog of SSObject instances.
252
253 Returns
254 -------
255 catalog : `pandas.DataFrame`
256 Catalog containing SSObject records, all existing records are
257 returned.
258 """
259 raise NotImplementedError()
260
261 @abstractmethod
262 def store(
263 self,
264 visit_time: astropy.time.Time,
265 objects: pandas.DataFrame,
266 sources: pandas.DataFrame | None = None,
267 forced_sources: pandas.DataFrame | None = None,
268 ) -> None:
269 """Store all three types of catalogs in the database.
270
271 Parameters
272 ----------
273 visit_time : `astropy.time.Time`
274 Time of the visit.
275 objects : `pandas.DataFrame`
276 Catalog with DiaObject records.
277 sources : `pandas.DataFrame`, optional
278 Catalog with DiaSource records.
279 forced_sources : `pandas.DataFrame`, optional
280 Catalog with DiaForcedSource records.
281
282 Notes
283 -----
284 This methods takes DataFrame catalogs, their schema must be
285 compatible with the schema of APDB table:
286
287 - column names must correspond to database table columns
288 - types and units of the columns must match database definitions,
289 no unit conversion is performed presently
290 - columns that have default values in database schema can be
291 omitted from catalog
292 - this method knows how to fill interval-related columns of DiaObject
293 (validityStart, validityEnd) they do not need to appear in a
294 catalog
295 - source catalogs have ``diaObjectId`` column associating sources
296 with objects
297
298 This operation need not be atomic, but DiaSources and DiaForcedSources
299 will not be stored until all DiaObjects are stored.
300 """
301 raise NotImplementedError()
302
303 @abstractmethod
304 def storeSSObjects(self, objects: pandas.DataFrame) -> None:
305 """Store or update SSObject catalog.
306
307 Parameters
308 ----------
309 objects : `pandas.DataFrame`
310 Catalog with SSObject records.
311
312 Notes
313 -----
314 If SSObjects with matching IDs already exist in the database, their
315 records will be updated with the information from provided records.
316 """
317 raise NotImplementedError()
318
319 @abstractmethod
320 def reassignDiaSources(self, idMap: Mapping[int, int]) -> None:
321 """Associate DiaSources with SSObjects, dis-associating them
322 from DiaObjects.
323
324 Parameters
325 ----------
326 idMap : `Mapping`
327 Maps DiaSource IDs to their new SSObject IDs.
328
329 Raises
330 ------
331 ValueError
332 Raised if DiaSource ID does not exist in the database.
333 """
334 raise NotImplementedError()
335
336 @abstractmethod
337 def dailyJob(self) -> None:
338 """Implement daily activities like cleanup/vacuum.
339
340 What should be done during daily activities is determined by
341 specific implementation.
342 """
343 raise NotImplementedError()
344
345 @abstractmethod
346 def countUnassociatedObjects(self) -> int:
347 """Return the number of DiaObjects that have only one DiaSource
348 associated with them.
349
350 Used as part of ap_verify metrics.
351
352 Returns
353 -------
354 count : `int`
355 Number of DiaObjects with exactly one associated DiaSource.
356
357 Notes
358 -----
359 This method can be very inefficient or slow in some implementations.
360 """
361 raise NotImplementedError()
362
363 @property
364 @abstractmethod
365 def metadata(self) -> ApdbMetadata:
366 """Object controlling access to APDB metadata (`ApdbMetadata`)."""
367 raise NotImplementedError()
368
369 @property
370 @abstractmethod
371 def admin(self) -> ApdbAdmin:
372 """Object providing adminitrative interface for APDB (`ApdbAdmin`)."""
373 raise NotImplementedError()
ApdbMetadata metadata(self)
Definition apdb.py:365
pandas.DataFrame getSSObjects(self)
Definition apdb.py:250
pandas.DataFrame getDiaObjects(self, Region region)
Definition apdb.py:119
pandas.DataFrame|None getDiaSources(self, Region region, Iterable[int]|None object_ids, astropy.time.Time visit_time)
Definition apdb.py:145
Apdb from_uri(cls, ResourcePathExpression uri)
Definition apdb.py:67
None dailyJob(self)
Definition apdb.py:337
Apdb from_config(cls, ApdbConfig config)
Definition apdb.py:50
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:268
Table|None tableDef(self, ApdbTables table)
Definition apdb.py:102
ApdbConfig getConfig(self)
Definition apdb.py:90
None storeSSObjects(self, pandas.DataFrame objects)
Definition apdb.py:304
int countUnassociatedObjects(self)
Definition apdb.py:346
None reassignDiaSources(self, Mapping[int, int] idMap)
Definition apdb.py:320
pandas.DataFrame|None getDiaForcedSources(self, Region region, Iterable[int]|None object_ids, astropy.time.Time visit_time)
Definition apdb.py:181
bool containsVisitDetector(self, int visit, int detector, Region region, astropy.time.Time visit_time)
Definition apdb.py:227
ApdbAdmin admin(self)
Definition apdb.py:371