def test_dynd_view_of_numpy_array(self):
        # Tests viewing a numpy array as a dynd.array
        nonnative = self.nonnative

        a = np.arange(10, dtype=np.int32)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.int32)
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(12, dtype=(nonnative + 'i4')).reshape(3,4)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_byteswap(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=np.int32).reshape(4,3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_unaligned(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=(nonnative + 'i4')).reshape(2,2,3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n),
                ndt.make_unaligned(ndt.make_byteswap(ndt.int32)))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)
 def test_ndt_type_as_numpy(self):
     self.assertEqual(ndt.bool.as_numpy(), np.dtype('bool'))
     self.assertEqual(ndt.int8.as_numpy(), np.dtype('int8'))
     self.assertEqual(ndt.int16.as_numpy(), np.dtype('int16'))
     self.assertEqual(ndt.int32.as_numpy(), np.dtype('int32'))
     self.assertEqual(ndt.int64.as_numpy(), np.dtype('int64'))
     self.assertEqual(ndt.uint8.as_numpy(), np.dtype('uint8'))
     self.assertEqual(ndt.uint16.as_numpy(), np.dtype('uint16'))
     self.assertEqual(ndt.uint32.as_numpy(), np.dtype('uint32'))
     self.assertEqual(ndt.uint64.as_numpy(), np.dtype('uint64'))
     self.assertEqual(ndt.float32.as_numpy(), np.dtype('float32'))
     self.assertEqual(ndt.float64.as_numpy(), np.dtype('float64'))
     self.assertEqual(ndt.complex_float32.as_numpy(), np.dtype('complex64'))
     self.assertEqual(ndt.complex_float64.as_numpy(), np.dtype('complex128'))
     # nonnative byte order
     nonnative = self.nonnative
     self.assertEqual(ndt.make_byteswap(ndt.int16).as_numpy(),
                 np.dtype(nonnative + 'i2'))
     self.assertEqual(ndt.make_byteswap(ndt.float64).as_numpy(),
                 np.dtype(nonnative + 'f8'))
     # aligned struct
     tp0 = ndt.type('c{x : int32, y : int64}').as_numpy()
     tp1 = np.dtype([('x', np.int32), ('y', np.int64)], align=True)
     self.assertEqual(tp0, tp1)
     # unaligned struct
     tp0 = ndt.make_cstruct([ndt.make_unaligned(ndt.int32),
                     ndt.make_unaligned(ndt.int64)],
                     ['x', 'y']).as_numpy()
     tp1 = np.dtype([('x', np.int32), ('y', np.int64)])
     self.assertEqual(tp0, tp1)
     # check some types which can't be converted
     self.assertRaises(TypeError, ndt.date.as_numpy)
     self.assertRaises(TypeError, ndt.datetime.as_numpy)
     self.assertRaises(TypeError, ndt.bytes.as_numpy)
     self.assertRaises(TypeError, ndt.string.as_numpy)
예제 #3
0
    def test_dynd_view_of_numpy_array(self):
        # Tests viewing a numpy array as a dynd.array
        nonnative = self.nonnative

        a = np.arange(10, dtype=np.int32)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.int32)
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(12, dtype=(nonnative + 'i4')).reshape(3, 4)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_byteswap(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=np.int32).reshape(4, 3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_unaligned(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=(nonnative + 'i4')).reshape(2, 2, 3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n),
                         ndt.make_unaligned(ndt.make_byteswap(ndt.int32)))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)
