本文整理汇总了Python中networkx.write_gml函数的典型用法代码示例。如果您正苦于以下问题:Python write_gml函数的具体用法?Python write_gml怎么用?Python write_gml使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_gml函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
baseDir = "/Users/francis/Documents/cpe480_texts/basicmaterials/chemicals"
os.chdir(baseDir)
articles = glob.glob('*.txt')
graph = nx.Graph()
print "Let's get to work!"
for index in range(0, len(articles)):
start = time.time()
article = articles[index]
print article, "- (",index + 1,"of", len(articles),")"
significant = blib.filterWords(article)
blib.handleDocumentNouns(significant["NN"], graph)
print article, "done."
print "Time Elapsed:", time.time() - start, "seconds"
print
print
# sentiment = determineSemtiment(significant[0])
# Export graph data to a file
edgeListFile = "chemicals.graph"
print "Writing graph to file...."
nx.write_gml(graph, edgeListFile)
print "done"
示例2: ministro_ministro
def ministro_ministro(G):
"""
Cria um grafo de ministros conectados de acordo com a sobreposição de seu uso da legislação
Construido a partir to grafo ministro_lei
"""
GM = nx.Graph()
for m in G:
try:
int(m)
except ValueError:# Add only if node is a minister
if m != "None":
GM.add_node(m.decode('utf-8'))
# Add edges
for n in GM:
for m in GM:
if n == m: continue
if GM.has_edge(n,m) or GM.has_edge(m,n): continue
# Edge weight is the cardinality of the intersection each node neighbor set.
w = len(set(nx.neighbors(G,n.encode('utf-8'))) & set(nx.neighbors(G,m.encode('utf-8')))) #encode again to allow for matches
if w > 5:
GM.add_edge(n,m,{'weight':w})
# abreviate node names
GMA = nx.Graph()
GMA.add_weighted_edges_from([(o.replace('MIN.','').strip(),d.replace('MIN.','').strip(),di['weight']) for o,d,di in GM.edges_iter(data=True)])
P.figure()
nx.draw_spectral(GMA)
nx.write_graphml(GMA,'ministro_ministro.graphml')
nx.write_gml(GMA,'ministro_ministro.gml')
nx.write_pajek(GMA,'ministro_ministro.pajek')
nx.write_dot(GMA,'ministro_ministro.dot')
return GMA
示例3: copy_layout
def copy_layout(from_fname, to_fname):
if not from_fname[-4:] =='.gml': from_name +='.gml'
if not to_fname[-4:] =='.gml': to_name +='.gml'
print 'reading A=', from_fname,'..',
g1 = NX.read_gml(from_fname)
labels1 = NX.get_node_attributes(g1, 'label')
n1 = set(labels1.values())
print len(n1),'nodes'
print 'reading B=', to_fname,'..',
g2 = NX.read_gml(to_fname)
labels2 = NX.get_node_attributes(g2, 'label')
n2 = set(labels2.values())
print len(n2),'nodes'
intersection = len(n2.intersection(n1))
percent=100.*intersection/len(n2)
print 'B.intersect(A)=',intersection,'(%.1f%%)'%percent
print 'copying layout..',
mapping = {}
for L1 in labels1:
for L2 in labels2:
if labels1[L1]==labels2[L2]:
mapping[L1] = L2
break
layout = NX.get_node_attributes(g1, 'graphics')
attr = dict([ ( mapping[ID], {'x':layout[ID]['x'],'y':layout[ID]['y']} ) for ID in mapping])
NX.set_node_attributes(g2, 'graphics', attr)
NX.write_gml(g2, to_fname)
print 'done.'
示例4: draw_lineage
def draw_lineage(self, recs, nodecolor="mediumseagreen",
edgecolor="lightslateblue", dpi=96,
lineage_img="GO_lineage.png", engine="pygraphviz",
gml=False, draw_parents=True, draw_children=True):
assert engine in GraphEngines
if engine == "pygraphviz":
G = self.make_graph_pygraphviz(recs, nodecolor, edgecolor, dpi,
draw_parents=draw_parents, draw_children=draw_children)
else:
G = self.make_graph_pydot(recs, nodecolor, edgecolor, dpi,
draw_parents=draw_parents, draw_children=draw_children)
if gml:
import networkx as nx # use networkx to do the conversion
pf = lineage_img.rsplit(".", 1)[0]
NG = nx.from_agraph(G) if engine == "pygraphviz" else nx.from_pydot(G)
del NG.graph['node']
del NG.graph['edge']
gmlfile = pf + ".gml"
nx.write_gml(NG, gmlfile)
print("GML graph written to {0}".format(gmlfile), file=sys.stderr)
print(("lineage info for terms %s written to %s" %
([rec.id for rec in recs], lineage_img)), file=sys.stderr)
if engine == "pygraphviz":
G.draw(lineage_img, prog="dot")
else:
G.write_png(lineage_img)
示例5: main
def main():
graph = nx.MultiDiGraph()
with open('partiler.csv') as f:
reader = csv.DictReader(f)
data = list(reader)
for row in data:
graph.add_node(row['Abbreviation'],
label=row['Abbreviation'],
type="Party",
political_position=row['PoliticalPosition'])
for descendant in string_to_list(row['DescendantOf']):
graph.add_edge(row['Abbreviation'], descendant, label="Descendant Of",
weight=20)
for ancestor in string_to_list(row['AncestorOf']):
graph.add_edge(row['Abbreviation'], ancestor, label="Ancestor Of",
weight=20)
for leader in string_to_list(row['Leader']):
graph.add_edge(row['Abbreviation'],
leader, label="Leader",
weight=10)
for ideology in string_to_list(row['Ideology']):
graph.add_node(ideology, type="Ideology")
graph.add_edge(row['Abbreviation'], ideology, label="Ideology",
weight=40)
nx.write_gml(graph, "data.gml")
示例6: usufyToGmlExport
def usufyToGmlExport(d, fPath):
'''
Workaround to export to a gml file.
:param d: Data to export.
:param fPath: File path.
'''
# Reading the previous gml file
try:
oldData=nx.read_gml(fPath)
except UnicodeDecodeError as e:
print "UnicodeDecodeError:\t" + str(e)
print "Something went wrong when reading the .gml file relating to the decoding of UNICODE."
import time as time
fPath+="_" +str(time.time())
print "To avoid losing data, the output file will be renamed to use the timestamp as:\n" + fPath + "_" + str(time.time())
print
# No information has been recovered
oldData = nx.Graph()
except Exception as e:
# No information has been recovered
oldData = nx.Graph()
newGraph = _generateGraphData(d, oldData)
# Writing the gml file
nx.write_gml(newGraph,fPath)
示例7: save_celltype_graph
def save_celltype_graph(self, filename="celltype_conn.gml", format="gml"):
"""
Save the celltype-to-celltype connectivity information in a file.
filename -- path of the file to be saved.
format -- format to save in. Using GML as GraphML support is
not complete in NetworkX.
"""
start = datetime.now()
if format == "gml":
nx.write_gml(self.__celltype_graph, filename)
elif format == "yaml":
nx.write_yaml(self.__celltype_graph, filename)
elif format == "graphml":
nx.write_graphml(self.__celltype_graph, filename)
elif format == "edgelist":
nx.write_edgelist(self.__celltype_graph, filename)
elif format == "pickle":
nx.write_gpickle(self.__celltype_graph, filename)
else:
raise Exception("Supported formats: gml, graphml, yaml. Received: %s" % (format))
end = datetime.now()
delta = end - start
config.BENCHMARK_LOGGER.info(
"Saved celltype_graph in file %s of format %s in %g s"
% (filename, format, delta.seconds + delta.microseconds * 1e-6)
)
print "Saved celltype connectivity graph in", filename
示例8: export_singleSTG
def export_singleSTG(self,pSid,Type,Isomorphy,localIGs,NNF,filename,initialRules):
# TODO: localIGs
# TODO: filename = Unterordner mit Dateien oder Dateiname filename+Suffix?
# TODO: GML nested network?
filename = str(filename)
if not filename[-4:]==".gml":
filename += ".gml"
self.set_initialRules(initialRules)
self.set_initialStates()
found=False
for i, pSet in enumerate(self._psc.get_parameterSets()):
if i==pSid:
found = True
break
if not found:
self.message("MC: STG with index %i does not exist. Choose index between 0 and %i."%(pSid,i), "error")
return
ts = TS.TransitionSystem(self, pSet)
ts.compute_destination()
if Type=="SCC":
ts.computeSCCDAG(Isomorphy,NNF)
nx.write_gml(ts._sccdag._nxSCCDAG,filename)
if NNF:
filename = filename[-4:]+".nnf"
han=open(filename,"w")
han.write(ts._sccdag._nnfNN)
han.close()
elif Type=="allStates":
nx.write_gml(ts.getDestinySTG(),filename)
else:
self.message("Choose either SCC Graph or Full State Graph.","error")
示例9: network_layout
def network_layout(gmt_fn, outfn=None):
## make a Graph object and write to gml for Gephi to
## do the layout
d_gmt = read_gmt(gmt_fn)
d_gmt_filt = {}
for term, genes in d_gmt.items():
if len(genes) >= 5:
d_gmt_filt[term] = genes
d_gmt = d_gmt_filt
print 'number of terms:', len(d_gmt)
umls_ids_kept = d_gmt.keys()
adj_matrix = jaccard_matrix(d_gmt)
m = adj_matrix > 0.2
# degrees = adj_matrix.sum(axis=0)
adj_matrix = adj_matrix * m.astype(int)
G = nx.from_numpy_matrix(adj_matrix)
print 'G: ',G.number_of_edges(), G.number_of_nodes()
for i in range(adj_matrix.shape[0]):
# G.node[i]['size'] = degrees[i]
# G.node[i]['size'] = len(d_gmt[umls_ids_kept[i]])
G.node[i]['size'] = G.degree(i)
G.node[i]['id'] = umls_ids_kept[i]
if outfn is not None:
nx.write_gml(G, outfn)
return G
示例10: export_topology
def export_topology(topology, filename='topology.gml'):
"""Write the topology to a file in Graph Modelling Language (GML) format
Filenames ending in .bz2 or .gz will be compressed
"""
nx.write_gml(topology, filename)
示例11: write_edge2cid
def write_edge2cid(e2c,filename, outFile, delimiter="\t"):
# write edge2cid three-column file
f = open(filename+".edge2comm.txt",'w')
c2c = dict( (c,i+1) for i,c in enumerate(sorted(list(set(e2c.values())))) ) # ugly...
#print c2c
for e,c in sorted(e2c.iteritems(), key=itemgetter(1)):
f.write( "%s%s%s%s%s\n" % (str(e[0]),delimiter,str(e[1]),delimiter,str(c2c[c])) )
f.close()
cid2edges,cid2nodes = defaultdict(set),defaultdict(set) # faster to recreate here than
for edge,cid in e2c.iteritems(): # to keep copying all dicts
cid2edges[cid].add( edge ) # during the linkage...
cid2nodes[cid] |= set(edge)
cid2edges,cid2nodes = dict(cid2edges),dict(cid2nodes)
# write list of edges for each comm, each comm on its own line
f,g = open(filename+".comm2edges.txt", 'w'),open(filename+".comm2nodes.txt", 'w')
for cid in sorted(cid2edges.keys()):
nodes,edges = map(str,cid2nodes[cid]), ["%s,%s" % (ni,nj) for ni,nj in cid2edges[cid]]
f.write( "\t".join(edges) ); f.write("\n")
g.write( "\t".join([str(cid)] + nodes) ); g.write("\n")
f.close(); g.close()
#print e2c
e2cRelabel=dict((edge,c2c.get(id)) for (edge, id) in e2c.items())
g=nx.Graph()
g.add_edges_from(e2cRelabel.keys())
nx.set_edge_attributes(g,"label",e2cRelabel)
nx.write_gml(g,outFile)
示例12: generate_overlap_gml
def generate_overlap_gml(overlap_data, contained_reads, gml_filename):
containment_tolerance = 50
permitted_error_pct = 2
import networkx as nx
G=nx.DiGraph()
node_in_graph = set()
for q_name in overlap_data:
if q_name in contained_reads:
continue
if q_name not in node_in_graph:
G.add_node(q_name)
targets = overlap_data[ q_name ].hits
targets_3prime = [ h for h in targets if h[4][1] < containment_tolerance and h[0] not in contained_reads]
targets_5prime = [ h for h in targets if h[3][1] < containment_tolerance and h[0] not in contained_reads]
targets_3prime.sort(key = lambda k:k[1])
targets_5prime.sort(key = lambda k:k[1])
if len(targets_3prime) > 0:
t = targets_3prime[0]
t_name = t[0]
if t_name not in node_in_graph:
G.add_node(t_name)
G.add_edge(q_name, t_name)
if len(targets_5prime) > 0:
t = targets_5prime[0]
t_name = t[0]
if t_name not in node_in_graph:
G.add_node(t_name)
G.add_edge(q_name, t_name)
nx.write_gml(G, gml_filename)
示例13: crawl
def crawl(self, n=None, dump=None):
'''Launch crawler on n pages if n != None, otherwise, it stops when all webpages have been explored'''
if n != None:
self.nPages = n
print "Start crawling ", self.startURL
while (self.nPages == None and len(self.crawling) > 0) or (self.nPages != None and len(self.crawled) <= self.nPages):
self.currentId += 1
if dump != None and (self.currentId)%dump == 0:
# Dump intermediary graph in case of crash or interrupt
nx.write_gml(self.G, 'graph_%06d.gml' % self.currentId)
self.dumpToFile(self.crawled, 'crawled_%06d.p' % self.currentId)
self.dumpToFile(self.crawling, 'crawling_%06d.p'% self.currentId)
currentURL = self.crawling.pop(0)
print "Crawling page %d of %d:"%(self.currentId, len(self.crawling + self.crawled)), currentURL.encode('ascii','xmlcharrefreplace')
self.crawled.append(currentURL)
# Get a list of new links from the current page
dirtyLinks = self.getLinks(currentURL)
cleanLinks = self.cleanLinks(dirtyLinks, currentURL)
newLinks = list(set(cleanLinks) - set(self.crawling + self.crawled))
self.crawling += newLinks
print '%d of %d new links found on the current page'%(len(newLinks), len(cleanLinks))
# Build network
self.G = self.updateNetwork(self.G, currentURL, cleanLinks)
self.dumpGraph()
return
示例14: betweenness_fracture
def betweenness_fracture(infile, outfile, fraction, recalculate = False):
"""
Removes given fraction of nodes from infile network in reverse order of
betweenness centrality (with or without recalculation of centrality values
after each node removal) and saves the network in outfile.
"""
g = networkx.read_gml(infile)
m = networkx.betweenness_centrality(g)
l = sorted(m.items(), key = operator.itemgetter(1), reverse = True)
largest_component = max(networkx.connected_components(g), key = len)
n = len(g.nodes())
for i in range(1, n):
g.remove_node(l.pop(0)[0])
if recalculate:
m = networkx.betweenness_centrality(g)
l = sorted(m.items(), key = operator.itemgetter(1),
reverse = True)
largest_component = max(networkx.connected_components(g), key = len)
if i * 1. / n >= fraction:
break
components = networkx.connected_components(g)
component_id = 1
for component in components:
for node in component:
g.node[node]["component"] = component_id
component_id += 1
networkx.write_gml(g, outfile)
示例15: main2
def main2():
number = 19
species,rules,parameterDict,observableList= readBNGXML.parseXML('output{0}.xml'.format(number))
graph = nx.Graph()
simpleGraph(graph,species,observableList,number)
nx.write_gml(graph,'graph_{0}.gml'.format(number))