LSST Applications 29.0.1,g0fba68d861+132dd21e0a,g107a963962+1bb9f809a9,g1fd858c14a+005be21cae,g21d47ad084+8a07b29876,g325378336f+5d73323c8f,g330003fc43+40b4eaffc6,g35bb328faa+fcb1d3bbc8,g36ff55ed5b+9c28a42a87,g4e0f332c67+5fbd1e3e73,g53246c7159+fcb1d3bbc8,g60b5630c4e+9c28a42a87,g67b6fd64d1+a38b34ea13,g78460c75b0+2f9a1b4bcd,g786e29fd12+cf7ec2a62a,g7b71ed6315+fcb1d3bbc8,g86c591e316+6b2b2d0295,g8852436030+bf14db0e33,g89139ef638+a38b34ea13,g8b8da53e10+e3777245af,g9125e01d80+fcb1d3bbc8,g989de1cb63+a38b34ea13,g9f1445be69+9c28a42a87,g9f33ca652e+52c8f07962,ga9baa6287d+9c28a42a87,ga9e4eb89a6+9f84bd6575,gabe3b4be73+1e0a283bba,gb037a4e798+f3cbcd26c0,gb1101e3267+e7be8da0f8,gb58c049af0+f03b321e39,gb89ab40317+a38b34ea13,gcf25f946ba+bf14db0e33,gd6cbbdb0b4+bce7f7457e,gd9a9a58781+fcb1d3bbc8,gde0f65d7ad+53d424b1ae,ge278dab8ac+222406d50a,ge410e46f29+a38b34ea13,ge80e9994a3+664d6357dc,gf67bdafdda+a38b34ea13
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__ = ["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
207 def containsVisitDetector(self, visit: int, detector: int) -> bool:
208 """Test whether any sources for a given visit-detector are present in
209 the APDB.
210
211 Parameters
212 ----------
213 visit, detector : `int`
214 The ID of the visit-detector to search for.
215
216 Returns
217 -------
218 present : `bool`
219 `True` if at least one DiaSource or DiaForcedSource record
220 may exist for the specified observation, `False` otherwise.
221 """
222 raise NotImplementedError()
223
224 @abstractmethod
225 def getSSObjects(self) -> pandas.DataFrame:
226 """Return catalog of SSObject instances.
227
228 Returns
229 -------
230 catalog : `pandas.DataFrame`
231 Catalog containing SSObject records, all existing records are
232 returned.
233 """
234 raise NotImplementedError()
235
236 @abstractmethod
237 def store(
238 self,
239 visit_time: astropy.time.Time,
240 objects: pandas.DataFrame,
241 sources: pandas.DataFrame | None = None,
242 forced_sources: pandas.DataFrame | None = None,
243 ) -> None:
244 """Store all three types of catalogs in the database.
245
246 Parameters
247 ----------
248 visit_time : `astropy.time.Time`
249 Time of the visit.
250 objects : `pandas.DataFrame`
251 Catalog with DiaObject records.
252 sources : `pandas.DataFrame`, optional
253 Catalog with DiaSource records.
254 forced_sources : `pandas.DataFrame`, optional
255 Catalog with DiaForcedSource records.
256
257 Notes
258 -----
259 This methods takes DataFrame catalogs, their schema must be
260 compatible with the schema of APDB table:
261
262 - column names must correspond to database table columns
263 - types and units of the columns must match database definitions,
264 no unit conversion is performed presently
265 - columns that have default values in database schema can be
266 omitted from catalog
267 - this method knows how to fill interval-related columns of DiaObject
268 (validityStart, validityEnd) they do not need to appear in a
269 catalog
270 - source catalogs have ``diaObjectId`` column associating sources
271 with objects
272
273 This operation need not be atomic, but DiaSources and DiaForcedSources
274 will not be stored until all DiaObjects are stored.
275 """
276 raise NotImplementedError()
277
278 @abstractmethod
279 def storeSSObjects(self, objects: pandas.DataFrame) -> None:
280 """Store or update SSObject catalog.
281
282 Parameters
283 ----------
284 objects : `pandas.DataFrame`
285 Catalog with SSObject records.
286
287 Notes
288 -----
289 If SSObjects with matching IDs already exist in the database, their
290 records will be updated with the information from provided records.
291 """
292 raise NotImplementedError()
293
294 @abstractmethod
295 def reassignDiaSources(self, idMap: Mapping[int, int]) -> None:
296 """Associate DiaSources with SSObjects, dis-associating them
297 from DiaObjects.
298
299 Parameters
300 ----------
301 idMap : `Mapping`
302 Maps DiaSource IDs to their new SSObject IDs.
303
304 Raises
305 ------
306 ValueError
307 Raised if DiaSource ID does not exist in the database.
308 """
309 raise NotImplementedError()
310
311 @abstractmethod
312 def dailyJob(self) -> None:
313 """Implement daily activities like cleanup/vacuum.
314
315 What should be done during daily activities is determined by
316 specific implementation.
317 """
318 raise NotImplementedError()
319
320 @abstractmethod
321 def countUnassociatedObjects(self) -> int:
322 """Return the number of DiaObjects that have only one DiaSource
323 associated with them.
324
325 Used as part of ap_verify metrics.
326
327 Returns
328 -------
329 count : `int`
330 Number of DiaObjects with exactly one associated DiaSource.
331
332 Notes
333 -----
334 This method can be very inefficient or slow in some implementations.
335 """
336 raise NotImplementedError()
337
338 @property
339 @abstractmethod
340 def metadata(self) -> ApdbMetadata:
341 """Object controlling access to APDB metadata (`ApdbMetadata`)."""
342 raise NotImplementedError()
ApdbMetadata metadata(self)
Definition apdb.py:340
bool containsVisitDetector(self, int visit, int detector)
Definition apdb.py:207
pandas.DataFrame getSSObjects(self)
Definition apdb.py:225
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:312
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:243
Table|None tableDef(self, ApdbTables table)
Definition apdb.py:88
None storeSSObjects(self, pandas.DataFrame objects)
Definition apdb.py:279
int countUnassociatedObjects(self)
Definition apdb.py:321
None reassignDiaSources(self, Mapping[int, int] idMap)
Definition apdb.py:295
pandas.DataFrame|None getDiaForcedSources(self, Region region, Iterable[int]|None object_ids, astropy.time.Time visit_time)
Definition apdb.py:167