Passed
Push — master ( 812b05...176334 )
by Konstantinos
02:19 queued 01:50
created

so_magic.som.manager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A MapId.__iter__() 0 3 1
A MapId.__dir__() 0 2 1
A MapId.__str__() 0 2 1
A MapManager.get_map() 0 5 1
A MagicMapManager.train() 0 2 1
A MapManager.train() 0 2 1
A MapId.from_self_organizing_map() 0 4 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A _build_hash() 0 2 1
1
import logging
2
import attr
3
4
from so_magic.utils import ObjectsPool
5
from .factory import SelfOrganizingMapFactory
6
7
logger = logging.getLogger(__name__)
8
9
10
def _build_hash(_self, *args, **kwargs):
11
    return str(MapId(*args, kwargs.get('initialization'), kwargs.get('map_type'), kwargs.get('grid_type')))
12
13
14
@attr.s
15
class MapManager:
16
    map_factory = attr.ib(init=True, default=SelfOrganizingMapFactory())
17
    pool = attr.ib(init=False, default=attr.Factory(
18
        lambda self: ObjectsPool.new_empty(self.map_factory.create, build_hash=_build_hash), takes_self=True))
19
20
    def get_map(self, *args, **kwargs):
21
        """
22
        'dataset', 'nb_cols', 'nb_rows', 'initialization', 'map_type', 'grid_type'
23
        """
24
        return self.pool.get_object(*args, **kwargs)
25
26
    def train(self, dataset, nb_cols, nb_rows, **kwargs):
27
        return self.map_factory.create(dataset, nb_cols, nb_rows, **kwargs)
28
29
30
@attr.s
31
class MagicMapManager:
32
    so_master = attr.ib(init=True)
33
    manager = attr.ib(init=False, default=MapManager())
34
35
    def train(self, nb_cols, nb_rows, **kwargs):
36
        return self.manager.train(self.so_master.dataset, nb_cols, nb_rows, **kwargs)
37
38
39
@attr.s
40
class MapId:
41
    dataset_name = attr.ib(init=True)
42
    _n_columns = attr.ib(init=True)
43
    _n_rows = attr.ib(init=True)
44
    initialization = attr.ib(init=True)
45
    map_type = attr.ib(init=True)
46
    grid_type = attr.ib(init=True)
47
48
    @staticmethod
49
    def from_self_organizing_map(somap, **kwargs):
50
        return MapId(kwargs.get('dataset_name', somap.dataset_name),
51
                     *[getattr(somap, attribute.name) for attribute in MapId.__attrs_attrs__[1:]])
52
53
    def __dir__(self):
54
        return sorted([attribute.name for attribute in self.__attrs_attrs__])
55
56
    def __iter__(self):
57
        """Default implementation of __iter__ to allow dict(self) in client code"""
58
        return iter([(k, getattr(self, k)) for k in self.__dir__()])
59
60
    def __str__(self):
61
        return '-'.join(str(getattr(self, _)) for _ in dir(self))
62
63
64
if __name__ == '__main__':
65
    map_manager = MapManager()
66