LSSTApplications  18.1.0
LSSTDataManagementBasePackage
Functions | Variables
lsst.ip.diffim.makeKernelBasisList Namespace Reference

Functions

def makeKernelBasisList (config, targetFwhmPix=None, referenceFwhmPix=None, basisDegGauss=None, metadata=None)
 
def generateAlardLuptonBasisList (config, targetFwhmPix=None, referenceFwhmPix=None, basisDegGauss=None, metadata=None)
 

Variables

int sigma2fwhm = 2. * np.sqrt(2. * np.log(2.))
 

Function Documentation

◆ generateAlardLuptonBasisList()

def lsst.ip.diffim.makeKernelBasisList.generateAlardLuptonBasisList (   config,
  targetFwhmPix = None,
  referenceFwhmPix = None,
  basisDegGauss = None,
  metadata = None 
)
Generate an Alard-Lupton kernel basis list based upon the Config and
the input FWHM of the science and template images.

Parameters
----------
config : `lsst.ip.diffim.PsfMatchConfigAL`
    Configuration object for the Alard-Lupton algorithm.
targetFwhmPix : `float`, optional
    Fwhm width (pixel) of the template exposure characteristic psf.
    This is the _target_ that will be matched to the science exposure.
referenceFwhmPix : `float`, optional
    Fwhm width (pixel) of the science exposure characteristic psf.
basisDegGauss : `list` of `int`, optional
    Polynomial degree of each Gaussian (sigma) basis. If None, defaults to `config.alardDegGauss`.
metadata : `lsst.daf.base.PropertySet`, optional
    If specified, object to collect metadata fields about the kernel basis list.

Returns
-------
basisList : `list` of `lsst.afw.math.kernel.FixedKernel`
    List of basis kernels. For each degree value ``n`` in ``config.basisDegGauss`` (n+2)(n+1)/2 kernels
    are generated and appended to the list in the order of the polynomial parameter number.
    See `lsst.afw.math.polynomialFunction2D` documentation for more details.

Notes
-----
The polynomial functions (``f``) are always evaluated in the -1.0, +1.0 range in both x, y directions,
edge to edge, with ``f(0,0)`` evaluated at the kernel center pixel, ``f(-1.0,-1.0)`` at the kernel
``(0,0)`` pixel. They are not scaled by the sigmas of the Gaussians.

Base Gaussian widths (sigmas in pixels) of the kernels are determined as:
    - If not all fwhm parameters are provided or ``config.scaleByFwhm==False``
      then ``config.alardNGauss`` and  ``config.alardSigGauss`` are used.
    - If ``targetFwhmPix<referenceFwhmPix`` (normal convolution):
      First sigma ``Sig_K`` is determined to satisfy: ``Sig_reference**2 = Sig_target**2 + Sig_K**2``.
      If it's larger than ``config.alardMinSig * config.alardGaussBeta``, make it the
      second kernel. Else make it the smallest kernel, unless only 1 kernel is asked for.
    - If ``referenceFwhmPix < targetFwhmPix`` (deconvolution):
      Define the progression of Gaussians using a
      method to derive a deconvolution sum-of-Gaussians from it's
      convolution counterpart. [1]_ Only use 3 since the algorithm
      assumes 3 components.

References
----------

.. [1] Ulmer, W.: Inverse problem of linear combinations of Gaussian convolution kernels
   (deconvolution) and some applications to proton/photon dosimetry and image
   processing. http://iopscience.iop.org/0266-5611/26/8/085002  Equation 40

Raises
------
RuntimeError
    - if ``config.kernelBasisSet`` is not equal to "alard-lupton"
ValueError
    - if ``config.kernelSize`` is even
    - if the number of Gaussians and the number of given
      sigma values are not equal or
    - if the number of Gaussians and the number of given
      polynomial degree values are not equal

Definition at line 83 of file makeKernelBasisList.py.

