Python networkx.from_dict_of_lists() Examples
The following are 10
code examples of networkx.from_dict_of_lists().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: graph.py From vk_friends with GNU General Public License v3.0 | 5 votes |
def __init__(self, s): if not glob(s['graph']): #if not glob(s[file]) self.dct = save_or_load(s['file'], False) #print(self.calc(self.dct)) self.graph = nx.from_dict_of_lists(self.dct) save_or_load(s['graph'], True, self.graph) else: self.graph = save_or_load(s['graph'], False)
Example #2
Source File: node_utils.py From graph_adversarial_attack with MIT License | 5 votes |
def load_binary_data(data_folder, dataset_str): """Load data.""" names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph'] objects = [] for i in range(len(names)): with open("{}/ind.{}.{}".format(data_folder, dataset_str, names[i]), 'rb') as f: if sys.version_info > (3, 0): objects.append(pkl.load(f, encoding='latin1')) else: objects.append(pkl.load(f)) x, y, tx, ty, allx, ally, graph = tuple(objects) test_idx_reorder = parse_index_file("{}/ind.{}.test.index".format(data_folder, dataset_str)) test_idx_range = np.sort(test_idx_reorder) if dataset_str == 'citeseer': # Fix citeseer dataset (there are some isolated nodes in the graph) # Find isolated nodes, add them as zero-vecs into the right position test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1) tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1])) tx_extended[test_idx_range-min(test_idx_range), :] = tx tx = tx_extended ty_extended = np.zeros((len(test_idx_range_full), y.shape[1])) ty_extended[test_idx_range-min(test_idx_range), :] = ty ty = ty_extended features = sp.vstack((allx, tx)).tolil() features[test_idx_reorder, :] = features[test_idx_range, :] StaticGraph.graph = nx.from_dict_of_lists(graph) labels = np.vstack((ally, ty)) labels[test_idx_reorder, :] = labels[test_idx_range, :] idx_test = test_idx_range.tolist() idx_train = range(len(y)) idx_val = range(len(y), len(y)+500) cmd_args.feature_dim = features.shape[1] cmd_args.num_class = labels.shape[1] return preprocess_features(features), labels, idx_train, idx_val, idx_test
Example #3
Source File: node_utils.py From graph_adversarial_attack with MIT License | 5 votes |
def load_txt_data(data_folder, dataset_str): idx_train = list(np.loadtxt(data_folder + '/train_idx.txt', dtype=int)) idx_val = list(np.loadtxt(data_folder + '/val_idx.txt', dtype=int)) idx_test = list(np.loadtxt(data_folder + '/test_idx.txt', dtype=int)) labels = np.loadtxt(data_folder + '/label.txt') with open(data_folder + '/meta.txt', 'r') as f: num_nodes, cmd_args.num_class, cmd_args.feature_dim = [int(w) for w in f.readline().strip().split()] graph = load_raw_graph(data_folder, dataset_str) assert len(graph) == num_nodes StaticGraph.graph = nx.from_dict_of_lists(graph) row_ptr = [] col_idx = [] vals = [] with open(data_folder + '/features.txt', 'r') as f: nnz = 0 for row in f: row = row.strip().split() row_ptr.append(nnz) for i in range(1, len(row)): w = row[i].split(':') col_idx.append(int(w[0])) vals.append(float(w[1])) nnz += int(row[0]) row_ptr.append(nnz) assert len(col_idx) == len(vals) and len(vals) == nnz and len(row_ptr) == num_nodes + 1 features = sp.csr_matrix((vals, col_idx, row_ptr), shape=(num_nodes, cmd_args.feature_dim)) return preprocess_features(features), labels, idx_train, idx_val, idx_test
Example #4
Source File: cluster_corr.py From altanalyze with Apache License 2.0 | 5 votes |
def partition_h5_file(h5_filename, gene_list=None, num_neighbors=10, num_trees=100, louvain_level=-1,genome=None): """ Reads a CellRanger h5 file and partitions it by clustering on the k-nearest neighbor graph Keyword arguments: gene_list - restricts the analysis to the specified list of gene symbols. Default is to not restrict num_neighbors - the number of nearest neighbors to compute for each cell num_trees - the number of trees used in the random forest that approximates the nearest neighbor calculation louvain_level - the level of the Louvain clustering dendrogram to cut at. Level 0 is the lowest (most granular) level, and higher levels get less granular. The highest level is considered the "best" set of clusters, but the number of levels is not known a priori. Hence, negative values will count down from the highest level, so -1 will always be the "best" clustering, regardless of the actual number of levels in the dendrogram Return Result: A dictionary, where the keys are partition ids and the values are the CellCollection for that partition """ if '.h5' in h5_filename: collection = CellCollection.from_cellranger_h5(h5_filename) data_type = 'h5' elif 'txt' in h5_filename: try: collection = CellCollection.from_tsvfile_alt(h5_filename,genome,gene_list=gene_list) except: collection = CellCollection.from_tsvfile(h5_filename,genome) data_type = 'txt' else: collection = CellCollection.from_cellranger_mtx(h5_filename,genome) data_type = 'mtx' if gene_list != None: collection.filter_genes_by_symbol(gene_list,data_type) neighbor_dict = nearest_neighbors(collection, num_neighbors=num_neighbors, n_trees=num_trees) cluster_definition = identify_clusters(networkx.from_dict_of_lists(neighbor_dict), louvain_level=louvain_level) return collection.partition(cluster_definition)
Example #5
Source File: convert.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def from_dict_of_lists(d,create_using=None): """Return a graph from a dictionary of lists. Parameters ---------- d : dictionary of lists A dictionary of lists adjacency representation. create_using : NetworkX graph Use specified graph for result. Otherwise a new graph is created. Examples -------- >>> dol= {0:[1]} # single edge (0,1) >>> G=nx.from_dict_of_lists(dol) or >>> G=nx.Graph(dol) # use Graph constructor """ G=_prep_create_using(create_using) G.add_nodes_from(d) if G.is_multigraph() and not G.is_directed(): # a dict_of_lists can't show multiedges. BUT for undirected graphs, # each edge shows up twice in the dict_of_lists. # So we need to treat this case separately. seen={} for node,nbrlist in d.items(): for nbr in nbrlist: if nbr not in seen: G.add_edge(node,nbr) seen[node]=1 # don't allow reverse edge to show up else: G.add_edges_from( ((node,nbr) for node,nbrlist in d.items() for nbr in nbrlist) ) return G
Example #6
Source File: convert.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def from_dict_of_lists(d, create_using=None): """Returns a graph from a dictionary of lists. Parameters ---------- d : dictionary of lists A dictionary of lists adjacency representation. create_using : NetworkX graph constructor, optional (default=nx.Graph) Graph type to create. If graph instance, then cleared before populated. Examples -------- >>> dol = {0: [1]} # single edge (0,1) >>> G = nx.from_dict_of_lists(dol) or >>> G = nx.Graph(dol) # use Graph constructor """ G = nx.empty_graph(0, create_using) G.add_nodes_from(d) if G.is_multigraph() and not G.is_directed(): # a dict_of_lists can't show multiedges. BUT for undirected graphs, # each edge shows up twice in the dict_of_lists. # So we need to treat this case separately. seen = {} for node, nbrlist in d.items(): for nbr in nbrlist: if nbr not in seen: G.add_edge(node, nbr) seen[node] = 1 # don't allow reverse edge to show up else: G.add_edges_from(((node, nbr) for node, nbrlist in d.items() for nbr in nbrlist)) return G
Example #7
Source File: convert.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def from_dict_of_lists(d, create_using=None): """Return a graph from a dictionary of lists. Parameters ---------- d : dictionary of lists A dictionary of lists adjacency representation. create_using : NetworkX graph Use specified graph for result. Otherwise a new graph is created. Examples -------- >>> dol = {0: [1]} # single edge (0,1) >>> G = nx.from_dict_of_lists(dol) or >>> G = nx.Graph(dol) # use Graph constructor """ G = _prep_create_using(create_using) G.add_nodes_from(d) if G.is_multigraph() and not G.is_directed(): # a dict_of_lists can't show multiedges. BUT for undirected graphs, # each edge shows up twice in the dict_of_lists. # So we need to treat this case separately. seen = {} for node, nbrlist in d.items(): for nbr in nbrlist: if nbr not in seen: G.add_edge(node, nbr) seen[node] = 1 # don't allow reverse edge to show up else: G.add_edges_from(((node, nbr) for node, nbrlist in d.items() for nbr in nbrlist)) return G
Example #8
Source File: data.py From graph-generation with MIT License | 4 votes |
def Graph_load(dataset = 'cora'): ''' Load a single graph dataset :param dataset: dataset name :return: ''' names = ['x', 'tx', 'allx', 'graph'] objects = [] for i in range(len(names)): load = pkl.load(open("dataset/ind.{}.{}".format(dataset, names[i]), 'rb'), encoding='latin1') # print('loaded') objects.append(load) # print(load) x, tx, allx, graph = tuple(objects) test_idx_reorder = parse_index_file("dataset/ind.{}.test.index".format(dataset)) test_idx_range = np.sort(test_idx_reorder) if dataset == 'citeseer': # Fix citeseer dataset (there are some isolated nodes in the graph) # Find isolated nodes, add them as zero-vecs into the right position test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder) + 1) tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1])) tx_extended[test_idx_range - min(test_idx_range), :] = tx tx = tx_extended features = sp.vstack((allx, tx)).tolil() features[test_idx_reorder, :] = features[test_idx_range, :] G = nx.from_dict_of_lists(graph) adj = nx.adjacency_matrix(G) return adj, features, G ######### code test ######## # adj, features,G = Graph_load() # print(adj) # print(G.number_of_nodes(), G.number_of_edges()) # _,_,G = Graph_load(dataset='citeseer') # G = max(nx.connected_component_subgraphs(G), key=len) # G = nx.convert_node_labels_to_integers(G) # # count = 0 # max_node = 0 # for i in range(G.number_of_nodes()): # G_ego = nx.ego_graph(G, i, radius=3) # # draw_graph(G_ego,prefix='test'+str(i)) # m = G_ego.number_of_nodes() # if m>max_node: # max_node = m # if m>=50: # print(i, G_ego.number_of_nodes(), G_ego.number_of_edges()) # count += 1 # print('count', count) # print('max_node', max_node)
Example #9
Source File: data.py From GraphRNN with MIT License | 4 votes |
def Graph_load(dataset = 'cora'): ''' Load a single graph dataset :param dataset: dataset name :return: ''' names = ['x', 'tx', 'allx', 'graph'] objects = [] for i in range(len(names)): load = pkl.load(open("dataset/ind.{}.{}".format(dataset, names[i]), 'rb'), encoding='latin1') # print('loaded') objects.append(load) # print(load) x, tx, allx, graph = tuple(objects) test_idx_reorder = parse_index_file("dataset/ind.{}.test.index".format(dataset)) test_idx_range = np.sort(test_idx_reorder) if dataset == 'citeseer': # Fix citeseer dataset (there are some isolated nodes in the graph) # Find isolated nodes, add them as zero-vecs into the right position test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder) + 1) tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1])) tx_extended[test_idx_range - min(test_idx_range), :] = tx tx = tx_extended features = sp.vstack((allx, tx)).tolil() features[test_idx_reorder, :] = features[test_idx_range, :] G = nx.from_dict_of_lists(graph) adj = nx.adjacency_matrix(G) return adj, features, G ######### code test ######## # adj, features,G = Graph_load() # print(adj) # print(G.number_of_nodes(), G.number_of_edges()) # _,_,G = Graph_load(dataset='citeseer') # G = max(nx.connected_component_subgraphs(G), key=len) # G = nx.convert_node_labels_to_integers(G) # # count = 0 # max_node = 0 # for i in range(G.number_of_nodes()): # G_ego = nx.ego_graph(G, i, radius=3) # # draw_graph(G_ego,prefix='test'+str(i)) # m = G_ego.number_of_nodes() # if m>max_node: # max_node = m # if m>=50: # print(i, G_ego.number_of_nodes(), G_ego.number_of_edges()) # count += 1 # print('count', count) # print('max_node', max_node)
Example #10
Source File: adjacency.py From parcellation_fragmenter with BSD 3-Clause "New" or "Revised" License | 4 votes |
def filtration(self, filter_indices=None, toArray=False, remap=False): """ Generate a local adjacency list, constrained to a subset of vertices on the surface. For each vertex in 'vertices', retain neighbors only if they also exist in 'vertices'. Parameters: - - - - - fitler_indices : array indices to include in sub-graph. If none, returns original graph. to_array : bool return adjacency matrix of filter_indices remap : bool remap indices to 0-len(filter_indices) Returns: - - - - G : array / dictionary down-sampled adjacency list / matrix """ assert hasattr(self, 'adj') if not np.any(filter_indices): G = self.adj.copy() else: filter_indices = np.sort(filter_indices) G = {}.fromkeys(filter_indices) for v in filter_indices: G[v] = list(set(self.adj[v]).intersection(set(filter_indices))) ind2sort = dict(zip( filter_indices, np.arange(len(filter_indices)))) if remap: remapped = { ind2sort[fi]: [ind2sort[nb] for nb in G[fi]] for fi in filter_indices} G = remapped if toArray: G = nx.from_dict_of_lists(G) nodes = G.nodes() nodes = np.argsort(nodes) G = nx.to_numpy_array(G) G = G[nodes, :][:, nodes] return G