-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_KNN.py
141 lines (107 loc) · 4.38 KB
/
test_KNN.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import unittest
from KNN_2D import KNN
from AnySizeVectorKNN import AnySizeVectorKNN
class Test2DKNN(unittest.TestCase):
#CREATE METHODS TO INITIALIZE TESTING EXAMPLES, BEFORE EACH TEST CASE
def setUp(self):
self.data_set = [(-2,4),(0,-2),(-1,0),(3,5),(-2,-3),(3,2)]
self.k = 3
self.ref_point = (0,3)
self.KNN = KNN()
def test_initial_knn(self):
knn = self.KNN
self.assertIsInstance(knn, KNN)
self.assertIsInstance(knn.k, int)
self.assertIsInstance(knn.ref_point, tuple)
self.assertIsInstance(knn.data_set, list)
# default KNN state
self.assertEqual(knn.k, 2)
self.assertEqual(knn.ref_point, (0,0))
self.assertEqual(knn.data_set, [])
# updating KNN State
knn.k = self.k
knn.ref_point = self.ref_point
knn.data_set = self.data_set
self.assertEqual(knn.k, 3)
self.assertEqual(knn.ref_point, (0,3))
self.assertEqual(knn.data_set, [(-2,4),(0,-2),(-1,0),(3,5),(-2,-3),(3,2)])
def test_knn_get_distance(self):
self.assertEqual(self.KNN.getDistance((0,1)), 1)
self.assertEqual(self.KNN.getDistance((2,4)), 4.47213595499958)
self.KNN.ref_point=(2,4)
self.assertEqual(self.KNN.getDistance((2,4)), 0)
def test_knn_format_data(self):
initial_formatted_data = self.KNN.formatData()
self.assertIsInstance(initial_formatted_data, list)
self.assertEqual(len(initial_formatted_data), 0)
self.KNN.data_set = self.data_set
new_formatted_data = self.KNN.formatData()
self.assertIsInstance(new_formatted_data, list)
self.assertIsInstance(new_formatted_data[0], dict)
self.assertEqual(len(new_formatted_data), 6)
self.assertEqual(new_formatted_data[0], {'coordinates': (-2, 4), 'distance': 4.47213595499958})
def test_knn_solve(self):
initial_solve = self.KNN.solve()
self.assertIsInstance(initial_solve, list)
self.assertEqual(len(initial_solve), 0)
self.KNN.data_set = self.data_set
new_solve = self.KNN.solve()
self.assertIsInstance(new_solve, list)
self.assertIsInstance(new_solve[0], dict)
self.KNN.k = self.k
new_k_solve = self.KNN.solve()
self.assertEqual(len(new_k_solve), 3)
self.assertLessEqual(new_k_solve[0]['distance'], new_k_solve[-1]['distance'])
class TestAnySizeVectorKNN(unittest.TestCase):
#CREATE METHODS TO INITIALIZE TESTING EXAMPLES, BEFORE EACH TEST CASE
def setUp(self):
self.data_set = [(-2,4,67,11),(0,-2,9),(20,5,6,11,-1,0),(1,0,0,2,0),(-2,-3),(3), (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.1), (1,2,3,4,5,6,7,8,9)]
self.k = 4
self.ref_point = (0,0,0,0,0,0,0,0)
self.KNN = AnySizeVectorKNN()
def test_any_size_vector_initial_knn(self):
knn = self.KNN
self.assertIsInstance(knn, AnySizeVectorKNN)
self.assertIsInstance(knn.k, int)
self.assertIsInstance(knn.ref_point, tuple)
self.assertIsInstance(knn.data_set, list)
# default KNN state
self.assertEqual(knn.k, 2)
self.assertEqual(knn.ref_point, (0,0))
self.assertEqual(knn.data_set, [])
# updating KNN State
knn.k = self.k
knn.ref_point = self.ref_point
knn.data_set = self.data_set
self.assertEqual(knn.k, 4)
self.assertEqual(knn.ref_point, (0,0,0,0,0,0,0,0))
self.assertEqual(knn.data_set, self.data_set)
def test_any_size_vector_knn_get_distance(self):
self.assertEqual(self.KNN.getDistance((0,1,0,0,0)), 1)
self.assertEqual(self.KNN.getDistance((2,4,1,4,2,1,0,2,9)), 11.269427669584644)
self.KNN.ref_point=(2,4,0,0,0,0,0)
self.assertEqual(self.KNN.getDistance((2,4)), 0)
def test_any_size_vector_knn_format_data(self):
initial_formatted_data = self.KNN.formatData()
self.assertIsInstance(initial_formatted_data, list)
self.assertEqual(len(initial_formatted_data), 0)
self.KNN.data_set = self.data_set
new_formatted_data = self.KNN.formatData()
self.assertIsInstance(new_formatted_data, list)
self.assertIsInstance(new_formatted_data[0], dict)
self.assertEqual(len(new_formatted_data), 8)
self.assertEqual(new_formatted_data[0], {'point': (-2, 4, 67, 11), 'distance': 68.044103344816})
def test_any_size_vector_knn_solve(self):
initial_solve = self.KNN.solve()
self.assertIsInstance(initial_solve, list)
self.assertEqual(len(initial_solve), 0)
self.KNN.data_set = self.data_set
new_solve = self.KNN.solve()
self.assertIsInstance(new_solve, list)
self.assertIsInstance(new_solve[0], dict)
self.KNN.k = self.k
new_k_solve = self.KNN.solve()
self.assertEqual(len(new_k_solve), 4)
self.assertLessEqual(new_k_solve[0]['distance'], new_k_solve[-1]['distance'])
if __name__ == '__main__':
unittest.main()