83  basisDegGauss=None, metadata=None):
84  """Generate an Alard-Lupton kernel basis list based upon the Config and
85  the input FWHM of the science and template images.
86 
87  Parameters
88  ----------
89  config : `lsst.ip.diffim.PsfMatchConfigAL`
90  Configuration object for the Alard-Lupton algorithm.
91  targetFwhmPix : `float`, optional
92  Fwhm width (pixel) of the template exposure characteristic psf.
93  This is the _target_ that will be matched to the science exposure.
94  referenceFwhmPix : `float`, optional
95  Fwhm width (pixel) of the science exposure characteristic psf.
96  basisDegGauss : `list` of `int`, optional
97  Polynomial degree of each Gaussian (sigma) basis. If None, defaults to `config.alardDegGauss`.
98  metadata : `lsst.daf.base.PropertySet`, optional
99  If specified, object to collect metadata fields about the kernel basis list.
100 
101  Returns
102  -------
103  basisList : `list` of `lsst.afw.math.kernel.FixedKernel`
104  List of basis kernels. For each degree value ``n`` in ``config.basisDegGauss`` (n+2)(n+1)/2 kernels
105  are generated and appended to the list in the order of the polynomial parameter number.
106  See `lsst.afw.math.polynomialFunction2D` documentation for more details.
107 
108  Notes
109  -----
110  The polynomial functions (``f``) are always evaluated in the -1.0, +1.0 range in both x, y directions,
111  edge to edge, with ``f(0,0)`` evaluated at the kernel center pixel, ``f(-1.0,-1.0)`` at the kernel
112  ``(0,0)`` pixel. They are not scaled by the sigmas of the Gaussians.
113 
114  Base Gaussian widths (sigmas in pixels) of the kernels are determined as:
115  - If not all fwhm parameters are provided or ``config.scaleByFwhm==False``
116  then ``config.alardNGauss`` and ``config.alardSigGauss`` are used.
117  - If ``targetFwhmPix<referenceFwhmPix`` (normal convolution):
118  First sigma ``Sig_K`` is determined to satisfy: ``Sig_reference**2 = Sig_target**2 + Sig_K**2``.
119  If it's larger than ``config.alardMinSig * config.alardGaussBeta``, make it the
120  second kernel. Else make it the smallest kernel, unless only 1 kernel is asked for.
121  - If ``referenceFwhmPix < targetFwhmPix`` (deconvolution):
122  Define the progression of Gaussians using a
123  method to derive a deconvolution sum-of-Gaussians from it's
124  convolution counterpart. [1]_ Only use 3 since the algorithm
125  assumes 3 components.
126 
127  References
128  ----------
129 
130  .. [1] Ulmer, W.: Inverse problem of linear combinations of Gaussian convolution kernels
131  (deconvolution) and some applications to proton/photon dosimetry and image
132  processing. http://iopscience.iop.org/0266-5611/26/8/085002 Equation 40
133 
134  Raises
135  ------
136  RuntimeError
137  - if ``config.kernelBasisSet`` is not equal to "alard-lupton"
138  ValueError
139  - if ``config.kernelSize`` is even
140  - if the number of Gaussians and the number of given
141  sigma values are not equal or
142  - if the number of Gaussians and the number of given
143  polynomial degree values are not equal
144  """
145 
146  if config.kernelBasisSet != "alard-lupton":
147  raise RuntimeError("Cannot generate %s basis within generateAlardLuptonBasisList" %
148  config.kernelBasisSet)
149 
150  kernelSize = config.kernelSize
151  fwhmScaling = config.kernelSizeFwhmScaling
152  basisNGauss = config.alardNGauss
153  basisSigmaGauss = config.alardSigGauss
154  basisGaussBeta = config.alardGaussBeta
155  basisMinSigma = config.alardMinSig
156  if basisDegGauss is None:
157  basisDegGauss = config.alardDegGauss
158 
159  if len(basisDegGauss) != basisNGauss:
160  raise ValueError("len(basisDegGauss) != basisNGauss : %d vs %d" % (len(basisDegGauss), basisNGauss))
161  if len(basisSigmaGauss) != basisNGauss:
162  raise ValueError("len(basisSigmaGauss) != basisNGauss : %d vs %d" %
163  (len(basisSigmaGauss), basisNGauss))
164  if (kernelSize % 2) != 1:
165  raise ValueError("Only odd-sized Alard-Lupton bases allowed")
166 
167  if (targetFwhmPix is None) or (referenceFwhmPix is None) or (not config.scaleByFwhm):
168  if metadata is not None:
169  metadata.add("ALBasisNGauss", basisNGauss)
170  metadata.add("ALBasisDegGauss", basisDegGauss)
171  metadata.add("ALBasisSigGauss", basisSigmaGauss)
172  metadata.add("ALKernelSize", kernelSize)
173 
174  return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss)
175 
176  targetSigma = targetFwhmPix / sigma2fwhm
177  referenceSigma = referenceFwhmPix / sigma2fwhm
178  logger = Log.getLogger("lsst.ip.diffim.generateAlardLuptonBasisList")
179  logger.debug("Generating matching bases for sigma %.2f pix -> %.2f pix", targetSigma, referenceSigma)
180 
181  # Modify the size of Alard Lupton kernels based upon the images FWHM
182  #
183  # Note the operation is : template.x.kernel = science
184  #
185  # Assuming the template and science image Psfs are Gaussians with
186  # the Fwhm above, Fwhm_T **2 + Fwhm_K **2 = Fwhm_S **2
187  #
188  if targetSigma == referenceSigma:
189  # Leave defaults as-is
190  pass
191  elif referenceSigma > targetSigma:
192  # Normal convolution
193 
194  # First Gaussian has the sigma that comes from the convolution
195  # of two Gaussians : Sig_S**2 = Sig_T**2 + Sig_K**2
196  #
197  # If it's larger than basisMinSigma * basisGaussBeta, make it the
198  # second kernel. Else make it the smallest kernel. Unless
199  # only 1 kernel is asked for.
200  kernelSigma = np.sqrt(referenceSigma**2 - targetSigma**2)
201  if kernelSigma < basisMinSigma:
202  kernelSigma = basisMinSigma
203 
204  basisSigmaGauss = []
205  if basisNGauss == 1:
206  basisSigmaGauss.append(kernelSigma)
207  nAppended = 1
208  else:
209  if (kernelSigma/basisGaussBeta) > basisMinSigma:
210  basisSigmaGauss.append(kernelSigma/basisGaussBeta)
211  basisSigmaGauss.append(kernelSigma)
212  nAppended = 2
213  else:
214  basisSigmaGauss.append(kernelSigma)
215  nAppended = 1
216 
217  # Any other Gaussians above basisNGauss=1 come from a scaling
218  # relationship: Sig_i+1 / Sig_i = basisGaussBeta
219  for i in range(nAppended, basisNGauss):
220  basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta)
221 
222  kernelSize = int(fwhmScaling * basisSigmaGauss[-1])
223  kernelSize += 0 if kernelSize%2 else 1 # Make sure it's odd
224  kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin))
225 
226  else:
227  # Deconvolution; Define the progression of Gaussians using a
228  # method to derive a deconvolution sum-of-Gaussians from it's
229  # convolution counterpart. Only use 3 since the algorithm
230  # assumes 3 components.
231  #
232  # http://iopscience.iop.org/0266-5611/26/8/085002 Equation 40
233 
234  # Use specializations for deconvolution
235  basisNGauss = config.alardNGaussDeconv
236  basisMinSigma = config.alardMinSigDeconv
237 
238  kernelSigma = np.sqrt(targetSigma**2 - referenceSigma**2)
239  if kernelSigma < basisMinSigma:
240  kernelSigma = basisMinSigma
241 
242  basisSigmaGauss = []
243  if (kernelSigma/basisGaussBeta) > basisMinSigma:
244  basisSigmaGauss.append(kernelSigma/basisGaussBeta)
245  basisSigmaGauss.append(kernelSigma)
246  nAppended = 2
247  else:
248  basisSigmaGauss.append(kernelSigma)
249  nAppended = 1
250 
251  for i in range(nAppended, basisNGauss):
252  basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta)
253 
254  kernelSize = int(fwhmScaling * basisSigmaGauss[-1])
255  kernelSize += 0 if kernelSize%2 else 1 # Make sure it's odd
256  kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin))
257 
258  # Now build a deconvolution set from these sigmas
259  sig0 = basisSigmaGauss[0]
260  sig1 = basisSigmaGauss[1]
261  sig2 = basisSigmaGauss[2]
262  basisSigmaGauss = []
263  for n in range(1, 3):
264  for j in range(n):
265  sigma2jn = (n - j)*sig1**2
266  sigma2jn += j * sig2**2
267  sigma2jn -= (n + 1)*sig0**2
268  sigmajn = np.sqrt(sigma2jn)
269  basisSigmaGauss.append(sigmajn)
270 
271  basisSigmaGauss.sort()
272  basisNGauss = len(basisSigmaGauss)
273  basisDegGauss = [config.alardDegGaussDeconv for x in basisSigmaGauss]
274 
275  if metadata is not None:
276  metadata.add("ALBasisNGauss", basisNGauss)
277  metadata.add("ALBasisDegGauss", basisDegGauss)
278  metadata.add("ALBasisSigGauss", basisSigmaGauss)
279  metadata.add("ALKernelSize", kernelSize)
280 
281  return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss)
282 
int min
int max

◆ makeKernelBasisList()

def lsst.ip.diffim.makeKernelBasisList.makeKernelBasisList (   config,
  targetFwhmPix = None,
  referenceFwhmPix = None,
  basisDegGauss = None,
  metadata = None 
)
Generate the delta function or Alard-Lupton kernel bases depending on the Config.
Wrapper to call either `lsst.ip.diffim.makeDeltaFunctionBasisList` or
`lsst.ip.diffim.generateAlardLuptonBasisList`.

Parameters
----------
config : `lsst.ip.diffim.PsfMatchConfigAL`
    Configuration object.
targetFwhmPix : `float`, optional
    Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
    Not used for delta function basis sets.
referenceFwhmPix : `float`, optional
    Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
    Not used for delta function basis sets.
basisDegGauss : `list` of `int`, optional
    Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
    Not used for delta function basis sets.
metadata : `lsst.daf.base.PropertySet`, optional
    Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
    Not used for delta function basis sets.

Returns
-------
basisList: `list` of `lsst.afw.math.kernel.FixedKernel`
    List of basis kernels.

Notes
-----
See `lsst.ip.diffim.generateAlardLuptonBasisList` and
`lsst.ip.diffim.makeDeltaFunctionBasisList` for more information.

Raises
------
ValueError
    If ``config.kernelBasisSet`` has an invalid value (not "alard-lupton" or "delta-function").

