-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaling.py
382 lines (310 loc) · 9.75 KB
/
scaling.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""Operators that scaling input distributions in the phenotype simulation.
Classes:
* Clip: A node that clips the input to be greater than or equal to some
minimum value and/or less than or equal to some maximum value.
* MinMaxScaler: A node that scales the input to be between 0 and 1 using
the minimum and maximum values of the input.
* StandardScaler: A node that scales the input to have mean 0 and standard
deviation 1 using the mean and standard deviation of the input.
* RobustScaler: A node that scales the input to have median 0 and interquartile
range 1 using the median and interquartile range of the input.
"""
import numpy as np
from scipy.stats import iqr
from pheno_sim.base_nodes import AbstractBaseFunctionNode
class Clip(AbstractBaseFunctionNode):
"""Operator node that clips the input based on a min and/or max value(s).
If a minimum value is provided, then all values less than the minimum
value are set to the minimum value. If a maximum value is provided, then
all values greater than the maximum value are set to the maximum value.
Example:
```python
>>> vals = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
>>> clip = Clip("clip", "vals", min_val=2, max_val=8)
>>> clip(vals)
array([[2, 2, 3],
[4, 5, 6],
[7, 8, 8]])
>>> clip = Clip("clip", "vals", min_val=4)
>>> clip(vals)
array([[4, 4, 4],
[4, 5, 6],
[7, 8, 9]])
>>> clip = Clip("clip", "vals", max_val=6)
>>> clip(vals)
array([[1, 2, 3],
[4, 5, 6],
[6, 6, 6]])
```
"""
def __init__(
self,
alias: str,
input_alias: str,
min_val: float = None,
max_val: float = None
):
"""Initialize Clip node.
Args:
alias: The alias of the node.
input_alias: The alias of the input node.
min_val (float, default None): The minimum value to clip to.
max_val (float, default None): The maximum value to clip to.
"""
super().__init__(alias)
self.inputs = input_alias
self.min_val = min_val
self.max_val = max_val
def run(self, input_vals):
"""Return the input clipped to the min and/or max value(s)."""
vals = input_vals.copy()
if self.min_val is not None:
vals = np.maximum(vals, self.min_val)
if self.max_val is not None:
vals = np.minimum(vals, self.max_val)
return vals
class MinMaxScaler(AbstractBaseFunctionNode):
"""Operator node that scales the input to be between 0 and 1.
The scaling is based on the minimum and maximum values of the input,
such that the minimum value of the input is mapped to 0 and the maximum
value of the input is mapped to 1. All other values are scaled linearly
between 0 and 1.
Scaling is done either by feature or among all features based on the
'by_feat' argument.
Example:
```python
>>> vals = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
>>> mms_by_feat = MinMaxScaler("mms", "vals", by_feat=True)
>>> mms_all = MinMaxScaler("mms", "vals", by_feat=False)
>>> print(mms_by_feat(vals))
array([[0. , 0.5, 1. ],
[0. , 0.5, 1. ],
[0. , 0.5, 1. ]])
>>> print(mms_all(vals))
array([[0. , 0.125, 0.25 ],
[0.375, 0.5 , 0.625],
[0.75 , 0.875, 1. ]])
```
"""
def __init__(self, alias: str, input_alias: str, by_feat: bool = True):
"""Initialize MinMaxScaler node.
Args:
alias: The alias of the node.
input_alias: The alias of the input node.
by_feat (bool, default True): Whether to scale by feature or
among all features.
"""
super().__init__(alias)
self.inputs = input_alias
self.by_feat = by_feat
def run(self, input_vals):
"""Return the input scaled to be between 0 and 1.
Args:
input_vals: The input values to scale.
Returns:
The input scaled to be between 0 and 1.
"""
if input_vals.ndim == 1:
input_vals = input_vals.reshape(1, -1)
if self.by_feat:
min_vals = input_vals.min(1, keepdims=True)
max_vals = input_vals.max(1, keepdims=True)
else:
min_vals = input_vals.min(keepdims=True)
max_vals = input_vals.max(keepdims=True)
# Avoid division by zero
ranges = max_vals - min_vals
if np.any(ranges == 0):
return np.where(ranges == 0, 0.5, (input_vals - min_vals) / ranges)
else:
return (input_vals - min_vals) / ranges
class StandardScaler(AbstractBaseFunctionNode):
"""Operator that scales input to have mean 0 and standard deviation 1.
Scaling is either done by feature or among all features based on the
'by_feat' argument.
Example:
```python
>>> vals = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
>>> std_scaler_by_feat = StandardScaler(
"std_scaler", "vals", by_feat=True
)
>>> std_scaler_all = StandardScaler(
"std_scaler", "vals", by_feat=False
)
>>> by_feat_out = std_scaler_by_feat(vals)
>>> all_out = std_scaler_all(vals)
>>> by_feat_out
array([[-1.225, 0. , 1.225],
[-1.225, 0. , 1.225],
[-1.225, 0. , 1.225]])
>>> all_out
array([[-1.549, -1.162, -0.775],
[-0.387, 0. , 0.387],
[ 0.775, 1.162, 1.549]])
>>> by_feat_out.mean(1)
array([0., 0., 0.])
>>> by_feat_out.std(1)
array([1., 1., 1.])
>>> all_out.mean(1)
array([-1.162, 0. , 1.162])
>>> all_out.mean()
0.0
>>> all_out.std()
1.0
"""
def __init__(self, alias: str, input_alias: str, by_feat: bool = True):
"""Initialize StandardScaler node.
Args:
alias: The alias of the node.
input_alias: The alias of the input node.
by_feat (bool, default True): Whether to scale by feature or
among all features.
"""
super().__init__(alias)
self.inputs = input_alias
self.by_feat = by_feat
def run(self, input_vals):
"""Scale the input to have mean 0 and standard deviation 1."""
if input_vals.ndim == 1:
input_vals = input_vals.reshape(1, -1)
if self.by_feat:
mean_vals = input_vals.mean(1, keepdims=True)
std_vals = input_vals.std(1, keepdims=True)
else:
mean_vals = input_vals.mean(keepdims=True)
std_vals = input_vals.std(keepdims=True)
# Avoid division by zero
if np.any(std_vals == 0):
return np.where(
std_vals == 0,
0.,
(input_vals - mean_vals) / std_vals
)
return (input_vals - mean_vals) / std_vals
class RobustScaler(AbstractBaseFunctionNode):
"""Operator that scales input to have median 0 and interquartile range 1.
Output interquartile range can be changed by using the 'out_iqr' argument.
Output median can be changed by using the 'out_median' argument.
Scaling is either done by feature or among all features based on the
'by_feat' argument.
Example:
```python
>>> vals = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
>>> robust_by_feat = RobustScaler("robust", "vals", by_feat=True)
>>> robust_all = RobustScaler("robust", "vals", by_feat=False)
>>> robust_by_feat(vals)
array([[-1., 0., 1.],
[-1., 0., 1.],
[-1., 0., 1.]])
>>> robust_all(vals)
array([[-1. , -0.75, -0.5 ],
[-0.25, 0. , 0.25],
[ 0.5 , 0.75, 1. ]])
>>> extreme_vals = np.array(
np.random.normal(0, 10, 1000).tolist() + [-1000000000000]
)
>>> robust_by_feat(extreme_vals)
array([[-7.454e-01, -6.908e-01, ..., -1.848e+00, -7.676e+10]])
>>> vals = np.random.uniform(0, 10, size=(1000, 5))
>>> robust_scaled = RobustScaler(
"robust", "vals", out_iqr=.5, out_median=1
)
>>> scaled_vals = robust_scaled(vals)
>>> np.median(scaled_vals, axis=0)
array([1., 1., 1., 1., 1.])
>>> iqr(scaled_vals, axis=0)
array([0.608, 0.546, 0.528, 0.483, 0.571])
```
"""
def __init__(
self,
alias: str,
input_alias: str,
by_feat: bool = True,
out_iqr: float = 1.0,
out_median: float = 0.0
):
"""Initialize RobustScaler node.
Args:
alias: The alias of the node.
input_alias: The alias of the input node.
by_feat (bool, default True): Whether to scale by feature or
among all features.
out_iqr (float, default 1.0): The interquartile range of the
output.
out_median (float, default 0.0): The median of the output.
"""
super().__init__(alias)
self.inputs = input_alias
self.by_feat = by_feat
self.out_iqr = out_iqr
self.out_median = out_median
def run(self, input_vals):
"""Scale the input to have median 0 and interquartile range 1."""
if input_vals.ndim == 1:
input_vals = input_vals.reshape(1, -1)
if self.by_feat:
medians = np.median(input_vals, axis=1, keepdims=True)
iqrs = iqr(input_vals, axis=1, keepdims=True)
else:
medians = np.median(input_vals, keepdims=True)
iqrs = iqr(input_vals, keepdims=True)
# To avoid division by zero, replace zero IQRs with 1
iqrs = np.where(iqrs == 0, 1, iqrs)
# Subtract the median and scale by the IQR
return (input_vals - medians) / iqrs * self.out_iqr + self.out_median
if __name__ == "__main__":
# Test MinMaxScaler
vals = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
mms_by_feat = MinMaxScaler("mms", "vals", by_feat=True)
mms_all = MinMaxScaler("mms", "vals", by_feat=False)
print(mms_by_feat(vals))
print(mms_all(vals))
# Test StandardScaler
std_scaler_by_feat = StandardScaler("std_scaler", "vals", by_feat=True)
std_scaler_all = StandardScaler("std_scaler", "vals", by_feat=False)
by_feat_out = std_scaler_by_feat(vals)
all_out = std_scaler_all(vals)
# Test RobustScaler
robust_by_feat = RobustScaler("robust", "vals", by_feat=True)
robust_all = RobustScaler("robust", "vals", by_feat=False)
robust_by_feat_out = robust_by_feat(vals)
robust_all_out = robust_all(vals)
extreme_vals = np.array(
np.random.normal(0, 10, 1000).tolist() + [-1000000000000]
)
extr_out = robust_by_feat(extreme_vals)
vals = np.random.uniform(0, 10, size=(1000, 5))
robust_scaled = RobustScaler(
"robust", "vals", out_iqr=.5, out_median=1
)
scaled_vals = robust_scaled(vals)
np.median(scaled_vals, axis=0)
iqr(scaled_vals, axis=0)
# Test Clip
clip = Clip("clip", "vals", min_val=2, max_val=8)
print(clip(vals))
clip = Clip("clip", "vals", min_val=4)
print(clip(vals))
clip = Clip("clip", "vals", max_val=6)
print(clip(vals))