LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
Public Member Functions | Public Attributes | List of all members
lsst.dax.apdb.apdbSchema.ApdbSchema Class Reference
Inheritance diagram for lsst.dax.apdb.apdbSchema.ApdbSchema:

Public Member Functions

def __init__ (self, engine, dia_object_index, dia_object_nightly, schema_file, extra_schema_file=None, column_map=None, afw_schemas=None, prefix="")
 
def makeSchema (self, drop=False, mysql_engine='InnoDB', oracle_tablespace=None, oracle_iot=False)
 
def getAfwSchema (self, table_name, columns=None)
 
def getAfwColumns (self, table_name)
 
def getColumnMap (self, table_name)
 

Public Attributes

 objects
 
 objects_nightly
 
 objects_last
 
 sources
 
 forcedSources
 
 visits
 

Detailed Description

Class for management of APDB schema.

Attributes
----------
objects : `sqlalchemy.Table`
    DiaObject table instance
objects_nightly : `sqlalchemy.Table`
    DiaObjectNightly table instance, may be None
objects_last : `sqlalchemy.Table`
    DiaObjectLast table instance, may be None
sources : `sqlalchemy.Table`
    DiaSource table instance
forcedSources : `sqlalchemy.Table`
    DiaForcedSource table instance
visits : `sqlalchemy.Table`
    ApdbProtoVisits table instance

Parameters
----------
engine : `sqlalchemy.engine.Engine`
    SQLAlchemy engine instance
dia_object_index : `str`
    Indexing mode for DiaObject table, see `ApdbConfig.dia_object_index`
    for details.
dia_object_nightly : `bool`
    If `True` then create per-night DiaObject table as well.
schema_file : `str`
    Name of the YAML schema file.
extra_schema_file : `str`, optional
    Name of the YAML schema file with extra column definitions.
column_map : `str`, optional
    Name of the YAML file with column mappings.
afw_schemas : `dict`, optional
    Dictionary with table name for a key and `afw.table.Schema`
    for a value. Columns in schema will be added to standard APDB
    schema (only if standard schema does not have matching column).
prefix : `str`, optional
    Prefix to add to all scheam elements.

Definition at line 162 of file apdbSchema.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.dax.apdb.apdbSchema.ApdbSchema.__init__ (   self,
  engine,
  dia_object_index,
  dia_object_nightly,
  schema_file,
  extra_schema_file = None,
  column_map = None,
  afw_schemas = None,
  prefix = "" 
)

Definition at line 219 of file apdbSchema.py.

221  afw_schemas=None, prefix=""):
222 
223  self._engine = engine
224  self._dia_object_index = dia_object_index
225  self._dia_object_nightly = dia_object_nightly
226  self._prefix = prefix
227 
228  self._metadata = MetaData(self._engine)
229 
230  self.objects = None
231  self.objects_nightly = None
232  self.objects_last = None
233  self.sources = None
234  self.forcedSources = None
235  self.visits = None
236 
237  if column_map:
238  column_map = os.path.expandvars(column_map)
239  _LOG.debug("Reading column map file %s", column_map)
240  with open(column_map) as yaml_stream:
241  # maps cat column name to afw column name
242  self._column_map = yaml.load(yaml_stream, Loader=yaml.SafeLoader)
243  _LOG.debug("column map: %s", self._column_map)
244  else:
245  _LOG.debug("No column map file is given, initialize to empty")
246  self._column_map = {}
247  self._column_map_reverse = {}
248  for table, cmap in self._column_map.items():
249  # maps afw column name to cat column name
250  self._column_map_reverse[table] = {v: k for k, v in cmap.items()}
251  _LOG.debug("reverse column map: %s", self._column_map_reverse)
252 
253  # build complete table schema
254  self._schemas = self._buildSchemas(schema_file, extra_schema_file,
255  afw_schemas)
256 
257  # map cat column types to alchemy
258  self._type_map = dict(DOUBLE=self._getDoubleType(),
259  FLOAT=sqlalchemy.types.Float,
260  DATETIME=sqlalchemy.types.TIMESTAMP,
261  BIGINT=sqlalchemy.types.BigInteger,
262  INTEGER=sqlalchemy.types.Integer,
263  INT=sqlalchemy.types.Integer,
264  TINYINT=sqlalchemy.types.Integer,
265  BLOB=sqlalchemy.types.LargeBinary,
266  CHAR=sqlalchemy.types.CHAR,
267  BOOL=sqlalchemy.types.Boolean)
268 
269  # generate schema for all tables, must be called last
270  self._makeTables()
271 
std::vector< SchemaItem< Flag > > * items

