-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbox_generator.py
255 lines (194 loc) · 9.02 KB
/
cbox_generator.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
import sys
from rdflib.collection import Collection
from common import *
from implementations.knime import implementations, components
def init_cbox() -> Graph:
cbox = get_graph_xp()
cbox.add((URIRef(str(cb)), RDF.type, OWL.Ontology))
cbox.add((URIRef(str(cb)), RDFS.label, Literal("ExtremeXP Ontology CBox")))
return cbox
def add_problems(cbox):
problems = [
cb.Description,
cb.Explanation,
cb.Prediction,
cb.DataCleaning,
cb.DataManagement,
cb.Classification,
cb.Clustering,
cb.AnomalyDetection,
cb.MissingValueManagement,
cb.DuplicationRemoval,
cb.Normalization,
]
subproblems = [
(cb.Description, [cb.Classification, cb.Clustering, cb.AnomalyDetection],),
(cb.DataCleaning, [cb.MissingValueManagement, cb.DuplicationRemoval, cb.Normalization],),
]
for p in problems:
cbox.add((p, RDF.type, tb.Problem))
for p, sps in subproblems:
for sp in sps:
cbox.add((sp, tb.subProblemOf, p))
def add_algorithms(cbox):
algorithms = [
# Clustering
(cb.KMeans, cb.Clustering),
(cb.DBSCAN, cb.Clustering),
(cb.HierarchicalClustering, cb.Clustering),
# Classification
(cb.DecisionTree, cb.Classification),
(cb.RandomForest, cb.Classification),
(cb.NaiveBayes, cb.Classification),
(cb.SVM, cb.Classification),
(cb.KNN, cb.Classification),
# Anomaly Detection
(cb.OneClassSVM, cb.AnomalyDetection),
(cb.IsolationForest, cb.AnomalyDetection),
(cb.LocalOutlierFactor, cb.AnomalyDetection),
# Missing Value Management
(cb.MeanImputation, cb.MissingValueManagement),
(cb.MedianImputation, cb.MissingValueManagement),
(cb.ModeImputation, cb.MissingValueManagement),
(cb.KNNImputation, cb.MissingValueManagement),
(cb.MissingValueRemoval, cb.MissingValueManagement),
# Duplication Removal
(cb.DuplicateRemoval, cb.DuplicationRemoval),
# Normalization
(cb.MinMaxScaling, cb.Normalization),
(cb.ZScoreScaling, cb.Normalization),
(cb.RobustNormalization, cb.Normalization),
# Data Management
(cb.TrainTestSplit, cb.DataManagement),
(cb.LabelExtraction, cb.DataManagement),
]
for algorithm, problem in algorithms:
cbox.add((algorithm, RDF.type, tb.Algorithm))
cbox.add((algorithm, tb.solves, problem))
def add_implementations(cbox):
for implementation in implementations:
print(f'Adding implementation {implementation.name}')
implementation.add_to_graph(cbox)
for implementation in implementations:
implementation.add_counterpart_relationship(cbox)
for component in components:
print(f'Adding component {component.name}')
component.add_to_graph(cbox)
for component in components:
component.add_counterpart_relationship(cbox)
def add_models(cbox):
models = [
'SVMModel',
'DecisionTreeModel',
'NormalizerModel',
'MissingValueModel',
]
cbox.add((cb.Model, RDFS.subClassOf, tb.Data))
for model in models:
cbox.add((cb.term(model), RDFS.subClassOf, cb.Model))
cbox.add((cb.term(model + 'Shape'), RDF.type, SH.NodeShape))
cbox.add((cb.term(model + 'Shape'), RDF.type, tb.DataTag))
cbox.add((cb.term(model + 'Shape'), SH.targetClass, cb.term(model)))
def add_shapes(cbox):
# NonNullNumericFeatureColumnShape
column_shape = cb.NonNullNumericFeatureColumnShape
# column_shape = BNode()
cbox.add((column_shape, RDF.type, SH.NodeShape))
numeric_column_property = cb.NumericColumnProperty
# numeric_column_property = BNode()
cbox.add((numeric_column_property, SH.path, dmop.hasDataPrimitiveTypeColumn))
cbox.add((numeric_column_property, SH['in'],
Collection(cbox, BNode(), seq=[dmop.Integer, dmop.Float]).uri))
non_null_column_property = cb.NonNullColumnProperty
# non_null_column_property = BNode()
cbox.add((non_null_column_property, SH.path, dmop.containsNulls))
cbox.add((non_null_column_property, SH.datatype, XSD.boolean))
cbox.add((non_null_column_property, SH.hasValue, Literal(False)))
feature_column_property = cb.FeatureColumnProperty
# feature_column_property = BNode()
cbox.add((feature_column_property, SH.path, dmop.isFeature))
cbox.add((feature_column_property, SH.datatype, XSD.boolean))
cbox.add((feature_column_property, SH.hasValue, Literal(True)))
feature_column = cb.FeatureColumnShape
# feature_column = BNode()
cbox.add((feature_column, RDF.type, SH.NodeShape))
cbox.add((feature_column, SH.targetClass, dmop.Column))
cbox.add((feature_column, SH.property, feature_column_property))
cbox.add((column_shape, SH.property, numeric_column_property))
cbox.add((column_shape, SH.property, non_null_column_property))
cbox.add((column_shape, SH.targetClass, feature_column))
# NonNullNumericFeatureTabularDatasetShape
non_null_numeric_tabular_dataset_shape = cb.NonNullNumericFeatureTabularDatasetShape
cbox.add((non_null_numeric_tabular_dataset_shape, RDF.type, SH.NodeShape))
cbox.add((non_null_numeric_tabular_dataset_shape, SH.targetClass, dmop.TabularDataset))
bnode = BNode()
cbox.add((bnode, SH.path, dmop.hasColumn))
cbox.add((bnode, SH.node, column_shape))
cbox.add((non_null_numeric_tabular_dataset_shape, SH.property, bnode))
# LabeledTabularDatasetShape
label_column_property = cb.LabelColumnProperty
cbox.add((label_column_property, SH.path, dmop.isLabel))
cbox.add((label_column_property, SH.datatype, XSD.boolean))
cbox.add((label_column_property, SH.hasValue, Literal(True)))
label_column_shape = cb.LabelColumnShape
cbox.add((label_column_shape, RDF.type, SH.NodeShape))
cbox.add((label_column_shape, SH.targetClass, dmop.Column))
cbox.add((label_column_shape, SH.property, label_column_property))
labeled_dataset_shape = cb.LabeledTabularDatasetShape
cbox.add((labeled_dataset_shape, RDF.type, SH.NodeShape))
cbox.add((labeled_dataset_shape, SH.targetClass, dmop.TabularDataset))
bnode_qualified = BNode()
cbox.add((bnode_qualified, SH.path, dmop.isLabel))
cbox.add((bnode_qualified, SH.hasValue, Literal(True)))
bnode_column = BNode()
cbox.add((bnode_column, SH.path, dmop.hasColumn))
cbox.add((bnode_column, SH.qualifiedValueShape, bnode_qualified))
cbox.add((bnode_column, SH.qualifiedMinCount, Literal(1)))
cbox.add((bnode_column, SH.minCount, Literal(1)))
cbox.add((labeled_dataset_shape, SH.property, bnode_column))
non_null_column_shape = cb.NonNullColumnShape
cbox.add((non_null_column_shape, RDF.type, SH.NodeShape))
cbox.add((non_null_column_shape, SH.targetClass, dmop.Column))
cbox.add((non_null_column_shape, SH.property, non_null_column_property))
bnode = BNode()
cbox.add((bnode, SH.path, dmop.hasColumn))
cbox.add((bnode, SH.node, non_null_column_shape))
non_null_tabular_dataset_shape = cb.NonNullTabularDatasetShape
cbox.add((non_null_tabular_dataset_shape, RDF.type, SH.NodeShape))
cbox.add((non_null_tabular_dataset_shape, SH.targetClass, dmop.TabularDataset))
cbox.add((non_null_tabular_dataset_shape, SH.property, bnode))
cbox.add((cb.TabularDataset, RDF.type, SH.NodeShape))
cbox.add((cb.TabularDataset, RDF.type, tb.DataTag))
cbox.add((cb.TabularDataset, SH.targetClass, dmop.TabularDataset))
numeric_column_shape = cb.NumericColumnShape
cbox.add((numeric_column_shape, RDF.type, SH.NodeShape))
cbox.add((numeric_column_shape, SH.targetClass, dmop.Column))
cbox.add((numeric_column_shape, SH.property, numeric_column_property))
bnode = BNode()
cbox.add((bnode, SH.path, dmop.hasColumn))
cbox.add((bnode, SH.node, numeric_column_shape))
numeric_tabular_dataset_shape = cb.NumericTabularDatasetShape
cbox.add((numeric_tabular_dataset_shape, RDF.type, SH.NodeShape))
cbox.add((numeric_tabular_dataset_shape, SH.targetClass, dmop.TabularDataset))
cbox.add((numeric_tabular_dataset_shape, SH.property, bnode))
cbox.add((cb.isNormalizedConstraint, RDF.type, SH.PropertyConstraintComponent))
cbox.add((cb.isNormalizedConstraint, SH.path, dmop.isNormalized))
cbox.add((cb.isNormalizedConstraint, SH.datatype, XSD.boolean))
cbox.add((cb.isNormalizedConstraint, SH.hasValue, Literal(True)))
cbox.add((cb.NormalizedTabularDatasetShape, RDF.type, SH.NodeShape))
cbox.add((cb.NormalizedTabularDatasetShape, RDF.type, tb.DataTag))
cbox.add((cb.NormalizedTabularDatasetShape, SH.property, cb.isNormalizedConstraint))
cbox.add((cb.NormalizedTabularDatasetShape, SH.targetClass, dmop.TabularDataset))
def main(dest='../ontologies/cbox.ttl'):
cbox = init_cbox()
add_problems(cbox)
add_algorithms(cbox)
add_implementations(cbox)
add_models(cbox)
add_shapes(cbox)
cbox.serialize(dest, format='turtle')
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
main()