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

# 

# 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 appropriate Kernel basis based on the Config""" 

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

return generateAlardLuptonBasisList(config, targetFwhmPix=targetFwhmPix, 

referenceFwhmPix=referenceFwhmPix, 

basisDegGauss=basisDegGauss, 

metadata=metadata) 

40 ↛ 44line 40 didn't jump to line 44, because the condition on line 40 was never false 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 based upon the Config and 

the input FWHM of the science and template images""" 

 

52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true 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 

62 ↛ 65line 62 didn't jump to line 65, because the condition on line 62 was never false if basisDegGauss is None: 

basisDegGauss = config.alardDegGauss 

 

65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true if len(basisDegGauss) != basisNGauss: 

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

67 ↛ 68line 67 didn't jump to line 68, because the condition on line 67 was never true if len(basisSigmaGauss) != basisNGauss: 

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

(len(basisSigmaGauss), basisNGauss)) 

70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never true 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 

# 

94 ↛ 96line 94 didn't jump to line 96, because the condition on line 94 was never true if targetSigma == referenceSigma: 

# Leave defaults as-is 

pass 

97 ↛ 141line 97 didn't jump to line 141, because the condition on line 97 was never false 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) 

107 ↛ 108line 107 didn't jump to line 108, because the condition on line 107 was never true if kernelSigma < basisMinSigma: 

kernelSigma = basisMinSigma 

 

basisSigmaGauss = [] 

111 ↛ 112line 111 didn't jump to line 112, because the condition on line 111 was never true if basisNGauss == 1: 

basisSigmaGauss.append(kernelSigma) 

nAppended = 1 

else: 

115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true 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)