예제 #1
0
def test_transpose():
    b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy')
    bt = b.T
    c = DataArray([[1, 3, 5], [2, 4, 6]], 'yx')
    yield nt.assert_true, bt.axes.x.index == 1 and bt.axes.y.index == 0
    yield nt.assert_true, bt.shape == (2, 3)
    yield nt.assert_true, (bt == c).all()
예제 #2
0
 def test_equality(self):
     B = DataArray(np.random.randn(200, 4, 10), axes=self.axes_spec)
     nt.assert_true(self.A.axes == B.axes)
     # What if axes differ by labels only?
     D = DataArray(np.random.randn(200, 4, 10),
                   axes=('date', 'stocks', 'metric'))
     nt.assert_false(self.A.axes == D.axes)
예제 #3
0
def test_reductions_keepdims():
    names = 'xyz'
    a = np.arange(24).reshape((2, 3, 4))
    da = DataArray(a, names)
    for idx, name in enumerate(names):
        axes_removed = AXES_REMOVED[name]
        # Test keepdims as kwarg
        for method in reductions:
            check_data_axes(da, method, name, axes_removed)
            if method not in ('ptp', 'argmin', 'argmax'):
                # Reductions taking keepdims argument
                check_data_axes(da, method, name, DA.axes, keepdims=True)
        # Test the individual functions with positional args
        dt = np.dtype(float)
        out = np.mean(da, axis=name)
        kd_out = DataArray(np.mean(a, axis=idx, keepdims=True), names)
        # Functions with signature axis, dtype, out, keepdims
        for method in ('mean', 'sum', 'prod', 'all', 'any'):
            check_data_axes(da, method, name, axes_removed, dt, out)
            check_data_axes(da, method, name, DA.axes, dt, kd_out, True)
        # Signature axis, out, dtype, ddof, keepdims
        for method in ('var', 'std'):
            check_data_axes(da, method, name, axes_removed, dt, out, 0)
            check_data_axes(da, method, name, DA.axes, dt, kd_out, 0, True)
        # Signature axis, out, keepdims
        for method in ('min', 'max'):
            check_data_axes(da, method, name, axes_removed, out)
            check_data_axes(da, method, name, DA.axes, kd_out, True)
        # Test reductions not using keepdims
        out_int = out.astype(np.intp)  # argmin/max have integer output
        for method in ('argmin', 'argmax'):
            check_data_axes(da, method, name, axes_removed, out_int)
        check_data_axes(da, 'ptp', name, axes_removed, out)
예제 #4
0
def test_label_mismatch():
    dar1 = DataArray([1, 2], [('time', ['A1', 'B1'])])
    dar2 = DataArray([1, 2], [('time', ['A2', 'B2'])])
    nt.assert_raises(NamedAxisError, dar1.__add__, dar2)
    nt.assert_raises(NamedAxisError, dar1.__sub__, dar2)
    nt.assert_raises(NamedAxisError, dar1.__mul__, dar2)
    nt.assert_raises(NamedAxisError, dar1.__div__, dar2)
예제 #5
0
def test_swapaxes():
    n_arr = np.random.randn(2, 4, 3)
    a = DataArray(n_arr, 'xyz')
    b = a.swapaxes('x', 'z')
    c = DataArray(n_arr.transpose(2, 1, 0), 'zyx')
    yield nt.assert_true, (c == b).all(), 'data not equal in swapaxes test'
    for ax1, ax2 in zip(b.axes, c.axes):
        yield nt.assert_true, ax1 == ax2, 'axes not equal in swapaxes test'
예제 #6
0
def test_squeeze():
    "Test squeeze method"
    d = DataArray(np.random.randn(3, 2, 9), 'xyz')
    d2 = d[None, :, None, :, :, None]
    nt.assert_true(d2.shape == (1, 3, 1, 2, 9, 1), 'newaxis slicing failed')
    d3 = d.squeeze()
    nt.assert_true(d3.shape == d.shape, 'squeezing length-1 dimensions failed')
    nt.assert_true(d3.names == d.names, 'Axes got lost in squeeze')
