本文整理汇总了Python中networkx.write_graphml函数的典型用法代码示例。如果您正苦于以下问题:Python write_graphml函数的具体用法?Python write_graphml怎么用?Python write_graphml使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_graphml函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_topology
def get_topology():
G = nx.Graph()
G.add_node("poi-1", packetloss=0.0, ip="0.0.0.0", geocode="US", bandwidthdown=17038, bandwidthup=2251, type="net", asn=0)
G.add_edge("poi-1", "poi-1", latency=50.0, jitter=0.0, packetloss=0.05)
s = StringIO()
nx.write_graphml(G, s)
return s.getvalue()
示例2: create_joined_multigraph
def create_joined_multigraph():
G=nx.DiGraph()
upp=nx.read_graphml('upperlevel_hashtags.graphml')
for ed in upp.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
G.add_edge(ed[1],ed[0],attr_dict=ed[2])
mid=nx.read_graphml('friendship_graph.graphml')
for ed in mid.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
inter=nx.read_graphml('interlevel_hashtags.graphml')
for ed in inter.edges(data=True):
G.add_edge(ed[0],ed[1],attr_dict=ed[2])
G.add_edge(ed[1],ed[0],attr_dict=ed[2])
down=nx.read_graphml('retweet.graphml')
mapping_f={}
for i,v in enumerate(down.nodes()):
mapping_f[v]='%iretweet_net' %i
for ed in down.edges(data=True):
G.add_edge(mapping_f[ed[0]],mapping_f[ed[1]],attr_dict=ed[2])
for nd in mid.nodes():
if nd in mapping_f:
G.add_edge(nd,mapping_f[nd])
G.add_edge(mapping_f[nd],nd)
nx.write_graphml(G,'joined_3layerdigraph.graphm')
return G,upp.nodes(),mid.nodes(),mapping_f.values()
示例3: main
def main():
files = []
for i in range(1,26):
files.append("db/Minna_no_nihongo_1.%02d.txt" % i)
for i in range(26,51):
files.append("db/Minna_no_nihongo_2.%02d.txt" % i)
words = get_words_from_files(files)
G=nx.Graph()
for w in words:
G.add_node(w)
G.node[w]['chapter'] = words[w]['chapter']
G.node[w]['kana'] = words[w]['kana']
G.node[w]['meaning'] = words[w]['meaning'][:-1]
for word1, word2 in itertools.combinations(words,2):
for w1 in word1[:-1]:
#print w1.encode('utf-8')
#print ud.name(w1)
if "CJK UNIFIED" in ud.name(w1) and w1 in word2:
#print word1.encode('utf-8'), word2.encode('utf-8')
G.add_edge(word1, word2)
break
#G = nx.connected_component_subgraphs(G)
G = sorted(nx.connected_component_subgraphs(G), key = len, reverse=True)
#print len(G)
#nx.draw(G)
nx.write_graphml(G[0], "kanjis.graphml", encoding='utf-8', prettyprint=True)
示例4: lei_vs_lei
def lei_vs_lei(nedges=None):
"""
Grafo de todas com todas (leis)
"""
# Verão original Flávio comentada
# curgrafo.execute('select lei_id_1,esfera_1,lei_1,lei_id_2,esfera_2, lei_2, peso from vw_gr_lei_lei where peso >300 and lei_id_2>2')
# curgrafo.execute('select lei_id_1,lei_tipo_1,lei_nome_1,lei_id_2,lei_tipo_2, lei_nome_2, peso from vw_gr_lei_lei where lei_count <= 20 and lei_id_1 = 1 and lei_id_2 <= 20 limit 0,1000')
# curgrafo.execute('select lei_id_1,lei_tipo_1,lei_nome_1,lei_id_2,lei_tipo_2, lei_nome_2, peso from vw_gr_lei_lei where lei_count <= 8 and lei_id_1 <= 20 and lei_id_2 <= 20 limit 0,1000')
curgrafo.execute('select lei_id_1,esfera_1,lei_1,lei_id_2,esfera_2, lei_2, peso from vw_gr_lei_lei where lei_count <= 10 and lei_id_1 <= 50 and lei_id_2 <= 200 limit 0,10000')
if not nedges:
res = curgrafo.fetchall()
nedges = len(res)
else:
res = curgrafo.fetchmany(nedges)
eds = [(i[0],i[3],i[6]) for i in res]
G = nx.Graph()
#eds = [i[:3] for i in res]
G.add_weighted_edges_from(eds)
print "== Grafo Lei_Lei =="
print "==> Order: ",G.order()
print "==> # Edges: ",len(G.edges())
# Adding attributes to nodes
for i in res:
G.node[i[0]]['esfera'] = i[1]
G.node[i[0]]['lei'] = i[2]
G.node[i[3]]['esfera'] = i[4]
G.node[i[3]]['lei'] = i[5]
nx.write_graphml(G,'lei_lei.graphml')
nx.write_gml(G,'lei_lei.gml')
nx.write_pajek(G,'lei_lei.pajek')
nx.write_dot(G,'lei_lei.dot')
return G,res
示例5: export_graph
def export_graph(G, write_filename):
write_dir = "./output/" + write_filename + "/"
if not os.path.isdir(write_dir):
os.mkdir(write_dir)
# Remove pho edge weights
for n1 in G.edge:
for n2 in G.edge[n1]:
G.edge[n1][n2]={}
print("\twriting gml")
for node in G.nodes_iter():
for key, val in list(G.node[node].items()):
G.node[node][key]=int(val)
nx.write_gml(G, write_dir + write_filename + ".gml")
print("\twriting graphml")
nx.write_graphml(G, write_dir + write_filename + ".graphml")
print("\twriting edgelist")
f = open(write_dir + write_filename + ".edgelist","w")
for edge in G.edges_iter():
f.write("\t".join([str(end) for end in list(edge)[:2]])+"\n")
f.close()
f = open(write_dir + write_filename + ".nodelist","w")
print("\twriting nodelist")
f.write("\t".join(["node_id"] + node_attributes) + "\n")
for node in G.nodes_iter():
f.write("\t".join([str(node)] + [str(G.node[node][attribute]) for attribute in node_attributes]) + "\n")
示例6: save_graph
def save_graph(self, graphname, fmt='edgelist'):
"""
Saves the graph to disk
**Positional Arguments:**
graphname:
- Filename for the graph
**Optional Arguments:**
fmt:
- Output graph format
"""
self.g.graph['ecount'] = nx.number_of_edges(self.g)
g = nx.convert_node_labels_to_integers(self.g, first_label=1)
if fmt == 'edgelist':
nx.write_weighted_edgelist(g, graphname, encoding='utf-8')
elif fmt == 'gpickle':
nx.write_gpickle(g, graphname)
elif fmt == 'graphml':
nx.write_graphml(g, graphname)
else:
raise ValueError('edgelist, gpickle, and graphml currently supported')
pass
示例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: finish_graph
def finish_graph():
nx.draw(g)
file_name='host_hops1.png'
file_nameg='host_hops1.graphml'
plt.savefig(file_name)
nx.write_graphml(g, file_nameg)
print ("Graph Drawn")
示例9: to_file
def to_file(self, name_graphml='AST2NX.graphml'):
""" write to a graphml file which can be read by a lot of professional visualization tools such as Cytoscape.
"""
if name_graphml.endswith('.graphml'):
nx.write_graphml(self.NetworkX, name_graphml)
else:
nx.write_graphml(self.NetworkX, name_graphml + '.graphml')
示例10: draw_base_graph
def draw_base_graph(self):
print 'writing base graphml ...'
G = nx.Graph()
G.add_nodes_from(xrange(self.num_nodes))
G.add_edges_from(self.E_base)
nx.write_graphml(G,'exodus.graphml')
print 'done ... (load in Gephi)'
示例11: build_graph
def build_graph(self):
self.node_list = [self.like_minded_friend, self.books_reco_from, self.movies_reco_from, self.music_reco_from,
self.sport_reco_from, 'Read a book? Ask me', 'Watch a movie? Ask me', 'Sing along with',
'Sports arena?', 'Similar tastes?']
#self.node_list.append(self.friends_likes[self.like_minded_friend][:10])
self.node_list.append(self.user_name)
self.G.add_nodes_from(self.node_list)
self.G.add_edge(self.user_name, 'Similar tastes?', color = 'purple')
self.G.add_edge('Similar tastes?', self.like_minded_friend,color = 'purple' )
self.G.add_edge(self.user_name, 'Read a book? Ask me', color = 'blue')
self.G.add_edge('Read a book? Ask me', self.books_reco_from, color = 'blue')
self.G.add_edge(self.user_name, 'Watch a movie? Ask me', color = 'green')
self.G.add_edge('Watch a movie? Ask me', self.movies_reco_from, color = 'green')
self.G.add_edge(self.user_name, 'Sing along with', color = 'yellow')
self.G.add_edge('Sing along with', self.music_reco_from, color = 'yellow')
self.G.add_edge(self.user_name, 'Sports arena?', color = 'orange')
self.G.add_edge('Sports arena?', self.sport_reco_from, color = 'orange')
self.G.add_edge(self.user_name, 'Pages you might like!', color = 'red')
for node in self.friends_likes[self.like_minded_friend][:10]:
self.G.add_edge('Pages you might like', node, color = 'red')
nx.write_graphml(self.G, 'FBViz' + ".graphml")
示例12: start
def start(self):
for id in self.oidRootNamePairs:
self.oidNamePairs,currIDs=Utils.getoidNames(self.oidNamePairs,id,Def.typ)
Utils.report('Processing current IDs: '+str(currIDs))
flip=(Def.typ=='fr')
self.addDirectedEdges(id, currIDs,flip=flip)
n=len(currIDs)
Utils.report('Total amount of IDs: '+str(n))
c=1
for cid in currIDs:
Utils.report('\tSub-level run: getting '+Def.typ2,str(c)+'of'+str(n)+Def.typ+cid)
self.oidNamePairs,ccurrIDs=Utils.getoidNames(self.oidNamePairs,cid,Def.typ2)
self.addDirectedEdges( cid, ccurrIDs)
c=c+1
for id in self.oidRootNamePairs:
if id not in self.oidNamePairs:
self.oidNamePairs[id]=self.oidRootNamePairs[id]
self.labelNodes(self.oidNamePairs)
Utils.report(nx.info(self.DG))
now = datetime.datetime.now()
timestamp = now.strftime("_%Y-%m-%d-%H-%M-%S")
fname=UserID._name.replace(' ','_')
nx.write_graphml(self.DG, '/'.join(['reports',fname+'_google'+Def.typ+'Friends_'+timestamp+".graphml"]))
nx.write_edgelist(self.DG, '/'.join(['reports',fname+'_google'+Def.typ+'Friends_'+timestamp+".txt"]),data=False)
示例13: create_graph
def create_graph():
G = nx.Graph()
data = []
reposts = []
client = MongoClient()
db = client["vk_db"]
collection = db["valid_communities_info"]
result = collection.find()
for res in result:
data.append(res)
db=client["reposts"]
collection = db["general"]
answer = collection.find()
for res in answer:
reposts.append(res)
for each in data:
G.add_node(each["screen_name"])
G.node[each["screen_name"]]['weight'] = each["weight"]
for each in reposts:
G.add_edge(get_name_by_id(each["owner"]), get_name_by_link(each["link"]), weight=each["times"])
nx.write_graphml(G,'vk.graphml')
示例14: main
def main():
print "time_evol module is the main code."
## to import a network of 3-node example
EDGE_FILE = 'C:\Boolean_Delay_in_Economics\Manny\EDGE_FILE.dat'
NODE_FILE = 'C:\Boolean_Delay_in_Economics\Manny\NODE_FILE.dat'
net = inet.read_network_from_file(EDGE_FILE, NODE_FILE)
nodes_list = inet.build_nodes_list(NODE_FILE)
'''
## to obtain time series data for all possible initial conditions for 3-node example network
timeSeriesData = ensemble_time_series(net, nodes_list, 2, 10)#, Nbr_States=2, MAX_TimeStep=20)
initState = 1
biStates = decimal_to_binary(nodes_list, initState)
print 'initial state', biStates
## to print time series data for each node: a, b, c starting particualr decimal inital condition 1
print 'a', timeSeriesData['a'][1]
print 'b', timeSeriesData['b'][1]
print 'c', timeSeriesData['c'][1]
'''
## to obtain and visulaize transition map in the network state space
decStateTransMap = net_state_transition(net, nodes_list)
nx.write_graphml(decStateTransMap,'C:\Boolean_Delay_in_Economics\Manny\Results\BDE.graphml')
'''
示例15: output_graph
def output_graph(mps, mp_data, edges):
G=nx.Graph()
# Define the nodes
for mp in mps:
G.add_node(mp, label=mp_data[mp]["name"], party=mp_data[mp]["party"], constituency=mp_data[mp]["constituency"])
# Process all known edges
for (mp_tuple,agr_data) in edges.items():
agreements = agr_data[0]
agreement_rate = agr_data[2]
# Depending on the selection criteria, filter out relationships
if agreement_rate < 85:
continue
# Determine a (normalized) weight, again depending on the desired graph
# edge_wt = agreements
range_min = 85
range_max = 100
weight_base = agreement_rate - range_min
edge_wt = ( float(weight_base) / float(range_max - range_min) )
G.add_edge(mp_tuple[0],mp_tuple[1], agreement=agreement_rate, weight=edge_wt )
nx.write_graphml(G, "mp_agreement.graphml")