Skip to content

Commit

Permalink
Merge pull request #849 from dopplershift/masked-gradient
Browse files Browse the repository at this point in the history
Fix mask propagation in gradient
  • Loading branch information
jrleeman authored May 17, 2018
2 parents 6aea8d4 + 66737b8 commit 7eb18c2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
sudo apt-get install libgeos-dev libproj-dev
python3 -m venv venv
. venv/bin/activate
pip install ".[test,cdm]" "shapely<1.5.17.post1" -f https://unidata-python.s3.amazonaws.com/wheelhouse/index.html
pip install --upgrade pytest
pip install ".[test,cdm]" "shapely<1.5.17.post1" --upgrade --upgrade-strategy=eager -f https://unidata-python.s3.amazonaws.com/wheelhouse/index.html
- save_cache:
paths:
Expand All @@ -43,4 +44,3 @@ jobs:
- store_artifacts:
path: test-reports
destination: test-reports

2 changes: 2 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Brian Mapes <[email protected]>
Kristen Pozsonyi <[email protected]>
Daryl Herzmann <[email protected]>
Kishan Mehta <[email protected]>
Matt Wilson <[email protected]>
Andrew Huang <[email protected]>
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ before_install:
- touch $WHEELDIR/download_marker && ls -lrt $WHEELDIR;
- travis_wait pip wheel -w $WHEELDIR $EXTRA_PACKAGES -f $WHEELHOUSE $PRE $VERSIONS;
- pip install $EXTRA_PACKAGES --upgrade --upgrade-strategy=eager --no-index -f file://$PWD/$WHEELDIR $VERSIONS;
- travis_wait pip wheel -w $WHEELDIR ".[$EXTRA_INSTALLS]" $EXTRA_PACKAGES -f $WHEELHOUSE $PRE $VERSIONS;
- travis_wait 30 pip wheel -w $WHEELDIR ".[$EXTRA_INSTALLS]" $EXTRA_PACKAGES -f $WHEELHOUSE $PRE $VERSIONS;
- rm -f $WHEELDIR/MetPy*.whl;

install:
Expand Down
4 changes: 4 additions & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ UCAR/Unidata
Aaron Hill
Abby Kenyon
Alex Haberlie
Andrew Huang
Assela Pathirana
Brian Mapes
Bryan Guarente
Claude Dicaire
Dan Dawson
Daryl Herzmann
Denis Sergeev
Eric Bruning
Expand Down
12 changes: 12 additions & 0 deletions metpy/calc/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,18 @@ def test_first_derivative_scalar_delta():
assert_array_almost_equal(df_dx, np.array([1., 1., 1.]), 6)


def test_first_derivative_masked():
"""Test that first_derivative properly propagates masks."""
data = np.ma.arange(7)
data[3] = np.ma.masked
df_dx = first_derivative(data, delta=1)

truth = np.ma.array([1., 1., 1., 1., 1., 1., 1.],
mask=[False, False, True, True, True, False, False])
assert_array_almost_equal(df_dx, truth)
assert_array_equal(df_dx.mask, truth.mask)


def test_second_derivative(deriv_1d_data):
"""Test second_derivative with a simple 1D array."""
d2v_dx2 = second_derivative(deriv_1d_data.values, x=deriv_1d_data.x)
Expand Down
24 changes: 22 additions & 2 deletions metpy/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,34 @@
import pandas as pd
import pytest

from metpy.testing import assert_array_equal, set_agg_backend # noqa: F401
from metpy.units import (atleast_1d, atleast_2d, check_units, diff,
from metpy.testing import assert_array_almost_equal, assert_array_equal
from metpy.testing import set_agg_backend # noqa: F401
from metpy.units import (atleast_1d, atleast_2d, check_units, concatenate, diff,
pandas_dataframe_to_unit_arrays, units)


warnings.filterwarnings('ignore', 'Pandas doesn\'t allow columns to be created', UserWarning)


def test_concatenate():
"""Test basic functionality of unit-aware concatenate."""
result = concatenate((3 * units.meter, 400 * units.cm))
assert_array_equal(result, np.array([3, 4]) * units.meter)
assert not isinstance(result.m, np.ma.MaskedArray)


def test_concatenate_masked():
"""Test concatenate preserves masks."""
d1 = units.Quantity(np.ma.array([1, 2, 3], mask=[False, True, False]), 'degC')
result = concatenate((d1, 32 * units.degF))

truth = np.ma.array([1, np.inf, 3, 0])
truth[1] = np.ma.masked

assert_array_almost_equal(result, units.Quantity(truth, 'degC'), 6)
assert_array_equal(result.mask, np.array([False, True, False, False]))


@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
def test_axhline():
r"""Ensure that passing a quantity to axhline does not error."""
Expand Down
8 changes: 7 additions & 1 deletion metpy/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ def concatenate(arrs, axis=0):
a = a.to(dest).magnitude
data.append(np.atleast_1d(a))

return units.Quantity(np.concatenate(data, axis=axis), dest)
# Use masked array concatenate to ensure masks are preserved, but convert to an
# array if there are no masked values.
data = np.ma.concatenate(data, axis=axis)
if not np.any(data.mask):
data = np.asarray(data)

return units.Quantity(data, dest)


def diff(x, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
'netCDF4'],
'examples': ['cartopy>=0.13.1'],
'test': ['pytest>=2.4', 'pytest-runner', 'pytest-mpl', 'pytest-flake8',
'cartopy>=0.13.1', 'flake8>3.2.0', 'flake8-builtins!=1.4.0',
'cartopy>=0.16.0', 'flake8>3.2.0', 'flake8-builtins!=1.4.0',
'flake8-comprehensions', 'flake8-copyright',
'flake8-docstrings', 'flake8-import-order', 'flake8-mutable',
'flake8-pep3101', 'flake8-print', 'flake8-quotes',
Expand Down

0 comments on commit 7eb18c2

Please sign in to comment.