-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtest_markers_buffer_manager.py
More file actions
143 lines (106 loc) · 4.8 KB
/
test_markers_buffer_manager.py
File metadata and controls
143 lines (106 loc) · 4.8 KB
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
142
143
import numpy as np
from numpy import testing as npt
import pytest
import fastplotlib as fpl
import pygfx
from fastplotlib.graphics.features import GraphicFeatureEvent, VertexMarkers
from fastplotlib.graphics.features._scatter import marker_names, vectorized_user_markers_to_std_markers
from .utils import (
generate_slice_indices,
generate_positions_spiral_data,
)
EVENT_RETURN_VALUE: GraphicFeatureEvent = None
def event_handler(ev):
global EVENT_RETURN_VALUE
EVENT_RETURN_VALUE = ev
MARKERS1 = list("osD+x^v<>*")
MARKERS2 = list(">+vx<so^*D")
marker_values_int = list()
for m in MARKERS1:
m = marker_names[m]
marker_values_int.append(pygfx.MarkerInt[m])
MARKERS1_INT = np.asarray(marker_values_int)
marker_values_int = list()
for m in MARKERS2:
m = marker_names[m]
marker_values_int.append(pygfx.MarkerInt[m])
MARKERS2_INT = np.asarray(marker_values_int)
@pytest.mark.parametrize("test_graphic", [True, False])
def test_create_buffer(test_graphic):
data = generate_positions_spiral_data("xyz")
if test_graphic:
fig = fpl.Figure()
scatter = fig[0, 0].add_scatter(data, markers=MARKERS1, uniform_marker=False)
vertex_markers = scatter.markers
assert isinstance(vertex_markers, VertexMarkers)
assert vertex_markers._fpl_buffer is scatter.world_object.geometry.markers
else:
vertex_markers = VertexMarkers(MARKERS1, len(data))
marker_values_str = np.asarray(list(map(marker_names.get, MARKERS1)))
assert (vertex_markers.value == marker_values_str).all()
npt.assert_equal(vertex_markers.value_int, MARKERS1_INT)
assert (vertex_markers.buffer.data is vertex_markers.value_int)
@pytest.mark.parametrize("test_graphic", [True, False])
@pytest.mark.parametrize("index", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10])
def test_int(test_graphic, index: int):
data = generate_positions_spiral_data("xyz")
if test_graphic:
fig = fpl.Figure()
scatter = fig[0, 0].add_scatter(data, markers=MARKERS1, uniform_marker=False)
scatter.add_event_handler(event_handler, "markers")
vertex_markers = scatter.markers
else:
vertex_markers = VertexMarkers(MARKERS1, len(data))
# set a marker at one spot
vertex_markers[index] = MARKERS2[index]
assert vertex_markers.value[index] == marker_names[MARKERS2[index]]
assert vertex_markers.value_int[index] == MARKERS2_INT[index]
# make sure all others are unchanged
indices = list(range(10))
indices.pop(index)
# these are int32 so assert equal should be fine
npt.assert_equal(vertex_markers[indices], MARKERS1_INT[indices])
npt.assert_equal(vertex_markers.value_int[indices], MARKERS1_INT[indices])
if test_graphic:
global EVENT_RETURN_VALUE
assert EVENT_RETURN_VALUE.type == "markers"
assert isinstance(EVENT_RETURN_VALUE, GraphicFeatureEvent)
assert EVENT_RETURN_VALUE.graphic is scatter
assert EVENT_RETURN_VALUE.target is scatter.world_object
assert EVENT_RETURN_VALUE.info["key"] == index
assert EVENT_RETURN_VALUE.info["value"] == MARKERS2[index]
@pytest.mark.parametrize("test_graphic", [True, False])
@pytest.mark.parametrize(
"slice_method", [generate_slice_indices(i) for i in range(1, 16)]
)
def test_slice(test_graphic, slice_method):
data = generate_positions_spiral_data("xyz")
if test_graphic:
fig = fpl.Figure()
scatter = fig[0, 0].add_scatter(data, markers=MARKERS1, uniform_marker=False)
scatter.add_event_handler(event_handler, "markers")
vertex_markers = scatter.markers
else:
vertex_markers = VertexMarkers(MARKERS1, len(data))
s = slice_method["slice"]
indices = slice_method["indices"]
offset = slice_method["offset"]
size = slice_method["size"]
others = slice_method["others"]
vertex_markers[s] = np.asarray(MARKERS2)[s]
# test event too
if test_graphic:
global EVENT_RETURN_VALUE
assert isinstance(EVENT_RETURN_VALUE, GraphicFeatureEvent)
assert EVENT_RETURN_VALUE.type == "markers"
assert EVENT_RETURN_VALUE.graphic is scatter
assert EVENT_RETURN_VALUE.target is scatter.world_object
if isinstance(s, slice):
assert EVENT_RETURN_VALUE.info["key"] == s
assert (EVENT_RETURN_VALUE.info["value"] == np.asarray(MARKERS2)[s]).all()
assert (vertex_markers.value[s] == vectorized_user_markers_to_std_markers(MARKERS2)[s]).all()
# these are int32 so assert equal should be fine
npt.assert_equal(vertex_markers.value_int[s], MARKERS2_INT[s])
# make sure other points aren't affected
assert (vertex_markers.value[others] == vectorized_user_markers_to_std_markers(np.asarray(MARKERS1)[others])).all()
npt.assert_equal(vertex_markers.value_int[others], MARKERS1_INT[others])