Skip to content

Commit

Permalink
Unit tests for random candidate reservation
Browse files Browse the repository at this point in the history
Add unit test for measurePsf to check candidate reserving code.
Add tests to existing photoCal tests to do the same for photoCal.
  • Loading branch information
pgee2000 committed Sep 27, 2017
1 parent 5f0d215 commit e4c3b9b
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 18 deletions.
56 changes: 38 additions & 18 deletions tests/test_photoCal.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import numpy as np

from lsst.daf.persistence import Butler
import lsst.meas.astrom as measAstrom
from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask
import lsst.afw.geom as afwGeom
import lsst.afw.table as afwTable
Expand Down Expand Up @@ -134,36 +133,57 @@ def testFlags(self):
mapper.addMapping(self.srcCat.schema.find(name).key)
cat.extend(self.srcCat, mapper=mapper)

# test that by default, no stars are reserved and used < candidates
# test that by default, no stars are reserved and all used are candidates
task.run(exposure=self.exposure, sourceCat=cat)
candidates = 0
used = 0
reserved = 0
for source in cat:
if source.get("calib_photometryCandidate"):
candidates += 1
if source.get("calib_photometryUsed"):
self.assertTrue(source.get("calib_photometryCandidate"))
used += 1
if source.get("calib_photometryReserved"):
reserved += 1
self.assertLessEqual(used, candidates)
self.assertEqual(reserved, 0)
self.assertFalse(source.get("calib_photometryReserved"))
# test that some very actually used
self.assertGreater(used, 0)

def testReserveFraction(self):
"""test that a reserve fraction can be selected, and that it differs with expId"""
schema = self.srcCat.schema
task = PhotoCalTask(self.refObjLoader, config=self.config, schema=schema)
mapper = afwTable.SchemaMapper(self.srcCat.schema, schema)
cat = afwTable.SourceCatalog(schema)
for name in self.srcCat.schema.getNames():
mapper.addMapping(self.srcCat.schema.find(name).key)
cat.extend(self.srcCat, mapper=mapper)

# set the reserve fraction, and see if the right proportion are reserved.
cat = afwTable.SourceCatalog(schema)
for name in self.srcCat.schema.getNames():
mapper.addMapping(self.srcCat.schema.find(name).key)
cat.extend(self.srcCat, mapper=mapper)
self.config.reserveFraction = .3
task.run(exposure=self.exposure, sourceCat=cat)
task.run(exposure=self.exposure, sourceCat=cat, expId=12345)
candidates = 0
reserved = 0
used = 0
reservedSources1 = []
for source in cat:
if source.get("calib_photometryCandidate"):
candidates += 1
if source.get("calib_photometryUsed"):
used += 1
if source.get("calib_photometryReserved"):
reserved += 1
self.assertEqual(reserved, int(.3 * candidates))
self.assertLessEqual(used, (candidates - reserved))
reservedSources1.append(source.getId())
reserved = len(reservedSources1)
self.assertEqual(int(self.config.reserveFraction * (candidates + reserved)), reserved)

# try again with a different id, and see if the list is different
cat = afwTable.SourceCatalog(schema)
for name in self.srcCat.schema.getNames():
mapper.addMapping(self.srcCat.schema.find(name).key)
cat.extend(self.srcCat, mapper=mapper)
self.config.reserveFraction = .3
task.run(exposure=self.exposure, sourceCat=cat, expId=67890)
reservedSources2 = []
for source in cat:
if source.get("calib_photometryReserved"):
reservedSources2.append(source.getId())
self.assertEqual(len(reservedSources1), len(reservedSources2))
self.assertNotEqual(reservedSources1, reservedSources2)

def testZeroPoint(self):
""" Test to see if we can compute a photometric zeropoint given a reference task"""
Expand Down
120 changes: 120 additions & 0 deletions tests/test_psfCandidateSelection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python
#
# LSST Data Management System
# Copyright 2008-2017 AURA/LSST
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# 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 LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#

from __future__ import division, print_function, absolute_import
import os
import unittest

import lsst.afw.image as afwImage
import lsst.utils.tests
from lsst.utils import getPackageDir
from lsst.obs.base import ExposureIdInfo
from lsst.log import Log
from lsst.pipe.tasks.characterizeImage import CharacterizeImageTask


class PsfFlagTestCase(lsst.utils.tests.TestCase):

def setUp(self):
# Load sample input from disk
expPath = os.path.join(getPackageDir("pipe_tasks"), "tests", "data", "v695833-e0-c000-a00.sci.fits")
self.exposure = afwImage.ExposureF(expPath)
# set log level so that warnings do not display
Log.getLogger("characterizeImage").setLevel(Log.ERROR)

def tearDown(self):
del self.exposure

def testFlags(self):
# test that all of the flags are defined and there is no reservation by default
# also test that used sources are a subset of candidate sources
task = CharacterizeImageTask()
results = task.characterize(self.exposure)
used = 0
reserved = 0
for source in results.sourceCat:
if source.get("calib_psfUsed"):
used += 1
self.assertTrue(source.get("calib_psfCandidate"))
if source.get("calib_psfReserved"):
reserved += 1
self.assertGreater(used, 0)
self.assertEqual(reserved, 0)

def testReserveFraction(self):
# test that a fraction of the possible candidates can be reserved
# and that different expIds can produce a different reserveLists
task = CharacterizeImageTask()
# set the reserve fraction, and see if the right proportion are reserved.
task.measurePsf.config.reserveFraction = .3
exposureIdInfo = ExposureIdInfo(12345, expBits=16)
results = task.characterize(self.exposure, exposureIdInfo=exposureIdInfo)
candidates = 0
reservedSources1 = []
for source in results.sourceCat:
if source.get("calib_psfCandidate"):
candidates += 1
if source.get("calib_psfReserved"):
reservedSources1.append(source.getId())
reserved = len(reservedSources1)
self.assertEqual(reserved, int(.3 * (candidates + reserved)))

# try again with a different id, and see if the list is different
# but the number of reserved sources is the same
exposureIdInfo = ExposureIdInfo(6789, expBits=16)
results = task.characterize(self.exposure, exposureIdInfo=exposureIdInfo)
reservedSources2 = []
for source in results.sourceCat:
if source.get("calib_psfReserved"):
reservedSources2.append(source.getId())
# Length of reserveList should be the same, but specific sources may differ
self.assertEqual(len(reservedSources1), len(reservedSources2))
self.assertNotEqual(reservedSources1, reservedSources2)

def testReserveSeedReproducible(self):
# test that the same seed twice will produce the same reserve set
task = CharacterizeImageTask()
task.measurePsf.config.reserveFraction = .3
results = task.characterize(self.exposure)
reservedSources1 = []
for source in results.sourceCat:
if source.get("calib_psfReserved"):
reservedSources1.append(source.getId())

# try again with the same seed (the default)
results = task.characterize(self.exposure)
reservedSources2 = []
for source in results.sourceCat:
if source.get("calib_psfReserved"):
reservedSources2.append(source.getId())
# reserveLists should be the same
self.assertEqual(reservedSources1, reservedSources2)


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass


if __name__ == "__main__":
lsst.utils.tests.init()
unittest.main()

0 comments on commit e4c3b9b

Please sign in to comment.