Skip to content

Fix Dataset.argmin #210

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 1 commit into from
Aug 9, 2014
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
34 changes: 32 additions & 2 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
backends, utils, align, indexing)
from xray.pycompat import iteritems, OrderedDict

from . import TestCase
from . import TestCase, unittest


_dims = {'dim1': 100, 'dim2': 50, 'dim3': 10}
Expand Down Expand Up @@ -792,7 +792,8 @@ def test_reduce_non_numeric(self):
data2 = create_test_data(seed=44)
add_vars = {'var4': ['dim1', 'dim2']}
for v, dims in sorted(add_vars.items()):
data = np.random.random_integers(0, 100, size=tuple(_dims[d] for d in dims)).astype(np.str_)
size = tuple(_dims[d] for d in dims)
data = np.random.random_integers(0, 100, size=size).astype(np.str_)
data1[v] = (dims, data, {'foo': 'variable'})

self.assertTrue('var4' not in data1.mean())
Expand All @@ -817,6 +818,35 @@ def test_reduce_keep_attrs(self):
self.assertEqual(len(ds.attrs), len(_attrs))
self.assertTrue(ds.attrs, attrs)

def test_reduce_argmin(self):
# regression test for #205
ds = Dataset({'a': ('x', [0, 1])})
expected = Dataset({'a': ([], 0)})
actual = ds.argmin()
self.assertDatasetIdentical(expected, actual)

actual = ds.argmin('x')
self.assertDatasetIdentical(expected, actual)

@unittest.skip('see github issue 209')
def test_reduce_only_one_axis(self):

def mean_only_one_axis(x, axis):
if not isinstance(axis, (int, np.integer)):
raise TypeError('non-integer axis')
return x.mean(axis)

ds = Dataset({'a': (['x', 'y'], [[0, 1, 2, 3, 4]])})
expected = Dataset({'a': ('x', [2])})
actual = ds.reduce(mean_only_one_axis, 'y')
self.assertDatasetIdentical(expected, actual)

with self.assertRaisesRegexp(TypeError, 'non-integer axis'):
ds.reduce(mean_only_one_axis)

with self.assertRaisesRegexp(TypeError, 'non-integer axis'):
ds.reduce(mean_only_one_axis, ['x', 'y'])

def test_apply(self):
data = create_test_data()
data.attrs['foo'] = 'bar'
Expand Down
9 changes: 9 additions & 0 deletions xray/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,20 @@ def reduce(self, func, dimension=None, keep_attrs=False, **kwargs):
reduce_dims = [dim for dim in var.dimensions if dim in dims]
if reduce_dims:
if name not in self.dimensions:
if len(reduce_dims) == 1:
# unpack dimensions for the benefit of functions like
# np.argmin which can't handle tuple arguments
reduce_dims, = reduce_dims
try:
variables[name] = var.reduce(func,
dimension=reduce_dims,
**kwargs)
except TypeError:
# array (e.g., string) does not support this reduction,
# so skip it
# TODO: rethink silently passing, because the problem
# may be the dimensions and kwargs arguments, not the
# dtype of the array
pass
else:
variables[name] = var
Expand Down