Definition at line 33 of file makeKernelBasisList.py.

33  basisDegGauss=None, metadata=None):
34  """Generate the delta function or Alard-Lupton kernel bases depending on the Config.
35  Wrapper to call either `lsst.ip.diffim.makeDeltaFunctionBasisList` or
36  `lsst.ip.diffim.generateAlardLuptonBasisList`.
37 
38  Parameters
39  ----------
40  config : `lsst.ip.diffim.PsfMatchConfigAL`
41  Configuration object.
42  targetFwhmPix : `float`, optional
43  Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
44  Not used for delta function basis sets.
45  referenceFwhmPix : `float`, optional
46  Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
47  Not used for delta function basis sets.
48  basisDegGauss : `list` of `int`, optional
49  Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
50  Not used for delta function basis sets.
51  metadata : `lsst.daf.base.PropertySet`, optional
52  Passed on to `lsst.ip.diffim.generateAlardLuptonBasisList`.
53  Not used for delta function basis sets.
54 
55  Returns
56  -------
57  basisList: `list` of `lsst.afw.math.kernel.FixedKernel`
58  List of basis kernels.
59 
60  Notes
61  -----
62  See `lsst.ip.diffim.generateAlardLuptonBasisList` and
63  `lsst.ip.diffim.makeDeltaFunctionBasisList` for more information.
64 
65  Raises
66  ------
67  ValueError
68  If ``config.kernelBasisSet`` has an invalid value (not "alard-lupton" or "delta-function").
69  """
70  if config.kernelBasisSet == "alard-lupton":
71  return generateAlardLuptonBasisList(config, targetFwhmPix=targetFwhmPix,
72  referenceFwhmPix=referenceFwhmPix,
73  basisDegGauss=basisDegGauss,
74  metadata=metadata)
75  elif config.kernelBasisSet == "delta-function":
76  kernelSize = config.kernelSize
77  return diffimLib.makeDeltaFunctionBasisList(kernelSize, kernelSize)
78  else:
79  raise ValueError("Cannot generate %s basis set" % (config.kernelBasisSet))
80 
81 
def generateAlardLuptonBasisList(config, targetFwhmPix=None, referenceFwhmPix=None, basisDegGauss=None, metadata=None)

Variable Documentation

◆ sigma2fwhm

int lsst.ip.diffim.makeKernelBasisList.sigma2fwhm = 2. * np.sqrt(2. * np.log(2.))

Definition at line 29 of file makeKernelBasisList.py.