本文整理汇总了Python中networkx.write_pajek函数的典型用法代码示例。如果您正苦于以下问题:Python write_pajek函数的具体用法?Python write_pajek怎么用?Python write_pajek使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_pajek函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getGraph
def getGraph(fileRef):
data = getFiles(fileName)
nodes = getNodes(data)
edges = getEdges(data)
graph = createNetwork(edges, nodes)
gml_output_path = os.path.join('output', 'network',
fileRef.
split('.')[0].
split('/')[1] + '.gml')
print "Writing GML file to %s" % gml_output_path
nx.write_gml(graph, gml_output_path)
net_output_path = os.path.join('output', 'network',
fileRef.
split('.')[0].
split('/')[1] + '.net')
print "Writing net file to %s" % net_output_path
nx.write_pajek(graph, net_output_path)
params = (graph.number_of_nodes(), graph.number_of_edges())
print "Graph has %s nodes, %s edges" % params
print
示例2: 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
示例3: generate_graphs_from_baited_loci
def generate_graphs_from_baited_loci():
# Adjacent duplets
print "Loading adjacent duplets"
graph, duplets = load_adjacent_duplets_graph()
linked_list_file = os.path.join(work_dir, "baited_adj_duplets.txt")
with open(linked_list_file, "w") as outf:
outf.write("#From\tTo\tWeight\n")
for edge in graph.edges_iter(data=True):
_from = edge[0]
_to = edge[1]
_weight = edge[2]["weight"]
outf.write("%s\t%s\t%s\n" % (_from, _to, _weight))
print "Writing adjacent duplets"
pajek_file = os.path.join(work_dir, "baited_adj_duplets.net")
nx.write_pajek(graph, pajek_file)
# duplets
print "Loading duplets"
graph, duplets = load_duplets_graph()
print "Writing duplets"
linked_list_file = os.path.join(work_dir, "baited_duplets.txt")
with open(linked_list_file, "w") as outf:
outf.write("#From\tTo\tWeight\n")
for edge in graph.edges_iter(data=True):
_from = edge[0]
_to = edge[1]
_weight = edge[2]["weight"]
outf.write("%s\t%s\t%s\n" % (_from, _to, _weight))
pajek_file = os.path.join(work_dir, "baited_duplets.net")
nx.write_pajek(graph, pajek_file)
示例4: saveGraphPajek
def saveGraphPajek(self, filename=None):
if filename is not None:
self.filename = filename
nx.write_pajek(self.G, self.filename)
logger.info(u"Saved - %s" % self.filename)
示例5: make_network
def make_network(raw_file_list):
# handles the individual nodes
collect_nodes_by_source = []
list_of_source_names = []
node_list = []
GB = nx.Graph()
# for all the nodes...
for i in range(len(raw_file_list)):
# check whether they are name, source or else (not returned). "i" works as an index to identify the respective node when it comes back
checker, a = my_containsAny(raw_file_list[i], i)
# raw data starts with a source, all following non-source lines refer to names or tags. So all returned nodes should be linked to each other
if checker == "source":
GB.add_node(raw_file_list[a], bipartite = 0)
source = raw_file_list[a]
while source == raw_file_list[a]:
if checker == "node":
GB.add_node(raw_file_list[a], bipartite = 1)
GB.add_edge(raw_file_list[a], raw_file_list[a+1])
G = bnx.weighted_projected_graph(GB, GB.nodes(["bipartite"]==1))
#nx.write_graphml(GB, "abolitionists_bipartite.graphml")
nx.write_pajek(G, "abolitionists.net")
print "done!"
示例6: 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
示例7: addTracks
def addTracks(artist, tracks, artistGraph):
for track in tracks:
# get list of users who have favorited this user's track
favoriters = client.get('/tracks/' + str(track) + '/favoriters')
print "Add Favoriters"
for user in favoriters:
addWeight(user.id, artist, artistGraph, 'fav_weight')
try:
print "Writing out new artists..."
nx.write_pajek(artistGraph, 'artistGraph.net')
print "New artists written successfully!"
print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
except IOError:
print "New artists could not be written..."
# get list of users who have commented on this user's track
commenters = client.get('/tracks/' + str(track) + '/comments')
print "Add Commenters"
for user in commenters:
addWeight(user.user_id, artist, artistGraph, 'com_weight')
try:
print "Writing out new artists..."
nx.write_pajek(artistGraph, 'artistGraph.net')
print "New artists written successfully!"
print "The artist graph currently contains " + str(len(artistGraph)) + " artists."
print "The artist graph currently contains " + str(nx.number_strongly_connected_components(artistGraph)) + " strongly connected components."
except IOError:
print "New artists could not be written..."
示例8: main
def main(max_depth = 2, graphfile='music_network.net', id_mapfile='artist_id_map.p'):
g = net.Graph()
id_map = []
for artist_id, artist_name in get_playlist_artists('science'):
id_map.append({'id': artist_id, 'name':artist_name})
snowball_sampling(g, artist_search, artist_id, id_map=id_map, max_depth=max_depth)
net.write_pajek(g, graphfile)
pickle.dump(id_map, open(id_mapfile,'wb'))
示例9: write_graph
def write_graph(G, gfile):
try:
print "Writing out new artists..."
nx.write_pajek(G, gfile)
print "New artists written successfully!"
print "The artist graph currently contains " + str(len(G)) + " artists."
print "The artist graph currently contains " + str(nx.number_strongly_connected_components(G)) + " strongly connected components."
except IOError:
print "New artists could not be written..."
示例10: processDirectory
def processDirectory(dir_path, top_number, lemma_flag):
"""
Process the directory the user inputs
"""
if (not lemma_flag):
print 'NO LEMMATIZING'
cross_graph = nx.MultiDiGraph()
for file_name in os.listdir(dir_path):
file_path = os.path.join(dir_path, file_name)
if os.path.isfile(file_path) and file_name.endswith('.txt'):
print 'analyzing ' + file_name, '...'
try:
file_open = io.open(file_path, 'r')
except IOError:
print 'OPEN FILE ' + file_path + ' ERROR'
sys.exit(1)
sent_seg = preprocessSent(file_open.read())
sent_depend = produceDepend(sent_seg, lemma_flag)
# print sent_seg
# print sent_depend
file_open.close()
single_graph = drawDependGraph(sent_depend, sent_seg, dir_path, file_name, lemma_flag)
# Doing the combination
cross_graph.add_nodes_from([v for v, d in single_graph.nodes(data = True) if v not in cross_graph.nodes()], freq = 0)
for u, v, d in single_graph.edges(data = True):
if (u, v) not in cross_graph.edges():
cross_graph.add_edge(u, v, dependency = d['dependency'], label = d['label'], freq = 0)
else:
list_dif_depend = [cross_graph[u][v][i]['dependency'] for i in range(len(cross_graph[u][v].items()))]
if d['dependency'] not in list_dif_depend:
cross_graph.add_edge(u, v, dependency = d['dependency'], label = d['label'], freq = 0)
for v, d in cross_graph.nodes(data = True):
if v in single_graph.nodes():
d['freq'] += single_graph.node[v]['freq']
for u, v, d in cross_graph.edges(data = True):
if (u, v) in single_graph.edges():
list_dif_depend = [single_graph[u][v][i]['dependency'] for i in range(len(single_graph[u][v].items()))]
dict_dif_depend = dict(zip(list_dif_depend, range(len(single_graph[u][v].items()))))
if d['dependency'] in list_dif_depend:
depend_index = dict_dif_depend[d['dependency']]
d['freq'] += single_graph[u][v][depend_index]['freq']
if lemma_flag:
nx.write_pajek(cross_graph, dir_path + 'syntactic_lemma/' + 'syntactic_graph_cross_txt_lemma.net')
graphAnalysis(cross_graph, top_number, dir_path + 'syntactic_lemma/' + 'syntactic_graph_lemma_analysis.csv')
else:
nx.write_pajek(cross_graph, dir_path + 'syntactic_no_lemma/' + 'syntactic_graph_cross_txt_no_lemma.net')
graphAnalysis(cross_graph, top_number, dir_path + 'syntactic_no_lemma/' + 'syntactic_graph_analysis_no_lemma.csv')
示例11: save_friend_list
def save_friend_list(friend_list,path='/home/pj/Python/social_analysis/',filename='friend.net'):
fullpath=path+filename
print '开始保存社交关系'
g=networkx.Graph()
for name in friend_list.keys():
for node in friend_list[name]:
g.add_edge(name,node[1])
networkx.write_pajek(g,fullpath)
print '好友列表存储在%s'%(fullpath)
return 1
示例12: write_graph_in_format
def write_graph_in_format(self, filerootname, fileformat='gexf'):
fileformat = fileformat.lower()
filename = "%s.%s" % (filerootname.replace(" ", "_"), fileformat)
if fileformat == 'json':
with open(filename, 'w') as f:
json.dump(json_graph.node_link_data(self), f)
elif fileformat == 'net':
nx.write_pajek(self, filename)
elif fileformat == 'gexf':
nx.write_gexf(self, filename)
示例13: writepajek
def writepajek(A,filename):
SP = np_to_scipy_matrix(A)
edges = []
for i in range(0,A.shape[0]):
for j in range(0,A.shape[1]):
if A[i,j]!=0:
edges.append(['A'+str(i),'B'+str(j)])
G = nx.bipartite.from_biadjacency_matrix(SP,create_using=None)
nx.write_pajek(G, filename + '.net')
示例14: run
def run(self):
# Run simulation for several type of networks, in this case Erdos-Renyi and Random Network
self.networks = [
{
'network': nx.scale_free_graph(n = self.nodes),
'name': 'ScaleFree'
},
{
'network': nx.erdos_renyi_graph(n = self.nodes, p = 0.1), # 0.2, 0.5
'name': 'ErdosRenyi'
},
{
'network': nx.random_regular_graph(n = self.nodes, d = 10),
'name': 'RandomNetwork'
}
]
for network in self.networks:
nxgraph = network['network']
graph = helper.convert_nxgraph_to_dict(nxgraph)
# write network in pajek
filename = 'pajek/'+ network['name'] + '_' + str(self.nodes) + '.net'
nx.write_pajek(nxgraph, filename)
for mu in self.mu_values:
print 'Starting...'
start_time = time.time()
# B beta (at least 51 values, B=0.02)
beta = 0.0
betas = []
averages = []
for i in range(0, 51):
start_time_beta = time.time()
sis_initial_model = sis.SIS(graph, mu, beta, self.p0)
simulation = mc.MonteCarlo(sis_initial_model, self.rep, self.t_max, self.t_trans)
total_average = simulation.run()
total_time_beta = time.time() - start_time_beta
betas.append(beta)
averages.append(total_average)
print 'B: {}, average: {}, time: {}s'.format(beta, total_average, total_time_beta)
beta += 0.02
total_time = time.time() - start_time
print 'total time: {}'.format(total_time)
# plot results and save in file
helper.plot_results(network['name'], self.nodes, mu, betas, averages)
helper.write_results(network['name'], self.nodes, mu, betas, averages)
break
示例15: main
def main():
badges = user_badge_extract('Badges.xml')
G = create_graph(badges)
# draw_graph(G)
# Saving the graph in Pajek format
nx.write_pajek(G, "graph.net")
# Saving the graph as AdjList
nx.write_adjlist(G, "graph.adjlist")