예제 #7
0
def test_squeeze():
    "Test squeeze method"
    d = DataArray(np.random.randn(3, 2, 9), "xyz")
    d2 = d[None, :, None, :, :, None]
    nt.assert_true(d2.shape == (1, 3, 1, 2, 9, 1), "newaxis slicing failed")
    d3 = d.squeeze()
    nt.assert_true(d3.shape == d.shape, "squeezing length-1 dimensions failed")
    nt.assert_true(d3.names == d.names, "Axes got lost in squeeze")
예제 #8
0
def test_swapaxes():
    n_arr = np.random.randn(2,4,3)
    a = DataArray(n_arr, 'xyz')
    b = a.swapaxes('x', 'z')
    c = DataArray(n_arr.transpose(2,1,0), 'zyx')
    yield nt.assert_true, (c==b).all(), 'data not equal in swapaxes test'
    for ax1, ax2 in zip(b.axes, c.axes):
        yield nt.assert_true, ax1==ax2, 'axes not equal in swapaxes test'
예제 #9
0
def test_swapaxes():
    n_arr = np.random.randn(2, 4, 3)
    a = DataArray(n_arr, "xyz")
    b = a.swapaxes("x", "z")
    c = DataArray(n_arr.transpose(2, 1, 0), "zyx")
    nt.assert_true((c == b).all(), "data not equal in swapaxes test")
    for ax1, ax2 in zip(b.axes, c.axes):
        nt.assert_true(ax1 == ax2, "axes not equal in swapaxes test")
예제 #10
0
def test_bug44():
    "Bug 44"
    # In instances where axis=None, the operation runs
    # on the flattened array. Here it makes sense to return
    # the op on the underlying np.ndarray.
    A = [[1, 2, 3], [4, 5, 6]]
    x = DataArray(A, 'xy').std()
    y = np.std(A)
    nt.assert_equal(x.sum(), y.sum())
예제 #11
0
def test_squeeze():
    "Test squeeze method"
    d = DataArray(np.random.randn(3,2,9), 'xyz')
    d2 = d[None,:,None,:,:,None]
    yield nt.assert_true, d2.shape == (1,3,1,2,9,1), 'newaxis slicing failed'
    d3 = d.squeeze()
    yield nt.assert_true, d3.shape == d.shape, \
          'squeezing length-1 dimensions failed'
    yield nt.assert_true, d3.labels == d.labels, 'Axes got lost in squeeze'
예제 #12
0
def test_bug44():
    "Bug 44"
    # In instances where axis=None, the operation runs
    # on the flattened array. Here it makes sense to return
    # the op on the underlying np.ndarray.
    A = [[1,2,3],[4,5,6]]
    x = DataArray(A, 'xy').std()
    y = np.std(A)
    nt.assert_equal( x.sum(), y.sum() )
예제 #13
0
def test_reshape_corners():
    "Test some corner cases for reshape"
    d = DataArray(np.random.randn(3, 4, 5), 'xyz')
    d2 = d.reshape(-1)
    yield nt.assert_true, d2.shape == (60, ), 'Flattened shape wrong'
    yield nt.assert_true, type(d2) is np.ndarray, 'Flattened type wrong'

    d2 = d.reshape(60)
    yield nt.assert_true, d2.shape == (60, ), 'Flattened shape wrong'
    yield nt.assert_true, type(d2) is np.ndarray, 'Flattened type wrong'
예제 #14
0
def test_reshape_corners():
    "Test some corner cases for reshape"
    d = DataArray(np.random.randn(3, 4, 5), "xyz")
    d2 = d.reshape(-1)
    nt.assert_true(d2.shape == (60,), "Flattened shape wrong")
    nt.assert_true(type(d2) is np.ndarray, "Flattened type wrong")

    d2 = d.reshape(60)
    nt.assert_true(d2.shape == (60,), "Flattened shape wrong")
    nt.assert_true(type(d2) is np.ndarray, "Flattened type wrong")
예제 #15
0
def test_reshape_corners():
    "Test some corner cases for reshape"
    d = DataArray(np.random.randn(3,4,5), 'xyz')
    d2 = d.reshape(-1)
    yield nt.assert_true, d2.shape == (60,), 'Flattened shape wrong'
    yield nt.assert_true, type(d2) is np.ndarray, 'Flattened type wrong'

    d2 = d.reshape(60)
    yield nt.assert_true, d2.shape == (60,), 'Flattened shape wrong'
    yield nt.assert_true, type(d2) is np.ndarray, 'Flattened type wrong'
예제 #16
0
def test_combination():
    narr = DataArray(np.zeros((1, 2, 3)), axes=('a', 'b', 'c'))
    n3 = DataArray(np.ones((1, 2, 3)), axes=('x', 'b', 'c'))
    yield nt.assert_raises, NamedAxisError, np.add, narr, n3
    # addition of scalar
    res = narr + 2
    yield nt.assert_true, isinstance(res, DataArray)
    yield nt.assert_equal, res.axes, narr.axes
    # addition of matching size array, with matching names
    res = narr + narr
    yield nt.assert_equal, res.axes, narr.axes
예제 #17
0
    def test_axes_name_collision(self):
        "Test .axes object for attribute collisions with axis names"
        A = DataArray(np.arange(6).reshape([1, 2, 3]), ("_arr", "_axes", "_namemap"))
        nt.assert_true(A.axes[0] is A.axes("_arr") is A.axes._arr)
        nt.assert_true(A.axes[1] is A.axes("_axes") is A.axes._axes)
        nt.assert_true(A.axes[2] is A.axes("_namemap") is A.axes._namemap)

        # Try to invoke some methods that use these attributes internally
        B = A[np.newaxis, ...]
        nt.assert_equal(B.shape, (1, 1, 2, 3))
        nt.assert_true(np.all(A + A == 2 * A))
예제 #18
0
    def test_axes_name_collision(self):
        "Test .axes object for attribute collisions with axis names"
        A = DataArray(
            np.arange(6).reshape([1, 2, 3]), ('_arr', '_axes', '_namemap'))
        nt.assert_true(A.axes[0] is A.axes('_arr') is A.axes._arr)
        nt.assert_true(A.axes[1] is A.axes('_axes') is A.axes._axes)
        nt.assert_true(A.axes[2] is A.axes('_namemap') is A.axes._namemap)

        # Try to invoke some methods that use these attributes internally
        B = A[np.newaxis, ...]
        nt.assert_equal(B.shape, (1, 1, 2, 3))
        nt.assert_true(np.all(A + A == 2 * A))
예제 #19
0
def test_basic():
    adata = [2, 3]
    a = DataArray(adata, 'x', float)
    yield nt.assert_equal, a.names, ('x', )
    yield nt.assert_equal, a.dtype, np.dtype(float)
    b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy')
    yield nt.assert_equal, b.names, ('x', 'y')
    # integer slicing
    b0 = b.axes.x[0]
    yield npt.assert_equal, b0, [1, 2]
    # slice slicing
    b1 = b.axes.x[1:]
    yield npt.assert_equal, b1, [[3, 4], [5, 6]]
예제 #20
0
def test_bug38():
    "Bug 38: DataArray.__repr__ should parse as a single entity"
    # Calling repr() on an ndarray prepends array (instead of np.array)
    arys = (
        DataArray(np.random.randint(0, 10000, size=(1,2,3,4,5)), 'abcde'),
        DataArray(np.random.randint(0, 10000, size=(3,3,3))), # Try with missing axes
        DataArray(np.random.randint(0, 10000, (2,4,5,6)), # Try with ticks
            ('a', ('b', ('b1','b2','b3','b4')), 'c', 'd')),
        )
    # Load `array` into namespace for `eval`
    array = np.array
    for A in arys:
        assert_datarray_equal(A, eval(repr(A)))
예제 #21
0
def test_basic():
    adata = [2, 3]
    a = DataArray(adata, 'x', float)
    nt.assert_equal(a.names, ('x', ))
    nt.assert_equal(a.dtype, np.dtype(float))
    b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy')
    nt.assert_equal(b.names, ('x', 'y'))
    # integer slicing
    b0 = b.axes.x[0]
    npt.assert_equal(b0, [1, 2])
    # slice slicing
    b1 = b.axes.x[1:]
    npt.assert_equal(b1, [[3, 4], [5, 6]])
