本文整理汇总了Python中igraph.Graph.to_directed方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.to_directed方法的具体用法?Python Graph.to_directed怎么用?Python Graph.to_directed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类igraph.Graph
的用法示例。
在下文中一共展示了Graph.to_directed方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ArgumentSet
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_directed [as 别名]
class ArgumentSet(object):
"""
An ``ArgumentSet`` is modeled as a dependency graph where vertices represent
the components of an argument. A vertex corresponding to the conclusion
of an argument *A* will **depend on** the premises and exceptions in *A*.
The graph is built using the `igraph <http://igraph.org/>`_ library. This
allows *attributes* to be associated with both vertices and edges.
Attributes are represented as Python dictionaries where the key (which
must be a string) is the name of the attribute and the value is the
attribute itself. For more details, see the
`igraph tutorial\
<http://igraph.org/python/doc/tutorial/tutorial.html#setting-and-retrieving-attributes>`_.
"""
def __init__(self):
self.graph = Graph()
self.graph.to_directed()
self.arg_count = 1
self.arguments = []
def propset(self):
"""
The set of :class:`PropLiteral`\ s represented by the vertices in
the graph.
Retrieving this set relies on the fact that :meth:`add_proposition`
sets a value for the ``prop`` attribute in vertices created when a
new proposition is added to the graph.
"""
g = self.graph
props = set()
try:
props = {p for p in g.vs['prop']}
except KeyError:
pass
return props
def add_proposition(self, proposition):
"""
Add a proposition to a graph if it is not already present as a vertex.
:param proposition: The proposition to be added to the graph.
:type proposition: :class:`PropLiteral`
:return: The graph vertex corresponding to the proposition.
:rtype: :class:`Graph.Vertex`
:raises TypeError: if the input is not a :class:`PropLiteral`.
"""
if isinstance(proposition, PropLiteral):
if proposition in self.propset():
logging.debug("Proposition '{}' is already in graph".\
format(proposition))
else:
# add the proposition as a vertex attribute, recovered via the
# key 'prop'
self.graph.add_vertex(prop=proposition)
logging.debug("Added proposition '{}' to graph".\
format(proposition))
return self.graph.vs.select(prop=proposition)[0]
else:
raise TypeError('Input {} should be PropLiteral'.\
format(proposition))
def add_argument(self, argument, arg_id=None):
"""
Add an argument to the graph.
:parameter argument: The argument to be added to the graph.
:type argument: :class:`Argument`
:parameter arg_id: The ID of the argument
:type arg_id: str or None
"""
g = self.graph
if arg_id is not None:
argument.arg_id = arg_id
else:
argument.arg_id = 'arg{}'.format(self.arg_count)
self.arg_count += 1
self.arguments.append(argument)
# add the arg_id as a vertex attribute, recovered via the 'arg' key
self.graph.add_vertex(arg=argument.arg_id)
arg_v = g.vs.select(arg=argument.arg_id)[0]
# add proposition vertices to the graph
conclusion_v = self.add_proposition(argument.conclusion)
self.add_proposition(argument.conclusion.negate())
premise_vs =\
[self.add_proposition(prop) for prop in sorted(argument.premises)]
exception_vs =\
[self.add_proposition(prop) for prop in sorted(argument.exceptions)]
target_vs = premise_vs + exception_vs
# add new edges to the graph
edge_to_arg = [(conclusion_v.index, arg_v.index)]
edges_from_arg = [(arg_v.index, target.index) for target in target_vs]
g.add_edges(edge_to_arg + edges_from_arg)
def get_arguments(self, proposition):
#.........这里部分代码省略.........
示例2: Graph
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_directed [as 别名]
from igraph import Graph
foster = Graph().LCF(90, [17, -9, 37, -37, 9, -17], 15)
foster.to_directed()
print "Is Directed? " + str(foster.is_directed())
for start in range(0, 90):
for end in range(0, 90):
# Don't delete this. Delete opposite direction edge
if start + 1 == end:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != 1:
foster.delete_edges([opposite_ID])
else:
opposite_ID = foster.get_eid(end, start, True, False)
if opposite_ID != -1:
current_ID = foster.get_eid(start, end, True, False)
if current_ID != -1:
foster.delete_edges([current_ID])
print "Number of Edges: " + str(len(foster.get_edgelist()))
print (foster.is_connected())
foster_list = foster.get_adjacency()
for sublist in foster_list:
sublist = map(str, sublist)
sublist_string = " ".join(sublist)
print (sublist_string)
示例3: contributor_network
# 需要导入模块: from igraph import Graph [as 别名]
# 或者: from igraph.Graph import to_directed [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'))
#.........这里部分代码省略.........