-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunittests.py
133 lines (107 loc) · 4.72 KB
/
unittests.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
###################################################################################################
####
#### CClabeling -- unittests
####
###################################################################################################
import unittest
import os, sys
import CC_labeling_8, graphics
import numpy as np
class TestSequenceFunctions(unittest.TestCase): #subclass unittest.TestCase
def testDisjointSets(self):
d = CC_labeling_8.disjointSet(100)
d.makeSet(3)
d.makeSet(4)
d.makeSet(5)
d.makeSet(6)
d.union(4,5)
d.union(4,6)
self.assertListEqual([4,3],[d.find(6),d.find(3)])
def testDisjointRegions(self):
d = CC_labeling_8.disjointRegions(100)
d.makeSet(3,1,1)
d.makeSet(4,2,1)
d.makeSet(5,4,3)
d.makeSet(6,4,2)
d.union(4,5)
d.union(4,6)
self.assertListEqual([(4, (2, 1)),(3, (1, 1))],[d.find(6),d.find(3)])
def testComponentLabeling(self):
mat = [[1, -1, 2, 2, 2], [-1, -1, 2, 2, 2],
[2, 2, 2, 2, 2], [2, 2, 2, 2, 2], [2, 2, 2, 2, 2]]
graphics.plotHeatMap(mat)
cclab = CC_labeling_8.CC_lab(mat)
cclab.connectedComponentLabel()
graphics.plotHeatMap(cclab.labels)
labelsComp = [[0, 1, 3, 3, 3], [1, 1, 3, 3, 3],
[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3]]
for i in range(len(labelsComp)):
self.assertListEqual(labelsComp[i],cclab.labels[i])
def testComponentLabeling2(self):
mat = [[1, -1, -1, 2, 1,3], [2, 2, 2, 2, 3,3],
[7, 7, 7, 7, 3,3], [6, 6, 7, 5, 4, 5], [6, 6, 6, 5, 5, 5]]
graphics.plotHeatMap(mat)
cclab = CC_labeling_8.CC_lab(mat)
cclab.connectedComponentLabel()
graphics.plotHeatMap(cclab.labels)
labelsComp = [[0, 1, 1, 5, 3, 4], [5, 5, 5, 5, 4, 4],
[6, 6, 6, 6, 4, 4], [7, 7, 6, 8, 9, 8],
[7, 7, 7, 8, 8, 8]]
for i in range(len(labelsComp)):
self.assertListEqual(labelsComp[i],cclab.labels[i])
def testComponentLabeling3(self): # visuel
map_,points, volumes = graphics.MapGenerator(100, 100, 10, 100)
graphics.plotHeatMap(map_)
cclab = CC_labeling_8.CC_lab(map_)
cclab.connectedComponentLabel()
graphics.plotHeatMap(cclab.labels)
def testComponentLabelingNeighborRegions(self):
mat = [[1, -1, -1, 2, 1,3], [2, 2, 2, 2, 3,3],
[7, 7, 7, 7, 3,3], [6, 6, 7, 5, 4, 5], [6, 6, 6, 5, 5, 5]]
graphics.plotHeatMap(mat)
cclab = CC_labeling_8.CC_lab(mat)
cclab.connectedComponentLabel()
graphics.plotHeatMap(cclab.labels)
computed = []
for i in range(len(mat)):
for j in range(len(mat[0])):
computed.append([x for x in (cclab.neighborRegions(cclab.labels[i][j],mat))])
labelsComp = [[1, 5], [0, 5], [0, 5], [0, 1, 3, 4, 6], [4, 5], [3, 5, 6, 8, 9], [0, 1, 3, 4, 6], [0, 1, 3, 4, 6],
[0, 1, 3, 4, 6], [0, 1, 3, 4, 6], [3, 5, 6, 8, 9], [3, 5, 6, 8, 9], [4, 5, 7, 8, 9], [4, 5, 7, 8, 9],
[4, 5, 7, 8, 9], [4, 5, 7, 8, 9], [3, 5, 6, 8, 9], [3, 5, 6, 8, 9],
[6, 8], [6, 8], [4, 5, 7, 8, 9], [4, 6, 7, 9], [4, 6, 8], [4, 6, 7, 9], [6, 8], [6, 8], [6, 8],
[4, 6, 7, 9], [4, 6, 7, 9], [4, 6, 7, 9]]
for i in range(len(labelsComp)):
self.assertListEqual(labelsComp[i],computed[i])
def assertListAlmostEqual(self, list1, list2, tol):
"""
Assert if two lists are almost equal
"""
self.assertEqual(len(list1), len(list2))
for a, b in zip(list1, list2):
self.assertAlmostEqual(a, b, tol)
def assertListEqual(self, list1, list2):
"""
Assert if two lists are almost equal
"""
self.assertEqual(len(list1), len(list2))
for a, b in zip(list1, list2):
self.assertEqual(a, b)
def assertMatrixAlmostEqual(self, mat1, mat2, tol):
"""
Assert if two matrices are almost equal
"""
self.assertEqual((len(mat1[0]),len(mat2[0])), (len(mat1[1]),len(mat2[1])))
for i in range(len(mat1)):
for a, b in zip(mat1[i], mat2[i]):
self.assertAlmostEqual(a, b, tol)
def assertMatrixEqual(self, mat1, mat2):
"""
Assert if two matrices are almost equal
"""
self.assertEqual((len(mat1[0]),len(mat2[0])), (len(mat1[1]),len(mat2[1])))
for i in range(len(mat1)):
for a, b in zip(mat1[i], mat2[i]):
self.assertEqual(a, b)
if __name__ == '__main__':
unittest.main()