Skip to content
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

Add arbitrary window smoother and refactor existing smoothers #1223

Merged
merged 8 commits into from
Jan 14, 2020
14 changes: 12 additions & 2 deletions docs/_templates/overrides/metpy.calc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ Standard Atmosphere
height_to_pressure_std
pressure_to_height_std

Smoothing
---------
.. autosummary::
:toctree: ./

smooth_gaussian
smooth_window
smooth_rectangular
smooth_circular
smooth_n_point

Other
-----

Expand All @@ -185,5 +196,4 @@ Other
parse_angle
reduce_point_density
resample_nn_1d
smooth_gaussian
smooth_n_point

24 changes: 12 additions & 12 deletions docs/gempak.rst
Original file line number Diff line number Diff line change
Expand Up @@ -758,19 +758,19 @@ blue is uncertain of parity, and white is unevaluated.
<td></td>
</tr>
<tr>
<td class="tg-notimplemented">SM5S(S)</td>
<td class="tg-notimplemented">5-point smoother</td>
<td class="tg-notimplemented"><a href="https://github.com/Unidata/MetPy/issues/659">Issue #659</a></td>
<td></td>
<td class="tg-implemented">SM5S(S)</td>
<td class="tg-implemented">5-point smoother</td>
<td class="tg-implemented"><a href="api/generated/metpy.calc.smooth_n_point.html#metpy.calc.smooth_n_point">metpy.calc.smooth_n_point</a></td>
<td></td>
<td class="tg-no">No</td>
<td></td>
</tr>
<tr>
<td class="tg-notimplemented">SM9S(S)</td>
<td class="tg-notimplemented">9-point smoother</td>
<td class="tg-notimplemented"><a href="https://github.com/Unidata/MetPy/issues/659">Issue #659</a></td>
<td></td>
<td class="tg-implemented">SM9S(S)</td>
<td class="tg-implemented">9-point smoother</td>
<td class="tg-implemented"><a href="api/generated/metpy.calc.smooth_n_point.html#metpy.calc.smooth_n_point">metpy.calc.smooth_n_point</a></td>
<td></td>
<td class="tg-no">No</td>
<td></td>
</tr>
<tr>
Expand Down Expand Up @@ -1150,11 +1150,11 @@ blue is uncertain of parity, and white is unevaluated.
<td></td>
</tr>
<tr>
<td class="tg-notimplemented">SM5V(V)</td>
<td class="tg-notimplemented">5-point smoother</td>
<td class="tg-notimplemented"><a href="https://github.com/Unidata/MetPy/issues/659">Issue #659</a></td>
<td></td>
<td class="tg-implemented">SM5V(V)</td>
<td class="tg-implemented">5-point smoother</td>
<td class="tg-implemented"><a href="api/generated/metpy.calc.smooth_n_point.html#metpy.calc.smooth_n_point">metpy.calc.smooth_n_point</a></td>
<td></td>
<td class="tg-no">No</td>
<td></td>
</tr>
<tr>
Expand Down
75 changes: 75 additions & 0 deletions examples/calculations/Smoothing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (c) 2015-2018 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""
Smoothing
=========
Using MetPy's smoothing functions.
This example demonstrates the various ways that MetPy's smoothing function
can be utilized. While this example utilizes basic NumPy arrays, these
functions all work equally well with Pint Quantities or xarray DataArrays.
"""

from itertools import product

import matplotlib.pyplot as plt
import numpy as np

import metpy.calc as mpcalc

###########################################
# Start with a base pattern with random noise
np.random.seed(61461542)
size = 128
x, y = np.mgrid[:size, :size]
distance = np.sqrt((x - size / 2) ** 2 + (y - size / 2) ** 2)
raw_data = np.random.random((size, size)) * 0.3 + distance / distance.max() * 0.7

fig, ax = plt.subplots(1, 1, figsize=(4, 4))
ax.set_title('Raw Data')
ax.imshow(raw_data, vmin=0, vmax=1)
ax.axis('off')
plt.show()

###########################################
# Now, create a grid showing different smoothing options
fig, ax = plt.subplots(3, 3, figsize=(12, 12))
for i, j in product(range(3), range(3)):
ax[i, j].axis('off')

# Gaussian Smoother
ax[0, 0].imshow(mpcalc.smooth_gaussian(raw_data, 3), vmin=0, vmax=1)
ax[0, 0].set_title('Gaussian - Low Degree')

ax[0, 1].imshow(mpcalc.smooth_gaussian(raw_data, 8), vmin=0, vmax=1)
ax[0, 1].set_title('Gaussian - High Degree')

# Rectangular Smoother
ax[0, 2].imshow(mpcalc.smooth_rectangular(raw_data, (3, 7), 2), vmin=0, vmax=1)
ax[0, 2].set_title('Rectangular - 3x7 Window\n2 Passes')

# 5-point smoother
ax[1, 0].imshow(mpcalc.smooth_n_point(raw_data, 5, 1), vmin=0, vmax=1)
ax[1, 0].set_title('5-Point - 1 Pass')

ax[1, 1].imshow(mpcalc.smooth_n_point(raw_data, 5, 4), vmin=0, vmax=1)
ax[1, 1].set_title('5-Point - 4 Passes')

# Circular Smoother
ax[1, 2].imshow(mpcalc.smooth_circular(raw_data, 2, 2), vmin=0, vmax=1)
ax[1, 2].set_title('Circular - Radius 2\n2 Passes')

# 9-point smoother
ax[2, 0].imshow(mpcalc.smooth_n_point(raw_data, 9, 1), vmin=0, vmax=1)
ax[2, 0].set_title('9-Point - 1 Pass')

ax[2, 1].imshow(mpcalc.smooth_n_point(raw_data, 9, 4), vmin=0, vmax=1)
ax[2, 1].set_title('9-Point - 4 Passes')

# Arbitrary Window Smoother
ax[2, 2].imshow(mpcalc.smooth_window(raw_data, np.diag(np.ones(5)), 2), vmin=0, vmax=1)
ax[2, 2].set_title('Custom Window (Diagonal) \n2 Passes')

plt.show()
Loading