Skip to content

Commit

Permalink
handle nans in colormap (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling authored Sep 26, 2024
1 parent 29e340c commit cce62e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
15 changes: 13 additions & 2 deletions ecoscope/analysis/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,22 @@ def apply_color_map(dataframe, input_column_name, cmap, output_column_name=None)
cmap = mpl.colormaps[cmap]
cmap = cmap.resampled(dataframe[input_column_name].nunique())

cmap_colors = cmap(range(dataframe[input_column_name].nunique()))
if pd.api.types.is_numeric_dtype(dataframe[input_column_name].dtype):
cmap_colors = []
val_min = dataframe[input_column_name].min()
val_max = dataframe[input_column_name].max()
for val in dataframe[input_column_name].unique():
cmap_colors.append(cmap((val - val_min) / (val_max - val_min)))
else:
cmap_colors = cmap(range(len(dataframe[input_column_name].unique())))

color_list = []
for color in cmap_colors:
color_list.append(tuple([round(val * 255) for val in color]))

# convert to hex first to put values in range(0,255), then to an RGBA tuple
cmap = pd.Series(
[hex_to_rgba(mpl.colors.to_hex(color)) for color in cmap_colors],
color_list,
index=dataframe[input_column_name].unique(),
)

Expand Down
5 changes: 4 additions & 1 deletion ecoscope/analysis/feature_density.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import numpy as np


def calculate_feature_density(selection, grid, geometry_type="point"):
def clip_density(cell):
if geometry_type == "point":
Expand All @@ -12,5 +15,5 @@ def clip_density(cell):
raise ValueError("Unsupported geometry type")

grid["density"] = grid.geometry.apply(clip_density)
# grid["density"] = grid["density"].replace(0, np.nan) # Set 0's to nan so they don't draw on map
grid["density"] = grid["density"].replace(0, np.nan) # Set 0's to nan so they don't draw on map
return grid
18 changes: 15 additions & 3 deletions tests/test_classifier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import pandas as pd
import numpy as np
from ecoscope.base import Trajectory
from ecoscope.analysis.classifier import apply_classification, apply_color_map

Expand Down Expand Up @@ -52,15 +53,26 @@ def test_apply_colormap(sample_df, cmap):
apply_classification(sample_df, input_column_name="value", scheme="equal_interval")
apply_color_map(sample_df, "value_classified", cmap, output_column_name="colormap")

assert len(set(sample_df["colormap"].unique())) == len(sample_df["value_classified"].unique())
assert len(sample_df["colormap"].unique()) == len(sample_df["value_classified"].unique())


def test_apply_colormap_with_nan():
df = pd.DataFrame(
data={"value": [1, 2, 3, 4, np.nan]},
index=["A", "B", "C", "D", "E"],
)
apply_color_map(df, "value", "viridis", output_column_name="colormap")

assert len(df["colormap"].unique()) == len(df["value"].unique())
assert df.loc["E"]["colormap"] == (0, 0, 0, 0)


def test_apply_colormap_k2(sample_df):
apply_classification(sample_df, input_column_name="value", scheme="equal_interval", k=2)
cmap = "viridis"
apply_color_map(sample_df, "value_classified", cmap, output_column_name="colormap")

assert len(set(sample_df["colormap"].unique())) == len(sample_df["value_classified"].unique())
assert len(sample_df["colormap"].unique()) == len(sample_df["value_classified"].unique())


def test_apply_colormap_user_defined(movebank_relocations):
Expand All @@ -79,7 +91,7 @@ def test_apply_colormap_user_defined(movebank_relocations):
]

apply_color_map(trajectory, "speed_bins", cmap)
assert len(set(trajectory["speed_bins_colormap"].unique())) == len(trajectory["speed_bins"].unique())
assert len(trajectory["speed_bins_colormap"].unique()) == len(trajectory["speed_bins"].unique())


def test_apply_colormap_cmap_user_defined_bad(movebank_relocations):
Expand Down

0 comments on commit cce62e3

Please sign in to comment.