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

# 

# LSST Data Management System 

# Copyright 2008-2013 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/>. 

# 

""" 

Code to load multi-Gaussian approximations to profiles from "The Tractor" 

into a lsst.shapelet.MultiShapeletBasis. 

 

Please see the README file in the data directory of the lsst.shapelet 

package for more information. 

""" 

from future import standard_library 

standard_library.install_aliases() # noqa 

from builtins import zip 

from builtins import str 

from builtins import range 

 

import numpy 

import os 

import re 

import sys 

import warnings 

import pickle 

 

import lsst.pex.exceptions 

from .radialProfile import RadialProfile 

from .multiShapeletBasis import MultiShapeletBasis 

from .shapeletFunction import ShapeletFunction 

 

 

def registerRadialProfiles(): 

"""Register the pickled profiles in the data directory with the RadialProfile singleton registry. 

 

This should only be called at import time by this module; it's only a function to avoid polluting 

the module namespace with all the local variables used here. 

""" 

dataDir = os.path.join(os.environ["SHAPELET_DIR"], "data") 

regex = re.compile(r"([a-z]+\d?)_K(\d+)_MR(\d+)\.pickle") 

for filename in os.listdir(dataDir): 

match = regex.match(filename) 

if not match: 

continue 

name = match.group(1) 

nComponents = int(match.group(2)) 

maxRadius = int(match.group(3)) 

try: 

profile = RadialProfile.get(name) 

except lsst.pex.exceptions.Exception: 

warnings.warn("No C++ profile for multi-Gaussian pickle file '%s'" % filename) 

continue 

with open(os.path.join(dataDir, filename), 'rb') as stream: 

69 ↛ 72line 69 didn't jump to line 72, because the condition on line 69 was never false if sys.version_info[0] >= 3: 

array = pickle.load(stream, encoding='latin1') 

else: 

array = pickle.load(stream) 

amplitudes = array[:nComponents] 

amplitudes /= amplitudes.sum() 

variances = array[nComponents:] 

76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never true if amplitudes.shape != (nComponents,) or variances.shape != (nComponents,): 

warnings.warn("Unknown format for multi-Gaussian pickle file '%s'" % filename) 

continue 

basis = MultiShapeletBasis(1) 

for amplitude, variance in zip(amplitudes, variances): 

radius = variance**0.5 

matrix = numpy.array([[amplitude / ShapeletFunction.FLUX_FACTOR]], dtype=float) 

basis.addComponent(radius, 0, matrix) 

profile.registerBasis(basis, nComponents, maxRadius) 

 

 

# We register all the profiles at module import time, to allow C++ code to access all available profiles 

# without having to later call Python code to unpickle them. 

registerRadialProfiles() 

 

 

def evaluateRadial(basis, r, sbNormalize=False, doComponents=False): 

"""Plot a single-element MultiShapeletBasis as a radial profile. 

""" 

ellipse = lsst.afw.geom.ellipses.Ellipse(lsst.afw.geom.ellipses.Axes()) 

coefficients = numpy.ones(1, dtype=float) 

msf = basis.makeFunction(ellipse, coefficients) 

ev = msf.evaluate() 

n = 1 

100 ↛ 101line 100 didn't jump to line 101, because the condition on line 100 was never true if doComponents: 

n += len(msf.getComponents()) 

z = numpy.zeros((n,) + r.shape, dtype=float) 

for j, x in enumerate(r): 

z[0, j] = ev(x, 0.0) 

105 ↛ 106line 105 didn't jump to line 106, because the condition on line 105 was never true if doComponents: 

for i, sf in enumerate(msf.getComponents()): 

evc = sf.evaluate() 

for j, x in enumerate(r): 

z[i+1, j] = evc(x, 0.0) 

110 ↛ 112line 110 didn't jump to line 112, because the condition on line 110 was never false if sbNormalize: 

z /= ev(1.0, 0.0) 

return z 

 

 

def integrateNormalizedFluxes(maxRadius=20.0, nSteps=5000): 

"""! 

After normalizing by surface brightness at r=1 r_e, integrate the profiles to compare 

relative fluxes between the true profiles and their approximations. 

 

@param[in] maxRadius Maximum radius to integrate the profile, in units of r_e. 

@param[in] nSteps Number of concrete points at which to evaluate the profile to 

do the integration (we just use the trapezoidal rule). 

""" 

radii = numpy.linspace(0.0, maxRadius, nSteps) 

profiles = {name: RadialProfile.get(name) for name in ("exp", "lux", "dev", "luv", 

"ser2", "ser3", "ser5")} 

evaluated = {} 

for name, profile in profiles.items(): 

evaluated[name] = profile.evaluate(radii) 

basis = profile.getBasis(8) 

