|
1
|
|
|
import attr |
|
2
|
|
|
import logging |
|
3
|
|
|
from .som_proxy import SelfOrganizingMapFactory |
|
4
|
|
|
|
|
5
|
|
|
logger = logging.getLogger(__name__) |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
@attr.s |
|
9
|
|
|
class ObjectsPool: |
|
10
|
|
|
constructor = attr.ib(init=True) |
|
11
|
|
|
_objects = {} |
|
12
|
|
|
|
|
13
|
|
|
def get_object(self, *args, **kwargs): |
|
14
|
|
|
key = self._build_hash(*args, **kwargs) |
|
15
|
|
|
if key not in ObjectsPool._objects: |
|
16
|
|
|
try: |
|
17
|
|
|
ObjectsPool._objects[key] = self.constructor(*args, **kwargs) |
|
18
|
|
|
except TypeError as e: |
|
19
|
|
|
print(e) |
|
20
|
|
|
msg = f"DEBUG: Args: [{', '.join(str(_) for _ in args)}], kwargs: {str(kwargs)}" |
|
21
|
|
|
# raise TypeError(msg) |
|
22
|
|
|
return None |
|
23
|
|
|
return ObjectsPool._objects[key] |
|
24
|
|
|
|
|
25
|
|
|
def _build_hash(self, *args, **kwargs): |
|
26
|
|
|
return hash('-'.join([str(_) for _ in args])) |
|
27
|
|
|
|
|
28
|
|
|
class SomapObjectPool(ObjectsPool): |
|
29
|
|
|
def _build_hash(self, *args, **kwargs): |
|
30
|
|
|
return str(MapId(*args, kwargs.get('initialization'), kwargs.get('map_type'), kwargs.get('grid_type'))) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
@attr.s |
|
34
|
|
|
class MapManager: |
|
35
|
|
|
map_factory = attr.ib(init=True, default=SelfOrganizingMapFactory()) |
|
36
|
|
|
pool = attr.ib(init=False, default=attr.Factory(lambda self: SomapObjectPool(self.map_factory.create), takes_self=True)) |
|
37
|
|
|
|
|
38
|
|
|
def get_map(self, *args, **kwargs): |
|
39
|
|
|
""" |
|
40
|
|
|
'dataset', 'n_rows', 'n_columns', 'initialization', 'map_type', 'grid_type' |
|
41
|
|
|
""" |
|
42
|
|
|
return self.pool.get_object(*args, **kwargs) |
|
43
|
|
|
|
|
44
|
|
|
def train(self, dataset, nb_cols, nb_rows, **kwargs): |
|
45
|
|
|
"""""" |
|
46
|
|
|
return self.map_factory.create(dataset, nb_cols, nb_rows, **kwargs) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
@attr.s |
|
50
|
|
|
class MagicMapManager: |
|
51
|
|
|
so_master = attr.ib(init=True) |
|
52
|
|
|
manager = attr.ib(init=False, default=MapManager()) |
|
53
|
|
|
|
|
54
|
|
|
def train(self, nb_cols, nb_rows, **kwargs): |
|
55
|
|
|
return self.manager.train(self.so_master.dataset, nb_cols, nb_rows, **kwargs) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
@attr.s |
|
59
|
|
|
class MapId: |
|
60
|
|
|
dataset_name = attr.ib(init=True) |
|
61
|
|
|
_n_columns = attr.ib(init=True) |
|
62
|
|
|
_n_rows = attr.ib(init=True) |
|
63
|
|
|
initialization = attr.ib(init=True) |
|
64
|
|
|
map_type = attr.ib(init=True) |
|
65
|
|
|
grid_type = attr.ib(init=True) |
|
66
|
|
|
|
|
67
|
|
|
@staticmethod |
|
68
|
|
|
def from_self_organizing_map(somap, **kwargs): |
|
69
|
|
|
return MapId(kwargs.get('dataset_name', somap.dataset_name), *[getattr(somap, attribute.name) for attribute in MapId.__attrs_attrs__[1:]]) |
|
70
|
|
|
|
|
71
|
|
|
def __dir__(self): |
|
72
|
|
|
return sorted([attribute.name for attribute in self.__attrs_attrs__]) |
|
73
|
|
|
|
|
74
|
|
|
def __iter__(self): |
|
75
|
|
|
"""Default implementation of __iter__ to allow dict(self) in client code""" |
|
76
|
|
|
return iter([(k, getattr(self, k)) for k in self.__dir__()]) |
|
77
|
|
|
|
|
78
|
|
|
def __str__(self): |
|
79
|
|
|
return '-'.join(str(getattr(self, _)) for _ in dir(self)) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if __name__ == '__main__': |
|
83
|
|
|
map_manager = MapManager() |
|
84
|
|
|
|
|
85
|
|
|
|