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)
 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)