LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+f5e6047307,21.0.0-11-g2b59f77+a9c1acf22d,21.0.0-11-ga42c5b2+86977b0b17,21.0.0-12-gf4ce030+76814010d2,21.0.0-13-g1721dae+760e7a6536,21.0.0-13-g3a573fe+768d78a30a,21.0.0-15-g5a7caf0+f21cbc5713,21.0.0-16-g0fb55c1+b60e2d390c,21.0.0-19-g4cded4ca+71a93a33c0,21.0.0-2-g103fe59+bb20972958,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+3ad5d60fb1,21.0.0-2-g7f82c8f+8babb168e8,21.0.0-2-g8f08a60+06509c8b61,21.0.0-2-g8faa9b5+616205b9df,21.0.0-2-ga326454+8babb168e8,21.0.0-2-gde069b7+5e4aea9c2f,21.0.0-2-gecfae73+1d3a86e577,21.0.0-2-gfc62afb+3ad5d60fb1,21.0.0-25-g1d57be3cd+e73869a214,21.0.0-3-g357aad2+ed88757d29,21.0.0-3-g4a4ce7f+3ad5d60fb1,21.0.0-3-g4be5c26+3ad5d60fb1,21.0.0-3-g65f322c+e0b24896a3,21.0.0-3-g7d9da8d+616205b9df,21.0.0-3-ge02ed75+a9c1acf22d,21.0.0-4-g591bb35+a9c1acf22d,21.0.0-4-g65b4814+b60e2d390c,21.0.0-4-gccdca77+0de219a2bc,21.0.0-4-ge8a399c+6c55c39e83,21.0.0-5-gd00fb1e+05fce91b99,21.0.0-6-gc675373+3ad5d60fb1,21.0.0-64-g1122c245+4fb2b8f86e,21.0.0-7-g04766d7+cd19d05db2,21.0.0-7-gdf92d54+04719a4bac,21.0.0-8-g5674e7b+d1bd76f71f,master-gac4afde19b+a9c1acf22d,w.2021.13
LSST Data Management Base Package
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask Class Reference
Inheritance diagram for lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask:
lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelMapper

Public Member Functions

def __init__ (self, *args, **kwargs)
 
def computeVarianceMean (self, exposure)
 
def run (self, scienceExposure, templateExposure, subtractedExposure, psfMatchingKernel, preConvKernel=None, xcen=None, ycen=None, svar=None, tvar=None, templateMatched=True)
 
def computeCommonShape (self, *shapes)
 
def computeCorrection (self, kappa, svar, tvar, preConvArr=None)
 
def computeCorrectedDiffimPsf (self, corrft, psfOld)
 
def computeCorrectedImage (self, corrft, imgOld)
 

Static Public Member Functions

def padCenterOriginArray (A, tuple newShape, useInverse=False)
 

Public Attributes

 statsControl
 
 freqSpaceShape
 

Static Public Attributes

 ConfigClass = DecorrelateALKernelConfig
 

Detailed Description

Decorrelate the effect of convolution by Alard-Lupton matching kernel in image difference

Notes
-----

Pipe-task that removes the neighboring-pixel covariance in an
image difference that are added when the template image is
convolved with the Alard-Lupton PSF matching kernel.

The image differencing pipeline task @link
ip.diffim.psfMatch.PsfMatchTask PSFMatchTask@endlink and @link
ip.diffim.psfMatch.PsfMatchConfigAL PSFMatchConfigAL@endlink uses
the Alard and Lupton (1998) method for matching the PSFs of the
template and science exposures prior to subtraction. The
Alard-Lupton method identifies a matching kernel, which is then
(typically) convolved with the template image to perform PSF
matching. This convolution has the effect of adding covariance
between neighboring pixels in the template image, which is then
added to the image difference by subtraction.

