Coverage for tests/test_ppdb.py : 17%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# This file is part of dax_ppdb. # # Developed for the LSST Data Management System. # This product includes software developed by the LSST Project # (http://www.lsst.org). # See the COPYRIGHT file at the top-level directory of this distribution # for details of code ownership. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
make_minimal_dia_source_schema)
# HTM indexing level used in the unit tests
"""Generate pixel ID ranges for some envelope region""" pointing_v = UnitVector3d(1., 1., -1.) fov = 0.05 # radians region = Circle(pointing_v, Angle(fov/2)) pixelator = HtmPixelization(HTM_LEVEL) indices = pixelator.envelope(region, 128) return indices.ranges()
"""Make a catalog containing a bunch of DiaObjects inside pixel envelope.
The number of created records will be equal number of ranges (one object per pixel range). Coordinates of the created objects are not usable. """ # make afw catalog schema = make_minimal_dia_object_schema() catalog = afwTable.SourceCatalog(schema)
# make small bunch of records, one entry per one pixel range, # we do not care about coordinates here, in current implementation # they are not used in any query v3d = Vector3d(1., 1., -1.) sp = SpherePoint(v3d) for oid, (start, end) in enumerate(pixel_ranges): record = catalog.addNew() record.set("id", oid) record.set("pixelId", start) record.set("coord_ra", sp.getRa()) record.set("coord_dec", sp.getDec())
return catalog
"""Make a catalog containing a bunch of DiaObjects inside pixel envelope.
The number of created records will be equal to the number of ranges (one object per pixel range). Coordinates of the created objects are not usable. """ v3d = Vector3d(1., 1., -1.) sp = SpherePoint(v3d) data_list = [] for oid, (start, end) in enumerate(pixel_ranges): tmp_dict = {"diaObjectId": oid, "pixelId": start, "ra": sp.getRa().asDegrees(), "decl": sp.getDec().asDegrees()} data_list.append(tmp_dict)
df = pandas.DataFrame(data=data_list) return df
"""Make a catalog containing a bunch of DiaSources associated with the input diaObjects. """ # make some sources schema = make_minimal_dia_source_schema() catalog = afwTable.BaseCatalog(schema) oids = [] for sid, obj in enumerate(objects): record = catalog.addNew() record.set("id", sid) record.set("ccdVisitId", 1) record.set("diaObjectId", obj["id"]) record.set("parent", 0) record.set("coord_ra", obj["coord_ra"]) record.set("coord_dec", obj["coord_dec"]) record.set("flags", 0) record.set("pixelId", obj["pixelId"]) oids.append(obj["id"])
return catalog, oids
"""Make a catalog containing a bunch of DiaSources associated with the input diaObjects. """ # make some sources catalog = [] oids = [] for sid, (index, obj) in enumerate(objects.iterrows()): catalog.append({"diaSourceId": sid, "ccdVisitId": 1, "diaObjectId": obj["diaObjectId"], "parentDiaSourceId": 0, "ra": obj["ra"], "decl": obj["decl"], "flags": 0, "pixelId": obj["pixelId"]}) oids.append(obj["diaObjectId"]) return pandas.DataFrame(data=catalog), oids
"""Make a catalog containing a bunch of DiaFourceSources associated with the input diaObjects. """ # make some sources schema = afwTable.Schema() schema.addField("diaObjectId", "L") schema.addField("ccdVisitId", "L") schema.addField("flags", "L") catalog = afwTable.BaseCatalog(schema) oids = [] for obj in objects: record = catalog.addNew() record.set("diaObjectId", obj["id"]) record.set("ccdVisitId", 1) record.set("flags", 0) oids.append(obj["id"])
return catalog, oids
"""Make a catalog containing a bunch of DiaFourceSources associated with the input diaObjects. """ # make some sources catalog = [] oids = [] for index, obj in objects.iterrows(): catalog.append({"diaObjectId": obj["diaObjectId"], "ccdVisitId": 1, "flags": 0}) oids.append(obj["diaObjectId"]) return pandas.DataFrame(data=catalog), oids
"""A test case for Ppdb class """
"""Validate catalog type and size
Parameters ---------- calalog : `lsst.afw.table.SourceCatalog` size : int Expected catalog size type : `type`, optional Expected catalog type """ self.assertIsInstance(catalog, type) self.assertEqual(len(catalog), size)
"""Test for making an instance of Ppdb using in-memory sqlite engine. """ # sqlite does not support default READ_COMMITTED, for in-memory # database have to use connection pool config = PpdbConfig(db_url="sqlite://", isolation_level="READ_UNCOMMITTED") ppdb = Ppdb(config) # the essence of a test here is that there are no exceptions. ppdb.makeSchema()
"""Test for getting data from empty database.
All get() methods should return empty results, only useful for checking that code is not broken. """
# set read_sources_months to 0 so that Forced/Sources are None config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", read_sources_months=0, read_forced_sources_months=0) ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# get objects by region res = ppdb.getDiaObjects(pixel_ranges, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
# get sources by region res = ppdb.getDiaSourcesInRegion(pixel_ranges, visit_time, return_pandas=self.use_pandas) self.assertIs(res, None)
# get sources by object ID, empty object list res = ppdb.getDiaSources([], visit_time, return_pandas=self.use_pandas)
# get forced sources by object ID, empty object list res = ppdb.getDiaForcedSources([], visit_time, return_pandas=self.use_pandas) self.assertIs(res, None)
"""Test for getting data from empty database.
All get() methods should return empty results, only useful for checking that code is not broken. """
# use non-zero months for Forced/Source fetching config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", read_sources_months=12, read_forced_sources_months=12) ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# get objects by region res = ppdb.getDiaObjects(pixel_ranges, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
# get sources by region res = ppdb.getDiaSourcesInRegion(pixel_ranges, visit_time, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
# get sources by object ID, empty object list, should return None res = ppdb.getDiaSources([], visit_time, return_pandas=self.use_pandas) self.assertIs(res, None)
# get sources by object ID, non-empty object list res = ppdb.getDiaSources([1, 2, 3], visit_time, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
# get forced sources by object ID, empty object list res = ppdb.getDiaForcedSources([], visit_time, return_pandas=self.use_pandas) self.assertIs(res, None)
# get sources by object ID, non-empty object list res = ppdb.getDiaForcedSources([1, 2, 3], visit_time, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
"""Test for getting DiaObjects from empty database using DiaObjectLast table.
All get() methods should return empty results, only useful for checking that code is not broken. """
# don't care about sources. config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", dia_object_index="last_object_table") ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges()
# get objects by region res = ppdb.getDiaObjects(pixel_ranges, return_pandas=self.use_pandas) self._assertCatalog(res, 0, type=self.data_type)
"""Store and retrieve DiaObjects."""
# don't care about sources. config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", dia_object_index="baseline") ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# make afw catalog with Objects if self.use_pandas: catalog = _makeObjectCatalogPandas(pixel_ranges) else: catalog = _makeObjectCatalog(pixel_ranges)
# store catalog ppdb.storeDiaObjects(catalog, visit_time)
# read it back and check sizes res = ppdb.getDiaObjects(pixel_ranges, return_pandas=self.use_pandas) self._assertCatalog(res, len(catalog), type=self.data_type)
"""Store and retrieve DiaObjects using DiaObjectLast table.""" # don't care about sources. config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", dia_object_index="last_object_table", object_last_replace=True) ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# make afw catalog with Objects if self.use_pandas: catalog = _makeObjectCatalogPandas(pixel_ranges) else: catalog = _makeObjectCatalog(pixel_ranges)
# store catalog ppdb.storeDiaObjects(catalog, visit_time)
# read it back and check sizes res = ppdb.getDiaObjects(pixel_ranges, return_pandas=self.use_pandas) self._assertCatalog(res, len(catalog), type=self.data_type)
"""Store and retrieve DiaSources.""" config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", read_sources_months=12, read_forced_sources_months=12) ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# have to store Objects first if self.use_pandas: objects = _makeObjectCatalogPandas(pixel_ranges) catalog, oids = _makeSourceCatalogPandas(objects) else: objects = _makeObjectCatalog(pixel_ranges) catalog, oids = _makeSourceCatalog(objects)
# save the objects ppdb.storeDiaObjects(objects, visit_time)
# save the sources ppdb.storeDiaSources(catalog)
# read it back and check sizes res = ppdb.getDiaSourcesInRegion(pixel_ranges, visit_time, self.use_pandas) self._assertCatalog(res, len(catalog), type=self.data_type)
# read it back using different method res = ppdb.getDiaSources(oids, visit_time, self.use_pandas) self._assertCatalog(res, len(catalog), type=self.data_type)
"""Store and retrieve DiaForcedSources."""
config = PpdbConfig(db_url="sqlite:///", isolation_level="READ_UNCOMMITTED", read_sources_months=12, read_forced_sources_months=12) ppdb = Ppdb(config) ppdb.makeSchema()
pixel_ranges = _makePixelRanges() visit_time = datetime.datetime.now()
# have to store Objects first if self.use_pandas: objects = _makeObjectCatalogPandas(pixel_ranges) catalog, oids = _makeForcedSourceCatalogPandas(objects) else: objects = _makeObjectCatalog(pixel_ranges) catalog, oids = _makeForcedSourceCatalog(objects)
ppdb.storeDiaObjects(objects, visit_time)
# save them ppdb.storeDiaForcedSources(catalog)
# read it back and check sizes res = ppdb.getDiaForcedSources(oids, visit_time, return_pandas=self.use_pandas) self._assertCatalog(res, len(catalog), type=self.data_type)
"""A test case for Ppdb using Pandas as the input/output"""
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |