23 __all__ = [
"makeKernelBasisList",
"generateAlardLuptonBasisList"]
25 from .
import diffimLib
29 sigma2fwhm = 2. * np.sqrt(2. * np.log(2.))
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`.
40 config : `lsst.ip.diffim.PsfMatchConfigAL`
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.
57 basisList: `list` of `lsst.afw.math.kernel.FixedKernel`
58 List of basis kernels.
62 See `lsst.ip.diffim.generateAlardLuptonBasisList` and
63 `lsst.ip.diffim.makeDeltaFunctionBasisList` for more information.
68 If ``config.kernelBasisSet`` has an invalid value (not "alard-lupton" or "delta-function").
70 if config.kernelBasisSet ==
"alard-lupton":
72 referenceFwhmPix=referenceFwhmPix,
73 basisDegGauss=basisDegGauss,
75 elif config.kernelBasisSet ==
"delta-function":
76 kernelSize = config.kernelSize
77 return diffimLib.makeDeltaFunctionBasisList(kernelSize, kernelSize)
79 raise ValueError(
"Cannot generate %s basis set" % (config.kernelBasisSet))
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.
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.
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.
111 - if ``config.kernelBasisSet`` is not equal to "alard-lupton"
113 - if ``config.kernelSize`` is even
114 - if the number of Gaussians and the number of given
115 sigma values are not equal or
116 - if the number of Gaussians and the number of given
117 polynomial degree values are not equal
121 The polynomial functions (``f``) are always evaluated in the -1.0, +1.0 range in both x, y directions,
122 edge to edge, with ``f(0,0)`` evaluated at the kernel center pixel, ``f(-1.0,-1.0)`` at the kernel
123 ``(0,0)`` pixel. They are not scaled by the sigmas of the Gaussians.
125 Base Gaussian widths (sigmas in pixels) of the kernels are determined as:
126 - If not all fwhm parameters are provided or ``config.scaleByFwhm==False``
127 then ``config.alardNGauss`` and ``config.alardSigGauss`` are used.
128 - If ``targetFwhmPix<referenceFwhmPix`` (normal convolution):
129 First sigma ``Sig_K`` is determined to satisfy: ``Sig_reference**2 = Sig_target**2 + Sig_K**2``.
130 If it's larger than ``config.alardMinSig * config.alardGaussBeta``, make it the
131 second kernel. Else make it the smallest kernel, unless only 1 kernel is asked for.
132 - If ``referenceFwhmPix < targetFwhmPix`` (deconvolution):
133 Define the progression of Gaussians using a
134 method to derive a deconvolution sum-of-Gaussians from it's
135 convolution counterpart. [1]_ Only use 3 since the algorithm
136 assumes 3 components.
140 ALBasisNGauss : `int`
141 The number of base Gaussians in the AL basis functions.
142 ALBasisDegGauss : `list` of `int`
143 Polynomial order of spatial modification of the base Gaussian functions.
144 ALBasisSigGauss : `list` of `float`
145 Sigmas in pixels of the base Gaussians.
147 Kernel stamp size is (ALKernelSize pix, ALKernelSize pix).
148 ALBasisMode : `str`, either of ``config``, ``convolution``, ``deconvolution``
149 Indicates whether the config file values, the convolution or deconvolution algorithm
150 was used to determine the base Gaussian sigmas and the kernel stamp size.
154 .. [1] Ulmer, W.: Inverse problem of linear combinations of Gaussian convolution kernels
155 (deconvolution) and some applications to proton/photon dosimetry and image
156 processing. http://iopscience.iop.org/0266-5611/26/8/085002 Equation 40
159 if config.kernelBasisSet !=
"alard-lupton":
160 raise RuntimeError(
"Cannot generate %s basis within generateAlardLuptonBasisList" %
161 config.kernelBasisSet)
163 kernelSize = config.kernelSize
164 fwhmScaling = config.kernelSizeFwhmScaling
165 basisNGauss = config.alardNGauss
166 basisSigmaGauss = config.alardSigGauss
167 basisGaussBeta = config.alardGaussBeta
168 basisMinSigma = config.alardMinSig
169 if basisDegGauss
is None:
170 basisDegGauss = config.alardDegGauss
172 if len(basisDegGauss) != basisNGauss:
173 raise ValueError(
"len(basisDegGauss) != basisNGauss : %d vs %d" % (len(basisDegGauss), basisNGauss))
174 if len(basisSigmaGauss) != basisNGauss:
175 raise ValueError(
"len(basisSigmaGauss) != basisNGauss : %d vs %d" %
176 (len(basisSigmaGauss), basisNGauss))
177 if (kernelSize % 2) != 1:
178 raise ValueError(
"Only odd-sized Alard-Lupton bases allowed")
180 logger = Log.getLogger(
"ip.diffim.generateAlardLuptonBasisList")
181 if (targetFwhmPix
is None)
or (referenceFwhmPix
is None)
or (
not config.scaleByFwhm):
182 logger.info(
"PSF sigmas are not available or scaling by fwhm disabled, "
183 "falling back to config values")
184 if metadata
is not None:
185 metadata.add(
"ALBasisNGauss", basisNGauss)
186 metadata.add(
"ALBasisDegGauss", basisDegGauss)
187 metadata.add(
"ALBasisSigGauss", basisSigmaGauss)
188 metadata.add(
"ALKernelSize", kernelSize)
189 metadata.add(
"ALBasisMode",
"config")
191 return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss)
193 targetSigma = targetFwhmPix / sigma2fwhm
194 referenceSigma = referenceFwhmPix / sigma2fwhm
195 logger.debug(
"Generating matching bases for sigma %.2f pix -> %.2f pix", targetSigma, referenceSigma)
204 if targetSigma == referenceSigma:
206 logger.info(
"Target and reference psf fwhms are equal, falling back to config values")
208 elif referenceSigma > targetSigma:
217 logger.info(
"Reference psf fwhm is the greater, normal convolution mode")
218 basisMode =
"convolution"
219 kernelSigma = np.sqrt(referenceSigma**2 - targetSigma**2)
220 if kernelSigma < basisMinSigma:
221 kernelSigma = basisMinSigma
225 basisSigmaGauss.append(kernelSigma)
228 if (kernelSigma/basisGaussBeta) > basisMinSigma:
229 basisSigmaGauss.append(kernelSigma/basisGaussBeta)
230 basisSigmaGauss.append(kernelSigma)
233 basisSigmaGauss.append(kernelSigma)
238 for i
in range(nAppended, basisNGauss):
239 basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta)
241 kernelSize = int(fwhmScaling * basisSigmaGauss[-1])
242 kernelSize += 0
if kernelSize%2
else 1
243 kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin))
254 logger.info(
"Target psf fwhm is the greater, deconvolution mode")
255 basisMode =
"deconvolution"
256 basisNGauss = config.alardNGaussDeconv
257 basisMinSigma = config.alardMinSigDeconv
259 kernelSigma = np.sqrt(targetSigma**2 - referenceSigma**2)
260 if kernelSigma < basisMinSigma:
261 kernelSigma = basisMinSigma
264 if (kernelSigma/basisGaussBeta) > basisMinSigma:
265 basisSigmaGauss.append(kernelSigma/basisGaussBeta)
266 basisSigmaGauss.append(kernelSigma)
269 basisSigmaGauss.append(kernelSigma)
272 for i
in range(nAppended, basisNGauss):
273 basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta)
275 kernelSize = int(fwhmScaling * basisSigmaGauss[-1])
276 kernelSize += 0
if kernelSize%2
else 1
277 kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin))
280 sig0 = basisSigmaGauss[0]
281 sig1 = basisSigmaGauss[1]
282 sig2 = basisSigmaGauss[2]
284 for n
in range(1, 3):
286 sigma2jn = (n - j)*sig1**2
287 sigma2jn += j * sig2**2
288 sigma2jn -= (n + 1)*sig0**2
289 sigmajn = np.sqrt(sigma2jn)
290 basisSigmaGauss.append(sigmajn)
292 basisSigmaGauss.sort()
293 basisNGauss = len(basisSigmaGauss)
294 basisDegGauss = [config.alardDegGaussDeconv
for x
in basisSigmaGauss]
296 if metadata
is not None:
297 metadata.add(
"ALBasisNGauss", basisNGauss)
298 metadata.add(
"ALBasisDegGauss", basisDegGauss)
299 metadata.add(
"ALBasisSigGauss", basisSigmaGauss)
300 metadata.add(
"ALKernelSize", kernelSize)
301 metadata.add(
"ALBasisMode", basisMode)
303 logger.debug(
"basisSigmaGauss: %s basisDegGauss: %s",
304 ','.join([
'{:.1f}'.format(v)
for v
in basisSigmaGauss]),
305 ','.join([
'{:d}'.format(v)
for v
in basisDegGauss]))
307 return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss)
def generateAlardLuptonBasisList(config, targetFwhmPix=None, referenceFwhmPix=None, basisDegGauss=None, metadata=None)
def makeKernelBasisList(config, targetFwhmPix=None, referenceFwhmPix=None, basisDegGauss=None, metadata=None)