예제 #22
0
def test_flatten_and_ravel():
    "Test the functionality of ravel() and flatten() methods"
    d = DataArray(np.arange(20).reshape(4, 5), "xy")
    df = d.flatten()
    nt.assert_true(type(df) is np.ndarray, "Type error in flatten")
    nt.assert_true(df.shape == (20,), "Wrong shape in flatten")
    df[:4] = 0
    nt.assert_false((d[0, :4] == 0).all(), "Copy not made in flatten")

    dr = d.ravel()
    nt.assert_true(type(dr) is np.ndarray, "Type error in ravel")
    nt.assert_true(dr.shape == (20,), "Wrong shape in ravel")
    dr[:4] = 0
    nt.assert_true((d[0, :4] == 0).all(), "View not made in ravel")
예제 #23
0
def test_bug34():
    "Bug 34: datetime.date ticks not handled by datarray_to_string"
    from datarray.print_grid import datarray_to_string
    from datetime import date as D
    A = DataArray([[1,2],[3,4]], [('row', ('a', D(2010,1,1))),('col', 'cd')])
    exp_out = """row       col                
--------- -------------------
          c         d        
a                 1         2
2010-01-0         3         4"""
    nt.assert_equal(datarray_to_string(A), exp_out)
    # Output for unsigned integers
    B = A.astype(np.uint32)
    nt.assert_equal(datarray_to_string(B), exp_out)
예제 #24
0
def test_flatten_and_ravel():
    "Test the functionality of ravel() and flatten() methods"
    d = DataArray(np.arange(20).reshape(4, 5), 'xy')
    df = d.flatten()
    yield nt.assert_true, type(df) is np.ndarray, 'Type error in flatten'
    yield nt.assert_true, df.shape == (20, ), 'Wrong shape in flatten'
    df[:4] = 0
    yield nt.assert_false, (d[0, :4] == 0).all(), 'Copy not made in flatten'

    dr = d.ravel()
    yield nt.assert_true, type(dr) is np.ndarray, 'Type error in ravel'
    yield nt.assert_true, dr.shape == (20, ), 'Wrong shape in ravel'
    dr[:4] = 0
    yield nt.assert_true, (d[0, :4] == 0).all(), 'View not made in ravel'
예제 #25
0
def test_flatten_and_ravel():
    "Test the functionality of ravel() and flatten() methods"
    d = DataArray(np.arange(20).reshape(4,5), 'xy')
    df = d.flatten()
    yield nt.assert_true, type(df) is np.ndarray, 'Type error in flatten'
    yield nt.assert_true, df.shape == (20,), 'Wrong shape in flatten'
    df[:4] = 0
    yield nt.assert_false, (d[0,:4] == 0).all(), 'Copy not made in flatten'

    dr = d.ravel()
    yield nt.assert_true, type(dr) is np.ndarray, 'Type error in ravel'
    yield nt.assert_true, dr.shape == (20,), 'Wrong shape in ravel'
    dr[:4] = 0
    yield nt.assert_true, (d[0,:4] == 0).all(), 'View not made in ravel'
예제 #26
0
def test_broadcast():
    b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy')
    a = DataArray([1, 0], 'y')
    # both of these should work
    c = b + a
    yield nt.assert_true, c.names == ('x', 'y'), 'simple broadcast failed'
    c = a + b
    yield nt.assert_true, c.names == ('x', 'y'), \
          'backwards simple broadcast failed'

    a = DataArray([1, 1, 1], 'x')
    # this should work too
    c = a[:, np.newaxis] + b
    yield nt.assert_true, c.names == ('x', 'y'), 'forward broadcast1 failed'
    c = b + a[:, np.newaxis]
    yield nt.assert_true, c.names == ('x', 'y'), 'forward broadcast2 failed'

    b = DataArray(np.random.randn(3, 2, 4), ['x', None, 'y'])
    a = DataArray(np.random.randn(2, 4), [None, 'y'])
    # this should work
    c = b + a
    yield nt.assert_true, c.names == ('x', None, 'y'), \
          'broadcast with unlabeled dimensions failed'
    # and this
    a = DataArray(np.random.randn(2, 1), [None, 'y'])
    c = b + a
    yield nt.assert_true, c.names == ('x', None, 'y'), \
          'broadcast with matched name, but singleton dimension failed'
    # check that labeled Axis names the resulting Axis
    b = DataArray(np.random.randn(3, 2, 4), ['x', 'z', 'y'])
    a = DataArray(np.random.randn(2, 4), [None, 'y'])
    # this should work
    c = b + a
    yield nt.assert_true, c.names == ('x', 'z', 'y'), \
          'broadcast with unlabeled dimensions failed'
