-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_random_boundary_betweenness_cast.pyx
74 lines (59 loc) · 3.04 KB
/
_random_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
61
62
63
64
65
66
67
68
69
70
71
72
73
from RandomBoundaryBetweennessCast cimport RandomBoundaryBetweennessCast
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.
# Where an array need only be iterated over sequentially, a pointer to its
# first element should be assigned as an attribute to the c_cast object. Only
# when its elements need to be accessed in a discontinous manner should the
# attribute be set as a std::vector
cdef class PyCast:
cdef RandomBoundaryBetweennessCast c_cast # Hold a C++ instance which we're wrapping
def __init__(self, long long ptr):
self.c_cast = RandomBoundaryBetweennessCast()
self.c_cast.G_ptr = PyLong_AsVoidPtr(ptr)
def random_boundary_betweenness_compute(self, long[:] sources,
long[:] targets, long[:] incoming,
int num_edges, double[:] weights):
self.c_cast.sources_len = <long>len(sources)
self.c_cast.targets_len = <long>len(targets)
cdef vector[int] sources_vec
cdef vector[int] targets_vec
cdef vector[float] incoming_vec
for i in range(len(sources)):
sources_vec.push_back(sources[i])
incoming_vec.push_back(incoming[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
self.c_cast.incoming = incoming_vec
if weights is None:
weights = np.ones(num_edges+len(targets), dtype=np.double)
else:
mean=np.mean(weights)
weights = np.append(weights, np.full(len(targets), mean,
dtype=np.double))
cdef double[:] weights_memview = weights
self.c_cast.weights_ptr = &weights_memview[0]
self.c_cast.random_boundary_betweenness_compute()
@property
def linear_random_boundary_betweenness(self):
_betweennesses = np.zeros((self.c_cast.num_edges-self.c_cast.targets_len),
dtype=np.double)
#Return all elements of the betweenness vector, except the final
#elements connecting the targets and to the ghost
for i in range(self.c_cast.num_edges-self.c_cast.targets_len):
_betweennesses[i] = self.c_cast.linear_betweennesses[i]
return _betweennesses
@property
def nonlinear_random_boundary_betweenness(self):
_betweennesses = np.zeros((self.c_cast.num_edges-self.c_cast.targets_len),
dtype=np.double)
#Return all elements of the betweenness vector, except the final
#elements connecting the targets and to the ghost
for i in range(self.c_cast.num_edges-self.c_cast.targets_len):
_betweennesses[i] = self.c_cast.nonlinear_betweennesses[i]
return _betweennesses