2017-01-13 57 views
3

thisの例でBokeh galleryに触発されて、巨大な量の収集データ(本質的に生物学的データの時間経過)をスライドするスライダを実装しようとしました。スライダでカスタムのJavaScriptコールバックを使用する代わりに、ウィジェットを使用しようとしました。これが機能するかどうかはわかりません。私の最小の実例を見つけてください。スライダと画像が正しく表示されますが、更新が行われないようです。Bokeh Slider with Pythonのコールバックで画像をスライド

##Creating the 15 different pictures 
#Want to make 15 different pictures of a certain field function evaluated on a grid of img_size_x x img_size_y 
import numpy as np 
img_size_x,img_size_y = (512,512) 
variations=15 

#Make the field 
xx,yy=np.meshgrid(np.arange(img_size_x),np.arange(img_size_y)) 

#Broadcast the field into as many copies as there are variations to make use of the ufuncs 
xx= np.tile(xx,variations).reshape(variations,img_size_x,img_size_y) 
yy= np.asarray(map(np.transpose,np.tile(yy.T,variations).reshape(variations,img_size_x,img_size_y))) 

varied_parameter=np.linspace(.01,0.5,variations) #frequencies of a sin/cos function, for example 
varied_parameter=np.repeat(varied_parameter,img_size_x*img_size_y).reshape(variations,img_size_x,img_size_y) #broadcast 

matrix_images=np.cos(varied_parameter*xx)+np.sin(varied_parameter*yy) # field function evaluated for diff frequencies. 

##Creation of the Bokeh interface to slide through these pictures 
from bokeh.plotting import figure, show, output_file, output_notebook 
from bokeh.models import ColumnDataSource 
from bokeh.layouts import row, widgetbox 
from bokeh.models.widgets import Slider 
import bokeh.palettes as pal 
output_notebook() 

data=matrix_images[0] #starting value for the column data source 
source = ColumnDataSource(dict(image=[data])) #the figure.image function takes a vector of matrices 
image_sl = Slider(title="Image number", value=0, start=0, end=variations-1, step=1) #slider to go through the images 
def update_img(attrname, old, new): 
    curr_value = image_sl.value 
    x=matrix_images[int(curr_value)] #make sure index is int to select image number 'curr_value' 
    source.data = dict(image=[x]) 

image_sl.on_change('value', update_img) #give slider its callback function 
inputs = widgetbox(image_sl) #wrap the slider into a display object 


p = figure(x_range=(0, 10), y_range=(0, 10)) 
# must give a vector of image data for image parameter 
p.image('image', source=source,x=0, y=0, dw=10, dh=10, palette=pal.Greys256) 

show(row([p,image_sl])) # open a browser 

答えて

0

使用しようとしているアップデートの種類は、Bokeh Serverでのみ有効です。 output_notebookまたはoutput_fileshowと使用すると、生成された出力は、実際のプロットをレンダリングするJavaScriptを埋め込んだHTMLです。つまり、これらのプロットはブラウザ内で実行されるスタンドアロンファイルと考えるべきです。つまり、これらのプロットはPythonコードに直接アクセスしたり実行したりすることはできません。

Bokehは、Pythonでプロットのコールバックを書くためのいくつかの方法を提供します。 1つはBokeh Serverを使用することです。あなたはそれについてhereを読むことができます。 Bokeh Serverで作業するためにコールバックを作成したので、この方法で作業するのはかなり簡単でした。私はエラーを引き起こすこれらの行をコメントアウトしました。

そして
xx= np.tile(xx,variations).reshape(... 
yy= np.asarray(map(np.transpose, ... 

curdoc from bokeh.io import curdocのインポートを追加しcurdoc().add_root(row([p,image_ls]))show(row([p,image_ls]))を置き換えます。そこから、例は$ bokeh serve --show name_of_your_file.pyで実行できます。

ノートブックまたはスタンドアロンファイルで実行するものが必要な場合。また、PyScriptコールバックを使用するオプションもあります。このコールバックはPythonのように見え、Pythonコードと同じファイルに書き込まれます。しかし、PyScriptというPythonコードをJavaAcriptにコンパイルするための言語として解釈されます。繰り返しますが、これはPythonランタイム環境にはアクセスできません。 Bokehオブジェクトをこれらのコールバックに渡すことは可能ですが、それはそれです。コールバックがmatrix_imagesにアクセスする必要があるため、これはおそらくあなたのユースケースには当てはまりません。もっとhereを読むことができます。インタラクションの追加に関するセクション全体を読むことをお勧めします。CustomJSコールバックの使い方の良い例がたくさんあります。

Jupyterノートブックウィジェットを使用している別のオプションです。このソリューションはあなたのノートブックでのみ動作します。あなたはこのアプローチについて読むことができますhere

関連する問題