Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

# 

# LSST Data Management System 

# Copyright 2008-2016 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["makeKernelBasisList", "generateAlardLuptonBasisList"] 

 

from . import diffimLib 

from lsst.log import Log 

import numpy as np 

 

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

 

 

def 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"). 

""" 

if config.kernelBasisSet == "alard-lupton": 

return generateAlardLuptonBasisList(config, targetFwhmPix=targetFwhmPix, 

referenceFwhmPix=referenceFwhmPix, 

basisDegGauss=basisDegGauss, 

metadata=metadata) 

elif config.kernelBasisSet == "delta-function": 

kernelSize = config.kernelSize 

return diffimLib.makeDeltaFunctionBasisList(kernelSize, kernelSize) 

else: 

raise ValueError("Cannot generate %s basis set" % (config.kernelBasisSet)) 

 

 

def 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 

""" 

 

if config.kernelBasisSet != "alard-lupton": 

raise RuntimeError("Cannot generate %s basis within generateAlardLuptonBasisList" % 

config.kernelBasisSet) 

 

kernelSize = config.kernelSize 

fwhmScaling = config.kernelSizeFwhmScaling 

basisNGauss = config.alardNGauss 

basisSigmaGauss = config.alardSigGauss 

basisGaussBeta = config.alardGaussBeta 

basisMinSigma = config.alardMinSig 

if basisDegGauss is None: 

basisDegGauss = config.alardDegGauss 

 

if len(basisDegGauss) != basisNGauss: 

raise ValueError("len(basisDegGauss) != basisNGauss : %d vs %d" % (len(basisDegGauss), basisNGauss)) 

if len(basisSigmaGauss) != basisNGauss: 

raise ValueError("len(basisSigmaGauss) != basisNGauss : %d vs %d" % 

(len(basisSigmaGauss), basisNGauss)) 

if (kernelSize % 2) != 1: 

raise ValueError("Only odd-sized Alard-Lupton bases allowed") 

 

if (targetFwhmPix is None) or (referenceFwhmPix is None) or (not config.scaleByFwhm): 

if metadata is not None: 

metadata.add("ALBasisNGauss", basisNGauss) 

metadata.add("ALBasisDegGauss", basisDegGauss) 

metadata.add("ALBasisSigGauss", basisSigmaGauss) 

metadata.add("ALKernelSize", kernelSize) 

 

return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss) 

 

targetSigma = targetFwhmPix / sigma2fwhm 

referenceSigma = referenceFwhmPix / sigma2fwhm 

logger = Log.getLogger("lsst.ip.diffim.generateAlardLuptonBasisList") 

logger.debug("Generating matching bases for sigma %.2f pix -> %.2f pix", targetSigma, referenceSigma) 

 

# Modify the size of Alard Lupton kernels based upon the images FWHM 

# 

# Note the operation is : template.x.kernel = science 

# 

# Assuming the template and science image Psfs are Gaussians with 

# the Fwhm above, Fwhm_T **2 + Fwhm_K **2 = Fwhm_S **2 

# 

if targetSigma == referenceSigma: 

# Leave defaults as-is 

pass 

elif referenceSigma > targetSigma: 

# Normal convolution 

 

# First Gaussian has the sigma that comes from the convolution 

# of two Gaussians : Sig_S**2 = Sig_T**2 + Sig_K**2 

# 

# If it's larger than basisMinSigma * basisGaussBeta, make it the 

# second kernel. Else make it the smallest kernel. Unless 

# only 1 kernel is asked for. 

kernelSigma = np.sqrt(referenceSigma**2 - targetSigma**2) 

if kernelSigma < basisMinSigma: 

kernelSigma = basisMinSigma 

 

basisSigmaGauss = [] 

if basisNGauss == 1: 

basisSigmaGauss.append(kernelSigma) 

nAppended = 1 

else: 

if (kernelSigma/basisGaussBeta) > basisMinSigma: 

basisSigmaGauss.append(kernelSigma/basisGaussBeta) 

basisSigmaGauss.append(kernelSigma) 

nAppended = 2 

else: 

basisSigmaGauss.append(kernelSigma) 

nAppended = 1 

 

# Any other Gaussians above basisNGauss=1 come from a scaling 

# relationship: Sig_i+1 / Sig_i = basisGaussBeta 

for i in range(nAppended, basisNGauss): 

basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta) 

 

kernelSize = int(fwhmScaling * basisSigmaGauss[-1]) 

kernelSize += 0 if kernelSize%2 else 1 # Make sure it's odd 

kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin)) 

 

else: 

# Deconvolution; Define the progression of Gaussians using a 

# method to derive a deconvolution sum-of-Gaussians from it's 

# convolution counterpart. Only use 3 since the algorithm 

# assumes 3 components. 

# 

# http://iopscience.iop.org/0266-5611/26/8/085002 Equation 40 

 

# Use specializations for deconvolution 

basisNGauss = config.alardNGaussDeconv 

basisMinSigma = config.alardMinSigDeconv 

 

kernelSigma = np.sqrt(targetSigma**2 - referenceSigma**2) 

if kernelSigma < basisMinSigma: 

kernelSigma = basisMinSigma 

 

basisSigmaGauss = [] 

if (kernelSigma/basisGaussBeta) > basisMinSigma: 

basisSigmaGauss.append(kernelSigma/basisGaussBeta) 

basisSigmaGauss.append(kernelSigma) 

nAppended = 2 

else: 

basisSigmaGauss.append(kernelSigma) 

nAppended = 1 

 

for i in range(nAppended, basisNGauss): 

basisSigmaGauss.append(basisSigmaGauss[-1]*basisGaussBeta) 

 

kernelSize = int(fwhmScaling * basisSigmaGauss[-1]) 

kernelSize += 0 if kernelSize%2 else 1 # Make sure it's odd 

kernelSize = min(config.kernelSizeMax, max(kernelSize, config.kernelSizeMin)) 

 

# Now build a deconvolution set from these sigmas 

sig0 = basisSigmaGauss[0] 

sig1 = basisSigmaGauss[1] 

sig2 = basisSigmaGauss[2] 

basisSigmaGauss = [] 

for n in range(1, 3): 

for j in range(n): 

sigma2jn = (n - j)*sig1**2 

sigma2jn += j * sig2**2 

sigma2jn -= (n + 1)*sig0**2 

sigmajn = np.sqrt(sigma2jn) 

basisSigmaGauss.append(sigmajn) 

 

basisSigmaGauss.sort() 

basisNGauss = len(basisSigmaGauss) 

basisDegGauss = [config.alardDegGaussDeconv for x in basisSigmaGauss] 

 

if metadata is not None: 

metadata.add("ALBasisNGauss", basisNGauss) 

metadata.add("ALBasisDegGauss", basisDegGauss) 

metadata.add("ALBasisSigGauss", basisSigmaGauss) 

metadata.add("ALKernelSize", kernelSize) 

 

return diffimLib.makeAlardLuptonBasisList(kernelSize//2, basisNGauss, basisSigmaGauss, basisDegGauss)