The pixel covariance may be corrected by whitening the noise of
the image difference. This task performs such a decorrelation by
computing a decorrelation kernel (based upon the A&L matching
kernel and variances in the template and science images) and
convolving the image difference with it. This process is described
in detail in [DMTN-021](http://dmtn-021.lsst.io).

This task has no standalone example, however it is applied as a
subtask of pipe.tasks.imageDifference.ImageDifferenceTask.

Definition at line 53 of file imageDecorrelation.py.

Constructor & Destructor Documentation

◆ __init__()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.__init__ (   self,
args,
**  kwargs 
)
Create the image decorrelation Task

Parameters
----------
args :
    arguments to be passed to ``lsst.pipe.base.task.Task.__init__``
kwargs :
    keyword arguments to be passed to ``lsst.pipe.base.task.Task.__init__``

Reimplemented in lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelMapper.

Definition at line 87 of file imageDecorrelation.py.

87  def __init__(self, *args, **kwargs):
88  """Create the image decorrelation Task
89 
90  Parameters
91  ----------
92  args :
93  arguments to be passed to ``lsst.pipe.base.task.Task.__init__``
94  kwargs :
95  keyword arguments to be passed to ``lsst.pipe.base.task.Task.__init__``
96  """
97  pipeBase.Task.__init__(self, *args, **kwargs)
98 
99  self.statsControl = afwMath.StatisticsControl()
100  self.statsControl.setNumSigmaClip(3.)
101  self.statsControl.setNumIter(3)
102  self.statsControl.setAndMask(afwImage.Mask.getPlaneBitMask(self.config.ignoreMaskPlanes))
103 
Pass parameters to a Statistics object.
Definition: Statistics.h:93

Member Function Documentation

◆ computeCommonShape()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.computeCommonShape (   self,
shapes 
)
Calculate the common shape for FFT operations. Set `self.freqSpaceShape`
internally.

Parameters
----------
shapes : one or more `tuple` of `int`
    Shapes of the arrays. All must have the same dimensionality.
    At least one shape must be provided.

Returns
-------
None.

Notes
-----
For each dimension, gets the smallest even number greater than or equal to
`N1+N2-1` where `N1` and `N2` are the two largest values.
In case of only one shape given, rounds up to even each dimension value.

Definition at line 300 of file imageDecorrelation.py.

300  def computeCommonShape(self, *shapes):
301  """Calculate the common shape for FFT operations. Set `self.freqSpaceShape`
302  internally.
303 
304  Parameters
305  ----------
306  shapes : one or more `tuple` of `int`
307  Shapes of the arrays. All must have the same dimensionality.
308  At least one shape must be provided.
309 
310  Returns
311  -------
312  None.
313 
314  Notes
315  -----
316  For each dimension, gets the smallest even number greater than or equal to
317  `N1+N2-1` where `N1` and `N2` are the two largest values.
318  In case of only one shape given, rounds up to even each dimension value.
319  """
320  S = np.array(shapes, dtype=int)
321  if len(shapes) > 2:
322  S.sort(axis=0)
323  S = S[-2:]
324  if len(shapes) > 1:
325  commonShape = np.sum(S, axis=0) - 1
326  else:
327  commonShape = S[0]
328  commonShape[commonShape % 2 != 0] += 1
329  self.freqSpaceShape = tuple(commonShape)
330  self.log.info(f"Common frequency space shape {self.freqSpaceShape}")
331 

◆ computeCorrectedDiffimPsf()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.computeCorrectedDiffimPsf (   self,
  corrft,
  psfOld 
)
Compute the (decorrelated) difference image's new PSF.

Parameters
----------
corrft : `numpy.ndarray`
    The frequency space representation of the correction calculated by
    `computeCorrection`. Shape must be `self.freqSpaceShape`.
psfOld : `numpy.ndarray`
    The psf of the difference image to be corrected.

Returns
-------
psfNew : `numpy.ndarray`
    The corrected psf, same shape as `psfOld`, sum normed to 1.

Notes
----
There is no algorithmic guarantee that the corrected psf can
meaningfully fit to the same size as the original one.

Definition at line 438 of file imageDecorrelation.py.

438  def computeCorrectedDiffimPsf(self, corrft, psfOld):
439  """Compute the (decorrelated) difference image's new PSF.
440 
441  Parameters
442  ----------
443  corrft : `numpy.ndarray`
444  The frequency space representation of the correction calculated by
445  `computeCorrection`. Shape must be `self.freqSpaceShape`.
446  psfOld : `numpy.ndarray`
447  The psf of the difference image to be corrected.
448 
449  Returns
450  -------
451  psfNew : `numpy.ndarray`
452  The corrected psf, same shape as `psfOld`, sum normed to 1.
453 
454  Notes
455  ----
456  There is no algorithmic guarantee that the corrected psf can
457  meaningfully fit to the same size as the original one.
458  """
459  psfShape = psfOld.shape
460  psfNew = self.padCenterOriginArray(psfOld, self.freqSpaceShape)
461  psfNew = np.fft.fft2(psfNew)
462  psfNew *= corrft
463  psfNew = np.fft.ifft2(psfNew)
464  psfNew = psfNew.real
465  psfNew = self.padCenterOriginArray(psfNew, psfShape, useInverse=True)
466  psfNew = psfNew/psfNew.sum()
467  return psfNew
468 

◆ computeCorrectedImage()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.computeCorrectedImage (   self,
  corrft,
  imgOld 
)
Compute the decorrelated difference image.

Parameters
----------
corrft : `numpy.ndarray`
    The frequency space representation of the correction calculated by
    `computeCorrection`. Shape must be `self.freqSpaceShape`.
imgOld : `numpy.ndarray`
    The difference image to be corrected.

Returns
-------
imgNew : `numpy.ndarray`
    The corrected image, same size as the input.

Definition at line 469 of file imageDecorrelation.py.

469  def computeCorrectedImage(self, corrft, imgOld):
470  """Compute the decorrelated difference image.
471 
472  Parameters
473  ----------
474  corrft : `numpy.ndarray`
475  The frequency space representation of the correction calculated by
476  `computeCorrection`. Shape must be `self.freqSpaceShape`.
477  imgOld : `numpy.ndarray`
478  The difference image to be corrected.
479 
480  Returns
481  -------
482  imgNew : `numpy.ndarray`
483  The corrected image, same size as the input.
484  """
485  expShape = imgOld.shape
486  imgNew = np.copy(imgOld)
487  filtInf = np.isinf(imgNew)
488  filtNan = np.isnan(imgNew)
489  imgNew[filtInf] = np.nan
490  imgNew[filtInf | filtNan] = np.nanmean(imgNew)
491  imgNew = self.padCenterOriginArray(imgNew, self.freqSpaceShape)
492  imgNew = np.fft.fft2(imgNew)
493  imgNew *= corrft
494  imgNew = np.fft.ifft2(imgNew)
495  imgNew = imgNew.real
496  imgNew = self.padCenterOriginArray(imgNew, expShape, useInverse=True)
497  imgNew[filtNan] = np.nan
498  imgNew[filtInf] = np.inf
499  return imgNew
500 
501 

◆ computeCorrection()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.computeCorrection (   self,
  kappa,
  svar,
  tvar,
  preConvArr = None 
)
Compute the Lupton decorrelation post-convolution kernel for decorrelating an
image difference, based on the PSF-matching kernel.

Parameters
----------
kappa : `numpy.ndarray`
    A matching kernel 2-d numpy.array derived from Alard & Lupton PSF matching.
svar : `float`
    Average variance of science image used for PSF matching.
tvar : `float`
    Average variance of the template (matched) image used for PSF matching.
preConvArr : `numpy.ndarray`, optional
    If not None, then pre-filtering was applied
    to science exposure, and this is the pre-convolution kernel.

Returns
-------
corrft : `numpy.ndarray` of `float`
    The frequency space representation of the correction. The array is real (dtype float).
    Shape is `self.freqSpaceShape`.

Notes
-----
The maximum correction factor converges to `sqrt(tvar/svar)` towards high frequencies.
This should be a plausible value.

Definition at line 380 of file imageDecorrelation.py.

380  def computeCorrection(self, kappa, svar, tvar, preConvArr=None):
381  """Compute the Lupton decorrelation post-convolution kernel for decorrelating an
382  image difference, based on the PSF-matching kernel.
383 
384  Parameters
385  ----------
386  kappa : `numpy.ndarray`
387  A matching kernel 2-d numpy.array derived from Alard & Lupton PSF matching.
388  svar : `float`
389  Average variance of science image used for PSF matching.
390  tvar : `float`
391  Average variance of the template (matched) image used for PSF matching.
392  preConvArr : `numpy.ndarray`, optional
393  If not None, then pre-filtering was applied
394  to science exposure, and this is the pre-convolution kernel.
395 
396  Returns
397  -------
398  corrft : `numpy.ndarray` of `float`
399  The frequency space representation of the correction. The array is real (dtype float).
400  Shape is `self.freqSpaceShape`.
401 
402  Notes
403  -----
404  The maximum correction factor converges to `sqrt(tvar/svar)` towards high frequencies.
405  This should be a plausible value.
406  """
407  kSum = np.sum(kappa)
408  kappa = self.padCenterOriginArray(kappa, self.freqSpaceShape)
409  kft = np.fft.fft2(kappa)
410  kftAbsSq = np.real(np.conj(kft) * kft)
411  # If there is no pre-convolution kernel, use placeholder scalars
412  if preConvArr is None:
413  preSum = 1.
414  preAbsSq = 1.
415  else:
416  preSum = np.sum(preConvArr)
417  preConvArr = self.padCenterOriginArray(preConvArr, self.freqSpaceShape)
418  preK = np.fft.fft2(preConvArr)
419  preAbsSq = np.real(np.conj(preK)*preK)
420 
421  denom = svar * preAbsSq + tvar * kftAbsSq
422  # Division by zero protection, though we don't expect to hit it
423  # (rather we'll have numerical noise)
424  tiny = np.finfo(kftAbsSq.dtype).tiny * 1000.
425  flt = denom < tiny
426  sumFlt = np.sum(flt)
427  if sumFlt > 0:
428  self.log.warnf("Avoid zero division. Skip decorrelation "
429  "at {} divergent frequencies.", sumFlt)
430  denom[flt] = 1.
431  kft = np.sqrt((svar * preSum*preSum + tvar * kSum*kSum) / denom)
432  # Don't do any correction at these frequencies
433  # the difference image should be close to zero anyway, so can't be decorrelated
434  if sumFlt > 0:
435  kft[flt] = 1.
436  return kft
437 
def warnf(fmt, *args, **kwargs)

◆ computeVarianceMean()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.computeVarianceMean (   self,
  exposure 
)

Definition at line 104 of file imageDecorrelation.py.

104  def computeVarianceMean(self, exposure):
105  statObj = afwMath.makeStatistics(exposure.getMaskedImage().getVariance(),
106  exposure.getMaskedImage().getMask(),
107  afwMath.MEANCLIP, self.statsControl)
108  var = statObj.getValue(afwMath.MEANCLIP)
109  return var
110 
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition: Statistics.h:354

◆ padCenterOriginArray()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.padCenterOriginArray (   A,
tuple  newShape,
  useInverse = False 
)
static
Zero pad an image where the origin is at the center and replace the
origin to the corner as required by the periodic input of FFT. Implement also
the inverse operation, crop the padding and re-center data.

Parameters
----------
A : `numpy.ndarray`
    An array to copy from.
newShape : `tuple` of `int`
    The dimensions of the resulting array. For padding, the resulting array
    must be larger than A in each dimension. For the inverse operation this
    must be the original, before padding size of the array.
useInverse : bool, optional
    Selector of forward, add padding, operation (False)
    or its inverse, crop padding, operation (True).

Returns
-------
R : `numpy.ndarray`
    The padded or unpadded array with shape of `newShape` and the same dtype as A.

Notes
-----
For odd dimensions, the splitting is rounded to
put the center pixel into the new corner origin (0,0). This is to be consistent
e.g. for a dirac delta kernel that is originally located at the center pixel.

Definition at line 333 of file imageDecorrelation.py.

333  def padCenterOriginArray(A, newShape: tuple, useInverse=False):
334  """Zero pad an image where the origin is at the center and replace the
335  origin to the corner as required by the periodic input of FFT. Implement also
336  the inverse operation, crop the padding and re-center data.
337 
338  Parameters
339  ----------
340  A : `numpy.ndarray`
341  An array to copy from.
342  newShape : `tuple` of `int`
343  The dimensions of the resulting array. For padding, the resulting array
344  must be larger than A in each dimension. For the inverse operation this
345  must be the original, before padding size of the array.
346  useInverse : bool, optional
347  Selector of forward, add padding, operation (False)
348  or its inverse, crop padding, operation (True).
349 
350  Returns
351  -------
352  R : `numpy.ndarray`
353  The padded or unpadded array with shape of `newShape` and the same dtype as A.
354 
355  Notes
356  -----
357  For odd dimensions, the splitting is rounded to
358  put the center pixel into the new corner origin (0,0). This is to be consistent
359  e.g. for a dirac delta kernel that is originally located at the center pixel.
360  """
361 
362  # The forward and inverse operations should round odd dimension halves at the opposite
363  # sides to get the pixels back to their original positions.
364  if not useInverse:
365  # Forward operation: First and second halves with respect to the axes of A.
366  firstHalves = [x//2 for x in A.shape]
367  secondHalves = [x-y for x, y in zip(A.shape, firstHalves)]
368  else:
369  # Inverse operation: Opposite rounding
370  secondHalves = [x//2 for x in newShape]
371  firstHalves = [x-y for x, y in zip(newShape, secondHalves)]
372 
373  R = np.zeros_like(A, shape=newShape)
374  R[-firstHalves[0]:, -firstHalves[1]:] = A[:firstHalves[0], :firstHalves[1]]
375  R[:secondHalves[0], -firstHalves[1]:] = A[-secondHalves[0]:, :firstHalves[1]]
376  R[:secondHalves[0], :secondHalves[1]] = A[-secondHalves[0]:, -secondHalves[1]:]
377  R[-firstHalves[0]:, :secondHalves[1]] = A[:firstHalves[0], -secondHalves[1]:]
378  return R
379 

◆ run()

def lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.run (   self,
  scienceExposure,
  templateExposure,
  subtractedExposure,
  psfMatchingKernel,
  preConvKernel = None,
  xcen = None,
  ycen = None,
  svar = None,
  tvar = None,
  templateMatched = True 
)
Perform decorrelation of an image difference exposure.

Decorrelates the diffim due to the convolution of the templateExposure with the
A&L PSF matching kernel. Currently can accept a spatially varying matching kernel but in
this case it simply uses a static kernel from the center of the exposure. The decorrelation
is described in [DMTN-021, Equation 1](http://dmtn-021.lsst.io/#equation-1), where
`exposure` is I_1; templateExposure is I_2; `subtractedExposure` is D(k);
`psfMatchingKernel` is kappa; and svar and tvar are their respective
variances (see below).

Parameters
----------
scienceExposure : `lsst.afw.image.Exposure`
    The original science exposure (before `preConvKernel` applied).
templateExposure : `lsst.afw.image.Exposure`
    The original template exposure warped into the science exposure dimensions.
subtractedExposure : `lsst.afw.iamge.Exposure`
    the subtracted exposure produced by
    `ip_diffim.ImagePsfMatchTask.subtractExposures()`. The `subtractedExposure` must
    inherit its PSF from `exposure`, see notes below.
psfMatchingKernel : `lsst.afw.detection.Psf`
    An (optionally spatially-varying) PSF matching kernel produced
    by `ip_diffim.ImagePsfMatchTask.subtractExposures()`.
preConvKernel : `lsst.afw.math.Kernel`, optional
    if not None, then the `scienceExposure` was pre-convolved with this kernel.
    Allowed only if ``templateMatched==True``.
xcen : `float`, optional
    X-pixel coordinate to use for computing constant matching kernel to use
    If `None` (default), then use the center of the image.
ycen : `float`, optional
    Y-pixel coordinate to use for computing constant matching kernel to use
    If `None` (default), then use the center of the image.
svar : `float`, optional
    Image variance for science image
    If `None` (default) then compute the variance over the entire input science image.
tvar : `float`, optional
    Image variance for template image
    If `None` (default) then compute the variance over the entire input template image.
templateMatched : `bool`, optional
    If True, the template exposure was matched (convolved) to the science exposure.
    See also notes below.

Returns
-------
result : `lsst.pipe.base.Struct`
    - ``correctedExposure`` : the decorrelated diffim

Notes
-----
The `subtractedExposure` is NOT updated. The returned `correctedExposure` has an updated but
spatially fixed PSF. It is calculated as the center of image PSF corrected by the center of
image matching kernel.

If ``templateMatched==True``, the templateExposure was matched (convolved)
to the ``scienceExposure`` by ``psfMatchingKernel``. Otherwise the ``scienceExposure``
was matched (convolved) by ``psfMatchingKernel``.

This task discards the variance plane of ``subtractedExposure`` and re-computes
it from the variance planes of ``scienceExposure`` and ``templateExposure``.
The image plane of ``subtractedExposure`` must be at the photometric level
set by the AL PSF matching in `ImagePsfMatchTask.subtractExposures`.
The assumptions about the photometric level are controlled by the
`templateMatched` option in this task.

Here we currently convert a spatially-varying matching kernel into a constant kernel,
just by computing it at the center of the image (tickets DM-6243, DM-6244).

We are also using a constant accross-the-image measure of sigma (sqrt(variance)) to compute
the decorrelation kernel.

TODO DM-23857 As part of the spatially varying correction implementation
consider whether returning a Struct is still necessary.

Definition at line 112 of file imageDecorrelation.py.

113  preConvKernel=None, xcen=None, ycen=None, svar=None, tvar=None, templateMatched=True):
114  """Perform decorrelation of an image difference exposure.
115 
116  Decorrelates the diffim due to the convolution of the templateExposure with the
117  A&L PSF matching kernel. Currently can accept a spatially varying matching kernel but in
118  this case it simply uses a static kernel from the center of the exposure. The decorrelation
119  is described in [DMTN-021, Equation 1](http://dmtn-021.lsst.io/#equation-1), where
120  `exposure` is I_1; templateExposure is I_2; `subtractedExposure` is D(k);
121  `psfMatchingKernel` is kappa; and svar and tvar are their respective
122  variances (see below).
123 
124  Parameters
125  ----------
126  scienceExposure : `lsst.afw.image.Exposure`
127  The original science exposure (before `preConvKernel` applied).
128  templateExposure : `lsst.afw.image.Exposure`
129  The original template exposure warped into the science exposure dimensions.
130  subtractedExposure : `lsst.afw.iamge.Exposure`
131  the subtracted exposure produced by
132  `ip_diffim.ImagePsfMatchTask.subtractExposures()`. The `subtractedExposure` must
133  inherit its PSF from `exposure`, see notes below.
134  psfMatchingKernel : `lsst.afw.detection.Psf`
135  An (optionally spatially-varying) PSF matching kernel produced
136  by `ip_diffim.ImagePsfMatchTask.subtractExposures()`.
137  preConvKernel : `lsst.afw.math.Kernel`, optional
138  if not None, then the `scienceExposure` was pre-convolved with this kernel.
139  Allowed only if ``templateMatched==True``.
140  xcen : `float`, optional
141  X-pixel coordinate to use for computing constant matching kernel to use
142  If `None` (default), then use the center of the image.
143  ycen : `float`, optional
144  Y-pixel coordinate to use for computing constant matching kernel to use
145  If `None` (default), then use the center of the image.
146  svar : `float`, optional
147  Image variance for science image
148  If `None` (default) then compute the variance over the entire input science image.
149  tvar : `float`, optional
150  Image variance for template image
151  If `None` (default) then compute the variance over the entire input template image.
152  templateMatched : `bool`, optional
153  If True, the template exposure was matched (convolved) to the science exposure.
154  See also notes below.
155 
156  Returns
157  -------
158  result : `lsst.pipe.base.Struct`
159  - ``correctedExposure`` : the decorrelated diffim
160 
161  Notes
162  -----
163  The `subtractedExposure` is NOT updated. The returned `correctedExposure` has an updated but
164  spatially fixed PSF. It is calculated as the center of image PSF corrected by the center of
165  image matching kernel.
166 
167  If ``templateMatched==True``, the templateExposure was matched (convolved)
168  to the ``scienceExposure`` by ``psfMatchingKernel``. Otherwise the ``scienceExposure``
169  was matched (convolved) by ``psfMatchingKernel``.
170 
171  This task discards the variance plane of ``subtractedExposure`` and re-computes
172  it from the variance planes of ``scienceExposure`` and ``templateExposure``.
173  The image plane of ``subtractedExposure`` must be at the photometric level
174  set by the AL PSF matching in `ImagePsfMatchTask.subtractExposures`.
175  The assumptions about the photometric level are controlled by the
176  `templateMatched` option in this task.
177 
178  Here we currently convert a spatially-varying matching kernel into a constant kernel,
179  just by computing it at the center of the image (tickets DM-6243, DM-6244).
180 
181  We are also using a constant accross-the-image measure of sigma (sqrt(variance)) to compute
182  the decorrelation kernel.
183 
184  TODO DM-23857 As part of the spatially varying correction implementation
185  consider whether returning a Struct is still necessary.
186  """
187  if preConvKernel is not None and not templateMatched:
188  raise ValueError("Pre-convolution and the matching of the "
189  "science exposure is not supported.")
190 
191  spatialKernel = psfMatchingKernel
192  kimg = afwImage.ImageD(spatialKernel.getDimensions())
193  bbox = subtractedExposure.getBBox()
194  if xcen is None:
195  xcen = (bbox.getBeginX() + bbox.getEndX()) / 2.
196  if ycen is None:
197  ycen = (bbox.getBeginY() + bbox.getEndY()) / 2.
198  self.log.info("Using matching kernel computed at (%d, %d)", xcen, ycen)
199  spatialKernel.computeImage(kimg, False, xcen, ycen)
200 
201  if svar is None:
202  svar = self.computeVarianceMean(scienceExposure)
203  if tvar is None:
204  tvar = self.computeVarianceMean(templateExposure)
205  self.log.infof("Variance (science, template): ({:.5e}, {:.5e})", svar, tvar)
206 
207  if templateMatched:
208  # Regular subtraction, we convolved the template
209  self.log.info("Decorrelation after template image convolution")
210  expVar = svar
211  matchedVar = tvar
212  exposure = scienceExposure
213  matchedExposure = templateExposure
214  else:
215  # We convolved the science image
216  self.log.info("Decorrelation after science image convolution")
217  expVar = tvar
218  matchedVar = svar
219  exposure = templateExposure
220  matchedExposure = scienceExposure
221 
222  # Should not happen unless entire image has been masked, which could happen
223  # if this is a small subimage of the main exposure. In this case, just return a full NaN
224  # exposure
225  if np.isnan(expVar) or np.isnan(matchedVar):
226  # Double check that one of the exposures is all NaNs
227  if (np.all(np.isnan(exposure.image.array))
228  or np.all(np.isnan(matchedExposure.image.array))):
229  self.log.warn('Template or science image is entirely NaNs: skipping decorrelation.')
230  outExposure = subtractedExposure.clone()
231  return pipeBase.Struct(correctedExposure=outExposure, )
232 
233  # The maximal correction value converges to sqrt(matchedVar/expVar).
234  # Correction divergence warning if the correction exceeds 4 orders of magnitude.
235  mOverExpVar = matchedVar/expVar
236  if mOverExpVar > 1e8:
237  self.log.warn("Diverging correction: matched image variance is "
238  " much larger than the unconvolved one's"
239  f", matchedVar/expVar:{mOverExpVar:.2e}")
240 
241  oldVarMean = self.computeVarianceMean(subtractedExposure)
242  self.log.info("Variance (uncorrected diffim): %f", oldVarMean)
243 
244  if preConvKernel is not None:
245  self.log.info('Using a pre-convolution kernel as part of decorrelation correction.')
246  kimg2 = afwImage.ImageD(preConvKernel.getDimensions())
247  preConvKernel.computeImage(kimg2, False)
248  pckArr = kimg2.array
249 
250  kArr = kimg.array
251  diffExpArr = subtractedExposure.image.array
252  psfImg = subtractedExposure.getPsf().computeKernelImage(geom.Point2D(xcen, ycen))
253  psfDim = psfImg.getDimensions()
254  psfArr = psfImg.array
255 
256  # Determine the common shape
257  kSum = np.sum(kArr)
258  kSumSq = kSum*kSum
259  self.log.debugf("Matching kernel sum: {:.2e}", kSum)
260  preSum = 1.
261  if preConvKernel is None:
262  self.computeCommonShape(kArr.shape, psfArr.shape, diffExpArr.shape)
263  corrft = self.computeCorrection(kArr, expVar, matchedVar)
264  else:
265  preSum = np.sum(pckArr)
266  self.log.debugf("pre-convolution kernel sum: {:.2e}", preSum)
267  self.computeCommonShape(pckArr.shape, kArr.shape,
268  psfArr.shape, diffExpArr.shape)
269  corrft = self.computeCorrection(kArr, expVar, matchedVar, preConvArr=pckArr)
270 
271  diffExpArr = self.computeCorrectedImage(corrft, diffExpArr)
272  corrPsfArr = self.computeCorrectedDiffimPsf(corrft, psfArr)
273 
274  psfcI = afwImage.ImageD(psfDim)
275  psfcI.array = corrPsfArr
276  psfcK = afwMath.FixedKernel(psfcI)
277  psfNew = measAlg.KernelPsf(psfcK)
278 
279  correctedExposure = subtractedExposure.clone()
280  correctedExposure.image.array[...] = diffExpArr # Allow for numpy type casting
281  # The subtracted exposure variance plane is already correlated, we cannot propagate
282  # it through another convolution; instead we need to use the uncorrelated originals
283  # The whitening should scale it to expVar + matchedVar on average
284  varImg = correctedExposure.variance.array
285  # Allow for numpy type casting
286  varImg[...] = preSum*preSum*exposure.variance.array + kSumSq*matchedExposure.variance.array
287  if not templateMatched:
288  # ImagePsfMatch.subtractExposures re-scales the difference in
289  # the science image convolution mode
290  varImg /= kSumSq
291  correctedExposure.setPsf(psfNew)
292 
293  newVarMean = self.computeVarianceMean(correctedExposure)
294  self.log.infof("Variance (corrected diffim): {:.5e}", newVarMean)
295 
296  # TODO DM-23857 As part of the spatially varying correction implementation
297  # consider whether returning a Struct is still necessary.
298  return pipeBase.Struct(correctedExposure=correctedExposure, )
299 
A kernel created from an Image.
Definition: Kernel.h:472
def debugf(fmt, *args, **kwargs)
def infof(fmt, *args, **kwargs)

Member Data Documentation

◆ ConfigClass

lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.ConfigClass = DecorrelateALKernelConfig
static

Definition at line 84 of file imageDecorrelation.py.

◆ freqSpaceShape

lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.freqSpaceShape

Definition at line 329 of file imageDecorrelation.py.

◆ statsControl

lsst.ip.diffim.imageDecorrelation.DecorrelateALKernelTask.statsControl

Definition at line 99 of file imageDecorrelation.py.


The documentation for this class was generated from the following file: