-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeaturization.py
181 lines (165 loc) · 8.15 KB
/
featurization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import numpy as np
import warnings
class MassiveFeatureTransform(object):
"""MassiveFeatureTransform method for saturation mutagenesis at k positions
"""
def __init__(self,
raw_vars,
cat_features=None,
Y=None,
categories=None,
method='Avg',
mode='independent'):
"""Constructor for the MassiveFeatureTransform class.
Args:
raw_vars : Input experiments expressed using raw variable names
cat_features : Featurizations of different categories of each variable
Y : Input training measurements
categories : Categories/Choices of each variable
method : Method to use the measurements as features, must be 'Avg', 'Max',
'Min', 'Rank'
mode : Mode to infer features at different varible locations. 'independent'
means varibles independently vary, 'correlate' means all the variables
share the same features if the experimental choices are the same
"""
self._raw_vars = raw_vars
self._cat_features = cat_features
self._Y = Y
self._catergories = categories
self._mode = mode
self._method = method
if categories == None:
categories = []
for i in range(raw_vars.shape[1]):
if mode == 'independent':
choice = list(set(raw_vars[:, i]))
elif mode == 'correlate' or mode == 'hybrid':
choice = list(set(raw_vars.ravel()))
categories.append(choice)
for i in range(self._raw_vars.shape[1]):
ids = np.where(self._raw_vars[:, i]== '*')[0]
self._raw_vars[ids, i] = self._raw_vars[0, i]
self._categories = categories
if cat_features == None:
cat_features = []
for i in range(raw_vars.shape[1]):
feature_choice = np.empty(len(categories[i]))
for j in range(len(categories[i])):
if mode == 'independent' or mode == 'hybrid':
ids = np.where(raw_vars[:, i] == categories[i][j])[0]
if len(ids) == 0:
ids = []
for t in range(raw_vars.shape[1]):
ids.extend(
list(
np.where(
np.logical_and(
raw_vars[:, t] == categories[i][j],
raw_vars[0, t] == raw_vars[0, i]))
[0]))
elif mode == 'correlate':
ids = []
for t in range(raw_vars.shape[1]):
ids.extend(
list(
np.where(
np.logical_and(
raw_vars[:, t] == categories[i][j],
raw_vars[0, t] == raw_vars[0, i]))
[0]))
if len(ids) != 0:
if self._method == 'Avg':
feature_choice[j] = np.mean(Y[ids])
elif self._method == 'Max':
feature_choice[j] = np.max(Y[ids])
elif self._method == 'Min':
feature_choice[j] = np.min(Y[ids])
else:
feature_choice[j] = j
cat_features.append(feature_choice)
self._cat_features = cat_features
def transform(self, raw_vars):
"""Transform input experiments to standard encodings
Args:
raw_vars : Input experiments expressed using raw variable names
"""
for i in range(raw_vars.shape[1]):
ids = np.where(raw_vars[:, i]== '*')[0]
raw_vars[ids, i] = self._raw_vars[0, i]
transformed_feature = np.ones(raw_vars.shape) * np.nan
for i in range(raw_vars.shape[1]):
for j in range(len(self._categories[i])):
ids = np.where(raw_vars[:, i] == self._categories[i][j])[0]
transformed_feature[ids, i] = self._cat_features[i][j]
try:
np.isnan(transformed_feature.sum())
except InputError as err:
print('InputError: A wrong experimental variable at ',
np.argwhere(np.isnan(transformed_feature)))
return transformed_feature
class FewFeatureTransform(MassiveFeatureTransform):
"""FewChangeMeasurement method for non-saturation mutagenesis
"""
def __init__(self,
raw_vars,
cat_features=None,
Y=None,
categories=None,
max_change_length=None,
method='Avg',
mode='independent'):
"""Constructor for the FewFeatureTransform class.
Args:
raw_vars : Input experiments expressed using raw variable names
cat_features : Featurizations of different categories of each variable
Y : Input training measurements
categories : Categories/Choices of each variable
max_change_length : Maximum numbers of varibles changing in an experiment
method : Method to use the measurements as features, must be 'Avg', 'Max',
'Min', 'Rank'
mode : Mode to infer features at different varible locations. 'independent'
means varibles independently vary, 'correlate' means all the variables
share the same features if the experimental choices are the same
"""
super(FewFeatureTransform, self).__init__(
raw_vars=raw_vars,
cat_features=cat_features,
Y=Y,
categories=categories,
mode=mode,
method=method)
self._max_change_length = max_change_length
if self._max_change_length == None:
self._max_change_length = 0
for i in range(1, raw_vars.shape[0]):
curr_len = len(np.where(raw_vars[0, :] != raw_vars[i, :])[0])
if curr_len >= self._max_change_length:
self._max_change_length = curr_len
def transform(self, raw_vars):
"""Transform input experiments to standard encodings
Args:
raw_vars : Input experiments expressed using raw variable names
"""
for i in range(raw_vars.shape[1]):
ids = np.where(raw_vars[:, i]== '*')[0]
raw_vars[ids, i] = self._raw_vars[0, i]
for i in range(raw_vars.shape[0]):
curr_len = len(np.where(self._raw_vars[0, :] != raw_vars[i, :])[0])
if curr_len > self._max_change_length:
warnings.warn(
"Entire search space changes more varibles per experiment. Need to retransform the training space"
)
self._max_change_length = curr_len
transformed_feature = -np.ones((raw_vars.shape[0],self._max_change_length*2))
for i in range(raw_vars.shape[0]):
loc_change = np.where(raw_vars[i,:] != self._raw_vars[0,:])[0]
if len(loc_change) ==0:
transformed_feature[i, self._max_change_length:] = np.ones(self._max_change_length)*self._Y[0]
else:
transformed_feature[i, 0:len(loc_change)] = loc_change
for j in range(len(loc_change)):
feat = np.where(np.array(self._categories[loc_change[j]]) == raw_vars[i,loc_change[j]])[0]
transformed_feature[i, self._max_change_length+j] = self._cat_features[loc_change[j]][feat]
if len(loc_change) != self._max_change_length:
transformed_feature[i, self._max_change_length+len(loc_change):] = np.ones(self._max_change_length-len(loc_change))*self._Y[0]
return transformed_feature