예제 #27
0
def test_ellipsis_slicing():
    a = DataArray(np.random.randn(2, 5, 6), 'xy')
    nt.assert_true((a[..., 0] == a[:, :, 0]).all(),
                   'slicing with ellipsis failed')
    nt.assert_true((a[0, ...] == a[0]).all(), 'slicing with ellipsis failed')
    nt.assert_true((a[0, ..., 0] == a[0, :, 0]).all(),
                   'slicing with ellipsis failed')
예제 #28
0
def test_1d_label_indexing():
    # issue #18
    cap_ax_spec = 'capitals', [
        'washington', 'london', 'berlin', 'paris', 'moscow'
    ]
    caps = DataArray(np.arange(5), [cap_ax_spec])
    caps.axis.capitals["washington"]
예제 #29
0
def test_shifty_axes():
    arr = np.random.randn(2, 5, 6)
    a = DataArray(arr, 'xy')
    # slicing out the "x" Axis triggered the unlabeled axis to change
    # name from "_2" to "_1".. make sure that this change is mapped
    b = a[0, :2]
    nt.assert_true((b == arr[0, :2]).all(), 'shifty axes strike again!')
예제 #30
0
def test_bug35():
    "Bug 35"
    txt_array = DataArray(['a', 'b'], axes=['dummy'])
    #calling datarray_to_string on string arrays used to fail
    print_grid.datarray_to_string(txt_array)
    #because get_formatter returned the class not an instance
    assert isinstance(print_grid.get_formatter(txt_array),
                      print_grid.StrFormatter)
예제 #31
0
def test_labels_slicing():
    p_arr = np.random.randn(2, 4, 5)
    ax_spec = 'capitals', ['washington', 'london', 'berlin', 'paris', 'moscow']
    d_arr = DataArray(p_arr, [None, None, ax_spec])
    a = d_arr.axes.capitals
    sub_arr = d_arr.axes.capitals['washington'::2]
    yield (nt.assert_equal, sub_arr.axes.capitals.labels, a.labels[0::2])
    yield nt.assert_true, (sub_arr == d_arr[:, :, 0::2]).all()
예제 #32
0
def test_wrapped_ops_axes():
    a = DataArray(np.random.randn(4, 2, 6), 'xyz')
    for m in methods:
        yield assert_axes_correct, a, m, 'x'
    for m in methods:
        yield assert_axes_correct, a, m, 'y'
    for m in methods:
        yield assert_axes_correct, a, m, 'z'
예제 #33
0
def test_axis_slicing_both_ways():
    a = DataArray(np.random.randn(3, 4, 5), 'xyz')

    b1 = a.axis.y[::2].axis.x[1:]
    b2 = a[a.aix.y[::2].x[1:]]

    yield nt.assert_true, (b1 == b2).all()
    yield nt.assert_true, b1.names == b2.names
예제 #34
0
def test_bug5():
    "Bug 5: Support 0d arrays"
    A = DataArray(10)
    # Empty tuples evaluate to false
    nt.assert_false(tuple(A.axes))
    nt.assert_equal(len(A.axes), 0)
    nt.assert_raises(IndexError, lambda: A.axes[0])
    nt.assert_false(A.names)
