-
Notifications
You must be signed in to change notification settings - Fork 1
/
ops.py
386 lines (285 loc) · 9.32 KB
/
ops.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
383
384
385
386
import numpy as np
import pandas as pd
import multiprocessing as mp
from functools import partial
pool = None
def pipe_pre_select_parallel(actions, dfs, cpucore):
if cpucore == 1:
results = [pipe_pre_select(actions, df) for df in dfs]
return results
else:
global pool
if pool is None:
pool = mp.Pool(cpucore)
# results = pool.map(partial(pipe_pre_select, actions), dfs)
results = list(pool.imap(partial(pipe_pre_select, actions), dfs, chunksize=16))
# with mp.Pool(cpucore) as pool:
# results = pool.map(partial(pipe_pre_select, actions), dfs)
return results
def pipe(actions, x, df):
dim = None
if x.dtype.name == 'category':
for i, op in enumerate(actions):
if isinstance(op, GroupBy) and not isinstance(actions[i+1], Count):
x = pd.get_dummies(x).astype(float)
dim = x.shape[1]
index = x.columns
break
for op in actions:
if isinstance(op, Count):
break
else:
x = pd.get_dummies(x).astype(float)
dim = x.shape[1]
index = x.columns
for op in actions:
x, df = op(x, df)
if isinstance(x, float) and not np.isfinite(x) and dim is not None:
x = pd.Series(np.full(dim, np.nan), index=index)
return x
def pipe_pre_select(actions, df):
if not isinstance(df, pd.DataFrame):
df = df()
select_op = actions[0]
x = select_op(df)
return pipe(actions[1:], x, df)
def pipe_post_select(actions, df):
select_op = actions[-1]
x = select_op(df)
return pipe(actions[:-1], x, df)
class SelectOp:
def __init__(self, col, dtype):
# self.terminal = False
# self.destructive = False
self.col = col
self.dtype = dtype
def __call__(self, df: pd.DataFrame):
x = df[self.col].copy()
return x
def valid_path(self, path):
pass
def __repr__(self):
return f'Select({self.col})'
__str__ = __repr__
class UnaryOp:
def __init__(self):
self.terminal = False
self.destructive = False
def __call__(self, x: pd.Series, df: pd.DataFrame = None):
pass
def valid_path(self, path):
return True
def __repr__(self):
return self.__class__.__name__ + '()'
__str__ = __repr__
class AggerateOp(UnaryOp):
agg_name = None
def __init__(self):
self.terminal = True
self.destructive = True
def __call__(self, x, df: pd.DataFrame = None):
if isinstance(x, float) or len(x) == 0:
return np.nan, None
return x.agg(self.agg_name), None
def valid_path(self, path):
if len(path) and isinstance(path[-1], GroupBy):
return True
for op in path:
if isinstance(op, SortBy):
return False
else:
return True
class BinaryOp:
def __init__(self, by_col):
self.by_col = by_col
self.terminal = False
self.destructive = False
def __call__(self, x: pd.Series, df: pd.DataFrame = None):
pass
def valid_path(self, path):
return True
def __repr__(self):
return self.__class__.__name__ + f'({self.by_col})'
__str__ = __repr__
class TernaryOp:
def __init__(self, by_col, cond):
self.by_col = by_col
self.cond = cond
self.terminal = False
self.destructive = False
def __repr__(self):
return self.__class__.__name__ + f'({self.by_col}, {self.cond})'
__str__ = __repr__
def __call__(self, x: pd.Series, df: pd.DataFrame = None):
pass
def valid_path(self, path):
return True
class Min(AggerateOp):
agg_name = 'min'
class Max(AggerateOp):
agg_name = 'max'
class Sum(AggerateOp):
agg_name = 'sum'
class Mean(AggerateOp):
agg_name = 'mean'
class Std(AggerateOp):
agg_name = 'std'
class Ptp(AggerateOp):
q = None
def __call__(self, x, *args):
if isinstance(x, float) or len(x) == 0:
return np.nan, None
if isinstance(x, pd.Series):
return x.agg('ptp'), None
else: # GroupyBy
if np.all(x.count() == 0):
return x.agg(np.max), None # all nan, expedient
else:
return x.agg(np.ptp), None
class Count(AggerateOp):
agg_name = 'count'
class Percentile(AggerateOp):
q = None
def __call__(self, x, *args):
if isinstance(x, float) or len(x) == 0:
return np.nan, None
if isinstance(x, pd.Series) or isinstance(x, pd.DataFrame):
# if x.dtype.name == 'category':
# x = pd.get_dummies(x)
return x.agg(np.percentile, 0, self.q), None
else: # GroupBy
return x.agg(np.percentile, self.q), None
def valid_path(self, path):
# GroupBy -X> Percentile
if len(path) and isinstance(path[-1], GroupBy):
return False
for op in path:
if isinstance(op, SortBy):
return False
else:
return True
class Percentile5(Percentile):
q = 5
class Percentile10(Percentile):
q = 10
class Percentile25(Percentile):
q = 25
class Percentile50(Percentile):
q = 50
class Percentile75(Percentile):
q = 75
class Percentile90(Percentile):
q = 90
class Percentile95(Percentile):
q = 95
class Abs(UnaryOp):
def __call__(self, x: pd.Series, df: pd.DataFrame):
return x.abs(), df
def valid_path(self, path):
for op in path:
if isinstance(op, Abs) or isinstance(op, GroupBy):
return False
else:
return True
class Top1(UnaryOp):
def __init__(self):
super().__init__()
self.terminal = True
self.destructive = True
def __call__(self, x: pd.Series, df: pd.DataFrame):
if len(x) == 0:
return np.nan, None
return x.iloc[0], None
def valid_path(self, path):
if len(path) and isinstance(path[-1], GroupBy):
return False
for op in path:
if isinstance(op, SortBy):
return True
else:
return False
class Top5(UnaryOp):
k = 5
def __call__(self, x: pd.Series, df: pd.DataFrame):
return x.iloc[:self.k], df.iloc[:self.k]
def valid_path(self, path):
if len(path) and isinstance(path[-1], GroupBy):
return False
for op in path:
if isinstance(op, SortBy):
return True
else:
return False
class GroupBy(BinaryOp):
def __init__(self, by_col):
super().__init__(by_col)
self.destructive = True
def __call__(self, x: pd.Series, df: pd.DataFrame):
return x.groupby(df[self.by_col]), None
def valid_path(self, path):
for op in path:
if isinstance(op, GroupBy):
return False
if (isinstance(op, FilterBy) or isinstance(op, RetainBy)) and op.by_col == self.by_col:
return False
else:
return True
class FilterBy(TernaryOp):
def __call__(self, x: pd.Series, df: pd.DataFrame):
idx = (df[self.by_col] != self.cond)
return x[idx], df[idx]
def valid_path(self, path):
for op in path:
if op.destructive:
return False
elif (isinstance(op, FilterBy) or isinstance(op, RetainBy)) and op.by_col == self.by_col and op.cond == op.cond:
return False
return True
class RetainBy(TernaryOp):
def __call__(self, x: pd.Series, df: pd.DataFrame):
idx = (df[self.by_col] == self.cond)
return x[idx], df[idx]
def valid_path(self, path):
for op in path:
if op.destructive:
return False
elif isinstance(op, RetainBy) and op.by_col == self.by_col:
return False
elif isinstance(op, FilterBy) and op.by_col == self.by_col and op.cond == op.cond:
return False
return True
class SortBy(TernaryOp):
def __init__(self, by_col, ascending=True):
super().__init__(by_col, ascending)
self.ascending = ascending
def __call__(self, x: pd.Series, df: pd.DataFrame):
if self.by_col == '__self__':
idx = np.argsort(x)
else:
idx = np.argsort(df[self.by_col])
if not self.ascending:
idx = idx[::-1]
if df is None:
return x.iloc[idx], None
else:
return x.iloc[idx], df.iloc[idx]
def valid_path(self, path):
if len(path) and isinstance(path[-1], GroupBy): # GroupBy -X> SortBy
return False
elif self.by_col == '__self__':
for op in path:
if isinstance(op, SortBy) and op.by_col == '__self__':
return False
return True
for op in path:
if op.destructive:
return False
if isinstance(op, SortBy) and op.by_col == self.by_col:
return False
else:
return True
terminal_ops = [Min, Max, Sum, Mean, Std, Ptp, Count, Percentile5, Percentile10,
Percentile25, Percentile50, Percentile75, Percentile90, Percentile95]
tsfm_op = [Abs]
top_ops = [Top1, Top5]
by_ops = [GroupBy, FilterBy, RetainBy, SortBy]