Skip to content

Commit

Permalink
Merge pull request pydata#181 from shoyer/back-to-coordinate
Browse files Browse the repository at this point in the history
Rename xray.Index back to Coordinate
  • Loading branch information
shoyer committed Jul 14, 2014
2 parents e2be878 + 5d255e1 commit 6c394b1
Show file tree
Hide file tree
Showing 12 changed files with 338 additions and 332 deletions.
14 changes: 7 additions & 7 deletions test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,24 @@ def assertDatasetAllClose(self, d1, d2, rtol=1e-05, atol=1e-08):
v2 = d2.variables[k]
self.assertVariableAllClose(v1, v2, rtol=rtol, atol=atol)

def assertIndexesEqual(self, d1, d2):
self.assertEqual(sorted(d1.indexes), sorted(d2.indexes))
for k in d1.indexes:
v1 = d1.indexes[k]
v2 = d2.indexes[k]
def assertCoordinatesEqual(self, d1, d2):
self.assertEqual(sorted(d1.coordinates), sorted(d2.coordinates))
for k in d1.coordinates:
v1 = d1.coordinates[k]
v2 = d2.coordinates[k]
self.assertVariableEqual(v1, v2)

def assertDataArrayEqual(self, ar1, ar2):
self.assertVariableEqual(ar1, ar2)
self.assertIndexesEqual(ar1, ar2)
self.assertCoordinatesEqual(ar1, ar2)

def assertDataArrayIdentical(self, ar1, ar2):
self.assertEqual(ar1.name, ar2.name)
self.assertDatasetIdentical(ar1.dataset, ar2.dataset)

def assertDataArrayAllClose(self, ar1, ar2, rtol=1e-05, atol=1e-08):
self.assertVariableAllClose(ar1, ar2, rtol=rtol, atol=atol)
self.assertIndexesEqual(ar1, ar2)
self.assertCoordinatesEqual(ar1, ar2)


class ReturnItem(object):
Expand Down
66 changes: 33 additions & 33 deletions test/test_data_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from copy import deepcopy
from textwrap import dedent

from xray import Dataset, DataArray, Index, Variable, align
from xray import Dataset, DataArray, Coordinate, Variable, align
from xray.pycompat import iteritems, OrderedDict
from . import TestCase, ReturnItem, source_ndarray

Expand All @@ -25,7 +25,7 @@ def test_repr(self):
<xray.DataArray 'my_variable' (time: 2, x: 3)>
array([[1, 2, 3],
[4, 5, 6]])
Indexes:
Coordinates:
time: Int64Index([0, 1], dtype='int64')
x: Int64Index([0, 1, 2], dtype='int64')
Linked dataset variables:
Expand All @@ -44,9 +44,9 @@ def test_properties(self):
self.assertEqual(getattr(self.dv, attr), getattr(self.v, attr))
self.assertEqual(len(self.dv), len(self.v))
self.assertVariableEqual(self.dv, self.v)
self.assertEqual(list(self.dv.indexes), list(self.ds.indexes))
for k, v in iteritems(self.dv.indexes):
self.assertArrayEqual(v, self.ds.indexes[k])
self.assertEqual(list(self.dv.coordinates), list(self.ds.coordinates))
for k, v in iteritems(self.dv.coordinates):
self.assertArrayEqual(v, self.ds.coordinates[k])
with self.assertRaises(AttributeError):
self.dv.name = 'bar'
with self.assertRaises(AttributeError):
Expand Down Expand Up @@ -85,25 +85,25 @@ def test_constructor(self):
'y': ('y', [-1, -2, -3])})[None]
self.assertDataArrayIdentical(expected, actual)

indexes = [['a', 'b'], [-1, -2, -3]]
actual = DataArray(data, indexes, ['x', 'y'])
coordinates = [['a', 'b'], [-1, -2, -3]]
actual = DataArray(data, coordinates, ['x', 'y'])
self.assertDataArrayIdentical(expected, actual)

