Note
Go to the end to download the full example code.
Tracks 3D with graph#
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/app/backends/_qt.py", line 928, in paintGL
self._vispy_canvas.events.draw(region=None)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 453, in __call__
self._invoke_callback(cb, event)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 471, in _invoke_callback
_handle_exception(self.ignore_callback_errors,
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 469, in _invoke_callback
cb(event)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 219, in on_draw
self._draw_scene()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 278, in _draw_scene
self.draw_visual(self.scene)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 316, in draw_visual
node.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/visuals.py", line 106, in draw
self._visual_superclass.draw(self)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 668, in draw
v.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/visuals.py", line 106, in draw
self._visual_superclass.draw(self)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 668, in draw
v.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 505, in draw
if self._prepare_draw(view=self) is False:
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/line/line.py", line 331, in _prepare_draw
self._pos_vbo.set_data(pos)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/gloo/buffer.py", line 189, in set_data
data = self._prepare_data(data, **kwargs)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/gloo/buffer.py", line 446, in _prepare_data
raise ValueError('Last dimension should be %s not %s'
ValueError: Last dimension should be 2 not 3
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/app/backends/_qt.py", line 928, in paintGL
self._vispy_canvas.events.draw(region=None)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 453, in __call__
self._invoke_callback(cb, event)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 471, in _invoke_callback
_handle_exception(self.ignore_callback_errors,
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/util/event.py", line 469, in _invoke_callback
cb(event)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 219, in on_draw
self._draw_scene()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 278, in _draw_scene
self.draw_visual(self.scene)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/canvas.py", line 316, in draw_visual
node.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/visuals.py", line 106, in draw
self._visual_superclass.draw(self)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 668, in draw
v.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/scene/visuals.py", line 106, in draw
self._visual_superclass.draw(self)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 668, in draw
v.draw()
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/visual.py", line 505, in draw
if self._prepare_draw(view=self) is False:
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/visuals/line/line.py", line 331, in _prepare_draw
self._pos_vbo.set_data(pos)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/gloo/buffer.py", line 189, in set_data
data = self._prepare_data(data, **kwargs)
File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/vispy/gloo/buffer.py", line 446, in _prepare_data
raise ValueError('Last dimension should be %s not %s'
ValueError: Last dimension should be 2 not 3
import numpy as np
import napari
def _circle(r, theta):
x = r * np.cos(theta)
y = r * np.sin(theta)
return x, y
def tracks_3d_merge_split():
"""Create tracks with splitting and merging."""
timestamps = np.arange(300)
def _trajectory(t, r, track_id):
theta = t * 0.1
x, y = _circle(r, theta)
z = np.zeros(x.shape)
tid = np.ones(x.shape) * track_id
return np.stack([tid, t, z, y, x], axis=1)
trackA = _trajectory(timestamps[:100], 30.0, 0)
trackB = _trajectory(timestamps[100:200], 10.0, 1)
trackC = _trajectory(timestamps[100:200], 50.0, 2)
trackD = _trajectory(timestamps[200:], 30.0, 3)
data = [trackA, trackB, trackC, trackD]
tracks = np.concatenate(data, axis=0)
tracks[:, 2:] += 50.0 # centre the track at (50, 50, 50)
graph = {1: 0, 2: [0], 3: [1, 2]}
features = {'time': tracks[:, 1]}
return tracks, features, graph
tracks, features, graph = tracks_3d_merge_split()
vertices = tracks[:, 1:]
viewer = napari.Viewer(ndisplay=3)
viewer.add_points(vertices, size=1, name='points', opacity=0.3)
viewer.add_tracks(tracks, features=features, graph=graph, name='tracks')
if __name__ == '__main__':
napari.run()