我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用networkx.set_node_attributes()。
def calculate_katz_centrality(graph): """ Compute the katz centrality for nodes. """ # if not graph.is_directed(): # raise nx.NetworkXError( \ # "katz_centrality() not defined for undirected graphs.") print "\n\tCalculating Katz Centrality..." print "\tWarning: This might take a long time larger pedigrees." g = graph A = nx.adjacency_matrix(g) from scipy import linalg as LA max_eign = float(np.real(max(LA.eigvals(A.todense())))) print "\t-Max.Eigenvalue(A) ", round(max_eign, 3) kt = nx.katz_centrality(g, tol=1.0e-4, alpha=1/max_eign-0.01, beta=1.0, max_iter=999999) nx.set_node_attributes(g, 'katz', kt) katz_sorted = sorted(kt.items(), key=itemgetter(1), reverse=True) for key, value in katz_sorted[0:10]: print "\t > ", key, round(value, 4) return g, kt
def find_partition(graph): # code and lib from http://perso.crans.org/aynaud/communities/ # must be an undirected graph g = graph partition = community.best_partition(g) print "Partitions found: ", len(set(partition.values())) # to show members of each partition: for i in set(partition.values()): members = [nodes for nodes in partition.keys() if partition[nodes] == i] print i, len(members) # if i==0: # # write out the subgraph # community_graph = graph.subgraph(members) # #draw_graph(community_graph) # #nx.write_edgelist(community_graph, "community.edgelist", data=False) # #for member in members: # # print member, i # print "Partition for node johncoogan: ", partition[node_id] nx.set_node_attributes(g, 'partition', partition) return g, partition
def nbest_centrality(G, metric, n=10, attr="centrality", **kwargs): # Compute the centrality scores for each vertex scores = metric(G, **kwargs) # Set the score as a property on each node nx.set_node_attributes(G, attr, scores) # Filter scores (do not include in book) ntypes = nx.get_node_attributes(G, 'type') phrases = [ item for item in scores.items() if ntypes.get(item[0], None) == "keyphrase" ] # Find the top n scores and print them along with their index topn = heapq.nlargest(n, phrases, key=itemgetter(1)) for idx, item in enumerate(topn): print("{}. {}: {:0.4f}".format(idx+1, *item)) return G
def __configure_response_function(self): """ Configure the response function """ if self.symbolic_: return rf = self.params["response_function"] F = {} for rf_case in rf: for node in rf_case["nodes"]: F[str(node)] = RF(rf_case) nx.set_node_attributes(self.G,name="rf", values=F) return ########################################################## # HELPER ##########################################################
def search(self, selected_affils, conf_name, year, exclude_papers=[], rtype="affil", force=False): """ Checks if the graph model already exists, otherwise creates one and runs the ranking on the nodes. """ graph = build_graph(conf_name, year, self.params['H'], self.params['min_topic_lift'], self.params['min_ngram_lift'], exclude_papers, force, load=True, save=self.save) # Store number of nodes for checking later self.nnodes = graph.number_of_nodes() # Rank nodes using subgraph scores = rank_nodes(graph, return_type=rtype, **self.params) # Adds the score to the nodes and writes to disk. A stupid cast # is required because write_gexf can't handle np.float64 scores = {nid: float(score) for nid, score in scores.items()} nx.set_node_attributes(graph, "score", scores) # nx.write_gexf(graph, utils.get_graph_file_name(model_folder, query)) # Returns the top values of the type of node of interest results = get_top_nodes(graph, scores.items(), limit=selected_affils, return_type=rtype) # Add to class object for future access self.graph = graph return results
def search(self, query, exclude=[], limit=20, rtype="paper", force=False): """ Checks if the graph model already exists, otherwise creates one and runs the ranking on the nodes. """ graph = build_graph(query, self.params['K'], self.params['H'], self.params['min_topic_lift'], self.params['min_ngram_lift'], exclude, force, load=True, save=self.save) # Store number of nodes for checking later self.nnodes = graph.number_of_nodes() # Rank nodes using subgraph scores = ranker.rank_nodes(graph, limit=limit, return_type=rtype, **self.params) # Adds the score to the nodes and writes to disk. A stupid cast # is required because write_gexf can't handle np.float64 scores = {nid: float(score) for nid, score in scores.items()} nx.set_node_attributes(graph, "score", scores) # nx.write_gexf(graph, utils.get_graph_file_name(model_folder, query)) # Returns the top values of the type of node of interest results = get_top_nodes(graph, scores.items(), limit=limit, return_type=rtype) # Add to class object for future access self.graph = graph return [str(pub_id) for _nid, pub_id, _score in results]
def build_function_graph(self, analysis_unit_dict): ''' BUILDS DIRECTED FUNCTION GRAPH input: a dictionary of functions from this dump file output: none. Side effect creates a graph linked to this object ''' if self.debug_verbose: print inspect.stack()[0][3] # BUILD CALL GRAPH self.function_graph = nx.DiGraph() G = self.function_graph for k, function_dict in analysis_unit_dict.iteritems(): if function_dict['function']: # MIGHT BE NONE WHEN FUNCTION IS CLASS CONSTRUCTOR (?) node = function_dict['function'].Id # Id of the Function #if not G.has_node(node): G.add_node(node) #, function_dict_key=k}) # FUNCTION CPP OBJECT IS NODE #else: all_attr = nx.get_node_attributes(G, 'function_id') all_attr[node] = k nx.set_node_attributes(G, 'function_id', all_attr) #function_dict['is_visited'] = False self.add_edges_to_function_graph(function_dict, G, node) self.function_graph = G
def set_node_attributes(self,side,name,values): ''' Set node of type side attributes from dictionary of nodes and values. Only sets one attribute at a time. Parameters ---------- name : string Attribute name side : int or str Tags for each side of the bipartite network. values: dict Dictionary of attribute values keyed by node. Nodes that do not belong to side will not be considered. ''' self._check_side(side) ns = set(values.keys()).intersection(set(self.nodes(side))) vals = {val:values[val] for val in ns} set_node_attributes(self,name,vals)
def refine_to_chain(g, from_attr, to_attr): '''can be used to refine basic blocks into blocks - the dual of contract_chains() assume g.node[n][attr] is a list returns a graph whose nodes are the refinement of the lists into paths the elements of the lists are held as to_attr the nodes become tuples (node_index, list_index)''' paths = [] for n in g.nodes_iter(): block = g.node[n][from_attr] size = len(block) path = nx.path_graph(size, create_using=nx.DiGraph()) nx.relabel_nodes(path, mapping={x:(n, x) for x in path.nodes()}, copy=False) path.add_edges_from(((n, size - 1), (s, 0)) for s in g.successors_iter(n)) paths.append(path) values = {(n, x): block for n in g.nodes_iter() for x, block in enumerate(g.node[n][from_attr])} res = nx.compose_all(paths) nx.set_node_attributes(res, to_attr, values) return res
def dataflow(g:'graph', start:'node', start_value, Analysis=ConsProp): import networkx as nx import graph_utils as gu gu.node_data_map_inplace(g, f=lambda n, d: Analysis.single_block_update, attr='transfer_function') nx.set_node_attributes(g, 'outb', {v: Analysis.initial() for v in g.nodes()}) nx.set_node_attributes(g, 'inb', {v: Analysis.initial() for v in g.nodes()}) g.node[start]['inb'] = Analysis.initial() wl = set(g.nodes()) while wl: u = wl.pop() udata = g.node[u] inb = udata['inb'] Analysis.join(inb, [g.node[x]['outb'] for x in g.predecessors(u)]) outb = udata['transfer_function'](udata[BLOCKNAME], inb) if outb != udata['outb']: udata['outb'] = outb wl.update(g.successors(u))
def calculate_degree(graph): print "\n\tCalculating node degree...\n" g = graph deg = nx.degree(g) nx.set_node_attributes(g, 'degree', deg) return g, deg
def calculate_odegree(graph): # will only work on DiGraph (directed graph) print "\n\tCalculating Outdegree..." g = graph odeg = g.out_degree() nx.set_node_attributes(g, 'odegree', odeg) outdeg_sorted = sorted(odeg.items(), key=itemgetter(1), reverse=True) for key, value in outdeg_sorted[0:10]: print "\t > ", key, value return g, odeg
def calculate_indegree(graph): # will only work on DiGraph (directed graph) print "\tCalculating Indegree..." g = graph indeg = g.in_degree() nx.set_node_attributes(g, 'indegree', indeg) indeg_sorted = sorted(indeg.items(), key=itemgetter(1), reverse=True) for key, value in indeg_sorted[0:10]: print "\t > ", key, value return g, indeg
def calculate_outdegree(graph): # will only work on DiGraph (directed graph) print "\n\tCalculating Outdegree Centrality..." g = graph outdeg = out_degree_centrality(g) nx.set_node_attributes(g, 'outdegree', outdeg) outdeg_sorted = sorted(outdeg.items(), key=itemgetter(1), reverse=True) for key, value in outdeg_sorted[0:10]: print "\t > ", key, round(value, 4) return g, outdeg
def calculate_closeness(graph): print "\n\tCalculating Closeness Centrality..." g = graph clo = nx.closeness_centrality(g) nx.set_node_attributes(g, 'closeness', clo) degclos_sorted = sorted(clo.items(), key=itemgetter(1), reverse=True) for key, value in degclos_sorted[0:10]: print "\t > ", key, round(value, 4) return g, clo
def calculate_eigenvector_centrality(graph): print "\n\tCalculating Eigenvector Centrality..." g = graph ec = nx.eigenvector_centrality_numpy(g) nx.set_node_attributes(g, 'eigenvector', ec) degeign_sorted = sorted(ec.items(), key=itemgetter(1), reverse=True) for key, value in degeign_sorted[0:10]: print "\t > ", key, round(value, 4) return g, ec
def calculate_degree_centrality(graph): print "\nCalculating Degree Centrality..." g = graph dc = nx.degree_centrality(g) nx.set_node_attributes(g, 'degree_cent', dc) degcent_sorted = sorted(dc.items(), key=itemgetter(1), reverse=True) for key, value in degcent_sorted[0:10]: print " > ", key, round(value, 4) return graph, dc
def voronoi_partition(G, outline): """ For 2D-embedded graph `G`, within the boundary given by the shapely polygon `outline`, returns `G` with the Voronoi cell region as an additional node attribute. """ #following line from vresutils.graph caused a bug #G = polygon_subgraph(G, outline, copy=False) points = list(vresutils.graph.get_node_attributes(G, 'pos').values()) regions = vresutils.graph.voronoi_partition_pts(points, outline, no_multipolygons=True) nx.set_node_attributes(G, 'region', dict(zip(G.nodes(), regions))) return G
def gdfs_to_graph(gdf_nodes, gdf_edges): """ Convert node and edge GeoDataFrames into a graph Parameters ---------- gdf_nodes : GeoDataFrame gdf_edges : GeoDataFrame Returns ------- networkx multidigraph """ G = nx.MultiDiGraph() G.graph['crs'] = gdf_nodes.crs G.graph['name'] = gdf_nodes.gdf_name.rstrip('_nodes') # add the nodes and their attributes to the graph G.add_nodes_from(gdf_nodes.index) attributes = gdf_nodes.to_dict() for attribute_name in gdf_nodes.columns: # only add this attribute to nodes which have a non-null value for it attribute_values = {k:v for k, v in attributes[attribute_name].items() if pd.notnull(v)} nx.set_node_attributes(G, name=attribute_name, values=attribute_values) # add the edges and attributes that are not u, v, key (as they're added # separately) or null for _, row in gdf_edges.iterrows(): attrs = {} for label, value in row.iteritems(): if (label not in ['u', 'v', 'key']) and (isinstance(value, list) or pd.notnull(value)): attrs[label] = value G.add_edge(u=row['u'], v=row['v'], key=row['key'], **attrs) return G
def _project_AA(self,side): """ Builds the projection of the bipartite network on to the chosen side. The projection is done using the ADAMIC-ADAR index. Parameters ---------- side : int or str Tags for each side of the bipartite network. """ self._check_side(side) aside = self.side if side == self.aside else self.aside net = self.edges(as_df=True)[[side,aside]] AA = merge(net,net,how='inner',left_on=aside,right_on=aside) nodes = self.nodes(side,as_df=True)[[side]].reset_index().rename(columns={'index':side+'_index'}) AA = merge(AA,nodes.rename(columns={side:side+'_x',side+'_index':side+'_index_x'}),how='left',right_on=side+'_x',left_on=side+'_x') AA = merge(AA,nodes.rename(columns={side:side+'_y',side+'_index':side+'_index_y'}),how='left',right_on=side+'_y',left_on=side+'_y') AA = AA[AA[side+'_index_x']>AA[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1) AA = merge(AA,self.degree(aside,as_df=True)) AA['AA'] = 1./log(AA['degree']) AA = AA[[side+'_x',side+'_y','AA']].groupby([side+'_x',side+'_y']).sum().reset_index() self.P[side] = gGraph(node_id=side) self.P[side].add_weighted_edges_from([val[1:] for val in AA.itertuples()]) nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left') properties = nodes.columns.values.tolist() properties.remove(side) for prop in properties: values = dict(zip(nodes[side].values,nodes[prop].values)) set_node_attributes(self.P[side],prop,values)
def _project_NK(self,side): """ Builds the projection of the bipartite network on to the chosen side. The projection is done using the NK conditional probability. Parameters ---------- side : int or str Tags for each side of the bipartite network. """ self._check_side(side) aside = self.side if side == self.aside else self.aside E = self.recombination_ease(aside) net = self.edges(as_df=True)[[side,aside]] nodes = merge(E,net).groupby(side).sum()[['E']].reset_index().reset_index().rename(columns={'index':side+'_index'}) dis = merge(E,merge(net,net,how='inner',left_on=aside,right_on=aside)) dis = dis.groupby([side+'_x',side+'_y']).sum()[['E']].reset_index().rename(columns={'E':'E_both'}) dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_x').drop(side,1) dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_y').drop(side,1) dis = dis[dis[side+'_index_x']>dis[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1) dis['p_x'] = dis['E_both']/dis['E_x'].astype(float) dis['p_y'] = dis['E_both']/dis['E_y'].astype(float) dis['fi'] = dis[['p_x','p_y']].min(1) dis = dis[[side+'_x',side+'_y','fi']] self.P[side] = gGraph(node_id=side) self.P[side].add_weighted_edges_from([val[1:] for val in dis.itertuples()]) nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left') properties = nodes.columns.values.tolist() properties.remove(side) for prop in properties: values = dict(zip(nodes[side].values,nodes[prop].values)) set_node_attributes(self.P[side],prop,values)
def _get_projection_pos(self,side,P,C=None): '''This function requires graph_tool''' pos = get_pos(P[[side+'_x',side+'_y']],node_id=side,comms=True,progress=False,C=C) X = {} Y = {} Cc = {} for i,x,y,c in pos.values: X[i]=x Y[i]=y Cc[i]=c self.set_node_attributes(side,'x',X) self.set_node_attributes(side,'y',Y) self.set_node_attributes(side,'c',Cc) return pos
def recombination_ease(self,side): nodes = self.nodes(side,as_df=True) if 'E' not in nodes.columns: aside = self.side if side == self.aside else self.aside net = self.edges(as_df=True)[[side,aside]] dis = merge(net,net,how='inner',left_on=aside,right_on=aside)[[side+'_x',side+'_y']].drop_duplicates() dis = dis[dis[side+'_x']!=dis[side+'_y']] E = merge(dis.groupby(side+'_x').count().reset_index().rename(columns={side+'_y':'n_c'}).rename(columns={side+'_x':side}),net.groupby(side).count().reset_index().rename(columns={aside:'n_p'}),how='outer').fillna(0) E['E'] = E['n_c']/E['n_p'] E = E[[side,'E']] self.set_node_attributes(side,'E',dict(E.values)) return E else: return nodes[[side,'E']]
def _prepare_overlaps(exons): """Compute splicegraph prior the computation of overlaps""" splice_graph = nx.DiGraph() exon_df = exons_to_df(exons) exon2coord = exon_to_coordinates(exons) splice_graph.add_nodes_from(exon2coord.keys()) nx.set_node_attributes( G=splice_graph, name='coordinates', values = exon2coord ) transcript2path = transcript_to_path(exon_df) for path in transcript2path.values(): splice_graph.add_path(path) return splice_graph
def calculate_centrality(G): degc = nx.degree_centrality(G) nx.set_node_attributes(G,'degree_cent', degc) degc_sorted = sorted(degc.items(), key=valuegetter(1), reverse=True) for key, value in degc_sorted[0:10]: print "Degree Centrailty:", key, value return G, degc
def add_junction(self, name, base_demand=0.0, demand_pattern=None, elevation=0.0, coordinates=None): """ Adds a junction to the water network model. Parameters ------------------- name : string Name of the junction. base_demand : float Base demand at the junction. demand_pattern : string or Pattern Name of the demand pattern or the actual Pattern object elevation : float Elevation of the junction. coordinates : tuple of floats X-Y coordinates of the node location. """ base_demand = float(base_demand) elevation = float(elevation) if not isinstance(demand_pattern, Pattern): demand_pattern = self.get_pattern(demand_pattern) junction = Junction(name, base_demand, demand_pattern, elevation) self._nodes[name] = junction self._junctions[name] = junction self._graph.add_node(name) if coordinates is not None: self.set_node_coordinates(name, coordinates) nx.set_node_attributes(self._graph, name='type', values={name:'junction'}) self._num_junctions += 1
def add_reservoir(self, name, base_head=0.0, head_pattern=None, coordinates=None): """ Adds a reservoir to the water network model. Parameters ---------- name : string Name of the reservoir. base_head : float, optional Base head at the reservoir. head_pattern : string or Pattern Name of the head pattern or the actual Pattern object coordinates : tuple of floats, optional X-Y coordinates of the node location. """ base_head = float(base_head) if head_pattern and isinstance(head_pattern, six.string_types): head_pattern = self.get_pattern(head_pattern) reservoir = Reservoir(name, base_head, head_pattern) self._nodes[name] = reservoir self._reservoirs[name] = reservoir self._graph.add_node(name) if coordinates is not None: self.set_node_coordinates(name, coordinates) nx.set_node_attributes(self._graph, name='type', values={name:'reservoir'}) self._num_reservoirs += 1
def set_node_coordinates(self, name, coordinates): """ Sets the node coordinates in the networkx graph. Parameters ---------- name : string Name of the node. coordinates : tuple X-Y coordinates. """ nx.set_node_attributes(self._graph, name='pos', values={name: coordinates})
def update_stackdepth(cfg): '''The stack depth is supposed to be independent of path. So dijkstra on the undirected graph suffices (and maybe too strong. we don't need minimality) The `undirected` part is just because we want to work with unreachable code too. ''' bidi = gu.copy_to_bidirectional(cfg, weight='stack_effect') depths = nx.single_source_dijkstra_path_length(bidi, source=0, weight='stack_effect') nx.set_node_attributes(cfg, 'stack_depth', depths) return cfg
def make_bcode_block_cfg(instructions): dbs = {b.offset: b for b in instructions} cfg = nx.DiGraph([(b.offset, dbs[j].offset, {'stack_effect': stack_effect}) for b in dbs.values() for (j, stack_effect) in b.next_list() if dbs.get(j)]) nx.set_node_attributes(cfg, name='BCode', values=dbs) update_stackdepth(cfg) # each node will hold a block of dictionaries - bcode and stack_depth basic_block_cfg = gu.contract_chains(cfg, blockname=BLOCKNAME) return basic_block_cfg
def run_analysis(cfg): import graph_utils as gu import networkx as nx Analysis = ConsProp def compute_transfer_function(): pass gu.node_data_map_inplace(cfg, attr='transfer', f=lambda n, d: compute_transfer_function(d)) nx.set_node_attributes(cfg, 'out', {n:Analysis() for n in cfg.nodes_iter()}) dataflow(cfg, 0, {}, Analysis)
def _project_CP(self,side): """ Builds the projection of the bipartite network on to the chosen side. The projection is done using conditional probability. Parameters ---------- side : int or str Tags for each side of the bipartite network. """ self._check_side(side) aside = self.side if side == self.aside else self.aside net = self.edges(as_df=True)[[side,aside]] dis = merge(net,net,how='inner',left_on=aside,right_on=aside).groupby([side+'_x',side+'_y']).count().reset_index().rename(columns={aside:'n_both'}) nodes = merge(self.nodes(side,as_df=True)[[side]].reset_index().rename(columns={'index':side+'_index'}),DataFrame(self.degree(side).items(),columns=[side,'n'])) dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_x').drop(side,1) dis = merge(dis,nodes,how='left',right_on=side,left_on=side+'_y').drop(side,1) dis = dis[dis[side+'_index_x']>dis[side+'_index_y']].drop([side+'_index_x',side+'_index_y'],1) dis['p_x'] = dis['n_both']/dis['n_x'].astype(float) dis['p_y'] = dis['n_both']/dis['n_y'].astype(float) dis['fi'] = dis[['p_x','p_y']].min(1) dis = dis[[side+'_x',side+'_y','fi']] self.P[side] = gGraph(node_id=side) self.P[side].add_weighted_edges_from([val[1:] for val in dis.itertuples()]) nodes = merge(self.P[side].nodes(as_df=True),self.nodes(side,as_df=True),how='left') properties = nodes.columns.values.tolist() properties.remove(side) for prop in properties: values = dict(zip(nodes[side].values,nodes[prop].values)) set_node_attributes(self.P[side],prop,values)
def build_splicegraph(exon_index): """Build the splicegraph from a dict of SeqRecords Splicegraph is a directed graph, whose nodes - are exon_ids, - attributes are - coordinates [(transcript1, start, end), ..., (transcriptN, start, end)] - sequence in str format and whose edges - are connected exons in any way - attributes are the overlap between them: - positive means there is an overlap of that number of bases - zero means no overlap - negative means a gap of that number of bases """ # Initialize grpah splice_graph = nx.DiGraph() # Add nodes splice_graph.add_nodes_from(exon_index.keys()) nx.set_node_attributes( G=splice_graph, name='coordinates', values= exon_to_coordinates(exon_index) ) nx.set_node_attributes( G=splice_graph, name='sequence', values={exon.id : str(exon.seq) for exon in exon_index.values()} ) # Edges transcript2path = transcript_to_path(exons_to_df(exon_index)) for path in transcript2path.values(): splice_graph.add_path(path) nx.set_edge_attributes( G=splice_graph, name='overlap', values = compute_edge_overlaps(splice_graph) ) return splice_graph
def reduce_graph_rings(self): ''' :return: ''' cycle_name_format = "R_{:}" index = 0 cycle = self.get_cycle() while cycle: cycle_name = cycle_name_format.format(index) self.graph.add_node(cycle_name) # ebunch = zip(cycle, (cycle[1:] + cycle[:1])) self.graph.remove_edges_from(cycle) for node1, node2 in cycle: if isinstance(node1, six.string_types): self.graph.add_edge(node1, cycle_name, attr_dict={"bond_features": Molecule.bond_features_between_contract_rings()}) continue neighbours = self.graph.neighbors(node1) if not neighbours: continue for neighbour in neighbours: edge_attrs = self.get_bond_features(neighbour, node1) self.graph.add_edge(neighbour, cycle_name, attr_dict={ "bond_features": edge_attrs}) self.graph.remove_edge(node1, neighbour) nx.set_node_attributes(self.graph, "atom_features", values={cycle_name: Molecule.atom_features_of_contract_rings(0)}) for node1, node2 in cycle: if not isinstance(node1, six.string_types): self.graph.remove_node(node1) index += 1 cycle = self.get_cycle() self.graph = nx.convert_node_labels_to_integers(self.graph, first_label=0) nx.draw(self.graph) self.no_of_atoms = len(self.graph)
def add_tank(self, name, elevation=0.0, init_level=3.048, min_level=0.0, max_level=6.096, diameter=15.24, min_vol=None, vol_curve=None, coordinates=None): """ Adds a tank to the water network model. Parameters ------------------- name : string Name of the tank. elevation : float Elevation at the Tank. init_level : float Initial tank level. min_level : float Minimum tank level. max_level : float Maximum tank level. diameter : float Tank diameter. min_vol : float Minimum tank volume. vol_curve : Curve object Curve object coordinates : tuple of floats X-Y coordinates of the node location. Raises ------ ValueError If `init_level` greater than `max_level` or less than `min_level` """ elevation = float(elevation) init_level = float(init_level) min_level = float(min_level) max_level = float(max_level) diameter = float(diameter) if min_vol is not None: min_vol = float(min_vol) if init_level < min_level: raise ValueError("Initial tank level must be greater than or equal to the tank minimum level.") if init_level > max_level: raise ValueError("Initial tank level must be less than or equal to the tank maximum level.") if vol_curve and isinstance(vol_curve, six.string_types): vol_curve = self.get_curve(vol_curve) tank = Tank(name, elevation, init_level, min_level, max_level, diameter, min_vol, vol_curve) self._nodes[name] = tank self._tanks[name] = tank self._graph.add_node(name) if coordinates is not None: self.set_node_coordinates(name, coordinates) nx.set_node_attributes(self._graph, name='type', values={name: 'tank'}) self._num_tanks += 1
def weight_graph(self, node_attribute={}, link_attribute={}): """ Return a weighted graph based on node and link attributes. The weighted graph changes the direction of the original link if the weight is negative. Parameters ---------- G : graph A networkx graph node_attribute : dict or pandas Series node attributes link_attribues : dict or pandas Series link attributes Returns ------- G : weighted graph A networkx weighted graph """ for node_name in self.nodes(): try: value = node_attribute[node_name] nx.set_node_attributes(self, name='weight', values={node_name: value}) except: pass for (node1, node2, link_name) in list(self.edges(keys=True)): try: value = link_attribute[link_name] if value < 0: # change the direction of the link and value link_type = self[node1][node2][link_name]['type'] # 'type' should be the only other attribute on G.edge self.remove_edge(node1, node2, link_name) self.add_edge(node2, node1, link_name) nx.set_edge_attributes(self, name='type', values={(node2, node1, link_name): link_type}) nx.set_edge_attributes(self, name='weight', values={(node2, node1, link_name): -value}) else: nx.set_edge_attributes(self, name='weight', values={(node1, node2, link_name): value}) except: pass
def test(): mnist = input_data.read_data_sets("MINST_data", one_hot=False) train_data = mnist.train.images.astype(np.float32) fraction = 50 train_labels = mnist.train._labels[:fraction] with open('sugbgraphs_labels.pickle', 'wb') as f: pickle.dump(train_labels, f) test_data = mnist.test.images.astype(np.float32) print(train_data.shape) patch_size = 4 n_ids = range(patch_size * patch_size) A = np.ones((patch_size * patch_size, patch_size * patch_size)) np.fill_diagonal(A, 0) cc = 0 train = [] bins = list(np.linspace(0.0, 1.0, 10)) for sample in train_data[:fraction]: sample = sample.reshape((28, 28)) sugbg = [] patches = image.extract_patches_2d(sample, (patch_size, patch_size)) cc += 1 for p in patches: if np.sum(p) == 0: continue G1 = nx.from_numpy_matrix(A) dictionary = dict(zip(n_ids, np.digitize(p.flatten(), bins))) nx.set_node_attributes(G1, 'label', dictionary) sugbg.append(G1) train.append(sugbg) print(cc) with open('sugbgraphs_train.pickle', 'wb') as f: pickle.dump(train, f) del train test = [] for sample in test_data[:5]: sample = sample.reshape((28, 28)) sugbg = [] patches = image.extract_patches_2d(sample, (patch_size, patch_size)) for p in patches: if np.sum(p) == 0: continue G1 = nx.from_numpy_matrix(A) p = np.histogram(p.flatten(), bins=np.linspace(0.0, 1.0, 10))[0] dictionary = dict(zip(n_ids, p)) nx.set_node_attributes(G1, 'label', dictionary) sugbg.append(G1) test.append(sugbg) with open('sugbgraphs_test.pickle', 'wb') as f: pickle.dump(sugbg, f)
def docs_to_networkx(dataset, cats, window_size=2, vocabulary_creation=True): ds = './datasets/%s/' % dataset Gs = [] labels = [] type_ = 2 vocab_creation = vocabulary_creation words = [] # for vocabulary for doc in os.listdir(ds): if 'train.txt' in doc: type_ = 1 if type_ == 1: if os.path.exists("ds/vocab.txt"): vocab_creation = False with open(ds + '/train.txt', 'r', encoding='iso-8859-1') as doc: dc = 1 for line in doc: label = line[0] labels.append(label) terms = extract_terms_from_sentence(line[1:], stopwords=stopwords.words('english'), lemmatize=True, stem=True, only_N_J=True) if vocab_creation: words.extend(terms) graph = terms_to_graph(terms, window_size) G = graph_to_networkx(graph, name=label + '_' + str(dc)) # G = nx.convert_node_labels_to_integers(G, first_label=1, label_attribute='label') nx.set_node_attributes(G, 'label', dict(zip(G.nodes(), G.nodes()))) Gs.append(G) dc += 1 else: if os.path.exists("ds/vocab.txt"): vocab_creation = False for cat in cats.keys(): for doc in os.listdir(ds + cat): terms = extract_terms_from_file(ds + cat + '/' + doc, stopwords=stopwords.words('english'), lemmatize=True, stem=True, only_N_J=True) if vocab_creation: words.extend(terms) graph = terms_to_graph(terms, window_size) G = graph_to_networkx(graph, name=cat + doc.split('.')[0]) # G = nx.convert_node_labels_to_integers(G, first_label=1, label_attribute='label') nx.set_node_attributes(G, name='label', values=dict(zip(G.nodes(), G.nodes()))) Gs.append(G) labels.append(cats[cat]) if vocab_creation: vocab = dict(Counter(words)) create_vocabulary_file(fname, vocab) return Gs, labels # needs fix or discard
def processFeatures(self): sys.stderr.write("parsing features and constructing kmer graph\n") kmer_q=deque() prev_feature=None #loop through figfams to create kmers repeat_num=1 update_repeats=[] for feature in self.feature_parser.parse(): if prev_feature and prev_feature.group_id == feature.group_id and prev_feature.contig_id == feature.contig_id: repeat_num+=1 update_repeats.append(prev_feature) if self.eat_repeats: continue else: if repeat_num >1: update_repeats.append(prev_feature) for to_up in update_repeats: to_up.repeat_num =repeat_num repeat_num=1 update_repeats=[] feature.feature_id= len(self.feature_index) self.feature_index.append(feature) if prev_feature == None or prev_feature.genome_id != feature.genome_id: self.trackDiversity(feature.feature_id, self.all_diversity) if feature.genome_id not in self.replicon_map: self.replicon_map[feature.genome_id]=set() else: self.replicon_map[feature.genome_id].add(feature.contig_id) if(prev_feature and prev_feature.contig_id != feature.contig_id): kmer_q=deque()#clear kmer stack because switching replicons self.prev_node=None self.prev_indices=[] elif prev_feature and prev_feature.contig_id == feature.contig_id: if prev_feature.start > feature.start: assert InputError #depending on the context populate the context bin with appropriate ids to detect duplicates if self.context: if(prev_feature and prev_feature.getContextValue(self.context) != feature.getContextValue(self.context)): self.context_bin.clear() kmer_q.append(feature)#append the feature to the queue if(len(kmer_q)>self.ksize): kmer_q.popleft() self.addRFNode(kmer_q) elif(len(kmer_q)== self.ksize): self.addRFNode(kmer_q)#right now only passing in the last figfams information else:#kmer size is less than ksize kmer=None prev_feature=feature if self.debug: nx.set_node_attributes(self.rf_graph, "visit", "")