-
Notifications
You must be signed in to change notification settings - Fork 219
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
Allow passing RGB xarray.DataArray images into grdimage #2590
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7e4c411
Allow passing RGB xarray.DataArray images into grdimage
weiji14 89da916
Refactor to use tempfile_from_image
weiji14 a773929
Let tilemap use tempfile_from_image func in virtualfile_from_data
weiji14 8c89da2
Lint to remove unneeded imports
weiji14 ba87dff
Merge branch 'main' into grdimage/rgb_dataarray
weiji14 819cc8b
Update docstring of grdimage with upstream GMT 6.4.0
weiji14 6a09606
Raise RuntimeWarning when input image dtype is not uint8
weiji14 fabcb09
Update pygmt/helpers/utils.py
weiji14 ba9a2a3
Set intersphinx link for xr.DataArray in docstring
weiji14 40eb80a
Merge branch 'main' into grdimage/rgb_dataarray
weiji14 d1d692b
Add back rioxarray import and ImportError check to tilemap.py
weiji14 15a7207
Lint to add missing line
weiji14 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,4 @@ | ||
outs: | ||
- md5: 2e919645d5af956ec4f8aa054a86a70a | ||
size: 110214 | ||
path: test_grdimage_image.png |
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,63 @@ | ||
""" | ||
Test Figure.grdimage on 3-band RGB images. | ||
""" | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
import xarray as xr | ||
from pygmt import Figure, which | ||
|
||
rasterio = pytest.importorskip("rasterio") | ||
rioxarray = pytest.importorskip("rioxarray") | ||
|
||
|
||
@pytest.fixture(scope="module", name="xr_image") | ||
def fixture_xr_image(): | ||
""" | ||
Load the image data from Blue Marble as an xarray.DataArray with shape | ||
{"band": 3, "y": 180, "x": 360}. | ||
""" | ||
geotiff = which(fname="@earth_day_01d_p", download="c") | ||
with rioxarray.open_rasterio(filename=geotiff) as rda: | ||
if len(rda.band) == 1: | ||
with rasterio.open(fp=geotiff) as src: | ||
df_colormap = pd.DataFrame.from_dict( | ||
data=src.colormap(1), orient="index" | ||
) | ||
array = src.read() | ||
|
||
red = np.vectorize(df_colormap[0].get)(array) | ||
green = np.vectorize(df_colormap[1].get)(array) | ||
blue = np.vectorize(df_colormap[2].get)(array) | ||
# alpha = np.vectorize(df_colormap[3].get)(array) | ||
|
||
rda.data = red | ||
da_red = rda.astype(dtype=np.uint8).copy() | ||
rda.data = green | ||
da_green = rda.astype(dtype=np.uint8).copy() | ||
rda.data = blue | ||
da_blue = rda.astype(dtype=np.uint8).copy() | ||
|
||
xr_image = xr.concat(objs=[da_red, da_green, da_blue], dim="band") | ||
assert xr_image.sizes == {"band": 3, "y": 180, "x": 360} | ||
return xr_image | ||
Comment on lines
+20
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should replace this with the |
||
|
||
|
||
@pytest.mark.mpl_image_compare | ||
def test_grdimage_image(): | ||
""" | ||
Plot a 3-band RGB image using file input. | ||
""" | ||
fig = Figure() | ||
fig.grdimage(grid="@earth_day_01d") | ||
return fig | ||
|
||
|
||
@pytest.mark.mpl_image_compare(filename="test_grdimage_image.png") | ||
def test_grdimage_image_dataarray(xr_image): | ||
""" | ||
Plot a 3-band RGB image using xarray.DataArray input. | ||
""" | ||
fig = Figure() | ||
fig.grdimage(grid=xr_image) | ||
return fig |
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.
Should maybe put this logic in
lib.virtualfile_from_data
, since we'll need to reuse it forgrdview
,grdcut
, and possibly other GMT modules that work with GMT_IMAGE.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.
Ok, created a new
tempfile_from_image
function at 89da916 (similar totempfile_from_geojson
). This can be replaced withlib.virtualfile_from_image
once that is implemented.