Skip to content

Commit 510997a

Browse files
committed
fixes
1 parent 7901c0c commit 510997a

10 files changed

+60
-80
lines changed

xarray/backends/common.py

-25
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,6 @@ def _decode_variable_name(name):
3030
return name
3131

3232

33-
def is_trivial_index(var):
34-
"""
35-
Determines if in index is 'trivial' meaning that it is
36-
equivalent to np.arange(). This is determined by
37-
checking if there are any attributes or encodings,
38-
if ndims is one, dtype is int and finally by comparing
39-
the actual values to np.arange()
40-
"""
41-
# if either attributes or encodings are defined
42-
# the index is not trivial.
43-
if len(var.attrs) or len(var.encoding):
44-
return False
45-
# if the index is not a 1d integer array
46-
if var.ndim > 1 or not var.dtype.kind == 'i':
47-
return False
48-
arange = np.arange(var.size, dtype=var.dtype)
49-
return np.all(var.values == arange)
50-
51-
5233
def robust_getitem(array, key, catch=Exception, max_retries=6,
5334
initial_delay=500):
5435
"""
@@ -200,12 +181,6 @@ def store_dataset(self, dataset):
200181

201182
def store(self, variables, attributes, check_encoding_set=frozenset()):
202183
self.set_attributes(attributes)
203-
neccesary_dims = [v.dims for v in variables.values()]
204-
neccesary_dims = set(itertools.chain(*neccesary_dims))
205-
# set all non-indexes and any index which is not trivial.
206-
variables = OrderedDict((k, v) for k, v in iteritems(variables)
207-
if not (k in neccesary_dims and
208-
is_trivial_index(v)))
209184
self.set_variables(variables, check_encoding_set)
210185

211186
def set_attributes(self, attributes):

xarray/conventions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def decode_cf(obj, concat_characters=True, mask_and_scale=True,
910910
identify coordinates.
911911
drop_variables: string or iterable, optional
912912
A variable or list of variables to exclude from being parsed from the
913-
dataset.This may be useful to drop variables with problems or
913+
dataset. This may be useful to drop variables with problems or
914914
inconsistent values.
915915
916916
Returns

xarray/core/alignment.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from . import ops, utils
99
from .common import _maybe_promote
1010
from .indexing import get_indexer
11-
from .pycompat import iteritems, OrderedDict
11+
from .pycompat import iteritems, OrderedDict, suppress
1212
from .utils import is_full_slice, is_dict_like
1313
from .variable import Variable, IndexVariable
1414

@@ -112,16 +112,16 @@ def align(*objects, **kwargs):
112112
# pandas). This is useful, e.g., for overwriting such duplicate indexes.
113113
joiner = _get_joiner(join)
114114
joined_indexes = {}
115-
for name, matching_indexes in iteritems(all_indexes):
116-
if name in indexes:
117-
index = utils.safe_cast_to_index(indexes[name])
115+
for dim, matching_indexes in iteritems(all_indexes):
116+
if dim in indexes:
117+
index = utils.safe_cast_to_index(indexes[dim])
118118
if any(not index.equals(other) for other in matching_indexes):
119-
joined_indexes[name] = index
119+
joined_indexes[dim] = index
120120
else:
121121
if any(not matching_indexes[0].equals(other)
122122
for other in matching_indexes[1:]):
123123
index = joiner(matching_indexes)
124-
joined_indexes[name] = index
124+
joined_indexes[dim] = index
125125
else:
126126
index = matching_indexes[0]
127127

@@ -384,18 +384,17 @@ def broadcast(*args, **kwargs):
384384
for arg in args:
385385
for dim in arg.dims:
386386
if dim not in common_coords and dim not in exclude:
387-
common_coords[dim] = arg.coords[dim].variable
388-
dims_map[dim] = common_coords[dim].size
387+
dims_map[dim] = get_size(arg, dim)
388+
if dim in arg.coords:
389+
common_coords[dim] = arg.coords[dim].variable
389390

390391
def _expand_dims(var):
391392
# Add excluded dims to a copy of dims_map
392393
var_dims_map = dims_map.copy()
393394
for dim in exclude:
394-
try:
395+
with suppress(ValueError):
396+
# ignore dim not in var.dims
395397
var_dims_map[dim] = var.shape[var.dims.index(dim)]
396-
except ValueError:
397-
# dim not in var.dims
398-
pass
399398

400399
return var.expand_dims(var_dims_map)
401400

@@ -414,6 +413,8 @@ def _broadcast_dataset(ds):
414413
coords.update(common_coords)
415414
return Dataset(data_vars, coords, ds.attrs)
416415

416+
print(common_coords, dims_map)
417+
417418
result = []
418419
for arg in args:
419420
if isinstance(arg, DataArray):

xarray/core/coordinates.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -219,25 +219,21 @@ def __delitem__(self, key):
219219
del self._data._coords[key]
220220

221221

222-
class LevelCoordinates(AbstractCoordinates):
223-
"""Dictionary like container for MultiIndex level coordinates.
222+
class LevelCoordinatesSource(object):
223+
"""Iterator for MultiIndex level coordinates.
224224
225-
Used for attribute style lookup. Not returned directly by any
226-
public methods.
225+
Used for attribute style lookup with AttrAccessMixin. Not returned directly
226+
by any public methods.
227227
"""
228-
def __init__(self, dataarray):
229-
self._data = dataarray
228+
def __init__(self, data_object):
229+
self._data = data_object
230230

