def compute_metric_backbone(G): print('> Computing Dijkstra APSP') dict_edges = {(i, j): prox2dist(d['weight']) for i, j, d in G.edges(data=True)} dij = Dijkstra.from_edgelist(dict_edges, directed=False, verbose=10) print('> Metric') poolresults = list(range(len(dij.N))) for node in dij.N: print('> Dijkstra node {node:d} of {n_nodes:d}'.format(node=node + 1, n_nodes=len( dij.N))) poolresults[node] = _cy_single_source_shortest_distances( node, dij.N, dij.E, dij.neighbors, ('min', 'sum'), verbose=2) shortest_distances, local_paths = map(list, zip(*poolresults)) dij.shortest_distances = dict(zip(dij.N, shortest_distances)) MSD = dij.get_shortest_distances(format='dict', translate=True) # dict_edges_backbone = {} dict_edges_s_values = {} for (i, j), d in dict_edges.items(): # Backbone = distance == distance-closure if d == MSD[i][j]: dict_edges_backbone[(i, j)] = True else: dict_edges_backbone[(i, j)] = False # S-Value = distance / distance-closure dict_edges_s_values[(i, j)] = d / MSD[i][j] return dict_edges_backbone, dict_edges_s_values
def test_dist2prox_prox2dist(): """ Test Utils: Prox2Dist & Dist2Prox """ assert np.isclose(dist2prox(prox2dist(P)) , P).all()
def test_dist2prox(): """ Test Utils: Dist2Prox """ assert np.isclose(prox2dist(P), D_true).all()
icxl, icxh = module['xy-coords']['x-values'] icyl, icyh = module['xy-coords']['y-values'] dfPCA_tmp = dfPCA.loc[((dfPCA[icx] >= icxl) & (dfPCA[icx] <= icxh) & (dfPCA[icy] >= icyl) & (dfPCA[icy] <= icyh)), ['gene', icx, icy]] component_node_ids = dfPCA_tmp.index.to_list() # Gtc = nx.subgraph(Gt, component_node_ids) # Converting prox2dist nx.set_edge_attributes(Gtc, name='distance', values={(i, j): prox2dist(d['weight']) for i, j, d in Gt.edges(data=True)}) # # Caculate Network Features # print('Calculating: degree centrality') dict_degree_centrality = nx.degree_centrality(Gt) nx.set_node_attributes(Gtc, name='degree_centrality', values=dict_degree_centrality) print("Calculating: eigevector centrality") dict_eigenvector_centrality = nx.eigenvector_centrality( Gtc, weight='weight') nx.set_node_attributes(Gtc, name='eigenvector_centrality',
from distanceclosure.dijkstra import Dijkstra from distanceclosure.utils import prox2dist import numpy as np import networkx as nx # # Test # # Numpy P = np.array([ [1.,.9,.1,0.], [.9,1.,.8,0.], [.1,.8,1.,.6], [0.,0.,.6,1.], ], dtype=float) D = prox2dist(P) # # Edgelist # edgelist_luis = { ('s','b'):.9, ('s','c'):.1, ('b','c'):.8, ('c','d'):.6, } edgelist_james = { ('s','a'):8, ('s','c'):6, ('s','d'):5, ('a','d'):2, ('a','e'):1,
} """ edgelist = { ('s','b'):.9, ('s','c'):.1, ('b','c'):.8, ('c','d'):.6, } """ matrix = np.array([ [1.,.9,.1,0.], [.9,1.,.8,0.], [.1,.8,1.,.6], [0.,0.,.6,1.], ], dtype=float) matrix = prox2dist(matrix) sparse = csr_matrix(matrix) source = 2 # NX #G = nx.from_edgelist(edgelist) G = nx.from_numpy_matrix(matrix) #nx.set_edge_attributes(G, 'weight', edgelist) nx_lenghts = nx.single_source_dijkstra_path_length(G, source=source, weight='weight') nx_paths = nx.single_source_dijkstra_path(G, source=source, weight='weight') d = Dijkstra(verbose=True) #d.from_edgelist(edgelist, directed=False) #d.from_numpy_matrix(matrix, directed=False) d.from_sparse_matrix(sparse, directed=False)
first_line = File.readlines() F = first_line[0].rstrip().split() for index in np.arange(len(rows)): Index_r = rows[index] Index_c = cols[index] if [F[Index_c], F[Index_r]] not in llista: llista.append([F[Index_r], F[Index_c]]) File.close() return llista # First, the DEanalysis.r script needs to be run to get the individual x individual correlation matrices. # Convert to Distance corr_matrix = 'Data/Correlation_matrix.txt' corr_matrix_array = from_corr_matrix_to_array(corr_matrix) D = prox2dist(corr_matrix_array) # Calculate transitive closure using the metric and ultra-metric (also known maximum flow) measure Cm = transitive_closure(D, kind='metric') Cu = transitive_closure(D, kind='ultrametric') # Retrieve the backbone edges Bm = backbone(D, Cm) Bu = backbone(D, Cu) # Backbone edges on Bm and Bu can be accessed, where edges with a 1 are metric. # We used the backbone edges based on the ultra-metric transitive closure. rows, cols = np.where(Bu == 1) # We get a two columns file where only the nodes that are connected through a backbone edge are showed. t = ""