LSST Applications g00274db5b6+edbf708997,g00d0e8bbd7+edbf708997,g199a45376c+5137f08352,g1fd858c14a+1d4b6db739,g262e1987ae+f4d9505c4f,g29ae962dfc+7156fb1a53,g2cef7863aa+73c82f25e4,g35bb328faa+edbf708997,g3e17d7035e+5b3adc59f5,g3fd5ace14f+852fa6fbcb,g47891489e3+6dc8069a4c,g53246c7159+edbf708997,g64539dfbff+9f17e571f4,g67b6fd64d1+6dc8069a4c,g74acd417e5+ae494d68d9,g786e29fd12+af89c03590,g7ae74a0b1c+a25e60b391,g7aefaa3e3d+536efcc10a,g7cc15d900a+d121454f8d,g87389fa792+a4172ec7da,g89139ef638+6dc8069a4c,g8d7436a09f+28c28d8d6d,g8ea07a8fe4+db21c37724,g92c671f44c+9f17e571f4,g98df359435+b2e6376b13,g99af87f6a8+b0f4ad7b8d,gac66b60396+966efe6077,gb88ae4c679+7dec8f19df,gbaa8f7a6c5+38b34f4976,gbf99507273+edbf708997,gc24b5d6ed1+9f17e571f4,gca7fc764a6+6dc8069a4c,gcc769fe2a4+97d0256649,gd7ef33dd92+6dc8069a4c,gdab6d2f7ff+ae494d68d9,gdbb4c4dda9+9f17e571f4,ge410e46f29+6dc8069a4c,geaed405ab2+e194be0d2b,w.2025.47
LSST Data Management Base Package
Loading...
Searching...
No Matches
lsst.dax.apdb.apdbSchema.ApdbSchema Class Reference
Inheritance diagram for lsst.dax.apdb.apdbSchema.ApdbSchema:
lsst.dax.apdb.sql.apdbSqlSchema.ApdbSqlSchema

Public Member Functions

 __init__ (self, str schema_file, str ss_schema_file)
 
type|str column_dtype (self, felis.datamodel.DataType|ExtraDataTypes felis_type)
 
VersionTuple schemaVersion (self)
 
bool has_mjd_timestamps (self)
 

Public Attributes

 tableSchemas
 

Protected Member Functions

tuple[Mapping[ApdbTables, Table], VersionTuple|None] _buildSchemas (cls, str schema_file)
 

Protected Attributes

tuple[Mapping[ApdbTables, Table], VersionTuple|None] _schemaVersion = self._buildSchemas(schema_file)
 

Detailed Description

Class for management of APDB schema.

Attributes
----------
tableSchemas : `dict`
    Maps table name to `TableDef` instance.

Parameters
----------
schema_file : `str`
    Location of the YAML file with APDB schema.
ss_schema_file : `str`
    Location of the YAML file with SSO schema. File will be loaded if APDB
    schema file does not contain SSObject/SSSource tables. Can be set to
    empty string to skip loading of SSObject/SSSource schema.

Definition at line 115 of file apdbSchema.py.

Constructor & Destructor Documentation

◆ __init__()

lsst.dax.apdb.apdbSchema.ApdbSchema.__init__ ( self,
str schema_file,
str ss_schema_file )

Definition at line 133 of file apdbSchema.py.

137 ):
138 # build complete table schema
139 self.tableSchemas, self._schemaVersion = self._buildSchemas(schema_file)
140 if ss_schema_file:
141 if ApdbTables.SSObject not in self.tableSchemas or ApdbTables.SSSource not in self.tableSchemas:
142 # Read additional SSP schema.
143 ssp_tables, _ = self._buildSchemas(ss_schema_file)
144 if ApdbTables.SSObject not in ssp_tables or ApdbTables.SSSource not in ssp_tables:
145 raise LookupError(f"Cannot locate SSObject/SSSource table in {ss_schema_file}")
146 self.tableSchemas = dict(self.tableSchemas) | {
147 ApdbTables.SSObject: ssp_tables[ApdbTables.SSObject],
148 ApdbTables.SSSource: ssp_tables[ApdbTables.SSSource],
149 }
150

Member Function Documentation

◆ _buildSchemas()

tuple[Mapping[ApdbTables, Table], VersionTuple | None] lsst.dax.apdb.apdbSchema.ApdbSchema._buildSchemas ( cls,
str schema_file )
protected
Create schema definitions for tables from felis schema.

Reads YAML schema and builds a dictionary containing
`.schema_model.Table` instances for each APDB table appearing in that
schema.

Parameters
----------
schema_file : `str`
    Name of YAML file with ``felis`` schema.

Returns
-------
tables : `dict` [`ApdbTables`, `schema_model.Table`]
    Mapping of table names to `.schema_model.Table` instances.
version : `VersionTuple` or `None`
    Schema version defined in schema file, `None` if version is not
    defined.

