O.rka
O.rka

Reputation: 30737

How to use HoloMap in Holoviews to view 2D Array slices (Python 3.5.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),

Upvotes: 1

Views: 1142

Answers (1)

philippjfr
philippjfr

Reputation: 4080

First of all thanks for the interest, I'm one of the authors of HoloViews. It's important to understand the distinction between a HoloMap and DynamicMap.

A HoloMap is much like a dictionary, you populate it with (key, value) pairs and then you can explore the visualization of that data with widgets. A DynamicMap does not contain any items when you construct it, instead you define a callback function which gets evaluated when the widget (or you) request a specific key. This means you can define a continuous range or a list of discrete samples on the dynamic Dimension, letting you explore much larger spaces than are possible with a HoloMap.

Taking your example, you can construct a HoloMap and a DynamicMap in the following ways:

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)])

If you have any more questions you can join us on Gitter. Note that we are planning on properly integrating xarray with HoloViews so you don't have to manually define a HoloMap/DynamicMap to explore it a multi-dimensional array.

Upvotes: 3

Related Questions