-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdq_assessment.py
More file actions
1292 lines (1083 loc) · 72.8 KB
/
Copy pathdq_assessment.py
File metadata and controls
1292 lines (1083 loc) · 72.8 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import configparser
import logging
import csv
from jinja2 import Environment, FileSystemLoader
import time
import glob
from collections import defaultdict
from rdflib.namespace import DCTERMS, VOID, SH, FOAF
from shacl_shape_builder import SHACLShapeBuilder
from utils import *
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="rdflib.term")
logging.basicConfig(level=logging.INFO)
class DQAssessment:
def __init__(self, config_path,
metadata_shapes=True,
data_shapes=True,
vocab_shapes=True):
self.metadata_shapes = metadata_shapes
self.data_shapes = data_shapes
self.config = self._load_config(config_path)
self.vocab_shapes = vocab_shapes
self._init_paths_and_params()
self.shape_builder = SHACLShapeBuilder(self)
self.counter_shapes = 0
self.total_elapsed_time = 0
self.data_shapes_elapsed_time = 0
self.vocab_shapes_elapsed_time = 0
self.metadata_shapes_elapsed_time = 0
self.graph_profile = None
def _load_config(self, path):
try:
config = configparser.ConfigParser()
read_files = config.read(path)
if not read_files:
raise FileNotFoundError(f"Config file not found at path: {path}")
return config
except configparser.ParsingError as e:
raise ValueError(f"Failed to parse config file '{path}': {e}")
def _init_paths_and_params(self):
settings = self.config['settings']
self.graph_file_path = settings['graph_file']
self.vocab_names = [v.strip() for v in settings["vocabularies"].split(",")]
self.graph_file_path = settings['graph_file']
self.graph_file_format = settings['graph_file_format']
self.dataset_name = settings["dataset_name"]
self.dataset_name = self.dataset_name.lower().replace(" ", "_")
self.metadata_file = settings['metadata_file']
self.metadata_file_format = settings['metadata_file_format']
# Parameters for SHACL shapes
self.metadata_class = settings['metadata_class']
self.type_property = settings['type_property']
self.labeling_property = settings['labeling_property']
self.description_property = settings['description_property']
self.interlinking_property = settings['interlinking_property']
self.base_namespace = settings['base_namespace'] # this is not being used!!!!! I think I didn't implement the shape that checks for the uriSpace...
self.uris_max_length = settings['uris_max_length']
self.vocab_names = [v.strip() for v in settings["vocabularies"].split(",")]
# Shapes templates
env = Environment(loader=FileSystemLoader("dq_assessment/shapes"))
self.data_template = env.get_template("data_shapes.template.ttl")
self.metadata_template = env.get_template("metadata_shapes.template.ttl")
self.vocabs_template = env.get_template("vocabulary_shapes.template.ttl")
self.regex_pattern = None
self.uri_space = None
self.aggregate_dict_counter = {}
def run(self):
self.profile_data()
logging.info(f"Finished profiling graph and vocabularies. Saved results in {PROFILE_DATASETS_FOLDER_PATH} & {PROFILE_VOCABULARIES_FOLDER_PATH}")
initial_time = time.time() # start of validation
# ---- Validate metadata shapes ----
if self.metadata_shapes and self.metadata_file:
validation_time = self.validate_metadata_shapes()
self.metadata_shapes_elapsed_time = validation_time
logging.info(f"Finished validating metadata shapes. \n Saved DQA results in '{DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)}dq_assessment_{self.dataset_name}_metadata.json'. Validation time: {validation_time}")
# ---- Validate data shapes ----
if self.data_shapes:
validation_time = self.validate_data_shapes()
self.data_shapes_elapsed_time = validation_time
logging.info(f"Finished validating data shapes. Saved DQA results in '{DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)}/dq_assessment_{self.dataset_name}_data.json'. Validation time: {validation_time}")
# ---- Validate shapes against vocabularies ----
if self.vocab_shapes:
validation_time = self.validate_vocabulary_shapes()
self.vocab_shapes_elapsed_time = validation_time
logging.info(f"Finished validating shapes against vocabularies. Saved DQA results in '{DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)}dq_assessment_[vocab_name].json'. Validation time: {validation_time}")
final_time = time.time() # end of validation
self.total_elapsed_time = final_time - initial_time
logging.info(f"Total elapsed time: {self.total_elapsed_time}")
# Remove specific .json result file created for shapes that need instantiation from vocabularies
if os.path.exists(DQ_MEASURES_DATA_SPECIFIC_TEMPLATE_FILE_PATH):
os.remove(DQ_MEASURES_DATA_SPECIFIC_TEMPLATE_FILE_PATH)
if os.path.exists(DQ_MEASURES_VOCABULARIES_SPECIFIC_TEMPLATE_FILE_PATH):
os.remove(DQ_MEASURES_VOCABULARIES_SPECIFIC_TEMPLATE_FILE_PATH)
self.create_dq_results_csv()
def profile_data(self):
if self.data_shapes:
graph_profile_output_path = f'{PROFILE_DATASETS_FOLDER_PATH}/{self.dataset_name}.json'
self.graph_profile = profile_graph(self, graph_profile_output_path)
logging.info(f"Graph profile saved in {graph_profile_output_path}.")
if self.vocab_shapes:
# Maps a vocabulary with its namespace
dict_vocab_file = {}
for vocab in self.vocab_names:
vocab_ns = profile_vocab(self, vocab)
vocab_name = self.config[vocab]["vocab_name"]
dict_vocab_file[vocab_name] = vocab_ns
self.dict_vocab_ns_file = dict_vocab_file
def validate_metadata_shapes(self):
"""
Validates shapes against the metadata file
"""
validation_time = 0
# Generate metadata shapes
shape_graph = create_shape_graph(self.metadata_template.module.metadata_shape(self.metadata_class))
# Save shapes
folder_path = f'{DATASETS_FOLDER_PATH}/{self.dataset_name}/shapes'
os.makedirs(folder_path, exist_ok=True)
file_path = f'{folder_path}/metadata_shapes.ttl'
shape_graph.serialize(destination=file_path, format='turtle')
logging.info(f'Metadata shapes for dataset {self.dataset_name} saved in {file_path}')
# Run validation
_, val_graph, _ , _, validation_time = validate_shacl_constraints(None, self.metadata_file, self.metadata_file_format, shape_graph, vocabs=None, config=None)
# Process & store validation results
self.process_validation_result_metadata(val_graph)
logging.info(f"Finished DQA for metadata file. Results saved in '{DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)}/dq_assessment_{self.dataset_name}_metadata.json'. \n")
return validation_time
def validate_vocabulary_shapes(self):
validation_time = 0
# Stores for each vocabulary the classes used in the dataset
class_vocab_map = {}
for class_ in self.graph_profile['classes']:
class_ns = get_ns(class_)
for vocab in self.vocab_names:
vocab_name = self.config[vocab]["vocab_name"]
vocab_ns = self.dict_vocab_ns_file[vocab_name]
if class_ns == vocab_ns:
if vocab_name not in class_vocab_map:
class_vocab_map[vocab_name] = []
class_vocab_map[vocab_name].append(class_)
# Stores for each vocabulary the properties used in the dataset
property_vocab_map = {}
for prop_ in self.graph_profile['properties']:
prop_ns = get_ns(prop_)
for vocab in self.vocab_names:
vocab_name = self.config[vocab]["vocab_name"]
vocab_ns = self.dict_vocab_ns_file[vocab_name]
if prop_ns == vocab_ns:
if vocab_name not in property_vocab_map:
property_vocab_map[vocab_name] = []
property_vocab_map[vocab_name].append(prop_)
self.counter_vocab_map = {}
for vocab in self.vocab_names:
vocab_name = self.config[vocab]['vocab_name']
# Instantiate shapes
shacl_shapes = self.shape_builder.vocabulary_shapes(self, vocab, property_vocab_map, class_vocab_map)
# Create shape graph
shape_graph = create_shape_graph(shacl_shapes)
# Store shapes
folder_path = f'{DATASETS_FOLDER_PATH}/{self.dataset_name}/shapes'
os.makedirs(folder_path, exist_ok=True)
file_path = f'{folder_path}/vocabulary_shapes_{vocab_name}.ttl'
shape_graph.serialize(destination=file_path, format='turtle')
logging.info(f'Shapes for vocabulary {self.dataset_name} saved in {file_path}')
# Validate shapes
file_path = self.config[vocab]["file_path"]
file_format = self.config[vocab]["file_format"]
_, val_graph, _, _, validation_time = validate_shacl_constraints(None, file_path, file_format, shape_graph, vocabs=[vocab], config=self.config)
with open(f'{PROFILE_VOCABULARIES_FOLDER_PATH}/{vocab_name}.json', 'r', encoding='utf-8') as f:
vocab_profile = json.load(f)
# Process validation results
self.process_validation_result_vocabularies(val_graph, vocab_name, vocab_profile, property_vocab_map, class_vocab_map)
return validation_time
def validate_data_shapes(self):
"""
Validates data shapes
"""
validation_time = 0
# Instantiate shapes
accessibility_shapes = self.shape_builder.accessibility_data_shapes()
self.regex_pattern, self.uri_space, contextual_shapes = self.shape_builder.contextual_data_shapes()
representational_shapes, self.shape_property_map_representational = self.shape_builder.representational_data_shapes(self.graph_profile)
# Update graph_profile because it gets updated inside intrinsic_data_shapes
intrinsic_shapes, self.graph_profile, self.shape_property_map_intrinsic, self.shape_class_map = self.shape_builder.intrinsic_data_shapes(self.graph_profile)
shacl_shapes = (accessibility_shapes + '\n' +
contextual_shapes + '\n' +
representational_shapes + '\n' +
intrinsic_shapes
)
# Create shapes graph
shape_graph = create_shape_graph(shacl_shapes)
# Save shapes graph
folder_path = f'{DATASETS_FOLDER_PATH}/{self.dataset_name}/shapes'
os.makedirs(folder_path, exist_ok=True)
file_path = f'{folder_path}/data_shapes.ttl'
shape_graph.serialize(destination=file_path, format='turtle')
logging.info(f'Data shapes for dataset {self.dataset_name} saved in {file_path}')
_, val_graph, _, self.graph_profile, validation_time = validate_shacl_constraints(self.graph_profile, self.graph_file_path, self.graph_file_format, shape_graph, self.vocab_names, self.config)
# Process validation results
results = self.process_validation_result_data(val_graph)
# Store dq assessment results
folder_path = DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)
os.makedirs(folder_path, exist_ok=True)
file_path = f'{folder_path}/dq_assessment_{self.dataset_name}_data.json'
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4)
return validation_time
def process_validation_result_metadata(self, results_graph):
"""
Process validation results for shapes validated against the metadata
"""
with open(DQ_MEASURES_METADATA_TEMPLATE_FILE_PATH, 'r', encoding='utf-8') as f:
results = json.load(f)
folder_path = DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)
os.makedirs(folder_path, exist_ok=True)
file_path = f'{folder_path}/dq_assessment_{self.dataset_name}_metadata.json'
validation_results = results_graph.subjects(RDF.type, SH.ValidationResult)
if not any(validation_results):
# If no validation results, save template files without updating measures
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4)
return
for result in results_graph.subjects(RDF.type, SH.ValidationResult):
metric, message, _ = get_metric_message(results_graph, result)
# All metadata metrics are binary, so if there's a validation result,
# it means the measure is 0
# Had to separate this shape so I could get the specific violation
if metric == 'AuthenticityOfDatasetAuthor' or metric == 'AuthenticityOfDatasetSource':
metric = 'AuthenticityOfDataset'
if metric == "DatasetMetadata": # contains multiple constraints
result_path = results_graph.value(result, SH.resultPath)
if result_path == DCTERMS.title or result_path == DCTERMS.description or result_path == FOAF.homepage:
metric = 'PresenceMetadata'
if result_path == VOID.exampleResource:
metric = 'ExemplaryResources'
if result_path == VOID.vocabulary:
metric = 'VocabularyExistence'
if result_path == VOID.feature:
metric = 'SerializationFormats'
if result_path == VOID.uriRegexPattern:
metric = 'URIRegexPressence'
if result_path == VOID.uriSpace:
metric = 'URISpacePressence'
if result_path == DCTERMS.license:
metric = 'MachineReadableLicense'
results[metric]["measure"] = 0
constraint_type = results_graph.value(result, SH.sourceConstraintComponent)
if constraint_type != SH.MinCountConstraintComponent and constraint_type != SH.OrConstraintComponent:
# Metadata shapes have constraints to check for the correctness of the values of properties
new_message = 'The property is present but the value is incorrect.'
results[metric]["message"] = new_message
else:
# If the constraint is a MinCount, it means the property is not present
results[metric]["message"] = message
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4)
def process_validation_result_vocabularies(self, results_graph, vocab, vocab_profile, property_vocab_map, class_vocab_map):
with open(DQ_MEASURES_VOCABULARIES_TEMPLATE_FILE_PATH, 'r', encoding='utf-8') as f:
metrics_generic = json.load(f)
if os.path.exists(DQ_MEASURES_VOCABULARIES_SPECIFIC_TEMPLATE_FILE_PATH):
with open(DQ_MEASURES_VOCABULARIES_SPECIFIC_TEMPLATE_FILE_PATH, 'r', encoding='utf-8') as f:
metrics_specific = json.load(f)
results = metrics_generic | metrics_specific
violating_entities_per_shape = defaultdict(set)
folder_path = DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)
file_path = f'{folder_path}dq_assessment_vocabularies_{vocab}.json'
os.makedirs(folder_path, exist_ok=True)
validation_results = results_graph.subjects(RDF.type, SH.ValidationResult)
if not any(validation_results):
# If no validation results, save template files without updating measures
for metric, info in results.items():
info['vocab'] = vocab
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4)
return
for result in results_graph.subjects(RDF.type, SH.ValidationResult):
metric, message, counter = get_metric_message(results_graph, result)
focus_node = results_graph.value(result, SH.focusNode)
if metric.startswith("UndefinedProperty"):
metric = f'{metric}_{counter}'
# check that the property is actually from the vocabulary
# some vocabularies define properties from other vocabularies as
# annotation properties
results[metric]["property"] = focus_node
results[metric]["measure"] = 0
elif metric.startswith("UndefinedClass"):
metric = f'{metric}_{counter}'
# check that the class is actually from the vocabulary
results[metric]["class"] = focus_node
results[metric]["measure"] = 0
elif metric and focus_node:
# consider unique focus nodes for each metric
if metric not in violating_entities_per_shape:
violating_entities_per_shape[metric] = set()
# TODO: check if this is needed
# check that the class/property is actually from the vocabulary
violating_entities_per_shape[metric].add(focus_node)
if results[metric]["message"] == "":
results[metric]["message"] = message
for metric, nodes in violating_entities_per_shape.items():
count = len(nodes)
if metric == 'LabelForClasses':
denominator = vocab_profile.get("num_all_classes", 1) + vocab_profile.get("num_other_classes", 1)
elif metric == 'LabelForProperties':
denominator = vocab_profile.get("num_all_properties", 1)+ vocab_profile.get("num_other_properties", 1)
ratio = 1 - (count / denominator)
results[metric]["measure"] = ratio
results[metric]['violations'] = '; '.join(nodes)
results[metric]['num_violations'] = count
for metric, info in results.items():
info['vocab'] = vocab
folder_path = DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)
file_path = f'{folder_path}dq_assessment_vocabularies_{vocab}.json'
os.makedirs(folder_path, exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(results, f, indent=4)
def process_validation_result_data(self, results_graph):
"""
Process validation results for shapes validated against the data.
"""
violating_entities_per_shape = defaultdict(lambda: {"entities": set()})
with open(DQ_MEASURES_DATA_GENERIC_TEMPLATE_FILE_PATH, 'r', encoding='utf-8') as f:
metrics_generic = json.load(f)
metrics_specific = {}
if os.path.exists(DQ_MEASURES_DATA_SPECIFIC_TEMPLATE_FILE_PATH):
with open(DQ_MEASURES_DATA_SPECIFIC_TEMPLATE_FILE_PATH, 'r', encoding='utf-8') as f:
metrics_specific = json.load(f)
results = metrics_generic | metrics_specific
if self.regex_pattern is None:
# There's no regex pattern provided for the URIs,
# hence, we don't need to calculate the URIRegexComplianceEntities,
results.pop("URIRegexComplianceEntities")
if self.uri_space is None:
# There's no uri space provided for the URIs,
# hence, we don't need to calculate the URISpaceComplianceEntities,
results.pop("URISpaceComplianceEntities")
if not self.labeling_property:
results.pop('DifferentLanguagesLabelsEntities')
if not self.description_property:
results.pop('DifferentLanguagesDescriptionsEntities')
validation_results = results_graph.subjects(RDF.type, SH.ValidationResult)
if not any(validation_results):
return results
for result in results_graph.subjects(RDF.type, SH.ValidationResult):
constraint_type = results_graph.value(result, SH.sourceConstraintComponent)
metric, message, counter = get_metric_message(results_graph, result)
if metric in BINARY_METRICS_DATA:
if counter != -1 and not metric.startswith("Deprecated"):
metric = f'{metric}_{counter}'
if metric.startswith("MisplacedProperties") or metric.startswith("InverseFunctionalProperty"):
results[metric]["property"] = self.shape_property_map_intrinsic[int(counter)]
if metric.startswith("SelfDescriptiveFormatProperties"):
results[metric]["property"] = self.shape_property_map_representational[int(counter)]
if metric.startswith("SchemaCompleteness") or metric.startswith("MisplacedClasses"):
results[metric]["class"] = self.shape_class_map[int(counter)]
# For deprecated classes violations are the entities that use the class
# For inverse functional properties violations are the entities that use the property
# For self-descriptive format properties violations are the values of the property that aren't IRIs
# For misplaced classes violations are the entities that use the class
if (metric.startswith("DeprecatedClasses") or
metric.startswith("InverseFunctionalProperty") or
metric.startswith("SelfDescriptiveFormatProperties")):
focus_node = results_graph.value(result, SH.focusNode)
if "violations" not in results[metric] or results[metric]['violations'] == '':
results[metric]['violations'] = str(focus_node.toPython())
elif results[metric]['violations'] != '':
results[metric]['violations'] += ';' + str(focus_node.toPython())
results[metric]["measure"] = 0
elif metric in COUNT_METRICS:
if metric in NUM_SUBJECTS_PER_PROPERTY:
# if an entity uses the property with the same value more than once ->
# the validation result outputs a single result per entity
# if the entity uses the property with different values and they all raise a validation result
# -> i have to consider unique focus nodes
# I instantiate the "entities" with a set
if counter != -1:
metric = f'{metric}_{counter}'
if metric not in violating_entities_per_shape:
if metric == 'UsageExternalURIEntities':
violating_entities_per_shape[metric] = {
"entities": set(),
"property": self.interlinking_property
}
elif metric == 'DifferentLanguagesLabelsEntities':
violating_entities_per_shape[metric] = {
"entities": set(),
"property": self.labeling_property
}
elif metric == 'DifferentLanguagesDescriptionsEntities':
violating_entities_per_shape[metric] = {
"entities": set(),
"property": self.description_property
}
else:
violating_entities_per_shape[metric] = {
"entities": set(),
"property": self.shape_property_map_intrinsic[int(counter)]
}
elif metric in NUM_ENTITIES_PER_CLASS:
if counter != -1:
metric = f'{metric}_{counter}'
if metric not in violating_entities_per_shape:
violating_entities_per_shape[metric] = {
"entities": set(),
"class": self.shape_class_map[int(counter)]
}
else:
if counter != -1:
metric = f'{metric}_{counter}'
if metric not in violating_entities_per_shape:
if metric.startswith("DeprecatedProp"):
violating_entities_per_shape[metric] = {
"entities": set(),
"property": self.shape_property_map_intrinsic[int(counter)]
}
else:
violating_entities_per_shape[metric] = {
"entities": set()
}
focus_node = results_graph.value(result, SH.focusNode)
if metric and focus_node:
violating_entities_per_shape[metric]['entities'].add(focus_node)
if results[metric]["message"] == "":
results[metric]["message"] = message
if constraint_type != SH.MinCountConstraintComponent:
# shapes have ranges to check for the effective usage of properties
# hence, the property can be present but is not being used properly
message += ' (the values for the property are not correct)'
for metric, info in violating_entities_per_shape.items():
count = len(info["entities"])
denominator = get_denominator(metric, info, self.graph_profile)
ratio = 1 - (count / denominator)
results[metric]["measure"] = ratio
results[metric]['violations'] = '; '.join(info['entities'])
results[metric]['num_violations'] = count
if 'class' in info:
results[metric]['class'] = info['class']
elif 'property' in info:
results[metric]['property'] = info['property']
return results
def create_aggregate_metric(self, metric_name, score, uri, type_, tuple_=False):
# type_ can be 'classes' or 'properties'
self.aggregate_dict_counter[metric_name][f'count_{metric_name}_shapes'] += 1
if str(score) == "1":
self.aggregate_dict_counter[metric_name][f'{metric_name}_ones'] += 1
else:
if tuple_:
self.aggregate_dict_counter[metric_name][f'{metric_name}_{type_}'].append((uri, score))
else:
self.aggregate_dict_counter[metric_name][f'{metric_name}_{type_}'].append(uri)
def create_metric_info(self, metric_name, ratio, violations, num_violations, vocab=None):
metric_dict = {
"misplaced_properties": {
'dimension': 'Consistency',
'metric_id': 'CN2',
'metric': 'No misplaced classes or properties',
'metric_description': 'Verifies that properties aren\'t used as classes',
'score': 0,
'message': 'properties are used as classes',
'metric_type': 'binary',
'metric_calculation': "0 if property is used as a class, 1 otherwise.",
"meta_metric_calculation": "Number of correctly used properties / Number of properties defined in vocabularies",
'shape_name': 'MisplacedPropertiesShape',
"shape_template": "ex:MisplacedPropertiesShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetNode PROPERTY_URI;\\n\\tsh:property [\\n\\tsh:path [sh:inversePath rdf:type];\\n\\tsh:maxCount 0;\\n\\t].",
'violations': '',
"violation_text": "properties used as a classes",
'num_violations': 0,
"vocab": ''
},
"misplaced_classes": {
'dimension': 'Consistency',
'metric_id': 'CN2',
'metric': 'No misplaced classes or properties',
'metric_description': 'Verifies that classes aren\'t used as properties',
'score': 0,
'message': 'classes are used as properties',
'metric_type': 'binary',
'metric_calculation': "0 if property is used as a class, 1 otherwise.",
"meta_metric_calculation": "Number of correctly used classes / Number of classes defined in vocabularies",
'shape_name': 'MisplacedClassesShape',
"shape_template": "ex:MisplacedClassesShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf rdf:type;\\n\\tsh:or (\\n\\t[sh:path rdf:type; sh:hasValue rdfs:Class;]\\n\\t[sh:path rdf:type; sh:hasValue rdf:Property;]\\n\\t[sh:path rdf:type; sh:hasValue owl:NamedIndividual;]\\n\\t[\\n\\tsh:path CLASS_URI;\\n\\tsh:maxCount 0;\\n\\t]\\n\\t).",
'violations': '',
"violation_text": "classes used as a property",
'num_violations': 0,
"vocab": ''
},
"correct_range": {
'dimension': 'Consistency',
'metric_id': 'CN9',
'metric': 'Correct domain and range definition',
'metric_description': 'Verifies that properties are used with the correct range.',
'score': 0,
'message': 'are used with incorrect range',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
'meta_metric_calculation': 'Number of properties used with a correct range / Number of properties with a defined range',
'shape_name': 'CorrectRangeShape',
"shape_template": "ex:CorrectRangeShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:property [\\nsh:path PROPERTY_URI;\\nsh:datatype DATATYPE; % or sh:class CLASS\\n].\\n\\nex:CorrectRangeShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:property [\\nsh:path PROPERTY_URI;\\nsh:nodeKind sh:BlankNodeOrIRI;\\n].\\n\\nex:CorrectRangeShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:property [\\nsh:path PROPERTY_URI;\\nsh:nodeKind sh:Literal;\\n].\\n\\nex:CorrectRangeShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:property [\\nsh:path PROPERTY_URI;\\nsh:or (\\n[ sh:nodeKind sh:BlankNodeOrIri; ]\\n[ sh:nodeKind sh:Literal; ]\\n);\\n].",
'violations': '',
"violation_text": "(property, individual score)",
'num_violations': 0,
"vocab": ''
},
"correct_domain": {
'dimension': 'Consistency',
'metric_id': 'CN9',
'metric': 'Correct domain and range definition.',
'metric_description': 'Verifies that properties are used with the correct domain.',
'score': 0,
'message': 'properties are used with incorrect domains',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
"meta_metric_calculation": "Number of properties used with their correct domain / Number of properties with a defined domain",
'shape_name': 'CorrectDomainShape',
"shape_template": "ex:CorrectDomainShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:class CLASS.\\n\\nex:CorrectDomainShape\\na sh:NodeShape ;\\nsh:targetSubjectsOf PROPERTY_URI;\\nsh:nodeKind sh:BlankNodeOrIRI.",
'violations': '',
"violation_text": "(property, individual score)",
'num_violations': 0,
"vocab": ''
},
"entities_disjoint_classes": {
'dimension': 'Consistency',
'metric_id': 'CN1',
'metric': 'No use of entities as members of disjoint classes',
'metric_description': 'Verifies there are no entities that are members of disjoint classes.',
'score': 0,
'message': 'classes have instances of disjoint classes',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities of the target class)',
"meta_metric_calculation": "Number of classes with no member as instance of a disjoint class / Number of disjoint classes",
'shape_name': 'EntitiesDisjointClassesShape',
"shape_template": "ex:EntitiesDisjointClassesShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetClass CLASS_URI;\\n\\tsh:not [ sh:class DISJOINT_CLASS_URI].",
'violations': '',
"violation_text": "(disjoint class, individual score)",
'num_violations': 0,
"vocab": ''
},
"irreflexive_property": {
'dimension': 'Consistency',
'metric_id': 'CN10',
'metric': 'No inconsistent values',
'metric_description': 'Verifies the correct usage of irreflexive properties.',
'score': 0,
'message': 'properties don\'t conform to their irreflexive characteristic',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of subjects that use the property)',
"meta_metric_calculation": "Number of irreflexive properties correctly used / Number of irreflexive properties",
'shape_name': 'IrreflexivePropertyShape',
"shape_template": "ex:IrreflexivePropertyShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:disjoint PROPERTY_URI.",
'violations': '',
"violation_text": "(irreflexive property, individual score)",
'num_violations': 0,
"vocab": ''
},
"self_descriptive_format_properties": {
"dimension": 'Interpretability',
"metric_id": "ITP1",
"metric": "Use of self-descriptive formats",
'metric_description': 'Verifies if properties use IRIs as values',
'score': 0,
'message': 'properties use at least one literal or blank node',
'metric_type': 'count',
'metric_calculation': '1 if the property uses IRIs as values, 0 otherwise.',
"meta_metric_calculation": "Number of properties that have IRIs as values / Number of properties used in the dataset",
'shape_name': 'SelfDescriptiveFormatPropertiesShape',
"shape_template": "ex:SelfDescriptiveFormatPropertiesShape\\na sh:NodeShape ;\\nsh:targetObjectsOf PROPERTY_URI;\\nsh:nodeKind sh:IRI.",
'violations': '',
"violation_text": "properties used with blank nodes or literals",
'num_violations': 0,
"vocab": ''
},
"misuse_object_properties": {
'dimension': 'Consistency',
'metric_id': 'CN3',
'metric': 'No misuse of owl:DatatypeProperty or owl:ObjectProperty',
'metric_description': 'Verifies that owl:ObjectProperty aren\'t used with Literals',
'score': 0,
'message': 'object properties are used with literals or blank nodes',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
"meta_metric_calculation": "Number of owl:ObjectProperty correctly used / Number of owl:ObjectProperty used in the dataset",
'shape_name': 'MisuseOwlObjectPropertiesShape',
"shape_template": "ex:MisuseObjectPropertiesShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:property [\\n\\t\\tsh:path PROPERTY_URI;\\n\\t\\tsh:nodeKind sh:BlankNodeOrIRI;\\n\\t].",
'violations': '',
"violation_text": "(object property, individual score)",
'num_violations': 0,
"vocab": ''
},
"misuse_datatype_properties": {
'dimension': 'Consistency',
'metric_id': 'CN3',
'metric': 'No misuse of owl:DatatypeProperty or owl:ObjectProperty',
'metric_description': 'Verifies that owl:DatatypeProperty are used with Literals',
'score': 0,
'message': 'datatype properties are used with IRIs',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
"meta_metric_calculation": "Number of owl:DatatypeProperty correctly used / Number of owl:DatatypeProperty used in the",
'shape_name': 'MisuseOwlDatatypePropertiesShape',
"shape_template": "ex:MisuseDatatypePropertiesShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:property [\\n\\tsh:path PROPERTY_URI;\\n\\tsh:nodeKind sh:Literal;\\n\\t].",
'violations': ' ',
"violation_text": "(datatype property, individual score)",
'num_violations': 0,
"vocab": ''
},
"functional_property": {
'dimension': 'Consistency',
'metric_id': 'CN10',
'metric': 'No inconsistent values',
'metric_description': 'Verifies the correct usage of functional properties.',
'score': 0,
'message': 'properties don\'t conform to their functional characteristic',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
"meta_metric_calculation": "Number of functional properties correctly used / Number of functional properties",
'shape_name': 'FunctionalPropertyShape',
"shape_template": "ex:FunctionalPropertyShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:property [\\n\\t\\tsh:path PROPERTY_URI;\\n\\t\\tsh:maxCount 1;\\n\\t].",
'violations': '',
"violation_text": "(functional property, individual score)",
'num_violations': 0,
"vocab": ''
},
"asymmetric_property": {
'dimension': 'Consistency',
'metric_id': 'CN10',
'metric': 'No inconsistent values',
'metric_description': 'Verifies the correct usage of asymmetric properties.',
'score': 0,
'message': 'properties don\'t conform to their asymmetric characteristic',
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities that use the property)',
"meta_metric_calculation": "Number of asymmetric properties correctly used / Number of asymmetric properties",
'shape_name': 'AsymmetricPropertyShape',
"shape_template": "ex:AsymmetricPropertyShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:property [\\n\\t\\tsh:path [sh:inversePath PROPERTY_URI];\\n\\t\\tsh:disjoint PROPERTY_URI;\\n\\t].",
'violations': '',
"violation_text": "(asymmetric property, individual score)",
'num_violations': 0,
"vocab": ''
},
"schema_completeness_class_usage": {
'dimension': 'Completeness',
'metric_id': 'CP1',
'metric': 'Schema completeness',
'metric_description': 'Verifies that classes defined in vocabularies are used in the dataset.',
'score': 0,
'message': 'classes aren\'t used in the dataset',
'metric_type': 'binary',
"metric_calculation": "1 if the class is used, 0 otherwise",
"meta_metric_calculation": "Number of classes used from the vocabularies / Number of classes defined in vocabularies",
'shape_name': 'SchemaCompletenessClassUsageShape',
"shape_template": "ex:NotNamedIndividualShape\\n\\ta sh:NodeShape;\\n\\tsh:property [\\n\\t\\tsh:path rdf:type ;\\n\\t\\tsh:not [ sh:hasValue owl:NamedIndividual ] ;\\n\\t].\\nex:SchemaCompletenessClassUsageShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetNode CLASS_URI ;\\n\\tsh:property [\\n\\t\\tsh:path [ sh:inversePath rdf:type ] ;\\n\\t\\tsh:minCount 1 ;\\n\\t\\tsh:qualifiedValueShape [\\n\\t\\t\\tsh:node ex:NotNamedIndividualShape ;\\n\\t\\t];\\n\\t\\tsh:qualifiedMinCount 1 ;\\n\\t].",
'violations': '',
"violation_text": "classes defined in the vocabulary that are not used in the dataset",
'num_violations': 0,
"vocab": ''
},
"malformed_literal": {
"dimension": "Syntactic Validity",
"metric_id": "SV3",
"metric": "No malformed datatype literals",
"score": 0,
"message": "properties are used with malformed datatype values",
"metric_description": "Verifies that datatype property's values follow the expected lexical syntax of the datatype or that it isn't an ill-typed literal.",
"metric_type": "count",
"metric_calculation": "1 - (Number of violations / Number of entities that use the property)",
"meta_metric_calculation": "Number of correctly used properties / Number of properties with a datatype range",
'shape_name': 'MalformedDatatypeShape',
"shape_template": "ex:MalformedLiteralShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf PROPERTY_URI;\\n\\tsh:property [\\n\\tsh:path PROPERTY_URI ;\\n\\tsh:datatype DATATYPE_URI;\\n\\t].",
"violations": "",
"violation_text": "(property used with a malformed literal, individual score)",
"num_violations": 0,
"vocab": ''
},
"deprecated_property": {
"dimension": "Consistency",
"metric_id": "CN4",
"metric": "Members of owl:DeprecatedClass or owl:DeprecatedProperty not used",
"score": 0,
"message": 'deprecated properties are used in the dataset',
"metric_description": "Verifies that deprecated properties aren't used",
'metric_type': 'count',
'metric_calculation': '1 - (Number of violations / Number of entities)',
'meta_metric_calculation': 'Number of unused deprecated properties / Number of deprecated properties',
'shape_name': 'DeprecatedPropertiesShape',
"shape_template": "ex:DeprecatedPropertiesUsageShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetSubjectsOf rdf:type;\\n\\tsh:or (\\n\\t\\t[sh:path rdf:type; sh:hasValue rdfs:Class;]\\n\\t\\t[sh:path rdf:type; sh:hasValue rdf:Property;]\\n\\t\\t[sh:path rdf:type; sh:hasValue owl:\\n\\t\\tNamedIndividual;]\\n\\t\\t[ sh:path PROPERTY_URI; sh:maxCount 0; ]\\n\\t).",
'violations': '',
"violation_text": "(deprecated property, individual score)",
'num_violations': 0,
'vocab': ''
},
"undefined_classes": {
"dimension": "Interpretability",
"metric_id": "ITP3",
"metric": "Invalid usage of undefined classes and properties",
"score": 0,
"message": "",
"metric_description": "Verifies that the classes used in the dataset are defined in the vocabulary.",
"metric_type": "binary",
"metric_calculation": "1 if the class is defined, 0 otherwise",
"meta_metric_calculation": "Number of defined classes (from the vocabulary) used / Number of classes (from the vocabulary) used in the dataset",
'shape_name': 'UndefinedClassShape',
"shape_template": "ex:UndefinedClassShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetNode CLASS_URI;\\n\\tsh:property [\\n\\t\\tsh:path rdf:type;\\n\\t\\tsh:hasValue rdfs:Class;\\n\\t\\tsh:minCount 1;\\n\\t].",
"violations": "",
"violation_text": "undefined classes",
"num_violations": 0,
"vocab": ''
},
"undefined_properties": {
"dimension": "Interpretability",
"metric_id": "ITP3",
"metric": "Invalid usage of undefined classes and properties",
"score": 0,
"message": "",
"metric_description": "Verifies that the properties used in the dataset are defined in the vocabulary.",
"metric_type": "binary",
"metric_calculation": "1 if the property is defined, 0 otherwise",
"meta_metric_calculation": "Number of defined properties (from the vocabulary) used / Number of properties (from the vocabulary) used in the dataset",
'shape_name': 'UndefinedPropertyShape',
"shape_template": "ex:UndefinedPropertyShape\\n\\ta sh:NodeShape ;\\n\\tsh:targetNode PROPERTY_URI;\\n\\tsh:property [\\n\\t\\tsh:path rdf:type;\\n\\t\\tsh:hasValue rdf:Property;\\n\\t\\tsh:minCount 1;\\n\\t].",
"violations": "",
"violation_text": "undefined properties",
"num_violations": 0,
"vocab": ''
}
}
metric_dict[metric_name]["score"] = ratio
if ratio < 1 and not vocab:
param_shapes = f'count_{metric_name}_shapes'
param_ones = f'{metric_name}_ones'
metric_dict[metric_name]["message"] = f'{self.aggregate_dict_counter[metric_name][param_shapes] - self.aggregate_dict_counter[metric_name][param_ones]} ' + metric_dict[metric_name]["message"]
else:
metric_dict[metric_name]["message"] = ''
metric_dict[metric_name]["violations"] = violations
metric_dict[metric_name]["num_violations"] = num_violations
if vocab:
metric_dict[metric_name]["vocab"] = vocab
return metric_dict[metric_name]
def create_dq_results_csv(self):
"""
Create a CSV with columns: 'dimension', 'metric', 'metric_id', 'metric_description', 'score', 'message', 'metric_type', 'metric_calculation', 'meta_metric_calculation', 'shape_template', 'violations'
from DQ assessment JSON files.
"""
results_folder = DQ_ASSESSMENT_RESULTS_FOLDER_PATH.format(dataset_name=self.dataset_name)
files = []
if self.validate_metadata_shapes:
files.append(f'dq_assessment_{self.dataset_name}_metadata.json')
if self.validate_data_shapes:
files.append(f'dq_assessment_{self.dataset_name}_data.json')
if self.validate_vocabulary_shapes:
vocab_files = [file.split("/")[-1] for file in glob.glob(f'{results_folder}dq_assessment_vocabularies_*.json')]
files.extend(vocab_files)
rows = []
counter_shapes = 0
for filename in files:
file_path = os.path.join(results_folder, filename)
if not os.path.exists(file_path):
continue
with open(file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
self.aggregate_dict_counter = {
"misplaced_properties": {
"misplaced_properties_ones": 0,
"count_misplaced_properties_shapes": 0,
"misplaced_properties_properties": []
},
"misplaced_classes": {
"misplaced_classes_ones": 0,
"count_misplaced_classes_shapes": 0,
"misplaced_classes_classes": []
},
"correct_range": {
"correct_range_ones": 0,
"count_correct_range_shapes": 0,
"correct_range_properties": []
},
"correct_domain": {
"correct_domain_ones": 0,
"count_correct_domain_shapes": 0,
"correct_domain_properties": []
},
"entities_disjoint_classes": {
"entities_disjoint_classes_ones": 0,
"count_entities_disjoint_classes_shapes": 0,
"entities_disjoint_classes_classes": []
},
"misuse_datatype_properties": {
"misuse_datatype_properties_ones": 0,
"count_misuse_datatype_properties_shapes": 0,
"misuse_datatype_properties_properties": []
},
"misuse_object_properties": {
"misuse_object_properties_ones": 0,
"count_misuse_object_properties_shapes": 0,
"misuse_object_properties_properties": []
},
"irreflexive_property": {
"irreflexive_property_ones": 0,
"count_irreflexive_property_shapes": 0,
"irreflexive_property_properties": []
},
"self_descriptive_format_properties": {
"self_descriptive_format_properties_ones": 0,
"count_self_descriptive_format_properties_shapes": 0,
"self_descriptive_format_properties_properties": []
},
"functional_property": {
"functional_property_ones": 0,
"count_functional_property_shapes": 0,
"functional_property_properties": []
},
"asymmetric_property": {
"asymmetric_property_ones": 0,
"count_asymmetric_property_shapes": 0,
"asymmetric_property_properties": []
},
"schema_completeness_class_usage": {
"schema_completeness_class_usage_ones": 0,
"count_schema_completeness_class_usage_shapes": 0,
"schema_completeness_class_usage_classes": []
},
"malformed_literal": {
"malformed_literal_ones": 0,
"count_malformed_literal_shapes": 0,
"malformed_literal_properties": []
},
"undefined_classes": {
"undefined_classes_ones": {},
"count_undefined_classes_shapes": {},
"undefined_classes_classes": {}
},
"undefined_properties": {
"undefined_properties_ones": {},
"count_undefined_properties_shapes": {},
"undefined_properties_properties": {}
},
"deprecated_property": {
"deprecated_property_ones": 0,
"count_deprecated_property_shapes": 0,
"deprecated_property_properties": []
}
}
for shape_name, info in data.items():
counter_shapes += 1