1
|
|
|
from collections import OrderedDict |
2
|
|
|
import attr |
3
|
|
|
|
4
|
|
|
from so_magic.utils import Observer, Subject |
5
|
|
|
from so_magic.data.variables.types import NominalVariableType |
6
|
|
|
from so_magic.data.features.features import TrackingFeature |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
@attr.s |
10
|
|
|
class BaseFeatureSet: |
11
|
|
|
features = attr.ib(init=True, default=[]) |
12
|
|
|
|
13
|
|
|
def __len__(self): |
14
|
|
|
return len(self.features) |
15
|
|
|
|
16
|
|
|
def __getitem__(self, item): |
17
|
|
|
return self.features[item] |
18
|
|
|
|
19
|
|
|
def __iter__(self): |
20
|
|
|
return iter(self.features) |
21
|
|
|
|
22
|
|
|
@classmethod |
23
|
|
|
def from_raw_extractors(cls, data): |
24
|
|
|
"""Create a Feature for each of the lists in the input data (list). Inner lists must satisfy 0 < len(l)""" |
25
|
|
|
return [TrackingFeature.from_callable(*args) for args in data] |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class FeatureSet(BaseFeatureSet): |
29
|
|
|
|
30
|
|
|
@property |
31
|
|
|
def encoded(self): |
32
|
|
|
for feature in self.features: |
33
|
|
|
if str(feature.state) == 'encoded': |
34
|
|
|
yield feature |
35
|
|
|
|
36
|
|
|
@property |
37
|
|
|
def not_encoded(self): |
38
|
|
|
for feature in self.features: |
39
|
|
|
if str(feature.state) != 'encoded': |
40
|
|
|
yield feature |
41
|
|
|
|
42
|
|
|
@property |
43
|
|
|
def binnable(self): |
44
|
|
|
for feature in self.features: |
45
|
|
|
if not isinstance(feature.var_type, NominalVariableType): |
46
|
|
|
yield feature |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
@attr.s |
50
|
|
|
class FeatureConfiguration(Observer): |
51
|
|
|
variables = attr.ib(init=True) |
52
|
|
|
_feature_vectors = attr.ib(default=attr.Factory( |
53
|
|
|
lambda self: OrderedDict([(variable_dict['variable'], []) for variable_dict in self.variables]), |
54
|
|
|
takes_self=True)) |
55
|
|
|
|
56
|
|
|
@property |
57
|
|
|
def valid_variables(self): |
58
|
|
|
return [self.valid_encoding(x) for x in self.variables] |
59
|
|
|
|
60
|
|
|
def valid_encoding(self, feature): |
61
|
|
|
return feature.valid_encoding(self.datapoints, self._feature_vectors[feature.variable]) |
62
|
|
|
|
63
|
|
|
def update(self, subject: Subject) -> None: |
64
|
|
|
self._feature_vectors[subject.variable] = subject.columns |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
@attr.s |
68
|
|
|
class FeatureManager: |
69
|
|
|
_feature_configuration = attr.ib(init=True) |
70
|
|
|
subject = attr.ib(init=True, default=Subject([])) |
71
|
|
|
|
72
|
|
|
@property |
73
|
|
|
def feature_configuration(self): |
74
|
|
|
return self._feature_configuration |
75
|
|
|
|
76
|
|
|
@feature_configuration.setter |
77
|
|
|
def feature_configuration(self, feature_configuration): |
78
|
|
|
self._feature_configuration = FeatureConfiguration(feature_configuration) |
79
|
|
|
|