so_magic.som.manager   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A MagicMapManager.train() 0 2 1
A MapId.__iter__() 0 3 1
A MapManager.train() 0 2 1
A MapId.from_self_organizing_map() 0 4 1
A MapId.__dir__() 0 2 1
A MapId.__str__() 0 2 1
A MapManager.get_map() 0 6 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(*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
        args: 'dataset', 'nb_cols', 'nb_rows'
23
        kwargs: 'initialization', 'maptype', 'gridtype'
24
        """
25
        return self.pool.get_object(*args, **kwargs)
26
27
    def train(self, dataset, nb_cols, nb_rows, **kwargs):
28
        return self.map_factory.create(dataset, nb_cols, nb_rows, **kwargs)
29
30
31
@attr.s
32
class MagicMapManager:
33
    so_master = attr.ib(init=True)
34
    manager = attr.ib(init=False, default=MapManager())
35
36
    def train(self, nb_cols, nb_rows, **kwargs):
37
        return self.manager.train(self.so_master.dataset, nb_cols, nb_rows, **kwargs)
38
39
40
@attr.s
41
class MapId:
42
    dataset_name = attr.ib(init=True)
43
    _n_columns = attr.ib(init=True)
44
    _n_rows = attr.ib(init=True)
45
    initialization = attr.ib(init=True)
46
    map_type = attr.ib(init=True)
47
    grid_type = attr.ib(init=True)
48
49
    @staticmethod
50
    def from_self_organizing_map(somap, **kwargs):
51
        return MapId(kwargs.get('dataset_name', somap.dataset_name),
52
                     *[getattr(somap, attribute.name) for attribute in MapId.__attrs_attrs__[1:]])
53
54
    def __dir__(self):
55
        return sorted([attribute.name for attribute in self.__attrs_attrs__])
56
57
    def __iter__(self):
58
        """Default implementation of __iter__ to allow dict(self) in client code"""
59
        return iter([(k, getattr(self, k)) for k in self.__dir__()])
60
61
    def __str__(self):
62
        return '-'.join(str(getattr(self, attribute.name)) for attribute in self.__attrs_attrs__)
63