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
|
import os
import numpy as np
import napari
"""
This example generates many random shapes.
There currently is a bug in triangulation that requires this additional step of sanitizing the shape data: https://github.com/orgs/napari/projects/18/views/2
"""
# logging.getLogger().setLevel(0)
def generate_shapes(filename):
# image_data = np.squeeze(cells3d()[:, 1, :, :])
# delayed_image_data = DelayedArray(image_data, delay_s=1)
# From https://github.com/napari/napari/blob/main/examples/nD_shapes.py
# create one random polygon per "plane"
shapes_per_slice = 1000
all_shapes = None
np.random.seed(0)
for _ in range(shapes_per_slice):
planes = np.tile(np.arange(128).reshape((128, 1, 1)), (1, 5, 1))
corners = np.random.uniform(0, 128, size=(128, 5, 2))
shapes = np.concatenate((planes, corners), axis=2)
if all_shapes is not None:
all_shapes = np.concatenate((all_shapes, shapes), axis=0)
else:
all_shapes = shapes
print('all_shapes', all_shapes.shape)
from vispy.geometry.polygon import PolygonData
good_shapes = []
for shape in all_shapes:
# Use try/except to filter all bad shapes
try:
PolygonData(
vertices=shape[:, 1:]
).triangulate()
except AssertionError:
pass
else:
good_shapes.append(shape)
print(len(good_shapes))
np.savez(filename, shapes=good_shapes)
test_filename = '/tmp/napari_example_shapes.npz'
# Create the example shapes if they do not exist
if not os.path.exists(test_filename):
print(
'Shapes file does not exist yet. Generating shapes. This may take a couple of minutes...'
)
generate_shapes(test_filename)
# Load the shapes
with np.load(test_filename) as data:
shapes = data['shapes']
# Test shapes in viewer
viewer = napari.Viewer()
viewer.show()
shapes_layer = viewer.add_shapes(
np.array(shapes),
shape_type='polygon',
name='sliced',
)
napari.run()
|