-
Notifications
You must be signed in to change notification settings - Fork 14
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
interactive plotting with lonboard
#67
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
302a97b
draft of the explore functionality based on `lonboard`
keewis 3606fb2
expose the draft
keewis 0851443
docstring for `explore`
keewis 774cf3f
use the proper import from `matplotlib` for the standard normalizer
keewis 2ffbb5f
use `pandas` to compute the half range
keewis 6348ce2
specify `vmin` and `vmax`
keewis 0c2dc44
explicitly specify `skipna`
keewis 41856c1
refactor to construct the polygons as geoarrow
keewis 091a7bd
allow choosing the transparency
keewis 2ef3192
move the healpix-specific code to `xdggs.healpix`
keewis 46411a4
change the signature of `cell_boundaries`
keewis 05495b8
also have h3 support the geoarrow cell boundaries
keewis 5149fa4
fix several typos
keewis 1ec8bf3
adjust the tests to fit the new dateline fix algorithm
keewis 269f684
update the list of extra dependencies for `explore`
keewis 27a6193
mention that this only works for 1D arrays for now
keewis f7ddf09
add optional dependencies for `explore`
keewis e2958b7
explicitly use `pyproj` to construct the crs string
keewis f96a64b
raise an error if the geometry type is wrong
keewis 49b54fe
wrap the output of `crs.to_json()` in a json dict
keewis 20fd427
verify that both backends produce the same polygons
keewis 8117e7d
move the data normalization to a separate function
keewis 0a2f577
move the table creation to a separate function
keewis 71f612a
check that the normalization works properly
keewis 998e6fb
coverage configuration
keewis 0d31059
pass on `center` to the normalizing function
keewis 5a71559
skip the plotting module if `arro3.core` is not available
keewis 76ffe15
use `json` to construct the arrow extension metadata
keewis 84938a1
Revert "skip the plotting module if `arro3.core` is not available"
keewis 6ca24de
workaround for missing macos-arm packages of arro3-core
keewis 6d9b78a
install arro3-core using `pip`
keewis c891e12
always include the cell ids in the table data
keewis 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
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
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
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,72 @@ | ||
import numpy as np | ||
|
||
|
||
def create_arrow_table(polygons, arr, coords=None): | ||
from arro3.core import Array, ChunkedArray, Schema, Table | ||
|
||
if coords is None: | ||
coords = ["latitude", "longitude"] | ||
|
||
array = Array.from_arrow(polygons) | ||
name = arr.name or "data" | ||
arrow_arrays = { | ||
"geometry": array, | ||
"cell_ids": ChunkedArray([Array.from_numpy(arr.coords["cell_ids"])]), | ||
name: ChunkedArray([Array.from_numpy(arr.data)]), | ||
} | { | ||
coord: ChunkedArray([Array.from_numpy(arr.coords[coord].data)]) | ||
for coord in coords | ||
if coord in arr.coords | ||
} | ||
|
||
fields = [array.field.with_name(name) for name, array in arrow_arrays.items()] | ||
schema = Schema(fields) | ||
|
||
return Table.from_arrays(list(arrow_arrays.values()), schema=schema) | ||
|
||
|
||
def normalize(var, center=None): | ||
from matplotlib.colors import CenteredNorm, Normalize | ||
|
||
if center is None: | ||
vmin = var.min(skipna=True) | ||
vmax = var.max(skipna=True) | ||
normalizer = Normalize(vmin=vmin, vmax=vmax) | ||
else: | ||
halfrange = np.abs(var - center).max(skipna=True) | ||
normalizer = CenteredNorm(vcenter=center, halfrange=halfrange) | ||
|
||
return normalizer(var.data) | ||
|
||
|
||
def explore( | ||
arr, | ||
cell_dim="cells", | ||
cmap="viridis", | ||
center=None, | ||
alpha=None, | ||
): | ||
import lonboard | ||
from lonboard import SolidPolygonLayer | ||
from lonboard.colormap import apply_continuous_cmap | ||
from matplotlib import colormaps | ||
|
||
if len(arr.dims) != 1 or cell_dim not in arr.dims: | ||
raise ValueError( | ||
f"exploration only works with a single dimension ('{cell_dim}')" | ||
) | ||
|
||
cell_ids = arr.dggs.coord.data | ||
grid_info = arr.dggs.grid_info | ||
|
||
polygons = grid_info.cell_boundaries(cell_ids, backend="geoarrow") | ||
|
||
normalized_data = normalize(arr.variable, center=center) | ||
|
||
colormap = colormaps[cmap] | ||
colors = apply_continuous_cmap(normalized_data, colormap, alpha=alpha) | ||
|
||
table = create_arrow_table(polygons, arr) | ||
layer = SolidPolygonLayer(table=table, filled=True, get_fill_color=colors) | ||
|
||
return lonboard.Map(layer) |
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import geoarrow.pyarrow as ga | ||
import shapely | ||
|
||
from xdggs.tests.matchers import ( # noqa: F401 | ||
Match, | ||
MatchResult, | ||
assert_exceptions_equal, | ||
) | ||
|
||
|
||
def geoarrow_to_shapely(arr): | ||
return shapely.from_wkb(ga.as_wkb(arr)) |
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
Oops, something went wrong.
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.
It doesn't look like you have docs yet, but if you use sphinx or mkdocs (with mkdocstrings), lonboard implements intersphinx, so you should be able to set it up so the return value is a hyperlink to the lonboard docs.
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.
thanks for the hint. I'll probably use
sphinx
to set up the docs, so I'll definitely make use of that!