evaluated["g" + name] = evaluateRadial(basis, radii, sbNormalize=True, doComponents=False)[0, :] 

fluxes = {name: numpy.trapz(z*radii, radii) for name, z in evaluated.items()} 

return fluxes 

 

 

def plotSuite(doComponents=False): 

"""Plot all the profiles defined in this module together: true exp and dev, the SDSS softended/truncated 

lux and luv, and the multi-Gaussian approximations to all of these. 

 

To plot the individual Gaussians that form the multi-Gaussian approximations, pass doComponents=True. 

 

Returns a tuple of (figure, axes), where 'figure' is the matplotlib figure that contains the plot, 

and axes is a 2x4 NumPy array of matplotlib axes objects 

""" 

from matplotlib import pyplot 

fig = pyplot.figure(figsize=(9, 4.7)) 

axes = numpy.zeros((2, 4), dtype=object) 

r1 = numpy.logspace(-3, 0, 1000, base=10) 

r2 = numpy.linspace(1, 10, 1000) 

r = [r1, r2] 

for i in range(2): 

for j in range(4): 

axes[i, j] = fig.add_subplot(2, 4, i*4+j+1) 

profiles = {name: RadialProfile.get(name) for name in ("exp", "lux", "dev", "luv")} 

basis = {name: profiles[name].getBasis(8) for name in profiles} 

z = numpy.zeros((2, 4), dtype=object) 

colors = ("k", "g", "b", "r") 

fig.subplots_adjust(wspace=0.025, hspace=0.025, bottom=0.15, left=0.1, right=0.98, top=0.92) 

centers = [None, None] 

for i in range(2): # 0=profile, 1=relative error 

for j in range(0, 4, 2): # grid columns: 0=exp-like, 2=dev-like 

bbox0 = axes[i, j].get_position() 

bbox1 = axes[i, j+1].get_position() 

bbox1.x0 = bbox0.x1 - 0.06 

bbox0.x1 = bbox1.x0 

centers[j/2] = 0.5*(bbox0.x0 + bbox1.x1) 

axes[i, j].set_position(bbox0) 

axes[i, j+1].set_position(bbox1) 

for j in range(0, 2): 

z[0, j] = [evaluateRadial(basis[k], r[j], sbNormalize=True, doComponents=doComponents) 

for k in ("exp", "lux")] 

z[0, j][0:0] = [profiles[k].evaluate(r[j])[numpy.newaxis, :] for k in ("exp", "lux")] 

z[0, j+2] = [evaluateRadial(basis[k], r[j], sbNormalize=True, doComponents=doComponents) 

for k in ("dev", "luv")] 

z[0, j+2][0:0] = [profiles[k].evaluate(r[j])[numpy.newaxis, :] for k in ("dev", "luv")] 

methodNames = [["loglog", "semilogy"], ["semilogx", "plot"]] 

for j in range(0, 4): # grid columns 

z[1, j] = [(z[0, j][0][0, :] - z[0, j][i][0, :])/z[0, j][0][0, :] for i in range(0, 4)] 

handles = [] 

method0 = getattr(axes[0, j], methodNames[0][j%2]) 

method1 = getattr(axes[1, j], methodNames[1][j%2]) 

for k in range(4): 

y0 = z[0, j][k] 

handles.append(method0(r[j%2], y0[0, :], color=colors[k])[0]) 

if doComponents: 

for l in range(1, y0.shape[0]): 

method0(r[j%2], y0[l, :], color=colors[k], alpha=0.25) 

method1(r[j%2], z[1, j][k], color=colors[k]) 

axes[0, j].set_xticklabels([]) 

axes[0, j].set_ylim(1E-6, 1E3) 

axes[1, j].set_ylim(-0.2, 1.0) 

for i, label in enumerate(("profile", "relative error")): 

axes[i, 0].set_ylabel(label) 

for t in axes[i, 0].get_yticklabels(): 

t.set_fontsize(11) 

for j in range(1, 4): 

axes[0, j].set_yticklabels([]) 

axes[1, j].set_yticklabels([]) 

xticks = [['$\\mathdefault{10^{%d}}$' % i for i in range(-3, 1)], 

[str(i) for i in range(1, 11)]] 

xticks[0][-1] = "" 

xticks[1][-1] = "" 

for j in range(0, 4): 

axes[1, j].set_xticklabels(xticks[j%2]) 

for t in axes[1, j].get_xticklabels(): 

t.set_fontsize(11) 

fig.legend(handles, ["exp/dev", "lux/luv", "approx exp/dev", "approx lux/luv"], 

loc='lower center', ncol=4) 

fig.text(centers[0], 0.95, "exponential", ha='center', weight='bold') 

fig.text(centers[1], 0.95, "de Vaucouleur", ha='center', weight='bold') 

return fig, axes