Skip to content

Commit

Permalink
Plot rgb with nan (#693)
Browse files Browse the repository at this point in the history
* Update sphinx_rtd_theme from 0.5.1 to 0.5.2

* Update sphinx_gallery from 0.8.2 to 0.9.0

* changes to plot nan values earthpy

* added a test

* small formatting

* Update pytest from 6.2.3 to 6.2.4

* Update tox from 3.23.0 to 3.23.1

* Update sphinx from 3.5.3 to 4.0.2

* Update pre-commit from 2.11.1 to 2.13.0

* Update pytest-cov from 2.11.1 to 2.12.1

* Added very basic Check CRS function.

* Update importlib-metadata from 3.10.0 to 4.5.0 (#706)

Co-authored-by: pyup-bot <github-bot@pyup.io>
  • Loading branch information
nkorinek and pyup-bot committed Jun 17, 2021
1 parent 127cb06 commit 817d6d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
13 changes: 11 additions & 2 deletions earthpy/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _stretch_im(arr, str_clip):
s_max = 100 - str_clip
arr_rescaled = np.zeros_like(arr)
for ii, band in enumerate(arr):
lower, upper = np.percentile(band, (s_min, s_max))
lower, upper = np.nanpercentile(band, (s_min, s_max))
arr_rescaled[ii] = exposure.rescale_intensity(
band, in_range=(lower, upper)
)
Expand Down Expand Up @@ -405,6 +405,11 @@ def plot_rgb(
if stretch:
rgb_bands = _stretch_im(rgb_bands, str_clip)

nan_check = np.isnan(rgb_bands)

if np.any(nan_check):
rgb_bands = np.ma.masked_array(rgb_bands, nan_check)

# If type is masked array - add alpha channel for plotting
if ma.is_masked(rgb_bands):
# Build alpha channel
Expand Down Expand Up @@ -582,7 +587,11 @@ def hist(
hist_range = (np.nanmin(arr_comp), np.nanmax(arr_comp))
fig, ax = plt.subplots(figsize=figsize)
ax.hist(
arr_comp, range=hist_range, bins=bins, color=colors[0], alpha=alpha
arr_comp,
range=hist_range,
bins=bins,
color=colors[0],
alpha=alpha,
)
if title:
ax.set(title=title[0])
Expand Down
16 changes: 16 additions & 0 deletions earthpy/tests/test_plot_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ def test_stretch_image(rgb_image):
plt.close()


def test_stretch_image_nan(rgb_image):
"""Test that running stretch actually stretches the data
to a max value of 255 and min value of 0 when nan values
are present within the plot_rgb fun."""

im, _ = rgb_image
np.place(im, im > 150, [0])
im = np.where(im < 25, np.nan, im)

ax = plot_rgb(im, stretch=True)
max_val = ax.get_images()[0].get_array().max()
min_val = ax.get_images()[0].get_array().min()
assert max_val == 255 and min_val == 0
plt.close()


def test_masked_im(rgb_image):
"""Test that a masked image will be plotted using an alpha channel.
Thus it should return an array that has a 4th dimension representing
Expand Down

0 comments on commit 817d6d4

Please sign in to comment.