Skip to content

Resolve more warnings in the xarray test suite #1964

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

Merged
merged 5 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Bug fixes
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Fix the precision drop after indexing datetime64 arrays (:issue:`1932`).
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- Silenced irrelevant warnings issued by ``open_rasterio`` (:issue:`1964`).
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Fix kwarg `colors` clashing with auto-inferred `cmap` (:issue:`1461`)
By `Deepak Cherian <https://github.com/dcherian>`_.

Expand Down
13 changes: 8 additions & 5 deletions xarray/backends/rasterio_.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import warnings
from collections import OrderedDict
from distutils.version import LooseVersion
import warnings

import numpy as np

Expand Down Expand Up @@ -250,10 +250,13 @@ def open_rasterio(filename, parse_coordinates=None, chunks=None, cache=None,
# Is the TIF tiled? (bool)
# We cast it to an int for netCDF compatibility
attrs['is_tiled'] = np.uint8(riods.is_tiled)
if hasattr(riods, 'transform'):
# Affine transformation matrix (tuple of floats)
# Describes coefficients mapping pixel coordinates to CRS
attrs['transform'] = tuple(riods.transform)
with warnings.catch_warnings():
# casting riods.transform to a tuple makes this future proof
warnings.simplefilter('ignore', FutureWarning)
if hasattr(riods, 'transform'):
# Affine transformation matrix (tuple of floats)
# Describes coefficients mapping pixel coordinates to CRS
attrs['transform'] = tuple(riods.transform)
if hasattr(riods, 'nodatavals'):
# The nodata values for the raster bands
attrs['nodatavals'] = tuple([np.nan if nodataval is None else nodataval
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,7 @@ def test_groupby_math(self):
actual = array.coords['x'] + grouped
assert_identical(expected, actual)

ds = array.coords['x'].to_dataset('X')
ds = array.coords['x'].to_dataset(name='X')
expected = array + ds
actual = grouped + ds
assert_identical(expected, actual)
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4163,7 +4163,7 @@ def test_rolling_construct(center, window):
# with fill_value
ds_rolling_mean = ds_rolling.construct(
'window', stride=2, fill_value=0.0).mean('window')
assert ds_rolling_mean.isnull().sum() == 0
assert (ds_rolling_mean.isnull().sum() == 0).to_array(dim='vars').all()
assert (ds_rolling_mean['x'] == 0.0).sum() >= 0


Expand Down
123 changes: 68 additions & 55 deletions xarray/tests/test_duck_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pytest
from numpy import array, nan
import warnings

from xarray import DataArray, concat
from xarray.core.duck_array_ops import (
Expand Down Expand Up @@ -208,50 +209,58 @@ def test_reduce(dim_num, dtype, dask, func, skipna, aggdim):
da = construct_dataarray(dim_num, dtype, contains_nan=True, dask=dask)
axis = None if aggdim is None else da.get_axis_num(aggdim)

if (LooseVersion(np.__version__) >= LooseVersion('1.13.0') and
da.dtype.kind == 'O' and skipna):
# Numpy < 1.13 does not handle object-type array.
try:
if skipna:
expected = getattr(np, 'nan{}'.format(func))(da.values,
axis=axis)
else:
expected = getattr(np, func)(da.values, axis=axis)

actual = getattr(da, func)(skipna=skipna, dim=aggdim)
assert np.allclose(actual.values, np.array(expected), rtol=1.0e-4,
equal_nan=True)
except (TypeError, AttributeError, ZeroDivisionError):
# TODO currently, numpy does not support some methods such as
# nanmean for object dtype
pass

# make sure the compatiblility with pandas' results.
actual = getattr(da, func)(skipna=skipna, dim=aggdim)
if func == 'var':
expected = series_reduce(da, func, skipna=skipna, dim=aggdim, ddof=0)
assert_allclose(actual, expected, rtol=rtol)
# also check ddof!=0 case
actual = getattr(da, func)(skipna=skipna, dim=aggdim, ddof=5)
expected = series_reduce(da, func, skipna=skipna, dim=aggdim, ddof=5)
assert_allclose(actual, expected, rtol=rtol)
else:
expected = series_reduce(da, func, skipna=skipna, dim=aggdim)
assert_allclose(actual, expected, rtol=rtol)

# make sure the dtype argument
if func not in ['max', 'min']:
actual = getattr(da, func)(skipna=skipna, dim=aggdim, dtype=float)
assert actual.dtype == float

# without nan
da = construct_dataarray(dim_num, dtype, contains_nan=False, dask=dask)
actual = getattr(da, func)(skipna=skipna)
expected = getattr(np, 'nan{}'.format(func))(da.values)
if actual.dtype == object:
assert actual.values == np.array(expected)
else:
assert np.allclose(actual.values, np.array(expected), rtol=rtol)
# TODO: remove these after resolving
# https://github.com/dask/dask/issues/3245
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'All-NaN slice')
warnings.filterwarnings('ignore', 'invalid value encountered in')

if (LooseVersion(np.__version__) >= LooseVersion('1.13.0') and
da.dtype.kind == 'O' and skipna):
# Numpy < 1.13 does not handle object-type array.
try:
if skipna:
expected = getattr(np, 'nan{}'.format(func))(da.values,
axis=axis)
else:
expected = getattr(np, func)(da.values, axis=axis)

actual = getattr(da, func)(skipna=skipna, dim=aggdim)
assert np.allclose(actual.values, np.array(expected),
rtol=1.0e-4, equal_nan=True)
except (TypeError, AttributeError, ZeroDivisionError):
# TODO currently, numpy does not support some methods such as
# nanmean for object dtype
pass

# make sure the compatiblility with pandas' results.
actual = getattr(da, func)(skipna=skipna, dim=aggdim)
if func == 'var':
expected = series_reduce(da, func, skipna=skipna, dim=aggdim,
ddof=0)
assert_allclose(actual, expected, rtol=rtol)
# also check ddof!=0 case
actual = getattr(da, func)(skipna=skipna, dim=aggdim, ddof=5)
expected = series_reduce(da, func, skipna=skipna, dim=aggdim,
ddof=5)
assert_allclose(actual, expected, rtol=rtol)
else:
expected = series_reduce(da, func, skipna=skipna, dim=aggdim)
assert_allclose(actual, expected, rtol=rtol)

# make sure the dtype argument
if func not in ['max', 'min']:
actual = getattr(da, func)(skipna=skipna, dim=aggdim, dtype=float)
assert actual.dtype == float

# without nan
da = construct_dataarray(dim_num, dtype, contains_nan=False, dask=dask)
actual = getattr(da, func)(skipna=skipna)
expected = getattr(np, 'nan{}'.format(func))(da.values)
if actual.dtype == object:
assert actual.values == np.array(expected)
else:
assert np.allclose(actual.values, np.array(expected), rtol=rtol)


@pytest.mark.parametrize('dim_num', [1, 2])
Expand Down Expand Up @@ -280,17 +289,21 @@ def test_argmin_max(dim_num, dtype, contains_nan, dask, func, skipna, aggdim):
da = construct_dataarray(dim_num, dtype, contains_nan=contains_nan,
dask=dask)

if aggdim == 'y' and contains_nan and skipna:
with pytest.raises(ValueError):
actual = da.isel(**{
aggdim: getattr(da, 'arg' + func)(dim=aggdim,
skipna=skipna).compute()})
return

actual = da.isel(**{aggdim: getattr(da, 'arg' + func)
(dim=aggdim, skipna=skipna).compute()})
expected = getattr(da, func)(dim=aggdim, skipna=skipna)
assert_allclose(actual.drop(actual.coords), expected.drop(expected.coords))
with warnings.catch_warnings():
warnings.filterwarnings('ignore', 'All-NaN slice')

if aggdim == 'y' and contains_nan and skipna:
with pytest.raises(ValueError):
actual = da.isel(**{
aggdim: getattr(da, 'arg' + func)(
dim=aggdim, skipna=skipna).compute()})
return

actual = da.isel(**{aggdim: getattr(da, 'arg' + func)
(dim=aggdim, skipna=skipna).compute()})
expected = getattr(da, func)(dim=aggdim, skipna=skipna)
assert_allclose(actual.drop(actual.coords),
expected.drop(expected.coords))


def test_argmin_max_error():
Expand Down