Skip to content

Commit

Permalink
Refactor ampOffset correction task
Browse files Browse the repository at this point in the history
  • Loading branch information
enourbakhsh committed Dec 5, 2023
1 parent ed8ae65 commit 84b0f06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
28 changes: 20 additions & 8 deletions python/lsst/ip/isr/ampOffset.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,24 @@ def run(self, exposure):
ampDims = [amp.getBBox().getDimensions() for amp in amps]
if not all(dim == ampDims[0] for dim in ampDims):
raise RuntimeError("All amps should have the same geometry.")
else:
# The zeroth amp is representative of all amps in the detector.
self.ampDims = ampDims[0]
# Determine amplifier geometry.
# ampWidths = {amp.getBBox().getWidth() for amp in amps}
# ampHeights = {amp.getBBox().getHeight() for amp in amps}
ampAreas = {amp.getBBox().getArea() for amp in amps}
if len(ampAreas) > 1:
raise NotImplementedError(
"Amp offset correction is not yet implemented for detectors with differing amp sizes."
)

# Assuming all the amps have the same geometry.
self.shortAmpSide = np.min(ampDims[0])

# TODO: Double check that this is true.
assert self.config.ampEdgeWidth < self.shortAmpSide - 2 * self.config.ampEdgeInset

# Fit and subtract background.
if self.config.doBackground:
maskedImage = exp.getMaskedImage()
Expand Down Expand Up @@ -193,13 +207,6 @@ def run(self, exposure):
"config to be 1 or less and rerun."
)

# Determine amplifier geometry.
ampAreas = {amp.getBBox().getArea() for amp in amps}
if len(ampAreas) > 1:
raise NotImplementedError(
"Amp offset correction is not yet implemented for detectors with differing amp sizes."
)

# Obtain association and offset matrices.
A, sides = self.getAmpAssociations(amps)
B = self.getAmpOffsets(im, amps, A, sides)
Expand Down Expand Up @@ -275,9 +282,14 @@ def getAmpAssociations(self, amps):
ampAssociations = np.zeros((nAmps, nAmps), dtype=int)
ampSides = np.full_like(ampAssociations, -1)

# Dictionary mapping side numbers to interface lengths.
interfaceLengthLookup = {i: self.ampDims[i % 2] for i in range(4)}
# interfaceLengthLookup = {i: 1 for i in range(4)} # For no weights!

for ampId in ampIds.ravel():
neighbors, sides = self.getNeighbors(ampIds, ampId)
ampAssociations[ampId, neighbors] = -1
interfaceWeights = np.array([1 / interfaceLengthLookup[side] for side in sides])
ampAssociations[ampId, neighbors] = -1 * interfaceWeights
ampSides[ampId, neighbors] = sides
ampAssociations[ampId, ampId] = -ampAssociations[ampId].sum()

Expand Down
3 changes: 3 additions & 0 deletions tests/test_ampOffset.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def testAmpOffset(self):
config = AmpOffsetConfig()
config.doBackground = False
config.doDetection = False
config.ampEdgeWidth = 12
task = AmpOffsetTask(config=config)
pedestals = task.run(exp).pedestals
self.assertEqual(np.sum(exp.image.array), 0)
Expand All @@ -94,6 +95,7 @@ def testAmpOffsetWithBackground(self):
config = AmpOffsetConfig()
config.doBackground = True
config.doDetection = True
config.ampEdgeWidth = 12
task = AmpOffsetTask(config=config)
pedestals = task.run(exp).pedestals
nAmps = len(amps)
Expand All @@ -117,6 +119,7 @@ def testAmpOffsetWithRampBackground(self):
config = AmpOffsetConfig()
config.doBackground = True
config.doDetection = True
config.ampEdgeWidth = 12
task = AmpOffsetTask(config=config)
pedestals = task.run(exp).pedestals
nAmps = len(amps)
Expand Down

0 comments on commit 84b0f06

Please sign in to comment.