Definition at line 189 of file apdbSchema.py.

189 def _buildSchemas(cls, schema_file: str) -> tuple[Mapping[ApdbTables, Table], VersionTuple | None]:
190 """Create schema definitions for tables from felis schema.
191
192 Reads YAML schema and builds a dictionary containing
193 `.schema_model.Table` instances for each APDB table appearing in that
194 schema.
195
196 Parameters
197 ----------
198 schema_file : `str`
199 Name of YAML file with ``felis`` schema.
200
201 Returns
202 -------
203 tables : `dict` [`ApdbTables`, `schema_model.Table`]
204 Mapping of table names to `.schema_model.Table` instances.
205 version : `VersionTuple` or `None`
206 Schema version defined in schema file, `None` if version is not
207 defined.
208 """
209 _LOG.debug("Loading felis schema from %s", schema_file)
210 felis_schema = felis.datamodel.Schema.from_uri(schema_file, context={"id_generation": True})
211 schema = Schema.from_felis(felis_schema)
212
213 # convert all dicts into classes
214 tables: MutableMapping[ApdbTables, Table] = {}
215 for table in schema.tables:
216 try:
217 table_enum = ApdbTables(table.name)
218 except ValueError:
219 # There may be other tables in the schema that do not belong
220 # to APDB.
221 continue
222 else:
223 tables[table_enum] = table
224
225 version: VersionTuple | None = None
226 if schema.version is not None:
227 version = VersionTuple.fromString(schema.version.current)
228
229 _LOG.debug("Loaded schema for tables %s", list(tables))
230 return tables, version
231

◆ column_dtype()

type | str lsst.dax.apdb.apdbSchema.ApdbSchema.column_dtype ( self,
felis.datamodel.DataType | ExtraDataTypes felis_type )
Return Pandas data type for a given Felis column type.

Parameters
----------
felis_type : `felis.datamodel.DataType`
    Felis type, on of the enums defined in `felis.datamodel` module.

Returns
-------
column_dtype : `type` or `str`
    Type that can be used for columns in Pandas.

Raises
------
TypeError
    Raised if type is cannot be handled.

Definition at line 151 of file apdbSchema.py.

151 def column_dtype(self, felis_type: felis.datamodel.DataType | ExtraDataTypes) -> type | str:
152 """Return Pandas data type for a given Felis column type.
153
154 Parameters
155 ----------
156 felis_type : `felis.datamodel.DataType`
157 Felis type, on of the enums defined in `felis.datamodel` module.
158
159 Returns
160 -------
161 column_dtype : `type` or `str`
162 Type that can be used for columns in Pandas.
163
164 Raises
165 ------
166 TypeError
167 Raised if type is cannot be handled.
168 """
169 try:
170 return _dtype_map[felis_type]
171 except KeyError:
172 raise TypeError(f"Unexpected Felis type: {felis_type}")
173

◆ has_mjd_timestamps()

bool lsst.dax.apdb.apdbSchema.ApdbSchema.has_mjd_timestamps ( self)
True if timestamps columns are in MJD (`bool`).

Definition at line 233 of file apdbSchema.py.

233 def has_mjd_timestamps(self) -> bool:
234 """True if timestamps columns are in MJD (`bool`)."""
235 table = self.tableSchemas[ApdbTables.DiaObject]
236 # Look for validityStartMjdTai or validityStart
237 for column in table.columns:
238 if column.name == "validityStartMjdTai":
239 return True
240 elif column.name == "validityStart":
241 return False
242 raise LookupError(
243 "Could not find validityStart or validityStartMjdTai column in DiaObject table schema."
244 )

◆ schemaVersion()

VersionTuple lsst.dax.apdb.apdbSchema.ApdbSchema.schemaVersion ( self)
Return schema version as defined in YAML schema file.

Returns
-------
version : `VersionTuple`
    Version number read from YAML file, if YAML file does not define
    schema version then "0.1.0" is returned.

Definition at line 174 of file apdbSchema.py.

174 def schemaVersion(self) -> VersionTuple:
175 """Return schema version as defined in YAML schema file.
176
177 Returns
178 -------
179 version : `VersionTuple`
180 Version number read from YAML file, if YAML file does not define
181 schema version then "0.1.0" is returned.
182 """
183 if self._schemaVersion is None:
184 return VersionTuple(0, 1, 0)
185 else:
186 return self._schemaVersion
187

Member Data Documentation

◆ _schemaVersion

tuple[Mapping[ApdbTables, Table], VersionTuple | None] lsst.dax.apdb.apdbSchema.ApdbSchema._schemaVersion = self._buildSchemas(schema_file)
protected

Definition at line 139 of file apdbSchema.py.

◆ tableSchemas

lsst.dax.apdb.apdbSchema.ApdbSchema.tableSchemas

Definition at line 139 of file apdbSchema.py.


The documentation for this class was generated from the following file: