本文整理汇总了Python中igraph.Graph.add_edges_from方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_edges_from方法的具体用法?Python Graph.add_edges_from怎么用?Python Graph.add_edges_from使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.add_edges_from方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_full_arnetminer_dataset
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import add_edges_from [as 别名]
def parse_full_arnetminer_dataset(should_profile, use_igraph):
"""
Parse the full arnetminer dataset in plaintext format. Vertex types in the graph are:
1: Authors
2: Papers
3: Conferences
4: Terms
"""
print "Parsing nodes for graph..."
input_file = open(os.path.join(project_root, 'data', 'Arnetminer-Full.txt'))
beginning = input_file.tell()
# Use either igraph or networkx
if use_igraph:
graph = Graph(directed=True)
else:
graph = MultiDiGraph()
# Counts for statistics
VALID_PAPERS = 8579222 # Most recent count of valid papers from Arnetminer
papers_processed = 0
# Caches for vertices & edges to add (batch adds together when using igraph)
vertices_to_add = []
edges_to_add = []
types_of_vertices_to_add = []
FLUSH_FREQUENCY = 100000 # 'Flush' cached vertices and edges this often
# Add each paper to graph (adding missing associated terms, authors, and conferences)
for title, authors, conference, terms, paper_index in __papers_from_file(input_file, should_profile):
# Output any test paper indices found
if title in test_papers:
print "Found test paper '%s' by %s, index: %d" % (title, ','.join(authors), paper_index)
if use_igraph:
# Use string, because otherwise integer overflow in igraph
paper_index = str(paper_index)
# Collect vertices and edges to add
vertices_to_add += [paper_index, conference] + authors + terms
types_of_vertices_to_add += [2, 3] + ([1] * len(authors)) + ([4] * len(terms))
for author in authors:
edges_to_add += [(author, paper_index), (paper_index, author)]
edges_to_add += [(conference, paper_index), (paper_index, conference)]
for term in terms:
edges_to_add += [(term, paper_index), (paper_index, term)]
# Every so often, actually mutate the graph
if papers_processed % FLUSH_FREQUENCY == 0:
for vertex, vertex_type in zip(vertices_to_add, types_of_vertices_to_add):
graph.add_vertex(vertex, type=vertex_type)
graph.add_edges(edges_to_add)
vertices_to_add = []
edges_to_add = []
types_of_vertices_to_add = []
else:
# Add symmetric edges & nodes (if they don't already exist in the network)
for author in authors:
graph.add_edges_from([(author, paper_index), (paper_index, author)])
graph.add_edges_from([(conference, paper_index), (paper_index, conference)])
for term in terms:
graph.add_edges_from([(term, paper_index), (paper_index, term)])
# Output progress
papers_processed += 1
if papers_processed % 10 == 0:
sys.stdout.write("\r Processed %d / %d papers..." % (papers_processed, VALID_PAPERS))
sys.stdout.flush()
# Basic statistics about cleanliness of data
count_and_percent_of_papers = lambda a: (a, total_papers, 100 * float(a) / total_papers)
print "\n\nTotal Papers: %d" % total_papers
print " Added (Successful): %d / %d (%2.2f%%)" % count_and_percent_of_papers(successful_papers)
print " Ignored (Bad Title): %d / %d (%2.2f%%)" % count_and_percent_of_papers(skipped_bad_title)
print " Skipped (Missing Conference): %d / %d (%2.2f%%)" % count_and_percent_of_papers(skipped_missing_conference)
print " Invalid (Unknown): %d / %d (%2.2f%%)\n\n" % count_and_percent_of_papers(invalid_papers)
# Rewind file
input_file.seek(beginning)
print "Parsing citations for graph..."
# Counts for statistics
papers_processed = 0
successful_citations = 0
omitted_paper_citations = 0
invalid_paper_citations = 0
invalid_citations = 0
# Cache for citations to add (batch graph actions for igraph)
citations_to_add = []
# Add citations to the graph
for citing_id, citations in __citations_from_file(input_file, should_profile):
#.........这里部分代码省略.........