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

# 

# LSST Data Management System 

# Copyright 2008-2017 AURA/LSST. 

# 

# 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 of the DipoleFitAlgorithm and its related tasks and plugins. 

 

Each test generates a fake image with two synthetic dipoles as input data. 

""" 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.table as afwTable 

import lsst.meas.base as measBase 

from lsst.ip.diffim.dipoleFitTask import (DipoleFitAlgorithm, DipoleFitTask) 

import lsst.ip.diffim.utils as ipUtils 

 

 

class DipoleTestImage(object): 

"""!Class to initialize test dipole image used by all tests below. 

 

@var display: Display (plot) the output dipole thumbnails (matplotlib) 

@var verbose: be verbose during fitting 

@var xc: x coordinate (pixels) of center(s) of input dipole(s) 

@var yc: y coordinate (pixels) of center(s) of input dipole(s) 

@var flux: flux(es) of input dipole(s) 

@var gradientParams: tuple with three parameters for linear background gradient 

@var offsets: pixel coordinates between lobes of dipoles 

 

Also stores all parameters used to generate the test image (to compare to fitting results). 

""" 

 

def __init__(self, xc=None, yc=None, flux=None, offsets=None, gradientParams=None): 

"""!Store the parameters, create the test image and run detection on it. 

 

@param xc iterable x coordinate (pixels) of center(s) of input dipole(s) 

@param yc iterable y coordinate (pixels) of center(s) of input dipole(s) 

@param offsets iterable pixel coord offsets between lobes of dipole(s) 

@param flux iterable fluxes of pos/neg lobes of dipole(s) 

@param gradientParams iterable three parameters for linear background gradient 

""" 

self.display = False # Display (plot) the output dipole thumbnails (matplotlib) 

self.verbose = False # be verbose during fitting 

 

self.xc = xc if xc is not None else [65.3, 24.2] 

self.yc = yc if yc is not None else [38.6, 78.5] 

self.offsets = offsets if offsets is not None else np.array([-2., 2.]) 

self.flux = flux if flux is not None else [2500., 2345.] 

self.gradientParams = gradientParams if gradientParams is not None else [10., 3., 5.] 

 

# The default tolerance for comparisons of fitted parameters with input values. 

# Given the noise in the input images (default noise value of 2.), this is a 

# useful test of algorithm robustness, and will guard against future regressions. 

self.rtol = 0.01 

 

self.generateTestImage() 

 

def generateTestImage(self): 

self.testImage = ipUtils.DipoleTestImage( 

w=100, h=100, 

xcenPos=self.xc + self.offsets, 

ycenPos=self.yc + self.offsets, 

xcenNeg=self.xc - self.offsets, 

ycenNeg=self.yc - self.offsets, 

flux=self.flux, fluxNeg=self.flux, 

noise=2., # Note the input noise - this affects the relative tolerances used. 

gradientParams=self.gradientParams) 

 

 

class DipoleFitTest(lsst.utils.tests.TestCase): 

"""!A test case for separately testing the dipole fit algorithm 

directly, and the single frame measurement. 

 

In each test, create a simulated diffim with two dipoles, noise, 

and a linear background gradient in the pre-sub images then 

compare the input fluxes/centroids with the fitted results. 

""" 

 

def testDipoleAlgorithm(self): 

"""!Test the dipole fitting algorithm directly (fitDipole()). 

 

Test that the resulting fluxes/centroids are very close to the 

input values for both dipoles in the image. 

""" 

params = DipoleTestImage() 

catalog = params.testImage.detectDipoleSources(minBinSize=32) 

 

for s in catalog: 

fp = s.getFootprint() 

self.assertTrue(len(fp.getPeaks()) == 2) 

 

rtol = params.rtol 

offsets = params.offsets 

testImage = params.testImage 

for i, s in enumerate(catalog): 

alg = DipoleFitAlgorithm(testImage.diffim, testImage.posImage, testImage.negImage) 

result, _ = alg.fitDipole( 

s, rel_weight=0.5, separateNegParams=False, 

verbose=params.verbose, display=params.display) 

 

self.assertFloatsAlmostEqual((result.posFlux + abs(result.negFlux))/2., 

params.flux[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result.posCentroidX, params.xc[i] + offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result.posCentroidY, params.yc[i] + offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result.negCentroidX, params.xc[i] - offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result.negCentroidY, params.yc[i] - offsets[i], rtol=rtol) 

 

def _runDetection(self, params): 

"""!Run 'diaSource' detection on the diffim, including merging of 

