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))
예제 #2
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))
예제 #3
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))
예제 #4
0
 def test_fixed_var(self):
     a = nd.array(
         [[(1, 2, 'a', 'b'), (3, 4, 'ab', 'cd')], [(5, 6, 'def', 'ghi')],
          [(7, 8, 'alpha', 'beta'), (9, 10, 'X', 'Y'),
           (11, 12, 'the', 'end')]],
         type='3 * var * {x: int32, y: int32, z: string, w: string}')
     # Selecting a single field
     b = nd.fields(a, 'x')
     self.assertEqual(
         nd.type_of(b),
         ndt.make_fixed_dim(
             3, ndt.make_var_dim(ndt.make_struct([ndt.int32], ['x']))))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     # Selecting two fields
     b = nd.fields(a, 'z', 'y')
     self.assertEqual(
         nd.type_of(b),
         ndt.make_fixed_dim(
             3,
             ndt.make_var_dim(
                 ndt.make_struct([ndt.string, ndt.int32], ['z', 'y']))))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     # Selecting three fields
     b = nd.fields(a, 'w', 'y', 'z')
     self.assertEqual(
         nd.type_of(b),
         ndt.make_fixed_dim(
             3,
             ndt.make_var_dim(
                 ndt.make_struct([ndt.string, ndt.int32, ndt.string],
                                 ['w', 'y', 'z']))))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     # Reordering all four fields
     b = nd.fields(a, 'w', 'y', 'x', 'z')
     self.assertEqual(
         nd.type_of(b),
         ndt.make_fixed_dim(
             3,
             ndt.make_var_dim(
                 ndt.make_struct(
                     [ndt.string, ndt.int32, ndt.int32, ndt.string],
                     ['w', 'y', 'x', 'z']))))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
예제 #5
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)
예제 #6
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)
예제 #7
0
 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)
예제 #8
0
 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)
 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 cfixed dimension
     a = nd.empty("cfixed[3] * int32")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.make_cfixed_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,))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, "3 * int32", access="immutable")
     # 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.make_strided_dim(ndt.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("strided * strided * 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("strided * strided * 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("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, 3, 4, ndt.int32, access="immutable")
예제 #10
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 cfixed dimension
     a = nd.empty('cfixed[3] * int32')
     self.assertEqual(a.access_flags, 'readwrite')
     self.assertEqual(nd.type_of(a), ndt.make_cfixed_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,))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, '3 * int32', access='immutable')
     # 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))
     # Can't create with access as immutable
     self.assertRaises(ValueError, nd.empty, 3, 4, ndt.int32, access='immutable')
예제 #11
0
 def test_fixed_var(self):
     a = nd.array([
             [(1, 2, 'a', 'b'),
              (3, 4, 'ab', 'cd')],
             [(5, 6, 'def', 'ghi')],
             [(7, 8, 'alpha', 'beta'),
              (9, 10, 'X', 'Y'),
              (11, 12, 'the', 'end')]],
             type='3 * var * {x: int32, y: int32, z: string, w: string}')
     # Selecting a single field
     b = nd.fields(a, 'x')
     self.assertEqual(nd.type_of(b), ndt.make_fixed_dim(3,
                                 ndt.make_var_dim(ndt.make_struct(
                     [ndt.int32],
                     ['x']))))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     # Selecting two fields
     b = nd.fields(a, 'z', 'y')
     self.assertEqual(nd.type_of(b), ndt.make_fixed_dim(3,
                                 ndt.make_var_dim(ndt.make_struct(
                     [ndt.string, ndt.int32],
                     ['z', 'y']))))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     # Selecting three fields
     b = nd.fields(a, 'w', 'y', 'z')
     self.assertEqual(nd.type_of(b), ndt.make_fixed_dim(3,
                                 ndt.make_var_dim(ndt.make_struct(
                     [ndt.string, ndt.int32, ndt.string],
                     ['w', 'y', 'z']))))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     # Reordering all four fields
     b = nd.fields(a, 'w', 'y', 'x', 'z')
     self.assertEqual(nd.type_of(b), ndt.make_fixed_dim(3,
                                 ndt.make_var_dim(ndt.make_struct(
                     [ndt.string, ndt.int32, ndt.int32, ndt.string],
                     ['w', 'y', 'x', 'z']))))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
