本文整理汇总了Python中igraph.Graph.vs方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.vs方法的具体用法?Python Graph.vs怎么用?Python Graph.vs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.vs方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: contributor_network
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import vs [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'))
#.........这里部分代码省略.........
示例2: get_activity_graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import vs [as 别名]
def get_activity_graph(self, episodes):
"""Generates the qstag for a set of input episode QSRs.
:param episodes: list of QSR Epiosdes generated using `compute_episodes(world_qsr)``
:type episodes: list
:return: igraph.Graph: An igraph graph object containing all the object, spatial and temporal nodes.
:rtype: igraph.Graph
"""
temporal_map = {'after': 'before',
'metby' : 'meets',
'overlapped_by': 'overlaps',
'started_by': 'starts',
'contains':'during',
'finished_by': 'finishes'
}
spatial_nodes_edges = {"rcc2": 2, "rcc3": 2, "rcc8": 2, "cardir": 2,
"qtcbs": 2, "qtccs": 2, "qtcbcs": 2, "argd": 2, "argprobd": 2, "mos": 1}
objects = {}
spatial_data = []
vertex_count = 0
graph = Graph(directed=True)
#print("Looping through the episodes...")
for (objs, relations, (intv_start, intv_end)) in episodes:
#print(objs, relations, (intv_start, intv_end))
#############################################
# Object Nodes: #
#############################################
number_of_edges = set([])
for rel in relations.keys():
number_of_edges.add(spatial_nodes_edges[rel])
if len(number_of_edges) != 1:
raise ValueError("QSRs with different spatial node edges selected.")
else:
spatial_edges = number_of_edges.pop()
for o in objs:
if o in objects: continue
graph.add_vertex(o)
objects[o] = vertex_count
if o in self.__object_types:
graph.vs()[vertex_count]['obj_type'] = self.__object_types[o]
graph.vs()[vertex_count]['node_type'] = 'object'
vertex_count += 1
object_ids = []
for o in objs:
object_ids.append(objects[o])
#print(" OBJECT IDS ", object_ids)
#############################################
# Spatial Nodes: #
#############################################
graph.add_vertex(relations)
graph.vs()[vertex_count]['node_type'] = 'spatial_relation'
# Add edges from spatial node to objects
edge_from_object = objects[objs[0]]
#print("adding edge: : ", edge_from_object, vertex_count)
graph.add_edge(edge_from_object, vertex_count)
self.__spatial_obj_edges.append( (edge_from_object, vertex_count) )
if spatial_edges is 2:
#print("TWO OBJECTS -- ")
edge_to_object = objects[objs[1]]
graph.add_edge(vertex_count, edge_to_object)
self.__spatial_obj_edges.append( (vertex_count, edge_to_object) )
elif spatial_edges is 3:
#print("THREE OBJECTS -- ")
edge_from_object_2 = objects[objs[1]]
edge_from_object_3 = objects[objs[2]]
graph.add_edge(edge_from_object_2, vertex_count)
graph.add_edge(edge_from_object_3, vertex_count)
self.__spatial_obj_edges.append( (edge_from_object_2, vertex_count) )
self.__spatial_obj_edges.append( (edge_from_object_3, vertex_count) )
spatial_data.append( (object_ids, vertex_count, (intv_start, intv_end)) )
vertex_count += 1
#############################################
# Temporal Nodes: #
#############################################
#print("data: \n", spatial_data)
#print("objects \n", objects)
(E_s, E_f) = self.get_E_set(objects, spatial_data)
#print("E_s: ", E_s)
#print("E_f: ", E_f)
temporal_vertices = {}
#.........这里部分代码省略.........
示例3: get_activity_graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import vs [as 别名]
def get_activity_graph(self, input_episodes, COLLAPSE_TEMPORAL_NODES):
# Generate activity graph from file with object interaction information
temporal_map = {'after': 'before',
'metby' : 'meets',
'overlapped_by': 'overlaps',
'started_by': 'starts',
'contains':'during',
'finished_by': 'finishes'
}
if type(input_episodes) != list:
# Episodes are given in a file
episodes = self._get_episodes_from_file(input_episodes)
else:
episodes = input_episodes
self.episodes = episodes
objects = {}
data = []
types = []
spatial_rels = {}
vertex_count = 0
graph = Graph(directed=True)
for (o1, o1t, o2, o2t, rel, intv_start, intv_end) in episodes:
# Add objects to the graph
if o1 not in objects:
graph.add_vertex(o1)
objects[o1] = vertex_count
graph.vs()[vertex_count]['obj_type'] = o1t
graph.vs()[vertex_count]['node_type'] = 'object'
vertex_count += 1
if o2 not in objects:
graph.add_vertex(o2)
objects[o2] = vertex_count
graph.vs()[vertex_count]['obj_type'] = o2t
graph.vs()[vertex_count]['node_type'] = 'object'
vertex_count += 1
# Add spatial node to the graph
graph.add_vertex(rel)
graph.vs()[vertex_count]['node_type'] = 'spatial_relation'
# Add edges from spatial node to objects
graph.add_edge(objects[o1], vertex_count)
graph.add_edge(vertex_count, objects[o2])
self.spatial_obj_edges.append((objects[o1], vertex_count))
self.spatial_obj_edges.append((vertex_count, objects[o2]))
data.append((objects[o1], objects[o2], vertex_count, intv_start, intv_end))
vertex_count += 1
(E_s, E_f) = self.get_E_set(objects, data)
temporal_vertices = {}
for ((o11, o12, rel1, s1, e1),(o21, o22, rel2, s2, e2)) in combinations(data,2):
#No temporal relation between two starting episodes or between two final episodes
if ((o11, o12, s1, e1) in E_s and (o21, o22, s2, e2) in E_s) or ((o11, o12, s1, e1) in E_f and (o21, o22, s2, e2) in E_f):
continue
temporal_rel = get_allen_relation(s1, e1, s2, e2)
# If temporal_rel is in temporal_map get its value otherwise keep it the same
# If the edges are directed, then we need to change the direction of the edges
# if we change change the temporal relation to its inverse
temporal_rel_new = temporal_map.get(temporal_rel, temporal_rel)
# Add temporal node to the graph
if COLLAPSE_TEMPORAL_NODES and temporal_rel_new in temporal_vertices:
temporal_rel_vertex_id = temporal_vertices[temporal_rel_new]
else:
graph.add_vertex(temporal_rel_new)
graph.vs()[vertex_count]['node_type'] = 'temporal_relation'
temporal_rel_vertex_id = vertex_count
temporal_vertices[temporal_rel_new] = vertex_count
vertex_count += 1
if temporal_rel_new == temporal_rel:
# Add edges from temporal node to the spatial nodes
graph.add_edge(rel1, temporal_rel_vertex_id)
graph.add_edge(temporal_rel_vertex_id, rel2)
self.temp_spatial_edges.append((rel1, temporal_rel_vertex_id))
self.temp_spatial_edges.append((temporal_rel_vertex_id, rel2))
else:
# PD: if an inverse temporal relation has been used, switch the edges around
graph.add_edge(temporal_rel_vertex_id, rel1)
graph.add_edge(rel2, temporal_rel_vertex_id)
self.temp_spatial_edges.append((temporal_rel_vertex_id, rel1))
self.temp_spatial_edges.append((rel2, temporal_rel_vertex_id))
return graph