예제 #4
0
    def test__type_from_numpy_dtype(self):
        # Tests converting numpy dtypes to dynd types
        # native byte order
        self.assertEqual(ndt.bool, ndt.type(np.dtype(np.bool)))
        self.assertEqual(ndt.int8, ndt.type(np.dtype(np.int8)))
        self.assertEqual(ndt.int16, ndt.type(np.dtype(np.int16)))
        self.assertEqual(ndt.int32, ndt.type(np.dtype(np.int32)))
        self.assertEqual(ndt.int64, ndt.type(np.dtype(np.int64)))
        self.assertEqual(ndt.uint8, ndt.type(np.dtype(np.uint8)))
        self.assertEqual(ndt.uint16, ndt.type(np.dtype(np.uint16)))
        self.assertEqual(ndt.uint32, ndt.type(np.dtype(np.uint32)))
        self.assertEqual(ndt.uint64, ndt.type(np.dtype(np.uint64)))
        self.assertEqual(ndt.float32, ndt.type(np.dtype(np.float32)))
        self.assertEqual(ndt.float64, ndt.type(np.dtype(np.float64)))
        self.assertEqual(ndt.complex_float32, ndt.type(np.dtype(np.complex64)))
        self.assertEqual(ndt.complex_float64,
                         ndt.type(np.dtype(np.complex128)))
        self.assertEqual(ndt.make_fixed_string(10, 'ascii'),
                         ndt.type(np.dtype('S10')))
        self.assertEqual(ndt.make_fixed_string(10, 'utf_32'),
                         ndt.type(np.dtype('U10')))

        # non-native byte order
        nonnative = self.nonnative

        self.assertEqual(ndt.make_byteswap(ndt.int16),
                         ndt.type(np.dtype(nonnative + 'i2')))
        self.assertEqual(ndt.make_byteswap(ndt.int32),
                         ndt.type(np.dtype(nonnative + 'i4')))
        self.assertEqual(ndt.make_byteswap(ndt.int64),
                         ndt.type(np.dtype(nonnative + 'i8')))
        self.assertEqual(ndt.make_byteswap(ndt.uint16),
                         ndt.type(np.dtype(nonnative + 'u2')))
        self.assertEqual(ndt.make_byteswap(ndt.uint32),
                         ndt.type(np.dtype(nonnative + 'u4')))
        self.assertEqual(ndt.make_byteswap(ndt.uint64),
                         ndt.type(np.dtype(nonnative + 'u8')))
        self.assertEqual(ndt.make_byteswap(ndt.float32),
                         ndt.type(np.dtype(nonnative + 'f4')))
        self.assertEqual(ndt.make_byteswap(ndt.float64),
                         ndt.type(np.dtype(nonnative + 'f8')))
        self.assertEqual(ndt.make_byteswap(ndt.complex_float32),
                         ndt.type(np.dtype(nonnative + 'c8')))
        self.assertEqual(ndt.make_byteswap(ndt.complex_float64),
                         ndt.type(np.dtype(nonnative + 'c16')))
    def test_ndt_type_from_numpy_dtype(self):
        # Tests converting numpy dtypes to dynd types
        # native byte order
        self.assertEqual(ndt.bool, ndt.type(np.dtype(np.bool)))
        self.assertEqual(ndt.int8, ndt.type(np.dtype(np.int8)))
        self.assertEqual(ndt.int16, ndt.type(np.dtype(np.int16)))
        self.assertEqual(ndt.int32, ndt.type(np.dtype(np.int32)))
        self.assertEqual(ndt.int64, ndt.type(np.dtype(np.int64)))
        self.assertEqual(ndt.uint8, ndt.type(np.dtype(np.uint8)))
        self.assertEqual(ndt.uint16, ndt.type(np.dtype(np.uint16)))
        self.assertEqual(ndt.uint32, ndt.type(np.dtype(np.uint32)))
        self.assertEqual(ndt.uint64, ndt.type(np.dtype(np.uint64)))
        self.assertEqual(ndt.float32, ndt.type(np.dtype(np.float32)))
        self.assertEqual(ndt.float64, ndt.type(np.dtype(np.float64)))
        self.assertEqual(ndt.complex_float32, ndt.type(np.dtype(np.complex64)))
        self.assertEqual(ndt.complex_float64, ndt.type(np.dtype(np.complex128)))
        self.assertEqual(ndt.make_fixedstring(10, 'ascii'),
                    ndt.type(np.dtype('S10')))
        self.assertEqual(ndt.make_fixedstring(10, 'utf_32'),
                    ndt.type(np.dtype('U10')))

        # non-native byte order
        nonnative = self.nonnative

        self.assertEqual(ndt.make_byteswap(ndt.int16),
                ndt.type(np.dtype(nonnative + 'i2')))
        self.assertEqual(ndt.make_byteswap(ndt.int32),
                ndt.type(np.dtype(nonnative + 'i4')))
        self.assertEqual(ndt.make_byteswap(ndt.int64),
                ndt.type(np.dtype(nonnative + 'i8')))
        self.assertEqual(ndt.make_byteswap(ndt.uint16),
                ndt.type(np.dtype(nonnative + 'u2')))
        self.assertEqual(ndt.make_byteswap(ndt.uint32),
                ndt.type(np.dtype(nonnative + 'u4')))
        self.assertEqual(ndt.make_byteswap(ndt.uint64),
                ndt.type(np.dtype(nonnative + 'u8')))
        self.assertEqual(ndt.make_byteswap(ndt.float32),
                ndt.type(np.dtype(nonnative + 'f4')))
        self.assertEqual(ndt.make_byteswap(ndt.float64),
                ndt.type(np.dtype(nonnative + 'f8')))
        self.assertEqual(ndt.make_byteswap(ndt.complex_float32),
                ndt.type(np.dtype(nonnative + 'c8')))
        self.assertEqual(ndt.make_byteswap(ndt.complex_float64),
                ndt.type(np.dtype(nonnative + 'c16')))
