-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_generation.py
More file actions
199 lines (156 loc) · 5.86 KB
/
error_generation.py
File metadata and controls
199 lines (156 loc) · 5.86 KB
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
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
""""""
from copy import deepcopy
import numpy as np
from selection import RandomSelector, PairSelector
class ErrorGenerator:
def __init__(self):
self.name = 'error generator'
def __enter__(self):
pass
def __exit__(self):
pass
def on(self, data, columns=None, row_fraction=.2):
return self.run(data, columns, row_fraction)
def run(self, data, columns=None, row_fraction=.2):
return data
class Anomalies(ErrorGenerator):
def __init__(self):
ErrorGenerator.__init__(self)
self.name = 'numeric anomalies'
def get_anomaly(self, mean, std):
factor = np.random.random() - .5
return mean + np.sign(factor)*(3. + 2 * factor)*std
def on(self, data, columns=None, row_fraction=.2):
return self.run(data, columns, row_fraction)
def run(self, data, columns=None, row_fraction=.2):
data = deepcopy(data)
selector = RandomSelector(row_fraction=row_fraction).on(data, columns)
for col in selector.keys():
idx = selector[col]
mean, std = np.mean(data.iloc[:, col]), np.std(data.iloc[:, col])
data.iloc[idx, col] = (np.vectorize(self.get_anomaly)(mean, std)
.astype(data.dtypes[col]))
return data
class ExplicitMissingValues(ErrorGenerator):
def __init__(self):
ErrorGenerator.__init__(self)
self.name = 'explicit misvals'
def apply(self, function, data, cell_ids):
data = deepcopy(data)
for col, idx in cell_ids.items():
data.iloc[idx, col] = np.vectorize(function)(data.iloc[idx, col])
return data
def run(self, data, columns=None, row_fraction=.2):
return self.apply(
lambda x: np.nan, data, RandomSelector(
row_fraction=row_fraction).on(data, columns))
class ImplicitMissingValues(ErrorGenerator):
def __init__(self):
ErrorGenerator.__init__(self)
self.name = 'implicit misvals'
def apply(self, function, data, cell_ids):
data = deepcopy(data)
for col, idx in cell_ids.items():
data.iloc[idx, col] = np.vectorize(function)(data.iloc[idx, col])
return data
def run(self, data, columns=None, row_fraction=.2):
tmp = self.apply(
lambda x: 9999,
data, RandomSelector(
row_fraction=row_fraction).on(data, columns='numeric'))
return self.apply(
lambda x: 'undefined',
tmp, RandomSelector(
row_fraction=row_fraction).on(data, columns='string'))
class Typos(ErrorGenerator):
class __Typos:
def __init__(self):
self.name = 'typos'
self.keyApprox = {
'q': "qwasedzx",
'w': "wqesadrfcx",
'e': "ewrsfdqazxcvgt",
'r': "retdgfwsxcvgt",
't': "tryfhgedcvbnju",
'y': "ytugjhrfvbnji",
'u': "uyihkjtgbnmlo",
'i': "iuojlkyhnmlp",
'o': "oipklujm",
'p': "plo['ik",
'a': "aqszwxwdce",
's': "swxadrfv",
'd': "decsfaqgbv",
'f': "fdgrvwsxyhn",
'g': "gtbfhedcyjn",
'h': "hyngjfrvkim",
'j': "jhknugtblom",
'k': "kjlinyhn",
'l': "lokmpujn",
'z': "zaxsvde",
'x': "xzcsdbvfrewq",
'c': "cxvdfzswergb",
'v': "vcfbgxdertyn",
'b': "bvnghcftyun",
'n': "nbmhjvgtuik",
'm': "mnkjloik"}
instance = None
def __init__(self):
if not Typos.instance:
Typos.instance = Typos.__Typos()
def __getattr__(self, name):
return getattr(self.instance, name)
def apply(self, function, data, cell_ids):
data = deepcopy(data)
for col, idx in cell_ids.items():
data.iloc[idx, col] = (np.vectorize(function)(data.iloc[idx, col])
.astype(data.dtypes[col]))
return data
def run(self, data, columns=None, row_fraction=.2):
return self.apply(
self.butterfinger,
data, RandomSelector(
row_fraction=row_fraction).on(data, columns='string'))
def butterfinger(self, text, prob=.05):
def foo(letter):
if letter.lower() in self.keyApprox.keys():
cond = np.random.random() <= prob
tmp = np.random.choice(
list(self.keyApprox[letter.lower()])) if cond else letter
return tmp.upper() if letter.isupper() else tmp
return letter
return np.array("".join(map(foo, text)))
class SwapFields(ErrorGenerator):
def __init__(self):
ErrorGenerator.__init__(self)
self.name = 'swap fields'
def apply(self, function, data, cell_ids):
df = deepcopy(data)
((lc, lr), (rc, rr)) = cell_ids.items()
# TODO: swap cols
(df.iloc[lr, lc], df.iloc[rr, rc]) = (df.iloc[rr, rc].values,
df.iloc[lr, lc].values)
return df
def run(self, data, columns=None, row_fraction=.2):
# print(columns)
return self.apply(None, data, PairSelector(
row_fraction=row_fraction).on(data, columns))
class MyErrorGenerator(ErrorGenerator):
def __init__(self):
ErrorGenerator.__init__(self)
self.name = 'partitioned data corruption'
def on(self, data):
return self.run(data)
def apply(self, ):
pass
def run(self, data):
pass
def main():
"""
"""
error_gen = Typos()
print(error_gen.butterfinger("Hello World"))
print(np.vectorize(error_gen.butterfinger)(["Hello", "World"]))
if __name__ == "__main__":
main()