Coverage for tests/test_association.py : 20%

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 ap_verify. # # 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, \ make_minimal_dia_object_schema, \ AssociationTask measureNumberNewDiaObjects, \ measureNumberUnassociatedDiaObjects, \ measureFractionUpdatedDiaObjects, \ measureNumberSciSources, \ measureFractionDiaSourcesToSciSources, \ measureTotalUnassociatedDiaObjects
# Define the root of the tests relative to this file
# Define a generic dataId 'filter': 'r'}
startId=0, schema=None, scatterArcsec=1.0, indexerIds=None, associatedIds=None): """Create dummy DIASources or DIAObjects for use in our tests.
Parameters ---------- pointLocsDeg : array-like (N, 2) of `float`s Positions of the test points to create in RA, DEC. startId : `int` Unique id of the first object to create. The remaining sources are incremented by one from the first id. schema : `lsst.afw.table.Schema` Schema of the objects to create. Defaults to the DIASource schema. scatterArcsec : `float` Scatter to add to the position of each DIASource. indexerIds : `list` of `ints`s Id numbers of pixelization indexer to store. Must be the same length as the first dimension of point_locs_deg. associatedIds : `list` of `ints`s Id numbers of associated DIAObjects to store. Must be the same length as the first dimension of point_locs_deg.
Returns ------- testPoints : `lsst.afw.table.SourceCatalog` Catalog of points to test. """ if schema is None: schema = make_minimal_dia_source_schema() sources = afwTable.SourceCatalog(schema)
for src_idx, (ra, dec,) in enumerate(pointLocsDeg): src = sources.addNew() src['id'] = src_idx + startId coord = afwGeom.SpherePoint(ra, dec, afwGeom.degrees) if scatterArcsec > 0.0: coord = coord.offset( np.random.rand() * 360 * afwGeom.degrees, np.random.rand() * scatterArcsec * afwGeom.arcseconds) if indexerIds is not None: src['pixelId'] = indexerIds[src_idx] if associatedIds is not None: src['diaObjectId'] = associatedIds[src_idx] src.setCoord(coord)
return sources
# Create an unrun AssociationTask. self.assocTask = AssociationTask()
# Create a empty butler repository and put data in it. self.numTestSciSources = 10 self.numTestDiaSources = 5 testSources = createTestPoints( pointLocsDeg=[[idx, idx] for idx in range(self.numTestSciSources)]) testDiaSources = createTestPoints( pointLocsDeg=[[idx, idx] for idx in range(self.numTestDiaSources)])
# Fake Butler to avoid initialization and I/O overhead def mockGet(datasetType, dataId=None): """An emulator for `lsst.daf.persistence.Butler.get` that can only handle test data. """ # Check whether dataIdDict is a subset of dataId if dataIdDict.items() <= dataId.items(): if datasetType == 'src': return testSources elif datasetType == 'deepDiff_diaSrc': return testDiaSources raise dafPersist.NoResults("Dataset not found:", datasetType, dataId) self.butler = NonCallableMock(spec=dafPersist.Butler, get=mockGet)
self.numTestDiaObjects = 5 self.diaObjects = createTestPoints( pointLocsDeg=[[idx, idx] for idx in range(self.numTestDiaObjects)], schema=make_minimal_dia_object_schema(['r'])) for diaObject in self.diaObjects: diaObject['nDiaSources'] = 1
del self.assocTask
if hasattr(self, "butler"): del self.butler
"""Verify that association information can be recovered from metadata. """ # Insert data into the task metadata. nUpdatedDiaObjects = 5 nNewDiaObjects = 6 nUnassociatedDiaObjects = 7 testAssocResult = pipeBase.Struct( n_updated_dia_objects=nUpdatedDiaObjects, n_new_dia_objects=nNewDiaObjects, n_unassociated_dia_objects=nUnassociatedDiaObjects,) self.assocTask._add_association_meta_data(testAssocResult) metadata = self.assocTask.getFullMetadata()
meas = measureNumberNewDiaObjects( metadata, "association", "association.numNewDIAObjects") self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name(metric="association.numNewDIAObjects")) self.assertEqual(meas.quantity, nNewDiaObjects * u.count)
meas = measureNumberUnassociatedDiaObjects( metadata, 'association', 'association.fracUpdatedDIAObjects') self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name( metric='association.fracUpdatedDIAObjects')) self.assertEqual(meas.quantity, nUnassociatedDiaObjects * u.count)
meas = measureFractionUpdatedDiaObjects( metadata, 'association', 'association.fracUpdatedDIAObjects') self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name(metric='association.fracUpdatedDIAObjects')) self.assertEqual(meas.quantity, nUpdatedDiaObjects / (nUpdatedDiaObjects + nUnassociatedDiaObjects) * u.dimensionless_unscaled)
""" Test the association measurements that require a butler. """ meas = measureNumberSciSources( self.butler, dataIdDict=dataIdDict, metricName='ip_diffim.numSciSrc') self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name(metric='ip_diffim.numSciSrc')) self.assertEqual(meas.quantity, self.numTestSciSources * u.count)
meas = measureFractionDiaSourcesToSciSources( self.butler, dataIdDict=dataIdDict, metricName='ip_diffim.fracDiaSrcToSciSrc') self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name(metric='ip_diffim.fracDiaSrcToSciSrc')) self.assertEqual(meas.quantity, self.numTestDiaSources / self.numTestSciSources * u.dimensionless_unscaled)
# Fake DB handle to avoid DB initialization overhead cursor = NonCallableMock(spec=sqlite3.Cursor) cursor.fetchall.return_value = [(len(self.diaObjects),)]
meas = measureTotalUnassociatedDiaObjects( cursor, metricName='association.numTotalUnassociatedDiaObjects') self.assertIsInstance(meas, Measurement) self.assertEqual( meas.metric_name, lsst.verify.Name( metric='association.numTotalUnassociatedDiaObjects')) self.assertEqual(meas.quantity, self.numTestDiaObjects * u.count)
""" Test attempting to create a measurement with data that the butler does not contain. """
with self.assertRaises(dafPersist.NoResults): measureNumberSciSources( self.butler, dataIdDict={'visit': 1000, 'filter': 'r'}, metricName='ip_diffim.fracDiaSrcToSciSrc')
with self.assertRaises(dafPersist.NoResults): measureFractionDiaSourcesToSciSources( self.butler, dataIdDict={'visit': 1000, 'filter': 'r'}, metricName='ip_diffim.fracDiaSrcToSciSrc')
with self.assertRaises(dafPersist.NoResults): measureNumberSciSources( self.butler, dataIdDict={'visit': 1111, 'filter': 'g'}, metricName='ip_diffim.fracDiaSrcToSciSrc')
with self.assertRaises(dafPersist.NoResults): measureFractionDiaSourcesToSciSources( self.butler, dataIdDict={'visit': 1111, 'filter': 'g'}, metricName='ip_diffim.fracDiaSrcToSciSrc')
""" Test for the correct failure when measuring from non-existent metadata. """ metadata = self.assocTask.getFullMetadata()
meas = measureNumberNewDiaObjects( metadata, "association", "association.numNewDIAObjects") self.assertIsNone(meas)
"""Verify that trying to measure a nonexistent metric fails. """ testAssocResult = pipeBase.Struct( n_updated_dia_objects=5, n_new_dia_objects=6, n_unassociated_dia_objects=7,) self.assocTask._add_association_meta_data(testAssocResult) metadata = self.assocTask.getFullMetadata() with self.assertRaises(TypeError): measureNumberNewDiaObjects( metadata, "association", "foo.bar.FooBar") with self.assertRaises(TypeError): measureNumberUnassociatedDiaObjects( metadata, "association", "foo.bar.FooBar") with self.assertRaises(TypeError): measureFractionUpdatedDiaObjects( metadata, "association", "foo.bar.FooBar")
with self.assertRaises(TypeError): measureNumberSciSources( self.butler, dataId=dataIdDict, metricName='foo.bar.FooBar') with self.assertRaises(TypeError): measureFractionDiaSourcesToSciSources( self.butler, dataId=dataIdDict, metricName='foo.bar.FooBar')
# Fake DB handle to avoid DB initialization overhead cursor = NonCallableMock(spec=sqlite3.Cursor) cursor.fetchall.return_value = [(0,)] with self.assertRaises(TypeError): measureTotalUnassociatedDiaObjects( cursor, metricName='foo.bar.FooBar')
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |