|
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(init=True, default=attr.Factory(lambda self: OrderedDict([(variable_dict['variable'], []) for variable_dict in self._variables]), takes_self=True)) |
|
53
|
|
|
|
|
54
|
|
|
@property |
|
55
|
|
|
def variables(self): |
|
56
|
|
|
return self._variables |
|
57
|
|
|
|
|
58
|
|
|
@variables.setter |
|
59
|
|
|
def variables(self, variables): |
|
60
|
|
|
self._variables = variables |
|
61
|
|
|
|
|
62
|
|
|
@property |
|
63
|
|
|
def valid_variables(self): |
|
64
|
|
|
return [self.valid_encoding(x) for x in self._variables] |
|
65
|
|
|
|
|
66
|
|
|
def valid_encoding(self, feature): |
|
67
|
|
|
return feature.valid_encoding(self.datapoints, self._feature_vectors[feature.variable]) |
|
68
|
|
|
|
|
69
|
|
|
def update(self, subject: Subject) -> None: |
|
70
|
|
|
self._feature_vectors[subject.variable] = subject.columns |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
@attr.s |
|
74
|
|
|
class FeatureManager: |
|
75
|
|
|
_feature_configuration = attr.ib(init=True) |
|
76
|
|
|
subject = attr.ib(init=True, default=Subject([])) |
|
77
|
|
|
|
|
78
|
|
|
@property |
|
79
|
|
|
def feature_configuration(self): |
|
80
|
|
|
return self._feature_configuration |
|
81
|
|
|
|
|
82
|
|
|
@feature_configuration.setter |
|
83
|
|
|
def feature_configuration(self, feature_configuration): |
|
84
|
|
|
self._feature_configuration = FeatureConfiguration(feature_configuration) |
|
85
|
|
|
|