indexes = [pd.Index(['a', 'b'], name='A'),
coordinates = [pd.Index(['a', 'b'], name='A'),
pd.Index([-1, -2, -3], name='B')]
actual = DataArray(data, indexes, ['x', 'y'])
actual = DataArray(data, coordinates, ['x', 'y'])
self.assertDataArrayIdentical(expected, actual)

indexes = {'x': ['a', 'b'], 'y': [-1, -2, -3]}
actual = DataArray(data, indexes, ['x', 'y'])
coordinates = {'x': ['a', 'b'], 'y': [-1, -2, -3]}
actual = DataArray(data, coordinates, ['x', 'y'])
self.assertDataArrayIdentical(expected, actual)

indexes = OrderedDict([('x', ['a', 'b']), ('y', [-1, -2, -3])])
actual = DataArray(data, indexes)
coordinates = OrderedDict([('x', ['a', 'b']), ('y', [-1, -2, -3])])
actual = DataArray(data, coordinates)
self.assertDataArrayIdentical(expected, actual)

indexes = pd.Series([['a', 'b'], [-1, -2, -3]], ['x', 'y'])
actual = DataArray(data, indexes)
coordinates = pd.Series([['a', 'b'], [-1, -2, -3]], ['x', 'y'])
actual = DataArray(data, coordinates)
self.assertDataArrayIdentical(expected, actual)

expected = Dataset({None: (['x', 'y'], data),
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_constructor(self):
def test_constructor_from_self_described(self):
data = [[-0.1, 21], [0, 2]]
expected = DataArray(data,
indexes={'x': ['a', 'b'], 'y': [-1, -2]},
coordinates={'x': ['a', 'b'], 'y': [-1, -2]},
dimensions=['x', 'y'], name='foobar',
attributes={'bar': 2}, encoding={'foo': 3})
actual = DataArray(expected)
Expand All @@ -163,7 +163,7 @@ def test_constructor_from_self_described(self):

panel = pd.Panel({0: frame})
actual = DataArray(panel)
expected = DataArray([data], expected.indexes, ['dim_0', 'x', 'y'])
expected = DataArray([data], expected.coordinates, ['dim_0', 'x', 'y'])
self.assertDataArrayIdentical(expected, actual)

expected = DataArray(['a', 'b'], name='foo')
Expand Down Expand Up @@ -249,30 +249,30 @@ def test_loc(self):
da.loc['a':'j'] = 0
self.assertTrue(np.all(da.values == 0))

def test_indexes(self):
indexes = [Index('x', [-1, -2]), Index('y', [0, 1, 2])]
da = DataArray(np.random.randn(2, 3), indexes, name='foo')
def test_coordinates(self):
coordinates = [Coordinate('x', [-1, -2]), Coordinate('y', [0, 1, 2])]
da = DataArray(np.random.randn(2, 3), coordinates, name='foo')

self.assertEquals(2, len(da.indexes))
self.assertEquals(2, len(da.coordinates))

self.assertEquals(['x', 'y'], list(da.indexes))
self.assertEquals(['x', 'y'], list(da.coordinates))

self.assertTrue(da.indexes[0].identical(indexes[0]))
self.assertTrue(da.indexes['x'].identical(indexes[0]))
self.assertTrue(da.indexes[1].identical(indexes[1]))
self.assertTrue(da.indexes['y'].identical(indexes[1]))
self.assertTrue(da.coordinates[0].identical(coordinates[0]))
self.assertTrue(da.coordinates['x'].identical(coordinates[0]))
self.assertTrue(da.coordinates[1].identical(coordinates[1]))
self.assertTrue(da.coordinates['y'].identical(coordinates[1]))

self.assertIn('x', da.indexes)
self.assertNotIn(0, da.indexes)
self.assertNotIn('foo', da.indexes)
self.assertIn('x', da.coordinates)
self.assertNotIn(0, da.coordinates)
self.assertNotIn('foo', da.coordinates)

with self.assertRaises(KeyError):
da.indexes['foo']
da.coordinates['foo']

expected = dedent("""\
x: Int64Index([-1, -2], dtype='int64')
y: Int64Index([0, 1, 2], dtype='int64')""")
actual = repr(da.indexes)
actual = repr(da.coordinates)
self.assertEquals(expected, actual)

def test_reindex(self):
Expand Down Expand Up @@ -474,12 +474,12 @@ def make_groupby_example_array(self):
def test_groupby_properties(self):
grouped = self.make_groupby_example_array().groupby('abc')
expected_unique = Variable('abc', ['a', 'b', 'c'])
self.assertVariableEqual(expected_unique, grouped.unique_index)
self.assertVariableEqual(expected_unique, grouped.unique_coord)
self.assertEqual(3, len(grouped))

def test_groupby_apply_identity(self):
expected = self.make_groupby_example_array()
idx = expected.indexes['y']
idx = expected.coordinates['y']
identity = lambda x: x
for g in ['x', 'y', 'abc', idx]:
for shortcut in [False, True]:
Expand Down
78 changes: 39 additions & 39 deletions test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import pandas as pd

from xray import (Dataset, DataArray, Index, Variable,
from xray import (Dataset, DataArray, Coordinate, Variable,
backends, utils, align, indexing)
from xray.pycompat import iteritems, OrderedDict

Expand Down Expand Up @@ -73,16 +73,16 @@ def test_repr(self):
data = create_test_data()
expected = dedent("""
<xray.Dataset>
Dimensions: (dim1: 100, dim2: 50, dim3: 10, time: 20)
Indexes:
dim1 X
dim2 X
dim3 X
time X
Non-indexes:
var1 0 1
var2 0 1
var3 1 0
Dimensions: (dim1: 100, dim2: 50, dim3: 10, time: 20)
Coordinates:
dim1 X
dim2 X
dim3 X
time X
Noncoordinates:
var1 0 1
var2 0 1
var3 1 0
Attributes:
Empty
""").strip()
Expand All @@ -91,10 +91,10 @@ def test_repr(self):

expected = dedent("""
<xray.Dataset>
Dimensions: ()
Indexes:
Dimensions: ()
Coordinates:
None
Non-indexes:
Noncoordinates:
None
Attributes:
Empty
Expand Down Expand Up @@ -130,14 +130,14 @@ def test_variable(self):
with self.assertRaises(ValueError):
a['qux'] = (('time', 'x'), d.T)

def test_indexes_create(self):
def test_coordinates_create(self):
a = Dataset()
vec = np.random.random((10,))
attributes = {'foo': 'bar'}
a['x'] = ('x', vec, attributes)
self.assertTrue('x' in a.indexes)
self.assertIsInstance(a.indexes['x'].as_pandas, pd.Index)
self.assertVariableIdentical(a.indexes['x'], a.variables['x'])
self.assertTrue('x' in a.coordinates)
self.assertIsInstance(a.coordinates['x'].as_index, pd.Index)
self.assertVariableIdentical(a.coordinates['x'], a.variables['x'])
b = Dataset()
b['x'] = ('x', vec, attributes)
self.assertVariableIdentical(a['x'], b['x'])
Expand All @@ -156,31 +156,31 @@ def test_indexes_create(self):
a['y'] = ('y', scal)
self.assertTrue('y' not in a.dimensions)

def test_indexes_properties(self):
def test_coordinates_properties(self):
data = Dataset({'x': ('x', [-1, -2]),
'y': ('y', [0, 1, 2]),
'foo': (['x', 'y'], np.random.randn(2, 3))})

self.assertEquals(2, len(data.indexes))
self.assertEquals(2, len(data.coordinates))

self.assertEquals(set(['x', 'y']), set(data.indexes))
self.assertEquals(set(['x', 'y']), set(data.coordinates))

self.assertVariableIdentical(data.indexes['x'], data['x'].variable)
self.assertVariableIdentical(data.indexes['y'], data['y'].variable)
self.assertVariableIdentical(data.coordinates['x'], data['x'].variable)
self.assertVariableIdentical(data.coordinates['y'], data['y'].variable)

self.assertIn('x', data.indexes)
self.assertNotIn(0, data.indexes)
self.assertNotIn('foo', data.indexes)
self.assertIn('x', data.coordinates)
self.assertNotIn(0, data.coordinates)
self.assertNotIn('foo', data.coordinates)

with self.assertRaises(KeyError):
data.indexes['foo']
data.coordinates['foo']
with self.assertRaises(KeyError):
data.indexes[0]
data.coordinates[0]

expected = dedent("""\
x: Int64Index([-1, -2], dtype='int64')
y: Int64Index([0, 1, 2], dtype='int64')""")
actual = repr(data.indexes)
actual = repr(data.coordinates)
self.assertEquals(expected, actual)

def test_equals_and_identical(self):
Expand Down Expand Up @@ -240,18 +240,18 @@ def test_isel(self):

ret = data.isel(dim1=0)
self.assertEqual({'time': 20, 'dim2': 50, 'dim3': 10}, ret.dimensions)
self.assertItemsEqual(list(data.nonindexes) + ['dim1'],
ret.nonindexes)
self.assertItemsEqual(list(data.noncoordinates) + ['dim1'],
ret.noncoordinates)

ret = data.isel(time=slice(2), dim1=0, dim2=slice(5))
self.assertEqual({'time': 2, 'dim2': 5, 'dim3': 10}, ret.dimensions)
self.assertItemsEqual(list(data.nonindexes) + ['dim1'],
ret.nonindexes)
self.assertItemsEqual(list(data.noncoordinates) + ['dim1'],
ret.noncoordinates)

ret = data.isel(time=0, dim1=0, dim2=slice(5))
self.assertItemsEqual({'dim2': 5, 'dim3': 10}, ret.dimensions)
self.assertItemsEqual(list(data.nonindexes) + ['dim1', 'time'],
ret.nonindexes)
self.assertItemsEqual(list(data.noncoordinates) + ['dim1', 'time'],
ret.noncoordinates)

def test_sel(self):
data = create_test_data()
Expand Down Expand Up @@ -467,7 +467,7 @@ def test_virtual_variables(self):
self.assertVariableEqual(data['time.dayofyear'],
Variable('time', 1 + np.arange(20)))
self.assertArrayEqual(data['time.month'].values,
data.variables['time'].as_pandas.month)
data.variables['time'].as_index.month)
self.assertArrayEqual(data['time.season'].values, 1)
# test virtual variable math
self.assertArrayEqual(data['time.dayofyear'] + 1, 2 + np.arange(20))
Expand Down Expand Up @@ -563,7 +563,7 @@ def test_groupby_errors(self):
with self.assertRaisesRegexp(ValueError, 'length does not match'):
data.groupby(data['dim1'][:3])
with self.assertRaisesRegexp(ValueError, "must have a 'dimensions'"):
data.groupby(data.indexes['dim1'].as_pandas)
data.groupby(data.coordinates['dim1'].as_index)

def test_groupby_reduce(self):
data = Dataset({'xy': (['x', 'y'], np.random.randn(3, 4)),
Expand Down Expand Up @@ -724,10 +724,10 @@ def test_lazy_load(self):
def test_reduce(self):
data = create_test_data()

self.assertEqual(len(data.mean().indexes), 0)
self.assertEqual(len(data.mean().coordinates), 0)

expected = data.max()
for var in data.nonindexes:
for var in data.noncoordinates:
expected = data[var].max()
actual = expected[var]
self.assertDataArrayEqual(expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions test/test_indexing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from xray import indexing, variable, Dataset, Variable, Index
from xray import indexing, variable, Dataset, Variable, Coordinate
from . import TestCase, ReturnItem


Expand Down Expand Up @@ -65,7 +65,7 @@ def test_orthogonal_indexer(self):

def test_convert_label_indexer(self):
# TODO: add tests that aren't just for edge cases
coord = Index('x', [1, 2, 3])
coord = Coordinate('x', [1, 2, 3])
with self.assertRaisesRegexp(ValueError, 'not all values found'):
indexing.convert_label_indexer(coord, [0])
with self.assertRaises(KeyError):
Expand Down
Loading

0 comments on commit 6c394b1

Please sign in to comment.