From 181e82c2437ee06a4ed0bde38129d0cfab7157a0 Mon Sep 17 00:00:00 2001 From: Zeitsperre Date: Wed, 12 Dec 2018 12:54:13 -0500 Subject: [PATCH] PEP8 --- xarray/backends/zarr.py | 2 +- xarray/core/dataarray.py | 2 +- xarray/core/options.py | 3 +- xarray/core/resample.py | 2 +- xarray/plot/plot.py | 3 +- .../temp/cftime_resample_pandas_comparison.py | 33 ++++++------ xarray/tests/temp/cftime_resample_tests.py | 44 ++++++++-------- xarray/tests/test_backends.py | 37 +++++-------- xarray/tests/test_cftimeindex_resample.py | 52 ++++++++++--------- xarray/tests/test_dataarray.py | 2 +- xarray/tests/test_utils.py | 21 ++++---- 11 files changed, 93 insertions(+), 108 deletions(-) diff --git a/xarray/backends/zarr.py b/xarray/backends/zarr.py index 5f19c826289..93e150a1f21 100644 --- a/xarray/backends/zarr.py +++ b/xarray/backends/zarr.py @@ -8,7 +8,7 @@ from ..core import indexing from ..core.pycompat import OrderedDict, integer_types, iteritems from ..core.utils import FrozenOrderedDict, HiddenKeyDict -from .common import AbstractWritableDataStore, ArrayWriter, BackendArray +from .common import AbstractWritableDataStore, BackendArray # need some special secret attributes to tell us the dimensions _DIMENSION_KEY = '_ARRAY_DIMENSIONS' diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index bccfd8b79d4..7e7ffe70729 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -16,7 +16,7 @@ assert_coordinate_consistent, remap_label_indexers) from .dataset import Dataset, merge_indexes, split_indexes from .formatting import format_item -from .options import OPTIONS, _get_keep_attrs +from .options import OPTIONS from .pycompat import OrderedDict, basestring, iteritems, range, zip from .utils import ( decode_numpy_dict_values, either_dict_or_kwargs, ensure_us_time_resolution) diff --git a/xarray/core/options.py b/xarray/core/options.py index ab461ca86bc..76fd8d3594c 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -63,7 +63,8 @@ def _get_keep_attrs(default): elif global_choice in [True, False]: return global_choice else: - raise ValueError("The global option keep_attrs must be one of True, False or 'default'.") + raise ValueError("The global option keep_attrs must be one of" + " True, False or 'default'.") class set_options(object): diff --git a/xarray/core/resample.py b/xarray/core/resample.py index b7dfb5c5cd5..a4fa23d27b9 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -194,7 +194,7 @@ def _interpolate(self, kind='linear'): .format(self._obj.data.name) ) - from ..coding.cftimeindex import CFTimeIndex + # from ..coding.cftimeindex import CFTimeIndex import cftime as cf import numpy as np if isinstance(self._obj[self._dim].values[0], cf.datetime): diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 8d21e084946..b709786dc57 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -14,7 +14,7 @@ import numpy as np import pandas as pd -from xarray.core.alignment import align +# from xarray.core.alignment import align from xarray.core.common import contains_cftime_datetimes from xarray.core.pycompat import basestring @@ -255,7 +255,6 @@ def _infer_line_data(darray, x, y, hue): huelabel = label_from_attrs(darray[huename]) hueplt = darray[huename] - xlabel = label_from_attrs(xplt) ylabel = label_from_attrs(yplt) diff --git a/xarray/tests/temp/cftime_resample_pandas_comparison.py b/xarray/tests/temp/cftime_resample_pandas_comparison.py index a63b3a2ca54..5e4b8e5bb8c 100644 --- a/xarray/tests/temp/cftime_resample_pandas_comparison.py +++ b/xarray/tests/temp/cftime_resample_pandas_comparison.py @@ -1,11 +1,11 @@ -import xarray as xr -import pandas as pd import numpy as np +import pandas as pd +import xarray as xr # Equal sampling comparisons: ti = pd.date_range('2000-01-01', periods=9, freq='T', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('T', label='left', closed='left').mean()) print(ps.resample('T', label='right', closed='left').mean()) print(ps.resample('T', label='left', closed='right').mean()) @@ -15,7 +15,7 @@ print(ps.resample('60S', label='left', closed='right').mean()) print(ps.resample('60S', label='right', closed='right').mean()) ti = pd.date_range('2000', periods=30, freq='MS', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('M', label='left', closed='left').max()) print(ps.resample('M', label='right', closed='left').max()) print(ps.resample('M', label='left', closed='right').max()) @@ -25,10 +25,9 @@ print(ps.resample('MS', label='left', closed='right').max()) print(ps.resample('MS', label='right', closed='right').max()) - # Downsampling comparisons: ti = pd.date_range('2000-01-01', periods=9, freq='MS', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('3M', label='left', closed='left').max()) print(ps.resample('3M', label='right', closed='left').max()) print(ps.resample('3M', label='left', closed='right').max()) @@ -55,20 +54,20 @@ print(ps.resample('2MS', label='right', closed='right').mean()) # Checking how label and closed args affect outputs ti = pd.date_range('2000-01-01', periods=9, freq='T', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('3T', label='left', closed='left').mean()) print(ps.resample('3T', label='right', closed='left').mean()) print(ps.resample('3T', label='left', closed='right').mean()) print(ps.resample('3T', label='right', closed='right').mean()) ti = pd.date_range('2000', periods=30, freq='MS') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('6MS', label='left', closed='left').max()) print(ps.resample('6MS', label='right', closed='left').max()) print(ps.resample('6MS', label='left', closed='right').max()) print(ps.resample('6MS', label='right', closed='right').max()) # Checking different aggregation funcs, also checking cases when label and closed == None ti = pd.date_range('2000', periods=30, freq='MS') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('MS').mean()) # essentially doing no resampling, should return original data print(ps.resample('6MS').mean()) print(ps.resample('6MS').asfreq()) # results do not match since xarray makes asfreq = mean (see resample.py) @@ -76,12 +75,11 @@ print(ps.resample('6MS').min()) print(ps.resample('6MS').max()) - # Upsampling comparisons: # At seconds-resolution, xr.cftime_range is 1 second off from pd.date_range ti = pd.date_range('2011-01-01T13:02:03', '2012-01-01T00:00:00', freq='D', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) -print(ps.resample('12T', base=0).interpolate().index) # testing T vs min +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) +print(ps.resample('12T', base=0).interpolate().index) # testing T vs min print(ps.resample('12min', base=0).interpolate().index) print(ps.resample('12min', base=1).interpolate().index) print(ps.resample('12min', base=5).interpolate().index) @@ -91,7 +89,7 @@ print(ps.resample('1D', base=0).mean().values) # essentially doing no resampling, should return original data # Pandas' upsampling behave aberrantly if start times for dates are not neat, should we replicate? ti = pd.date_range('2000-01-01T13:02:03', '2000-02-01T00:00:00', freq='D', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) # results unchanged if array of floats used +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) # results unchanged if array of floats used print(ps.resample('8H', base=0, closed='left').interpolate().values) print(ps.resample('8H', base=0, closed='left').sum().values) print(ps.resample('8H', base=0, closed='left').mean().values) @@ -100,19 +98,18 @@ print(ps.resample('8H', base=0, closed='right').mean().values) # Neat start times (00:00:00) produces expected behavior when upsampling with pandas ti = pd.date_range('2000-01-01T00:00:00', '2000-02-01T00:00:00', freq='D', tz='UTC') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('12T', base=0).interpolate()) print(ps.resample('12T', base=24, closed='left').interpolate()) print(ps.resample('12T', base=24, closed='right', label='left').interpolate()) ti = pd.date_range('2000', periods=30, freq='MS') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('D').interpolate()) - # Shows how resample-apply produces different results with Series and DataArray ti = pd.date_range('2000', periods=30, freq='MS') -da = xr.DataArray(np.arange(100, 100+ti.size), [('time', ti)]) +da = xr.DataArray(np.arange(100, 100 + ti.size), [('time', ti)]) print(da.resample(time='6MS').sum()) ti = pd.date_range('2000', periods=30, freq='MS') -ps = pd.Series(np.arange(100, 100+ti.size), index=ti) +ps = pd.Series(np.arange(100, 100 + ti.size), index=ti) print(ps.resample('6MS').sum()) diff --git a/xarray/tests/temp/cftime_resample_tests.py b/xarray/tests/temp/cftime_resample_tests.py index 6357365dada..285ae1213a9 100644 --- a/xarray/tests/temp/cftime_resample_tests.py +++ b/xarray/tests/temp/cftime_resample_tests.py @@ -1,10 +1,10 @@ -import xarray as xr import numpy as np +import xarray as xr # Equal sampling comparisons: times = xr.cftime_range('2000-01-01', periods=9, freq='T', tz='UTC') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='T', label='left', closed='left').mean()) print(da.resample(time='T', label='right', closed='left').mean()) print(da.resample(time='T', label='left', closed='right').mean()) @@ -14,7 +14,7 @@ print(da.resample(time='60S', label='left', closed='right').mean()) print(da.resample(time='60S', label='right', closed='right').mean()) times = xr.cftime_range('2000', periods=30, freq='MS') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='M', label='left', closed='left').max()) print(da.resample(time='M', label='right', closed='left').max()) print(da.resample(time='M', label='left', closed='right').max()) @@ -24,10 +24,9 @@ print(da.resample(time='MS', label='left', closed='right').max()) print(da.resample(time='MS', label='right', closed='right').max()) - # Downsampling comparisons: times = xr.cftime_range('2000-01-01', periods=9, freq='MS', tz='UTC') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='3M', label='left', closed='left').max()) print(da.resample(time='3M', label='right', closed='left').max()) print(da.resample(time='3M', label='left', closed='right').max()) @@ -54,20 +53,20 @@ print(da.resample(time='2MS', label='right', closed='right').mean()) # Checking how label and closed args affect outputs times = xr.cftime_range('2000-01-01', periods=9, freq='T', tz='UTC') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='3T', label='left', closed='left').mean()) print(da.resample(time='3T', label='right', closed='left').mean()) print(da.resample(time='3T', label='left', closed='right').mean()) print(da.resample(time='3T', label='right', closed='right').mean()) times = xr.cftime_range('2000', periods=30, freq='MS') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='6MS', label='left', closed='left').max()) print(da.resample(time='6MS', label='right', closed='left').max()) print(da.resample(time='6MS', label='left', closed='right').max()) print(da.resample(time='6MS', label='right', closed='right').max()) # Checking different aggregation funcs, also checking cases when label and closed == None times = xr.cftime_range('2000', periods=30, freq='MS') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='MS').mean()) # essentially doing no resampling, should return original data print(da.resample(time='6MS').mean()) print(da.resample(time='6MS').asfreq()) # results do not match since xarray makes asfreq = mean (see resample.py) @@ -75,22 +74,22 @@ print(da.resample(time='6MS').min()) print(da.resample(time='6MS').max()) - # Upsampling comparisons: # At seconds-resolution, xr.cftime_range is 1 second off from pd.date_range times = xr.cftime_range('2011-01-01T13:02:03', '2012-01-01T00:00:00', freq='D') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='12T', base=0).interpolate().indexes) # testing 'T' vs 'min' print(da.resample(time='12min', base=0).interpolate().indexes) print(da.resample(time='12min', base=1).interpolate().indexes) print(da.resample(time='12min', base=5).mean().indexes) print(da.resample(time='12min', base=17).mean().indexes) print(da.resample(time='12S', base=17).interpolate().indexes) -print(da.resample(time='1D', base=0).interpolate().values) # essentially doing no resampling, should return original data +print( + da.resample(time='1D', base=0).interpolate().values) # essentially doing no resampling, should return original data print(da.resample(time='1D', base=0).mean().values) # essentially doing no resampling, should return original data # Upsampling with non 00:00:00 dates. Sum and mean matches pandas behavior but interpolate doesn't. times = xr.cftime_range('2000-01-01T13:02:03', '2000-02-01T00:00:00', freq='D') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='8H', base=0, closed='left').interpolate().values) print(da.resample(time='8H', base=0, closed='left').sum().values) print(da.resample(time='8H', base=0, closed='left').mean().values) @@ -99,41 +98,42 @@ print(da.resample(time='8H', base=0, closed='right').mean().values) # Neat start times (00:00:00) produces behavior matching pandas' times = xr.cftime_range('2000-01-01T00:00:00', '2000-02-01T00:00:00', freq='D') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='12T', base=0).interpolate()) print(da.resample(time='12T', base=24, closed='left').interpolate()) print(da.resample(time='12T', base=24, closed='right', label='left').interpolate()) times = xr.cftime_range('2000', periods=30, freq='MS') -da = xr.DataArray(np.arange(100, 100+times.size), [('time', times)]) +da = xr.DataArray(np.arange(100, 100 + times.size), [('time', times)]) print(da.resample(time='D').interpolate()) - # Check that Dataset and DataArray returns the same resampling results times = xr.cftime_range('2000-01-01', periods=9, freq='T', tz='UTC') -ds = xr.Dataset(data_vars={'data1': ('time', np.arange(100, 100+times.size)), - 'data2': ('time', np.arange(500, 500+times.size))}, +ds = xr.Dataset(data_vars={'data1': ('time', np.arange(100, 100 + times.size)), + 'data2': ('time', np.arange(500, 500 + times.size))}, coords={'time': times}) print(ds.resample(time='3T', label='left', closed='left').mean()) print(ds.resample(time='3T', label='right', closed='left').mean()) print(ds.resample(time='3T', label='left', closed='right').mean()) print(ds.resample(time='3T', label='right', closed='right').mean()) times = xr.cftime_range('2000', periods=30, freq='MS') -ds = xr.Dataset(data_vars={'data1': ('time', np.arange(100, 100+times.size)), - 'data2': ('time', np.arange(500, 500+times.size))}, +ds = xr.Dataset(data_vars={'data1': ('time', np.arange(100, 100 + times.size)), + 'data2': ('time', np.arange(500, 500 + times.size))}, coords={'time': times}) print(ds.resample(time='6MS', label='left', closed='left').max()) print(ds.resample(time='6MS', label='right', closed='left').max()) print(ds.resample(time='6MS', label='left', closed='right').max()) print(ds.resample(time='6MS', label='right', closed='right').max()) - # Check that nc files read as dask arrays can be resampled # import os -testfilepath = os.path.join(os.path.expanduser('~'), 'Dropbox', 'code', 'Ouranos', 'testdata', 'NRCANdaily', 'nrcan_canada_daily_tasmax_1990.nc') + +testfilepath = os.path.join(os.path.expanduser('~'), 'Dropbox', 'code', 'Ouranos', 'testdata', 'NRCANdaily', + 'nrcan_canada_daily_tasmax_1990.nc') xr.set_options(enable_cftimeindex=True) test_ds = xr.open_dataset(testfilepath, chunks={'time': 10}) -test_ds['time'] = xr.cftime_range('1999-01-01', '1999-12-31', freq='D') # regular calendars are still read as pandas date_range even though enable_cftimeindex=True +test_ds['time'] = xr.cftime_range('1999-01-01', '1999-12-31', + freq='D') # regular calendars are still read as pandas date_range even though enable_cftimeindex=True # test_ds.fillna(0).resample(time='3MS') # NaN in results still present print(test_ds.resample(time='MS').mean()) monthly_avg = test_ds.resample(time='MS').mean() diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 80d3a9d526e..f78f143ab34 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -23,11 +23,10 @@ from xarray.backends.netCDF4_ import _extract_nc4_variable_encoding from xarray.backends.pydap_ import PydapDataStore from xarray.core import indexing +from xarray.core.options import set_options from xarray.core.pycompat import ( ExitStack, basestring, dask_array_type, iteritems) -from xarray.core.options import set_options from xarray.tests import mock - from . import ( assert_allclose, assert_array_equal, assert_equal, assert_identical, has_dask, has_netCDF4, has_scipy, network, raises_regex, requires_cftime, @@ -54,7 +53,6 @@ except ImportError: pass - ON_WINDOWS = sys.platform == 'win32' @@ -107,7 +105,6 @@ def create_boolean_data(): class TestCommon(object): def test_robust_getitem(self): - class UnreliableArrayFailure(Exception): pass @@ -644,13 +641,13 @@ def test_unsigned_roundtrip_mask_and_scale(self): with self.roundtrip(encoded) as actual: for k in decoded.variables: assert decoded.variables[k].dtype == \ - actual.variables[k].dtype + actual.variables[k].dtype assert_allclose(decoded, actual, decode_bytes=False) with self.roundtrip(encoded, open_kwargs=dict(decode_cf=False)) as actual: for k in encoded.variables: assert encoded.variables[k].dtype == \ - actual.variables[k].dtype + actual.variables[k].dtype assert_allclose(encoded, actual, decode_bytes=False) def test_roundtrip_mask_and_scale(self): @@ -1035,7 +1032,7 @@ def test_open_encodings(self): iteritems(actual['time'].encoding) if k in expected['time'].encoding) assert actual_encoding == \ - expected['time'].encoding + expected['time'].encoding def test_dump_encodings(self): # regression test for #709 @@ -1291,7 +1288,6 @@ def test_write_inconsistent_chunks(self): @requires_zarr class ZarrBase(CFEncodedBase): - DIMENSION_KEY = '_ARRAY_DIMENSIONS' @contextlib.contextmanager @@ -1391,7 +1387,7 @@ def test_chunk_encoding_with_dask(self): # - encoding specified - # specify compatible encodings - for chunk_enc in 4, (4, ): + for chunk_enc in 4, (4,): ds_chunk4['var1'].encoding.update({'chunks': chunk_enc}) with self.roundtrip(ds_chunk4) as actual: assert (4,) == actual['var1'].encoding['chunks'] @@ -1922,7 +1918,6 @@ def skip_if_not_engine(engine): def test_open_mfdataset_manyfiles(readengine, nfiles, parallel, chunks, file_cache_maxsize): - # skip certain combinations skip_if_not_engine(readengine) @@ -1962,7 +1957,6 @@ def setup_files_and_datasets(self): ds1, ds2 = self.gen_datasets_with_common_coord_and_time() with create_tmp_file() as tmpfile1: with create_tmp_file() as tmpfile2: - # save data to the temporary files ds1.to_netcdf(tmpfile1) ds2.to_netcdf(tmpfile2) @@ -1985,14 +1979,14 @@ def gen_datasets_with_common_coord_and_time(self): coords={ 't': (['t', ], t1), 'x': (['x', ], x) - }) + }) ds2 = Dataset(data_vars={self.var_name: (['t', 'x'], v2), self.coord_name: ('x', 2 * x)}, coords={ 't': (['t', ], t2), 'x': (['x', ], x) - }) + }) return ds1, ds2 @@ -2017,7 +2011,6 @@ def test_common_coord_when_datavars_all(self): with self.setup_files_and_datasets() as (files, [ds1, ds2]): # open the files with the data_var option with open_mfdataset(files, data_vars=opt) as ds: - coord_shape = ds[self.coord_name].shape coord_shape1 = ds1[self.coord_name].shape coord_shape2 = ds2[self.coord_name].shape @@ -2034,7 +2027,6 @@ def test_common_coord_when_datavars_minimal(self): with self.setup_files_and_datasets() as (files, [ds1, ds2]): # open the files using data_vars option with open_mfdataset(files, data_vars=opt) as ds: - coord_shape = ds[self.coord_name].shape coord_shape1 = ds1[self.coord_name].shape coord_shape2 = ds2[self.coord_name].shape @@ -2046,7 +2038,6 @@ def test_common_coord_when_datavars_minimal(self): assert coord_shape2 == coord_shape def test_invalid_data_vars_value_should_fail(self): - with self.setup_files_and_datasets() as (files, _): with pytest.raises(ValueError): with open_mfdataset(files, data_vars='minimum'): @@ -2125,11 +2116,11 @@ def test_open_mfdataset(self): with open_mfdataset([tmp1, tmp2]) as actual: assert isinstance(actual.foo.variable.data, da.Array) assert actual.foo.variable.data.chunks == \ - ((5, 5),) + ((5, 5),) assert_identical(original, actual) with open_mfdataset([tmp1, tmp2], chunks={'x': 3}) as actual: assert actual.foo.variable.data.chunks == \ - ((3, 2, 3, 2),) + ((3, 2, 3, 2),) with raises_regex(IOError, 'no files to open'): open_mfdataset('foo-bar-baz-*.nc') @@ -2328,7 +2319,7 @@ def convert_to_pydap_dataset(self, original): ds[key] = v # check all dims are stored in ds for d in original.coords: - ds[d] = BaseType(d, original[d].values, dimensions=(d, ), + ds[d] = BaseType(d, original[d].values, dimensions=(d,), **original[d].attrs) return ds @@ -2715,7 +2706,7 @@ def create_tmp_geotiff(nx=4, ny=3, nz=3, 'band': np.arange(nz) + 1, 'y': -np.arange(ny) * d + b + dy / 2, 'x': np.arange(nx) * c + a + dx / 2, - }) + }) yield tmp_file, expected @@ -2827,7 +2818,6 @@ def test_indexing(self): with create_tmp_geotiff(8, 10, 3, transform_args=[1, 2, 0.5, 2.], crs='+proj=latlong') as (tmp_file, expected): with xr.open_rasterio(tmp_file, cache=False) as actual: - # tests # assert_allclose checks all data + coordinates assert_allclose(actual, expected) @@ -2857,7 +2847,8 @@ def test_indexing(self): assert_allclose(expected.isel(**ind), actual.isel(**ind)) assert not actual.variable._in_memory - ind = {'band': 0, 'x': np.array([0, 0]), 'y': np.array([1, 1, 1])} + ind = {'band': 0, 'x': np.array([0, 0]), + 'y': np.array([1, 1, 1])} assert_allclose(expected.isel(**ind), actual.isel(**ind)) assert not actual.variable._in_memory @@ -2940,7 +2931,6 @@ def test_caching(self): crs='+proj=latlong') as (tmp_file, expected): # Cache is the default with xr.open_rasterio(tmp_file) as actual: - # This should cache everything assert_allclose(actual, expected) @@ -2955,7 +2945,6 @@ def test_chunks(self): crs='+proj=latlong') as (tmp_file, expected): # Chunk at open time with xr.open_rasterio(tmp_file, chunks=(1, 2, 2)) as actual: - import dask.array as da assert isinstance(actual.data, da.Array) assert 'open_rasterio' in actual.data.name diff --git a/xarray/tests/test_cftimeindex_resample.py b/xarray/tests/test_cftimeindex_resample.py index ea034ba248f..b8e2167d861 100644 --- a/xarray/tests/test_cftimeindex_resample.py +++ b/xarray/tests/test_cftimeindex_resample.py @@ -1,15 +1,17 @@ from __future__ import absolute_import -import itertools -import pytest import datetime as dt +import itertools import numpy as np import pandas as pd +import pytest + import xarray as xr -from xarray.tests import assert_array_equal, assert_identical +# from xarray.tests import assert_array_equal, assert_identical + @pytest.fixture() def pd_index(): return pd.date_range('2000-01-01', periods=30, freq='MS', tz='UTC') @@ -41,19 +43,19 @@ def base_xr_index(): def da(index): - return xr.DataArray(np.arange(100., 100.+index.size), + return xr.DataArray(np.arange(100., 100. + index.size), coords=[index], dims=['time']) def series(index): - return pd.Series(np.arange(100., 100.+index.size), index=index) + return pd.Series(np.arange(100., 100. + index.size), index=index) -@ pytest.mark.parametrize(('closed', 'label', 'freq'), - list(itertools.product( - ['left', 'right'], - ['left', 'right'], - ['2MS', '2M', '3MS', '3M', '7MS', '7M']))) +@pytest.mark.parametrize(('closed', 'label', 'freq'), + list(itertools.product( + ['left', 'right'], + ['left', 'right'], + ['2MS', '2M', '3MS', '3M', '7MS', '7M']))) def test_downsampler(closed, label, freq): downsamp_series = series(pd_index()).resample( freq, closed=closed, label=label).mean().dropna() @@ -65,11 +67,11 @@ def test_downsampler(closed, label, freq): timestamp in downsamp_da.indexes['time']])) -@ pytest.mark.parametrize(('closed', 'label', 'freq'), - list(itertools.product( - ['left', 'right'], - ['left', 'right'], - ['2MS', '2M', '3MS', '3M', 'AS', 'A', '2AS', '2A']))) +@pytest.mark.parametrize(('closed', 'label', 'freq'), + list(itertools.product( + ['left', 'right'], + ['left', 'right'], + ['2MS', '2M', '3MS', '3M', 'AS', 'A', '2AS', '2A']))) def test_downsampler_daily(closed, label, freq): downsamp_series = series(daily_pd_index()).resample( freq, closed=closed, label=label).mean().dropna() @@ -81,11 +83,11 @@ def test_downsampler_daily(closed, label, freq): timestamp in downsamp_da.indexes['time']])) -@ pytest.mark.parametrize(('closed', 'label', 'freq'), - list(itertools.product( - ['left', 'right'], - ['left', 'right'], - ['MS', 'M', '7D', 'D']))) +@pytest.mark.parametrize(('closed', 'label', 'freq'), + list(itertools.product( + ['left', 'right'], + ['left', 'right'], + ['MS', 'M', '7D', 'D']))) def test_upsampler(closed, label, freq): # the testing here covers cases of equal sampling as well # for pandas --not xarray--, .ffill() and .bfill() gives @@ -105,11 +107,11 @@ def test_upsampler(closed, label, freq): timestamp in upsamp_da.indexes['time']])) -@ pytest.mark.parametrize(('closed', 'label', 'base'), - list(itertools.product( - ['left', 'right'], - ['left', 'right'], - [1, 5, 12, 17, 24]))) +@pytest.mark.parametrize(('closed', 'label', 'base'), + list(itertools.product( + ['left', 'right'], + ['left', 'right'], + [1, 5, 12, 17, 24]))) def test_upsampler_base(closed, label, base, freq='12H'): upsamp_series = series(base_pd_index()).resample( freq, closed=closed, label=label, base=base).mean().dropna() diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index abdbdbdbfdf..8386eff29dc 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -11,7 +11,7 @@ import xarray as xr from xarray import ( - DataArray, Dataset, IndexVariable, Variable, align, broadcast, set_options) + DataArray, Dataset, IndexVariable, Variable, align, broadcast) from xarray.coding.times import CFDatetimeCoder, _import_cftime from xarray.convert import from_cdms2 from xarray.core.common import ALL_DIMS, full_like diff --git a/xarray/tests/test_utils.py b/xarray/tests/test_utils.py index ed07af0d7bb..6d4b99533bb 100644 --- a/xarray/tests/test_utils.py +++ b/xarray/tests/test_utils.py @@ -9,11 +9,10 @@ import xarray as xr from xarray.coding.cftimeindex import CFTimeIndex from xarray.core import duck_array_ops, utils -from xarray.core.options import set_options +# from xarray.core.options import set_options from xarray.core.pycompat import OrderedDict from xarray.core.utils import either_dict_or_kwargs from xarray.testing import assert_identical - from . import ( assert_array_equal, has_cftime, has_cftime_or_netCDF4, requires_cftime, requires_dask) @@ -24,6 +23,7 @@ class TestAlias(object): def test(self): def new_method(): pass + old_method = utils.alias(new_method, 'old_method') assert 'deprecated' in old_method.__doc__ with pytest.warns(Warning, match='deprecated'): @@ -35,10 +35,10 @@ def test_safe_cast_to_index(): x = np.arange(5) td = x * np.timedelta64(1, 'D') for expected, array in [ - (dates, dates.values), - (pd.Index(x, dtype=object), x.astype(object)), - (pd.Index(td), td), - (pd.Index(td, dtype=object), td.astype(object)), + (dates, dates.values), + (pd.Index(x, dtype=object), x.astype(object)), + (pd.Index(td), td), + (pd.Index(td, dtype=object), td.astype(object)), ]: actual = utils.safe_cast_to_index(array) assert_array_equal(expected, actual) @@ -129,8 +129,7 @@ def test_unsafe(self): utils.update_safety_check(self.x, self.z) def test_ordered_dict_intersection(self): - assert {'b': 'B'} == \ - utils.ordered_dict_intersection(self.x, self.y) + assert {'b': 'B'} == utils.ordered_dict_intersection(self.x, self.y) assert {} == utils.ordered_dict_intersection(self.x, self.z) def test_dict_equiv(self): @@ -174,8 +173,7 @@ def test_sorted_keys_dict(self): x = {'a': 1, 'b': 2, 'c': 3} y = utils.SortedKeysDict(x) assert list(y) == ['a', 'b', 'c'] - assert repr(utils.SortedKeysDict()) == \ - "SortedKeysDict({})" + assert repr(utils.SortedKeysDict()) == "SortedKeysDict({})" def test_chain_map(self): m = utils.ChainMap({'x': 0, 'y': 1}, {'x': -100, 'z': 2}) @@ -236,7 +234,7 @@ def test_relative_tolerance(self): class Test_hashable(object): def test_hashable(self): - for v in [False, 1, (2, ), (3, 4), 'four']: + for v in [False, 1, (2,), (3, 4), 'four']: assert utils.hashable(v) for v in [[5, 6], ['seven', '8'], {9: 'ten'}]: assert not utils.hashable(v) @@ -267,7 +265,6 @@ def test_hidden_key_dict(): def test_either_dict_or_kwargs(): - result = either_dict_or_kwargs(dict(a=1), None, 'foo') expected = dict(a=1) assert result == expected