| 1 |  |  | from abc import ABC, abstractmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | from typing import Iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import attr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from so_magic.utils import Observer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from .tabular_data_interface import TabularDataInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class DatapointsInterface(ABC): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     """The Datapoints interface gives access to the 'observations' property.""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     @abstractmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     def observations(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         raise NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class StructuredDataInterface(ABC): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     @abstractmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     def attributes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         raise NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class DatapointsFactory: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     constructors = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     def register_constructor(cls, name): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         def wrapper(subclass): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             cls.constructors[name] = subclass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             return subclass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     @classmethod | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     def create(cls, name, *args, **kwargs) -> Iterable: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if name not in cls.constructors: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             raise ValueError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 f"Request Engine of type '{name}'; supported are [{', '.join(sorted(cls.constructors.keys()))}]") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             return cls.constructors[name](*args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         except TypeError as e: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             print(f"Datapoints creation failed. Args: [{', '.join(f'{i}: {str(_)}' for i, _ in enumerate(args))}]") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             print(f"Kwargs: [{', '.join(f'{k}: {v}' for k, v in kwargs.items())}]") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             print(e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             import sys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             sys.exit(1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | @attr.s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | @DatapointsFactory.register_constructor('structured-data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | class StructuredData(DatapointsInterface, StructuredDataInterface): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     """Structured data. There are specific attributes/variables per observation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     Args: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         observations (object): a reference to an object that encapsulates structured data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     _observations = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     _attributes = attr.ib(init=True, converter=lambda input_value: [x for x in input_value]) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     @property | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     def attributes(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return self._attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     def observations(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return self._observations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     @observations.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     def observations(self, observations): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         self._observations = observations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | class AbstractTabularData(StructuredData, TabularDataInterface, ABC): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     def __iter__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return self.iterrows() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | @attr.s | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | @DatapointsFactory.register_constructor('tabular-data') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | class TabularData(AbstractTabularData): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     """Table-like datapoints that are loaded in memory""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     def columns(self) -> Iterable: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     def rows(self) -> Iterable: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     retriever = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     iterator = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     mutator = attr.ib(init=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     def attributes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return self.iterator.columnnames(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     def column(self, identifier): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return self.retriever.column(identifier, self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     def row(self, identifier): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return self.retriever.row(identifier, self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     def get_numerical_attributes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return self.retriever.get_numerical_attributes(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     def get_categorical_attributes(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return iter(set(self.attributes) - set([_ for _ in self.retriever.get_numerical_attributes(self)])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     def nb_columns(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         return self.retriever.nb_columns(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     @property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     def nb_rows(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return self.retriever.nb_rows(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     def __len__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return self.retriever.nb_rows(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     def __iter__(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return self.iterator.iterrows(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     def iterrows(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return self.iterator.iterrows(self) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     def itercolumns(self): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  |         return self.iterator.itercolumns(self) | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |