question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difficulties creating a holomap of composed rasterized images

See original GitHub issue

The code below should generate a holomap of holoviews rasterized images bunched together using + operators.

import numpy
import matplotlib
import bokeh
from matplotlib.cm import viridis, plasma, gist_ncar, gist_stern, PuBu, Blues
import holoviews
from holoviews.operation.datashader import rasterize
holoviews.extension('bokeh')
#AggregationMethods = [ 'mean','max','mode','std']
AggregationMethods = [ 'mean','max']
Hows = [ 'linear', 'bilinear' ]
# uncomment the next line to see a different error message
#Hows = [ 'nearest', 'linear' ]

OutputImageFilePattern = 'TestImage_%s_%i_%s_%s' 
ImageIntensityScale = 100000
PlotHeight = 100
PlotSizes = [500]
for library in ['numpy', 'matplotlib', 'holoviews']:
    print ( library + ' ' + str(eval(library + '.__version__')))

def PlotCluster(MatrixClusters, MatrixDistances, OutputFile, How, AggregationMethod, PlotSize, PlotHeight = PlotHeight):
    "plot the cluster"
    DistBarData = holoviews.Image(MatrixDistances)
    ShadedDistBar = rasterize (DistBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = True).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover']) 
    ClusterBarData = holoviews.Image(MatrixClusters)
    ShadedClusterBar = rasterize (ClusterBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = True).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover']) 
    Merged = (ShadedDistBar + ShadedClusterBar).cols(1)
    return Merged

HoloMapDict = {}
numpy.random.seed(1)
UnitDistanceVec = numpy.random.rand(25000)
UnitClusterVec = (numpy.random.rand(25000)*100).astype(int)  
print (UnitDistanceVec[:10])    
print (UnitClusterVec[:10])
BarSize = (PlotHeight,1)
UnitClusterVecRearranged = numpy.array(UnitClusterVec)
MatrixClusters = numpy.tile( UnitClusterVecRearranged, BarSize).astype(int)  
UnitDistanceVecsRarranged = numpy.array(UnitDistanceVec)
UnitDistanceVecAsInt = (numpy.array(UnitDistanceVecsRarranged)/max(UnitClusterVec)*ImageIntensityScale).astype(int)
MatrixDistances = numpy.tile( UnitDistanceVecAsInt, BarSize)

for AggregationMethod in AggregationMethods:
    for How in Hows:
        for PlotSize in PlotSizes:
            OutputFile = OutputImageFilePattern%('',PlotSize,How,AggregationMethod)
            Merged = PlotCluster(MatrixClusters,MatrixDistances, OutputFile, How, AggregationMethod, PlotSize)
            HoloMapDict[(AggregationMethod, How )] = Merged
Combined = holoviews.HoloMap(HoloMapDict, kdims=['Aggregation', 'interpolation'])
holoviews.save(Combined, 'combined', 'html')

The code above does not work as expected and generates the following printout:

numpy 1.16.3
matplotlib 3.0.3
holoviews 1.12.1
[4.17022005e-01 7.20324493e-01 1.14374817e-04 3.02332573e-01
 1.46755891e-01 9.23385948e-02 1.86260211e-01 3.45560727e-01
 3.96767474e-01 5.38816734e-01]
