def compare(self, g_1, g_2, verbose=False): """Compute the kernel value (similarity) between two graphs. Parameters ---------- g1 : networkx.Graph First graph. g2 : networkx.Graph Second graph. Returns ------- k : The similarity value between g1 and g2. """ # Diagonal superior matrix of the floyd warshall shortest # paths: fwm1 = np.array(nx.floyd_warshall_numpy(g_1)) fwm1 = np.where(fwm1 == np.inf, 0, fwm1) fwm1 = np.where(fwm1 == np.nan, 0, fwm1) fwm1 = np.triu(fwm1, k=1) bc1 = np.bincount(fwm1.reshape(-1).astype(int)) fwm2 = np.array(nx.floyd_warshall_numpy(g_2)) fwm2 = np.where(fwm2 == np.inf, 0, fwm2) fwm2 = np.where(fwm2 == np.nan, 0, fwm2) fwm2 = np.triu(fwm2, k=1) bc2 = np.bincount(fwm2.reshape(-1).astype(int)) # Copy into arrays with the same length the non-zero shortests # paths: v1 = np.zeros(max(len(bc1), len(bc2)) - 1) v1[range(0, len(bc1)-1)] = bc1[1:] v2 = np.zeros(max(len(bc1), len(bc2)) - 1) v2[range(0, len(bc2)-1)] = bc2[1:] return np.sum(v1 * v2)
def test_zero_weight(self): G = nx.DiGraph() edges = [(1,2,-2), (2,3,-4), (1,5,1), (5,4,0), (4,3,-5), (2,5,-7)] G.add_weighted_edges_from(edges) dist = nx.floyd_warshall_numpy(G) assert_equal(int(numpy.min(dist)), -14) G = nx.MultiDiGraph() edges.append( (2,5,-7) ) G.add_weighted_edges_from(edges) dist = nx.floyd_warshall_numpy(G) assert_equal(int(numpy.min(dist)), -14)
def test_zero_weight(self): G = nx.DiGraph() edges = [(1, 2, -2), (2, 3, -4), (1, 5, 1), (5, 4, 0), (4, 3, -5), (2, 5, -7)] G.add_weighted_edges_from(edges) dist = nx.floyd_warshall_numpy(G) assert int(numpy.min(dist)) == -14 G = nx.MultiDiGraph() edges.append((2, 5, -7)) G.add_weighted_edges_from(edges) dist = nx.floyd_warshall_numpy(G) assert int(numpy.min(dist)) == -14
def trainSOMOBatch(matX, G, tmax=1000, sigma0=1.0): m, n = matX.shape # Distance among nodes calculation matD = np.asarray(nx.floyd_warshall_numpy(G))**2 for t in range(tmax): # For each point of input space get the best matching b = [0 for x in range(n)] for j in range(n): xj = np.array(matX[:, j]).astype(np.float) b[j] = getBestMatchingUnit(G, xj) # update the topological adaption rate sigma = sigma0 * np.exp(-t / tmax) # update all weights for i in G: summation_xj_multipliedby_h = np.array((0, 0, 0)).astype(np.float) summation_h = 0 for j in range(n): xj = np.array(matX[:, j]).astype(np.float) summation_xj_multipliedby_h = summation_xj_multipliedby_h + ( calculate_h(matD, b[j], i, sigma) * xj) summation_h = summation_h + calculate_h(matD, b[j], i, sigma) G.nodes[i]['w'] = summation_xj_multipliedby_h / summation_h if t == (tmax - 1): plotFigure(matX, G, "Batch " + str(tmax)) return G
def get_distance_matrix_from_graph(network, filename = None, floyd = True): """ Returns and optionally stores the distance matrix for a given network. By default the networkX BFS implementation is used. Parameters ---------- network : a NetworkX graph (ATTENTION: nodes need to be sequentially numbered starting at 1!) filename : destination for storing the matrix (optional) floyd : set to true to use floyd warshall instead of BFS Returns ------- A Numpy matrix storing all pairs shortest paths for the given network (or the nodes in the given nodelist). """ n = nx.number_of_nodes(network) if floyd: D = nx.floyd_warshall_numpy(network) else: D_dict = nx.all_pairs_shortest_path_length(network) D = numpy.zeros((n,n)) for row, col_dict in D_dict.iteritems(): for col in col_dict: D[row-1,col-1] = col_dict[col] if filename: numpy.savetxt(filename, D, fmt='%s', delimiter=",", newline="\n") return D
def test_weighted_numpy_two_edges(self): XG4 = nx.Graph() XG4.add_weighted_edges_from([[0, 1, 2], [1, 2, 2], [2, 3, 1], [3, 4, 1], [4, 5, 1], [5, 6, 1], [6, 7, 1], [7, 0, 1]]) dist = nx.floyd_warshall_numpy(XG4) assert dist[0, 2] == 4
def __call__(self): number_of_nodes = int( np.random. noncentral_chisquare(self.degrees_freedom, self.noncentrality) ) + 1 nodes = [] nodes.append(np.random.normal(loc=0, scale=10, size=(2, ))) for i in range(number_of_nodes - 1): if self.sampling_mean == 'previous': location = nodes[-1] else: location = self.sampling_mean nodes.append( np.random.normal( loc=location, scale=self.sampling_std, size=(2, ) ) ) connectivity_matrix = np.zeros((len(nodes), len(nodes))) for i, ni in enumerate(nodes): for j, nj in zip(range(i, len(nodes)), nodes[i:]): if i == j: continue connectivity_matrix[j, i] = connectivity_matrix[ i, j] = self.connectivity_fn( ni, nj, sensitivity=self.sensitivity ) G = nx.from_numpy_matrix(connectivity_matrix) dists = nx.floyd_warshall_numpy(G) dists[np.isinf(dists)] = 0 nodes = np.stack(nodes) return nodes, dists, connectivity_matrix
def contacts2distances(contacts): """ Infer distances from contact matrix """ # create graph graph = nx.Graph() graph.add_nodes_from(range(contacts.shape[0])) for row in range(contacts.shape[0]): for col in range(contacts.shape[1]): freq = contacts[row, col] if freq != 0: graph.add_edge(col, row, weight=1/freq) # find shortest paths spath_mat = nx.floyd_warshall_numpy(graph, weight='weight') # create distance matrix distances = np.zeros(contacts.shape) for row in range(contacts.shape[0]): for col in range(contacts.shape[1]): if spath_mat[row, col] == float('inf'): distances[row, col] = 1000000 else: distances[row, col] = spath_mat[row, col] return distances
def _generate(self): if self.ALWAYS_SAME: np.random.seed(1) # to make all random values same # skills of every person: every person has a list of s elements describing his/her skill values fall in [0-1]. if self.DISTRIBUTION == 'Uniform': self.skills = np.random.rand(self.n, self.s) elif self.DISTRIBUTION == 'Normal': self.skills = np.random.randn(self.n, self.s) * 0.1666 + 0.5 else: raise ValueError('ERROR in DISTRIBUTION value: ' % self.DISTRIBUTION) # risk taking of every person: it is also a value between [0,1]. if self.DISTRIBUTION == 'Uniform': self.risk_takings = np.random.rand(self.n) elif self.DISTRIBUTION == 'Normal': self.risk_takings = np.random.randn(self.n) * 0.1666 + 0.5 # network connectivity (or distance) is a matrix of n*n: we always can for any # graph, compute the shortest path among all nodes # and normalize it between [0,1]. We assume, relationships are not directed. if self.ALWAYS_SAME: # it keeps the same numbers which is helpful for debugging purposes G = nx.to_undirected(nx.scale_free_graph(self.n, seed=1)) else: # it changes the structure every time we run G = nx.to_undirected(nx.scale_free_graph(self.n)) D = nx.floyd_warshall_numpy(G) D /= np.max(D) self.network_connectivity = D
def convert_points_to_graph(x, G): C = euclid_dist(x, x) idx = np.argsort(C, axis=1) # Preprocess graph for i, j in G.edges: G[i][j]['weight'] = C[i, j] # Add all edges of the circle for i in range(100): G.add_edge(i, (i + 1) % 100, weight=C[i, (i + 1) % 40]) # Add all edges of the knn for i in range(C.shape[0]): for j in range(1, 3): G.add_edge(i, idx[i, j], weight=C[i, idx[i, j]]) if not nx.is_connected(G): comp = list(nx.connected_components(G)) for c in range(1, nx.number_connected_components(G)): for j in range(3, 10): for n in comp[c]: if idx[n, j] in comp[0]: G.add_edge(n, idx[n, j], weight=C[n, idx[n, j]]) break if nx.is_connected(G): break print(f"CHECK CONNECTION = ", nx.is_connected(G)) dist = nx.floyd_warshall_numpy(G) return dist, G
def contacts2distances(contacts): """ Infer distances from contact matrix """ # create graph graph = nx.Graph() graph.add_nodes_from(range(contacts.shape[0])) for row in range(contacts.shape[0]): for col in range(contacts.shape[1]): freq = contacts[row, col] if freq != 0: graph.add_edge(col, row, weight=1 / freq) # find shortest paths spath_mat = nx.floyd_warshall_numpy(graph, weight='weight') # create distance matrix distances = np.zeros(contacts.shape) for row in range(contacts.shape[0]): for col in range(contacts.shape[1]): if spath_mat[row, col] == float('inf'): distances[row, col] = 1000000 else: distances[row, col] = spath_mat[row, col] return distances
def test_weighted_numpy(self): XG4=nx.Graph() XG4.add_weighted_edges_from([ [0,1,2],[1,2,2],[2,3,1], [3,4,1],[4,5,1],[5,6,1], [6,7,1],[7,0,1] ]) dist = nx.floyd_warshall_numpy(XG4) assert_equal(dist[0,2],4)
def analyze(G,inputs=()): """Compute effective distance matrix and analyze simulation results Input: G: Weighted undirected NetworkX graphs inputs: can be used to provide additional needed information Output: D: N x N effective distance matrix (a numpy array) """ G1 = nx.convert_node_labels_to_integers(G,label_attribute='country') country_dict={} for i in range(len(G1)): country_dict[G1.nodes[i]['country']]=i A = nx.adjacency_matrix(G1).todense() N = G1.number_of_nodes() figures_for_analyze(G) P=np.zeros((N,N)) for i in range(N): P[i,:]=A[i,:]/(A[i,:].sum()) d=np.zeros((N,N)) iK,jK = np.where( P != 0) for i,j in zip(iK,jK): d[i,j]=1-np.log(P[i,j]) #create new directed graph using effective distance d G2=nx.from_numpy_matrix(d,create_using=nx.DiGraph) #find shortest paths D=nx.floyd_warshall_numpy(G2, weight="weight") figures_for_analyze(G,D) return D
def test_weight_parameter_numpy(self): XG4 = nx.Graph() XG4.add_edges_from([ (0, 1, { "heavy": 2 }), (1, 2, { "heavy": 2 }), (2, 3, { "heavy": 1 }), (3, 4, { "heavy": 1 }), (4, 5, { "heavy": 1 }), (5, 6, { "heavy": 1 }), (6, 7, { "heavy": 1 }), (7, 0, { "heavy": 1 }), ]) dist = nx.floyd_warshall_numpy(XG4, weight="heavy") assert dist[0, 2] == 4
def sorteddm(g): logger = logging.getLogger() result = hashlib.md5( matrix_sort(np.array( nx.floyd_warshall_numpy(g))).encode('utf-8')).hexdigest() logger.debug(result) return result
def floydTransformation(G, edge_weight=None): """Transform graph G to its corresponding shortest-paths graph using Floyd-transformation. Parameters ---------- G : NetworkX graph The graph to be tramsformed. edge_weight : string edge attribute corresponding to the edge weight. The default edge weight is bond_type. Return ------ S : NetworkX graph The shortest-paths graph corresponding to G. References ---------- [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE. """ spMatrix = nx.floyd_warshall_numpy(G, weight=edge_weight) S = nx.Graph() S.add_nodes_from(G.nodes(data=True)) ns = list(G.nodes()) for i in range(0, G.number_of_nodes()): for j in range(i + 1, G.number_of_nodes()): if spMatrix[i, j] != np.inf: S.add_edge(ns[i], ns[j], cost=spMatrix[i, j]) return S
def __cost_function(self): """ Driving Cost Function """ distances = nx.floyd_warshall_numpy(self.G) driving_cost_function = [] for c in range(self.number_of_homes + 1): summation = [] for i in range(self.number_of_locations): for j in range(self.number_of_locations): summation.append( grb.QuadExpr( self.arrangement_matrix[i][c] * distances.item( (i, j)) * self.arrangement_matrix[j][c + 1])) self.model.update() driving_cost_function.append(grb.quicksum(summation)) """ Walking Cost Function """ walking_cost_function = [] for row in range(self.number_of_locations): for col in range(self.number_of_locations): walking_cost_function.append( grb.LinExpr(self.walking_matrix[row][col] * distances.item( (row, col)))) self.model.update() """ Set Objective Function """ cost_function = driving_cost_function + walking_cost_function self.model.setObjective(grb.quicksum(cost_function), grb.GRB.MINIMIZE)
def test_weight_parameter_numpy(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') XG4 = nx.Graph() XG4.add_edges_from([(0, 1, { 'heavy': 2 }), (1, 2, { 'heavy': 2 }), (2, 3, { 'heavy': 1 }), (3, 4, { 'heavy': 1 }), (4, 5, { 'heavy': 1 }), (5, 6, { 'heavy': 1 }), (6, 7, { 'heavy': 1 }), (7, 0, { 'heavy': 1 })]) dist = nx.floyd_warshall_numpy(XG4, weight='heavy') assert_equal(dist[0, 2], 4)
def distance_distribution(G): """Computes the distribution of node distances of a graph. Uses the Floyd-Warshall algorithm for the distances and formats it in the D-measure article standard. Args: G (nx.Graph): graph with N nodes Returns: nodes_distrib (np.array): (N, N) matrix containing the normalized distribution of distances of the graph. For each node i, the distribution is (p_1, p_2, ..., p_j, ..., p_N), where p_j is the proportion of nodes in the graph at distance j of the node i. Nodes with distance N are disconnected from the graph. """ N = G.number_of_nodes() dist_matrix = np.asarray(nx.floyd_warshall_numpy(G, weight=1)) dist_matrix[dist_matrix == np.inf] = N nodes_distrib = np.zeros((N, N + 1)) for row in range(len(dist_matrix)): for length in dist_matrix[row]: nodes_distrib[row][int(length)] += 1 nodes_distrib /= (N - 1) return nodes_distrib[:, 1:]
def fit_transform(self, X, y=None): self.n = X.shape[0] tree = sc.spatial.KDTree(X) dist, idx = tree.query(X, self.k + 1) G = nx.Graph() G.add_nodes_from(idx[:, 0]) for i in range(1, self.k + 1): G.add_weighted_edges_from(np.c_[idx[:, [0, i]], dist[:, i]]) P = np.asarray(nx.floyd_warshall_numpy(G)) Y = KernelPCA(self.components, kernel='precomputed').fit_transform(-0.5 * (P**2.0)) self.P_tril = P[np.tril_indices(P.shape[0], k=-1)] print("Initial loss:", self.cda_loss(Y)) res = minimize(self.cda_loss, Y.reshape(self.n * self.components), method='L-BFGS-B', jac=self.cda_loss_grad, options={ 'disp': True, 'gtol': 1e-9, 'maxiter': self.max_iter }) Y = res.x.reshape(self.n, self.components) print("Trained loss:", self.cda_loss(Y)) return Y
def perform_clustering(filepath, method, metric, cutoff, dataset_name=None): path = IMG_DIR + dataset_name + '-' + metric + '-' + method ext = '.png' G = nx.read_gml(filepath) if dataset_name == 'karate': G = nx.convert_node_labels_to_integers(G, first_label=0) if metric == 'euclidean' or metric == 'correlation': adj_matrix = nx.adjacency_matrix(G) distance_matrix = adj_matrix.todense() linkage_matrix = hierarchy.linkage(distance_matrix, method=method, metric=metric) elif metric == 'shortest-paths': distance_matrix = nx.floyd_warshall_numpy(G) linkage_matrix = hierarchy.linkage(distance_matrix, method=method) elif metric == 'commute-time': adj_matrix = nx.adjacency_matrix(G) distance_matrix = commute_time_matrix(G, adj_matrix) linkage_matrix = hierarchy.linkage(distance_matrix, method=method) else: print "invalid metric" return pyplot.subplot(1, 1, 1) hierarchy.dendrogram(linkage_matrix) # pyplot.show() pyplot.savefig(path + ext) clustering = hierarchy.fcluster(linkage_matrix, cutoff) draw_graph(G, clustering, path + '-graph' + ext)
def initialize_rope_from_cloud(xyzs, plotting=False): xyzs = xyzs.reshape(-1,3) if len(xyzs) > 500: xyzs = xyzs[::len(xyzs)//500] pdists = ssd.squareform(ssd.pdist(xyzs,'sqeuclidean')) G = nx.Graph() for (i_from, row) in enumerate(pdists): to_inds = np.flatnonzero(row[:i_from] < MAX_DIST**2) for i_to in to_inds: G.add_edge(i_from, i_to, weight = pdists[i_from, i_to]) A = nx.floyd_warshall_numpy(G) A[np.isinf(A)] = 0 (i_from_long, i_to_long) = np.unravel_index(A.argmax(), A.shape) nodes = G.nodes(); path = nx.shortest_path(G, source=nodes[i_from_long], target=nodes[i_to_long]) xyz_path = xyzs[path,:] xyzs_unif = curves.unif_resample(xyz_path,N_CTRL_PTS,tol=.005) labels = np.ones(len(xyzs_unif)-1,'int') labels[[0,-1]] = 2 if plotting: import enthought.mayavi.mlab as mlab mlab.plot3d(*xyzs_unif.T, tube_radius=.001) mlab.points3d(*xyzs.T, scale_factor=.01) mlab.show() return xyzs_unif, labels
def headSTwig_selection(query, roots): # List of nodes of query nodes = query.nodes() # Number of nodes of the query graph #n = len(nodes) # Index of roots in nodes roots_id = [nodes.index(r) for r in roots] # Distance matrix between all pair of nodes in the query M = nx.floyd_warshall_numpy(query, nodes) # We are interested only in distance between root-nodes M = M[roots_id, :] M = M[:, roots_id] # List of max M_ri,rj for each root ''' max_M = [] for r in range(0,len(roots)): max = np.amax(M[r,:]) max_M.append(max) ''' max_M = [np.amax(M[r, :]) for r in range(0, len(roots))] # Index of the min distance head_id = max_M.index(min(max_M)) # Head STwig selection # root of the STwig head_root = roots[head_id] return head_root
def __init__(self, partial_graph): self.number_of_points_partial = 3 if self.point_features is None: self.point_features = np.zeros((self.number_of_points, 4)) for i, j in self.point_edge_list: self.point_features[i][2:] = self.points_coordinates[ j] - self.points_coordinates[i] self.point_features[j][:2] = self.points_coordinates[ i] - self.points_coordinates[j] self.connectivity_matrix = np.zeros( (self.number_of_points, self.number_of_points) ) for i, j in self.point_edge_list: self.connectivity_matrix[i][j] = 1 self.connectivity_matrix[j][i] = 1 self.graph = nx.from_numpy_matrix(self.connectivity_matrix) self.dists = nx.floyd_warshall_numpy(self.graph) self.dists[np.isinf(self.dists)] = 0 if partial_graph: self._forward = self._partial_graph_forward else: self._forward = self._full_graph_forward
def __init__(self, n_0, average_degreee_0, n, m, degree_only=False): # Define os principais parâmetros self.n_0 = n_0 self.average_degreee_0 = average_degreee_0 self.n = n self.m = m # Cria grafo inicial como uma rede de Erdos-Renyi self.G = Networks.ErdosRenyi_network(self.n_0, self.average_degreee_0).G new_edge = np.zeros(self.m) # Adiciona novo nó for j in range(1, self.n-self.n_0+1): # print(str(j)+'/'+str(self.n-self.n_0)) self.G.add_node(self.n_0+j) # Obtem nós com seus respectivos graus possible_edges = np.array(self.G.degree, dtype=float)[:,] # Normaliza possible_edges[:,1] = possible_edges[:,1]/np.sum(possible_edges[:,1]) # Realiza nova conexão for i in range(self.m): # Escolhe conexão new_edge[i] = np.random.choice(possible_edges[:,0], p=possible_edges[:,1]) # print(new_edge[i]) # Elimina nó escolhido possible_edges = np.delete(possible_edges, np.where(possible_edges[:,0] == new_edge[i]), axis=0) # Renormaliza possible_edges[:,1] = possible_edges[:,1]/np.sum(possible_edges[:,1]) self.G.add_edge(self.n_0+j, new_edge[i]) # Obtem a média e o desvio padrão do grau self.degree = np.array(sorted([d for n, d in self.G.degree()], reverse=True)) self.degree_mu = self.degree.mean() self.degree_sigma = self.degree.std() if not degree_only: # Obtem a média e o desvio padrão do coeficiente de aglomeração self.clustering_coefficient = np.array(sorted([nx.clustering(self.G,n) for n in nx.clustering(self.G)], reverse=True)) self.clustering_coefficient_mu = self.clustering_coefficient.mean() self.clustering_coefficient_sigma = self.clustering_coefficient.std() # Obtem a média e o desvio padrão do caminho mínimo para todos os pares de pontos # através do método de Floyd-Warshall fw_aux = np.asarray(nx.floyd_warshall_numpy(self.G)).reshape(-1) self.floyd_warshall = np.array(np.delete(fw_aux, np.where(np.logical_or(fw_aux == 0, fw_aux == float('inf')))), dtype=int) self.shortest_path_length_mu = self.floyd_warshall.mean() self.shortest_path_length_sigma = self.floyd_warshall.std() #Identificador único do grafo gerado self.dt_string = datetime.now().strftime("_%d-%m-%Y_%H-%M-%S") self.filename = 'img/Barabasi-Albert'+'_n='+str(self.n)+'_m='+str(self.m)+self.dt_string
def getCertainDistPairing(adjMat, dist = 4, numOfPairs = 1): G = nx.from_numpy_array(adjMat) distMat = np.array(nx.floyd_warshall_numpy(G)) all_pairing = np.array(np.where(distMat == dist)).T if len(all_pairing) >= numOfPairs: return all_pairing[np.random.choice(len(all_pairing), numOfPairs, replace = False)] else: return all_pairing[np.random.choice(len(all_pairing), len(all_pairing), replace = False)]
def graph_node_position_mds(G): nx.set_edge_attributes(G, 1, 'weight') length = nx.floyd_warshall_numpy(G) mds = manifold.MDS(n_components=2, dissimilarity='precomputed') pos = mds.fit(length).embedding_ return pos
def graph_node_position_mds(G): for edge in G.edges(): G.add_edge(edge[0], edge[1], weight=1) A = nx.floyd_warshall_numpy(G) #print(G.edges(data=True)) mds = skln.MDS(n_components=2, dissimilarity='precomputed') pos = mds.fit(A).embedding_ return pos
def test_nodelist(): G = nx.path_graph(7) dist = nx.floyd_warshall_numpy(G, nodelist=[3, 5, 4, 6, 2, 1, 0]) assert dist[0, 3] == 3 assert dist[0, 1] == 2 assert dist[6, 2] == 4 pytest.raises(nx.NetworkXError, nx.floyd_warshall_numpy, G, [1, 3]) pytest.raises(nx.NetworkXError, nx.floyd_warshall_numpy, G, list(range(9)))
def test_cycle_numpy(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') dist = nx.floyd_warshall_numpy(nx.cycle_graph(7)) assert_equal(dist[0,3],3) assert_equal(dist[0,4],3)
def graph_diameter(G): """Compute the diameter of a given graph. NOTE: Given graph MUST be STRONGLY connected. """ # @TODO: choose the better algorithm depending on the density of # the graph return nx.floyd_warshall_numpy(G).max()
def graph_node_position_Laplacian(G): nx.set_edge_attributes(G, 1, 'weight') length = nx.floyd_warshall_numpy(G) spec = manifold.SpectralEmbedding(n_components=2) pos = spec.fit(length).embedding_ return pos
def test_weight_parameter_numpy(self): XG4 = nx.Graph() XG4.add_edges_from([ (0, 1, {'heavy': 2}), (1, 2, {'heavy': 2}), (2, 3, {'heavy': 1}), (3, 4, {'heavy': 1}), (4, 5, {'heavy': 1}), (5, 6, {'heavy': 1}), (6, 7, {'heavy': 1}), (7, 0, {'heavy': 1}) ]) dist = nx.floyd_warshall_numpy(XG4, weight='heavy') assert_equal(dist[0, 2], 4)
def _compare(self, g1, g2): """Compute the kernel value between the two graphs. Parameters ---------- g1 : ndarray Adjacency matrix of the first graph. g2 : ndarray Adjacency matrix of the second graph. Returns ------- k : The similarity value between g1 and g2. """ if self.binary_edges: g1 = np.where(g1 > self.th, 1, 0) g2 = np.where(g2 > self.th, 1, 0) else: g1 = np.where(g1 > self.th, g1, 0) g2 = np.where(g2 > self.th, g2, 0) g1 = nx.from_numpy_matrix(g1) g2 = nx.from_numpy_matrix(g2) #Diagonal superior matrix of the floyd warshall shortest paths fwm1 = np.array(nx.floyd_warshall_numpy(g1)) fwm1 = np.where(fwm1==np.inf, 0, fwm1) fwm1 = np.where(fwm1==np.nan, 0, fwm1) fwm1 = np.triu(fwm1, k=1) bc1 = np.bincount(fwm1.reshape(-1).astype(int)) fwm2 = np.array(nx.floyd_warshall_numpy(g2)) fwm2 = np.where(fwm2==np.inf, 0, fwm2) fwm2 = np.where(fwm2==np.nan, 0, fwm2) fwm2 = np.triu(fwm2, k=1) bc2 = np.bincount(fwm2.reshape(-1).astype(int)) #Copy into arrays with the same length the non-zero shortests paths v1 = np.zeros(max(len(bc1),len(bc2)) - 1) v1[range(0,len(bc1)-1)] = bc1[1:] v2 = np.zeros(max(len(bc1),len(bc2)) - 1) v2[range(0,len(bc2)-1)] = bc2[1:] return np.sum(v1 * v2)
def mds_embed(graph): sorted_node_list = sorted(list(graph.nodes()), key=len) dmat = nx.floyd_warshall_numpy(graph, nodelist=sorted_node_list) gmds = MDS(n_jobs=-2, dissimilarity="precomputed") embed_pts = gmds.fit_transform(dmat) return (embed_pts, dmat, sorted_node_list)
def _cal_dist2center(G, Centeroids): """ Calculate the distances to cluster centers """ D_Matrix = nx.floyd_warshall_numpy(G) Dict = {} for i in Centeroids: Dict[i] = [] for j in range(len(G.nodes())): Dict[i].append(D_Matrix[i,j]) return(Dict)
def compare(self, g_1, g_2, verbose=False): """Compute the kernel value (similarity) between two graphs. Parameters ---------- g1 : networkx.Graph First graph. g2 : networkx.Graph Second graph. alpha : interger < 1 A rule of thumb for setting it is to take the largest power of 10 which is samller than 1/d^2, being d the largest degree in the dataset of graphs. Returns ------- k : The similarity value between g1 and g2. """ #Diagonal superior matrix of the floyd warshall shortest paths # pdb.set_trace() fwm1 = np.array(nx.floyd_warshall_numpy(g_1)) fwm1 = np.where(fwm1==np.inf, 0, fwm1) fwm1 = np.where(fwm1==np.nan, 0, fwm1) fwm1 = np.triu(fwm1, k=1) bc1 = np.bincount(fwm1.reshape(-1).astype(int)) # print bc1 fwm2 = np.array(nx.floyd_warshall_numpy(g_2)) fwm2 = np.where(fwm2==np.inf, 0, fwm2) fwm2 = np.where(fwm2==np.nan, 0, fwm2) fwm2 = np.triu(fwm2, k=1) bc2 = np.bincount(fwm2.reshape(-1).astype(int)) # print bc2 # pdb.set_trace() #Copy into arrays with the same length the non-zero shortests paths v1 = np.zeros(max(len(bc1),len(bc2)) - 1) v1[range(0,len(bc1)-1)] = bc1[1:] v2 = np.zeros(max(len(bc1),len(bc2)) - 1) v2[range(0,len(bc2)-1)] = bc2[1:] return np.sum(v1 * v2)
def test_weighted_numpy(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') XG3=nx.Graph() XG3.add_weighted_edges_from([ [0,1,2],[1,2,12],[2,3,1], [3,4,5],[4,5,1],[5,0,10] ]) dist = nx.floyd_warshall_numpy(XG3) assert_equal(dist[0,3],15)
def Dis_Clus(G): """adding one row to distance matrix The new row shows nodes clusters each node is a cluster at initial""" D_Matrix = nx.floyd_warshall_numpy(G) nodes_label = [] for i in range(len(G.nodes())): nodes_label.append(set(G.nodes()[i])) A = np.vstack([D_Matrix, nodes_label]) return(A)
def test_weight_parameter_numpy(self): try: import numpy except ImportError: raise SkipTest('numpy not available.') XG4 = nx.Graph() XG4.add_edges_from([ (0, 1, {'heavy': 2}), (1, 2, {'heavy': 2}), (2, 3, {'heavy': 1}), (3, 4, {'heavy': 1}), (4, 5, {'heavy': 1}), (5, 6, {'heavy': 1}), (6, 7, {'heavy': 1}), (7, 0, {'heavy': 1}) ]) dist = nx.floyd_warshall_numpy(XG4, weight='heavy') assert_equal(dist[0, 2], 4)
def _get_all_distances(self): if not self._distances is None: return self._distances nodes = self._frame_graph.nodes(data=True) frame_ids, frame_names = zip(*[(i, n['obj'].name) for i, n in nodes]) # calculate all pairwise distances np_distances = nx.floyd_warshall_numpy(self._frame_graph, nodelist=frame_ids) # pack up into a nice DataFrame keyed on frame.name self._distances = pd.DataFrame(np_distances, index=frame_names, columns=frame_names) return self._distances
def get_clustering_results(graph, k): adj_matrix = networkx.adjacency_matrix(graph).toarray() dist_matrix = networkx.floyd_warshall_numpy(graph) # k = KDeterminant().get_best_k(matrix) return { # "AP dist euclidean": AP(dist_matrix, 'euclidean'), # "AP dist precomputed": AP(dist_matrix, 'precomputed'), # "AP adj euclidean": AP(adj_matrix, 'euclidean'), # "AP adj precomputed": AP(adj_matrix, 'precomputed') "Agglomerative": Agglomerative(dist_matrix, k), "K-means": K_means(dist_matrix, k), "Spectral": Spectral(adj_matrix, k) }
def compute_distancematrix(self): """ compute the normalized distance matrix. """ self.distmat = nx.floyd_warshall_numpy(self.graph) if np.isinf(self.distmat).any(): print "Warning: the graph is disconnected." self.maxdist = self.distmat[np.isfinite(self.distmat)].max() # find the max among the finite elements df = np.nan_to_num(self.distmat) # Replace INFINITY entries with some large finite float self.distmat = (df - df.min()) / (self.maxdist - self.distmat.min()) # this is ok for large floats else: self.maxdist = self.distmat.max() self.distmat = (self.distmat - self.distmat.min()) / (self.maxdist - self.distmat.min())
def generateAllPairsDistance(g, max_weight=27): ''' Given connectivity graph g, this will return a numpy matrix that represents the all-pairs shortest path lengths using floyd-warshall algorithm. Since the edge weights of g represent affinities, a copy will be created that changes edges to be distances by subtracting the edge weight from the max_weight. g' has edge weights: W'(i,j) = max_weight - W(i,j) if i<>j, else 0. D = floyd_warshall_all_pairs(g') ''' g2 = generateDistanceGraph(g, max_weight) print "Computing all-pairs shortest path distances..." D = nx.floyd_warshall_numpy(g2) return D
def initialize_rope(xyzs, plotting=False): pdists = ssd.squareform(ssd.pdist(xyzs)) for (i_from, row) in enumerate(pdists): to_inds = np.flatnonzero(row[:i_from] < MAX_DIST) for i_to in to_inds: G.add_edge(i_from, i_to, weight = pdists[i_from, i_to]) A = nx.floyd_warshall_numpy(G) A[np.isinf(A)] = 0 (i_from_long, i_to_long) = np.unravel_index(A.argmax(), A.shape) path = nx.shortest_path(G, source=i_from_long, target=i_to_long) xyz_path = xyz[path,:] xyzs_unif = curves.unif_resample(total_path,N_CTRL_PTS,tol=.002) labels = np.ones(len(xyzs_unif),'int') labels[[1,-1]] = 2 return xyzs_unif, labels
def effective_diameter(graph, q=0.9): if graph.number_of_edges() == 0: return 0 P = nx.floyd_warshall_numpy(graph) P[np.diag_indices(P.shape[0])] = np.inf paths = np.sort(P[P != np.inf]) if paths.shape[0] != 0: ind = np.floor((paths.shape[0]-1)*q) if paths[ind].size == 0: return 0 else: return np.mean(paths[ind]) else: return 0 '''
def calc_geodesic_distances(xyz): """ Calculates pairwise geodesic distances. Note that we generate the graph by projecting to 2D """ x,y = xyz[:,:2].T tri = Triangulation(x,y) G = nx.Graph() #G.add_nodes_from(xrange(len(xyz))) for (i0, i1) in tri.edge_db: dist = np.linalg.norm(xyz[i1] - xyz[i0]) if dist < .03: G.add_edge(i0, i1, weight = np.linalg.norm(xyz[i1] - xyz[i0])) distmat = np.asarray(nx.floyd_warshall_numpy(G)) finitevals = distmat[np.isfinite(distmat)] distmat[~np.isfinite(distmat)] = finitevals.max() * 3 return distmat
def floyd_warshall(self, backend): nodes = self.graph.nodes() if not backend and len(nodes) > 400: raise network_big.NetworkTooBigException(len(nodes)) logging.info("Computing Floyd-Warshall.") infvalue = 127 #sys.maxint F = nx.floyd_warshall_numpy(self.graph, dtype=np.int8, infvalue=infvalue) w = {} for i in range(0, len(nodes)): for j in range(0, len(nodes)): if not F[i,j] == infvalue: w[nodes[i],nodes[j]] = F[i,j] return w
def _kmeans(self): """ Run multiple trials of k-means clustering, and outputt is the best centers, and cluster labels """ D_Matrix = nx.floyd_warshall_numpy(self.G) Old_Centroids = set(self._kmeans_init()) Clusters = self._kmeans_run(self._kmeans_init()) New_Centroids = set(self._update_centers(D_Matrix, Clusters)) while True : if Old_Centroids == New_Centroids: return(New_Centroids) break else: Old_Centroids = New_Centroids New_Centroids = list(New_Centroids) Clusters = self._kmeans_run(New_Centroids) New_Centroids = set(self._update_centers(D_Matrix, Clusters))
def _kmeans(G, n_clusters): """ Run multiple trials of k-means clustering, and outputt is the best centers, and cluster labels """ D_Matrix = nx.floyd_warshall_numpy(G) Old_Centroids = set(_kmeans_init(G, n_clusters, method='balanced')) Clusters = _kmeans_run(G, n_clusters, _kmeans_init(G, n_clusters)) New_Centroids = set(_update_centers(D_Matrix, Clusters, n_clusters)) while True : if Old_Centroids == New_Centroids: return(New_Centroids) #,_kmeans_run(G, n_clusters, New_Centroids)) break else: Old_Centroids = New_Centroids New_Centroids = list(New_Centroids) Clusters = _kmeans_run(G, n_clusters, New_Centroids) New_Centroids = set(_update_centers(D_Matrix, Clusters, n_clusters))
def generateGraphFromFile(): g = nx.Graph() nodes = [] with open("File.txt") as f: for line in f: line = line.replace("\n","") lineArr = line.split(" ") dummy = tuple(lineArr[0:3]) nodes.append(dummy) g.add_weighted_edges_from(nodes) matrixPaths = nx.floyd_warshall_numpy(g,nodelist=None, weight='weight') #print matrixPaths listNodes = g.nodes() #print listNodes center = nx.center(g,e=None) #print center pos=nx.spring_layout(g) return (center, matrixPaths, listNodes,g,pos)
def calculate(self, P): C = self._prop.carbon G = Graph() G.add_nodes_from(a.GetIdx() for a in self.mol.GetAtoms()) for bond in self.mol.GetBonds(): i = bond.GetBeginAtomIdx() j = bond.GetEndAtomIdx() pi = bond.GetBondTypeAsDouble() with self.rethrow_zerodiv(): w = (C * C) / (P[i] * P[j] * pi) G.add_edge(i, j, weight=w) sp = floyd_warshall_numpy(G) np.fill_diagonal(sp, [1. - C / P[a.GetIdx()] for a in self.mol.GetAtoms()]) return sp
def longest_shortest_path(G): A = nx.floyd_warshall_numpy(G) A[np.isinf(A)] = 0 (i_from_long, i_to_long) = np.unravel_index(A.argmax(), A.shape) path = nx.shortest_path(G, source=i_from_long, target=i_to_long) return path
def test_directed_cycle_numpy(self): G = nx.DiGraph() G.add_cycle([0,1,2,3]) pred,dist = nx.floyd_warshall_predecessor_and_distance(G) D = nx.utils.dict_to_numpy_array(dist) assert_equal(nx.floyd_warshall_numpy(G),D)
# make graph of reasonably nearby points # make a spanning tree from jds_image_proc import pcd_io import scipy.spatial.distance as ssd, numpy as np import networkx as nx MAX_DIST = .03 (xyz,) , rgb = pcd_io.load_xyzrgb("/tmp/comm/rope_pts/data000000000093.pcd") pdists = ssd.squareform(ssd.pdist(xyz)) G = nx.Graph() for (i_from, row) in enumerate(pdists): to_inds = np.flatnonzero(row[:i_from] < MAX_DIST) for i_to in to_inds: G.add_edge(i_from, i_to, weight = pdists[i_from, i_to]) A = nx.floyd_warshall_numpy(G) A[np.isinf(A)] = 0 (i_from_long, i_to_long) = np.unravel_index(A.argmax(), A.shape) path = nx.shortest_path(G, source=i_from_long, target=i_to_long) xyz_path = xyz[path,:] import enthought.mayavi.mlab as mlab mlab.clf() mlab.plot3d(*xyz_path.T,tube_radius=.02) mlab.points3d(*xyz.T, scale_factor=.025, color=(1,0,0))