Skip to content

Commit

Permalink
Merge pull request #669 from lsst/tickets/DM-37085
Browse files Browse the repository at this point in the history
DM-37085: Add copyWith method to VisitInfo
  • Loading branch information
fred3m committed Nov 29, 2022
2 parents abe93b2 + 69d7e23 commit 2409686
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
98 changes: 98 additions & 0 deletions python/lsst/afw/image/_visitInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,110 @@
"""

from lsst.utils.deprecated import deprecate_pybind11
from lsst.utils import continueClass
from ._imageLib import VisitInfo


__all__ = []


@continueClass
class VisitInfo: # noqa: F811
def copyWith(
self,
exposureId=None,
exposureTime=None,
darkTime=None,
date=None,
ut1=None,
era=None,
boresightRaDec=None,
boresightAzAlt=None,
boresightAirmass=None,
boresightRotAngle=None,
rotType=None,
observatory=None,
weather=None,
instrumentLabel=None,
id=None,
focusZ=None,
observationType=None,
scienceProgram=None,
observationReason=None,
object=None,
hasSimulatedContent=None,
):
if exposureId is None:
# Note: exposureId is deprecated and `VisitInfo` no longer contains
# an `exposureId` property, so we use the getter until
# this is removed in DM-32138.
exposureId = self.getExposureId()
if exposureTime is None:
exposureTime = self.exposureTime
if darkTime is None:
darkTime = self.darkTime
if date is None:
date = self.date
if ut1 is None:
ut1 = self.ut1
if era is None:
era = self.era
if boresightRaDec is None:
boresightRaDec = self.boresightRaDec
if boresightAzAlt is None:
boresightAzAlt = self.boresightAzAlt
if boresightAirmass is None:
boresightAirmass = self.boresightAirmass
if boresightRotAngle is None:
boresightRotAngle = self.boresightRotAngle
if rotType is None:
rotType = self.rotType
if observatory is None:
observatory = self.observatory
if weather is None:
weather = self.weather
if instrumentLabel is None:
instrumentLabel = self.instrumentLabel
if id is None:
id = self.id
if focusZ is None:
focusZ = self.focusZ
if observationType is None:
observationType = self.observationType
if scienceProgram is None:
scienceProgram = self.scienceProgram
if observationReason is None:
observationReason = self.observationReason
if object is None:
object = self.object
if hasSimulatedContent is None:
hasSimulatedContent = self.hasSimulatedContent

return VisitInfo(
exposureId=exposureId,
exposureTime=exposureTime,
darkTime=darkTime,
date=date,
ut1=ut1,
era=era,
boresightRaDec=boresightRaDec,
boresightAzAlt=boresightAzAlt,
boresightAirmass=boresightAirmass,
boresightRotAngle=boresightRotAngle,
rotType=rotType,
observatory=observatory,
weather=weather,
instrumentLabel=instrumentLabel,
id=id,
focusZ=focusZ,
observationType=observationType,
scienceProgram=scienceProgram,
observationReason=observationReason,
object=object,
hasSimulatedContent=hasSimulatedContent,
)


VisitInfo.getExposureId = deprecate_pybind11(
VisitInfo.getExposureId,
reason="Replaced by VisitInfo.id for full focal plane identifiers and by ExposureInfo.id for "
Expand Down
51 changes: 51 additions & 0 deletions tests/test_visitInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,57 @@ def testValueConstructor_data2(self):
self._testValueConstructor(self.data2, self.localEra2, self.hourAngle2)
self._testProperties(self.data2, self.localEra2, self.hourAngle2)

def testCopyWith(self):
visitInfo1 = makeVisitInfo(self.data1)
visitInfo2 = makeVisitInfo(self.data2)

updateFields1 = [
"exposureTime",
"darkTime",
"date",
"ut1",
"era",
"boresightRaDec",
"boresightAzAlt",
"boresightAirmass",
"boresightRotAngle",
]

updateFields2 = [
"rotType",
"observatory",
"weather",
"instrumentLabel",
"id",
"focusZ",
"observationType",
"scienceProgram",
"observationReason",
"object",
"hasSimulatedContent",
]

kwargs1 = {k: getattr(visitInfo2, k) for k in updateFields1}
kwargs2 = {k: getattr(visitInfo2, k) for k in updateFields2}

newVisit1 = visitInfo1.copyWith(**kwargs1)
newVisit2 = visitInfo1.copyWith(**kwargs2)

for field in updateFields1:
self.assertEqual(getattr(newVisit1, field), getattr(visitInfo2, field))
self.assertEqual(getattr(newVisit2, field), getattr(visitInfo1, field))

for field in updateFields2:
self.assertEqual(getattr(newVisit1, field), getattr(visitInfo1, field))
self.assertEqual(getattr(newVisit2, field), getattr(visitInfo2, field))

# Test the deprecated exposureId.
# This code can be removed with DM-32138.
deprecatedVisit = visitInfo1.copyWith(exposureId=3)
self.assertEqual(deprecatedVisit.getExposureId(), 3)
deprecatedCopy = deprecatedVisit.copyWith(**kwargs1)
self.assertEqual(deprecatedCopy.getExposureId(), 3)

def testTablePersistence(self):
"""Test that VisitInfo can be round-tripped with current code.
"""
Expand Down

0 comments on commit 2409686

Please sign in to comment.