@@ -209,79 +209,13 @@ def value(self) -> NDArray:
209
209
def buffer (self ) -> pygfx .Buffer | pygfx .Texture :
210
210
return self ._buffer
211
211
212
- def cleanup_key (self , key : int | np .ndarray [int , bool ] | slice | tuple [slice , ...]) -> int | np .ndarray | range | tuple [range , ...]:
213
- """
214
- Cleanup slice indices for setitem, returns positive indices. Converts negative indices to positive if necessary.
215
-
216
- Returns a cleaned up key corresponding to only the first dimension.
217
- """
218
- upper_bound = self .value .shape [0 ]
219
-
220
- if isinstance (key , int ):
221
- if abs (key ) > upper_bound : # absolute value in case negative index
222
- raise IndexError (f"key value: { key } out of range for dimension with size: { upper_bound } " )
223
- return key
224
-
225
- elif isinstance (key , np .ndarray ):
226
- if key .ndim > 1 :
227
- raise TypeError (f"Can only use 1D boolean or integer arrays for fancy indexing" )
228
-
229
- # if boolean array convert to integer array of indices
230
- if key .dtype == bool :
231
- key = np .nonzero (key )[0 ]
232
-
233
- if key .size < 1 :
234
- return np .array ([], dtype = np .int64 )
235
-
236
- # make sure indices within bounds of feature buffer range
237
- if key [- 1 ] > upper_bound :
238
- raise IndexError (
239
- f"Index: `{ key [- 1 ]} ` out of bounds for feature array of size: `{ upper_bound } `"
240
- )
241
-
242
- # make sure indices are integers
243
- if np .issubdtype (key .dtype , np .integer ):
244
- return key
245
-
246
- raise TypeError (f"Can only use 1D boolean or integer arrays for fancy indexing graphic features" )
247
-
248
- elif isinstance (key , tuple ):
249
- # multiple dimension slicing
250
- if not all ([isinstance (k , (int , slice , range , np .ndarray )) for k in key ]):
251
- raise TypeError (key )
252
-
253
- cleaned_tuple = list ()
254
- # cleanup the key for each dim
255
- for k in key :
256
- cleaned_tuple .append (self .cleanup_key (k ))
257
-
258
- return key
259
-
260
- if not isinstance (key , (slice , range )):
261
- raise TypeError ("Must pass slice or int object" )
262
-
263
- start = key .start if key .start is not None else 0
264
- stop = key .stop if key .stop is not None else self .value .shape [0 ] - 1
265
- # absolute value of the step in case it's negative
266
- # since we convert start and stop to be positive below it is fine for step to be converted to positive
267
- step = abs (key .step ) if key .step is not None else 1
268
-
269
- # modulus in case of negative indices
270
- start %= upper_bound
271
- stop %= upper_bound
272
-
273
- if start > stop :
274
- raise ValueError (f"start index: { start } greater than stop index: { stop } " )
275
-
276
- return range (start , stop , step )
277
-
278
212
def __getitem__ (self , item ):
279
213
return self .buffer .data [item ]
280
214
281
215
def __setitem__ (self , key , value ):
282
216
raise NotImplementedError
283
217
284
- def _update_range (self , key : int | slice | np .ndarray [int | bool ] | tuple [slice , ...]):
218
+ def _update_range (self , key : int | slice | np .ndarray [int | bool ] | list [ bool | int ] | tuple [slice , ...]):
285
219
"""
286
220
Uses key from slicing to determine the offset and
287
221
size of the buffer to mark for upload to the GPU
@@ -308,23 +242,24 @@ def _update_range(self, key: int | slice | np.ndarray[int | bool] | tuple[slice,
308
242
309
243
elif isinstance (key , (np .ndarray , list )):
310
244
if isinstance (key , list ):
311
- # convert to 1D array
245
+ # convert to array
312
246
key = np .array (key )
313
- if not key .ndim == 1 :
314
- raise TypeError (key )
247
+
248
+ if not key .ndim == 1 :
249
+ raise TypeError (key )
315
250
316
251
if key .dtype == bool :
317
252
# convert bool mask to integer indices
318
253
key = np .nonzero (key )[0 ]
319
254
320
- if key .size < 1 :
321
- # nothing to update
322
- return
323
-
324
255
if not np .issubdtype (key .dtype , np .integer ):
325
256
# fancy indexing doesn't make sense with non-integer types
326
257
raise TypeError (key )
327
258
259
+ if key .size < 1 :
260
+ # nothing to update
261
+ return
262
+
328
263
# convert any negative integer indices to positive indices
329
264
key %= upper_bound
330
265
0 commit comments