| 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 |  |  |  |