Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Created June 16, 2020 00:40
Show Gist options
  • Save GenevieveBuckley/ef5a5fb12e8fcaef14653551c1cba983 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/ef5a5fb12e8fcaef14653551c1cba983 to your computer and use it in GitHub Desktop.
Appending data with napari threads
"""
This script is adapted from https://github.com/napari/napari/blob/master/examples/interactive_scripting.py
Instead of replacing the entire layer data, we want to append to the data stack.
However it produces "WARNING: Cannot make QOpenGLContext current in a different thread"
"""
import numpy as np
import napari
from napari.qt import thread_worker
import time
with napari.gui_qt():
# create the viewer with an image
data = np.random.random((1, 512, 512))
viewer = napari.Viewer()
layer = viewer.add_image(data)
@thread_worker(start_thread=True)
def layer_update(*, update_period, num_updates):
# number of times to update
for k in range(num_updates):
time.sleep(update_period)
dat = np.random.random((1, 512, 512))
layer.data = np.concatenate((dat, layer.data), axis=0)
# check that data layer is properly assigned and not blocked?
while layer.data.all() != dat.all():
layer.data = dat
yield
layer_update(update_period=0.05, num_updates=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment