126 """Calculate amp offset values, determine corrective pedestals for each
127 amp, and update the input exposure in-place.
131 exposure: `lsst.afw.image.Exposure`
132 Exposure to be corrected for amp offsets.
136 exp = exposure.clone()
137 bitMask = exp.mask.getPlaneBitMask(self.background.config.ignoredPixelMask)
138 amps = exp.getDetector().getAmplifiers()
141 ampDims = [amp.getBBox().getDimensions()
for amp
in amps]
142 if not all(dim == ampDims[0]
for dim
in ampDims):
143 raise RuntimeError(
"All amps should have the same geometry.")
149 if self.config.doBackground:
150 maskedImage = exp.getMaskedImage()
152 nX = exp.getWidth() // (self.
shortAmpSide * self.config.backgroundFractionSample) + 1
153 nY = exp.getHeight() // (self.
shortAmpSide * self.config.backgroundFractionSample) + 1
159 bg = self.background.fitBackground(maskedImage, nx=int(nX), ny=int(nY))
160 bgImage = bg.getImageF(self.background.config.algorithm, self.background.config.undersampleStyle)
161 maskedImage -= bgImage
164 if self.config.doDetection:
165 schema = SourceTable.makeMinimalSchema()
166 table = SourceTable.make(schema)
171 _ = self.detection.
run(table=table, exposure=exp, sigma=2)
174 if (exp.mask.array & bitMask).all():
176 "All pixels masked: cannot calculate any amp offset corrections. All pedestals are being set "
179 pedestals = np.zeros(len(amps))
183 im.array[(exp.mask.array & bitMask) > 0] = np.nan
185 if self.config.ampEdgeWindowFrac > 1:
187 f
"The specified fraction (`ampEdgeWindowFrac`={self.config.ampEdgeWindowFrac}) of the "
188 "edge length exceeds 1. This leads to complications downstream, after convolution in "
189 "the `getSideAmpOffset()` method. Please modify the `ampEdgeWindowFrac` value in the "
190 "config to be 1 or less and rerun."
194 ampAreas = {amp.getBBox().getArea()
for amp
in amps}
195 if len(ampAreas) > 1:
196 raise NotImplementedError(
197 "Amp offset correction is not yet implemented for detectors with differing amp sizes."
206 pedestals = np.nan_to_num(np.linalg.lstsq(A, B, rcond=
None)[0])
208 metadata = exposure.getMetadata()
209 for amp, pedestal
in zip(amps, pedestals):
210 ampIm = exposure.image[amp.getBBox()].array
212 ampName = amp.getName()
214 f
"LSST ISR AMPOFFSET PEDESTAL {ampName}",
216 f
"Pedestal level subtracted from amp {ampName}",
218 self.log.info(f
"amp pedestal values: {', '.join([f'{x:.4f}' for x in pedestals])}")
220 return Struct(pedestals=pedestals)
223 """Determine amp geometry and amp associations from a list of
226 Parse an input list of amplifiers to determine the layout of amps
227 within a detector, and identify all amp sides (i.e., the
228 horizontal and vertical junctions between amps).
230 Returns a matrix with a shape corresponding to the geometry of the amps
235 amps : `list` [`lsst.afw.cameraGeom.Amplifier`]
236 List of amplifier objects used to deduce associations.
240 ampAssociations : `numpy.ndarray`
241 An N x N matrix (N = number of amplifiers) that illustrates the
242 connections between amplifiers within the detector layout. Each row
243 and column index corresponds to the ampIds of a specific pair of
244 amplifiers, and the matrix elements indicate their associations as
247 -1: Association exists (direction specified in the ampSides matrix)
248 n >= 1: Diagonal elements indicate the number of neighboring
249 amplifiers for the corresponding ampId==row==column number.
251 ampSides : `numpy.ndarray`
252 An N x N matrix (N = the number of amplifiers) representing the amp
253 side information corresponding to the `ampAssociations`
254 matrix. The elements are integers defined as below:
255 -1: No side due to no association or the same amp (diagonals)
256 0: Side on the bottom
261 xCenters = [amp.getBBox().getCenterX()
for amp
in amps]
262 yCenters = [amp.getBBox().getCenterY()
for amp
in amps]
263 xIndices = np.ceil(xCenters / np.min(xCenters) / 2).astype(int) - 1
264 yIndices = np.ceil(yCenters / np.min(yCenters) / 2).astype(int) - 1
267 ampIds = np.zeros((len(set(yIndices)), len(set(xIndices))), dtype=int)
269 for ampId, xIndex, yIndex
in zip(np.arange(nAmps), xIndices, yIndices):
270 ampIds[yIndex, xIndex] = ampId
272 ampAssociations = np.zeros((nAmps, nAmps), dtype=int)
273 ampSides = np.full_like(ampAssociations, -1)
275 for ampId
in ampIds.ravel():
277 ampAssociations[ampId, neighbors] = -1
278 ampSides[ampId, neighbors] = sides
279 ampAssociations[ampId, ampId] = -ampAssociations[ampId].sum()
281 if ampAssociations.sum() != 0:
282 raise RuntimeError(
"The `ampAssociations` array does not sum to zero.")
284 if not np.all(ampAssociations == ampAssociations.T):
285 raise RuntimeError(
"The `ampAssociations` is not symmetric about the diagonal.")
287 self.log.debug(
"amp associations:\n%s", ampAssociations)
288 self.log.debug(
"amp sides:\n%s", ampSides)
290 return ampAssociations, ampSides
328 """Calculate the amp offsets for all amplifiers.
332 im : `lsst.afw.image._image.ImageF`
333 Amplifier image to extract data from.
334 amps : `list` [`lsst.afw.cameraGeom.Amplifier`]
335 List of amplifier objects.
336 associations : numpy.ndarray
337 An N x N matrix containing amp association information, where N is
338 the number of amplifiers.
339 sides : numpy.ndarray
340 An N x N matrix containing amp side information, where N is the
341 number of amplifiers.
345 ampsOffsets : `numpy.ndarray`
346 1D float array containing the calculated amp offsets for all
349 ampsOffsets = np.zeros(len(amps))
351 interfaceOffsetLookup = {}
352 for ampId, ampAssociations
in enumerate(associations):
353 ampNeighbors = np.ravel(np.where(ampAssociations < 0))
354 for ampNeighbor
in ampNeighbors:
355 ampSide = sides[ampId][ampNeighbor]
356 edgeA = ampsEdges[ampId][ampSide]
357 edgeB = ampsEdges[ampNeighbor][(ampSide + 2) % 4]
358 if ampId < ampNeighbor:
360 interfaceOffsetLookup[f
"{ampId}{ampNeighbor}"] = interfaceOffset
362 interfaceOffset = -interfaceOffsetLookup[f
"{ampNeighbor}{ampId}"]
363 ampsOffsets[ampId] += interfaceOffset
367 """Calculate the amp edges for all amplifiers.
371 im : `lsst.afw.image._image.ImageF`
372 Amplifier image to extract data from.
373 amps : `list` [`lsst.afw.cameraGeom.Amplifier`]
374 List of amplifier objects.
375 ampSides : `numpy.ndarray`
376 An N x N matrix containing amp side information, where N is the
377 number of amplifiers.
381 ampEdges : `dict` [`int`, `dict` [`int`, `numpy.ndarray`]]
382 A dictionary containing amp edge(s) for each amplifier,
383 corresponding to one or more potential sides, where each edge is
384 associated with a side. The outer dictionary has integer keys
385 representing amplifier IDs, and the inner dictionary has integer
386 keys representing side IDs for each amplifier and values that are
387 1D arrays of floats representing the 1D medianified strips from the
388 amp image, referred to as "amp edge":
389 {ampID: {sideID: numpy.ndarray}, ...}
391 ampEdgeOuter = self.config.ampEdgeInset + self.config.ampEdgeWidth
394 0: (slice(-ampEdgeOuter, -self.config.ampEdgeInset), slice(
None)),
395 1: (slice(
None), slice(-ampEdgeOuter, -self.config.ampEdgeInset)),
396 2: (slice(self.config.ampEdgeInset, ampEdgeOuter), slice(
None)),
397 3: (slice(
None), slice(self.config.ampEdgeInset, ampEdgeOuter)),
399 for ampId, (amp, ampSides)
in enumerate(zip(amps, ampSides)):
401 ampIm = im[amp.getBBox()].array
403 for ampSide
in ampSides:
406 strip = ampIm[slice_map[ampSide]]
408 with warnings.catch_warnings():
409 warnings.filterwarnings(
"ignore",
r"All-NaN (slice|axis) encountered")
410 ampEdges[ampId][ampSide] = np.nanmedian(strip, axis=ampSide % 2)
414 """Calculate the amp offset for a given interface between two
420 ID of the first amplifier.
422 ID of the second amplifier.
423 edgeA : numpy.ndarray
424 Amp edge for the first amplifier.
425 edgeB : numpy.ndarray
426 Amp edge for the second amplifier.
430 interfaceOffset : float
431 The calculated amp offset value for the given interface between
434 interfaceId = f
"{ampIdA}{ampIdB}"
438 edgeDiff = edgeA - edgeB
439 window = int(self.config.ampEdgeWindowFrac * len(edgeDiff))
441 edgeDiffSum = np.convolve(np.nan_to_num(edgeDiff), np.ones(window),
"same")
442 edgeDiffNum = np.convolve(~np.isnan(edgeDiff), np.ones(window),
"same")
443 edgeDiffAvg = edgeDiffSum / np.clip(edgeDiffNum, 1,
None)
444 edgeDiffAvg[np.isnan(edgeDiff)] = np.nan
446 interfaceOffset = makeStatistics(edgeDiffAvg, MEANCLIP, sctrl).getValue()
450 ampEdgeGoodFrac = 1 - (np.sum(np.isnan(edgeDiffAvg)) / len(edgeDiffAvg))
451 minFracFail = ampEdgeGoodFrac < self.config.ampEdgeMinFrac
452 maxOffsetFail = np.abs(interfaceOffset) > self.config.ampEdgeMaxOffset
453 if minFracFail
or maxOffsetFail:
456 f
"The fraction of unmasked pixels for amp interface {interfaceId} is below the threshold "
457 f
"({self.config.ampEdgeMinFrac}) or the absolute offset value exceeds the limit "
458 f
"({self.config.ampEdgeMaxOffset} ADU). Setting the interface offset to 0."
461 f
"amp interface {interfaceId} : "
462 f
"viable edge difference frac = {ampEdgeGoodFrac}, "
463 f
"interface offset = {interfaceOffset:.3f}"
465 return interfaceOffset