Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-35039: Add tests for CfhtIsrTask to obs_cfht #107

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
140 changes: 140 additions & 0 deletions tests/test_isr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the blank line in the beginning is required.

# This file is part of obs_cfht.
#
# 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/>.
#
import os
import unittest
import warnings

import lsst.afw.image as afwImage
import lsst.utils.tests

from lsst.obs.cfht import MegaPrime
from lsst.obs.cfht.cfhtIsrTask import CfhtIsrTask


class CfhtIsrTestCase(lsst.utils.tests.TestCase):
"""Test for the CFHT IsrTask wrapper."""

def setUp(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's only one test case, it doesn't make any difference now, but if you foresee other test cases making use of the exposure and camera attributes, make it a setUpClass method so that it doesn't have to read in the images for every test case.

testDataPackage = "testdata_cfht"
try:
testDataDirectory = lsst.utils.getPackageDir(testDataPackage)
except LookupError as e:
warnings.warn(e.args[0])
raise unittest.SkipTest("Skipping tests as testdata_cfht is not setup")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we'd ask for the test data package in the preamble and use a unittest.skipIf decorator around the entire test class. This is more efficient than having every test run the same code that will always fail.


# We'll need a detector, so get the first one.
self.camera = MegaPrime().getCamera()
detector = self.camera[0]

# Get the override.
self.configPath = os.path.join(lsst.utils.getPackageDir("obs_lsst"), "config",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be depending on obs_lsst in this test.

"isr.py")
# Read the image data.
imageLocation = os.path.join(testDataDirectory, "DATA/raw/08BL05/w2.+2+2/2008-11-01/i2",
"1038843o.fits.fz")
self.exposure = self.imageHandler(imageLocation, detector)

def test_processing(self):
# Process image and confirm it doesn't fail.

config = CfhtIsrTask.ConfigClass()
# copy = CfhtIsrTask.ConfigClass()
# # Why does load not load the config? ;_;
# copy.load(self.configPath)
# config.compare(copy)

# Manually fix config.
config.doDark = False
config.doDefect = False
config.doFlat = False
config.doBias = False
config.doFringe = False
config.fringeAfterFlat = False
config.doWrite = False
config.fringe.filters = ["i.MP9701", "i.MP9702", "z.MP9801"]
config.fringe.pedestal = True
config.fringe.small = 1
config.fringe.large = 50
config.doAssembleIsrExposures = True
config.doSuspect = False
config.fluxMag0T1 = {"i.MP9702": 100.0}

task = CfhtIsrTask(config=config)
results = task.run(self.exposure, camera=self.camera)
md = results.exposure.getMetadata()
amplifiers = list(results.exposure.getDetector().getAmplifiers())

# Check that saturation level are set correctly.
self.assertEqual(md['SATURATE'], 65535)

# Check that the gain and read noise match what is in the
# GAINA and GAINB/RDNOISEA RDNOISEB fields.
self.assertEqual(md['GAINA'], amplifiers[0].getGain())
self.assertEqual(md['GAINB'], amplifiers[1].getGain())
self.assertEqual(md['RDNOISEA'], amplifiers[0].getReadNoise())
self.assertEqual(md['RDNOISEB'], amplifiers[1].getReadNoise())

def imageHandler(self, location, detector=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should perhaps be a staticmethod, since none of self's attributes are used. Also, this could go either before or after setUp(Class) method, in case we want to add additional tests later.

"""Quick method to handle image reading.

Parameters
----------
location : `str`
FITS file location.
detector : `lsst.afw.cameraGeom.Detector`, optional
Detector to attach to the exposure.

Returns
-------
exp : `lsst.afw.image.Exposure`
Fully constructed exposure.
"""
if detector:
hdu = detector.getId() + 1
else:
hdu = 1

reader = afwImage.ExposureFitsReader(location)
imReader = afwImage.ImageFitsReader(location, hdu=hdu)

exp = afwImage.makeExposure(afwImage.makeMaskedImage(imReader.read()))
exp.setMetadata(reader.readMetadata())
exp.setInfo(reader.readExposureInfo())

if detector:
exp.setDetector(detector)

return exp


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


def setup_module(module):
lsst.utils.tests.init()


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