Skip to content

Commit

Permalink
tests for heatmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygeo committed Nov 13, 2017
1 parent dde580c commit 9f1dabc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions mapboxgl/__init__.py
@@ -1,4 +1,4 @@
from .viz import CircleViz, GraduatedCircleViz
from .viz import CircleViz, GraduatedCircleViz, HeatmapViz

__version__ = "0.0.3"
__all__ = ['CircleViz', 'GraduatedCircleViz']
__all__ = ['CircleViz', 'GraduatedCircleViz', 'HeatmapViz']
2 changes: 2 additions & 0 deletions mapboxgl/utils.py
Expand Up @@ -27,6 +27,7 @@ def df_to_geojson(df, properties=None, lat='lat', lon='lon', precision=None):

return geojson


def scale_between(minval, maxval, numStops):
""" Scale a min and max value to equal interval domain with
numStops discrete values
Expand Down Expand Up @@ -57,6 +58,7 @@ def create_radius_stops(breaks, min_radius, max_radius):
stops.append([b, radius_breaks[i]])
return stops


def create_weight_stops(breaks):
"""Convert data breaks into a heatmap-weight ramp
"""
Expand Down
5 changes: 3 additions & 2 deletions mapboxgl/viz.py
Expand Up @@ -188,6 +188,7 @@ def create_html(self):

return templates.format('graduated_circle', **options)


class HeatmapViz(object):
"""Create a heatmap viz"""

Expand All @@ -210,8 +211,8 @@ def __init__(self,
:param weight_property: property to determine heatmap weight. EX. "population"
:param weight_stops: stops to determine heatmap weight. EX. [[10, 0], [100, 1]]
:param color_stops: stops to determine heatmap color. EX. [[0, "red"], [0.5, "blue"], [1, "green"]]]
:param radius_stops: stops to determine heatmap radius based on zoom. EX: [[0, 1], [12, 30]]]
:param color_stops: stops to determine heatmap color. EX. [[0, "red"], [0.5, "blue"], [1, "green"]]
:param radius_stops: stops to determine heatmap radius based on zoom. EX: [[0, 1], [12, 30]]
:param style_url: url to mapbox style
:param access_token: Mapbox GL JS access token.
Expand Down
17 changes: 16 additions & 1 deletion tests/test_html.py
Expand Up @@ -3,7 +3,7 @@

import pytest

from mapboxgl.viz import CircleViz, GraduatedCircleViz
from mapboxgl.viz import CircleViz, GraduatedCircleViz, HeatmapViz
from mapboxgl.errors import TokenError


Expand All @@ -23,6 +23,7 @@ def test_secret_key_CircleViz(data):
with pytest.raises(TokenError):
CircleViz(data, access_token=secret)


def test_secret_key_GraduatedCircleViz(data):
"""Secret key raises a token error
"""
Expand Down Expand Up @@ -87,3 +88,17 @@ def test_display_GraduatedCircleViz(display, data):
access_token=TOKEN)
viz.show()
display.assert_called_once()


@patch('mapboxgl.viz.display')
def test_display_HeatmapViz(display, data):
"""Assert that show calls the mocked display function
"""
viz = HeatmapViz(data,
weight_property="Avg Medicare Payments",
weight_stops=[[10, 0], [100, 1]],
color_stops=[[0, "red"], [0.5, "blue"], [1, "green"]],
radius_stops=[[0, 1], [12, 30]],
access_token=TOKEN)
viz.show()
display.assert_called_once()
7 changes: 6 additions & 1 deletion tests/test_utils.py
Expand Up @@ -2,7 +2,7 @@

import pytest

from mapboxgl.utils import df_to_geojson, scale_between, create_radius_stops
from mapboxgl.utils import df_to_geojson, scale_between, create_radius_stops, create_weight_stops


class MockDataframe(object):
Expand Down Expand Up @@ -85,3 +85,8 @@ def test_create_radius_stops(df):
domain = [7678.214347826088, 5793.63142857143, 1200]
radius_stops = create_radius_stops(domain, 1, 10)
assert radius_stops == [[7678.214347826088, 1.0], [5793.63142857143, 4.0], [1200, 7.0]]


def test_create_weight_stops(df):
res = create_weight_stops([1, 2, 3, 4])
assert res == [[1, 0.0], [2, 0.25], [3, 0.5], [4, 0.75]]

0 comments on commit 9f1dabc

Please sign in to comment.