-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
120 lines (107 loc) · 5.34 KB
/
test.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
import math
import numpy as np
import random
import topologika as ta
import unittest
class TestAPI(unittest.TestCase):
def test_construction(self):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32))
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [32, 32, 32])
with self.assertRaises(ValueError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [32, 32])
with self.assertRaises(ValueError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [32, 32, 32, 32])
# TODO: region size > domain size should work correctly; TEST!
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [256, 256, 256])
with self.assertRaises(TypeError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [32.1, 32, 32])
with self.assertRaises(TypeError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), ['hello', 32, 32])
with self.assertRaises(ValueError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32), [-1, -2, -3])
with self.assertRaises(ValueError):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.uint8))
forest = ta.MergeForest(array=np.random.rand(128, 128, 128).astype(np.float32))
forest = ta.MergeForest(array=np.random.rand(128, 128, 128).astype(np.float32), region_shape=[32, 32, 32])
def test_maxima_query(self):
forest = ta.MergeForest(np.random.rand(128, 128, 128).astype(np.float32))
ta.maxima(forest)
def test_componentmax_query(self):
data = np.random.rand(128, 128, 128).astype(np.float32)
forest = ta.MergeForest(data)
ta.componentmax(forest, (10, 0, 0), 0.1)
ta.componentmax(forest, vertex=(10, 0, 0), threshold=0.1)
with self.assertRaises(ValueError):
ta.componentmax(forest, (-1, 0, 0), 0.1)
with self.assertRaises(ValueError):
ta.componentmax(forest, (data.shape[0], 0, 0), 0.1)
with self.assertRaises(TypeError):
ta.componentmax()
with self.assertRaises(TypeError):
ta.componentmax(forest, vertex=(10, 0, 0))
with self.assertRaises(TypeError):
ta.componentmax(forest, vertex=(10, 0, 0))
self.assertIsNone(ta.componentmax(forest, (0, 0, 0), float('inf')))
def test_component_query(self):
data = np.random.rand(128, 128, 128).astype(np.float32)
forest = ta.MergeForest(data)
ta.component(forest, (10, 0, 0), 0.1)
ta.component(forest, vertex=(10, 0, 0), threshold=0.1)
with self.assertRaises(ValueError):
ta.component(forest, (-1, 0, 0), 0.1)
with self.assertRaises(ValueError):
ta.component(forest, (data.shape[0], 0, 0), 0.1)
with self.assertRaises(TypeError):
ta.component()
with self.assertRaises(TypeError):
ta.component(forest, vertex=(10, 0, 0))
# TODO: should we return an empty component instead of None?
self.assertIsNone(ta.component(forest, (0, 0, 0), float('inf')))
def test_components_query(self):
data = np.random.rand(128, 128, 128).astype(np.float32)
forest = ta.MergeForest(data)
ta.components(forest, 0.1)
ta.components(forest, threshold=0.1)
self.assertEqual(ta.components(forest, float('inf')), [])
with self.assertRaises(TypeError):
ta.components()
class TestPersistenceAndTriplet(unittest.TestCase):
@classmethod
def setUpClass(cls):
# superlevel-set merge tree as index (value) nodes
# 22 (26)
# 0 (25) /
# \ 8 (23) /
# \ 2 (20) / /
# \ \ / /
# \ 5 (19) /
# \ / /
# 1 (18) /
# \ /
# 16 (8)
# |
cls.data = np.array([
[[25, 18, 20],
[24, 10, 19],
[22, 9, 23]],
[[0, 1, 2],
[4, 5, 6],
[3, 8, 7]],
[[13, 14, 15],
[12, 26, 16],
[11, 21, 17]]], dtype=np.float32)
cls.forest = ta.MergeForest(cls.data, region_shape=[2, 2, 2])
def test_maxima_query(self):
maxima = ta.maxima(self.forest)
self.assertEqual(set(maxima), set([(0, 0, 0), (0, 0, 2), (0, 2, 2), (2, 1, 1)]))
def test_persistence_query(self):
self.assertEqual(ta.persistence(self.forest, (0, 0, 0)), 17)
self.assertEqual(ta.persistence(self.forest, (0, 0, 2)), 1)
self.assertEqual(ta.persistence(self.forest, (0, 2, 2)), 5)
self.assertEqual(ta.persistence(self.forest, (2, 1, 1)), math.inf)
self.assertEqual(ta.persistence(self.forest, (1, 0, 0)), 0)
def test_triplet_query(self):
self.assertEqual(ta.triplet(self.forest, (0, 0, 0)), ((0, 0, 0), (1, 2, 1), (2, 1, 1)))
self.assertEqual(ta.triplet(self.forest, (0, 0, 2)), ((0, 0, 2), (0, 1, 2), (0, 2, 2)))
self.assertEqual(ta.triplet(self.forest, (0, 2, 2)), ((0, 2, 2), (0, 0, 1), (0, 0, 0)))
self.assertEqual(ta.triplet(self.forest, (2, 1, 1)), None)