231-
@property
232-
def _names(self):
233-
return set(self._data._level_coords)
231+
def __getitem__(self, key):
232+
# not necessary -- everything here can already be found on coords.
233+
raise KeyError
234234

235-
@property
236-
def variables(self):
237-
level_coords = OrderedDict(
238-
(k, self._data[v].variable.get_level_variable(k))
239-
for k, v in self._data._level_coords.items())
240-
return Frozen(level_coords)
235+
def __iter__(self):
236+
return iter(self._data._level_coords)
241237

242238

243239
class Indexes(Mapping, formatting.ReprMixin):

xarray/core/dataarray.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from . import utils
1515
from .alignment import align
1616
from .common import AbstractArray, BaseDataObject, squeeze
17-
from .coordinates import (DataArrayCoordinates, LevelCoordinates,
17+
from .coordinates import (DataArrayCoordinates, LevelCoordinatesSource,
1818
Indexes)
1919
from .dataset import Dataset
2020
from .pycompat import iteritems, basestring, OrderedDict, zip
@@ -471,7 +471,7 @@ def __delitem__(self, key):
471471
@property
472472
def _attr_sources(self):
473473
"""List of places to look-up items for attribute-style access"""
474-
return [self.coords, LevelCoordinates(self), self.attrs]
474+
return [self.coords, LevelCoordinatesSource(self), self.attrs]
475475

476476
def __contains__(self, key):
477477
return key in self._coords
@@ -1639,7 +1639,8 @@ def dot(self, other):
16391639
axes = (self.get_axis_num(dims), other.get_axis_num(dims))
16401640
new_data = ops.tensordot(self.data, other.data, axes=axes)
16411641

1642-
new_coords = self.coords.merge(other.coords).drop(dims)
1642+
new_coords = self.coords.merge(other.coords)
1643+
new_coords = new_coords.drop([d for d in dims if d in new_coords])
16431644
new_dims = ([d for d in self.dims if d not in dims] +
16441645
[d for d in other.dims if d not in dims])
16451646

xarray/core/dataset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from . import formatting
1616
from .. import conventions
1717
from .alignment import align
18-
from .coordinates import DatasetCoordinates, LevelCoordinates, Indexes
18+
from .coordinates import DatasetCoordinates, LevelCoordinatesSource, Indexes
1919
from .common import ImplementsDatasetReduce, BaseDataObject
2020
from .merge import (dataset_update_method, dataset_merge_method,
2121
merge_data_and_coords)
@@ -499,7 +499,7 @@ def __deepcopy__(self, memo=None):
499499
@property
500500
def _attr_sources(self):
501501
"""List of places to look-up items for attribute-style access"""
502-
return [self, LevelCoordinates(self), self.attrs]
502+
return [self, LevelCoordinatesSource(self), self.attrs]
503503

504504
def __contains__(self, key):
505505
"""The 'in' operator will return true or false depending on whether

xarray/test/test_backends.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,8 @@ class MiscObject:
10881088
class TestValidateAttrs(TestCase):
10891089
def test_validating_attrs(self):
10901090
def new_dataset():
1091-
return Dataset({'data': ('y', np.arange(10.0))})
1091+
return Dataset({'data': ('y', np.arange(10.0))},
1092+
{'y': np.arange(10)})
10921093

10931094
def new_dataset_and_dataset_attrs():
10941095
ds = new_dataset()

xarray/test/test_combine.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ def rectify_dim_order(dataset):
3131
for dim in ['dim1', 'dim2']:
3232
datasets = [g for _, g in data.groupby(dim, squeeze=False)]
3333
self.assertDatasetIdentical(data, concat(datasets, dim))
34-
self.assertDatasetIdentical(
35-
data, concat(datasets, data[dim]))
36-
self.assertDatasetIdentical(
37-
data, concat(datasets, data[dim], coords='minimal'))
3834

39-
datasets = [g for _, g in data.groupby(dim, squeeze=True)]
40-
concat_over = [k for k, v in iteritems(data.coords)
41-
if dim in v.dims and k != dim]
42-
actual = concat(datasets, data[dim], coords=concat_over)
43-
self.assertDatasetIdentical(data, rectify_dim_order(actual))
35+
dim = 'dim2'
36+
self.assertDatasetIdentical(
37+
data, concat(datasets, data[dim]))
38+
self.assertDatasetIdentical(
39+
data, concat(datasets, data[dim], coords='minimal'))
4440

45-
actual = concat(datasets, data[dim], coords='different')
46-
self.assertDatasetIdentical(data, rectify_dim_order(actual))
41+
datasets = [g for _, g in data.groupby(dim, squeeze=True)]
42+
concat_over = [k for k, v in iteritems(data.coords)
43+
if dim in v.dims and k != dim]
44+
actual = concat(datasets, data[dim], coords=concat_over)
45+
self.assertDatasetIdentical(data, rectify_dim_order(actual))
46+
47+
actual = concat(datasets, data[dim], coords='different')
48+
self.assertDatasetIdentical(data, rectify_dim_order(actual))
4749

4850
# make sure the coords argument behaves as expected
4951
data.coords['extra'] = ('dim4', np.arange(3))
@@ -111,7 +113,8 @@ def test_concat_autoalign(self):
111113
ds2 = Dataset({'foo': DataArray([1, 2], coords=[('x', [1, 3])])})
112114
actual = concat([ds1, ds2], 'y')
113115
expected = Dataset({'foo': DataArray([[1, 2, np.nan], [1, np.nan, 2]],
114-
dims=['y', 'x'], coords={'y': [0, 1], 'x': [1, 2, 3]})})
116+
dims=['y', 'x'],
117+
coords={'x': [1, 2, 3]})})
115118
self.assertDatasetIdentical(expected, actual)
116119

117120
def test_concat_errors(self):
@@ -184,7 +187,7 @@ def test_concat_promote_shape(self):
184187
objs = [Dataset({'x': [0]}, {'y': -1}),
185188
Dataset({'x': [1, 2]}, {'y': -2})]
186189
actual = concat(objs, 'x')
187-
expected = Dataset({}, {'y': ('x', [-1, -2, -2])})
190+
expected = Dataset({'x': [0, 1, 2]}, {'y': ('x', [-1, -2, -2])})
188191
self.assertDatasetIdentical(actual, expected)
189192

190193
# broadcast 1d x 1d -> 2d
@@ -196,8 +199,8 @@ def test_concat_promote_shape(self):
196199

197200
def test_concat_do_not_promote(self):
198201
# GH438
199-
objs = [Dataset({'y': ('t', [1])}, {'x': 1}),
200-
Dataset({'y': ('t', [2])}, {'x': 1})]
202+
objs = [Dataset({'y': ('t', [1])}, {'x': 1, 't': 0}),
203+
Dataset({'y': ('t', [2])}, {'x': 1, 't': 0})]
201204
expected = Dataset({'y': ('t', [1, 2])}, {'x': 1, 't': [0, 0]})
202205
actual = concat(objs, 't')
203206
self.assertDatasetIdentical(expected, actual)
@@ -291,8 +294,7 @@ def test_auto_combine(self):
291294
objs = [Dataset(OrderedDict([('x', ('a', [0])), ('y', ('a', [0]))])),
292295
Dataset(OrderedDict([('y', ('a', [1])), ('x', ('a', [1]))]))]
293296
actual = auto_combine(objs)
294-
expected = Dataset(
295-
{'x': ('a', [0, 1]), 'y': ('a', [0, 1]), 'a': [0, 0]})
297+
expected = Dataset({'x': ('a', [0, 1]), 'y': ('a', [0, 1])})
296298
self.assertDatasetIdentical(expected, actual)
297299

298300
objs = [Dataset({'x': [0], 'y': [0]}), Dataset({'y': [1], 'x': [1]})]
@@ -323,7 +325,8 @@ def test_auto_combine_previously_failed(self):
323325
datasets = [Dataset({'a': ('x', [2, 3]), 'x': [1, 2]}),
324326
Dataset({'a': ('x', [1, 2]), 'x': [0, 1]})]
325327
expected = Dataset({'a': (('t', 'x'),
326-
[[np.nan, 2, 3], [1, 2, np.nan]])})
328+
[[np.nan, 2, 3], [1, 2, np.nan]])},
329+
{'x': [0, 1, 2]})
327330
actual = auto_combine(datasets, concat_dim='t')
328331
self.assertDatasetIdentical(expected, actual)
329332

xarray/test/test_dask.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ def assertLazyAndAllClose(self, expected, actual):
202202
def setUp(self):
203203
self.values = np.random.randn(4, 6)
204204
self.data = da.from_array(self.values, chunks=(2, 2))
205-
self.eager_array = DataArray(self.values, dims=('x', 'y'), name='foo')
206-
self.lazy_array = DataArray(self.data, dims=('x', 'y'), name='foo')
205+
self.eager_array = DataArray(self.values, coords={'x': range(4)},
206+
dims=('x', 'y'), name='foo')
207+
self.lazy_array = DataArray(self.data, coords={'x': range(4)},
208+
dims=('x', 'y'), name='foo')
207209

208210
def test_rechunk(self):
209211
chunked = self.eager_array.chunk({'x': 2}).chunk({'y': 2})

xarray/test/test_dataarray.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2102,7 +2102,8 @@ def test_to_dataset_retains_keys(self):
21022102
self.assertDatasetEqual(array, result)
21032103

21042104
def test__title_for_slice(self):
2105-
array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'])
2105+
array = DataArray(np.ones((4, 3, 2)), dims=['a', 'b', 'c'],
2106+
coords={'a': range(4), 'b': range(3), 'c': range(2)})
21062107
self.assertEqual('', array._title_for_slice())
21072108
self.assertEqual('c = 0', array.isel(c=0)._title_for_slice())
21082109
title = array.isel(b=1, c=0)._title_for_slice()

0 commit comments

Comments
 (0)