Path: blob/main/tests/plugins/test_heat_map.py
1601 views
"""1Test HeatMap2------------3"""45import numpy as np6import pytest78import folium9from folium.plugins import HeatMap10from folium.template import Template11from folium.utilities import normalize121314def test_heat_map():15np.random.seed(3141592)16data = np.random.normal(size=(100, 2)) * np.array([[1, 1]]) + np.array([[48, 5]])17m = folium.Map([48.0, 5.0], zoom_start=6)18hm = HeatMap(data)19m.add_child(hm)20m._repr_html_()2122out = normalize(m._parent.render())2324# We verify that the script import is present.25script = '<script src="https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js"></script>' # noqa26assert script in out2728# We verify that the script part is correct.29tmpl = Template(30"""31var {{this.get_name()}} = L.heatLayer(32{{this.data}},33{34minOpacity: {{this.min_opacity}},35maxZoom: {{this.max_zoom}},36radius: {{this.radius}},37blur: {{this.blur}},38gradient: {{this.gradient}}39})40.addTo({{this._parent.get_name()}});41"""42)4344assert tmpl.render(this=hm)4546bounds = m.get_bounds()47np.testing.assert_allclose(48bounds,49[50[46.218566840847025, 3.0302801394447734],51[50.75345011431167, 7.132453997672826],52],53)545556def test_heatmap_data():57data = HeatMap(np.array([[3, 4, 1], [5, 6, 1], [7, 8, 0.5]])).data58assert isinstance(data, list)59assert len(data) == 360for i in range(len(data)):61assert isinstance(data[i], list)62assert len(data[i]) == 3636465def test_heat_map_exception():66with pytest.raises(ValueError):67HeatMap(np.array([[4, 5, 1], [3, 6, np.nan]]))68with pytest.raises(Exception):69HeatMap(np.array([3, 4, 5]))707172