1

I recently found out about holoviews and the hv.Image method is a nice alternative to plt.image. There is a really cool feature called hv.HoloMap that allows one to input a function and adjust parameters within the function to interactively view the resulting 2D array. I tried following a few examples of initiating the HoloMap object and an alternative dynamicMap object but couldn't get it to work with my data. (http://holoviews.org/Tutorials/Showcase.html)

In my real datasets, I will have 3D array and I would like to slice along an axis (z in this case) where I could interactively view the resulting slices. I made a basic example with numpy and xarray below:

How can I structure my basic function image_slice (iterate over the z dimension) with my hv.HoloMap (or hv.dynamicMap) object to view 2D slices of my 3D DataArray?

import xarray as xr
import numpy as np
import holoviews as hv; hv.notebook_extension()


#Building 2D Array (X & Y)
dist = np.linspace(-0.5,0.5,202)   # Linear spatial sampling
XY,YX = np.meshgrid(dist, dist)

#Add along 3rd Dimension
Z_list = []
for i in range(10):
    Z_list.append(xr.DataArray(XY*i,dims=["x","y"]))

#Concat list of 2D Arrays into a 3D Array
DA_3D = xr.concat(Z_list,dim="z")
# DA_3D.shape 
# (10, 202, 202)

def image_slice(DA_var,k):
    return(hv.Image(DA_var[k,:,:].values))

#http://holoviews.org/Tutorials/Showcase.html Interactive Exploration w/ Circular Wave example
keys = [(DA_3D,k) for k in range(10)] #Every combination
items = [(k, image_slice(*k)) for k in keys] 
# visual_slice = hv.HoloMap(items)
# TypeError: unhashable type: 'DataArray


dmap = hv.DynamicMap(slice_image, kdims=[hv.Dimension('z_axis',range=(0, 10))])
# dmap
# TypeError: slice_image() missing 1 required positional argument: 'k'
# Which makes perfect sense because the first argument is the DataArray object but I don't know how to input that into this type of object since `hv.Dimension` requires a range

I use Python 3.5.1 and Holoviews Version((1, 4, 3),

4

1 に答える 1

3

まず、関心をお寄せいただきありがとうございます。私は HoloViews の作成者の 1 人です。HoloMapaとの違いを理解することが重要DynamicMapです。

HoloMap は辞書によく似ています。(キー、値) のペアを入力すると、ウィジェットを使用してそのデータの視覚化を調べることができます。DynamicMap は、作成時に項目を含まず、代わりに、ウィジェット (またはユーザー) が特定のキーを要求したときに評価されるコールバック関数を定義します。これは、動的な Dimension で連続的な範囲または個別のサンプルのリストを定義できることを意味し、HoloMap で可能なよりもはるかに大きな空間を探索できます。

例を挙げると、次の方法で HoloMap と DynamicMap を構築できます。

import xarray as xr
import numpy as np
import holoviews as hv; 
hv.notebook_extension()

#Building 2D Array (X & Y)
dist = np.linspace(-0.5,0.5,202)   # Linear spatial sampling
XY,YX = np.meshgrid(dist, dist)

#Add along 3rd Dimension
Z_list = []
for i in range(10):
    Z_list.append(xr.DataArray(XY*i,dims=["x","y"]))

#Concat list of 2D Arrays into a 3D Array
DA_3D = xr.concat(Z_list,dim="z")
# DA_3D.shape 
# (10, 202, 202)

def image_slice(k):
    return(hv.Image(DA_3D[k,:,:].values))

keys = list(range(10))

# Construct a HoloMap by evaluating the function over all the keys
hmap = hv.HoloMap([(k, image_slice(k)) for k in keys], kdims=['z_axis'])

# Construct a DynamicMap by defining the sampling on the Dimension
dmap = hv.DynamicMap(image_slice, kdims=[hv.Dimension('z_axis', values=keys)])

さらに質問がある場合は、Gitterに参加してください。xarray を HoloViews と適切に統合することを計画しているため、多次元配列を探索するために HoloMap/DynamicMap を手動で定義する必要はありません。

于 2016-05-08T15:22:05.200 に答える