forked from HoloClean/holoclean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholoclean.py
229 lines (209 loc) · 7.41 KB
/
holoclean.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
from dataset import Dataset
from dcparser import Parser
from domain import DomainEngine
from detect import DetectEngine
from repair import RepairEngine
from evaluate import EvalEngine
# Arguments for HoloClean
arguments = [
(('-u', '--db_user'),
{'metavar': 'DB_USER',
'dest': 'db_user',
'default': 'holocleanuser',
'type': str,
'help': 'User for DB used to persist state.'}),
(('-p', '--password', '--pass'),
{'metavar': 'PASSWORD',
'dest': 'db_pwd',
'default': 'abcd1234',
'type': str,
'help': 'Password for DB used to persist state.'}),
(('-h', '--host'),
{'metavar': 'HOST',
'dest': 'db_host',
'default': 'localhost',
'type': str,
'help': 'Host for DB used to persist state.'}),
(('-d', '--database'),
{'metavar': 'DATABASE',
'dest': 'db_name',
'default': 'holo',
'type': str,
'help': 'Name of DB used to persist state.'}),
(('-t', '--threads'),
{'metavar': 'THREADS',
'dest': 'threads',
'default': 20,
'type': int,
'help': 'How many threads to use for parallel execution.'}),
(('-dbt', '--timeout'),
{'metavar': 'TIMEOUT',
'dest': 'timeout',
'default': 60000,
'type': int,
'help': 'Timeout for expensive featurization queries.'}),
(('-s', '--seed'),
{'metavar': 'SEED',
'dest': 'seed',
'default': 45,
'type': int,
'help': 'The seed to be used for torch.'}),
(('-l', '--learning-rate'),
{'metavar': 'LEARNING_RATE',
'dest': 'learning_rate',
'default': 0.001,
'type': float,
'help': 'The learning rate used during training.'}),
(('-k', '--pruning-topk'),
{'metavar': 'PRUNING_TOPK',
'dest': 'pruning_topk',
'default': 10,
'type': float,
'help': 'Top-k used for domain pruning step.'}),
(('-o', '--optimizer'),
{'metavar': 'OPTIMIZER',
'dest': 'optimizer',
'default': 'adam',
'type': str,
'help': 'Optimizer used for learning.'}),
(('-e', '--epochs'),
{'metavar': 'LEARNING_EPOCHS',
'dest': 'epochs',
'default': 100,
'type': float,
'help': 'Number of epochs used for training.'}),
(('-w', '--weight_decay'),
{'metavar': 'WEIGHT_DECAY',
'dest': 'weight_decay',
'default': 0.0,
'type': float,
'help': 'Weight decay across iterations.'}),
(('-m', '--momentum'),
{'metavar': 'MOMENTUM',
'dest': 'momentum',
'default': 0.0,
'type': float,
'help': 'Momentum for SGD.'}),
(('-b', '--batch-size'),
{'metavar': 'BATCH_SIZE',
'dest': 'batch_size',
'default': 1,
'type': int,
'help': 'The batch size during training.'})
]
# Flags for Holoclean mode
flags = [
(tuple(['--verbose']),
{'default': False,
'dest': 'verbose',
'action': 'store_true',
'help': 'verbose'}),
(tuple(['--bias']),
{'default': False,
'dest': 'bias',
'action': 'store_true',
'help': 'Use bias term'})
]
class HoloClean:
"""
Main entry point for HoloClean.
It creates a HoloClean Data Engine
"""
def __init__(self, **kwargs):
"""
Constructor for Holoclean
:param kwargs: arguments for HoloClean
"""
# Initialize default execution arguments
arg_defaults = {}
for arg, opts in arguments:
if 'directory' in arg[0]:
arg_defaults['directory'] = opts['default']
else:
arg_defaults[opts['dest']] = opts['default']
# Initialize default execution flags
for arg, opts in flags:
arg_defaults[opts['dest']] = opts['default']
for key in kwargs:
arg_defaults[key] = kwargs[key]
# Initialize additional arguments
for (arg, default) in arg_defaults.items():
setattr(self, arg, kwargs.get(arg, default))
# Init empty session collection
self.session = Session(arg_defaults)
class Session:
"""
Session class controls the entire pipeline of HC
"""
def __init__(self, env, name="session"):
"""
Constructor for Holoclean session
:param env: Holoclean environment
:param name: Name for the Holoclean session
"""
# Initialize members
self.name = name
self.env = env
self.ds = Dataset(name,env)
self.dc_parser = Parser(env, self.ds)
self.domain_engine = DomainEngine(env, self.ds)
self.detect_engine = DetectEngine(env, self.ds)
self.repair_engine = RepairEngine(env, self.ds)
self.eval_engine = EvalEngine(env, self.ds)
def load_data(self, name, f_path, f_name, na_values=None):
status, load_time = self.ds.load_data(name, f_path,f_name, na_values=na_values)
print(status)
if self.env['verbose']:
print('Time to load dataset: %.2f secs'%load_time)
def load_dcs(self, f_path, f_name):
status, load_time = self.dc_parser.load_denial_constraints(f_path, f_name)
print(status)
if self.env['verbose']:
print('Time to load dirty data: %.2f secs'%load_time)
def get_dcs(self):
return self.dc_parser.get_dcs()
def detect_errors(self, detect_list):
status, detect_time = self.detect_engine.detect_errors(detect_list)
print(status)
if self.env['verbose']:
print('Time to detect errors: %.2f secs'%detect_time)
def setup_domain(self):
status, domain_time = self.domain_engine.setup()
print(status)
if self.env['verbose']:
print('Time to setup the domain: %.2f secs'%domain_time)
def repair_errors(self, featurizers):
status, feat_time = self.repair_engine.setup_featurized_ds(featurizers)
print(status)
if self.env['verbose']:
print('Time to featurize data: %.2f secs'%feat_time)
status, setup_time = self.repair_engine.setup_repair_model()
print(status)
if self.env['verbose']:
print('Time to setup repair model: %.2f secs' % feat_time)
status, fit_time = self.repair_engine.fit_repair_model()
print(status)
if self.env['verbose']:
print('Time to fit repair model: %.2f secs'%fit_time)
status, infer_time = self.repair_engine.infer_repairs()
print(status)
if self.env['verbose']:
print('Time to infer correct cell values: %.2f secs'%infer_time)
status, time = self.ds.get_inferred_values()
print(status)
if self.env['verbose']:
print('Time to collect inferred values: %.2f secs' % time)
status, time = self.ds.get_repaired_dataset()
print(status)
if self.env['verbose']:
print('Time to store repaired dataset: %.2f secs' % time)
def evaluate(self, f_path, f_name, get_tid, get_attr, get_value, na_values=None):
name = self.ds.raw_data.name + '_clean'
status, load_time = self.eval_engine.load_data(name, f_path, f_name, get_tid, get_attr, get_value, na_values=na_values)
print(status)
if self.env['verbose']:
print('Time to evaluate repairs: %.2f secs'%load_time)
status, report_time = self.eval_engine.eval_report()
print(status)
if self.env['verbose']:
print('Time to generate report: %.2f secs' % report_time)