positive and negative sources. 

 

Then run DipoleFitTask on the image and return the resulting catalog. 

""" 

 

# Create the various tasks and schema -- avoid code reuse. 

testImage = params.testImage 

detectTask, schema = testImage.detectDipoleSources(doMerge=False, minBinSize=32) 

 

measureConfig = measBase.SingleFrameMeasurementConfig() 

 

measureConfig.slots.calibFlux = None 

measureConfig.slots.modelFlux = None 

measureConfig.slots.gaussianFlux = None 

measureConfig.slots.shape = None 

measureConfig.slots.centroid = "ip_diffim_NaiveDipoleCentroid" 

measureConfig.doReplaceWithNoise = False 

 

measureConfig.plugins.names = ["base_CircularApertureFlux", 

"base_PixelFlags", 

"base_SkyCoord", 

"base_PsfFlux", 

"ip_diffim_NaiveDipoleCentroid", 

"ip_diffim_NaiveDipoleFlux", 

"ip_diffim_PsfDipoleFlux"] 

 

# Here is where we make the dipole fitting task. It can run the other measurements as well. 

# This is an example of how to pass it a custom config. 

measureTask = DipoleFitTask(config=measureConfig, schema=schema) 

 

table = afwTable.SourceTable.make(schema) 

detectResult = detectTask.run(table, testImage.diffim) 

# catalog = detectResult.sources 

# deblendTask.run(self.dipole, catalog, psf=self.dipole.getPsf()) 

 

fpSet = detectResult.fpSets.positive 

fpSet.merge(detectResult.fpSets.negative, 2, 2, False) 

sources = afwTable.SourceCatalog(table) 

fpSet.makeSources(sources) 

 

measureTask.run(sources, testImage.diffim, testImage.posImage, testImage.negImage) 

return sources 

 

def _checkTaskOutput(self, params, sources, rtol=None): 

"""!Compare the fluxes/centroids in `sources` are entered 

into the correct slots of the catalog, and have values that 

are very close to the input values for both dipoles in the 

image. 

 

Also test that the resulting fluxes are close to those 

generated by the existing ip_diffim_DipoleMeasurement task 

(PsfDipoleFit). 

""" 

 

182 ↛ 184line 182 didn't jump to line 184, because the condition on line 182 was never false if rtol is None: 

rtol = params.rtol 

offsets = params.offsets 

for i, r1 in enumerate(sources): 

result = r1.extract("ip_diffim_DipoleFit*") 

self.assertFloatsAlmostEqual((result['ip_diffim_DipoleFit_pos_instFlux'] + 

abs(result['ip_diffim_DipoleFit_neg_instFlux']))/2., 

params.flux[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_pos_centroid_x'], 

params.xc[i] + offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_pos_centroid_y'], 

params.yc[i] + offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_neg_centroid_x'], 

params.xc[i] - offsets[i], rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_neg_centroid_y'], 

params.yc[i] - offsets[i], rtol=rtol) 

# Note this is dependent on the noise (variance) being realistic in the image. 

# otherwise it throws off the chi2 estimate, which is used for classification: 

self.assertTrue(result['ip_diffim_DipoleFit_flag_classification']) 

 

# compare to the original ip_diffim_PsfDipoleFlux measurements 

result2 = r1.extract("ip_diffim_PsfDipoleFlux*") 

self.assertFloatsAlmostEqual((result['ip_diffim_DipoleFit_pos_instFlux'] + 

abs(result['ip_diffim_DipoleFit_neg_instFlux']))/2., 

(result2['ip_diffim_PsfDipoleFlux_pos_instFlux'] + 

abs(result2['ip_diffim_PsfDipoleFlux_neg_instFlux']))/2., 

rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_pos_centroid_x'], 

result2['ip_diffim_PsfDipoleFlux_pos_centroid_x'], 

rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_pos_centroid_y'], 

result2['ip_diffim_PsfDipoleFlux_pos_centroid_y'], 

rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_neg_centroid_x'], 

result2['ip_diffim_PsfDipoleFlux_neg_centroid_x'], 

rtol=rtol) 

self.assertFloatsAlmostEqual(result['ip_diffim_DipoleFit_neg_centroid_y'], 

result2['ip_diffim_PsfDipoleFlux_neg_centroid_y'], 

rtol=rtol) 

 

return result 

 

def testDipoleTask(self): 

"""!Test the dipole fitting singleFramePlugin. 

 

