Skip to content

Instantly share code, notes, and snippets.

@VolkerH
Last active May 6, 2022 08:59
Show Gist options
  • Save VolkerH/9b031e184ebcace1845f7da6c91e51a8 to your computer and use it in GitHub Desktop.
Save VolkerH/9b031e184ebcace1845f7da6c91e51a8 to your computer and use it in GitHub Desktop.
Point recoloring in napari
import time
import napari
import numpy as np
from napari.qt import thread_worker
from qtpy.QtWidgets import QPushButton, QVBoxLayout, QWidget
PROPNAME_ACQUIRED = "acquired"
#def generate_point_grid(nx=500, ny=100, dx=20, dy=20): # starting to lag with current napari master
def generate_point_grid(nx=1500, ny=100, dx=20, dy=20): # still working with Martin's branch
yy, xx = np.meshgrid(np.arange(ny) * dy, np.arange(nx) * dx)
points = np.vstack([yy.ravel(), xx.ravel()]).T
return points
viewer=napari.Viewer()
points = generate_point_grid()
props = {PROPNAME_ACQUIRED: [False] * len(points)}
pts_layer = viewer.add_points(
data=points,
properties=props,
face_color=PROPNAME_ACQUIRED,
edge_color=PROPNAME_ACQUIRED,
face_color_cycle=["yellow", "blue"],
size=10,
name="Point Scanner",)
def change_point_property(point_index):
propertties=pts_layer.properties
propertties[PROPNAME_ACQUIRED][point_index] = True
pts_layer.properties = propertties
pts_layer.face_color=PROPNAME_ACQUIRED
@thread_worker(connect={'yielded': change_point_property})
def recolor_thread(*_):
for i in range(len(pts_layer.data)):
time.sleep(0.05) # 20 fps
yield i
return
button_layout = QVBoxLayout()
start_btn = QPushButton("Start")
start_btn.clicked.connect(recolor_thread)
button_layout.addWidget(start_btn)
_widget = QWidget()
_widget.setLayout(button_layout)
_widget.setObjectName("Threading Examples")
viewer.window.add_dock_widget(_widget, allowed_areas=["right"])
if __name__ == '__main__':
napari.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment