Skip to content

Commit

Permalink
filter NaNs if they arrive in the legend (wildlife-dynamics#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling authored Nov 29, 2024
1 parent fbe8125 commit f7eb723
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
22 changes: 19 additions & 3 deletions ecoscope/mapping/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,31 @@ def add_legend(self, labels: list | pd.Series, colors: list | pd.Series, **kwarg
style: dict
Additional style params
"""
nans = None
if isinstance(labels, pd.Series):
if pd.api.types.is_numeric_dtype(labels):
nans = np.argwhere(np.isnan(labels))
labels = labels.unique().tolist()
if isinstance(colors, pd.Series):
colors = colors.unique().tolist()

labels = [str(label) for label in labels]
colors = [color_tuple_to_css(color) if isinstance(color, tuple) else color for color in colors]
if len(labels) != len(colors):
raise ValueError("Unique label and color values must be of equal number")

self.add_widget(LegendWidget(labels=labels, colors=colors, **kwargs))
if nans is not None:
filtered_labels = []
filtered_colors = []
for index, value in enumerate(labels):
if index not in nans:
filtered_labels.append(labels[index])
filtered_colors.append(colors[index])
labels = filtered_labels
colors = filtered_colors

widget_labels = [str(label) for label in labels]
widget_colors = [color_tuple_to_css(color) if isinstance(color, tuple) else color for color in colors]

self.add_widget(LegendWidget(labels=widget_labels, colors=widget_colors, **kwargs))

def add_north_arrow(self, **kwargs):
"""
Expand Down
33 changes: 33 additions & 0 deletions tests/test_ecomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import ee
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
import ecoscope
Expand All @@ -16,6 +17,7 @@
TitleWidget,
SaveImageWidget,
FullscreenWidget,
LegendWidget,
)


Expand Down Expand Up @@ -81,6 +83,37 @@ def test_add_legend_series():
m = EcoMap(default_widgets=False)
m.add_legend(labels=pd.Series(["Black", "White"]), colors=pd.Series([(0, 0, 0, 255), (255, 255, 255, 255)]))
assert len(m.deck_widgets) == 1
legend = m.deck_widgets[0]
assert isinstance(legend, LegendWidget)
assert legend.labels == ["Black", "White"]
assert legend.colors == ["rgba(0, 0, 0, 1.0)", "rgba(255, 255, 255, 1.0)"]


def test_add_legend_series_with_nan():
m = EcoMap(default_widgets=False)
m.add_legend(
labels=pd.Series([0, 1, np.nan, 5, np.nan]),
colors=pd.Series([(0, 0, 0, 255), (255, 255, 255, 255), (0, 0, 0, 0), (100, 100, 100, 255), (0, 0, 0, 0)]),
)
assert len(m.deck_widgets) == 1
legend = m.deck_widgets[0]
assert isinstance(legend, LegendWidget)
assert legend.labels == ["0.0", "1.0", "5.0"]
assert legend.colors == ["rgba(0, 0, 0, 1.0)", "rgba(255, 255, 255, 1.0)", "rgba(100, 100, 100, 1.0)"]


def test_add_legend_series_unbalanced_good():
m = EcoMap(default_widgets=False)
m.add_legend(
labels=pd.Series(["Black", "White", "White"]),
colors=pd.Series([(0, 0, 0, 255), (255, 255, 255, 255), (255, 255, 255, 255)]),
)


def test_add_legend_series_unbalanced_bad():
m = EcoMap(default_widgets=False)
with pytest.raises(ValueError, match="Unique label and color values must be of equal number"):
m.add_legend(labels=pd.Series(["Black", "White"]), colors=pd.Series([(0, 0, 0, 255), (0, 0, 0, 255)]))


def test_add_north_arrow():
Expand Down

0 comments on commit f7eb723

Please sign in to comment.