本文整理汇总了Python中igraph.Graph.es方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.es方法的具体用法?Python Graph.es怎么用?Python Graph.es使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.es方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contributor_network
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import es [as 别名]
def contributor_network(cls):
'Network graph of authors, editors, translators, and journals'
# NOTE: this is a bit slow to be generating on the fly.
# For now, cache the network after it's generated, but that
# should probably be refined
graph = cache.get(cls.contributor_network_cache_key)
if graph:
logger.debug('Using cached journal contributor network graph')
return graph
graph = Graph()
graph.to_directed() # we want a directed graph
full_start = time.time()
# gather edges in an ordered dict to avoid generating duplicate
# edges, and so edge weights can be added efficiently
# - key is a tuple of source & target nodes, edge label, i.e.
# ((source, target), label)
# - value is the count or weight of that edge
edges = OrderedDict()
# helper method to add edges:
# set count to 1 if not already present; increase count if present
def add_edge(edge):
if edge not in edges:
edges[edge] = 1
else:
edges[edge] += 1
# start = time.time()
# prefetch journal contributors all at once, for efficiency
journals = Journal.objects.all().prefetch_related(
'schools', 'issue_set__editors', 'issue_set__item_set__creators',
'issue_set__item_set__translators')
# NOTE: this query is currently the slowest step in generating the
# graph, nearly ~4s in dev. It can only be timed here if it is
# forced to evaluate via list or similar, but it is slightly more
# efficient not to evaluate it that way
# logger.debug('Retrieved journal contributor data from db in %.2f sec',
# time.time() - start)
for j in journals:
start = time.time()
# starting count, to easily calculate number of nodes & edges added
vtx_count = len(graph.vs())
edge_count = len(edges)
graph.add_vertex(j.network_id, label=unicode(j),
type=j.network_type,
schools=[s.name for s in j.schools.all()])
# journal editors are at the issue level
for issue in j.issue_set.all():
editors = issue.editors.all()
for i, editor in enumerate(editors):
# only add if not already present
if editor.network_id not in graph.vs['name']:
graph.add_vertex(editor.network_id,
type=editor.network_type,
label=editor.firstname_lastname)
add_edge(((editor.network_id, j.network_id), 'editor'))
# add a co-editor rel to any other editors on this issue
for co_editor in editors[i+1:]:
add_edge(((editor.network_id, co_editor.network_id),
'co-editor'))
# authors and translators are at the item level
for item in issue.item_set.all():
authors = item.creators.all()
for i, author in enumerate(authors):
# only add person if not already present in the graph
if author.network_id not in graph.vs['name']:
graph.add_vertex(author.network_id,
label=author.firstname_lastname,
type=author.network_type)
# author is a journal contributor
add_edge(((author.network_id, j.network_id),
'contributor'))
# each author is connected to the issue editors who
# edited their work
for editor in editors:
add_edge(((editor.network_id, author.network_id),
'edited'))
# add a co-author to any other authors on this item
for co_author in authors[i+1:]:
add_edge(((author.network_id, co_author.network_id),
'co-author'))
for translator in item.translators.all():
# only add person if not already present in the graph
if translator.network_id not in graph.vs['name']:
graph.add_vertex(translator.network_id,
label=translator.firstname_lastname,
type=translator.network_type)
# translators are connected to the journal they contributed to
add_edge(((translator.network_id, j.network_id),
'translator'))
#.........这里部分代码省略.........