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)
Esempio n. 2
0
 def test_type_from_ctype_cstruct(self):
     class POINT(ctypes.Structure):
         _fields_ = [('x', ctypes.c_int32), ('y', ctypes.c_int32)]
     self.assertEqual(ndt.make_cstruct(
                             [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_cstruct([POINT, ndt.int8, ndt.float32, POINT],
                             ['pos', 'flags', 'size', 'vel']),
                     ndt.type(DATA))
Esempio n. 3
0
 def test_struct_function(self):
     a = nd.array(date(1955,3,13))
     s = a.to_struct().eval()
     self.assertEqual(nd.dtype_of(s),
                     ndt.make_cstruct(
                         [ndt.int32, ndt.int16, ndt.int16],
                         ['year', 'month', 'day']))
     self.assertEqual(nd.as_py(s.year), 1955)
     self.assertEqual(nd.as_py(s.month), 3)
     self.assertEqual(nd.as_py(s.day), 13)
Esempio n. 4
0
 def test_struct_function(self):
     a = nd.array(time(13, 25, 8, 765432))
     s = a.to_struct().eval()
     self.assertEqual(nd.dtype_of(s),
                     ndt.make_cstruct(
                         [ndt.int8, ndt.int8, ndt.int8, ndt.int32],
                         ['hour', 'minute', 'second', 'tick']))
     self.assertEqual(nd.as_py(s.hour), 13)
     self.assertEqual(nd.as_py(s.minute), 25)
     self.assertEqual(nd.as_py(s.second), 8)
     self.assertEqual(nd.as_py(s.tick), 7654320)
 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)
Esempio n. 6
0
 def test_struct_casting(self):
     a = nd.array([date(1912,3,4), date(2002,1,30)])
     # cast from date to struct
     s = a.ucast(ndt.make_cstruct([ndt.int64, ndt.int16, ndt.int8],
                                     ['year', 'month', 'day']))
     s = s.eval()
     self.assertEqual(nd.as_py(s.year), [1912, 2002])
     self.assertEqual(nd.as_py(s.month), [3, 1])
     self.assertEqual(nd.as_py(s.day), [4, 30])
     # cast from struct back to date
     d = s.ucast(ndt.date)
     d = d.eval()
     self.assertEqual(nd.as_py(d), [date(1912,3,4), date(2002,1,30)])
Esempio n. 7
0
 def test_struct_casting(self):
     a = nd.array([time(13, 25, 8, 765432), time(23, 52)])
     # cast from time to struct
     s = a.ucast(ndt.make_cstruct([ndt.int64, ndt.int16, ndt.int8, ndt.int32],
                                     ['hour', 'minute', 'second', 'tick']))
     s = s.eval()
     self.assertEqual(nd.as_py(s.hour), [13, 23])
     self.assertEqual(nd.as_py(s.minute), [25, 52])
     self.assertEqual(nd.as_py(s.second), [8, 0])
     self.assertEqual(nd.as_py(s.tick), [7654320, 0])
     # cast from struct back to time
     t = s.ucast(ndt.time)
     t = t.eval()
     self.assertEqual(nd.as_py(t), [time(13, 25, 8, 765432), time(23, 52)])