Test that the resulting fluxes/centroids are entered into the 

correct slots of the catalog, and have values that are very 

close to the input values for both dipoles in the image. 

 

Also test that the resulting fluxes are close to those 

generated by the existing ip_diffim_DipoleMeasurement task 

(PsfDipoleFit). 

""" 

params = DipoleTestImage() 

sources = self._runDetection(params) 

self._checkTaskOutput(params, sources) 

 

def testDipoleTaskNoPosImage(self): 

"""!Test the dipole fitting singleFramePlugin in the case where no 

`posImage` is provided. It should be the same as above because 

`posImage` can be constructed from `diffim+negImage`. 

 

Test that the resulting fluxes/centroids are entered into the 

correct slots of the catalog, and have values that are very 

close to the input values for both dipoles in the image. 

 

Also test that the resulting fluxes are close to those 

generated by the existing ip_diffim_DipoleMeasurement task 

(PsfDipoleFit). 

""" 

params = DipoleTestImage() 

params.testImage.posImage = None 

sources = self._runDetection(params) 

self._checkTaskOutput(params, sources) 

 

def testDipoleTaskNoNegImage(self): 

"""!Test the dipole fitting singleFramePlugin in the case where no 

`negImage` is provided. It should be the same as above because 

`negImage` can be constructed from `posImage-diffim`. 

 

Test that the resulting fluxes/centroids are entered into the 

correct slots of the catalog, and have values that are very 

close to the input values for both dipoles in the image. 

 

Also test that the resulting fluxes are close to those 

generated by the existing ip_diffim_DipoleMeasurement task 

(PsfDipoleFit). 

""" 

params = DipoleTestImage() 

params.testImage.negImage = None 

sources = self._runDetection(params) 

self._checkTaskOutput(params, sources) 

 

def testDipoleTaskNoPreSubImages(self): 

"""!Test the dipole fitting singleFramePlugin in the case where no 

pre-subtraction data (`posImage` or `negImage`) are provided. 

In this case it just fits a dipole model to the diffim 

(dipole) image alone. Note that this test will only pass for 

widely-separated dipoles. 

 

Test that the resulting fluxes/centroids are entered into the 

correct slots of the catalog, and have values that are very 

close to the input values for both dipoles in the image. 

 

Also test that the resulting fluxes are close to those 

generated by the existing ip_diffim_DipoleMeasurement task 

(PsfDipoleFit). 

""" 

params = DipoleTestImage() 

params.testImage.posImage = params.testImage.negImage = None 

sources = self._runDetection(params) 

self._checkTaskOutput(params, sources) 

 

def testDipoleEdge(self): 

"""!Test the too-close-to-image-edge scenario for dipole fitting 

singleFramePlugin. 

 

Test that the dipoles which are too close to the edge are 

flagged as such in the catalog and do not raise an error that is 

not caught. Make sure both diaSources are actually detected, 

if not measured. 

""" 

 

params = DipoleTestImage(xc=[5.3, 4.8], yc=[4.6, 96.5]) 

sources = self._runDetection(params) 

 

self.assertTrue(len(sources) == 2) 

 

for i, s in enumerate(sources): 

result = s.extract("ip_diffim_DipoleFit*") 

self.assertTrue(result.get("ip_diffim_DipoleFit_flag")) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()