-
Notifications
You must be signed in to change notification settings - Fork 32
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
WIP classify to rgba #211
Merged
Merged
WIP classify to rgba #211
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e36a113
add classify rgb function
knaaptime bec1919
alpha
knaaptime 6070e8c
move to util; add test
knaaptime 1f7434e
CI: ensure 3.9 envs are compatible (#218)
martinfleis 4de91bb
CI: doctest only on ubuntu latest (#219)
martinfleis 0fad7cd
CI: test against Python 3.12 (#220)
martinfleis aab5ddd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8e9ac98
[pre-commit.ci] pre-commit autoupdate (#221)
pre-commit-ci[bot] f1f4add
plot histogram
knaaptime d780b2f
reorder args
knaaptime ce219a0
despine optional; add comments
knaaptime 171a566
warn not raise if sns missing
knaaptime 394bd67
martins improvements
knaaptime 918ff1c
simplify args
knaaptime 815a75f
mpl not pandas in docstring
knaaptime e668c8e
linewidth
knaaptime 447df21
add tests
knaaptime c89ef62
pytestmpl
knaaptime 5ff49da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 46fee1e
rm unused kwarg logic
knaaptime bfefddf
forgot color
knaaptime e85b810
Merge branch 'main' of github.com:pysal/mapclassify into classify
knaaptime d8805db
kwargs no k
knaaptime cb9d014
document kwargs
knaaptime 0f64631
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import geopandas | ||
import numpy as np | ||
from mapclassify.util import get_rgba | ||
|
||
world = geopandas.read_file( | ||
"https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip" | ||
) | ||
|
||
|
||
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), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from ._classify_API import classify as _classify | ||
|
||
|
||
def get_rgba( | ||
values, | ||
classifier="quantiles", | ||
cmap="viridis", | ||
alpha=1, | ||
nan_color=[255, 255, 255, 255], | ||
**kwargs, | ||
): | ||
"""Convert array of values into RGBA colors using a colormap and classifier. | ||
|
||
Parameters | ||
---------- | ||
values : list-like | ||
array of input values | ||
classifier : str, optional | ||
string description of a mapclassify classifier, by default "quantiles" | ||
cmap : str, optional | ||
name of matplotlib colormap to use, by default "viridis" | ||
alpha : float | ||
alpha parameter that defines transparency. Should be in the range [0,1] | ||
nan_color : list, optional | ||
RGBA color to fill NaN values, by default [255, 255, 255, 255] | ||
kwargs : dict | ||
additional keyword arguments are passed to `mapclassify.classify` | ||
|
||
Returns | ||
------- | ||
numpy.array | ||
array of lists with each list containing four values that define a color using | ||
RGBA specification. | ||
""" | ||
try: | ||
import pandas as pd | ||
from matplotlib import cm | ||
from matplotlib.colors import Normalize | ||
except ImportError as e: | ||
raise ImportError("This function requires pandas and matplotlib") from e | ||
if not (alpha <= 1) and (alpha >= 0): | ||
raise ValueError("alpha must be in the range [0,1]") | ||
if not pd.api.types.is_list_like(nan_color) and not len(nan_color) == 4: | ||
raise ValueError("`nan_color` must be list-like of 4 values: (R,G,B,A)") | ||
|
||
# only operate on non-NaN values | ||
v = pd.Series(values, dtype=object) | ||
legit_indices = v[~v.isna()].index.values | ||
|
||
# transform (non-NaN) values into class bins | ||
bins = _classify(v.dropna().values, scheme=classifier, **kwargs).yb | ||
|
||
# create a normalizer using the data's range (not strictly 1-k...) | ||
norm = Normalize(min(bins), max(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) | ||
v.update(colors) | ||
v = v.fillna(f"{nan_color}").apply(list) | ||
|
||
return v.values |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kwargs should probably be mentioned here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, i'm always unsure what to do in the docstrings when there's a catchall
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always mention where are they passed to.