def test_getitem(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', True) xam['i'] = lpx.XAttr('i', lpx.MapStrVectorStr()) xad = XAttrDict(xam) xad['i'] = {'i': ['i1', 'i2']} xad['j'] = {'j': 'j1'} assert xad['a'] == -1 assert xad['b'] == [1, 2] assert xad['c'] == [[1, 2], [0, 0]] assert xad['d'] == -1.5 assert xad['e'] == [1.5, 2] assert xad['f'] == 'f' assert xad['g'] == ['gg', 'gg'] assert xad['h'] is True assert xad['i'] == {'i': ['i1', 'i2']} assert xad['i'] != {'a': ['a']} assert xad['j'] == {'j': 'j1'} with self.assertRaises(KeyError): xad['z']
def test_to_dict(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', False) xad = XAttrDict(xam) xad['i'] = {'i': ['i1', 'i2']} xad['j'] = {'j': 'j1'} d = xad.to_dict() assert isinstance(d['a'], int) assert isinstance(d['b'], list) assert isinstance(d['c'], list) assert isinstance(d['d'], float) assert isinstance(d['e'], list) assert isinstance(d['f'], str) assert isinstance(d['g'], list) assert isinstance(d['h'], bool) assert isinstance(d['i'], dict) assert isinstance(d['i']['i'], list) assert isinstance(d['j'], dict) assert isinstance(d['j']['j'], str) assert d['j']['j'] == 'j1'
def test_xattr(self): xa = lpx.XAttr('a', -1) assert xa.name == 'a' assert xa.type == 'INT' assert xa.i == -1 xa = lpx.XAttr('b', lpx.IntVector([1, 2, 3])) assert xa.name == 'b' assert xa.type == 'INTS' assert xa.ints == lpx.IntVector([1, 2, 3]) xa = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2, 3]), lpx.IntVector([0, 0])])) assert xa.name == 'c' assert xa.type == 'INTS2D' assert xa.ints2d == lpx.IntVector2D( [lpx.IntVector([1, 2, 3]), lpx.IntVector([0, 0])]) xa = lpx.XAttr('d', -1.5) assert xa.name == 'd' assert xa.type == 'FLOAT' assert xa.f == -1.5 xa = lpx.XAttr('e', lpx.FloatVector([1, 2, 3])) assert xa.name == 'e' assert xa.type == 'FLOATS' assert xa.floats == lpx.FloatVector([1, 2, 3]) xa = lpx.XAttr('f', 'f') assert xa.name == 'f' assert xa.type == 'STRING' assert xa.s == 'f' xa = lpx.XAttr('e', lpx.StrVector(['a', 'b'])) assert xa.name == 'e' assert xa.type == 'STRINGS' assert xa.strings == lpx.StrVector(['a', 'b'])
def test_get(self): xg = lpx.XGraph('g') X1 = lpx.XLayer(name='x1', xtype=lpx.StrVector(['X1']), bottoms=lpx.StrVector([])) xg.add(X1) X1_xg = xg.get('x1') assert X1_xg.name == 'x1' assert X1_xg.xtype[0] == 'X1' assert X1_xg.bottoms == lpx.StrVector([]) # If we adjust X1, this doesn't get represented in X1_xg X1.xtype[0] = 'X11' assert X1_xg.xtype[0] == 'X1' X1_xg.xtype[0] = 'X11' assert X1_xg.xtype[0] == 'X11' X1_xg.xtype = lpx.StrVector(['X111']) assert X1_xg.xtype[0] == 'X111'
def test_xlayer_factory(self): X1 = defaultXLayer() assert X1.layer == lpx.StrVector([]) assert X1.tops == lpx.StrVector([]) assert X1.bottoms == lpx.StrVector([]) assert X1.targets == lpx.StrVector([]) assert X1.target == 'cpu' X1 = X1._replace(name='test', tops=['test']) assert X1.name == 'test' assert X1.tops == lpx.StrVector(['test']) X2 = defaultXLayer() assert X2.layer == lpx.StrVector([]) assert X2.tops == lpx.StrVector([]) assert X2.bottoms == lpx.StrVector([]) assert X2.targets == lpx.StrVector([])
def __setitem__(self, key: str, value): value_error = "Unsupported value: {} for attribute: {}. XAttr values"\ " can only be of types: int, float, str, List[int],"\ " List[float], List[str], List[List[int]],"\ " Dict[str, str], Dict[Str, List[Str]]"\ .format(value, key) # TODO: improve this code if isinstance(value, list): if all([isinstance(e, int) for e in value]): value = lpx.IntVector(value) elif all([isinstance(e, float) for e in value]): value = lpx.FloatVector(value) elif all([isinstance(e, str) for e in value]): value = lpx.StrVector(value) elif all([isinstance(e, list) for e in value]): if all([[isinstance(ee, int) for ee in e] for e in value]): value = lpx.IntVector2D([lpx.IntVector(v) for v in value]) else: raise ValueError(value_error) elif all([isinstance(e, IntVector) for e in value]): value = lpx.IntVector2D([e.get_lpx_vector() for e in value]) else: raise ValueError(value_error) elif isinstance(value, dict): values = list(value.values()) if len(values) > 0 and isinstance(list(value.values())[0], str): value = MapStrStr.from_dict(value).get_lpx_map() else: value = MapStrVectorStr.from_dict(value).get_lpx_map() # raise ValueError("Unsupported dictionary for XAttr with" # " values" # " of type: {}, only 'Str' and 'List[Str]'" # " supported" # .format(type(value.volues()[0]))) elif isinstance(value, (MapStrVectorStr, MapStrStr)): value = value.get_lpx_map() elif isinstance(value, (StrVector, IntVector, IntVector2D, FloatVector)): value = value.get_lpx_vector() elif value is not None and \ not isinstance(value, (float, int, str, StrVector, IntVector, IntVector2D, FloatVector)): raise ValueError(value_error) if value is not None: xattr = lpx.XAttr(key, value) else: xattr = lpx.XAttr(key) self._xattr_map[key] = xattr
def test_xlayer_constructor(self): X = XLayer() assert X.name == "" assert X.type == [] assert X.type == lpx.StrVector([]) assert X.shapes == TensorShape(lpx.IntVector([])) assert X.shapes == [] assert X.sizes == lpx.IntVector([]) assert X.sizes == [] assert X.tops == lpx.StrVector([]) assert X.tops == [] assert X.bottoms == lpx.StrVector([]) assert X.bottoms == [] assert X.layer == lpx.StrVector([]) assert X.layer == [] assert X.data == [] assert X.targets == lpx.StrVector([]) assert X.targets == [] assert X.target == 'cpu' assert X.subgraph is None assert X.internal is False assert X.attrs == XAttrDict(lpx.XAttrMap())
def bottoms(self, bottoms_: list): self._xlayer.bottoms = lpx.StrVector(bottoms_)
def test_add(self): xg = lpx.XGraph('g') X1 = lpx.XLayer(name='x1', xtype=lpx.StrVector(['X1']), bottoms=lpx.StrVector([])) xg.add(X1) assert 'x1' in xg assert len(xg.get_input_names()) == 1 assert xg.get_input_names()[0] == 'x1' assert len(xg.get_output_names()) == 1 assert xg.get_output_names()[0] == 'x1' X2 = lpx.XLayer(name='x2', xtype=lpx.StrVector(['X2']), bottoms=lpx.StrVector(['x1'])) xg.add(X2) assert len(xg) == 2 X1_xg = xg.get('x1') assert len(X1_xg.tops) == 1 assert X1_xg.tops[0] == 'x2' assert len(xg.get_input_names()) == 1 assert xg.get_input_names()[0] == 'x1' assert len(xg.get_output_names()) == 1 assert xg.get_output_names()[0] == 'x2' assert xg.get_layer_names() == lpx.StrVector(['x1', 'x2']) X3 = lpx.XLayer(name='x3', xtype=lpx.StrVector(['X3']), bottoms=lpx.StrVector(['x2'])) xg.add(X3) assert xg.get_input_names() == lpx.StrVector(['x1']) assert xg.get_output_names() == lpx.StrVector(['x3']) assert xg.get_layer_names() == lpx.StrVector(['x1', 'x2', 'x3']) assert xg.get('x2').tops == lpx.StrVector(['x3']) assert xg.get('x2').bottoms == lpx.StrVector(['x1']) X4 = lpx.XLayer(name='x4', xtype=lpx.StrVector(['X4']), bottoms=lpx.StrVector(['x1']), tops=lpx.StrVector(['x3'])) xg.add(X4) assert xg.get_input_names() == lpx.StrVector(['x1']) assert xg.get_output_names() == lpx.StrVector(['x3']) assert xg.get_layer_names() == lpx.StrVector(['x1', 'x2', 'x4', 'x3']) assert xg.get('x2').tops == lpx.StrVector(['x3']) assert xg.get('x2').bottoms == lpx.StrVector(['x1']) assert xg.get('x4').tops == lpx.StrVector(['x3']) assert xg.get('x4').bottoms == lpx.StrVector(['x1']) X5 = lpx.XLayer(name='x5', xtype=lpx.StrVector(['X5']), bottoms=lpx.StrVector([]), tops=lpx.StrVector(['x4'])) X6 = lpx.XLayer(name='x6', xtype=lpx.StrVector(['X6']), bottoms=lpx.StrVector(['x4']), tops=lpx.StrVector([])) xg.add(X5) xg.add(X6) assert xg.get_input_names() == lpx.StrVector(['x1', 'x5']) assert xg.get_output_names() == lpx.StrVector(['x3', 'x6']) assert xg.get_layer_names() == \ lpx.StrVector(['x1', 'x2', 'x5', 'x4', 'x3', 'x6']) assert xg.get('x2').tops == lpx.StrVector(['x3']) assert xg.get('x2').bottoms == lpx.StrVector(['x1']) assert xg.get('x4').tops == lpx.StrVector(['x3', 'x6']) assert xg.get('x4').bottoms == lpx.StrVector(['x1', 'x5'])
def test_update(self): xg = lpx.XGraph('g') X1 = lpx.XLayer(name='x1', xtype=lpx.StrVector(['X1']), bottoms=lpx.StrVector([])) xg.add(X1) assert xg.get('x1').xtype == lpx.StrVector(['X1']) X1.xtype[0] = 'X11' assert xg.get('x1').xtype == lpx.StrVector(['X1']) xg.update(X1.name) assert xg.get('x1').xtype == lpx.StrVector(['X1']) X2 = lpx.XLayer(name='x2', xtype=lpx.StrVector(['X2']), bottoms=lpx.StrVector(['x1'])) X3 = lpx.XLayer(name='x3', xtype=lpx.StrVector(['X3']), bottoms=lpx.StrVector(['x2'])) xg.add(X2) xg.add(X3) X2 = xg.get('x2') X3 = xg.get('x3') assert xg.get_layer_names() == \ lpx.StrVector(['x1', 'x2', 'x3']) X3.bottoms = lpx.StrVector(['x2']) xg.update(X3.name) assert xg.get_layer_names() == \ lpx.StrVector(['x1', 'x2', 'x3']) assert xg.get('x2').tops == lpx.StrVector(['x3']) assert xg.get('x2').bottoms == lpx.StrVector(['x1']) assert xg.get('x3').bottoms == lpx.StrVector(['x2']) assert xg.get('x1').tops == lpx.StrVector(['x2']) xg.remove(X2.name) X2.bottoms = lpx.StrVector(['x3']) X2.tops = lpx.StrVector(['x1']) xg.add(X2) assert xg.get_layer_names() == \ lpx.StrVector(['x3', 'x2', 'x1']) assert xg.get_input_names() == lpx.StrVector(['x3']) assert xg.get_output_names() == lpx.StrVector(['x1']) assert xg.get('x2').tops == lpx.StrVector(['x1']) assert xg.get('x2').bottoms == lpx.StrVector(['x3']) assert xg.get('x3').bottoms == lpx.StrVector([]) assert xg.get('x3').tops == lpx.StrVector(['x2']) assert xg.get('x1').tops == lpx.StrVector([]) assert xg.get('x1').bottoms == lpx.StrVector(['x2'])
def test_str_vector(self): iv = lpx.StrVector(['a', 'b', 'c']) ivx = StrVector(iv) assert ivx == iv assert ivx == ['a', 'b', 'c'] # Append ivx.append('d') assert len(iv) == 4 assert len(ivx) == 4 # Contains assert 'b' in iv assert 'b' in ivx assert 'e' not in iv assert 'e' not in ivx # Delete del ivx[3] assert ivx == ['a', 'b', 'c'] assert iv == lpx.StrVector(['a', 'b', 'c']) # Equal assert ivx == ['a', 'b', 'c'] assert ivx == lpx.StrVector(['a', 'b', 'c']) assert iv == lpx.StrVector(['a', 'b', 'c']) # Extend ivx.extend(['d', 'e']) assert len(iv) == 5 assert len(ivx) == 5 # Get item assert ivx[3] == 'd' assert ivx[-1] == 'e' with self.assertRaises(IndexError): ivx[6] # Iter c = ['a', 'b', 'c', 'd', 'e'] for i, e in enumerate(ivx): assert e == c[i] for i, e in enumerate(iv): assert e == c[i] # Length assert len(ivx) == len(iv) assert len(ivx) == 5 # Not equal assert iv != lpx.StrVector(['a', 'b', 'c']) assert ivx != ['a', 'b', 'c', 'd'] # Repr assert repr(iv) == "StrVector[a, b, c, d, e]" assert repr(ivx) == "StrVector[a, b, c, d, e]" # Str assert str(iv) == "StrVector[a, b, c, d, e]" assert str(ivx) == "StrVector[a, b, c, d, e]" # Set ivx[0] = 'z' assert ivx == ['z', 'b', 'c', 'd', 'e'] assert iv == lpx.StrVector(['z', 'b', 'c', 'd', 'e']) with self.assertRaises(IndexError): ivx[6] = 'z' # def test_xbuffer_vector(self): # iv = lpx.XBufferVector([np.array(1, 2, 3)]) # ivx = FloatVector(iv) # assert ivx == iv # assert ivx == [1, 1.5, 3] # # Append # ivx.append(4) # assert len(iv) == 4 # assert len(ivx) == 4 # # Contains # assert 1.5 in iv # assert 1.5 in ivx # assert 1.51 not in iv # assert 1.51 not in ivx # with self.assertRaises(TypeError): # assert 'a' not in ivx # # Delete # del ivx[3] # assert ivx == [1, 1.5, 3] # assert iv == lpx.FloatVector([1, 1.5, 3]) # # Equal # assert ivx == [1, 1.5, 3] # assert ivx == lpx.FloatVector([1, 1.5, 3]) # assert iv == lpx.FloatVector([1, 1.5, 3]) # # Extend # ivx.extend([4, 5]) # assert len(iv) == 5 # assert len(ivx) == 5 # # Get item # assert ivx[3] == 4 # assert ivx[-1] == 5 # with self.assertRaises(IndexError): # ivx[6] # # Iter # c = [1, 1.5, 3, 4, 5] # for i, e in enumerate(ivx): # assert e == c[i] # for i, e in enumerate(iv): # assert e == c[i] # # Length # assert len(ivx) == len(iv) # assert len(ivx) == 5 # # Not equal # assert iv != lpx.FloatVector([1, 1.5, 3]) # assert ivx != [1, 1.5, 3, 4] # # Repr # assert repr(iv) == "FloatVector[1, 1.5, 3, 4, 5]" # assert repr(ivx) == "FloatVector[1, 1.5, 3, 4, 5]" # # Str # assert str(iv) == "FloatVector[1, 1.5, 3, 4, 5]" # assert str(ivx) == "FloatVector[1, 1.5, 3, 4, 5]" # # Set # ivx[0] = -1 # assert ivx == [-1, 1.5, 3, 4, 5] # assert iv == lpx.FloatVector([-1, 1.5, 3, 4, 5]) # with self.assertRaises(IndexError): # ivx[6] = -1
def test_get_copy(self): xam = lpx.XAttrMap() xam['a'] = lpx.XAttr('a', -1) xam['b'] = lpx.XAttr('b', lpx.IntVector([1, 2])) xam['c'] = lpx.XAttr( 'c', lpx.IntVector2D([lpx.IntVector([1, 2]), lpx.IntVector([0, 0])])) xam['d'] = lpx.XAttr('d', -1.5) xam['e'] = lpx.XAttr('e', lpx.FloatVector([1.5, 2])) xam['f'] = lpx.XAttr('f', 'f') xam['g'] = lpx.XAttr('g', lpx.StrVector(['gg', 'gg'])) xam['h'] = lpx.XAttr('h', False) xad = XAttrDict(xam) a_c = xad._get_copy('a') assert a_c == -1 xad['a'] = 1 assert a_c == -1 assert xad['a'] == 1 ints_c = xad._get_copy('b') assert ints_c == [1, 2] xad['b'][0] = -1 assert xad['b'] == [-1, 2] assert ints_c == [1, 2] ints2d_c = xad._get_copy('c') assert ints2d_c == [[1, 2], [0, 0]] xad['c'][0] = [-1, 2] assert xad['c'] == [[-1, 2], [0, 0]] assert ints2d_c == [[1, 2], [0, 0]] assert xad._get_copy('d') == -1.5 floats_c = xad._get_copy('e') assert floats_c == [1.5, 2] xad['e'] = [-1.5, 2.] assert xad['e'] == [-1.5, 2] assert floats_c == [1.5, 2] s_c = xad._get_copy('f') assert s_c == 'f' xad['f'] = 'ff' assert xad['f'] == 'ff' assert s_c == 'f' strings_c = xad._get_copy('g') assert strings_c == ['gg', 'gg'] xad['g'] = ['gg'] assert xad['g'] == ['gg'] assert strings_c == ['gg', 'gg'] b_c = xad._get_copy('h') assert b_c is False xad['h'] = True assert xad['h'] is True assert b_c is False with self.assertRaises(KeyError): xad._get_copy('z')
def __setitem__(self, key, value): if isinstance(value, list): value = lpx.StrVector(value) elif isinstance(value, StrVector): value = value.get_lpx_vector() self._map.__setitem__(key, value)
class OpaqueFunc(object): # TypeCode conversion functions # First: C++ -> Python # Second: Python -> C++ type_codes_ = { TypeCode.vInt: ( lambda arg_: IntVector(arg_.ints), lambda arg_: lpx.OpaqueValue(lpx.IntVector(arg_))), TypeCode.Str: ( lambda arg_: arg_.s, lambda arg_: lpx.OpaqueValue(arg_)), TypeCode.Byte: ( lambda arg_: arg_.bytes, lambda arg_: lpx.OpaqueValue(arg_)), TypeCode.vStr: ( lambda arg_: StrVector(arg_.strings), lambda arg_: lpx.OpaqueValue(lpx.StrVector(arg_))), TypeCode.StrContainer: ( lambda arg_: StrContainer.from_lib(arg_.str_c), lambda arg_: lpx.OpaqueValue(arg_._str_c)), TypeCode.BytesContainer: ( lambda arg_: BytesContainer.from_lib(arg_.bytes_c), lambda arg_: lpx.OpaqueValue(arg_._bytes_c)), TypeCode.XGraph: ( lambda arg_: XGraph._from_xgraph(arg_.xg), lambda arg_: lpx.OpaqueValue(arg_._xgraph)), TypeCode.XBuffer: ( lambda arg_: XBuffer.from_lib(arg_.xb), lambda arg_: lpx.OpaqueValue(arg_._xb)), TypeCode.vXBuffer: ( lambda arg_: [XBuffer.from_lib(e) for e in arg_.xbuffers], lambda arg_: lpx.OpaqueValue( lpx.XBufferHolderVector([xb._xb for xb in arg_]))), TypeCode.OpaqueFunc: ( lambda arg_: OpaqueFunc.from_lib(arg_.of), lambda arg_: lpx.OpaqueValue(arg_._of)) } def __init__(self, func: Callable = None, type_codes: List[TypeCode] = None) -> None: self._of = lpx.OpaqueFunc() if type_codes is None: type_codes = [] if func is not None: self.set_func(func, type_codes) @classmethod def from_lib(cls, _of: lpx.OpaqueFunc) -> 'OpaqueFunc': of = OpaqueFunc.__new__(cls) of._of = _of return of def set_func(self, func: Callable, type_codes: List[TypeCode]): # if type_codes is not None: for tc in type_codes: if tc not in OpaqueFunc.type_codes_: raise NotImplementedError("Function with argument of" " unsupported type: {} provided" .format(tc.name)) def opaque_func_wrapper(args): new_args = [] if type_codes is not None: args_type_codes = type_codes else: args_type_codes = [TypeCode(args[i].get_type_code_int()) for i in range(len(args))] for tc, arg_ in zip(args_type_codes, args): if tc not in OpaqueFunc.type_codes_: raise ValueError(f"Unsupported type code: {tc}") arg_ = OpaqueFunc.type_codes_[tc][0](arg_) new_args.append(arg_) func(*new_args) arg_type_codes_ = lpx.IntVector([tc.value for tc in type_codes]) self._of.set_func(opaque_func_wrapper, arg_type_codes_) def __call__(self, *args: Any) -> None: """ Call internal lib OpaqueFunc with provided args """ args_type_codes = self.get_arg_type_codes() if len(args) != len(args_type_codes): raise ValueError("Invalid number of arguments detected." " OpaqueFunc is expecting {} arguments" " but got: {}" .format(len(args_type_codes), len(args))) oa_v = [] for tc, arg_ in zip(args_type_codes, args): if tc not in OpaqueFunc.type_codes_: raise ValueError(f"Unsupported type code: {tc}") oa_v.append(OpaqueFunc.type_codes_[tc][1](arg_)) oa = lpx.OpaqueArgs(oa_v) self._of(oa) def get_arg_type_codes(self): return [TypeCode(i) for i in self._of.get_arg_type_codes()] def get_nb_type_codes(self): return len(self.get_arg_type_codes()) def __del__(self): pass
def targets(self, targets_: list): self._xlayer.targets = lpx.StrVector(targets_)
def __setitem__(self, key, value): if isinstance(key, slice): value = lpx.StrVector(value) return self.get_lpx_vector().__setitem__(key, value)
def layer(self, layer_: list): self._xlayer.layer = lpx.StrVector(layer_)
def test_remove(self): xg = lpx.XGraph('g') X1 = lpx.XLayer(name='x1', xtype=lpx.StrVector(['X1']), bottoms=lpx.StrVector([])) xg.add(X1) assert 'x1' in xg assert len(xg) == 1 xg.remove('x1') assert len(xg) == 0 X2 = lpx.XLayer(name='x2', xtype=lpx.StrVector(['X2']), bottoms=lpx.StrVector(['x1'])) X3 = lpx.XLayer(name='x3', xtype=lpx.StrVector(['X3']), bottoms=lpx.StrVector(['x2'])) X4 = lpx.XLayer(name='x4', xtype=lpx.StrVector(['X4']), bottoms=lpx.StrVector(['x1']), tops=lpx.StrVector(['x3'])) X5 = lpx.XLayer(name='x5', xtype=lpx.StrVector(['X5']), bottoms=lpx.StrVector([]), tops=lpx.StrVector(['x4'])) X6 = lpx.XLayer(name='x6', xtype=lpx.StrVector(['X6']), bottoms=lpx.StrVector(['x4']), tops=lpx.StrVector([])) xg.add(X1) xg.add(X2) xg.add(X3) xg.add(X4) xg.add(X5) xg.add(X6) assert len(xg) == 6 xg.remove('x2') assert len(xg) == 5 assert xg.get_input_names() == lpx.StrVector(['x1', 'x5']) assert xg.get_output_names() == lpx.StrVector(['x3', 'x6']) assert xg.get_layer_names() == \ lpx.StrVector(['x1', 'x5', 'x4', 'x3', 'x6']) xg.remove('x1') assert len(xg) == 4 assert xg.get_input_names() == lpx.StrVector(['x5']) assert xg.get_output_names() == lpx.StrVector(['x3', 'x6']) assert xg.get_layer_names() == \ lpx.StrVector(['x5', 'x4', 'x3', 'x6']) xg.remove('x6') assert len(xg) == 3 assert xg.get_input_names() == lpx.StrVector(['x5']) assert xg.get_output_names() == lpx.StrVector(['x3']) assert xg.get_layer_names() == lpx.StrVector(['x5', 'x4', 'x3']) xg.remove('x4') assert len(xg) == 2 assert xg.get_input_names() == lpx.StrVector(['x5', 'x3']) assert xg.get_output_names() == lpx.StrVector(['x3', 'x5']) assert xg.get_layer_names() == lpx.StrVector(['x3', 'x5']) xg.remove('x3') assert len(xg) == 1 assert xg.get_input_names() == lpx.StrVector(['x5']) assert xg.get_output_names() == lpx.StrVector(['x5']) assert xg.get_layer_names() == lpx.StrVector(['x5']) xg.remove('x5') assert len(xg) == 0 assert xg.get_input_names() == lpx.StrVector([]) assert xg.get_output_names() == lpx.StrVector([]) assert xg.get_layer_names() == lpx.StrVector([])
def tops(self, tops_: list): self._xlayer.tops = lpx.StrVector(tops_)
def type(self, value: list): self._xlayer.xtype = lpx.StrVector(value)
def test_map_str_vector_str(self): # Constructor _m = lpx.MapStrVectorStr() _m['a'] = lpx.StrVector(['a', 'b']) m = MapStrVectorStr(_m) assert len(m) == 1 # Contains assert 'a' in m assert 'b' not in m with self.assertRaises(TypeError): assert 1 not in m _m['b'] = lpx.StrVector(['b', 'b']) # Delete assert len(m) == 2 del m['b'] assert len(m) == 1 assert m['a'] == ['a', 'b'] _m['c'] = lpx.StrVector(['c', 'c']) # Equal assert m == {'a': ['a', 'b'], 'c': ['c', 'c']} assert m == {'a': lpx.StrVector(['a', 'b']), 'c': lpx.StrVector(['c', 'c'])} # Get item assert m['a'] == ['a', 'b'] assert m['a'] == lpx.StrVector(['a', 'b']) assert m.get('b') is None with self.assertRaises(KeyError): m['b'] # Get lpx map assert m.get_lpx_map() == _m # Items assert ('a', ['a', 'b']) in m.items() assert ('c', ('c', 'c')) in m.items() # Keys assert set(m.keys()) == set(['a', 'c']) # Length assert len(m) == 2 assert len(_m) == 2 # Not equal assert m != {'a': ['a', 'b']} # Set m['d'] = ['d'] assert len(m) == 3 m['a'] = lpx.StrVector(['a']) assert len(m) == 3 assert m['a'] == ['a'] m['c'][0] = 'cc' # to dict d = m.to_dict() assert len(d) == 3 assert d['a'] == ['a'] assert d['c'] == ['cc', 'c'] d['c'][1] = 'cc' assert d['c'] == ['cc', 'cc'] assert m['c'] == ['cc', 'c'] assert d['d'] == ['d'] # Update m.update({'a': ['update'], 'e': ['e']}) assert len(m) == 4 assert m['a'][0] == 'update' assert m['e'] == ['e']