OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Holoviews Dynamic map returns only one entry for time

  • Thread starter Thread starter user24681547
  • Start date Start date
U

user24681547

Guest
I am trying to create a Panel program, which displays Holoviews.DynamicMaps. Right now I can create the Maps and Display them. I only display one at a time and change between them using a dropdown Menu. Some of the Maps not only have kdims Longitude and Latitude but also time and I want that when a Map I change into has this time dimension a Slider widget should appear. This slider should allow me to slide through the time dimension. When I run my code this all works fine, but the slider only has one date on it despite me putting more dates in the time dimension. I tried to figure out where this came from and found that, when selecting a new Map with time dimension to be displayed the callback from the dropdown get the information for the slider like this

Code:
    def dropdown_callback(event):
        dynamic_map_display.object = images[event.new]
        if 'time' in images[event.new].dimensions('all'):
            time_values = images[event.new].dimension_values('time')

Here images is a dict that contains all Maps and the event.new is the current selected option in the dropdown menu, which contains the keys for images. If the Map now has the time dimension it tries extraction the values, but here there is only one value returned (I think it is the value currently displayed and is the first value that was contained in the array that I gave as time dimension.) I don't know where I lose the other values. Does anyone know where I made a mistake?

I create the Maps with:

Code:
            if config['tseries']:
                kdims = ['time', 'Longitude', 'Latitude']
                vdims = [var]
                tim = plotdata[file][var]['time']
                ds = gv.Dataset((tim, lon, lat, val), kdims=kdims, vdims=vdims)
                
                image = ds.to(gv.Image, ['Longitude', 'Latitude'])

            else:
                ds = gv.Dataset((lon, lat, val),
                                ['Longitude', 'Latitude'], var)
                image = ds.to(gv.Image, ['Longitude', 'Latitude'])

after creating the image, dimension_values does return the complete array tim I put into the map.

The I give the map background tile and plot some points on it with:

Code:
fig = bg_fig * regrid(image) * dyn_coast * filtered

after which I wrap the all maps created into a dict and give it to the render function where the problem is.

Code:
def render_images(figures, cnfg):
    images = dict()

    # gives every dynamic map name for the dropdown menu
    for file in cnfg.keys():
        for var in cnfg[file].keys():
            dd_name = cnfg[file][var]['dd_name']
            images[dd_name] = figures[file][var]

    # create changing DropDown widget
    class DropdownMenu(param.Parameterized):
        option = param.Selector(default=list(images.keys())[0], objects=list(images.keys()), doc="Select an option")

    dropdown_menu = DropdownMenu()
    dropdown_widget = pn.Param(dropdown_menu.param, parameters=['option'], show_name=False)

    # Initialize the DateSlider with dummy values to avoid UnsetValueError
    date_slider = pn.widgets.DateSlider(name='Time', start=datetime(2000, 1, 1), end=datetime(2000, 1, 2), value=datetime(2000, 1, 1))
    date_slider.visible = False

    # starting image as default from dropdown menu
    dynamic_map_display = pn.panel(images[dropdown_menu.option])

    def update_slider(time_values):
        date_slider.start = np.min(time_values)
        date_slider.end = np.max(time_values)
        date_slider.value = date_slider.start

    def dropdown_callback(event):
        dynamic_map_display.object = images[event.new]
        
        if 'time' in images[event.new].dimensions('all'):

            # here is the problem, this only return one value instead of the complete array
            time_values = images[event.new].dimension_values('time')

            update_slider(time_values)
            date_slider.visible = True
        else:
            date_slider.visible = False

    def slider_callback(event):
        selected_image = dynamic_map_display.object
        if 'time' in selected_image.dimensions('all'):
            time_values = selected_image.dimension_values('time')
            time_index = np.argmin(np.abs(time_values - event.new))
            dynamic_map_display.object = selected_image.select(time=time_values[time_index])
    
    dropdown_menu.param.watch(dropdown_callback, 'option')
    date_slider.param.watch(slider_callback, 'value')

    layout = pn.Column(dropdown_widget, date_slider, dynamic_map_display)
        
    # Display the layout
    layout.servable()
