def test__reordered_axes(): a = Axis('x', 0, None) b = Axis('y', 1, None) c = Axis('z', 2, None) res = _reordered_axes([a, b, c], (1, 2, 0)) names_inds = [(ax.name, ax.index) for ax in res] yield nt.assert_equal, set(names_inds), set([('y', 0), ('z', 1), ('x', 2)])
def test_axes_numeric_access(self): for i, spec in enumerate(self.axes_spec): try: name, labels = spec except ValueError: name, labels = spec, None nt.assert_true(self.A.axes[i] == Axis( name=name, index=i, parent_arr=self.A, labels=labels))
def test_2d(): b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy') yield (nt.assert_equals, b.names, ('x', 'y')) # Check row named slicing rs = b.axes.x[0] yield (npt.assert_equal, rs, [1, 2]) yield nt.assert_equal, rs.names, ('y', ) yield nt.assert_equal, tuple(rs.axes), (Axis('y', 0, rs), ) # Now, check that when slicing a row, we get the right names in the output yield (nt.assert_equal, b.axes.x[1:].names, ('x', 'y')) # Check column named slicing cs = b.axes.y[1] yield (npt.assert_equal, cs, [2, 4, 6]) yield nt.assert_equal, cs.names, ('x', ) yield nt.assert_equal, tuple(cs.axes), (Axis('x', 0, cs), ) # What happens if we do normal slicing? rs = b[0] yield (npt.assert_equal, rs, [1, 2]) yield nt.assert_equal, rs.names, ('y', ) yield nt.assert_equal, tuple(rs.axes), (Axis('y', 0, rs), )
def test_singleton_axis_prep(): b = DataArray(np.random.randn(5, 6), 'xz') slicing = (None, ) shape, axes, key = _make_singleton_axes(b, slicing) key_should_be = (slice(None), ) # should be trimmed shape_should_be = (1, 5, 6) ax_should_be = [Axis(l, i, b) for i, l in enumerate((None, 'x', 'z'))] yield nt.assert_true, key_should_be == key, 'key translated poorly' yield nt.assert_true, shape_should_be == shape, 'shape computed poorly' yield nt.assert_true, all([a1==a2 for a1,a2 in zip(ax_should_be, axes)]), \ 'axes computed poorly'
def test_singleton_axis_prep2(): # a little more complicated b = DataArray(np.random.randn(5, 6), 'xz') slicing = (0, None) shape, axes, key = _make_singleton_axes(b, slicing) key_should_be = (0, ) # should be trimmed shape_should_be = (5, 1, 6) ax_should_be = [Axis(l, i, b) for i, l in enumerate(('x', None, 'z'))] nt.assert_true(key_should_be == key, 'key translated poorly') nt.assert_true(shape_should_be == shape, 'shape computed poorly') nt.assert_true(all([a1 == a2 for a1, a2 in zip(ax_should_be, axes)]), 'axes computed poorly')
def test__pull_axis(): a = Axis('x', 0, None) b = Axis('y', 1, None) c = Axis('z', 2, None) t_pos = Axis('y', 1, None) t_neg = Axis('x', 5, None) axes = [a, b, c] yield nt.assert_true, t_pos in axes yield nt.assert_false, t_neg in axes yield nt.assert_equal, axes, _pull_axis(axes, t_neg) yield nt.assert_equal, axes[:-1], _pull_axis(axes, c) new_axes = [a, Axis('z', 1, None)] yield nt.assert_equal, new_axes, _pull_axis(axes, t_pos)
def test__pull_axis(): a = Axis('x', 0, None) b = Axis('y', 1, None) c = Axis('z', 2, None) t_pos = Axis('y', 1, None) t_neg = Axis('x', 5, None) axes = [a, b, c] nt.assert_true(t_pos in axes) nt.assert_false(t_neg in axes) nt.assert_equal(axes, _pull_axis(axes, t_neg)) nt.assert_equal(axes[:-1], _pull_axis(axes, c)) new_axes = [a, Axis('z', 1, None)] nt.assert_equal(new_axes, _pull_axis(axes, t_pos))
def test_axis_equal(): ax1 = Axis('aname', 0, None) ax2 = Axis('aname', 0, None) yield nt.assert_equal, ax1, ax2 # The array to which the axis points does not matter in comparison ax3 = Axis('aname', 0, np.arange(10)) yield nt.assert_equal, ax1, ax3 # but the index does ax4 = Axis('aname', 1, None) yield nt.assert_not_equal, ax1, ax4 # so does the name ax5 = Axis('anothername', 0, None) yield nt.assert_not_equal, ax1, ax5 # and obviously both yield nt.assert_not_equal, ax4, ax5 # Try with labels ax6 = Axis('same', 0, None, labels=['a', 'b']) ax7 = Axis('same', 0, None, labels=['a', 'b']) yield nt.assert_equal, ax6, ax7 ax8 = Axis('same', 0, None, labels=['a', 'xx']) yield nt.assert_not_equal, ax6, ax8
'''Tests for DataArray and friend''' import sys PY3 = sys.version_info[0] >= 3 import numpy as np from datarray.datarray import (Axis, DataArray, NamedAxisError, AxesManager, _pull_axis, _reordered_axes) import nose.tools as nt import numpy.testing as npt DA = DataArray(np.random.randn(4, 2, 6), 'xyz') YZ = AxesManager(DA, (Axis('y', 0, None), Axis('z', 1, None))) XZ = AxesManager(DA, (Axis('x', 0, None), Axis('z', 1, None))) XY = AxesManager(DA, (Axis('x', 0, None), Axis('y', 1, None))) AXES_REMOVED = dict(x=YZ, y=XZ, z=XY) def test_axis_equal(): ax1 = Axis('aname', 0, None) ax2 = Axis('aname', 0, None) nt.assert_equal(ax1, ax2) # The array to which the axis points does not matter in comparison ax3 = Axis('aname', 0, np.arange(10)) nt.assert_equal(ax1, ax3) # but the index does ax4 = Axis('aname', 1, None) nt.assert_not_equal(ax1, ax4) # so does the name