Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplement rgbas #228

Merged
merged 9 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions mapclassify/tests/test_rgba.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import geopandas
import numpy as np
from numpy.testing import assert_array_equal
from mapclassify.util import get_rgba

world = geopandas.read_file(
Expand All @@ -9,9 +10,5 @@

def test_rgba():
colors = get_rgba(world.area, cmap="viridis")[0]
assert colors == [
np.float64(68.08602),
np.float64(1.24287),
np.float64(84.000825),
np.float64(255.0),
]
assert_array_equal(colors, np.array([68, 1, 84, 255]))

37 changes: 14 additions & 23 deletions mapclassify/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import numpy as np

from ._classify_API import classify as _classify


def get_rgba(
values,
classifier="quantiles",
scheme="quantiles",
jGaboardi marked this conversation as resolved.
Show resolved Hide resolved
cmap="viridis",
alpha=1,
nan_color=[255, 255, 255, 255],
Expand All @@ -15,7 +17,7 @@ def get_rgba(
----------
values : list-like
array of input values
classifier : str, optional
scheme : str, optional
string description of a mapclassify classifier, by default "quantiles"
knaaptime marked this conversation as resolved.
Show resolved Hide resolved
cmap : str, optional
name of matplotlib colormap to use, by default "viridis"
Expand All @@ -29,12 +31,12 @@ def get_rgba(
Returns
-------
numpy.array
array of lists with each list containing four values that define a color using
RGBA specification.
numpy array (n,4) of lists with each list containing four values that define a
knaaptime marked this conversation as resolved.
Show resolved Hide resolved
color using RGBA specification.
"""
try:
import pandas as pd
from matplotlib import cm
from matplotlib import colormaps
from matplotlib.colors import Normalize
except ImportError as e:
raise ImportError("This function requires pandas and matplotlib") from e
Expand All @@ -46,28 +48,17 @@ def get_rgba(
# only operate on non-NaN values
v = pd.Series(values, dtype=object)
legit_indices = v[~v.isna()].index.values

legit_vals = v.dropna().values
# transform (non-NaN) values into class bins
bins = _classify(v.dropna().values, scheme=classifier, **kwargs).yb
bins = _classify(legit_vals, scheme=scheme, **kwargs).yb

# create a normalizer using the data's range (not strictly 1-k...)
norm = Normalize(min(bins), max(bins))
normalized_vals = norm(bins)

# map values to colors
n_cmap = cm.ScalarMappable(norm=norm, cmap=cmap)

# create array of RGB values (lists of 4) of length n
vals = [n_cmap.to_rgba(i, alpha=alpha) for i in bins]

# convert decimals to whole numbers
rgbas = []
for val in vals:
# convert each value in the array of lists
rgbas.append([i * 255 for i in val])

# replace non-nan values with colors
colors = pd.Series(rgbas, index=legit_indices)
rgbas = colormaps[cmap](normalized_vals, bytes=True)
colors = pd.Series(list(rgbas), index=legit_indices).apply(np.array)
v.update(colors)
v = v.fillna(f"{nan_color}").apply(list)
v = v.fillna(f"{nan_color}").apply(np.array)

return v.values
return np.stack(v.values)