def threshold_shortest_paths(mtx, treatment=False):
""" Threshold a graph via via shortest path identification using Dijkstra's algorithm.
.. [Dimitriadis2010] Dimitriadis, S. I., Laskaris, N. A., Tsirka, V., Vourkas, M., Micheloyannis, S., & Fotopoulos, S. (2010). Tracking brain dynamics via time-dependent network analysis. Journal of neuroscience methods, 193(1), 145-155.
Parameters
----------
mtx : array-like, shape(N, N)
Symmetric, weighted and undirected connectivity matrix.
treatment : boolean
Convert the weights to distances by inversing the matrix. Also,
fill the diagonal with zeroes. Default `false`.
Returns
-------
binary_mtx : array-like, shape(N, N)
A binary mask matrix.
"""
imtx = mtx
if treatment:
imtx = 1.0 / mtx
np.fill_diagonal(imtx, 0.0)
binary_mtx = np.zeros_like(imtx, dtype=np.int32)
graph = nx.from_numpy_matrix(imtx)
paths = dict(nx.all_pairs_dijkstra_path(graph))
N, _ = np.shape(mtx)
for x in range(N):
for y in range(N):
r_path = paths[x][y]
num_nodes = len(r_path)
ind1 = -1
ind2 = -1
for m in range(0, num_nodes - 1):
ind1 = ind1 + 1
ind2 = ind1 + 1
binary_mtx[r_path[ind1], r_path[ind2]] = 1
binary_mtx[r_path[ind2], r_path[ind1]] = 1
return binary_mtx
评论列表
文章目录