Skip to content

Commit

Permalink
Merge pull request #28 from lsst/tickets/DM-45269
Browse files Browse the repository at this point in the history
DM-45269: Read v1.3 piff pkls in piff v1.4
  • Loading branch information
jmeyers314 authored Jul 16, 2024
2 parents 607f77f + 5e0a357 commit cd45df2
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
13 changes: 12 additions & 1 deletion python/lsst/meas/extensions/piff/piffPsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
__all__ = ["PiffPsf"]

import pickle
import piff
from packaging.version import Version
import numpy as np
from lsst.afw.typehandling import StorableHelperFactory
from lsst.meas.algorithms import ImagePsf
Expand Down Expand Up @@ -64,7 +66,16 @@ def _write(self):

@staticmethod
def _read(pkl):
return PiffPsf(*pickle.loads(pkl))
width, height, piffResult = pickle.loads(pkl)
# We need to do surgery on pickles created with earlier versions of
# piff when using piff version 1.4 or later.
if Version(piff.version) >= Version("1.4"):
if not hasattr(piffResult, "_num"):
piffResult._num = None
piffResult.model._num = None
piffResult.model._fit_flux = None
piffResult.interp._num = None
return PiffPsf(width, height, piffResult)

# ImagePsf overrides

Expand Down
Binary file added tests/data/exp.fits
Binary file not shown.
Binary file added tests/data/psfIm_-200_30.fits
Binary file not shown.
Binary file added tests/data/psfIm_0_0.fits
Binary file not shown.
Binary file added tests/data/psfIm_10_100.fits
Binary file not shown.
Binary file added tests/data/psfIm_nan.fits
Binary file not shown.
47 changes: 45 additions & 2 deletions tests/test_psf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from pathlib import Path

import galsim # noqa: F401
import piff
import unittest
import numpy as np
import copy
Expand All @@ -38,7 +41,7 @@
from lsst.meas.base import SingleFrameMeasurementTask
from lsst.meas.extensions.piff.piffPsfDeterminer import PiffPsfDeterminerConfig, PiffPsfDeterminerTask
from lsst.meas.extensions.piff.piffPsfDeterminer import _validateGalsimInterpolant
from packaging import version
from packaging.version import Version


def psfVal(ix, iy, x, y, sigma1, sigma2, b):
Expand Down Expand Up @@ -424,6 +427,46 @@ def checkPiffDeterminer(self, **kwargs):
newPsf.computeImage(newPsf.getAveragePosition())
)

def testReadOldPiffVersions(self):
"""Test we can read psfs serialized with older versions of Piff."""
point_names = [
(geom.Point2D(0, 0), "psfIm_0_0.fits"),
(geom.Point2D(10, 100), "psfIm_10_100.fits"),
(geom.Point2D(-200, 30), "psfIm_-200_30.fits"),
(geom.Point2D(float("nan")), "psfIm_nan.fits"),
]

if False: # Documenting block that created the test data...
# Specifically interested in the case where the PSF is created with
# piff older than v1.4
assert Version(piff.version) < Version("1.4")

self.setupDeterminer()
stars = self.starSelector.run(self.catalog, exposure=self.exposure)
psfCandidateList = self.makePsfCandidates.run(
stars.sourceCat,
exposure=self.exposure
).psfCandidates
psf, _ = self.psfDeterminer.determinePsf(
self.exposure,
psfCandidateList,
)
self.exposure.setPsf(psf)

path = Path(__file__).parent / "data" / "exp.fits"
self.exposure.writeFits(str(path))
for point, name in point_names:
psfIm = psf.computeImage(point)
psfIm.writeFits(str(path.with_name(name)))

path = Path(__file__).parent / "data" / "exp.fits"
exposure = afwImage.ExposureF(str(path))
for point, name in point_names:
self.assertImagesAlmostEqual(
afwImage.ImageF(str(path.with_name(name))),
exposure.getPsf().computeImage(point)
)

def testPiffDeterminer_default(self):
"""Test piff with the default config."""
self.checkPiffDeterminer()
Expand Down Expand Up @@ -468,7 +511,7 @@ def testPiffDeterminer_skyCoords_with_rotation(self, angle_degrees):
self.checkPiffDeterminer(useCoordinates='sky', kernelSize=35)

# TODO: Merge this case with the above in DM-44467.
@unittest.skipUnless(version.parse(galsim.version) >= version.parse("2.5.2"),
@unittest.skipUnless(Version(galsim.version) >= Version("2.5.2"),
reason="Requires GalSim >= 2.5.2",
)
def testPiffDeterminer_skyCoords_rot45(self):
Expand Down

0 comments on commit cd45df2

Please sign in to comment.