[91 86 65 77 59 47 48 45  8 87]
WARNING:param.Warning: Nesting Layouts within a HoloMap makes it difficult to access your data or control how it appears; we recommend calling .collate() on the HoloMap in order to follow the recommended nesting structure shown in the Composing Data user guide (https://goo.gl/2YS8LJ)
Traceback (most recent call last):
  File "Test2.py", line 50, in <module>
    holoviews.save(Combined, 'combined', 'html')
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\util\__init__.py", line 749, in save
    return renderer_obj.save(obj, filename, fmt=fmt)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\plotting\renderer.py", line 549, in save
    plot = self_or_cls.get_plot(obj)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\plotting\bokeh\renderer.py", line 134, in get_plot
    plot = super(BokehRenderer, self_or_cls).get_plot(obj, renderer, **kwargs)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\plotting\renderer.py", line 190, in get_plot
    obj = collate(obj)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\plotting\util.py", line 75, in collate
    return obj.collate()
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\core\spaces.py", line 349, in collate
    drop_constant=drop_constant)()
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\core\element.py", line 468, in __call__
    dict(constant_keys))
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\core\element.py", line 519, in _add_dimensions
    new_item[k] = self._add_dimensions(v, dims[::-1], constant_keys)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\core\element.py", line 508, in _add_dimensions
    new_item = new_item.add_dimension(dim, 0, val)
  File "C:\Users\Work\Anaconda2\envs\py3\lib\site-packages\holoviews\core\spaces.py", line 1804, in add_dimension
    raise NotImplementedError('Cannot add dimensions to a DynamicMap, '
NotImplementedError: Cannot add dimensions to a DynamicMap, cast to a HoloMap first.

I tired using collate on the holomap object created after creating the holomap object and adding data directly to the object, yet I am getting the same error.

I need a solution to have combo boxes that will alow me to switch between different aggregation and interpolation method - if holoviews does not implement it, can I do it with panel? My alternative is using tab layout in panel - however, it would be nice having it with the holoviews combo boxes if possible. In any case, I want it to be a static html page without the need to run a server. Hopefully iy is possible.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
jbednarcommented, May 31, 2019

Apart from adding collate, I think all you need is dynamic=False, which isn’t a problem if you’re exporting to HTML anyway. The underlying problem is that rasterize creates a DynamicMap if dynamic=True, and you can’t simply collate and reorganize a dynamic process like that. Instead, this code should be rewritten to create one big DynamicMap with non-dynamic components, but that’s a complete reorganization, and this should work for now since you don’t need the result to be dynamic anyway…

import numpy
import matplotlib
import bokeh
from matplotlib.cm import viridis, plasma, gist_ncar, gist_stern, PuBu, Blues
import holoviews
from holoviews.operation.datashader import rasterize
holoviews.extension('bokeh')
#AggregationMethods = [ 'mean','max','mode','std']
AggregationMethods = [ 'mean','max']
Hows = [ 'linear', 'bilinear' ]
# uncomment the next line to see a different error message
#Hows = [ 'nearest', 'linear' ]

OutputImageFilePattern = 'TestImage_%s_%i_%s_%s' 
ImageIntensityScale = 100000
PlotHeight = 100
PlotSizes = [500]
for library in ['numpy', 'matplotlib', 'holoviews']:
    print ( library + ' ' + str(eval(library + '.__version__')))

def PlotCluster(MatrixClusters, MatrixDistances, OutputFile, How, AggregationMethod, PlotSize, PlotHeight = PlotHeight):
    "plot the cluster"
    DistBarData = holoviews.Image(MatrixDistances)
    ShadedDistBar = rasterize (DistBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = False).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover'], axiswise=True, framewise=True) 
    ClusterBarData = holoviews.Image(MatrixClusters)
    ShadedClusterBar = rasterize (ClusterBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = False).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover'], axiswise=True, framewise=True) 
    Merged = (ShadedDistBar + ShadedClusterBar).cols(1)
    return Merged

HoloMapDict = {}
numpy.random.seed(1)
UnitDistanceVec = numpy.random.rand(25000)
UnitClusterVec = (numpy.random.rand(25000)*100).astype(int)  
print (UnitDistanceVec[:10])    
print (UnitClusterVec[:10])
BarSize = (PlotHeight,1)
UnitClusterVecRearranged = numpy.array(UnitClusterVec)
MatrixClusters = numpy.tile( UnitClusterVecRearranged, BarSize).astype(int)  
UnitDistanceVecsRarranged = numpy.array(UnitDistanceVec)
UnitDistanceVecAsInt = (numpy.array(UnitDistanceVecsRarranged)/max(UnitClusterVec)*ImageIntensityScale).astype(int)
MatrixDistances = numpy.tile( UnitDistanceVecAsInt, BarSize)

for AggregationMethod in AggregationMethods:
    for How in Hows:
        for PlotSize in PlotSizes:
            OutputFile = OutputImageFilePattern%('',PlotSize,How,AggregationMethod)
            Merged = PlotCluster(MatrixClusters,MatrixDistances, OutputFile, How, AggregationMethod, PlotSize)
            HoloMapDict[(AggregationMethod, How )] = Merged

Combined = holoviews.HoloMap(HoloMapDict, kdims=['Aggregation', 'interpolation']).collate()
holoviews.save(Combined, 'combined', 'html')
0reactions
Jacob-Barhakcommented, Aug 3, 2019

This specific problem was solved by James a long time ago - I should have been closed. So I am closing it so it wont bother the developers. There was another issue there with saving the html with the widgets which is related to another issue I started (https://github.com/pyviz/holoviews/issues/3849) that deals with saving.

To close this problem I am reiterating the code that will work.

import numpy
import matplotlib
import bokeh
from matplotlib.cm import viridis, plasma, gist_ncar, gist_stern, PuBu, Blues
import holoviews
from holoviews.operation.datashader import rasterize
holoviews.extension('bokeh')
#AggregationMethods = [ 'mean','max','mode','std']
AggregationMethods = [ 'mean','max']
Hows = [ 'linear', 'bilinear' ]
# uncomment the next line to see a different error message
#Hows = [ 'nearest', 'linear' ]

OutputImageFilePattern = 'TestImage_%s_%i_%s_%s' 
ImageIntensityScale = 100000
PlotHeight = 100
PlotSizes = [500]
for library in ['numpy', 'matplotlib', 'holoviews']:
    print ( library + ' ' + str(eval(library + '.__version__')))

def PlotCluster(MatrixClusters, MatrixDistances, OutputFile, How, AggregationMethod, PlotSize, PlotHeight = PlotHeight):
    "plot the cluster"
    DistBarData = holoviews.Image(MatrixDistances)
    ShadedDistBar = rasterize (DistBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = False).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover'], axiswise=True, framewise=True) 
    ClusterBarData = holoviews.Image(MatrixClusters)
    ShadedClusterBar = rasterize (ClusterBarData, aggregator = AggregationMethod, interpolation = How, dynamic  = False).opts(cmap=PuBu, xaxis=None, yaxis=None, height=PlotHeight, width=PlotSize, tools=['hover'], axiswise=True, framewise=True) 
    Merged = (ShadedDistBar + ShadedClusterBar).cols(1)
    return Merged

HoloMapDict = {}
numpy.random.seed(1)
UnitDistanceVec = numpy.random.rand(25000)
UnitClusterVec = (numpy.random.rand(25000)*100).astype(int)  
print (UnitDistanceVec[:10])    
print (UnitClusterVec[:10])
BarSize = (PlotHeight,1)
UnitClusterVecRearranged = numpy.array(UnitClusterVec)
MatrixClusters = numpy.tile( UnitClusterVecRearranged, BarSize).astype(int)  
UnitDistanceVecsRarranged = numpy.array(UnitDistanceVec)
UnitDistanceVecAsInt = (numpy.array(UnitDistanceVecsRarranged)/max(UnitClusterVec)*ImageIntensityScale).astype(int)
MatrixDistances = numpy.tile( UnitDistanceVecAsInt, BarSize)

for AggregationMethod in AggregationMethods:
    for How in Hows:
        for PlotSize in PlotSizes:
            OutputFile = OutputImageFilePattern%('',PlotSize,How,AggregationMethod)
            Merged = PlotCluster(MatrixClusters,MatrixDistances, OutputFile, How, AggregationMethod, PlotSize)
            HoloMapDict[(AggregationMethod, How )] = Merged

Combined = holoviews.HoloMap(HoloMapDict, kdims=['Aggregation', 'interpolation']).collate()
holoviews.save(Combined, 'combined.html')

Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difficulties creating a holomap of composed rasterized ...
The code below should generate a holomap of holoviews rasterized images bunched together using + operators. import numpy import matplotlib ...
Read more >
Raster vs. Vector Images - All About Images - Research Guides
Raster images are compiled using pixels, or tiny dots, containing unique color and tonal information that come together to create the image.
Read more >
Raster Images vs. Vector Graphics - ReproMail
Computer graphics can be created as either raster or vector images. ... A bitmap is a grid of individual pixels that collectively compose...
Read more >
Raster vs. vector: What are the differences? - Adobe
When working with digital photos, graphic design, logos, and other digital images, raster and vector are the two most common file types you'll...
Read more >
Raster (Bitmap) vs Vector
Bit map or Raster images are common images created with pixel-based ... Because vector graphics are not composed of pixels they are resolution-independent....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found