예제 #35
0
def test_axis_slicing():
    np_arr = np.random.randn(3,4,5)
    a = DataArray(np_arr, 'xyz')
    b = a[ a.aix.y[:2].x[::2] ]
    yield nt.assert_true, (b==a[::2,:2]).all(), 'unordered axis slicing failed'

    b = a[ a.aix.z[:2] ]
    yield nt.assert_true, (b==a.axis.z[:2]).all(), 'axis slicing inconsistent'
    yield nt.assert_true, b.names == ('x', 'y', 'z')
예제 #36
0
def test_newaxis_slicing():
    b = DataArray([[1, 2], [3, 4], [5, 6]], 'xy')
    b2 = b[np.newaxis]
    yield nt.assert_true, b2.shape == (1, ) + b.shape
    yield nt.assert_true, b2.axes[0].name == None

    b2 = b[:, np.newaxis]
    yield nt.assert_true, b2.shape == (3, 1, 2)
    yield nt.assert_true, (b2[:, 0, :] == b).all()
예제 #37
0
def test_axis_make_slice():
    p_arr = np.random.randn(2, 4, 5)
    ax_spec = 'capitals', ['washington', 'london', 'berlin', 'paris', 'moscow']
    d_arr = DataArray(p_arr, [None, None, ax_spec])
    a = d_arr.axes.capitals
    sl = a.make_slice(slice('london', 'moscow'))
    should_be = (slice(None), slice(None), slice(1, 4))
    yield nt.assert_equal, should_be, sl, 'slicing tuple from labels not correct'
    sl = a.make_slice(slice(1, 4))
    yield nt.assert_equal, should_be, sl, 'slicing tuple from idx not correct'
예제 #38
0
def test_reshape():
    d = DataArray(np.random.randn(3, 4, 5), "xyz")
    new_shape = (1, 3, 1, 4, 5)
    # Test padding the shape
    d2 = d.reshape(new_shape)
    new_labels = (None, "x", None, "y", "z")
    nt.assert_true(d2.names == new_labels, "Array with inserted dimensions has wrong labels")
    nt.assert_true(d2.shape == new_shape, "New shape wrong")

    # Test trimming the shape
    d3 = d2.reshape(d.shape)
    nt.assert_true(d3.names == d.names, "Array with removed dimensions has wrong labels")
    nt.assert_true(d3.shape == d.shape, "New shape wrong")

    # Test a combo of padding and trimming
    d4 = d2.reshape(3, 4, 1, 5, 1)
    new_labels = ("x", "y", None, "z", None)
    nt.assert_true(d4.names == new_labels, "Array with inserted and removed dimensions has wrong labels")
    nt.assert_true(d4.shape == (3, 4, 1, 5, 1), "New shape wrong")
예제 #39
0
def test_1d():
    adata = [2, 3]
    a = DataArray(adata, 'x', int)
    # Verify scalar extraction
    yield (nt.assert_true, isinstance(a.axes.x[0], int))
    # Verify indexing of axis
    yield (nt.assert_equals, a.axes.x.index, 0)
    # Iteration checks
    for i, val in enumerate(a.axes.x):
        yield (nt.assert_equals, val, adata[i])
        yield (nt.assert_true, isinstance(val, int))
예제 #40
0
def test_bug34():
    "Bug 34: datetime.date ticks not handled by datarray_to_string"
    from datarray.print_grid import datarray_to_string
    from datetime import date as D
    A = DataArray([[1, 2], [3, 4]], [('row', ('a', D(2010, 1, 1))),
                                     ('col', 'cd')])
    nt.assert_equal(
        datarray_to_string(A), """row       col                
--------- -------------------
          c         d        
a                 1         2
2010-01-0         3         4""")
예제 #41
0
def test_reshape():
    d = DataArray(np.random.randn(3,4,5), 'xyz')
    new_shape = (1,3,1,4,5)
    # Test padding the shape
    d2 = d.reshape(new_shape)
    new_labels = (None, 'x', None, 'y', 'z')
    yield nt.assert_true, d2.labels == new_labels, \
          'Array with inserted dimensions has wrong labels'
    yield nt.assert_true, d2.shape == new_shape, 'New shape wrong'

    # Test trimming the shape
    d3 = d2.reshape(d.shape)
    yield nt.assert_true, d3.labels == d.labels, \
          'Array with removed dimensions has wrong labels'
    yield nt.assert_true, d3.shape == d.shape, 'New shape wrong'

    # Test a combo of padding and trimming
    d4 = d2.reshape(3,4,1,5,1)
    new_labels = ('x', 'y', None, 'z', None)
    yield nt.assert_true, d4.labels == new_labels, \
          'Array with inserted and removed dimensions has wrong labels'
    yield nt.assert_true, d4.shape == (3,4,1,5,1), 'New shape wrong'