예제 #6
0
    def test__type_as_numpy(self):
        self.assertEqual(ndt.bool.as_numpy(), np.dtype('bool'))
        self.assertEqual(ndt.int8.as_numpy(), np.dtype('int8'))
        self.assertEqual(ndt.int16.as_numpy(), np.dtype('int16'))
        self.assertEqual(ndt.int32.as_numpy(), np.dtype('int32'))
        self.assertEqual(ndt.int64.as_numpy(), np.dtype('int64'))
        self.assertEqual(ndt.uint8.as_numpy(), np.dtype('uint8'))
        self.assertEqual(ndt.uint16.as_numpy(), np.dtype('uint16'))
        self.assertEqual(ndt.uint32.as_numpy(), np.dtype('uint32'))
        self.assertEqual(ndt.uint64.as_numpy(), np.dtype('uint64'))
        self.assertEqual(ndt.float32.as_numpy(), np.dtype('float32'))
        self.assertEqual(ndt.float64.as_numpy(), np.dtype('float64'))
        self.assertEqual(ndt.complex_float32.as_numpy(), np.dtype('complex64'))
        self.assertEqual(ndt.complex_float64.as_numpy(),
                         np.dtype('complex128'))
        # nonnative byte order
        nonnative = self.nonnative
        self.assertEqual(
            ndt.make_byteswap(ndt.int16).as_numpy(),
            np.dtype(nonnative + 'i2'))
        self.assertEqual(
            ndt.make_byteswap(ndt.float64).as_numpy(),
            np.dtype(nonnative + 'f8'))
        """
        TODO: This test fails since we changed cstruct -> struct.

        # aligned struct
        tp0 = ndt.type('{x : int32, y : int64}').as_numpy()
        tp1 = np.dtype([('x', np.int32), ('y', np.int64)], align=True)
        self.assertEqual(tp0, tp1)
        # unaligned struct
        tp0 = ndt.make_struct([ndt.make_unaligned(ndt.int32),
                        ndt.make_unaligned(ndt.int64)],
                        ['x', 'y']).as_numpy()
        tp1 = np.dtype([('x', np.int32), ('y', np.int64)])
        self.assertEqual(tp0, tp1)
        """

        # check some types which can't be converted
        self.assertRaises(TypeError, ndt.date.as_numpy)
        self.assertRaises(TypeError, ndt.datetime.as_numpy)
        self.assertRaises(TypeError, ndt.bytes.as_numpy)
        self.assertRaises(TypeError, ndt.string.as_numpy)
