-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopf_wrapper.py
593 lines (415 loc) · 15.9 KB
/
opf_wrapper.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
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
import os
from ctypes import CDLL, Structure, POINTER, c_float, c_int, c_char, c_char_p, byref
class LibOPF:
"""A class to hold the LibOPF's integration.
"""
def __init__(self):
"""Initialization method.
"""
# Creates the OPF property
self._OPF = CDLL(os.environ['OPF_DIR']+'/OPF.so')
def wrap_function(lib, funcname, restype, argtypes):
"""Wraps a function using ctypes.
Args:
lib (func): The function pointer to be loaded.
funcname (string): The name of the function.
restype (types): Function's return type.
argtypes (types): Types of the arguments.
"""
# Gets the function object
func = lib.__getattr__(funcname)
# Gets the type of the response
func.restype = restype
# Gets the arguments' types
func.argtypes = argtypes
return func
class RealHeap(Structure, LibOPF):
"""A class to hold the RealHeap's structure.
"""
# Fields that belongs to the structure
_fields_ = [
("cost", POINTER(c_float)),
("color", POINTER(c_char)),
("pixel", POINTER(c_int)),
("pos", POINTER(c_int)),
("last", c_int),
("n", c_int),
("removal_policy", c_char)
]
def __init__(self):
"""Initialization method.
"""
# Override its parent class
super().__init__()
class Set(Structure):
"""A class to hold the Set's structure.
"""
pass
# Fields that belongs to the structure
Set._fields_ = [
("elems", c_int),
("next", POINTER(Set))
]
class SNode(Structure):
"""A class to hold the subgraph's Node structure.
"""
# Fields that belongs to the structure
_fields_ = [
("pathvalue", c_float),
("dens", c_float),
("radius", c_float),
("label", c_int),
("root", c_int),
("pred", c_int),
("truelabel", c_int),
("position", c_int),
("feat", POINTER(c_float)),
("status", c_char),
("relevant", c_char),
("nplatadj", c_int),
("adj", POINTER(Set))
]
class Subgraph(Structure, LibOPF):
"""A class to hold the Subgraph structure.
"""
# Fields that belongs to the structure
_fields_ = [
("node", POINTER(SNode)),
("nnodes", c_int),
("nfeats", c_int),
("bestk", c_int),
("nlabels", c_int),
("df", c_float),
("mindens", c_float),
("maxdens", c_float),
("k", c_float),
("ordered_list_of_nodes", POINTER(c_int))
]
def __init__(self):
"""Initialization method.
"""
# Override its parent class
super().__init__()
class OPF(LibOPF):
"""Wraps methods from the LibOPF.
"""
def __init__(self):
"""Initialization method.
"""
# Overrides its parent class
super().__init__()
def _createrealheap(self, n, cost):
# Creates the pointer to the function
createrealheap = self._OPF.CreateRealHeap
# Gets the type of the response
createrealheap.restype = POINTER(RealHeap)
# Gets the arguments types
createrealheap.argtypes = [c_int, POINTER(c_float)]
# Actually uses the function
q = createrealheap(n, cost)
return q
def _insertrealheap(self, heap, pixel):
# Creates the pointer to the function
insertrealheap = self._OPF.InsertRealHeap
# Gets the type of the response
insertrealheap.restype = c_char
# Gets the arguments types
insertrealheap.argtypes = [POINTER(RealHeap), c_int]
# Actually uses the function
q = insertrealheap(heap, pixel)
return q
def _isemptyrealheap(self, heap):
# Creates the pointer to the function
isemptyrealheap = self._OPF.IsEmptyRealHeap
# Gets the type of the response
isemptyrealheap.restype = c_char
# Gets the arguments types
isemptyrealheap.argtypes = [POINTER(RealHeap)]
# Actually uses the function
q = isemptyrealheap(heap)
return q
def _removerealheap(self, heap, pixel):
# Creates the pointer to the function
removerealheap = self._OPF.RemoveRealHeap
# Gets the type of the response
removerealheap.restype = c_char
# Gets the arguments types
removerealheap.argtypes = [POINTER(RealHeap), POINTER(c_int)]
# Actually uses the function
q = removerealheap(heap, pixel)
return q
def _updaterealheap(self, heap, p, value):
# Creates the pointer to the function
updaterealheap = self._OPF.UpdateRealHeap
# Gets the arguments types
updaterealheap.argtypes = [POINTER(RealHeap), c_int, c_float]
# Actually uses the function
updaterealheap(heap, p, value)
def _destroyrealheap(self, heap):
"""Destroys a heap.
Args:
heap (RealHeap): Heap object to be destroyed.
"""
# Creates the pointer to the function
destroyrealheap = self._OPF.DestroyRealHeap
# Gets the arguments types
destroyrealheap.argtypes = [POINTER(POINTER(RealHeap))]
# Actually uses the function
destroyrealheap(heap)
def _createsubgraph(self, nnodes):
"""Allocate nodes without features.
Args:
nnodes (int): number of nodes.
"""
# Creates the pointer to the function
createsubgraph = self._OPF.CreateSubgraph
# Gets the type of the response
createsubgraph.restype = POINTER(Subgraph)
# Gets the arguments types
createsubgraph.argtypes = [c_int]
# Actually uses the function
g = createsubgraph(nnodes)
return g
def _readsubgraph(self, dataset):
"""Reads a subgraph from a .opf file.
Args:
dataset (string): Path to .opf dataset file.
"""
# Creates the pointer to the function
readsubgraph = self._OPF.ReadSubgraph
# Gets the type of the response
readsubgraph.restype = POINTER(Subgraph)
# Gets the arguments types
readsubgraph.argtypes = [c_char_p]
# Actually uses the function
g = readsubgraph(dataset)
return g
def _writesubgraph(self, subgraph, file_name):
"""Writes a subgraph to a .opf file.
Args:
subgraph (Subgraph): Subgraph object to be written.
file_name (string): Path to the file that will be saved.
"""
# Creates the pointer to the function
writesubgraph = self._OPF.WriteSubgraph
# Gets the arguments types
writesubgraph.argtypes = [POINTER(Subgraph), c_char_p]
# Actually uses the function
writesubgraph(subgraph, file_name)
def _destroysubgraph(self, subgraph):
"""Destroys a subgraph.
Args:
subgraph (Subgraph): Subgraph object to be destroyed.
"""
# Creates the pointer to the function
destroysubgraph = self._OPF.DestroySubgraph
# Gets the arguments types
destroysubgraph.argtypes = [POINTER(POINTER(Subgraph))]
# Actually uses the function
destroysubgraph(subgraph)
def _splitsubgraph(self, sg, sg1, sg2, perc):
# Creates the pointer to the function
splitsubgraph = self._OPF.opf_SplitSubgraph
# Gets the arguments types
splitsubgraph.argtypes = [POINTER(Subgraph), POINTER(POINTER(Subgraph)), POINTER(POINTER(Subgraph)), c_float]
# Actually uses the function
splitsubgraph(sg, sg1, sg2, perc)
def _copysubgraph(self, subgraph):
# Creates the pointer to the function
copysubgraph = self._OPF.opf_SplitSubgraph
# Gets the type of the response
copysubgraph.restype = POINTER(Subgraph)
# Gets the arguments types
copysubgraph.argtypes = [POINTER(Subgraph)]
# Actually uses the function
copysubgraph(subgraph)
def _writemodelfile(self, subgraph, file_name):
"""Writes a subgraph to a model file.
Args:
subgraph (Subgraph): Subgraph object to be written.
file_name (string): Path to the file that will be saved.
"""
# Creates the pointer to the function
writemodelfile = self._OPF.opf_WriteModelFile
# Gets the arguments types
writemodelfile.argtypes = [POINTER(Subgraph), c_char_p]
# Actually uses the function
writemodelfile(subgraph, file_name)
def _readmodelfile(self, file_name):
"""Reads a model file to a subgraph.
Args:
file_name (string): Path to the model file that will be read.
"""
# Creates the pointer to the function
readmodelfile = self._OPF.opf_ReadModelFile
# Gets the type of the response
readmodelfile.restype = POINTER(Subgraph)
# Gets the arguments types
readmodelfile.argtypes = [c_char_p]
# Actually uses the function
g = readmodelfile(file_name)
return g
def _modelfile2txt(self):
"""Converts the classifier.opf from binary to text.
"""
print('Converting classifier.opf from binary to text ...')
# Creates the pointer to the function
modelfile2txt = self._OPF.opf_ModelFile2Txt
# Actually uses the function
modelfile2txt()
def _writeoutputfile(self, subgraph, file_name):
"""Writes an output file.
Args:
subgraph (Subgraph): Subgraph object to be written.
file_name (string): Path to the file that will be saved.
"""
# Creates the pointer to the function
writeoutputfile = self._OPF.opf_WriteOutputFile
# Gets the argument types
writeoutputfile.argtypes = [POINTER(Subgraph), c_char_p]
# Actually uses the function
writeoutputfile(subgraph, file_name)
def _readoutputfile(self, file_name, subgraph):
"""Reads an output file.
Args:
subgraph (Subgraph): Subgraph object to be read.
file_name (string): Path to the file that will be read.
"""
# Creates the pointer to the function
readoutputfile = self._OPF.opf_ReadOutputFile
# Gets the argument types
readoutputfile.argtypes = [c_char_p, POINTER(Subgraph)]
# Actually uses the function
readoutputfile(file_name, subgraph)
def _training(self, train):
"""Trains a model using supervised OPF.
Args:
train (Subgraph): Training subgraph.
"""
# Creates the pointer to the function
training = self._OPF.opf_OPFTraining
# Gets the argument types
training.argtypes = [POINTER(Subgraph)]
# Actually uses the function
training(train)
def _classifying(self, train, test):
"""Classifies a model.
Args:
train (Subgraph): Training subgraph.
test (Subgraph): Test subgraph.
"""
# Creates the pointer to the function
classifying = self._OPF.opf_OPFClassifying
# Gets the argument types
classifying.argtypes = [POINTER(Subgraph), POINTER(Subgraph)]
# Actually uses the function
classifying(train, test)
def _accuracy(self, subgraph):
"""Computes the model's accuracy.
Args:
subgraph (Subgraph): Subgraph to compute its accuracy.
"""
# Creates the pointer to the function
accuracy = self._OPF.opf_Accuracy
# Gets the type of the response
accuracy.restype = c_float
# Gets the argument types
accuracy.argtypes = [POINTER(Subgraph)]
# Actually uses the function
result = accuracy(subgraph)
return result
def _alloc_float_array(self, n):
alloc_float = self._OPF.AllocFloatArray
alloc_float.restype = POINTER(c_float)
alloc_float.argtype = c_int
result = alloc_float(n)
return result
def _eucldistlog(self, f1, f2, n):
"""Compute discretized Euclidean distance between feature vectors.
"""
# Creates the pointer to the function
eucldistlog = self._OPF.opf_EuclDistLog
# Gets the type of the response
eucldistlog.restype = c_float
# Gets the argument types
eucldistlog.argtypes = [POINTER(c_float), POINTER(c_float), c_int]
# Actually uses the function
result = eucldistlog(f1, f2, n)
return result
# --------------------------------------------------------------
def train(opf, train):
"""Performs the supervised OPF traning.
Args:
opf (OPF): OPF class instance.
train (subgraph): Training subgraph.
"""
# Performs the supervised OPF training
opf._training(train)
# Writes the model file
opf._writemodelfile(train, 'classifier.opf'.encode('utf-8'))
# Writes the output file
opf._writeoutputfile(train, 'training.dat.out'.encode('utf-8'))
# Destroys the subgraph
opf._destroysubgraph(train)
def classify(opf, test):
"""Performs the supervised OPF classification.
Args:
opf (OPF): OPF class instance.
test (subgraph): Testing subgraph.
"""
# Reads the model file
train = opf._readmodelfile('classifier.opf'.encode('utf-8'))
# Performs the supervised OPF classification
opf._classifying(train, test)
# Writes the output file
opf._writeoutputfile(test, 'testing.dat.out'.encode('utf-8'))
# Destroys the subgraph
opf._destroysubgraph(test)
def acc(opf, test):
"""Performs the OPF accuracy computation.
Args:
opf (OPF): OPF class instance.
test (subgraph): Testing subgraph.
"""
# Reads the output file
opf._readoutputfile('testing.dat.out'.encode('utf-8'), test)
# Performs the accuracy computation
acc = opf._accuracy(test)
print('Accuracy: %.2f' % (acc*100))
# Destroys the subgraph
opf._destroysubgraph(test)
return acc
def subgraph_from_selected_features(opf, sg, features):
newsg = opf._createsubgraph(sg.contents.nnodes)
newsg.contents.nlabels = sg.contents.nlabels
newsg.contents.nfeats = 0;
for i in range(sg.contents.nfeats):
if (features[i]):
newsg.contents.nfeats += 1
for i in range(newsg.contents.nnodes):
newsg.contents.node[i].feat = opf._alloc_float_array(newsg.contents.nfeats)
newsg.contents.node[i].truelabel = sg.contents.node[i].truelabel
newsg.contents.node[i].position = sg.contents.node[i].position
k = 0
for j in range(sg.contents.nfeats):
if (features[j]):
newsg.contents.node[i].feat[k] = sg.contents.node[i].feat[j]
k += 1
return newsg
def split_subgraph(opf, g, training_perc, evaluating_perc, testing_perc):
if (training_perc + evaluating_perc + testing_perc) != 1.0:
print('Percentage summation is not equal to 1')
return
if (training_perc == 0.0 and testing_perc == 0.0):
print('Percentage of either training set or test set is equal to 0')
return
gaux = POINTER(Subgraph)()
gtraining = POINTER(Subgraph)()
gevaluating = POINTER(Subgraph)()
gtesting = POINTER(Subgraph)()
opf._splitsubgraph(g, byref(gaux), byref(gtesting), training_perc + evaluating_perc)
if (evaluating_perc > 0):
opf._splitsubgraph(gaux, byref(gtraining), byref(gevaluating), training_perc / (training_perc + evaluating_perc))
else:
gtraining = opf._copysubgraph(gaux)
return gtraining, gevaluating, gtesting