예제 #42
0
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'
예제 #43
0
class TestAxesManager:
    def setUp(self):
        self.axes_spec = ('date', ('stocks', ('aapl', 'ibm', 'goog', 'msft')), 'metric')
        self.A = DataArray(np.random.randn(200, 4, 10), axes=self.axes_spec)

    def test_axes_name_collision(self):
        "Test .axes object for attribute collisions with axis names"
        A = DataArray(np.arange(6).reshape([1,2,3]), 
                ('_arr', '_axes', '_namemap'))
        nt.assert_true(A.axes[0] is A.axes('_arr') is A.axes._arr)
        nt.assert_true(A.axes[1] is A.axes('_axes') is A.axes._axes)
        nt.assert_true(A.axes[2] is A.axes('_namemap') is A.axes._namemap)
        
        # Try to invoke some methods that use these attributes internally
        B = A[np.newaxis, ...]
        nt.assert_equal(B.shape, (1,1,2,3))
        nt.assert_true(np.all(A + A == 2*A))

    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_axes_attribute_access(self):
        for spec in self.axes_spec:
            try:
                name,labels = spec
            except ValueError:
                name,labels = spec,None
            nt.assert_true(getattr(self.A.axes, name) is self.A.axes(name))

    def test_equality(self):
        B = DataArray(np.random.randn(200, 4, 10), axes=self.axes_spec)
        nt.assert_true(self.A.axes == B.axes)
        # What if axes differ by labels only?
        D = DataArray(np.random.randn(200, 4, 10), axes=('date', 'stocks', 'metric')) 
        nt.assert_false(self.A.axes == D.axes)
예제 #44
0
 def setUp(self):
     self.axes_spec = ("date", ("stocks", ("aapl", "ibm", "goog", "msft")), "metric")
     self.A = DataArray(np.random.randn(200, 4, 10), axes=self.axes_spec)
예제 #45
0
def test_bug3():
    "Bug 3"
    x = np.array([1,2,3])
    y = DataArray(x, 'x')
    nt.assert_equal( x.sum(), y.sum() )
    nt.assert_equal( x.max(), y.max() )
예제 #46
0
def test_array_set_name():
    a = DataArray(np.arange(20).reshape(2, 5, 2), "xyz")
    a.set_name(0, "u")
    nt.assert_equal(a.axes[0].name, "u", "name change failed")
    nt.assert_equal(a.axes.u, a.axes[0], "name remapping failed")
    nt.assert_equal(a.axes.u.index, 0, "name remapping failed")
예제 #47
0
 def setUp(self):
     self.axes_spec = ('date', ('stocks', ('aapl', 'ibm', 'goog', 'msft')), 'metric')
     self.A = DataArray(np.random.randn(200, 4, 10), axes=self.axes_spec)
예제 #48
0
def test_array_set_name():
    a = DataArray(np.arange(20).reshape(2,5,2), 'xyz')
    a.set_name(0, 'u')
    yield nt.assert_equal, a.axes[0].name, 'u', 'name change failed'
    yield nt.assert_equal, a.axes.u, a.axes[0], 'name remapping failed'
    yield nt.assert_equal, a.axes.u.index, 0, 'name remapping failed'
예제 #49
0
def test_array_set_label():
    a = DataArray(np.arange(20).reshape(2,5,2), 'xyz')
    a.set_label(0, 'u')
    yield nt.assert_equal, a.axes[0].label, 'u', 'label change failed'
    yield nt.assert_equal, a.axis.u, a.axes[0], 'label remapping failed'
    yield nt.assert_equal, a.axis.u.index, 0, 'label remapping failed'