def test_numpy_dynd_fixedstring_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_fixedstring(7, 'utf_32'), nd.dtype_of(b))
        else:
            self.assertEqual(ndt.make_fixedstring(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_fixedstring(7, 'utf_32'))
        self.assertEqual(
                ndt.make_convert(
                    ndt.make_fixedstring(7, 'utf_32'),
                    ndt.make_fixedstring(7, 'ascii')),
                nd.dtype_of(b_u))
        # Evaluate to its value array
        b_u = b_u.eval()
        self.assertEqual(
                ndt.make_fixedstring(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))
Пример #2
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.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))
Пример #3
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)