Skip to content

Commit

Permalink
Have Standardizers add obstime to LayeredImage (#525)
Browse files Browse the repository at this point in the history
* Have fits and butler standardizers set obstimes

* Clean up unit tests

* Formatting fixes

* Fix comments

* Address comments

* Add type conversion back in for consistency
  • Loading branch information
wilsonbb authored Mar 21, 2024
1 parent 43218e4 commit 5ff0cff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/kbmod/standardizers/butler_standardizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,13 @@ def toLayeredImage(self):
psfs = self.standardizePSF()

# guaranteed to exist, i.e. safe to access
mjds = meta["mjd"]
if isinstance(meta["mjd"], (list, tuple)):
mjds = meta["mjd"]
else:
mjds = (meta["mjd"] for e in self.processable)

imgs = []
for sci, var, mask, psf, t in zip(sciences, variances, masks, psfs, mjds):
imgs.append(LayeredImage(RawImage(sci), RawImage(var), RawImage(mask), t, psf))
mask = mask.astype(np.float32)
imgs.append(LayeredImage(RawImage(sci, t), RawImage(var, t), RawImage(mask, t), psf))
return imgs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def toLayeredImage(self):
# copy. TODO: fix when/if CPP stuff is fixed.
imgs = []
for sci, var, mask, psf, t in zip(sciences, variances, masks, psfs, mjds):
imgs.append(LayeredImage(RawImage(sci), RawImage(var), RawImage(mask.astype(np.float32)), psf))

# Converts nd array mask from bool to np.float32
mask = mask.astype(np.float32)
imgs.append(LayeredImage(RawImage(sci, t), RawImage(var, t), RawImage(mask, t), psf))
return imgs
26 changes: 26 additions & 0 deletions tests/test_butlerstd.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,32 @@ def test_psf(self):
self.assertIsInstance(psf, PSF)
self.assertEqual(psf.get_std(), std.config["psf_std"])

def test_to_layered_image(self):
"""Test ButlerStandardizer can create a LayeredImage."""
std = Standardizer.get(DatasetId(8), butler=self.butler)
self.assertIsInstance(std, ButlerStandardizer)

# Get the expected FITS files and extract the MJD from the header
fits = FitsFactory.get_fits(8, spoof_data=True)
hdr = fits["PRIMARY"].header
expected_mjd = Time(hdr["DATE-AVG"]).mjd

# Get list of layered images froom the standardizer
butler_imgs = std.toLayeredImage()
self.assertEqual(1, len(butler_imgs))
img = butler_imgs[0]

# Compare standardized images
np.testing.assert_equal(fits["IMAGE"].data, img.get_science().image)
np.testing.assert_equal(fits["VARIANCE"].data, img.get_variance().image)
np.testing.assert_equal(fits["MASK"].data, img.get_mask().image)

# Test that we correctly set metadata
self.assertEqual(expected_mjd, img.get_obstime())
self.assertEqual(expected_mjd, img.get_science().obstime)
self.assertEqual(expected_mjd, img.get_variance().obstime)
self.assertEqual(expected_mjd, img.get_mask().obstime)


if __name__ == "__main__":
unittest.main()
27 changes: 25 additions & 2 deletions tests/test_standardizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
KBMODV1,
KBMODV1Config,
FitsStandardizer,
SingleExtensionFits,
MultiExtensionFits,
)


Expand Down Expand Up @@ -344,6 +342,31 @@ def test_psf(self):
psf = next(std2.standardizePSF())
self.assertEqual(psf.get_std(), std2.config["psf_std"][0])

def test_to_layered_image(self):
"""Test that KBMODV1 standardizer can create LayeredImages."""
std = Standardizer.get(self.fits, force=KBMODV1)
self.assertIsInstance(std, KBMODV1)

# Get the expected FITS files and extract the MJD from the header
hdr = self.fits["PRIMARY"].header
expected_mjd = Time(hdr["DATE-AVG"], format="isot").mjd

# Get list of layered images from the standardizer
layered_imgs = std.toLayeredImage()
self.assertEqual(1, len(layered_imgs))
img = layered_imgs[0]

# Compare standardized images
np.testing.assert_equal(self.fits["IMAGE"].data, img.get_science().image)
np.testing.assert_equal(self.fits["VARIANCE"].data, img.get_variance().image)
np.testing.assert_equal(self.fits["MASK"].data, img.get_mask().image)

# Test that we correctly set metadata
self.assertEqual(expected_mjd, img.get_obstime())
self.assertEqual(expected_mjd, img.get_science().obstime)
self.assertEqual(expected_mjd, img.get_variance().obstime)
self.assertEqual(expected_mjd, img.get_mask().obstime)


if __name__ == "__main__":
unittest.main()

0 comments on commit 5ff0cff

Please sign in to comment.