-
Notifications
You must be signed in to change notification settings - Fork 0
/
testOPMb.py
122 lines (96 loc) · 4.73 KB
/
testOPMb.py
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
from __future__ import division, print_function, absolute_import
from copy import copy
import unittest
import numpy as np
from optimistic_pattern_matcher_b import OptimisticPatternMatcherB
__deg_to_rad__ = np.pi/180
class TestPythonOptimisticPatternMatcherB(unittest.TestCase):
"""Unittest suite for the python implimentation of
Optimistic Pattern Matcher B.
"""
def setUp(self):
np.random.seed(12345)
n_points = 1000
self.reference_catalog = np.random.rand(n_points, 3)*4096
self.reference_catalog[:, 2] = (
np.random.power(1.2, size=n_points)*4 + 20)
self.source_catalog = copy(self.reference_catalog)
def testInit(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog, max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
def testConstructAndMatchPattern(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog, max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
pattern_list, cos_theta, sin_theta = self.pyOPMb._construct_and_match_pattern(
self.source_catalog[:5, :2], 5)
self.assertGreater(len(pattern_list), 0)
self.assertIsNotNone(cos_theta)
self.assertIsNotNone(sin_theta)
pattern_list, cos_theta, sin_theta = self.pyOPMb._construct_and_match_pattern(
self.source_catalog[:8, :2], 5)
self.assertGreater(len(pattern_list), 0)
self.assertIsNotNone(cos_theta)
self.assertIsNotNone(sin_theta)
def testMatchPerfect(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog, max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
matches, distances = self.pyOPMb.match(self.source_catalog, 8, 5)
self.assertEqual(len(matches), len(self.reference_catalog))
self.assertTrue(np.all(distances < 1e-8))
def testMatchMoreSources(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog[:500], max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
matches, distances = self.pyOPMb.match(self.source_catalog, 8, 5)
self.assertEqual(len(matches), len(self.reference_catalog[:500]))
self.assertTrue(np.all(distances < 1e-8))
def testMatchMoreReferences(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog, max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
matches, distances = self.pyOPMb.match(self.source_catalog[:500], 8, 5)
self.assertEqual(len(matches), len(self.reference_catalog[:500]))
self.assertTrue(np.all(distances < 1e-8))
def testMatchShiftRotated(self):
self.pyOPMb = OptimisticPatternMatcherB(
reference_catalog=self.reference_catalog, max_shift=1000.0,
max_rotation=10.0, dist_tol=75, ang_tol=0.75, max_match_dist=3,
min_matches=30, max_n_patterns=250)
# Shift the source catalog by a given amount.
shift_xy = np.random.uniform(-500, 500, size=2)
# Pick a random rotation center.
center = np.random.uniform(1, 4096, size=2)
# Pick a random rotation.
rot = np.random.uniform(-6, 6)
print("\tShift %.2f %.2f" % (shift_xy[0], shift_xy[1]))
print("\tCenter %.2f %.2f, Rotation %.4f" %
(center[0], center[1], rot))
tmp_source = np.empty_like(self.source_catalog)
tmp_source[:, 2] = self.source_catalog[:, 2]
tmp_source[:, 0] = (
(self.source_catalog[:, 0] -
center[0])*np.cos(rot*__deg_to_rad__) -
(self.source_catalog[:, 1] -
center[1])*np.sin(rot*__deg_to_rad__) + shift_xy[0])
tmp_source[:, 1] = (
(self.source_catalog[:, 0] -
center[0])*np.sin(rot*__deg_to_rad__) +
(self.source_catalog[:, 1] -
center[1])*np.cos(rot*__deg_to_rad__) + shift_xy[1])
tmp_source[:, 0] += center[0]
tmp_source[:, 1] += center[1]
matches, distances = self.pyOPMb.match(tmp_source, 8, 5)
self.assertEqual(len(matches), len(self.reference_catalog))
self.assertTrue(np.all(distances < 1e-8))
def tearDown(self):
pass
if __name__ == '__main__':
unittest.main()