Example #1
0
    def test_lift_arrfunc(self):
        # First get a ckernel from numpy
        requiregil = False
        af = _lowlevel.arrfunc_from_ufunc(np.ldexp,
                        (np.float64, np.float64, np.int32),
                        requiregil)
        self.assertEqual(nd.as_py(af.proto),
                         ndt.type("(float64, int32) -> float64"))

        # Now lift it
        af_lifted = _lowlevel.lift_arrfunc(af)
        self.assertEqual(nd.as_py(af_lifted.proto),
                         ndt.type("(Dims... * float64, Dims... * int32) -> Dims... * float64"))
        # Create some compatible arguments
        in0 = nd.array([[1, 2, 3], [4, 5], [6], [7,9,10]],
                       type='fixed * var * float64')
        in1 = nd.array([[-1], [10], [100], [-12]], type='fixed * 1 * int32')
        # Instantiate and call the kernel on these arguments
        out = af_lifted(in0, in1)
        # Verify that we got the expected result
        self.assertEqual(nd.as_py(out),
                    [[0.5, 1.0, 1.5],
                     [4096.0, 5120.0],
                     [float(6*2**100)],
                     [0.001708984375, 0.002197265625, 0.00244140625]])
 def test__type_from_h5py_special(self):
     # h5py 2.3 style "special dtype"
     dt = np.dtype(object, metadata={'vlen' : str})
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(object, metadata={'vlen' : unicode})
         self.assertEqual(ndt.type(dt), ndt.string)
     # h5py 2.2 style "special dtype"
     dt = np.dtype(('O', [( ({'type': str},'vlen'), 'O' )] ))
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(('O', [( ({'type': unicode},'vlen'), 'O' )] ))
         self.assertEqual(ndt.type(dt), ndt.string)
     # Should be able to roundtrip dynd -> numpy -> dynd
     x = nd.array(['testing', 'one', 'two'])
     self.assertEqual(nd.type_of(x), ndt.type('3 * string'))
     y = nd.as_numpy(x, allow_copy=True)
     self.assertEqual(y.shape, (3,))
     self.assertEqual(y[0], 'testing')
     self.assertEqual(y[1], 'one')
     self.assertEqual(y[2], 'two')
     self.assertEqual(y.dtype.kind, 'O')
     if sys.version_info < (3, 0):
         self.assertEqual(y.dtype.metadata, {'vlen' : unicode})
     else:
         self.assertEqual(y.dtype.metadata, {'vlen' : str})
     z = nd.array(y)
     self.assertEqual(nd.type_of(z), nd.type_of(x))
     self.assertEqual(nd.as_py(z), nd.as_py(x))
    def test_single_struct(self):
        a = nd.array([12, 'test', True], type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array([12, 'test', True], dtype='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        a = nd.array({'x':12, 'y':'test', 'z':True}, type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array({'x':12, 'y':'test', 'z':True}, dtype='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)
 def test_empty(self):
     # Constructor from scalar type
     a = nd.empty(ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     # Constructor from type with fixed dimension
     a = nd.empty('3 * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from type with fixed dimension, accesskwarg
     a = nd.empty('3 * int32', access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     # Constructor from shape as single integer
     a = nd.empty(3, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     self.assertEqual(a.shape, (3,))
     # Constructor from shape as tuple
     a = nd.empty((3,4), ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3,4))
     # Constructor from shape as variadic arguments
     a = nd.empty(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3,4))
     # Constructor from shape as variadic arguments, access kwarg
     a = nd.empty(3, 4, ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3,4))
    def test_single_struct(self):
        a = nd.array([12, "test", True], type="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array([12, "test", True], dtype="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        a = nd.array({"x": 12, "y": "test", "z": True}, type="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array({"x": 12, "y": "test", "z": True}, dtype="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)
Example #6
0
 def test_arrfunc_from_instantiate_pyfunc(self):
     # Test wrapping make_assignment_ckernel as an arrfunc
     def instantiate_assignment(out_ckb, ckb_offset, dst_tp, dst_arrmeta,
                                src_tp, src_arrmeta, kernreq, ectx):
         out_ckb = _lowlevel.CKernelBuilderStruct.from_address(out_ckb)
         return _lowlevel.make_assignment_ckernel(out_ckb, ckb_offset,
                         dst_tp, dst_arrmeta,
                         src_tp[0], src_arrmeta[0],
                         kernreq, ectx)
     af = _lowlevel.arrfunc_from_instantiate_pyfunc(
                 instantiate_assignment, "(date) -> string")
     self.assertEqual(nd.as_py(af.proto), ndt.type("(date) -> string"))
     in0 = nd.array('2012-11-05', ndt.date)
     out = af(in0)
     self.assertEqual(nd.as_py(out), '2012-11-05')
     # Also test it as a lifted kernel
     af_lifted = _lowlevel.lift_arrfunc(af)
     self.assertEqual(nd.as_py(af_lifted.proto),
                      ndt.type("(Dims... * date) -> Dims... * string"))
     from datetime import date
     in0 = nd.array([['2013-03-11', date(2010, 10, 10)],
                     [date(1999, 12, 31)],
                     []], type='3 * var * date')
     out = af_lifted(in0)
     self.assertEqual(nd.as_py(out),
                     [['2013-03-11', '2010-10-10'],
                      ['1999-12-31'], []])
    def test_single_struct_array(self):
        a = nd.array([(0,0), (3,5), (12,10)], dtype='{x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([{'x':0,'y':0}, {'x':3,'y':5}, {'x':12,'y':10}],
                    dtype='{x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([[(3, 'X')], [(10, 'L'), (12, 'M')]],
                        dtype='{count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(nd.type_of(a),
                       ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])

        a = nd.array([[{'count':3, 'size':'X'}],
                        [{'count':10, 'size':'L'}, {'count':12, 'size':'M'}]],
                        dtype='{count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(nd.type_of(a), ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])
Example #8
0
 def test_ckernel_deferred_from_pyfunc(self):
     # Test wrapping make_assignment_ckernel as a deferred ckernel
     def instantiate_assignment(out_ckb, ckb_offset, types, meta,
                                kerntype, ectx):
         out_ckb = _lowlevel.CKernelBuilderStruct.from_address(out_ckb)
         return _lowlevel.make_assignment_ckernel(out_ckb, ckb_offset,
                         types[0], meta[0],
                         types[1], meta[1],
                         'expr', kerntype, ectx)
     ckd = _lowlevel.ckernel_deferred_from_pyfunc(instantiate_assignment,
                     [ndt.string, ndt.date])
     self.assertEqual(nd.as_py(ckd.types), [ndt.string, ndt.date])
     out = nd.empty(ndt.string)
     in0 = nd.array('2012-11-05', ndt.date)
     ckd.__call__(out, in0)
     self.assertEqual(nd.as_py(out), '2012-11-05')
     # Also test it as a lifted kernel
     ckd_lifted = _lowlevel.lift_ckernel_deferred(ckd,
                     ['3 * var * string', '3 * var * date'])
     self.assertEqual(nd.as_py(ckd_lifted.types),
                 [ndt.type('3 * var * string'), ndt.type('3 * var * date')])
     out = nd.empty('3 * var * string')
     from datetime import date
     in0 = nd.array([['2013-03-11', date(2010, 10, 10)],
                     [date(1999, 12, 31)],
                     []], type='3 * var * date')
     ckd_lifted.__call__(out, in0)
     self.assertEqual(nd.as_py(out),
                     [['2013-03-11', '2010-10-10'],
                      ['1999-12-31'], []])
Example #9
0
 def test_empty(self):
     # Constructor from scalar type
     a = nd.empty(ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     # Constructor from type with fixed dimension
     a = nd.empty('3 * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3, ))
     # Constructor from type with fixed dimension, accesskwarg
     a = nd.empty('3 * int32', access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3, ))
     # Constructor from shape as single integer
     a = nd.empty(3, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     self.assertEqual(a.shape, (3, ))
     # Constructor from shape as tuple
     a = nd.empty((3, 4), ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     # Constructor from shape as variadic arguments
     a = nd.empty(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     # Constructor from shape as variadic arguments, access kwarg
     a = nd.empty(3, 4, ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
 def check_constructor(self, cons, value):
     # Constructor from scalar type
     a = cons(ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     self.assertEqual(nd.as_py(a), value)
     # Constructor from type with fixed dimension
     a = cons('3 * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value]*3)
     # Constructor from shape as single integer
     a = cons(3, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [value]*3)
     # Constructor from shape as tuple
     a = cons((3,4), ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3,4))
     self.assertEqual(nd.as_py(a), [[value]*4]*3)
     # Constructor from shape as variadic arguments
     a = cons(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3,4))
     self.assertEqual(nd.as_py(a), [[value]*4]*3)
Example #11
0
 def check_constructor(self, cons, value):
     # Constructor from scalar type
     a = cons(ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     self.assertEqual(nd.as_py(a), value)
     # Constructor from type with fixed dimension
     a = cons('3 * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as single integer
     a = cons(3, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as tuple
     a = cons((3, 4), ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor from shape as variadic arguments
     a = cons(3, 4, ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
 def test_simple(self):
     self.assertTrue(ndt.int32.matches(ndt.int32))
     self.assertTrue(ndt.int16.matches('T'))
     self.assertTrue(ndt.int16.matches('... * T'))
     self.assertTrue(ndt.int16.matches('A... * T'))
     self.assertTrue(ndt.type('strided * var * int').matches('M * A... * N * T'))
     self.assertFalse(ndt.type('strided * int').matches('M * A... * N * T'))
 def test_struct(self):
     a = nd.parse_json('{x:int32, y:string, z:float32}',
                       '{"x":20, "y":"testing one two three", "z":-3.25}')
     self.assertEqual(nd.type_of(a),
                      ndt.type('{x:int32, y:string, z:float32}'))
     self.assertEqual(nd.type_of(a[...]),
                      ndt.type('{x:int32, y:string, z:float32}'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[1]), ndt.string)
     self.assertEqual(nd.type_of(a[2]), ndt.float32)
     self.assertEqual(nd.type_of(a[-3]), ndt.int32)
     self.assertEqual(nd.type_of(a[-2]), ndt.string)
     self.assertEqual(nd.type_of(a[-1]), ndt.float32)
     self.assertEqual(
         nd.type_of(a[1:]),
         ndt.make_struct([ndt.string, ndt.float32], ['y', 'z']))
     self.assertEqual(nd.type_of(a[::-2]),
                      ndt.make_struct([ndt.float32, ndt.int32], ['z', 'x']))
     self.assertEqual(nd.as_py(a[0]), 20)
     self.assertEqual(nd.as_py(a[1]), "testing one two three")
     self.assertEqual(nd.as_py(a[2]), -3.25)
     self.assertEqual(nd.as_py(a[1:]), {
         'y': 'testing one two three',
         'z': -3.25
     })
     self.assertEqual(nd.as_py(a[::-2]), {'x': 20, 'z': -3.25})
 def test_ndt_type_from_h5py_special(self):
     # h5py 2.3 style "special dtype"
     dt = np.dtype(object, metadata={'vlen' : str})
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(object, metadata={'vlen' : unicode})
         self.assertEqual(ndt.type(dt), ndt.string)
     # h5py 2.2 style "special dtype"
     dt = np.dtype(('O', [( ({'type': str},'vlen'), 'O' )] ))
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(('O', [( ({'type': unicode},'vlen'), 'O' )] ))
         self.assertEqual(ndt.type(dt), ndt.string)
     # Should be able to roundtrip dynd -> numpy -> dynd
     x = nd.array(['testing', 'one', 'two'])
     self.assertEqual(nd.type_of(x), ndt.type('3 * string'))
     y = nd.as_numpy(x, allow_copy=True)
     self.assertEqual(y.dtype.kind, 'O')
     if sys.version_info < (3, 0):
         self.assertEqual(y.dtype.metadata, {'vlen' : unicode})
     else:
         self.assertEqual(y.dtype.metadata, {'vlen' : str})
     z = nd.array(y)
     self.assertEqual(nd.type_of(z), nd.type_of(x))
     self.assertEqual(nd.as_py(z), nd.as_py(x))
 def test_dynamic_fromiter_onetype(self):
     # Constructing with an iterator like this uses a dynamic
     # array construction method. In this simple case, we
     # use generators that have a consistent type
     # bool result
     a = nd.array(iter([True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('2 * bool'))
     self.assertEqual(nd.as_py(a), [True, False])
     # int32 result
     a = nd.array(iter([1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('4 * int32'))
     self.assertEqual(nd.as_py(a), [1, 2, 1, 0])
     # int64 result
     a = nd.array(iter([10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('5 * int64'))
     self.assertEqual(nd.as_py(a), [10000000000, 1, 2, 1, 0])
     # float64 result
     a = nd.array(iter([3.25, 10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('6 * float64'))
     self.assertEqual(nd.as_py(a), [3.25, 10000000000, 1, 2, 1, 0])
     # complex[float64] result
     a = nd.array(iter([3.25j, 3.25, 10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('7 * complex[float64]'))
     self.assertEqual(nd.as_py(a), [3.25j, 3.25, 10000000000, 1, 2, 1, 0])
     """
Example #16
0
    def test_dynamic_fromiter_onetype(self):
        # Constructing with an iterator like this uses a dynamic
        # array construction method. In this simple case, we
        # use generators that have a consistent type
        # bool result
        a = nd.array(iter([True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('2 * bool'))
        self.assertEqual(nd.as_py(a), [True, False])
        # int32 result
        a = nd.array(iter([1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('4 * int32'))
        self.assertEqual(nd.as_py(a), [1, 2, 1, 0])
        # int64 result
        a = nd.array(iter([10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('5 * int64'))
        self.assertEqual(nd.as_py(a), [10000000000, 1, 2, 1, 0])
        # float64 result
        a = nd.array(iter([3.25, 10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('6 * float64'))
        self.assertEqual(nd.as_py(a), [3.25, 10000000000, 1, 2, 1, 0])
        # complex[float64] result
        a = nd.array(iter([3.25j, 3.25, 10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('7 * complex[float64]'))
        self.assertEqual(nd.as_py(a), [3.25j, 3.25, 10000000000, 1, 2, 1, 0])

        """
    def test_unicode_array(self):
        a = nd.array([u'this', 'is', u'a', 'test'], dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])

        a = nd.array([u'this', 'is', u'a', 'test'], dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('4 * string["U16"]'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
    def test_unicode_array(self):
        a = nd.array([u"this", "is", u"a", "test"], dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type("strided * string"))
        self.assertEqual(nd.as_py(a), ["this", "is", "a", "test"])

        a = nd.array([u"this", "is", u"a", "test"], dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('strided * string["U16"]'))
        self.assertEqual(nd.as_py(a), ["this", "is", "a", "test"])
Example #19
0
 def test_empty_array(self):
     # Empty arrays default to int32
     a = nd.array([])
     self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     self.assertEqual(a.shape, (0, ))
     a = nd.array([[], [], []])
     self.assertEqual(nd.type_of(a), ndt.type('3 * 0 * int32'))
     self.assertEqual(a.shape, (3, 0))
 def test_empty_array(self):
     # Empty arrays default to int32
     a = nd.array([])
     self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     self.assertEqual(a.shape, (0,))
     a = nd.array([[], [], []])
     self.assertEqual(nd.type_of(a), ndt.type('3 * 0 * int32'))
     self.assertEqual(a.shape, (3, 0))
Example #21
0
 def test_type_shape(self):
     # The shape attribute of ndt.type
     tp = ndt.type('3 * 4 * int32')
     self.assertEqual(tp.shape, (3, 4))
     tp = ndt.type('Fixed * 3 * var * int32')
     self.assertEqual(tp.shape, (-1, 3, -1))
     tp = ndt.type('var * 3 * 2 * int32')
     self.assertEqual(tp.shape, (-1, 3, 2))
Example #22
0
 def test_type_shape(self):
     # The shape attribute of ndt.type
     tp = ndt.type('3 * 4 * int32')
     self.assertEqual(tp.shape, (3, 4))
     tp = ndt.type('Fixed * 3 * var * int32')
     self.assertEqual(tp.shape, (-1, 3, -1))
     tp = ndt.type('var * 3 * 2 * int32')
     self.assertEqual(tp.shape, (-1, 3, 2))
 def test_struct(self):
     # Tests of cstruct datashape
     dt = ndt.type('c{x: cfixed[3] * int32, y: string}')
     self.assertEqual(dt.type_id, 'cstruct')
     self.assertEqual(nd.as_py(dt.field_names), ['x', 'y'])
     # Tests of struct datashape
     dt = ndt.type('{x: 3 * int32, y: string}')
     self.assertEqual(dt.type_id, 'struct')
     self.assertEqual(nd.as_py(dt.field_names), ['x', 'y'])
 def test_ragged_fromlistofiter_typepromo(self):
     # list of iterators
     vals = [[True, False], [False, 2, 3], [-10000000000], [True, 10, 3.125, 5.5j]]
     a = nd.array([iter(x) for x in vals])
     self.assertEqual(nd.type_of(a), ndt.type("strided * var * complex[float64]"))
     self.assertEqual(nd.as_py(a), vals)
     # list of list/iterator
     a = nd.array([[1, 2, 3], (1.5 * x for x in range(4)), iter([-1, 1])])
     self.assertEqual(nd.type_of(a), ndt.type("strided * var * float64"))
     self.assertEqual(nd.as_py(a), [[1, 2, 3], [1.5 * x for x in range(4)], [-1, 1]])
    def test_unicode_array(self):
        a = nd.array([u'this', 'is', u'a', 'test'],
                        dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])

        a = nd.array([u'this', 'is', u'a', 'test'],
                        dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('4 * string["U16"]'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
Example #26
0
    def run(self, size):
        if self.cuda:
            dst_tp = ndt.type('cuda_device[{} * float64]'.format(size))
        else:
            dst_tp = ndt.type('{} * float64'.format(size))
        dst = nd.empty(dst_tp)

        with CUDATimer() if self.cuda else Timer() as timer:
            nd.uniform(dst_tp=dst_tp)

        return timer.elapsed_time()
  def run(self, size):
    if self.cuda:
      dst_tp = ndt.type('cuda_device[{} * float64]'.format(size))
    else:
      dst_tp = ndt.type('{} * float64'.format(size))
    dst = nd.empty(dst_tp)

    with CUDATimer() if self.cuda else Timer() as timer:
      nd.uniform(dst_tp = dst_tp)

    return timer.elapsed_time()
 def test_datetime_types(self):
     import datetime
     self.assertEqual(ndt.date, ndt.type(datetime.date))
     self.assertEqual(str(ndt.date), "date")
     self.assertEqual(repr(ndt.date), "ndt.date")
     self.assertEqual(ndt.time, ndt.type(datetime.time))
     self.assertEqual(str(ndt.time), "time")
     self.assertEqual(repr(ndt.time), "ndt.time")
     self.assertEqual(ndt.datetime, ndt.type(datetime.datetime))
     self.assertEqual(str(ndt.datetime), "datetime")
     self.assertEqual(repr(ndt.datetime), "ndt.datetime")
Example #29
0
 def test_datetime_types(self):
     import datetime
     self.assertEqual(ndt.date, ndt.type(datetime.date))
     self.assertEqual(str(ndt.date), "date")
     self.assertEqual(repr(ndt.date), "ndt.date")
     self.assertEqual(ndt.time, ndt.type(datetime.time))
     self.assertEqual(str(ndt.time), "time")
     self.assertEqual(repr(ndt.time), "ndt.time")
     self.assertEqual(ndt.datetime, ndt.type(datetime.datetime))
     self.assertEqual(str(ndt.datetime), "datetime")
     self.assertEqual(repr(ndt.datetime), "ndt.datetime")
Example #30
0
 def test_creation(self):
     af = nd.empty('arrfunc')
     self.assertEqual(nd.type_of(af).type_id, 'arrfunc')
     # Test there is a string version of a NULL arrfunc
     self.assertTrue(str(af) != '')
     self.assertEqual(nd.as_py(af.proto), ndt.type())
     # Test there is a string version of an initialized arrfunc
     af = _lowlevel.make_arrfunc_from_assignment(
                 ndt.float32, ndt.int64, "nocheck")
     self.assertTrue(str(af) != '')
     self.assertEqual(nd.as_py(af.proto), ndt.type("(int64) -> float32"))
 def test_ndt_type_from_numpy_dtype_struct(self):
     # aligned struct
     tp0 = ndt.type(np.dtype([('x', np.int32), ('y', np.int64)],
                         align=True))
     tp1 = ndt.type('c{x : int32, y : int64}')
     self.assertEqual(tp0, tp1)
     # unaligned struct
     tp0 = ndt.type(np.dtype([('x', np.int32), ('y', np.int64)]))
     tp1 = ndt.make_cstruct([ndt.make_unaligned(ndt.int32),
                     ndt.make_unaligned(ndt.int64)],
                     ['x', 'y'])
     self.assertEqual(tp0, tp1)
 def test__type_from_numpy_dtype_struct(self):
     # aligned struct
     tp0 = ndt.type(np.dtype([('x', np.int32), ('y', np.int64)],
                         align=True))
     tp1 = ndt.type('{x : int32, y : int64}')
     self.assertEqual(tp0, tp1)
     # unaligned struct
     tp0 = ndt.type(np.dtype([('x', np.int32), ('y', np.int64)]))
     tp1 = ndt.make_struct([ndt.make_unaligned(ndt.int32),
                     ndt.make_unaligned(ndt.int64)],
                     ['x', 'y'])
     self.assertEqual(tp0, tp1)
 def test_ragged_fromlistofiter_typepromo(self):
     # list of iterators
     vals = [[True, False], [False, 2, 3], [-10000000000],
             [True, 10, 3.125, 5.5j]]
     a = nd.array([iter(x) for x in vals])
     self.assertEqual(nd.type_of(a), ndt.type('4 * var * complex[float64]'))
     self.assertEqual(nd.as_py(a), vals)
     # list of list/iterator
     a = nd.array([[1, 2, 3], (1.5 * x for x in range(4)), iter([-1, 1])])
     self.assertEqual(nd.type_of(a), ndt.type('3 * var * float64'))
     self.assertEqual(nd.as_py(a),
                      [[1, 2, 3], [1.5 * x for x in range(4)], [-1, 1]])
Example #34
0
    def test_type_from_ctypes_carray(self):
        self.assertEqual(ndt.make_fixed_dim(10, ndt.int32),
                ndt.type(ctypes.c_int32 * 10))
        self.assertEqual(ndt.make_fixed_dim((10, 3), ndt.int32),
                ndt.type((ctypes.c_int32 * 3) * 10))
        self.assertEqual(ndt.make_fixed_dim((10, 3, 4), ndt.int32),
                ndt.type(((ctypes.c_int32 * 4) * 3) * 10))

        class POINT(ctypes.Structure):
            _fields_ = [('x', ctypes.c_int32), ('y', ctypes.c_int32)]
        self.assertEqual(ndt.make_fixed_dim(10, ndt.type(POINT)),
                ndt.type(POINT * 10))
 def test_ragged_fromiter(self):
     # Strided array of var from list of iterators
     a = nd.array([(1 + x for x in range(3)), (5 * x - 10 for x in range(5)), [2, 10]], type="strided * var * int32")
     self.assertEqual(nd.type_of(a), ndt.type("strided * var * int32"))
     self.assertEqual(nd.as_py(a), [[1, 2, 3], [-10, -5, 0, 5, 10], [2, 10]])
     # Var array of var from iterator of iterators
     a = nd.array(((2 * x for x in range(y)) for y in range(4)), type="var * var * int32")
     self.assertEqual(nd.type_of(a), ndt.type("var * var * int32"))
     self.assertEqual(nd.as_py(a), [[], [0], [0, 2], [0, 2, 4]])
     # Range of ranges
     a = nd.array(range(i) for i in range(4))
     self.assertEqual(nd.as_py(a), [list(range(i)) for i in range(4)])
Example #36
0
 def __init__(self, dyndarr):
     if nd.ndim_of(dyndarr) <= 0:
         raise IndexError('Need at least one dimension for iteration')
     self._index = 0
     self._len = len(dyndarr)
     ds = datashape.dshape(nd.dshape_of(dyndarr))
     self._dshape = ds.subarray(1)
     self._c_dtype = ndt.type(str(self._dshape))
     self._usebuffer = (ndt.type(str(ds)) != nd.type_of(dyndarr))
     self._buffer = None
     self._buffer_index = -1
     self._dyndarr = dyndarr
 def test_ndt_type_from_h5py_special(self):
     # h5py 2.3 style "special dtype"
     dt = np.dtype(object, metadata={'vlen' : str})
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(object, metadata={'vlen' : unicode})
         self.assertEqual(ndt.type(dt), ndt.string)
     # h5py 2.2 style "special dtype"
     dt = np.dtype(('O', [( ({'type': str},'vlen'), 'O' )] ))
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(('O', [( ({'type': unicode},'vlen'), 'O' )] ))
         self.assertEqual(ndt.type(dt), ndt.string)
 def test_dynamic_fromiter_int64typepromo(self):
     # Test iterator construction cases promoting from an int64
     # float64 result
     a = nd.array(iter([10000000000, 2, 3.25]))
     self.assertEqual(nd.type_of(a), ndt.type('3 * float64'))
     self.assertEqual(nd.as_py(a), [10000000000, 2, 3.25])
     # complex[float64] result
     a = nd.array(iter([10000000000, 2, 3.25j]))
     self.assertEqual(nd.type_of(a), ndt.type('3 * complex[float64]'))
     self.assertEqual(nd.as_py(a), [10000000000, 2, 3.25j])
     # Should raise an error mixing int64 and string/bytes
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, "test"]))
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, u"test"]))
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, b"test"]))
 def test_dynamic_fromiter_int64typepromo(self):
     # Test iterator construction cases promoting from an int64
     # float64 result
     a = nd.array(iter([10000000000, 2, 3.25]))
     self.assertEqual(nd.type_of(a), ndt.type('3 * float64'))
     self.assertEqual(nd.as_py(a), [10000000000, 2, 3.25])
     # complex[float64] result
     a = nd.array(iter([10000000000, 2, 3.25j]))
     self.assertEqual(nd.type_of(a), ndt.type('3 * complex[float64]'))
     self.assertEqual(nd.as_py(a), [10000000000, 2, 3.25j])
     # Should raise an error mixing int64 and string/bytes
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, "test"]))
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, u"test"]))
     self.assertRaises(TypeError, nd.array, iter([10000000000, 2, b"test"]))
Example #40
0
 def test_strided_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     self.assertEqual(nd.type_of(a), ndt.type('A * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('A * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('A * int32'))
     self.assertEqual(nd.as_py(a[0]), 0)
     self.assertEqual(nd.as_py(a[99]), 99)
     self.assertEqual(nd.as_py(a[-1]), 99)
     self.assertEqual(nd.as_py(a[-100]), 0)
     self.assertRaises(IndexError, lambda x : x[-101], a)
     self.assertRaises(IndexError, lambda x : x[100], a)
     self.assertRaises(IndexError, lambda x : x[-101:], a)
     self.assertRaises(IndexError, lambda x : x[-5:101:2], a)
Example #41
0
    def test_nested_struct_array(self):
        a = nd.array([((0, 1), 0), ((2, 2), 5), ((100, 10), 10)],
                     type='3 * {x:{a:int16, b:int16}, y:int32}')
        self.assertEqual(nd.type_of(a),
                         ndt.type('3 * {x:{a:int16, b:int16}, y:int32}'))
        self.assertEqual(nd.as_py(a.x.a), [0, 2, 100])
        self.assertEqual(nd.as_py(a.x.b), [1, 2, 10])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([{
            'x': {
                'a': 0,
                'b': 1
            },
            'y': 0
        }, {
            'x': {
                'a': 2,
                'b': 2
            },
            'y': 5
        }, {
            'x': {
                'a': 100,
                'b': 10
            },
            'y': 10
        }],
                     type='3 * {x:{a:int16, b:int16}, y:int32}')
        self.assertEqual(nd.type_of(a),
                         ndt.type('3 * {x:{a:int16, b:int16}, y:int32}'))
        self.assertEqual(nd.as_py(a.x.a), [0, 2, 100])
        self.assertEqual(nd.as_py(a.x.b), [1, 2, 10])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array(
            [[(3, ('X', 10))], [(10, ('L', 7)), (12, ('M', 5))]],
            type=
            '2 * var * {count:int32, size:{name:fixed_string[1,"A"], id: int8}}'
        )
        self.assertEqual(
            nd.type_of(a),
            ndt.type(
                '2 * var * {count:int32, size:{name:fixed_string[1,"A"], id: int8}}'
            ))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size.name), [['X'], ['L', 'M']])
        self.assertEqual(nd.as_py(a.size.id), [[10], [7, 5]])
 def test_ragged_fromiter(self):
     # Strided array of var from list of iterators
     a = nd.array([(1 + x for x in range(3)),
                   (5 * x - 10 for x in range(5)), [2, 10]],
                  type='Fixed * var * int32')
     self.assertEqual(nd.type_of(a), ndt.type('3 * var * int32'))
     self.assertEqual(nd.as_py(a),
                      [[1, 2, 3], [-10, -5, 0, 5, 10], [2, 10]])
     # Var array of var from iterator of iterators
     a = nd.array(((2 * x for x in range(y)) for y in range(4)),
                  type='var * var * int32')
     self.assertEqual(nd.type_of(a), ndt.type('var * var * int32'))
     self.assertEqual(nd.as_py(a), [[], [0], [0, 2], [0, 2, 4]])
     # Range of ranges
     a = nd.array(range(i) for i in range(4))
     self.assertEqual(nd.as_py(a), [list(range(i)) for i in range(4)])
Example #43
0
 def test_simple(self):
     a = nd.array([1, 2, 3])
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     # Modifying 'a' shouldn't affect 'b', because it's a copy
     b = nd.array(a)
     a[1] = 10
     self.assertEqual(nd.as_py(b), [1, 2, 3])
Example #44
0
def data_descriptor_from_cffi(ffi, cdata, writable):
    """
    Parameters
    ----------
    ffi : cffi.FFI
        The cffi namespace which contains the cdata.
    cdata : cffi.CData
        The cffi data object which owns the data.
    writable : bool
        Should be true if the data is writable, flase
        if it's read-only.
    """
    if not isinstance(cdata, ffi.CData):
        raise TypeError('object is not a cffi.CData object, has type %s' %
                        type(cdata))
    owner = (ffi, cdata)
    # Get the raw pointer out of the cdata as an integer
    ptr = int(ffi.cast('uintptr_t', ffi.cast('char *', cdata)))
    ds = datashape.from_cffi(ffi, ffi.typeof(cdata))
    if (isinstance(ds, datashape.DataShape)
            and isinstance(ds[0], datashape.TypeVar)):
        # If the outermost dimension is an array without fixed
        # size, get its size from the data
        ds = datashape.DataShape(*(datashape.Fixed(len(cdata)), ) + ds[1:])
    access = "readwrite" if writable else "readonly"
    dyndarr = _lowlevel.array_from_ptr(ndt.type(str(ds)), ptr, owner, access)
    return DyNDDataDescriptor(dyndarr)
 def test_fixed_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('1 * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x: x[-101], a)
     self.assertRaises(IndexError, lambda x: x[100], a)
Example #46
0
 def test_type_from_ctype_struct(self):
     class POINT(ctypes.Structure):
         _fields_ = [('x', ctypes.c_int32), ('y', ctypes.c_int32)]
     self.assertEqual(ndt.make_struct(
                             [ndt.int32, ndt.int32],['x', 'y']),
                     ndt.type(POINT))
     class DATA(ctypes.Structure):
         _fields_ = [
                     ('pos', POINT),
                     ('flags', ctypes.c_int8),
                     ('size', ctypes.c_float),
                     ('vel', POINT)
                    ]
     self.assertEqual(ndt.make_struct([POINT, ndt.int8, ndt.float32, POINT],
                             ['pos', 'flags', 'size', 'vel']),
                     ndt.type(DATA))
Example #47
0
 def test_fixed_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('1 * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x : x[-101], a)
     self.assertRaises(IndexError, lambda x : x[100], a)
Example #48
0
 def dynd_arr(self):
     from ..io.client import requests
     """Downloads the data and returns a local in-memory nd.array"""
     # TODO: Need binary serialization
     j = requests.get_remote_json(self.url)
     tp = ndt.type(str(self.dshape))
     return nd.parse_json(tp, j)
 def test_ragged_initial_empty_typepromo(self):
     # iterator of lists, first one is empty
     vals = [[],
             [False, 2, 3]]
     a = nd.array(iter(x) for x in vals)
     self.assertEqual(nd.type_of(a), ndt.type('2 * var * int32'))
     self.assertEqual(nd.as_py(a), vals)
    def test_simple(self):
        a = nd.asarray([1, 2, 3], access='rw')
        self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))

        # Modifying 'a' should affect 'b', because it's a view
        b = nd.asarray(a)
        self.assertEqual(nd.as_py(b), [1, 2, 3])
        a[1] = 10
        self.assertEqual(nd.as_py(b), [1, 10, 3])

        # Can take a readonly view, but still modify the original
        b = nd.asarray(a, access='r')
        self.assertEqual(nd.as_py(b), [1, 10, 3])
        a[1] = 20
        self.assertEqual(nd.as_py(b), [1, 20, 3])
        # The readonly view we took can't be written to
        def assign_at(x, i, y):
            x[i] = y
        self.assertRaises(RuntimeError, assign_at, b, 1, 30)

        # Asking for immutable makes a copy instead of a view
        b = nd.asarray(a, access='immutable')
        self.assertEqual(nd.as_py(b), [1, 20, 3])
        a[1] = 40
        self.assertEqual(nd.as_py(b), [1, 20, 3])

        # Asking for immutable from a non-immutable
        # readonly array makes a copy
        aprime = nd.asarray(a, access='r')
        b = nd.asarray(aprime, access='immutable')
        self.assertEqual(nd.as_py(aprime), [1, 40, 3])
        self.assertEqual(nd.as_py(b), [1, 40, 3])
        a[1] = 50
        self.assertEqual(nd.as_py(aprime), [1, 50, 3])
        self.assertEqual(nd.as_py(b), [1, 40, 3])
Example #51
0
 def test_symbolic_type(self):
     tp = ndt.type('(int, real) -> complex')
     self.assertEqual(tp.type_id, 'callable')
     self.assertEqual(nd.as_py(tp.pos_types), [ndt.int32, ndt.float64])
     #        self.assertEqual(tp.return_type, ndt.complex_float64)
     tp = ndt.type('MyType')
     #      self.assertEqual(tp.type_id, 'typevar')
     #       self.assertEqual(tp.name, 'MyType')
     tp = ndt.type('MyDim * int')
     self.assertEqual(tp.type_id, 'typevar_dim')
     #     self.assertEqual(tp.name, 'MyDim')
     #    self.assertEqual(tp.element_type, ndt.int32)
     tp = ndt.type('... * int')
     self.assertEqual(tp.type_id, 'ellipsis_dim')
     #   self.assertEqual(tp.element_type, ndt.int32)
     tp = ndt.type('MyEll... * int')
     self.assertEqual(tp.type_id, 'ellipsis_dim')
Example #52
0
 def test_empty_array_dtype(self):
     a = nd.array([], type=ndt.make_fixed_dim(0, ndt.int64))
     self.assertEqual(nd.type_of(a), ndt.type('0 * int64'))
     self.assertEqual(a.shape, (0, ))
     #        Todo: Need to reenable this failing test
     #        a = nd.array([], dtype='Fixed * float64')
     #        self.assertEqual(nd.type_of(a), ndt.type('0 * float64'))
     #        self.assertEqual(a.shape, (0,))
     a = nd.array([], type='var * int16')
     self.assertEqual(nd.type_of(a), ndt.type('var * int16'))
     self.assertEqual(len(a), 0)
     a = nd.array([], type='0 * int16')
     self.assertEqual(nd.type_of(a), ndt.type('0 * int16'))
     self.assertEqual(len(a), 0)
     a = nd.array([], type='0 * 3 * int16')
     self.assertEqual(nd.type_of(a), ndt.type('0 * 3 * int16'))
     self.assertEqual(a.shape, (0, 3))
 def check_constructor_readwrite(self, cons, value):
     # Constructor from scalar type
     a = cons(ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.int32)
     self.assertEqual(nd.as_py(a), value)
     # Constructor from type with fixed dimension
     a = cons('3 * int32', access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_fixed_dim(3, ndt.int32))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as single integer
     a = cons(3, ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [value] * 3)
     # Constructor from shape as tuple
     a = cons((3, 4), ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor from shape as variadic arguments
     a = cons(3, 4, ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * 4 * int32'))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor of a struct type
     a = cons(3, '{x: int32, y: int32}', access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.type('3 * {x: int32, y: int32}'))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [{'x': value, 'y': value}] * 3)
     # Constructor of a struct type
     a = cons(3, ndt.make_struct([ndt.int32] * 2, ['x', 'y']), access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(
         nd.type_of(a),
         ndt.make_fixed_dim(3, ndt.make_struct([ndt.int32] * 2,
                                               ['x', 'y'])))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), [{'x': value, 'y': value}] * 3)
Example #54
0
    def test_single_struct_array(self):
        a = nd.array([(0, 0), (3, 5), (12, 10)], type='3 * {x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([{
            'x': 0,
            'y': 0
        }, {
            'x': 3,
            'y': 5
        }, {
            'x': 12,
            'y': 10
        }],
                     type='3 * {x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([[(3, 'X')], [(10, 'L'), (12, 'M')]],
                     type='2 * var * {count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(
            nd.type_of(a),
            ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])

        a = nd.array([[{
            'count': 3,
            'size': 'X'
        }], [{
            'count': 10,
            'size': 'L'
        }, {
            'count': 12,
            'size': 'M'
        }]],
                     type='2 * var * {count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(
            nd.type_of(a),
            ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])
 def test_empty(self):
     # A fixed dimension of non-zero size gets pushed down
     a = nd.array([], dtype='3 * int32')
     self.assertEqual(nd.type_of(a), ndt.type('0 * 3 * int32'))
     self.assertEqual(nd.as_py(a), [])
     # A fixed dimension of zero size gets absorbed
     a = nd.array([], dtype='0 * int32')
     self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     self.assertEqual(nd.as_py(a), [])
     # A symbolic fixed dimension gets absorbed
     #       Todo: Need to reenable this failing test
     #        a = nd.array([], dtype='Fixed * int32')
     #        self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     #       self.assertEqual(nd.as_py(a), [])
     # A var dimension gets absorbed
     a = nd.array([], dtype='var * int32')
     self.assertEqual(nd.type_of(a), ndt.type('var * int32'))
     self.assertEqual(nd.as_py(a), [])
 def test_var_dim(self):
     # TODO: Reenable tests below when var dim slicing is implemented properly
     a = nd.empty('var * int32')
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[:]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     # self.assertEqual(nd.type_of(a[0:1]), ndt.type('fixed * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     #self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     #self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x: x[-101], a)
     self.assertRaises(IndexError, lambda x: x[100], a)
Example #57
0
 def test_scalar_types(self):
     self.assertEqual(ndt.bool, ndt.type(bool))
     self.assertEqual(ndt.int32, ndt.type(int))
     self.assertEqual(ndt.float64, ndt.type(float))
     self.assertEqual(ndt.complex_float64, ndt.type(complex))
     self.assertEqual(ndt.string, ndt.type(str))
     self.assertEqual(ndt.bytes, ndt.type(bytearray))
     if sys.version_info[0] == 2:
         self.assertEqual(ndt.string, ndt.type(unicode))
     if sys.version_info[0] >= 3:
         self.assertEqual(ndt.bytes, ndt.type(bytes))
Example #58
0
    def test_single_struct(self):
        a = nd.array([12, 'test', True], type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a),
                         ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        a = nd.array({
            'x': 12,
            'y': 'test',
            'z': True
        },
                     type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a),
                         ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)
    def test_numpy_dynd_fixed_string_interop(self):
        # Tests converting fixed-size string arrays to/from numpy
        # ASCII Numpy -> dynd
        a = np.array(['abc', 'testing', 'array'])
        b = nd.view(a)
        if sys.version_info >= (3, 0):
            self.assertEqual(ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b))
        else:
            self.assertEqual(ndt.make_fixed_string(7, 'ascii'), nd.dtype_of(b))
        self.assertEqual(nd.dtype_of(b), ndt.type(a.dtype))

        # Make sure it's ascii
        a = a.astype('S7')
        b = nd.view(a)

        # ASCII dynd -> Numpy
        c = np.asarray(b)
        self.assertEqual(a.dtype, c.dtype)
        assert_array_equal(a, c)
        # verify 'a' and 'c' are looking at the same data
        a[1] = 'modify'
        assert_array_equal(a, c)

        # ASCII dynd -> UTF32 dynd
        b_u = b.ucast(ndt.make_fixed_string(7, 'utf_32'))
        self.assertEqual(
                ndt.make_convert(
                    ndt.make_fixed_string(7, 'utf_32'),
                    ndt.make_fixed_string(7, 'ascii')),
                nd.dtype_of(b_u))
        # Evaluate to its value array
        b_u = b_u.eval()
        self.assertEqual(
                ndt.make_fixed_string(7, 'utf_32'),
                nd.dtype_of(b_u))

        # UTF32 dynd -> Numpy
        c_u = np.asarray(b_u)
        self.assertEqual(nd.dtype_of(b_u), ndt.type(c_u.dtype))
        assert_array_equal(a.astype('U'), c_u)
        # 'a' and 'c_u' are not looking at the same data
        a[1] = 'diff'
        self.assertFalse(np.all(a == c_u))