-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQEvaluator.py
404 lines (309 loc) · 13.1 KB
/
DQEvaluator.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import pandas as pd
import numpy as np
from time import gmtime, strftime
import json
import os
import csv
# -------- BATCH -----------
def completeness_missing(df):
"""
This function calculate the completeness considering the number of missing values
:param df: The Dataframe to evaluate
:return: The completeness of the provided Dataframe
"""
tot_values = df.count().sum()
completeness = 1 - ((pd.isna(df).sum().sum()) / tot_values)
return completeness
def accuracy_evaluation_boolean_categ(attribute, values_range):
"""
This function calculate the accuracy considering if each value is in the provided range.
:param attribute: The attribute to evaluate
:param values_range: The range of values admitted for each attribute in analysis
e.g.: values_range={'type': 'categ', 'interval': ['e','p']}
:return: The accuracy of the provided attribute
"""
accuracy_bool = []
for row in attribute:
if row in values_range['interval']:
accuracy_bool.append(True)
else:
accuracy_bool.append(False)
return accuracy_bool.count(True) / len(accuracy_bool)
def accuracy_evaluation_boolean_float(attribute, values_range):
"""
This function calculate the accuracy considering if each value is in the provided range.
:param df: The attribute to evaluate
:param values_range: The range of values admitted for the attribute in analysis
e.g.: values_range={"type": "float", "interval": {"min": 5, "max": 100} }
:return: The accuracy of the provided attribute
"""
accuracy_bool=[]
for row in attribute:
if (row >=values_range['interval']['min']) & (row <= values_range['interval']['max']):
accuracy_bool.append(True)
else:
accuracy_bool.append(False)
return accuracy_bool.count(True) / len(accuracy_bool)
def accuracy_evaluation_distance(attribute, values_range):
"""
This function calculate the accuracy considering the distance between the expected interval
:param df: The Dataframe to evaluate
:param eMean: The expected mean of each attribute to evaluate
:param eInterval: the expected interval of each attribute to evaluate
:return: The accuracy of the provided Dataframe
"""
minV = values_range['interval']['min']
maxV=values_range['interval']['max']
eMean=values_range['interval']['mean']
eInterval=maxV-minV
accuracy_distance = []
# accuracy max(0, 1-|(x-eMean)/(eInterval*0.5)|) eInterval= 2*std
for row in attribute:
accuracy_distance.append(max(0, 1 - abs((row - eMean) / (eInterval * 0.5))))
return np.mean(accuracy_distance)
def consistency_evaluation(df, rules):
"""
This function calculate the consistency considering the support of the association rules provided.
:param df: The Dataframe to evaluate
:param rules: The association rules to use for the evaluation
:return: The consistency of the provided Dataframe
"""
#remove rules about not present columns
rules_to_delete=[]
for r in rules:
for el in r:
for a in el:
if a not in df.columns:
rules_to_delete.append(r)
for rul in rules_to_delete:
try:
rules.remove(rul)
except:
pass
#if there are not association rules, return 0
if len(rules)==0:
print('Impossibile to evaluate consistency dimension without rules')
return 0
rules_consistency = []
for r in rules:
ant_values = []
weighted_consistency = []
antec = r[0]
cons = r[1]
for col in antec:
ant_values.append(df[col].unique())
# single antecedent
if len(ant_values) == 1:
for a1 in ant_values[0]:
if not pd.isna(a1):
subset = df.loc[df[antec[0]] == a1]
denom = subset.shape[0]
numerat = max(subset[cons[0]].value_counts())
consistency = numerat / denom
weighted_consistency.append(consistency * len(subset) / len(df))
# print("ANTEC: ", antec[0], " = ", a1, " consistency: ", consistency)
# two antecedents
else:
for a1 in ant_values[0]:
for a2 in ant_values[1]:
subset = df.loc[df[antec[0]] == a1].loc[df[antec[1]] == a2]
if not subset.empty:
denom = len(subset)
numerat = max(subset[cons[0]].value_counts())
consistency = numerat / denom
weighted_consistency.append(consistency * len(subset) / len(df))
# print("ANTEC: ", antec[0], " = ", a1, " ", antec[1], " = ", a2, " consistency: ",consistency)
# consistency delle combinizioni di valori degli antecedenti di una data regola
# print("Rule ", r, "weighted consistency: ", weighted_consistency)
# consistency della regola (somma delle consistency pesate)
# print("Rule ", r, "consistency: ", sum(weighted_consistency), "\n")
rules_consistency.append(sum(weighted_consistency))
# elenco delle consistency delle varie regole
# print("Rules consistency: ", rules_consistency)
consistency_dataset = sum(rules_consistency) / len(rules_consistency)
return consistency_dataset
def completeness_frequency(time_column, frequency):
recordCount=len(time_column)
for time in time_column:
time = pd.to_datetime(time)
interval=max(time_column)-min(time_column)
interval=interval.total_seconds()
completeness_frequency= min(1, recordCount/(frequency*interval))
return completeness_frequency
# ------------- STREAM ---------------
def separe_windows(df, window_size):
number_of_windows = (df.shape[0] + (window_size - 1)) // window_size
subsets = []
for i in range(number_of_windows): # create subsets of the sliding windows
subsets.append(df.iloc[i * window_size:(i + 1) * window_size])
return subsets
def stream_completeness_missing(df, window_size=10):
number_of_windows = (df.shape[0] + (window_size - 1)) // window_size
subsets=separe_windows(df, window_size)
completeness_subsets = []
for i in range(number_of_windows): # completeness
tot_values = subsets[i].count().sum()
completeness_subset = (1 - ((pd.isna(subsets[i]).sum().sum()) / tot_values))
weighted_completeness = (completeness_subset * (i + 1)) / number_of_windows
completeness_subsets.append(weighted_completeness)
completeness = 1 / (number_of_windows - ((number_of_windows - 1) / 2)) * sum(completeness_subsets)
return completeness
def stream_accuracy_boolean_categ(attribute,values_range, window_size=10):
number_of_windows = (attribute.shape[0] + (window_size - 1)) // window_size
subsets=separe_windows(attribute, window_size)
accuracy_subsets = []
for i in range(number_of_windows):
accuracy_subset = accuracy_evaluation_boolean_categ(subsets[i], values_range)
weighted_accuracy = (accuracy_subset * (i + 1)) / number_of_windows
accuracy_subsets.append(weighted_accuracy)
accuracy = 1 / (number_of_windows - ((number_of_windows - 1) / 2)) * sum(accuracy_subsets)
return accuracy
def stream_accuracy_boolean_float(attribute,values_range, window_size=10):
number_of_windows = (attribute.shape[0] + (window_size - 1)) // window_size
subsets=separe_windows(attribute, window_size)
accuracy_subsets = []
for i in range(number_of_windows):
accuracy_subset = accuracy_evaluation_boolean_float(subsets[i], values_range)
weighted_accuracy = (accuracy_subset * (i + 1)) / number_of_windows
accuracy_subsets.append(weighted_accuracy)
accuracy = 1 / (number_of_windows - ((number_of_windows - 1) / 2)) * sum(accuracy_subsets)
return accuracy
def timeliness_evaluation(time_column, volatility):
current_time=strftime("%Y-%m-%d %H:%M:%S", gmtime())
current_time=pd.to_datetime(str(current_time))
currencies=[]
for time in time_column:
time=pd.to_datetime(time)
currencies.append(current_time-time)
timeliness=[]
for currency in currencies:
timeliness.append(max(0,1-currency.total_seconds()/volatility))
return np.mean(timeliness)
def volume_calc(source):
count=0
with open(source, newline='') as f:
reader = csv.reader(f)
for row in reader:
count=count+1
return count
def DQEvaluator(method, attribute_list=None):
"""
This function evaluate the DQ of the provided dataframe wrt the dq metadata specification
:return: the DQ dimensions evaluated
"""
metrics = {
'batch': {
'global': {
'consistency': consistency_evaluation,
'completeness_missing': completeness_missing
},
'attribute': {
'float': {
'accuracy': accuracy_evaluation_boolean_float},
'timestamp': {
'timeliness': timeliness_evaluation},
'categ': {
'accuracy': accuracy_evaluation_boolean_categ}
}
},
'stream': {
'global': {
'completeness_missing': stream_completeness_missing,
'consistency': consistency_evaluation
},
'attribute': {
'float': {
'accuracy': stream_accuracy_boolean_float},
'timestamp': {
'timeliness': timeliness_evaluation,
'completeness_frequency': completeness_frequency},
'categ':
{'accuracy': stream_accuracy_boolean_categ}
}
}
}
config_dict = json.load(open("config_files/config_dictionary.json"))
config = json.load(open("config_files/"+config_dict[method]))
df=pd.read_csv(method)
source_type=config['source_type']
datatypes=config['datatypes']
rules=config['association_rules']
values_range=config['values_range']
volatility=0
try:
volatility = config['volatility']
except:
pass
try:
frequency = config['update_frequency']
except:
pass
# if filter is not None:
# selection
# for p in filter:
# values = filter[p]
# df = df.loc[df[p].isin(values)]
if attribute_list is not None:
# projection
df = df[attribute_list]
completeness=0
consistency=0
accuracy = []
accuracy_mean = 0
accuracy_min = 0
accuracy_max = 0
timeliness=0
complet_freq=0
volume=0
# DIM GLOBALI
for i in metrics[source_type]['global']:
if i == 'completeness_missing':
parameters = {'df': df}
completeness_metric = metrics[source_type]['global'][i]
completeness=completeness_metric(**parameters)
if i == 'consistency':
parameters = {'df': df, 'rules': rules}
consistency_metric = metrics[source_type]['global'][i]
consistency=consistency_metric(**parameters)
#delete attribute not present in the df, from the datatypes dictionary
attr_to_delete=[]
for attr in datatypes:
if attr not in df.columns:
attr_to_delete.append(attr)
for attr in attr_to_delete:
try:
datatypes.pop(attr)
except:
pass
if datatypes :
for attr in datatypes: #cicla sugli attributi
for dim in metrics[source_type]['attribute'][datatypes[attr]]: #cicla sulle dimensioni da valutare sull'attributo
if dim=='accuracy':
accuracy_metric = metrics[source_type]['attribute'][datatypes[attr]]['accuracy']
parameters = {'attribute': df[attr], 'values_range': values_range[attr]}
accuracy.append(accuracy_metric(**parameters))
if dim=='timeliness':
if volatility!= 0:
time_column=pd.to_datetime(df[attr])
timeliness_metric = metrics[source_type]['attribute'][datatypes[attr]][
'timeliness']
parameters= {'time_column': time_column, 'volatility':volatility}
timeliness=timeliness_metric(**parameters)
if dim=='completeness_frequency':
time_column=pd.to_datetime(df[attr])
completeness_frequency_metric=metrics[source_type]['attribute'][datatypes[attr]][
'completeness_frequency']
parameters = {'time_column': time_column, 'frequency': frequency}
complet_freq=completeness_frequency_metric(**parameters)
if accuracy:
accuracy_mean=np.mean(accuracy)
if complet_freq:
completeness=(completeness+complet_freq)/2
volume=volume_calc(method)
results={'completeness': completeness,
'consistency': consistency,
'accuracy': accuracy_mean,
'timeliness': timeliness,
'volume': volume
}
return results