Member Function Documentation

◆ getAfwColumns()

def lsst.dax.apdb.apdbSchema.ApdbSchema.getAfwColumns (   self,
  table_name 
)
Returns mapping of afw column names to Column definitions.

Parameters
----------
table_name : `str`
    One of known APDB table names.

Returns
-------
column_map : `dict`
    Mapping of afw column names to `ColumnDef` instances.

Definition at line 437 of file apdbSchema.py.

437  def getAfwColumns(self, table_name):
438  """Returns mapping of afw column names to Column definitions.
439 
440  Parameters
441  ----------
442  table_name : `str`
443  One of known APDB table names.
444 
445  Returns
446  -------
447  column_map : `dict`
448  Mapping of afw column names to `ColumnDef` instances.
449  """
450  table = self._schemas[table_name]
451  col_map = self._column_map.get(table_name, {})
452 
453  cmap = {}
454  for column in table.columns:
455  afw_name = col_map.get(column.name, column.name)
456  cmap[afw_name] = column
457  return cmap
458 

◆ getAfwSchema()

def lsst.dax.apdb.apdbSchema.ApdbSchema.getAfwSchema (   self,
  table_name,
  columns = None 
)
Return afw schema for given table.

Parameters
----------
table_name : `str`
    One of known APDB table names.
columns : `list` of `str`, optional
    Include only given table columns in schema, by default all columns
    are included.

Returns
-------
schema : `lsst.afw.table.Schema`
column_map : `dict`
    Mapping of the table/result column names into schema key.

Definition at line 369 of file apdbSchema.py.

369  def getAfwSchema(self, table_name, columns=None):
370  """Return afw schema for given table.
371 
372  Parameters
373  ----------
374  table_name : `str`
375  One of known APDB table names.
376  columns : `list` of `str`, optional
377  Include only given table columns in schema, by default all columns
378  are included.
379 
380  Returns
381  -------
382  schema : `lsst.afw.table.Schema`
383  column_map : `dict`
384  Mapping of the table/result column names into schema key.
385  """
386 
387  table = self._schemas[table_name]
388  col_map = self._column_map.get(table_name, {})
389 
390  # make a schema
391  col2afw = {}
392  schema = afwTable.SourceTable.makeMinimalSchema()
393  for column in table.columns:
394  if columns and column.name not in columns:
395  continue
396  afw_col = col_map.get(column.name, column.name)
397  if afw_col in schema.getNames():
398  # Continue if the column is already in the minimal schema.
399  key = schema.find(afw_col).getKey()
400  elif column.type in ("DOUBLE", "FLOAT") and column.unit == "deg":
401  #
402  # NOTE: degree to radian conversion is not supported (yet)
403  #
404  # angles in afw are radians and have special "Angle" type
405  key = schema.addField(afw_col,
406  type="Angle",
407  doc=column.description or "",
408  units="rad")
409  elif column.type == "BLOB":
410  # No BLOB support for now
411  key = None
412  else:
413  units = column.unit or ""
414  # some units in schema are not recognized by afw but we do not care
415  if self._afw_type_map_reverse[column.type] == 'String':
416  key = schema.addField(afw_col,
417  type=self._afw_type_map_reverse[column.type],
418  doc=column.description or "",
419  units=units,
420  parse_strict="silent",
421  size=10)
422  elif units == "deg":
423  key = schema.addField(afw_col,
424  type='Angle',
425  doc=column.description or "",
426  parse_strict="silent")
427  else:
428  key = schema.addField(afw_col,
429  type=self._afw_type_map_reverse[column.type],
430  doc=column.description or "",
431  units=units,
432  parse_strict="silent")
433  col2afw[column.name] = key
434 
435  return schema, col2afw
436 

