22from __future__
import annotations
24__all__ = [
"ApdbConfig",
"Apdb",
"ApdbInsertId",
"ApdbTableData"]
27from abc
import ABC, abstractmethod
29from dataclasses
import dataclass
30from typing
import Optional
31from uuid
import UUID, uuid4
35from felis.simple
import Table
39from .apdbSchema
import ApdbTables
43 """Return path name of a data file in sdm_schemas package."""
44 return os.path.join(
"${SDM_SCHEMAS_DIR}",
"yml", basename)
48 """Part of Apdb configuration common to all implementations."""
50 read_sources_months = Field[int](doc=
"Number of months of history to read from DiaSource", default=12)
51 read_forced_sources_months = Field[int](
52 doc=
"Number of months of history to read from DiaForcedSource", default=12
54 schema_file = Field[str](
55 doc=
"Location of (YAML) configuration file with standard schema", default=
_data_file_name(
"apdb.yaml")
57 schema_name = Field[str](doc=
"Name of the schema in YAML configuration file.", default=
"ApdbSchema")
58 extra_schema_file = Field[str](
59 doc=
"Location of (YAML) configuration file with extra schema, "
60 "definitions in this file are merged with the definitions in "
61 "'schema_file', extending or replacing parts of the schema.",
64 deprecated=
"This field is deprecated, its value is not used.",
66 use_insert_id = Field[bool](
68 "If True (default), make and fill additional tables used for getHistory methods. "
69 "Databases created with earlier versions of APDB may not have these tables, "
70 "and corresponding methods will not work for them."
77 """Abstract class for representing table data."""
81 """Return ordered sequence of column names in the table.
85 names : `list` [`str`]
88 raise NotImplementedError()
91 def rows(self) -> Iterable[tuple]:
92 """Return table rows, each row is a tuple of values.
96 rows : `iterable` [`tuple`]
99 raise NotImplementedError()
102@dataclass(frozen=True)
104 """Class used to identify single insert operation.
106 Instances of this class are used
to identify the units of transfer
from
107 APDB to PPDB. Usually single `ApdbInsertId` corresponds to a single call to
115 """Generate new unique insert identifier."""
120 """Abstract interface for APDB."""
122 ConfigClass = ApdbConfig
125 def tableDef(self, table: ApdbTables) -> Optional[Table]:
126 """Return table schema definition for a given table.
131 One of the known APDB tables.
135 tableSchema : `felis.simple.Table` or `
None`
136 Table schema description, `
None`
is returned
if table
is not
137 defined by this implementation.
139 raise NotImplementedError()
143 """Create or re-create whole database schema.
148 If True then drop all tables before creating new ones.
150 raise NotImplementedError()
154 """Returns catalog of DiaObject instances from a given region.
156 This method returns only the last version of each DiaObject. Some
157 records in a returned catalog may be outside the specified region, it
158 is up to a client to ignore those records
or cleanup the catalog before
164 Region to search
for DIAObjects.
168 catalog : `pandas.DataFrame`
169 Catalog containing DiaObject records
for a region that may be a
170 superset of the specified region.
172 raise NotImplementedError()
176 self, region: Region, object_ids: Optional[Iterable[int]], visit_time:
dafBase.DateTime
177 ) -> Optional[pandas.DataFrame]:
178 """Return catalog of DiaSource instances from a given region.
183 Region to search for DIASources.
184 object_ids : iterable [ `int` ], optional
185 List of DiaObject IDs to further constrain the set of returned
186 sources. If `
None` then returned sources are
not constrained. If
187 list
is empty then empty catalog
is returned
with a correct
190 Time of the current visit.
194 catalog : `pandas.DataFrame`,
or `
None`
195 Catalog containing DiaSource records. `
None`
is returned
if
196 ``read_sources_months`` configuration parameter
is set to 0.
200 This method returns DiaSource catalog
for a region
with additional
201 filtering based on DiaObject IDs. Only a subset of DiaSource history
202 is returned limited by ``read_sources_months`` config parameter, w.r.t.
203 ``visit_time``. If ``object_ids``
is empty then an empty catalog
is
204 always returned
with the correct schema (columns/types). If
205 ``object_ids``
is `
None` then no filtering
is performed
and some of the
206 returned records may be outside the specified region.
208 raise NotImplementedError()
212 self, region: Region, object_ids: Optional[Iterable[int]], visit_time:
dafBase.DateTime
213 ) -> Optional[pandas.DataFrame]:
214 """Return catalog of DiaForcedSource instances from a given region.
219 Region to search for DIASources.
220 object_ids : iterable [ `int` ], optional
221 List of DiaObject IDs to further constrain the set of returned
222 sources. If list
is empty then empty catalog
is returned
with a
223 correct schema. If `
None` then returned sources are
not
224 constrained. Some implementations may
not support latter case.
226 Time of the current visit.
230 catalog : `pandas.DataFrame`,
or `
None`
231 Catalog containing DiaSource records. `
None`
is returned
if
232 ``read_forced_sources_months`` configuration parameter
is set to 0.
237 May be raised by some implementations
if ``object_ids``
is `
None`.
241 This method returns DiaForcedSource catalog
for a region
with additional
242 filtering based on DiaObject IDs. Only a subset of DiaSource history
243 is returned limited by ``read_forced_sources_months`` config parameter,
244 w.r.t. ``visit_time``. If ``object_ids``
is empty then an empty catalog
245 is always returned
with the correct schema (columns/types). If
246 ``object_ids``
is `
None` then no filtering
is performed
and some of the
247 returned records may be outside the specified region.
249 raise NotImplementedError()
253 """Return collection of insert identifiers known to the database.
257 ids : `list` [`ApdbInsertId`] or `
None`
258 List of identifiers, they may be time-ordered
if database supports
259 ordering. `
None`
is returned
if database
is not configured to store
262 raise NotImplementedError()
266 """Remove insert identifiers from the database.
270 ids : `iterable` [`ApdbInsertId`]
271 Insert identifiers, can include items returned from `getInsertIds`.
275 This method causes Apdb to forget about specified identifiers. If there
276 are any auxiliary data associated
with the identifiers, it
is also
277 removed
from database (but data
in regular tables
is not removed).
278 This method should be called after successful transfer of data
from
279 APDB to PPDB to free space used by history.
281 raise NotImplementedError()
285 """Returns catalog of DiaObject instances from a given time period
286 including the history of each DiaObject.
290 ids : `iterable` [`ApdbInsertId`]
291 Insert identifiers, can include items returned from `getInsertIds`.
295 data : `ApdbTableData`
296 Catalog containing DiaObject records. In addition to all regular
297 columns it will contain ``insert_id`` column.
301 This part of API may
not be very stable
and can change before the
302 implementation finalizes.
304 raise NotImplementedError()
308 """Returns catalog of DiaSource instances from a given time period.
312 ids : `iterable` [`ApdbInsertId`]
313 Insert identifiers, can include items returned from `getInsertIds`.
317 data : `ApdbTableData`
318 Catalog containing DiaSource records. In addition to all regular
319 columns it will contain ``insert_id`` column.
323 This part of API may
not be very stable
and can change before the
324 implementation finalizes.
326 raise NotImplementedError()
330 """Returns catalog of DiaForcedSource instances from a given time
335 ids : `iterable` [`ApdbInsertId`]
336 Insert identifiers, can include items returned from `getInsertIds`.
340 data : `ApdbTableData`
341 Catalog containing DiaForcedSource records. In addition to all
342 regular columns it will contain ``insert_id`` column.
346 This part of API may
not be very stable
and can change before the
347 implementation finalizes.
349 raise NotImplementedError()
353 """Returns catalog of SSObject instances.
357 catalog : `pandas.DataFrame`
358 Catalog containing SSObject records, all existing records are
361 raise NotImplementedError()
366 visit_time: dafBase.DateTime,
367 objects: pandas.DataFrame,
368 sources: Optional[pandas.DataFrame] =
None,
369 forced_sources: Optional[pandas.DataFrame] =
None,
371 """Store all three types of catalogs in the database.
377 objects : `pandas.DataFrame`
378 Catalog with DiaObject records.
379 sources : `pandas.DataFrame`, optional
380 Catalog
with DiaSource records.
381 forced_sources : `pandas.DataFrame`, optional
382 Catalog
with DiaForcedSource records.
386 This methods takes DataFrame catalogs, their schema must be
387 compatible
with the schema of APDB table:
389 - column names must correspond to database table columns
390 - types
and units of the columns must match database definitions,
391 no unit conversion
is performed presently
392 - columns that have default values
in database schema can be
394 - this method knows how to fill interval-related columns of DiaObject
395 (validityStart, validityEnd) they do
not need to appear
in a
397 - source catalogs have ``diaObjectId`` column associating sources
400 raise NotImplementedError()
404 """Store or update SSObject catalog.
408 objects : `pandas.DataFrame`
409 Catalog with SSObject records.
413 If SSObjects
with matching IDs already exist
in the database, their
414 records will be updated
with the information
from provided records.
416 raise NotImplementedError()
420 """Associate DiaSources with SSObjects, dis-associating them
426 Maps DiaSource IDs to their new SSObject IDs.
431 Raised
if DiaSource ID does
not exist
in the database.
433 raise NotImplementedError()
437 """Implement daily activities like cleanup/vacuum.
439 What should be done during daily activities is determined by
440 specific implementation.
442 raise NotImplementedError()
446 """Return the number of DiaObjects that have only one DiaSource
447 associated with them.
449 Used
as part of ap_verify metrics.
454 Number of DiaObjects
with exactly one associated DiaSource.
458 This method can be very inefficient
or slow
in some implementations.
460 raise NotImplementedError()
464 """Make a `~lsst.pex.config.ConfigurableField` for Apdb.
469 Help text for the field.
Class for handling dates/times, including MJD, UTC, and TAI.
ConfigurableField makeField(cls, str doc)
None deleteInsertIds(self, Iterable[ApdbInsertId] ids)
Optional[pandas.DataFrame] getDiaSources(self, Region region, Optional[Iterable[int]] object_ids, dafBase.DateTime visit_time)
list[ApdbInsertId]|None getInsertIds(self)
None store(self, dafBase.DateTime visit_time, pandas.DataFrame objects, Optional[pandas.DataFrame] sources=None, Optional[pandas.DataFrame] forced_sources=None)
Optional[Table] tableDef(self, ApdbTables table)
pandas.DataFrame getSSObjects(self)
pandas.DataFrame getDiaObjects(self, Region region)
ApdbTableData getDiaSourcesHistory(self, Iterable[ApdbInsertId] ids)
Optional[pandas.DataFrame] getDiaForcedSources(self, Region region, Optional[Iterable[int]] object_ids, dafBase.DateTime visit_time)
ApdbTableData getDiaObjectsHistory(self, Iterable[ApdbInsertId] ids)
None makeSchema(self, bool drop=False)
None storeSSObjects(self, pandas.DataFrame objects)
int countUnassociatedObjects(self)
None reassignDiaSources(self, Mapping[int, int] idMap)
ApdbTableData getDiaForcedSourcesHistory(self, Iterable[ApdbInsertId] ids)
ApdbInsertId new_insert_id(cls)
Iterable[tuple] rows(self)
list[str] column_names(self)
Region is a minimal interface for 2-dimensional regions on the unit sphere.
str _data_file_name(str basename)