Skip to content

Commit ff85c5f

Browse files
committed
[multi_tag] Add test+Fix bug for 6 diff type of intersections for 2D pos
1 parent 27a9428 commit ff85c5f

File tree

2 files changed

+80
-10
lines changed

2 files changed

+80
-10
lines changed

nixworks/multi_tag.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _intersect(st, ed, true_start, true_end):
9191
if _in_range(ed, true_start, true_end):
9292
true_end = ed
9393
# check if other corners maybe in range
94-
if not _in_range(st, true_start, true_end) and not \
94+
if not _in_range(st, true_start, true_end) or not \
9595
_in_range(ed, true_start, true_end):
9696
if isinstance(st, np.ndarray):
9797
tmp_starts = [(x, y) for x, y in zip(st, true_start)]
@@ -120,6 +120,8 @@ def _intersect(st, ed, true_start, true_end):
120120
raise ValueError("Only start or end point of intersection exists.")
121121
elif start_update and end_update:
122122
true_start = tmp_start
123+
true_start = np.array(true_start)
124+
true_end = np.array(true_end)
123125
return true_start, true_end
124126

125127

nixworks/test/test_multi_tag.py

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,86 @@ def test_two_1d(self):
3232
t2 = self.block.create_tag("t2", "test", position=(10,))
3333
t1.references.append(self.ref1d)
3434
t2.references.append(self.ref1d)
35+
3536
# detached - no intersection
3637
t1.extent = [5]
3738
t2.extent = [5]
3839
i = intersection(self.ref1d, [t1, t2])
3940
assert i is None
40-
u = union(self.ref1d, [t1, t2])
41-
np.testing.assert_array_equal(u[0], self.arr1d[0:6])
42-
np.testing.assert_array_equal(u[1], self.arr1d[10:16])
43-
# intersected
41+
42+
# intersected interval
4443
t1.extent = [12]
4544
i = intersection(self.ref1d, [t1, t2])
4645
np.testing.assert_array_almost_equal(np.array(i), self.arr1d[10:13])
47-
u = union(self.ref1d, [t1, t2])
48-
np.testing.assert_array_equal(u[0], self.arr1d[0:16])
46+
47+
# intersected point
48+
t1.extent = [10]
49+
i = intersection(self.ref1d, [t1, t2])
50+
np.testing.assert_array_almost_equal(np.array(i), self.arr1d[10:13])
4951
# covered
5052
t1.extent = [30]
5153
i = intersection(self.ref1d, [t1, t2])
5254
np.testing.assert_array_almost_equal(np.array(i), t2.tagged_data(0)[:])
53-
u = union(self.ref1d, [t1, t2])
54-
np.testing.assert_array_equal(u[0], t1.tagged_data(0)[:])
55+
56+
def test_2d(self):
57+
# create a 2d reference data
58+
ref_data = np.arange(100).reshape((10, 10))
59+
ref2d = self.block.create_data_array('ref2d', 'ref', data=ref_data)
60+
# There should be six different styles of 2d tags intersecting
61+
# 1. tags that intersects at the corner
62+
t1 = self.block.create_tag("corner1", "test", position=(0, 0))
63+
t2 = self.block.create_tag("corner2", "test", position=(5, 5))
64+
t1.references.append(ref2d)
65+
t2.references.append(ref2d)
66+
t1.extent = [7, 7]
67+
t2.extent = [4, 4]
68+
i = intersection(ref2d, [t1, t2])
69+
np.testing.assert_array_almost_equal(np.array(i), ref2d[5:8, 5:8])
70+
# 2. tags that intersects as a cross form
71+
t1 = self.block.create_tag("cross1", "test", position=(5, 0))
72+
t2 = self.block.create_tag("cross2", "test", position=(2, 2))
73+
t1.references.append(ref2d)
74+
t2.references.append(ref2d)
75+
t1.extent = [2, 8] # end point is (7, 8)
76+
t2.extent = [6, 2] # end point is (8, 4)
77+
i = intersection(ref2d, [t1, t2])
78+
np.testing.assert_array_almost_equal(np.array(i), ref2d[5:8, 2:5])
79+
# 3. one tag is completely covered by a larger tag
80+
t1 = self.block.create_tag("inclusive1", "test", position=(0, 0))
81+
t2 = self.block.create_tag("inclusive2", "test", position=(2, 2))
82+
t1.references.append(ref2d)
83+
t2.references.append(ref2d)
84+
t1.extent = [7, 7]
85+
t2.extent = [3, 3]
86+
i = intersection(ref2d, [t1, t2])
87+
np.testing.assert_array_almost_equal(np.array(i), ref2d[2:6, 2:6])
88+
# 4. tags that intersect as a rectangle
89+
t1 = self.block.create_tag("rect1", "test", position=(0, 0))
90+
t2 = self.block.create_tag("rect2", "test", position=(0, 2))
91+
t1.references.append(ref2d)
92+
t2.references.append(ref2d)
93+
t1.extent = [4, 4]
94+
t2.extent = [4, 4]
95+
i = intersection(ref2d, [t1, t2])
96+
np.testing.assert_array_almost_equal(np.array(i), ref2d[0:5, 2:5])
97+
# 5. T-shape: like a cross, but only stick out on one side
98+
t1 = self.block.create_tag("t-shape1", "test", position=(0, 0))
99+
t2 = self.block.create_tag("t-shape2", "test", position=(2, 2))
100+
t1.references.append(ref2d)
101+
t2.references.append(ref2d)
102+
t1.extent = [6, 6]
103+
t2.extent = [5, 3] # end point (7, 5) #TODO: BUG
104+
i = intersection(ref2d, [t1, t2])
105+
np.testing.assert_array_almost_equal(np.array(i), ref2d[2:7, 2:6])
106+
# 6. no intersection
107+
t1 = self.block.create_tag("notinter1", "test", position=(0, 0))
108+
t2 = self.block.create_tag("notinter2", "test", position=(5, 5))
109+
t1.references.append(ref2d)
110+
t2.references.append(ref2d)
111+
t1.extent = [3, 3]
112+
t2.extent = [3, 3]
113+
i = intersection(ref2d, [t1, t2])
114+
assert i is None
55115

56116
def test_multi_nd(self):
57117
d = np.zeros((2, 3))
@@ -77,7 +137,6 @@ def test_multi_nd(self):
77137
u = union(self.ref3d, [t1])
78138
np.testing.assert_array_almost_equal(u[0][:], self.arr3d[0:5, 0:5, 0:5])
79139
u = union(self.ref3d, [t1, t2])
80-
print(u[0][:].size)
81140
np.testing.assert_array_almost_equal(u[0][:], self.arr3d[0:6, 0:6, 0:6])
82141
p3 = self.block.create_data_array("pos3", "pos", data=np.array([[5, 5, 5], [6, 6, 6]]))
83142
t3 = self.block.create_multi_tag("t3", "test", positions=p3)
@@ -102,4 +161,13 @@ def test_flat_positions(self):
102161
t4.references.append(self.ref1d)
103162
intersection(0, [t3, t4])
104163

164+
def test_union(self):
165+
pass
166+
# u = union(self.ref1d, [t1, t2])
167+
# np.testing.assert_array_equal(u[0], self.arr1d[0:6])
168+
# np.testing.assert_array_equal(u[1], self.arr1d[10:16])
169+
# u = union(self.ref1d, [t1, t2])
170+
# np.testing.assert_array_equal(u[0], self.arr1d[0:16])
171+
# u = union(self.ref1d, [t1, t2])
172+
# np.testing.assert_array_equal(u[0], t1.tagged_data(0)[:])
105173

0 commit comments

Comments
 (0)