◆ getColumnMap()

def lsst.dax.apdb.apdbSchema.ApdbSchema.getColumnMap (   self,
  table_name 
)
Returns mapping of column names to Column definitions.

Parameters
----------
table_name : `str`
    One of known APDB table names.

Returns
-------
column_map : `dict`
    Mapping of column names to `ColumnDef` instances.

Definition at line 459 of file apdbSchema.py.

459  def getColumnMap(self, table_name):
460  """Returns mapping of column names to Column definitions.
461 
462  Parameters
463  ----------
464  table_name : `str`
465  One of known APDB table names.
466 
467  Returns
468  -------
469  column_map : `dict`
470  Mapping of column names to `ColumnDef` instances.
471  """
472  table = self._schemas[table_name]
473  cmap = {column.name: column for column in table.columns}
474  return cmap
475 

◆ makeSchema()

def lsst.dax.apdb.apdbSchema.ApdbSchema.makeSchema (   self,
  drop = False,
  mysql_engine = 'InnoDB',
  oracle_tablespace = None,
  oracle_iot = False 
)
Create or re-create all tables.

Parameters
----------
drop : `bool`, optional
    If True then drop tables before creating new ones.
mysql_engine : `str`, optional
    MySQL engine type to use for new tables.
oracle_tablespace : `str`, optional
    Name of Oracle tablespace, only useful with oracle
oracle_iot : `bool`, optional
    Make Index-organized DiaObjectLast table.

Definition at line 339 of file apdbSchema.py.

339  def makeSchema(self, drop=False, mysql_engine='InnoDB', oracle_tablespace=None, oracle_iot=False):
340  """Create or re-create all tables.
341 
342  Parameters
343  ----------
344  drop : `bool`, optional
345  If True then drop tables before creating new ones.
346  mysql_engine : `str`, optional
347  MySQL engine type to use for new tables.
348  oracle_tablespace : `str`, optional
349  Name of Oracle tablespace, only useful with oracle
350  oracle_iot : `bool`, optional
351  Make Index-organized DiaObjectLast table.
352  """
353 
354  # re-make table schema for all needed tables with possibly different options
355  _LOG.debug("clear metadata")
356  self._metadata.clear()
357  _LOG.debug("re-do schema mysql_engine=%r oracle_tablespace=%r",
358  mysql_engine, oracle_tablespace)
359  self._makeTables(mysql_engine=mysql_engine, oracle_tablespace=oracle_tablespace,
360  oracle_iot=oracle_iot)
361 
362  # create all tables (optionally drop first)
363  if drop:
364  _LOG.info('dropping all tables')
365  self._metadata.drop_all()
366  _LOG.info('creating all tables')
367  self._metadata.create_all()
368 

Member Data Documentation

◆ forcedSources

lsst.dax.apdb.apdbSchema.ApdbSchema.forcedSources

Definition at line 234 of file apdbSchema.py.

◆ objects

lsst.dax.apdb.apdbSchema.ApdbSchema.objects

Definition at line 230 of file apdbSchema.py.

◆ objects_last

lsst.dax.apdb.apdbSchema.ApdbSchema.objects_last

Definition at line 232 of file apdbSchema.py.

◆ objects_nightly

lsst.dax.apdb.apdbSchema.ApdbSchema.objects_nightly

Definition at line 231 of file apdbSchema.py.

◆ sources

lsst.dax.apdb.apdbSchema.ApdbSchema.sources

Definition at line 233 of file apdbSchema.py.

◆ visits

lsst.dax.apdb.apdbSchema.ApdbSchema.visits

Definition at line 235 of file apdbSchema.py.


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