예제 #7
0
 def test_type_id(self):
     # Numeric type id
     self.assertEqual(self.type_id_of(ndt.bool),
                     _lowlevel.type_id.BOOL)
     self.assertEqual(self.type_id_of(ndt.int8),
                     _lowlevel.type_id.INT8)
     self.assertEqual(self.type_id_of(ndt.int16),
                     _lowlevel.type_id.INT16)
     self.assertEqual(self.type_id_of(ndt.int32),
                     _lowlevel.type_id.INT32)
     self.assertEqual(self.type_id_of(ndt.int64),
                     _lowlevel.type_id.INT64)
     self.assertEqual(self.type_id_of(ndt.uint8),
                     _lowlevel.type_id.UINT8)
     self.assertEqual(self.type_id_of(ndt.uint16),
                     _lowlevel.type_id.UINT16)
     self.assertEqual(self.type_id_of(ndt.uint32),
                     _lowlevel.type_id.UINT32)
     self.assertEqual(self.type_id_of(ndt.uint64),
                     _lowlevel.type_id.UINT64)
     self.assertEqual(self.type_id_of(ndt.float32),
                     _lowlevel.type_id.FLOAT32)
     self.assertEqual(self.type_id_of(ndt.float64),
                     _lowlevel.type_id.FLOAT64)
     self.assertEqual(self.type_id_of(ndt.complex_float32),
                     _lowlevel.type_id.COMPLEX_FLOAT32)
     self.assertEqual(self.type_id_of(ndt.complex_float64),
                     _lowlevel.type_id.COMPLEX_FLOAT64)
     # String/bytes
     self.assertEqual(self.type_id_of(ndt.string),
                     _lowlevel.type_id.STRING)
     self.assertEqual(self.type_id_of(ndt.make_fixedstring(16)),
                     _lowlevel.type_id.FIXEDSTRING)
     self.assertEqual(self.type_id_of(ndt.bytes),
                     _lowlevel.type_id.BYTES)
     self.assertEqual(self.type_id_of(ndt.make_fixedbytes(16)),
                     _lowlevel.type_id.FIXEDBYTES)
     self.assertEqual(self.type_id_of(ndt.json),
                     _lowlevel.type_id.JSON)
     # Date
     self.assertEqual(self.type_id_of(ndt.date),
                     _lowlevel.type_id.DATE)
     # Property
     self.assertEqual(self.type_id_of(nd.type_of(ndt.date(2000, 1, 1).year)),
                     _lowlevel.type_id.PROPERTY)
     # Categorical
     self.assertEqual(self.type_id_of(ndt.make_categorical([1, 2, 3])),
                     _lowlevel.type_id.CATEGORICAL)
     # Struct
     self.assertEqual(self.type_id_of(ndt.make_struct(
                                 [ndt.int32, ndt.int32], ['x', 'y'])),
                     _lowlevel.type_id.STRUCT)
     self.assertEqual(self.type_id_of(ndt.type('{x : int32, y : int32}')),
                     _lowlevel.type_id.FIXEDSTRUCT)
     # Convert/byteswap/view
     self.assertEqual(self.type_id_of(ndt.make_convert(
                                 ndt.int32, ndt.int8)),
                     _lowlevel.type_id.CONVERT)
     self.assertEqual(self.type_id_of(ndt.make_byteswap(ndt.int32)),
                     _lowlevel.type_id.BYTESWAP)
     self.assertEqual(self.type_id_of(ndt.make_view(
                                 ndt.int32, ndt.uint32)),
                     _lowlevel.type_id.VIEW)
     # CUDA types
     if ndt.cuda_support:
         self.assertEqual(self.type_id_of(ndt.type('cuda_device[int32]')),
                          _lowlevel.type_id.CUDA_DEVICE)
         self.assertEqual(self.type_id_of(ndt.type('cuda_host[int32]')),
                          _lowlevel.type_id.CUDA_HOST)
     # Uniform arrays
     self.assertEqual(self.type_id_of(ndt.type('3 * int32')),
                     _lowlevel.type_id.FIXED_DIM)
     self.assertEqual(self.type_id_of(ndt.type('strided * int32')),
                     _lowlevel.type_id.STRIDED_DIM)
     self.assertEqual(self.type_id_of(ndt.type('var * int32')),
                     _lowlevel.type_id.VAR_DIM)
     # GroupBy
     self.assertEqual(self.type_id_of(nd.type_of(nd.groupby([1, 2],
                                                            ['a', 'a']))),
                     _lowlevel.type_id.GROUPBY)
     # Type
     self.assertEqual(self.type_id_of(ndt.type('type')),
                     _lowlevel.type_id.TYPE)