예제 #12
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         return
         # This needs to be fixed for Python 3
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"], type=ndt.make_fixed_dim(2, ndt.bytes))
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
예제 #13
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         return
         # This needs to be fixed for Python 3
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"],
                      type=ndt.make_fixed_dim(2, ndt.bytes))
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
예제 #14
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))
예제 #15
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))
예제 #16
0
 def test_full_of_struct(self):
     # Constructor of a struct type
     a = nd.full(3, '{x: int32, y: int32}', value=[1,5], 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': 1, 'y': 5}]*3)
     # Constructor of a struct type
     a = nd.full(3, ndt.make_struct([ndt.int32]*2, ['x', 'y']),
                 value={'x' : 3, 'y' : 10}, 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': 3, 'y': 10}]*3)
예제 #17
0
    def write_single(self, idx, ptr, count=1):
        if len(idx) != self.nindex:
            raise IndexError('Incorrect number of indices (got %d, require %d)' %
                           (len(idx), self.nindex))

        idx = tuple(operator.index(i) for i in idx)
        c_dtype = self._c_dtype

        if count != 1:
            idx = idx[:-1] + (slice(idx[-1], idx[-1]+count),)
            c_dtype = ndt.make_fixed_dim(count, c_dtype)

        # Create a temporary DyND array around the ptr data.
        # Note that we can't provide an owner because the parameter
        # is just the bare pointer.
        tmp = _lowlevel.array_from_ptr(c_dtype, ptr,
                        None, 'readonly')
        # Use DyND's assignment to set the values
        self._dyndarr[idx] = tmp
예제 #18
0
 def read_single(self, idx, count=1):
     idxlen = len(idx)
     if idxlen != self.nindex:
         raise IndexError('Incorrect number of indices (got %d, require %d)' %
                        (len(idx), self.nindex))
     idx = tuple(operator.index(i) for i in idx)
     if count == 1:
         x = self._dyndarr[idx]
         # Make it C-contiguous and in native byte order
         x = x.cast(self._c_dtype).eval()
         self._tmpbuffer = x
         return _lowlevel.data_address_of(x)
     else:
         idx = idx[:-1] + (slice(idx[-1], idx[-1]+count),)
         x = self._dyndarr[idx]
         # Make it C-contiguous and in native byte order
         x = x.cast(ndt.make_fixed_dim(count, self._c_dtype)).eval()
         self._tmpbuffer = x
         return _lowlevel.data_address_of(x)
 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.make_strided_dim(ndt.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("strided * strided * 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("strided * strided * int32"))
     self.assertEqual(a.shape, (3, 4))
     self.assertEqual(nd.as_py(a), [[value] * 4] * 3)
     # Constructor of a cstruct type
     a = cons(3, "{x: int32, y: int32}", access="rw")
     self.assertEqual(a.access_flags, "readwrite")
     self.assertEqual(nd.type_of(a), ndt.type("strided * {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_strided_dim(ndt.make_struct([ndt.int32] * 2, ["x", "y"])))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), [{"x": value, "y": value}] * 3)
예제 #20
0
def load_json_directory_array(root, array_name):
    # Load the datashape
    dsfile = root + ".datashape"
    if not path.isfile(dsfile):
        raise Exception("No datashape file found for array %s" % array_name)
    with open(dsfile) as f:
        dt = ndt.type(f.read())

    # Scan for JSON files, assuming they're just #.json
    # Sort them numerically
    files = sorted([(int(path.splitext(path.basename(x))[0]), x) for x in glob.glob(path.join(root, "*.json"))])
    files = [x[1] for x in files]
    # Make an array with an extra fixed dimension, then
    # read a JSON file into each element of that array
    dt = ndt.make_fixed_dim(len(files), dt)
    arr = nd.empty(dt)
    for i, fname in enumerate(files):
        nd.parse_json(arr[i], nd.memmap(fname))
    arr.flag_as_immutable()
    return array(arr)
예제 #21
0
 def test_full_of_struct(self):
     # Constructor of a struct type
     a = nd.full(3, '{x: int32, y: int32}', value=[1, 5], 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': 1, 'y': 5}] * 3)
     # Constructor of a struct type
     a = nd.full(3,
                 ndt.make_struct([ndt.int32] * 2, ['x', 'y']),
                 value={
                     'x': 3,
                     'y': 10
                 },
                 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': 3, 'y': 10}] * 3)
예제 #22
0
    def buffered_ptr(self, idx, count=1):
        if len(idx) != self.nindex:
            raise IndexError('Incorrect number of indices (got %d, require %d)' %
                           (len(idx), self.nindex))

        idx = tuple(operator.index(i) for i in idx)
        c_dtype = self._c_dtype

        if count != 1:
            idx = idx[:-1] + (slice(idx[-1], idx[-1]+count),)
            c_dtype = ndt.make_fixed_dim(count, c_dtype)

        dst_arr = self._dyndarr[idx]
        buf_arr = dst_arr.cast(c_dtype).eval()
        buf_ptr = _lowlevel.data_address_of(buf_arr)
        if buf_ptr == _lowlevel.data_address_of(dst_arr):
            # If no buffering is needed, just return the pointer
            return buffered_ptr_ctxmgr(buf_ptr, None)
        else:
            def buffer_flush():
                dst_arr[...] = buf_arr
            return buffered_ptr_ctxmgr(buf_ptr, buffer_flush)
예제 #23
0
    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.cast(ndt.make_fixed_dim(3, ndt.make_fixed_string(7, 'utf_32')))
        # Evaluate to its value array
        b_u = b_u
        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))
예제 #24
0
def load_json_file_list_array(root, array_name):
    # Load the datashape
    dsfile = root + '.datashape'
    if not path.isfile(dsfile):
        raise Exception('No datashape file found for array %s' % array_name)
    with open(dsfile) as f:
        dt = ndt.type(f.read())

    # Scan for JSON files -- no assumption on file suffix

    #open list of files and load into python list
    files = root + '.files'
    with open(files) as f:
        l_files = [fs.strip() for fs in f]

    # Make an array with an extra fixed dimension, then
    # read a JSON file into each element of that array
    dt = ndt.make_fixed_dim(len(l_files), dt)
    arr = nd.empty(dt)
    for i, fname in enumerate(l_files):
        with open(fname) as f:
            nd.parse_json(arr[i], f.read())
    arr.flag_as_immutable()
    return array(arr)
예제 #25
0
 def test_fixed_array(self):
     # Tests of datashapes that produce the DyND fixed array type
     self.assertEqual(ndt.make_fixed_dim(3, ndt.int32),
                     ndt.type('3 * int32'))
     self.assertEqual(ndt.make_fixed_dim((5, 2), ndt.float64),
                     ndt.type('5 * 2 * float64'))
예제 #26
0
 def test_string_assign_to_slice(self):
     a = nd.array(['a', 'b', 'c', 'd', 'e'], type = ndt.make_fixed_dim(5, 'fixed_string[8]'))
     a[:3] = 'test'
     self.assertEqual(nd.as_py(a), ['test', 'test', 'test', 'd', 'e'])
예제 #27
0
 def test_unicode_array(self):
     a = nd.array([u'this', 'is', u'a', 'test'],
                     type=ndt.make_fixed_dim(4, ndt.string))
     self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
     self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
예제 #28
0
 def test_unicode_array(self):
     a = nd.array([u'this', 'is', u'a', 'test'],
                  type=ndt.make_fixed_dim(4, ndt.string))
     self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
     self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
예제 #29
0
 def test_string_assign_to_slice(self):
     a = nd.array(['a', 'b', 'c', 'd', 'e'],
                  type=ndt.make_fixed_dim(5, 'fixed_string[8]'))
     a[:3] = 'test'
     self.assertEqual(nd.as_py(a), ['test', 'test', 'test', 'd', 'e'])