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

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

# 

# LSST Data Management System 

# Copyright 2015-2016 LSST/AURA 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

""" 

Tests for RGB Images 

 

Run with: 

./rgb.py 

or 

python 

>>> import rgb; rgb.run() 

""" 

import os 

import math 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.detection as afwDetect 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.afw.display.ds9 as ds9 

import lsst.afw.display.rgb as rgb 

 

ver1, ver2, ver3 = 1, 3, 1 

NO_MATPLOTLIB_STRING = "Requires matplotlib >= %d.%d.%d" % (ver1, ver2, ver3) 

try: 

import matplotlib 

mplVersion = matplotlib.__version__ 

# Split at + to check for development version (PEP 440) 

mplVersion = mplVersion.split('+') 

versionInfo = tuple(int(s.strip("rc")) for s in mplVersion[0].split(".")) 

HAVE_MATPLOTLIB = versionInfo >= (ver1, ver2, ver3) 

except ImportError: 

HAVE_MATPLOTLIB = False 

 

try: 

import scipy.misc 

scipy.misc.imresize 

HAVE_SCIPY_MISC = True 

except (ImportError, AttributeError): 

HAVE_SCIPY_MISC = False 

 

try: 

type(display) 

except NameError: 

display = False 

 

 

def saturate(image, satValue): 

"""Simulate saturation on an image, so we can test 'replaceSaturatedPixels' 

 

Takes an Image, sets saturated pixels to NAN and masks them, returning 

a MaskedImage. 

""" 

image = afwImage.makeMaskedImage(image) 

afwDetect.FootprintSet(image, afwDetect.Threshold(satValue), "SAT") 

arr = image.getImage().getArray() 

arr[np.where(arr >= satValue)] = np.nan 

return image 

 

 

R, G, B = 2, 1, 0 

 

 

class RgbTestCase(unittest.TestCase): 

"""A test case for Rgb""" 

 

def setUp(self): 

self.min, self.range, self.Q = 0, 5, 20 # asinh 

 

width, height = 85, 75 

self.images = [] 

self.images.append(afwImage.ImageF(lsst.geom.ExtentI(width, height))) 

self.images.append(afwImage.ImageF(lsst.geom.ExtentI(width, height))) 

self.images.append(afwImage.ImageF(lsst.geom.ExtentI(width, height))) 

 

for (x, y, A, g_r, r_i) in [(15, 15, 1000, 1.0, 2.0), 

(50, 45, 5500, -1.0, -0.5), 

(30, 30, 600, 1.0, 2.5), 

(45, 15, 20000, 1.0, 1.0), 

]: 

for i in range(len(self.images)): 

if i == B: 

amp = A 

elif i == G: 

amp = A*math.pow(10, 0.4*g_r) 

109 ↛ 112line 109 didn't jump to line 112, because the condition on line 109 was never false elif i == R: 

amp = A*math.pow(10, 0.4*r_i) 

 

self.images[i].set(x, y, amp) 

 

psf = afwMath.AnalyticKernel( 

15, 15, afwMath.GaussianFunction2D(2.5, 1.5, 0.5)) 

 

convolvedImage = type(self.images[0])(self.images[0].getDimensions()) 

randomImage = type(self.images[0])(self.images[0].getDimensions()) 

rand = afwMath.Random("MT19937", 666) 

for i in range(len(self.images)): 

afwMath.convolve(convolvedImage, self.images[i], psf, True, True) 

afwMath.randomGaussianImage(randomImage, rand) 

randomImage *= 2 

convolvedImage += randomImage 

self.images[i][:] = convolvedImage 

del convolvedImage 

del randomImage 

 

def tearDown(self): 

for im in self.images: 

del im 

del self.images 

 

def testStarsAsinh(self): 

"""Test creating an RGB image using an asinh stretch""" 

asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q) 

rgbImage = asinhMap.makeRgbImage( 

self.images[R], self.images[G], self.images[B]) 

 

140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testStarsAsinhZscale(self): 

"""Test creating an RGB image using an asinh stretch estimated using zscale""" 

 

rgbImages = [self.images[R], self.images[G], self.images[B]] 

 

map = rgb.AsinhZScaleMapping(rgbImages[0]) 

rgbImage = map.makeRgbImage(*rgbImages) 

 

