Skip to content

Instantly share code, notes, and snippets.

@klockeph
Created November 6, 2020 15:44
Show Gist options
  • Save klockeph/10e57b6babe77fed1ba854fd416c8baa to your computer and use it in GitHub Desktop.
Save klockeph/10e57b6babe77fed1ba854fd416c8baa to your computer and use it in GitHub Desktop.
import time
import threading
import atexit
import numpy as np
from vispy import app, scene, gloo, visuals
rolling_tex = """
float rolling_texture(vec2 pos) {
if( pos.x < 0 || pos.x > 1 || pos.y < 0 || pos.y > 1) {
return 0.0f;
}
vec2 uv = vec2(mod(pos.x, 1), pos.y);
return texture2D($texture, uv).r;
}
"""
cmap = """
vec4 colormap(float x) {
if(x == 1){
return vec4(0,255,0,1);
} if(x == 12345) { // TODO: VERY VERY VERY DIRTY HACK
return vec4(255,0,0,1);
}
return vec4(x,x,x,1);
}
"""
class ScrollingImage(scene.Image):
def __init__(self, shape, parent):
self._shape = shape
self._color_fn = visuals.shaders.Function(rolling_tex)
self._ctex = gloo.Texture2D(np.zeros(shape+(1,), dtype='float32'),
format='luminance', internalformat='r32f')
self._color_fn['texture'] = self._ctex
# self._color_fn['shift'] = 0
self.ptr = 0
scene.Image.__init__(self, method='impostor', parent=parent)
# self.set_gl_state('additive', cull_face=False)
self.shared_program.frag['get_data'] = self._color_fn
cfun = visuals.shaders.Function(cmap)
self.shared_program.frag['color_transform'] = cfun
@property
def size(self):
return self._shape
def add_bar(self, bar_height):
print(bar_height, end=',')
bar = np.zeros((self._shape[0],1,1), dtype=np.float32)
bar[0:bar_height] = 1
self._ctex[:, self.ptr] = bar
self.ptr = (self.ptr + 1) % self._shape[1]
self.update()
def _prepare_draw(self, view):
# hacky progressbar
from_ = (self.ptr + 1) % self._shape[1]
to_ = (self.ptr + 3) % self._shape[1]
if(to_ > from_):
self._ctex[:, from_:to_] = np.full((self._shape[0], 1,1), 12345, dtype=np.float32)
if self._need_vertex_update:
self._build_vertex_data()
if view._need_method_update:
self._update_method(view)
HEIGHT = 750
WIDTH = 500
GRID_COLS = 8
GRID_ROWS = 5
def visualize():
win = scene.SceneCanvas(keys='interactive', show=True, fullscreen=False)
grid = win.central_widget.add_grid()
view3 = grid.add_view(
row=0,
col=1,
row_span=GRID_ROWS,
col_span=GRID_COLS,
camera='panzoom',
border_color='grey')
#image = ScrollingImage((SAMPLES_PER_SECOND*5, HEIGHT), parent=view3.scene)
image = ScrollingImage((HEIGHT, WIDTH), parent=view3.scene)
view3.camera.rect = (0, 0, image.size[1], image.size[0])
gridlines = scene.GridLines(color=(1, 1, 1, 1), parent=image)
print(image.size)
# Add axes
yax = scene.AxisWidget(orientation='left', axis_label="Counts")
##yax.stretch = (0.05, 1)
grid.add_widget(yax, 0, 0, row_span=GRID_ROWS)
yax.link_view(view3)
xax = scene.AxisWidget(orientation='bottom', tick_label_margin=15)
grid.add_widget(xax, GRID_ROWS, 1, col_span=GRID_COLS)
xax.link_view(view3)
last_height = 0
def update(ev):
nonlocal last_height
height = (last_height + 10) % HEIGHT
image.add_bar(height)
last_height = height
timer = app.Timer(interval='auto', connect=update)
timer.start()
app.run()
if __name__ == '__main__':
visualize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment