-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_boundary_betweenness_cast.pyx
61 lines (45 loc) · 2.09 KB
/
_boundary_betweenness_cast.pyx
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
from BoundaryBetweennessCast cimport BoundaryBetweennessCast
from cpython.long cimport PyLong_AsVoidPtr
import numpy as np
from libcpp.vector cimport vector
# Create a Cython extension type which holds a C++ instance
# as an attribute and create a bunch of forwarding methods
# Python extension type.
cdef class PyCast:
cdef BoundaryBetweennessCast c_cast # Hold a C++ instance which we're wrapping
def __init__(self, long long ptr):
self.c_cast = BoundaryBetweennessCast()
self.c_cast.G_ptr = PyLong_AsVoidPtr(ptr)
def boundary_betweenness_compute(self, long long[:] sources,
long long[:] targets, int num_edges,
double[:] weights):
self.c_cast.sources_len = <long>len(sources)
self.c_cast.targets_len = <long>len(targets)
"""
cdef vector[long long] sources_vec
cdef vector[long long] targets_vec
for i in range(len(sources)):
sources_vec.push_back(sources[i])
for i in range(len(targets)):
targets_vec.push_back(targets[i])
self.c_cast.sources = sources_vec
self.c_cast.targets = targets_vec
"""
if weights is None:
weights = np.ones(num_edges+1, dtype=np.double)
cdef double[:] weights_memview = weights
self.c_cast.weights_ptr = &weights_memview[0]
cdef long long[:] sources_memview = sources
cdef long long[:] targets_memview = targets
#cdef double[:] weights_memview = weights
self.c_cast.sources_ptr = &sources_memview[0]
self.c_cast.targets_ptr = &targets_memview[0]
self.c_cast.weights_ptr = &weights_memview[0]
self.c_cast.boundary_betweenness_compute()
@property
def boundary_betweenness(self):
_boundary_betweennesses = np.zeros((self.c_cast.num_edges),
dtype=np.double)
for i in range(self.c_cast.num_edges):
_boundary_betweennesses[i] = self.c_cast.betweennesses[i]
return _boundary_betweennesses