Skip to content

Commit 19d21b8

Browse files
author
Joe Hamman
committed
backward compat and add benchmarks
1 parent 263ec98 commit 19d21b8

File tree

3 files changed

+147
-2
lines changed

3 files changed

+147
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ install:
8989
- python xarray/util/print_versions.py
9090

9191
script:
92+
- git diff upstream/master **/*py | flake8 --diff --exit-zero || true
9293
- python -OO -c "import xarray"
9394
- py.test xarray --cov=xarray --cov-config ci/.coveragerc --cov-report term-missing --verbose $EXTRA_FLAGS
94-
- git diff upstream/master **/*py | flake8 --diff --exit-zero || true
9595

9696
after_success:
9797
- coveralls
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
5+
import pandas as pd
6+
7+
try:
8+
import dask
9+
import dask.multiprocessing
10+
except ImportError:
11+
pass
12+
13+
import xarray as xr
14+
15+
from . import randn, requires_dask
16+
17+
18+
def make_bench_data(shape, frac_nan, chunks):
19+
vals = randn(shape, frac_nan)
20+
coords = {'time': pd.date_range('2000-01-01', freq='D',
21+
periods=shape[0])}
22+
da = xr.DataArray(vals, dims=('time', 'x', 'y'), coords=coords)
23+
24+
if chunks is not None:
25+
da = da.chunk(chunks)
26+
27+
return da
28+
29+
30+
def time_interpolate_na(shape, chunks, method, limit):
31+
if chunks is not None:
32+
requires_dask()
33+
da = make_bench_data(shape, 0.1, chunks=chunks)
34+
actual = da.interpolate_na(dim='time', method='linear', limit=limit)
35+
36+
if chunks is not None:
37+
actual = actual.compute()
38+
39+
40+
time_interpolate_na.param_names = ['shape', 'chunks', 'method', 'limit']
41+
time_interpolate_na.params = ([(3650, 200, 400), (100, 25, 25)],
42+
[None, {'x': 25, 'y': 25}],
43+
['linear', 'spline', 'quadratic', 'cubic'],
44+
[None, 3])
45+
46+
47+
def time_ffill(shape, chunks, limit):
48+
49+
da = make_bench_data(shape, 0.1, chunks=chunks)
50+
actual = da.ffill(dim='time', limit=limit)
51+
52+
if chunks is not None:
53+
actual = actual.compute()
54+
55+
56+
time_ffill.param_names = ['shape', 'chunks', 'limit']
57+
time_ffill.params = ([(3650, 200, 400), (100, 25, 25)],
58+
[None, {'x': 25, 'y': 25}],
59+
[None, 3])
60+
61+
62+
def time_bfill(shape, chunks, limit):
63+
64+
da = make_bench_data(shape, 0.1, chunks=chunks)
65+
actual = da.bfill(dim='time', limit=limit)
66+
67+
if chunks is not None:
68+
actual = actual.compute()
69+
70+
71+
time_bfill.param_names = ['shape', 'chunks', 'limit']
72+
time_bfill.params = ([(3650, 200, 400), (100, 25, 25)],
73+
[None, {'x': 25, 'y': 25}],
74+
[None, 3])

xarray/core/npcompat.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
try:
7-
from numpy import nancumsum, nancumprod
7+
from numpy import nancumsum, nancumprod, flip
88
except ImportError: # pragma: no cover
99
# Code copied from newer versions of NumPy (v1.12).
1010
# Used under the terms of NumPy's license, see licenses/NUMPY_LICENSE.
@@ -174,3 +174,74 @@ def nancumprod(a, axis=None, dtype=None, out=None):
174174
"""
175175
a, mask = _replace_nan(a, 1)
176176
return np.cumprod(a, axis=axis, dtype=dtype, out=out)
177+
178+
def flip(m, axis):
179+
"""
180+
Reverse the order of elements in an array along the given axis.
181+
182+
The shape of the array is preserved, but the elements are reordered.
183+
184+
.. versionadded:: 1.12.0
185+
186+
Parameters
187+
----------
188+
m : array_like
189+
Input array.
190+
axis : integer
191+
Axis in array, which entries are reversed.
192+
193+
194+
Returns
195+
-------
196+
out : array_like
197+
A view of `m` with the entries of axis reversed. Since a view is
198+
returned, this operation is done in constant time.
199+
200+
See Also
201+
--------
202+
flipud : Flip an array vertically (axis=0).
203+
fliplr : Flip an array horizontally (axis=1).
204+
205+
Notes
206+
-----
207+
flip(m, 0) is equivalent to flipud(m).
208+
flip(m, 1) is equivalent to fliplr(m).
209+
flip(m, n) corresponds to ``m[...,::-1,...]`` with ``::-1`` at position n.
210+
211+
Examples
212+
--------
213+
>>> A = np.arange(8).reshape((2,2,2))
214+
>>> A
215+
array([[[0, 1],
216+
[2, 3]],
217+
218+
[[4, 5],
219+
[6, 7]]])
220+
221+
>>> flip(A, 0)
222+
array([[[4, 5],
223+
[6, 7]],
224+
225+
[[0, 1],
226+
[2, 3]]])
227+
228+
>>> flip(A, 1)
229+
array([[[2, 3],
230+
[0, 1]],
231+
232+
[[6, 7],
233+
[4, 5]]])
234+
235+
>>> A = np.random.randn(3,4,5)
236+
>>> np.all(flip(A,2) == A[:,:,::-1,...])
237+
True
238+
"""
239+
if not hasattr(m, 'ndim'):
240+
m = np.asarray(m)
241+
indexer = [slice(None)] * m.ndim
242+
try:
243+
indexer[axis] = slice(None, None, -1)
244+
except IndexError:
245+
raise ValueError("axis=%i is invalid for the %i-dimensional "
246+
"input array" % (axis, m.ndim))
247+
return m[tuple(indexer)]

0 commit comments

Comments
 (0)