5
5
import pygfx
6
6
7
7
from fastplotlib .graphics ._features import ColorFeature
8
+ from .utils import generate_slice_indices
8
9
9
10
10
11
def generate_color_inputs (name : str ) -> list [str , np .ndarray , list , tuple ]:
@@ -18,108 +19,18 @@ def generate_color_inputs(name: str) -> list[str, np.ndarray, list, tuple]:
18
19
return [s , a , l , t ]
19
20
20
21
21
- def generate_slice_indices (kind : int ):
22
- n_elements = 10
23
- a = np .arange (n_elements )
24
-
25
- match kind :
26
- case 0 :
27
- # simplest, just int
28
- s = 2
29
- indices = [2 ]
30
-
31
- case 1 :
32
- # everything
33
- s = slice (None , None , None )
34
- indices = list (range (10 ))
35
-
36
- case 2 :
37
- # positive continuous range
38
- s = slice (1 , 5 , None )
39
- indices = [1 , 2 , 3 , 4 ]
40
-
41
- case 3 :
42
- # positive stepped range
43
- s = slice (2 , 8 , 2 )
44
- indices = [2 , 4 , 6 ]
45
-
46
- case 4 :
47
- # negative continuous range
48
- s = slice (- 5 , None , None )
49
- indices = [5 , 6 , 7 , 8 , 9 ]
50
-
51
- case 5 :
52
- # negative backwards
53
- s = slice (- 5 , None , - 1 )
54
- indices = [5 , 4 , 3 , 2 , 1 , 0 ]
55
-
56
- case 5 :
57
- # negative backwards stepped
58
- s = slice (- 5 , None , - 2 )
59
- indices = [5 , 3 , 1 ]
60
-
61
- case 6 :
62
- # negative stepped forward
63
- s = slice (- 5 , None , 2 )
64
- indices = [5 , 7 , 9 ]
65
-
66
- case 7 :
67
- # both negative
68
- s = slice (- 8 , - 2 , None )
69
- indices = [2 , 3 , 4 , 5 , 6 , 7 ]
70
-
71
- case 8 :
72
- # both negative and stepped
73
- s = slice (- 8 , - 2 , 2 )
74
- indices = [2 , 4 , 6 ]
75
-
76
- case 9 :
77
- # positive, negative, negative
78
- s = slice (8 , - 9 , - 2 )
79
- indices = [8 , 6 , 4 , 2 ]
80
-
81
- case 10 :
82
- # only stepped forward
83
- s = slice (None , None , 2 )
84
- indices = [0 , 2 , 4 , 6 , 8 ]
85
-
86
- case 11 :
87
- # only stepped backward
88
- s = slice (None , None , - 3 )
89
- indices = [9 , 6 , 3 , 0 ]
90
-
91
- case 12 :
92
- # list indices
93
- s = [2 , 5 , 9 ]
94
- indices = [2 , 5 , 9 ]
95
-
96
- case 13 :
97
- # bool indices
98
- s = a > 5
99
- indices = [6 , 7 , 8 , 9 ]
100
-
101
- case 14 :
102
- # list indices with negatives
103
- s = [1 , 4 , - 2 ]
104
- indices = [1 , 4 , 8 ]
105
-
106
- case 15 :
107
- # array indices
108
- s = np .array ([1 , 4 , - 7 , 9 ])
109
- indices = [1 , 4 , 3 , 9 ]
110
-
111
- others = [i for i in a if i not in indices ]
112
-
113
- offset , size = (min (indices ), np .ptp (indices ) + 1 )
114
-
115
- return {"slice" : s , "indices" : indices , "others" : others , "offset" : offset , "size" : size }
116
-
117
-
118
22
def make_colors_buffer () -> ColorFeature :
119
23
colors = ColorFeature (colors = "w" , n_colors = 10 )
120
24
return colors
121
25
122
26
27
+ @pytest .mark .parametrize ("color_input" , [* generate_color_inputs ("r" ), * generate_color_inputs ("g" ), * generate_color_inputs ("b" )])
28
+ def test_create_buffer (color_input ):
29
+ colors = ColorFeature (colors = color_input , n_colors = 10 )
30
+ truth = np .repeat ([pygfx .Color (color_input )], 10 , axis = 0 )
31
+ npt .assert_almost_equal (colors [:], truth )
32
+
33
+
123
34
def test_int ():
124
35
# setting single points
125
36
colors = make_colors_buffer ()
@@ -194,6 +105,7 @@ def test_tuple(slice_method):
194
105
195
106
196
107
@pytest .mark .parametrize ("color_input" , generate_color_inputs ("red" ))
108
+ # skip testing with int since that results in shape [1, 4] with np.repeat, int tested in independent unit test
197
109
@pytest .mark .parametrize ("slice_method" , [generate_slice_indices (i ) for i in range (1 , 16 )])
198
110
def test_slice (color_input , slice_method : dict ):
199
111
# slicing only first dim
0 commit comments