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

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 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/>. 

# 

 

from copy import copy 

import unittest 

 

import numpy as np 

 

from lsst.meas.astrom.pessimistic_pattern_matcher_b_3D \ 

import PessimisticPatternMatcherB 

from lsst.log import Log 

 

__deg_to_rad__ = np.pi/180 

 

 

class TestPessimisticPatternMatcherB(unittest.TestCase): 

 

"""Unittest suite for the Pessimistic Pattern Matcher B. 

""" 

 

def setUp(self): 

np.random.seed(12345) 

 

n_points = 1000 

# reference_obj_array is a number array representing 

# 3D points randomly draw on a 1 sq deg patch. 

self.reference_obj_array = np.empty((n_points, 4)) 

cos_theta_array = np.random.uniform( 

np.cos(np.pi/2 + 0.5*__deg_to_rad__), 

np.cos(np.pi/2 - 0.5*__deg_to_rad__), size=n_points) 

sin_theta_array = np.sqrt(1 - cos_theta_array**2) 

phi_array = np.random.uniform(-0.5, 0.5, size=n_points)*__deg_to_rad__ 

self.reference_obj_array[:, 0] = sin_theta_array*np.cos(phi_array) 

self.reference_obj_array[:, 1] = sin_theta_array*np.sin(phi_array) 

self.reference_obj_array[:, 2] = cos_theta_array 

self.reference_obj_array[:, 3] = ( 

np.random.power(1.2, size=n_points)*4 + 20) 

 

# Our initial source catalog is a straight copy of the reference 

# array at first. In some of the tests we will add rotations and 

# shifts to the data in order to test the input and outputs of our 

# matcher. 

self.source_obj_array = copy(self.reference_obj_array) 

self.log = Log() 

 

def testConstructPattern(self): 

""" Test that a specified pattern can be found in the reference 

data and that the explicit ids match. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

pattern_struct = self.pyPPMb._construct_pattern_and_shift_rot_matrix( 

self.source_obj_array[:6, :3], 6, np.cos(np.radians(60. / 3600.)), 

np.cos(np.radians(1.0)) ** 2, np.radians(5./3600.)) 

pattern_list = pattern_struct.ref_candidates 

self.assertGreater(len(pattern_list), 0) 

self.assertEqual(pattern_list[0], 0) 

self.assertEqual(pattern_list[1], 1) 

self.assertEqual(pattern_list[2], 2) 

self.assertEqual(pattern_list[3], 3) 

self.assertEqual(pattern_list[4], 4) 

self.assertEqual(pattern_list[5], 5) 

 

pattern_struct = self.pyPPMb._construct_pattern_and_shift_rot_matrix( 

self.source_obj_array[:9, :3], 6, np.cos(np.radians(60. / 3600.)), 

np.cos(np.radians(1.0)) ** 2, np.radians(5./3600.)) 

pattern_list = pattern_struct.ref_candidates 

self.assertGreater(len(pattern_list), 0) 

self.assertEqual(pattern_list[0], 0) 

self.assertEqual(pattern_list[1], 1) 

self.assertEqual(pattern_list[2], 2) 

self.assertEqual(pattern_list[3], 3) 

self.assertEqual(pattern_list[4], 4) 

self.assertEqual(pattern_list[5], 5) 

 

pattern_struct = self.pyPPMb._construct_pattern_and_shift_rot_matrix( 

self.source_obj_array[[2, 4, 8, 16, 32, 64], :3], 6, 

np.cos(np.radians(60. / 3600.)), np.cos(np.radians(1.0)) ** 2, 

np.radians(5./3600.)) 

pattern_list = pattern_struct.ref_candidates 

self.assertEqual(pattern_list[0], 2) 

self.assertEqual(pattern_list[1], 4) 

self.assertEqual(pattern_list[2], 8) 

self.assertEqual(pattern_list[3], 16) 

self.assertEqual(pattern_list[4], 32) 

self.assertEqual(pattern_list[5], 64) 

 

def testMatchPerfect(self): 

""" Input objects that have no shift or rotation to the matcher 

and test that we return a match. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60., max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testOptimisticMatch(self): 

""" Test the optimistic mode of the pattern matcher. That is 

the algorithm with the early exit strategy as described in 

Tabur 2007. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=1, max_n_patterns=100, max_shift=60., max_rotation=6.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testMatchSkip(self): 

""" Test the ability to skip specified patterns in the matching 

process. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60., max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=np.array([0])) 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testMatchMoreSources(self): 

""" Test the case where we have more sources than references 

but no rotation or shift. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:500, :3], 

log=self.log) 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60.0, max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array[:500])) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testMatchMoreReferences(self): 

""" Test the case where we have more references than sources 

but no rotation or shift. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array[:500], n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60., max_rotation=1.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array[:500])) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testShift(self): 

""" Test the matcher when a shift is applied to the data. 

 

We say shift here as while we are rotating the unit-sphere in 3D, on 

our 'focal plane' this will appear as a shift. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

theta = np.radians(45.0 / 3600.) 

cos_theta = np.cos(theta) 

sin_theta = np.sin(theta) 

theta_rotation = self.pyPPMb._create_spherical_rotation_matrix( 

np.array([0, 0, 1]), cos_theta, sin_theta) 

 

self.source_obj_array[:, :3] = np.dot( 

theta_rotation, 

self.source_obj_array[:, :3].transpose()).transpose() 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60, max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testRotation(self): 

""" Test the matcher for when a roation is applied to the data. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

phi = 2.5*__deg_to_rad__ 

cos_phi = np.cos(phi) 

sin_phi = np.sin(phi) 

phi_rotation = self.pyPPMb._create_spherical_rotation_matrix( 

np.array([1, 0, 0]), cos_phi, sin_phi) 

 

self.source_obj_array[:, :3] = np.dot( 

phi_rotation, self.source_obj_array[:, :3].transpose()).transpose() 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60, max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testShiftRotation(self): 

""" Test both a shift and rotation being applied to the data. 

""" 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

theta = np.radians(45.0 / 3600.) 

cos_theta = np.cos(theta) 

sin_theta = np.sin(theta) 

theta_rotation = self.pyPPMb._create_spherical_rotation_matrix( 

np.array([0, 0, 1]), cos_theta, sin_theta) 

 

phi = 2.5 * __deg_to_rad__ 

cos_phi = np.cos(phi) 

sin_phi = np.sin(phi) 

phi_rotation = self.pyPPMb._create_spherical_rotation_matrix( 

np.array([1, 0, 0]), cos_phi, sin_phi) 

 

shift_rot_matrix = np.dot(theta_rotation, phi_rotation) 

 

self.source_obj_array[:, :3] = np.dot( 

shift_rot_matrix, 

self.source_obj_array[:, :3].transpose()).transpose() 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60., max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 0.01/3600.0 * __deg_to_rad__)) 

 

def testLinearDistortion(self): 

""" Create a simple linear distortion and test that the correct 

references are still matched. 

""" 

 

self.pyPPMb = PessimisticPatternMatcherB( 

reference_array=self.reference_obj_array[:, :3], 

log=self.log) 

 

max_z = np.cos(np.pi/2 + 0.5 * __deg_to_rad__) 

min_z = np.cos(np.pi/2 - 0.5 * __deg_to_rad__) 

# The max shift in position we add to the position will be 25 

# arcseconds. 

max_distort = 25.0 / 3600. * __deg_to_rad__ 

self.source_obj_array[:, 2] = ( 

self.source_obj_array[:, 2] - 

max_distort * (self.source_obj_array[:, 2] - min_z) / 

(max_z - min_z)) 

# Renomalize the 3 vectors to be unit length. 

distorted_dists = np.sqrt(self.source_obj_array[:, 0] ** 2 + 

self.source_obj_array[:, 1] ** 2 + 

self.source_obj_array[:, 2] ** 2) 

self.source_obj_array[:, 0] /= distorted_dists 

self.source_obj_array[:, 1] /= distorted_dists 

self.source_obj_array[:, 2] /= distorted_dists 

 

match_struct = self.pyPPMb.match( 

source_array=self.source_obj_array, n_check=9, n_match=6, 

n_agree=2, max_n_patterns=100, max_shift=60., max_rotation=5.0, 

max_dist=5., min_matches=30, pattern_skip_array=None) 

 

self.assertEqual(len(match_struct.match_ids), 

len(self.reference_obj_array)) 

self.assertTrue( 

np.all(match_struct.distances_rad < 10 / 3600.0 * __deg_to_rad__)) 

 

 

320 ↛ 321line 320 didn't jump to line 321, because the condition on line 320 was never trueif __name__ == '__main__': 

unittest.main()