Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
python-visualization
GitHub Repository: python-visualization/folium
Path: blob/main/tests/plugins/test_heat_map.py
1601 views
1
"""
2
Test HeatMap
3
------------
4
"""
5
6
import numpy as np
7
import pytest
8
9
import folium
10
from folium.plugins import HeatMap
11
from folium.template import Template
12
from folium.utilities import normalize
13
14
15
def test_heat_map():
16
np.random.seed(3141592)
17
data = np.random.normal(size=(100, 2)) * np.array([[1, 1]]) + np.array([[48, 5]])
18
m = folium.Map([48.0, 5.0], zoom_start=6)
19
hm = HeatMap(data)
20
m.add_child(hm)
21
m._repr_html_()
22
23
out = normalize(m._parent.render())
24
25
# We verify that the script import is present.
26
script = '<script src="https://cdn.jsdelivr.net/gh/python-visualization/folium@main/folium/templates/leaflet_heat.min.js"></script>' # noqa
27
assert script in out
28
29
# We verify that the script part is correct.
30
tmpl = Template(
31
"""
32
var {{this.get_name()}} = L.heatLayer(
33
{{this.data}},
34
{
35
minOpacity: {{this.min_opacity}},
36
maxZoom: {{this.max_zoom}},
37
radius: {{this.radius}},
38
blur: {{this.blur}},
39
gradient: {{this.gradient}}
40
})
41
.addTo({{this._parent.get_name()}});
42
"""
43
)
44
45
assert tmpl.render(this=hm)
46
47
bounds = m.get_bounds()
48
np.testing.assert_allclose(
49
bounds,
50
[
51
[46.218566840847025, 3.0302801394447734],
52
[50.75345011431167, 7.132453997672826],
53
],
54
)
55
56
57
def test_heatmap_data():
58
data = HeatMap(np.array([[3, 4, 1], [5, 6, 1], [7, 8, 0.5]])).data
59
assert isinstance(data, list)
60
assert len(data) == 3
61
for i in range(len(data)):
62
assert isinstance(data[i], list)
63
assert len(data[i]) == 3
64
65
66
def test_heat_map_exception():
67
with pytest.raises(ValueError):
68
HeatMap(np.array([[4, 5, 1], [3, 6, np.nan]]))
69
with pytest.raises(Exception):
70
HeatMap(np.array([3, 4, 5]))
71
72