-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
169 lines (144 loc) · 6.06 KB
/
utils.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
import itertools
import brainconn as bc
import numpy as np
import scipy
from tqdm import tqdm
def navigation_wu(nav_dist_mat, sc_mat, show_progress=True):
nav_paths = [] # (source, target, distance, hops, path)
for src in tqdm(range(len(nav_dist_mat)), disable=not show_progress):
for tar in range(len(nav_dist_mat)):
curr_pos = src
curr_path = [src]
curr_dist = 0
while curr_pos != tar:
neig = np.where(sc_mat[curr_pos, :] != 0)[0]
if len(neig) == 0:
curr_path = []
curr_dist = np.inf
break
neig_dist_to_tar = nav_dist_mat[neig, tar]
min_dist_idx = np.argmin(neig_dist_to_tar)
new_pos = neig[min_dist_idx]
if new_pos in curr_path:
curr_path = []
curr_dist = np.inf
break
else:
curr_path.append(new_pos)
curr_dist += nav_dist_mat[curr_pos, new_pos]
curr_pos = new_pos
nav_paths.append((src, tar, curr_dist, len(curr_path) - 1, curr_path))
nav_sr = len([_ for _ in nav_paths if _[3] != -1]) / len(nav_paths)
nav_sr_node = []
for k, g in itertools.groupby(
sorted(nav_paths, key=lambda x: x[0]), key=lambda x: x[0]
):
curr_path = list(g)
nav_sr_node.append(len([_ for _ in curr_path if _[3] != -1]) / len(curr_path))
nav_path_len, nav_path_hop = np.zeros_like(nav_dist_mat), np.zeros_like(
nav_dist_mat
)
for nav_item in nav_paths:
i, j, length, hop, _ = nav_item
if hop != -1:
nav_path_len[i, j] = length
nav_path_hop[i, j] = hop
else:
nav_path_len[i, j] = np.inf
nav_path_hop[i, j] = np.inf
return nav_sr, nav_sr_node, nav_path_len, nav_path_hop, nav_paths
def search_information(W, L, has_memory=False):
N = len(W)
if np.allclose(W, W.T):
flag_triu = True
else:
flag_triu = False
T = np.linalg.solve(np.diag(np.sum(W, axis=1)), W)
_, hops, Pmat = bc.distance.distance_wei_floyd(L, transform=None)
SI = np.zeros((N, N))
SI[np.eye(N) > 0] = np.nan
for i in range(N):
for j in range(N):
if (j > i and flag_triu) or (not flag_triu and i != j):
path = bc.distance.retrieve_shortest_path(i, j, hops, Pmat)
lp = len(path) - 1
if flag_triu:
if np.any(path):
pr_step_ff = np.zeros(lp)
pr_step_bk = np.zeros(lp)
if has_memory:
pr_step_ff[0] = T[path[0], path[1]]
pr_step_bk[lp - 1] = T[path[lp], path[lp - 1]]
for z in range(1, lp):
pr_step_ff[z] = T[path[z], path[z + 1]] / (
1 - T[path[z - 1], path[z]]
)
pr_step_bk[lp - z - 1] = T[
path[lp - z], path[lp - z - 1]
] / (1 - T[path[lp - z + 1], path[lp - z]])
else:
for z in range(lp):
pr_step_ff[z] = T[path[z], path[z + 1]]
pr_step_bk[z] = T[path[z + 1], path[z]]
prob_sp_ff = np.prod(pr_step_ff)
prob_sp_bk = np.prod(pr_step_bk)
SI[i, j] = -np.log2(prob_sp_ff)
SI[j, i] = -np.log2(prob_sp_bk)
else:
if np.any(path):
pr_step_ff = np.zeros(lp)
if has_memory:
pr_step_ff[0] = T[path[0], path[1]]
for z in range(1, lp):
pr_step_ff[z] = T[path[z], path[z + 1]] / (
1 - T[path[z - 1], path[z]]
)
else:
for z in range(lp):
pr_step_ff[z] = T[path[z], path[z + 1]]
prob_sp_ff = np.prod(pr_step_ff)
SI[i, j] = -np.log2(prob_sp_ff)
else:
SI[i, j] = np.inf
return SI
def group_by_index(val_List, idx_list):
result = []
for _ in sorted(set(idx_list)):
result.append([val_List[it] for it, idx in enumerate(idx_list) if idx == _])
return result
def communicability_wei(adjacency):
"""
Computes the communicability of pairs of nodes in `adjacency`
Parameters
----------
adjacency : (N, N) array_like
Weighted, direct/undirected connection weight/length array
Returns
-------
cmc : (N, N) numpy.ndarray
Symmetric array representing communicability of nodes {i, j}
References
----------
Crofts, J. J., & Higham, D. J. (2009). A weighted communicability measure
applied to complex brain networks. Journal of the Royal Society Interface,
6(33), 411-414.
Examples
--------
>>> from netneurotools import metrics
>>> A = np.array([[2, 0, 3], [0, 2, 1], [0.5, 0, 1]])
>>> Q = metrics.communicability_wei(A)
>>> Q
array([[0. , 0. , 1.93581903],
[0.07810379, 0. , 0.94712177],
[0.32263651, 0. , 0. ]])
"""
# negative square root of nodal degrees
row_sum = adjacency.sum(1)
neg_sqrt = np.power(row_sum, -0.5)
square_sqrt = np.diag(neg_sqrt)
# normalize input matrix
for_expm = square_sqrt @ adjacency @ square_sqrt
# calculate matrix exponential of normalized matrix
cmc = scipy.sparse.linalg.expm(for_expm)
cmc[np.diag_indices_from(cmc)] = 0
return cmc