Skip to content

Commit

Permalink
Merge pull request #719 from lsst/tickets/DM-42012
Browse files Browse the repository at this point in the history
DM-42012: Add a unittest for MaskedImageFitsReader
  • Loading branch information
arunkannawadi authored Dec 6, 2023
2 parents 98a6847 + 880d2ce commit ab0ec9d
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from lsst.geom import Box2I, Point2I, Extent2I, Point2D, Box2D, SpherePoint, degrees
from lsst.afw.geom import makeSkyWcs, Polygon
from lsst.afw.table import ExposureTable
from lsst.afw.image import (Image, Mask, Exposure, LOCAL, PARENT, MaskPixel, VariancePixel,
from lsst.afw.image import (Image, Mask, MaskedImage, Exposure, LOCAL, PARENT, MaskPixel, VariancePixel,
ImageFitsReader, MaskFitsReader, MaskedImageFitsReader, ExposureFitsReader,
FilterLabel, PhotoCalib, ApCorrMap, VisitInfo, TransmissionCurve,
CoaddInputs, ExposureInfo, ExposureF)
Expand Down Expand Up @@ -103,6 +103,29 @@ def testMaskFitsReader(self):
self.assertEqual(subIn.getXY0(), reader.readXY0(*args))
self.assertImagesEqual(subIn, mask)

def testMaskedImageFitsReader(self):
for n, dtypeIn in enumerate(self.dtypes):
with self.subTest(dtypeIn=dtypeIn):
maskedImageIn = MaskedImage(self.bbox, dtype=dtypeIn)
maskedImageIn.image.array[:, :] = np.random.randint(low=1, high=5,
size=maskedImageIn.image.array.shape
)
maskedImageIn.mask.array[:, :] = np.random.randint(low=1, high=5,
size=maskedImageIn.mask.array.shape
)
maskedImageIn.variance.array[:, :] = np.random.randint(low=1, high=5,
size=maskedImageIn.variance.array.shape
)
with lsst.utils.tests.getTempFilePath(".fits") as fileName:
maskedImageIn.writeFits(fileName)
reader = MaskedImageFitsReader(fileName)
self.assertEqual(reader.readBBox(), self.bbox)
self.assertEqual(reader.readImageDType(), dtypeIn)
self.assertEqual(reader.fileName, fileName)
self.checkMultiPlaneReader(reader, maskedImageIn, fileName, self.dtypes[n:],
compare=self.assertMaskedImagesEqual)
self.checkMaskedImageFitsReader(maskedImageIn, fileName, self.dtypes[n:])

def checkMultiPlaneReader(self, reader, objectIn, fileName, dtypesOut, compare):
"""Test operations common to MaskedImageFitsReader and ExposureFitsReader.
Expand Down Expand Up @@ -148,20 +171,20 @@ def checkMultiPlaneReader(self, reader, objectIn, fileName, dtypesOut, compare):
self.assertImagesEqual(image2, object2.image)
compare(subIn, object2)

def checkMaskedImageFitsReader(self, exposureIn, fileName, dtypesOut):
def checkMaskedImageFitsReader(self, maskedImageIn, fileName, dtypesOut):
"""Test MaskedImageFitsReader.
Parameters
----------
exposureIn : `Exposure`
maskedImageIn : `MaskedImage`
Object originally saved, to compare against.
fileName : `str`
Name of the file the reader is reading.
dtypesOut : sequence of `numpy.dype`
Compatible image pixel types to try to read in.
"""
reader = MaskedImageFitsReader(fileName)
self.checkMultiPlaneReader(reader, exposureIn.maskedImage, fileName, dtypesOut,
self.checkMultiPlaneReader(reader, maskedImageIn, fileName, dtypesOut,
compare=self.assertMaskedImagesEqual)

def checkExposureFitsReader(self, exposureIn, fileName, dtypesOut):
Expand Down Expand Up @@ -295,7 +318,7 @@ def testMultiPlaneFitsReaders(self):
exposureIn.setDetector(detector)
with lsst.utils.tests.getTempFilePath(".fits") as fileName:
exposureIn.writeFits(fileName)
self.checkMaskedImageFitsReader(exposureIn, fileName, self.dtypes[n:])
self.checkMaskedImageFitsReader(exposureIn.maskedImage, fileName, self.dtypes[n:])
self.checkExposureFitsReader(exposureIn, fileName, self.dtypes[n:])

def test31035(self):
Expand Down

0 comments on commit ab0ec9d

Please sign in to comment.