Skip to content

Instantly share code, notes, and snippets.

@VolkerH
Created May 6, 2022 09:37
Show Gist options
  • Save VolkerH/2bd087d4d502e45f8112fb39bf4174af to your computer and use it in GitHub Desktop.
Save VolkerH/2bd087d4d502e45f8112fb39bf4174af to your computer and use it in GitHub Desktop.
Update points for point scanner, compare different approaches.
import time
import napari
import numpy as np
from napari.qt import thread_worker
from qtpy.QtWidgets import QPushButton, QVBoxLayout, QWidget
# Point property for MALDI points
PROPNAME_ACQUIRED = "acquired"
#
# breaks down
#def generate_maldi_points(nx=500, ny=100, dx=20, dy=20):
def generate_point_grid(nx=10000, ny=150, dx=20, dy=20):
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",)
acquired_pts_layer = viewer.add_points(
face_color='red',
size=10,
name="Scanned Points",)
shown = [False] * len(points)
unhide_pts_layer = viewer.add_points(
data=points,
face_color='green',
size=10,
name="Scanned Points",
shown=shown)
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
def add_points(point_index):
# to allow a fair comparison we work backwards,
# otherwise we would only start with addding
# a few points which would of course be faster
# (we would only notice a slowdown after a whhile)
acquired_pts_layer.data = points[:-point_index]
def show_points(point_index):
shown[point_index] = True
unhide_pts_layer.shown=shown
@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
@thread_worker(connect={'yielded': add_points})
def points_in_new_layer_thread(*_):
for i in range(len(pts_layer.data)):
time.sleep(0.05) # 20 fps
yield i
return
@thread_worker(connect={'yielded': show_points})
def show_points_thread(*_):
shown = [False] * len(points)
unhide_pts_layer.shown = shown
for i in range(len(pts_layer.data)):
time.sleep(0.05) # 20 fps
yield i
return
button_layout = QVBoxLayout()
recolor_btn = QPushButton("Recolor")
recolor_btn.clicked.connect(recolor_thread)
button_layout.addWidget(recolor_btn)
add_pts_btn = QPushButton("AddPts")
add_pts_btn.clicked.connect(points_in_new_layer_thread)
button_layout.addWidget(add_pts_btn)
show_pts_btn = QPushButton("ShowPts")
show_pts_btn.clicked.connect(show_points_thread)
button_layout.addWidget(show_pts_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