151 ↛ 152line 151 didn't jump to line 152, because the condition on line 151 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testStarsAsinhZscaleIntensity(self): 

"""Test creating an RGB image using an asinh stretch estimated using zscale on the intensity""" 

 

rgbImages = [self.images[R], self.images[G], self.images[B]] 

 

map = rgb.AsinhZScaleMapping(rgbImages) 

rgbImage = map.makeRgbImage(*rgbImages) 

 

162 ↛ 163line 162 didn't jump to line 163, because the condition on line 162 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testStarsAsinhZscaleIntensityPedestal(self): 

"""Test creating an RGB image using an asinh stretch estimated using zscale on the intensity 

where the images each have a pedestal added""" 

 

rgbImages = [self.images[R], self.images[G], self.images[B]] 

 

pedestal = [100, 400, -400] 

for i, ped in enumerate(pedestal): 

rgbImages[i] += ped 

 

map = rgb.AsinhZScaleMapping(rgbImages, pedestal=pedestal) 

rgbImage = map.makeRgbImage(*rgbImages) 

 

178 ↛ 179line 178 didn't jump to line 179, because the condition on line 178 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testStarsAsinhZscaleIntensityBW(self): 

"""Test creating a black-and-white image using an asinh stretch estimated 

using zscale on the intensity""" 

 

rgbImage = rgb.AsinhZScaleMapping(self.images[R]).makeRgbImage() 

 

187 ↛ 188line 187 didn't jump to line 188, because the condition on line 187 was never true if display: 

rgb.displayRGB(rgbImage) 

 

@unittest.skipUnless(HAVE_MATPLOTLIB, NO_MATPLOTLIB_STRING) 

def testMakeRGB(self): 

"""Test the function that does it all""" 

satValue = 1000.0 

with lsst.utils.tests.getTempFilePath(".png") as fileName: 

red = saturate(self.images[R], satValue) 

green = saturate(self.images[G], satValue) 

blue = saturate(self.images[B], satValue) 

rgb.makeRGB(red, green, blue, self.min, self.range, self.Q, fileName=fileName, 

saturatedBorderWidth=1, saturatedPixelValue=2000) 

self.assertTrue(os.path.exists(fileName)) 

 

def testLinear(self): 

"""Test using a specified linear stretch""" 

 

rgbImage = rgb.LinearMapping(-8.45, 13.44).makeRgbImage(self.images[R]) 

 

207 ↛ 208line 207 didn't jump to line 208, because the condition on line 207 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testLinearMinMax(self): 

"""Test using a min/max linear stretch 

 

N.b. also checks that an image passed to the ctor is used as the default in makeRgbImage() 

""" 

 

rgbImage = rgb.LinearMapping(image=self.images[R]).makeRgbImage() 

 

218 ↛ 219line 218 didn't jump to line 219, because the condition on line 218 was never true if display: 

rgb.displayRGB(rgbImage) 

 

def testZScale(self): 

"""Test using a zscale stretch""" 

 

rgbImage = rgb.ZScaleMapping(self.images[R]).makeRgbImage() 

 

226 ↛ 227line 226 didn't jump to line 227, because the condition on line 226 was never true if display: 

plt = rgb.displayRGB(rgbImage, False) 

plt.title("zscale") 

plt.show() 

 

@unittest.skipUnless(HAVE_MATPLOTLIB, NO_MATPLOTLIB_STRING) 

def testWriteStars(self): 

"""Test writing RGB files to disk""" 

asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q) 

rgbImage = asinhMap.makeRgbImage( 

self.images[R], self.images[G], self.images[B]) 

with lsst.utils.tests.getTempFilePath(".png") as fileName: 

rgb.writeRGB(fileName, rgbImage) 

self.assertTrue(os.path.exists(fileName)) 

 

def testSaturated(self): 

"""Test interpolating saturated pixels""" 

 

satValue = 1000.0 

for f in [R, G, B]: 

self.images[f] = saturate(self.images[f], satValue) 

 

rgb.replaceSaturatedPixels( 

self.images[R], self.images[G], self.images[B], 1, 2000) 

# 

# Check that we replaced those NaNs with some reasonable value 

# 

for f in [R, G, B]: 

self.assertTrue(np.isfinite( 

self.images[f].getImage().getArray()).all()) 

 

if False: 

ds9.mtv(self.images[B], frame=0, title="B") 

ds9.mtv(self.images[G], frame=1, title="G") 

ds9.mtv(self.images[R], frame=2, title="R") 

# 

# Prepare for generating an output file 

# 

for f in [R, G, B]: 

self.images[f] = self.images[f].getImage() 

 

asinhMap = rgb.AsinhMapping(self.min, self.range, self.Q) 

rgbImage = asinhMap.makeRgbImage( 

self.images[R], self.images[G], self.images[B]) 

 

271 ↛ 272line 271 didn't jump to line 272, because the condition on line 271 was never true if display: 

rgb.displayRGB(rgbImage) 

 

@unittest.skipUnless(HAVE_SCIPY_MISC, "Resizing images requires scipy.misc") 

def testStarsResizeToSize(self): 

"""Test creating an RGB image of a specified size""" 

 

xSize = self.images[R].getWidth()//2 

ySize = self.images[R].getHeight()//2 

for rgbImages in ([self.images[R], self.images[G], self.images[B]], 

[afwImage.ImageU(_.getArray().astype('uint16')) for _ in [ 

self.images[R], self.images[G], self.images[B]]]): 

rgbImage = rgb.AsinhZScaleMapping(rgbImages[0]).makeRgbImage(*rgbImages, 

xSize=xSize, ySize=ySize) 

 

if display: 

rgb.displayRGB(rgbImage) 

 

@unittest.skipUnless(HAVE_SCIPY_MISC, "Resizing images requires scipy.misc") 

def testStarsResizeSpecifications(self): 

"""Test creating an RGB image changing the output """ 

 

rgbImages = [self.images[R], self.images[G], self.images[B]] 

map = rgb.AsinhZScaleMapping(rgbImages[0]) 

 

for xSize, ySize, frac in [(self.images[R].getWidth()//2, self.images[R].getHeight()//2, None), 

(2*self.images[R].getWidth(), None, None), 

(self.images[R].getWidth()//2, None, None), 

(None, self.images[R].getHeight()//2, None), 

(None, None, 0.5), 

(None, None, 2), 

]: 

rgbImage = map.makeRgbImage( 

*rgbImages, xSize=xSize, ySize=ySize, rescaleFactor=frac) 

 

h, w = rgbImage.shape[0:2] 

self.assertTrue(xSize is None or xSize == w) 

self.assertTrue(ySize is None or ySize == h) 

self.assertTrue(frac is None or w == int(frac*self.images[R].getWidth()), 

"%g == %g" % (w, int((frac if frac else 1)*self.images[R].getWidth()))) 

 

if display: 

rgb.displayRGB(rgbImage) 

 

@unittest.skipUnless(HAVE_SCIPY_MISC, "Resizing images requires scipy.misc") 

@unittest.skipUnless(HAVE_MATPLOTLIB, NO_MATPLOTLIB_STRING) 

def testMakeRGBResize(self): 

"""Test the function that does it all, including rescaling""" 

rgb.makeRGB(self.images[R], self.images[G], 

self.images[B], xSize=40, ySize=60) 

 

with lsst.utils.tests.getTempFilePath(".png") as fileName: 

rgb.makeRGB(self.images[R], self.images[G], 

self.images[B], fileName=fileName, rescaleFactor=0.5) 

self.assertTrue(os.path.exists(fileName)) 

 

def writeFileLegacyAPI(self, fileName): 

"""Test that the legacy API still works, although it's deprecated""" 

asinh = rgb.asinhMappingF(self.min, self.range, self.Q) 

rgbImage = rgb.RgbImageF( 

self.images[R], self.images[G], self.images[B], asinh) 

if False: 

ds9.mtv(self.images[B], frame=0, title="B") 

ds9.mtv(self.images[G], frame=1, title="G") 

ds9.mtv(self.images[R], frame=2, title="R") 

 

rgbImage.write(fileName) 

 

@unittest.skipUnless(HAVE_MATPLOTLIB, NO_MATPLOTLIB_STRING) 

def testWriteStarsLegacyAPI(self): 

with lsst.utils.tests.getTempFilePath(".png") as fileName: 

self.writeFileLegacyAPI(fileName) 

self.assertTrue(os.path.exists(fileName)) 

 

 

class TestMemory(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

354 ↛ 355line 354 didn't jump to line 355, because the condition on line 354 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()