예제 #1
0
 def shapes(self, shapes_):
     if isinstance(shapes_, TensorShape)\
             or (isinstance(shapes_, list) and
                 (len(shapes_) == 0
                  or isinstance(shapes_[0], int))):
         self._xlayer.shapes = \
             lpx.IntVector2D([lpx.IntVector(shapes_)])
         self._xlayer.shapes_t = "TensorShape"
     else:
         assert all([isinstance(e, (list, TensorShape)) for e in shapes_])
         self._xlayer.shapes = \
             lpx.IntVector2D([lpx.IntVector(s) for s in shapes_])
         self._xlayer.shapes_t = "TupleShape"
예제 #2
0
    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
예제 #3
0
    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'
예제 #4
0
    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']
예제 #5
0
    def test_get_item_slice(self):

        ts = TupleShape([TensorShape([1]), TensorShape([2])])

        ts_slice = ts[:]

        assert len(ts_slice) == 2
        assert ts_slice == [[1], [2]]
        assert isinstance(ts_slice[0], TensorShape)
        assert isinstance(ts_slice[1], TensorShape)

        _shape = lpx.IntVector2D([lpx.IntVector([1]),
                                  lpx.IntVector([2])])
        ts = TupleShape(IntVector2D(_shape))

        ts_slice = ts[:]

        assert len(ts_slice) == 2
        assert ts_slice == [[1], [2]]
        assert isinstance(ts_slice[0], TensorShape)
        assert isinstance(ts_slice[1], TensorShape)

        ts[0:2] = [[-1, 2], [-1, 3]]

        assert ts == [[-1, 2], [-1, 3]]
예제 #6
0
    def test_to_list(self):

        ts = TupleShape([TensorShape([1, 2]), TensorShape([2, 4])])

        assert ts == [[1, 2], [2, 4]]
        assert ts.tolist() == [[1, 2], [2, 4]]

        _shape = lpx.IntVector2D([lpx.IntVector([1, 2]),
                                  lpx.IntVector([2, 4])])
        ts = TupleShape(IntVector2D(_shape))

        assert ts.tolist() == [[1, 2], [2, 4]]
예제 #7
0
    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'])
예제 #8
0
    def test_get_set_item(self):

        _shape = lpx.IntVector2D([lpx.IntVector([-1, 2]),
                                  lpx.IntVector([-1, 2, 4])])
        ts = TupleShape(IntVector2D(_shape))

        assert len(ts) == 2
        assert isinstance(ts[0], TensorShape)
        assert ts[0] == [-1, 2]
        assert ts[1] == [-1, 2, 4]

        with self.assertRaises(IndexError):
            ts[2]

        ts[0] = [-1, 3]
        assert ts == [[-1, 3], [-1, 2, 4]]
예제 #9
0
    def test_set_value(self):

        ts = TupleShape([TensorShape([1, 2]), TensorShape([2, 4])])

        ts.set_value(0, -1)

        assert len(ts) == 2
        assert ts[0] == TensorShape([-1, 2])
        assert ts[1] == TensorShape([-1, 4])

        _shape = lpx.IntVector2D([lpx.IntVector([1, 2]),
                                  lpx.IntVector([2, 4])])
        ts = TupleShape(IntVector2D(_shape))

        ts.set_value(0, -1)

        assert len(ts) == 2
        assert ts[0] == TensorShape([-1, 2])
        assert ts[1] == TensorShape([-1, 4])
예제 #10
0
    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')
예제 #11
0
 def __setitem__(self, key, value):
     if isinstance(key, slice):
         value = lpx.IntVector2D([lpx.IntVector(v) for v in value])
         self.__vector.__setitem__(key, value)
     else:
         self.__vector.__setitem__(key, lpx.IntVector(value))
예제 #12
0
    def test_int_vector_2D(self):

        iv = lpx.IntVector2D([lpx.IntVector([])])
        ivx = IntVector2D(iv)
        assert ivx == iv
        assert ivx == [[]]

        # Append
        assert len(iv) == 1
        assert len(ivx) == 1
        ivx.append([4, 5, 6])
        assert len(iv) == 2
        assert len(ivx) == 2
        assert ivx == [[], [4, 5, 6]]
        ivx[0].append(1)
        ivx[0].append(2)
        ivx[0].append(3)
        assert len(iv) == 2
        assert len(ivx) == 2
        assert ivx == [[1, 2, 3], [4, 5, 6]]

        # Contains
        assert lpx.IntVector([1, 2, 3]) in iv
        assert lpx.IntVector([4, 5, 6]) in iv
        assert [1, 2, 3] in ivx
        assert [4, 5, 6] in ivx
        assert lpx.IntVector([1, 2]) not in iv
        assert [1, 2] not in ivx
        with self.assertRaises(TypeError):
            assert 1 not in ivx
        with self.assertRaises(TypeError):
            assert 'a' not in ivx

        # Delete
        with self.assertRaises(IndexError):
            del ivx[2]
        del ivx[1]
        assert ivx == [[1, 2, 3]]
        assert iv == lpx.IntVector2D([lpx.IntVector([1, 2, 3])])

        # Equal
        assert ivx == [[1, 2, 3]]
        assert ivx == lpx.IntVector2D([lpx.IntVector([1, 2, 3])])
        assert iv == lpx.IntVector2D([lpx.IntVector([1, 2, 3])])

        # Extend
        ivx.extend([[4, 5]])
        assert len(iv) == 2
        assert len(ivx) == 2

        ivx += [[6, 7]]
        assert len(ivx) == 3
        del ivx[2]

        # Get item
        assert ivx[0] == [1, 2, 3]
        assert ivx[-1] == [4, 5]
        with self.assertRaises(IndexError):
            ivx[6]
        assert ivx[0:1] == [[1, 2, 3]]
        assert ivx[:] == [[1, 2, 3], [4, 5]]

        # Insert
        ivx.insert(0, [0, 0])
        assert len(ivx) == 3
        assert len(iv) == 3
        assert ivx[0] == [0, 0]
        del ivx[0]
        assert len(ivx) == 2

        # Iter
        c = [[1, 2, 3], [4, 5]]
        for i, e in enumerate(ivx):
            assert isinstance(e, IntVector)
            assert e == c[i]
        for i, e in enumerate(iv):
            assert isinstance(e, lpx.IntVector)
            assert e == lpx.IntVector(c[i])

        # Length
        assert len(ivx) == len(iv)
        assert len(ivx) == 2

        # Not equal
        assert iv != lpx.IntVector2D([lpx.IntVector([1, 2, 3])])
        assert ivx != [[1, 2, 3], [4]]

        # Repr
        assert repr(ivx) == "IntVector2D[IntVector[1, 2, 3], IntVector[4, 5]]"

        # Str
        assert str(ivx) == "IntVector2D[IntVector[1, 2, 3], IntVector[4, 5]]"

        # Set
        ivx[0] = [-1, 2, 3]
        assert ivx == [[-1, 2, 3], [4, 5]]
        assert iv == lpx.IntVector2D([lpx.IntVector([-1, 2, 3]),
                                      lpx.IntVector([4, 5])])
        with self.assertRaises(IndexError):
            ivx[6] = [5, 6]