<p>I am trying to create a Panel program, which displays Holoviews.DynamicMaps.
Right now I can create the Maps and Display them. I only display one at a time and change between them using a dropdown Menu. Some of the Maps not only have kdims Longitude and Latitude but also time and I want that when a Map I change into has this time dimension a Slider widget should appear. This slider should allow me to slide through the time dimension. When I run my code this all works fine, but the slider only has one date on it despite me putting more dates in the time dimension. I tried to figure out where this came from and found that, when selecting a new Map with time dimension to be displayed the callback from the dropdown get the information for the slider like this</p>
<pre><code> def dropdown_callback(event):
dynamic_map_display.object = images[event.new]
if 'time' in images[event.new].dimensions('all'):
time_values = images[event.new].dimension_values('time')
</code></pre>
<p>Here images is a dict that contains all Maps and the event.new is the current selected option in the dropdown menu, which contains the keys for images.
If the Map now has the time dimension it tries extraction the values, but here there is only one value returned (I think it is the value currently displayed and is the first value that was contained in the array that I gave as time dimension.)
I don't know where I lose the other values. Does anyone know where I made a mistake?</p>
<p>I create the Maps with:</p>
<pre><code> if config['tseries']:
kdims = ['time', 'Longitude', 'Latitude']
vdims = [var]
tim = plotdata[file][var]['time']
ds = gv.Dataset((tim, lon, lat, val), kdims=kdims, vdims=vdims)

image = ds.to(gv.Image, ['Longitude', 'Latitude'])

else:
ds = gv.Dataset((lon, lat, val),
['Longitude', 'Latitude'], var)
image = ds.to(gv.Image, ['Longitude', 'Latitude'])
</code></pre>
<p>after creating the image, dimension_values does return the complete array tim I put into the map.</p>
<p>The I give the map background tile and plot some points on it with:</p>
<pre><code>fig = bg_fig * regrid(image) * dyn_coast * filtered
</code></pre>
<p>after which I wrap the all maps created into a dict and give it to the render function where the problem is.</p>
<pre><code>def render_images(figures, cnfg):
images = dict()

# gives every dynamic map name for the dropdown menu
for file in cnfg.keys():
for var in cnfg[file].keys():
dd_name = cnfg[file][var]['dd_name']
images[dd_name] = figures[file][var]

# create changing DropDown widget
class DropdownMenu(param.Parameterized):
option = param.Selector(default=list(images.keys())[0], objects=list(images.keys()), doc="Select an option")

dropdown_menu = DropdownMenu()
dropdown_widget = pn.Param(dropdown_menu.param, parameters=['option'], show_name=False)

# Initialize the DateSlider with dummy values to avoid UnsetValueError
date_slider = pn.widgets.DateSlider(name='Time', start=datetime(2000, 1, 1), end=datetime(2000, 1, 2), value=datetime(2000, 1, 1))
date_slider.visible = False

# starting image as default from dropdown menu
dynamic_map_display = pn.panel(images[dropdown_menu.option])

def update_slider(time_values):
date_slider.start = np.min(time_values)
date_slider.end = np.max(time_values)
date_slider.value = date_slider.start

def dropdown_callback(event):
dynamic_map_display.object = images[event.new]

if 'time' in images[event.new].dimensions('all'):

# here is the problem, this only return one value instead of the complete array
time_values = images[event.new].dimension_values('time')

update_slider(time_values)
date_slider.visible = True
else:
date_slider.visible = False

def slider_callback(event):
selected_image = dynamic_map_display.object
if 'time' in selected_image.dimensions('all'):
time_values = selected_image.dimension_values('time')
time_index = np.argmin(np.abs(time_values - event.new))
dynamic_map_display.object = selected_image.select(time=time_values[time_index])

dropdown_menu.param.watch(dropdown_callback, 'option')
date_slider.param.watch(slider_callback, 'value')

layout = pn.Column(dropdown_widget, date_slider, dynamic_map_display)

# Display the layout
layout.servable()
</code></pre>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top