I’ve been running into an issue, in which in previous versions of napari, I could reliably clear a shapes layer using
layer.data = []
or
layer.data = np.empty((0, 2))
This worked, although I’ve always felt a little uncomfortable with this, as it feels like I should be using a method as opposed to directly tampering with data. Is there a “correct” way to clear a layer? (I would prefer not to remove the layer entirely).
As of upgrading to napari 0.3.6, I’m getting a new error when I try to clear out data from a shapes layer (points layer seems okay). The relevant section of my code looks like:
for layer in viewer.layers: # erase all annotation layers
layer_type = layer.as_layer_data_tuple()[2]
if layer_type == 'points':
layer.data = np.empty((0, 2))
elif layer_type == 'shapes':
layer.data = []
which used to work, but now produces this error:
File "my_prog.py", line 402, in annotate
layer.data = []
File "/home/nate/.pyenv/versions/histo_new/lib/python3.7/site-packages/napari/layers/shapes/shapes.py", line 512, in data
self.add(data, shape_type=shape_type)
File "/home/nate/.pyenv/versions/histo_new/lib/python3.7/site-packages/napari/layers/shapes/shapes.py", line 1432, in add
if np.array(data[0]).ndim == 1:
IndexError: list index out of range
A minimal example would be something like this:
import napari
import numpy as np
viewer = napari.Viewer()
viewer.add_shapes(np.random.rand(10,5,2)*100, shape_type='polygon')
viewer.layers['Shapes'].data = [] # this used to work in 0.3.5!
Any suggestions as to 1) the appropriate way to clear data, and in lieu of that 2) ideas about why this is no longer working would be greatly appreciated. Thanks!