-
Notifications
You must be signed in to change notification settings - Fork 224
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
Changes from 8 commits
7e4c411
89da916
a773929
8c89da2
ba87dff
819cc8b
6a09606
fabcb09
ba9a2a3
40eb80a
d1d692b
15a7207
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,7 +112,7 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False): | |
Returns | ||
------- | ||
kind : str | ||
One of: ``'file'``, ``'grid'``, ``'matrix'``, ``'vectors'``. | ||
One of: ``'file'``, ``'grid'``, ``image``, ``'matrix'``, ``'vectors'``. | ||
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. TODO handle merge conflicts after #2493 is merged. |
||
|
||
Examples | ||
-------- | ||
|
@@ -130,11 +130,13 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False): | |
'file' | ||
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3))) | ||
'grid' | ||
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5))) | ||
'image' | ||
""" | ||
if isinstance(data, (str, pathlib.PurePath)): | ||
kind = "file" | ||
elif isinstance(data, xr.DataArray): | ||
kind = "grid" | ||
kind = "image" if len(data.dims) == 3 else "grid" | ||
elif hasattr(data, "__geo_interface__"): | ||
kind = "geojson" | ||
elif data is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
outs: | ||
- md5: 2e919645d5af956ec4f8aa054a86a70a | ||
size: 110214 | ||
path: test_grdimage_image.png |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
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 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dtype", | ||
["int8", "uint16", "int16", "uint32", "int32", "float32", "float64"], | ||
) | ||
def test_grdimage_image_dataarray_unsupported_dtype(dtype, xr_image): | ||
""" | ||
Plot a 3-band RGB image using xarray.DataArray input, with an unsupported | ||
data type. | ||
""" | ||
fig = Figure() | ||
image = xr_image.astype(dtype=dtype) | ||
with pytest.warns(expected_warning=RuntimeWarning) as record: | ||
fig.grdimage(grid=image) | ||
assert len(record) == 1 |
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.
Took a look at
grdhisteq
, but it doesn't appear to support 3-band image inputs, only 1-band grids, so suggesting to useskimage.exposure.equalize_hist
instead. Could probably raise a feature request to upstream GMT to let grdhisteq support this too?