def test_simple(self): a = nd.array([(1, 2, 'a', 'b'), (3, 4, 'ab', 'cd'), (5, 6, 'def', 'ghi')], dtype='{x: int32, y: int32, z: string, w: string}') # Selecting a single field b = nd.fields(a, 'x') self.assertEqual(nd.dtype_of(b), 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.dtype_of(b), 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.dtype_of(b), 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.dtype_of(b), 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))
def test_struct(self): a = nd.parse_json('{x:int32, y:string, z:float32}', '{"x":20, "y":"testing one two three", "z":-3.25}') self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:float32}')) self.assertEqual(nd.type_of(a[...]), ndt.type('{x:int32, y:string, z:float32}')) self.assertEqual(nd.type_of(a[0]), ndt.int32) self.assertEqual(nd.type_of(a[1]), ndt.string) self.assertEqual(nd.type_of(a[2]), ndt.float32) self.assertEqual(nd.type_of(a[-3]), ndt.int32) self.assertEqual(nd.type_of(a[-2]), ndt.string) self.assertEqual(nd.type_of(a[-1]), ndt.float32) self.assertEqual( nd.type_of(a[1:]), ndt.make_struct([ndt.string, ndt.float32], ['y', 'z'])) self.assertEqual(nd.type_of(a[::-2]), ndt.make_struct([ndt.float32, ndt.int32], ['z', 'x'])) self.assertEqual(nd.as_py(a[0]), 20) self.assertEqual(nd.as_py(a[1]), "testing one two three") self.assertEqual(nd.as_py(a[2]), -3.25) self.assertEqual(nd.as_py(a[1:]), { 'y': 'testing one two three', 'z': -3.25 }) self.assertEqual(nd.as_py(a[::-2]), {'x': 20, 'z': -3.25})
def test_full_of_struct(self): # Constructor of a cstruct 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("strided * {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_strided_dim(ndt.make_struct([ndt.int32] * 2, ["x", "y"]))) self.assertEqual(a.shape, (3,)) self.assertEqual(nd.as_py(a), [{"x": 3, "y": 10}] * 3)
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))
def test_type_from_ctype_struct(self): class POINT(ctypes.Structure): _fields_ = [('x', ctypes.c_int32), ('y', ctypes.c_int32)] self.assertEqual(ndt.make_struct( [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_struct([POINT, ndt.int8, ndt.float32, POINT], ['pos', 'flags', 'size', 'vel']), ndt.type(DATA))
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 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_struct_function(self): import os a = nd.array(date(1955, 3, 13)) s = a.to_struct.eval() self.assertEqual(nd.dtype_of(s), ndt.make_struct([ndt.int16, ndt.int8, ndt.int8], ["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)
def test_struct(self): a = nd.parse_json('{x:int32, y:string, z:float32}', '{"x":20, "y":"testing one two three", "z":-3.25}') self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:float32}')) self.assertEqual(nd.type_of(a[...]), ndt.type('{x:int32, y:string, z:float32}')) self.assertEqual(nd.type_of(a[0]), ndt.int32) self.assertEqual(nd.type_of(a[1]), ndt.string) self.assertEqual(nd.type_of(a[2]), ndt.float32) self.assertEqual(nd.type_of(a[-3]), ndt.int32) self.assertEqual(nd.type_of(a[-2]), ndt.string) self.assertEqual(nd.type_of(a[-1]), ndt.float32) self.assertEqual(nd.type_of(a[1:]), ndt.make_struct([ndt.string, ndt.float32], ['y', 'z'])) self.assertEqual(nd.type_of(a[::-2]), ndt.make_struct([ndt.float32, ndt.int32], ['z', 'x'])) self.assertEqual(nd.as_py(a[0]), 20) self.assertEqual(nd.as_py(a[1]), "testing one two three") self.assertEqual(nd.as_py(a[2]), -3.25) self.assertEqual(nd.as_py(a[1:]), {'y':'testing one two three', 'z':-3.25}) self.assertEqual(nd.as_py(a[::-2]), {'x':20, 'z':-3.25})
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_struct([ndt.int16, ndt.int8, ndt.int8], ['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)
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_struct( [ndt.int16, ndt.int8, ndt.int8], ['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)
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)
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))
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)
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_struct([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_struct_function(self): a = nd.array(time(13, 25, 8, 765432)) s = a.to_struct().eval() self.assertEqual(nd.dtype_of(s), ndt.make_struct( [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_struct_function(self): a = nd.array(time(13, 25, 8, 765432)) s = a.to_struct.eval() self.assertEqual( nd.dtype_of(s), ndt.make_struct([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__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)
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)
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_struct([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)])
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_struct([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)])
def contributions_stream(stream_file): f = open(stream_file, 'rb') # Description of this dataset headers = f.readline().strip() # read out the headers line headers = headers.split(',') # The types for the different fields htypes = [ ndt.int32, ndt.int16, ndt.int16] + \ [ ndt.string ] * 4 + \ [ ndt.bool, ndt.float64 ] + \ [ ndt.string ] * 33 # Build the DyND data type dtype = ndt.make_struct(htypes, headers) sreader = csv.reader(f) return sreader, dtype
def test_simple(self): a = nd.array([ (1, 2, 'a', 'b'), (3, 4, 'ab', 'cd'), (5, 6, 'def', 'ghi')], type='3 * {x: int32, y: int32, z: string, w: string}') # Selecting a single field b = nd.fields(a, 'x') self.assertEqual(nd.dtype_of(b), 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.dtype_of(b), 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.dtype_of(b), 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.dtype_of(b), 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))
def test_struct_type(self): tp = ndt.make_struct([ndt.int32, ndt.int64], ['x', 'y']) self.assertTrue(tp.field_types, [ndt.int32, ndt.int64]) self.assertTrue(tp.field_names, ['x', 'y']) self.assertEqual(tp.arrmeta_size, 2 * ctypes.sizeof(ctypes.c_void_p)) self.assertTrue(tp.data_size is None)
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)