本文整理汇总了Python中networkx.DiGraph.predecessors方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.predecessors方法的具体用法?Python DiGraph.predecessors怎么用?Python DiGraph.predecessors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.DiGraph
的用法示例。
在下文中一共展示了DiGraph.predecessors方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import predecessors [as 别名]
def __init__(self, path, version="0"):
g = DiGraph()
gaged_reaches = []
db = openFile(path, "r")
table = db.getNode("/", "networks/network" + str(version))
reaches = {}
# read data out of file
for row in table:
if str(row["ComID"]) != "-1":
reaches[row["ComID"]] = Reach(self, row)
else:
reaches[row["ComID"]] = "-1"
g.add_edge(Reach(self, row), "-1")
if row["MonitoredFlag"] == "1":
gaged_reaches.append(row["ComID"])
db.close()
# make network
for comid in reaches.keys():
to_comID = reaches[comid]._ToComID
if to_comID != "-1":
g.add_edge(reaches[comid], reaches[to_comID])
else:
g.add_edge(reaches[comid], -1)
self._g_unbroken = g.copy()
self._g_unbroken_reverse = self._g_unbroken.reverse()
# break upstream of monitored reaches
for i in gaged_reaches:
if i != "-1":
up = g.predecessors(reaches[i])
for j in up:
if j != "-1":
g.delete_edge(j, reaches[i])
else:
g.delete_edge(j, "-1")
self._g = g
self._g_rev = g.reverse()
self._version = str(version)
self._path = str(path)
self._reaches = reaches
db.close()
示例2: gantt
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import predecessors [as 别名]
def gantt(job_list, context, filename='gantt.html'):
"""
"""
db = context.get_compmake_db()
if not job_list:
# job_list = list(top_targets(db))
job_list = all_jobs(db)
# plus all the jobs that were defined by them
job_list = set(job_list)
# job_list.update(definition_closure(job_list, db))
from networkx import DiGraph
G = DiGraph()
cq = CacheQueryDB(db)
for job_id in job_list:
cache = cq.get_job_cache(job_id)
length = cache.int_make.get_cputime_used()
attr_dict = dict(cache=cache, length=length)
G.add_node(job_id, **attr_dict)
dependencies = cq.direct_children(job_id)
for c in dependencies:
G.add_edge(c, job_id)
defined = cq.jobs_defined(job_id)
for c in defined:
G.add_edge(job_id, c)
order = topological_sort(G)
for job_id in order:
length = G.node[job_id]['length']
pre = list(G.predecessors(job_id))
# print('%s pred %s' % (job_id, pre))
if not pre:
T0 = 0
G.node[job_id]['CP'] = None
else:
# find predecessor with highest T1
import numpy as np
T1s = list(G.node[_]['T1'] for _ in pre)
i = np.argmax(T1s)
T0 = T1s[i]
G.node[job_id]['CP'] = pre[i]
T1 = T0 + length
G.node[job_id]['T0'] = T0
G.node[job_id]['T1'] = T1
G.node[job_id]['critical'] = False
sg_ideal = SimpleGantt()
by_ideal_completion = sorted(order, key=lambda _: G.node[_]['T1'])
last = by_ideal_completion[-1]
path = []
while last is not None:
path.append(last)
G.node[last]['critical'] = True
last = G.node[last]['CP']
print('Critical path:')
for job_id in reversed(path):
length = G.node[job_id]['length']
print('- %.1f s %s' % (length, job_id))
for job_id in by_ideal_completion:
T0 = G.node[job_id]['T0']
T1 = G.node[job_id]['T1']
# length = G.node[job_id]['length']
dependencies = list(G.predecessors(job_id))
# cache = G.node[job_id]['cache']
periods = OrderedDict()
periods['ideal'] = (T0, T1)
critical = G.node[job_id]['critical']
sg_ideal.add_job(job_id, dependencies, periods=periods, critical=critical)
sg_actual = SimpleGantt()
order_actual = sorted(order, key=lambda _: G.node[_]['cache'].int_make.t0)
for job_id in order_actual:
cache = G.node[job_id]['cache']
critical = G.node[job_id]['critical']
dependencies = list(G.predecessors(job_id))
periods = OrderedDict()
periods['make'] = cache.int_make.walltime_interval()
sg_actual.add_job(job_id, dependencies, periods=periods, critical=critical)
sg_actual_detailed = SimpleGantt()
for job_id in order_actual:
cache = G.node[job_id]['cache']
critical = G.node[job_id]['critical']
periods = OrderedDict()
periods['load'] = cache.int_load_results.walltime_interval()
periods['compute'] = cache.int_compute.walltime_interval()
periods['gc'] = cache.int_gc.walltime_interval()
periods['save'] = cache.int_save_results.walltime_interval()
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import predecessors [as 别名]
class DepF :
def __init__(self, field) :
self.__offsets = []
self.__field = field
############ create depency field graph #########
# Initial values to build the graph (depth, width, cycles)
self.__depth = 3
self.__width = 3
self.__cycles = 2
self.__G = DiGraph()
G = self.__G
G.add_node( self._new_node(G) )
# Create randomlu the graph without cycle
self.__random( G, 0 )
# Find the best path to add cycles
d = all_pairs_dijkstra_path_length( G )
l = list( reversed( sorted( d, key=lambda key: len(d[key]) ) ) )
for i in l :
if self.__cycles == 0 :
break
d_tmp = sorted( d[i], key=lambda key: d[i][key] )
G.add_edge( d_tmp[-1], i)
self.__cycles = self.__cycles - 1
print simple_cycles( G )
print G.node
print G.edge
print G.degree()
############ Add field <-> higher #####################################
# field F depends of the higher degree
# F <---> sorted( G.degree(), key=lambda key : G.degree()[key] )[-1]
#######################################################################
degree = G.degree()
high_degree = sorted( degree, key=lambda key : degree[key] )[-1]
# Link our protected field with the node which has the highest degree
G.add_edge( field.get_name(), high_degree )
G.add_edge( high_degree, field.get_name() )
#draw_graphviz(G)
#write_dot(G,'file.dot')
def get_field(self) :
return self.__field
def add_offset(self, idx) :
x = Offset( idx )
self.__offsets.append( x )
return x
def run(self, _vm, _analysis, _vm_generate) :
###############################################################
## dict (method) of list ( offset / list (of instructions) ) #
## - insert an element #
## - modify the offset with the new insertion #
###############################################################
list_OB = {}
############ Create dependencies fields ############
fields = { self.__field.get_name() : Field( _vm, _analysis, _vm_generate, self.__field, self, True ) }
fields[ self.__field.get_name() ].run( self.__G.degree()[ self.__field.get_name() ] )
print self.__field.get_name(), ": REAL_FIELD ->", self.__G.predecessors( self.__field.get_name() ), self.__G.degree()[self.__field.get_name()]
############ Create the name, the initial value and all access of the field ############
for i in self.__G.node :
# We have not yet add this new field
if i not in fields :
print i, "PRE ->", self.__G.predecessors( i ), self.__G.degree()[i]
name, access_flag, descriptor = _analysis.get_like_field()
_vm.insert_field( self.__field.get_class_name(), name, [ access_flag, descriptor ] )
fields[ i ] = Field( _vm, _analysis, _vm_generate, _vm.get_field_descriptor( self.__field.get_class_name(), name, descriptor ), self )
# degree of the field, prefix must be add if the protection is for a real field
fields[ i ].run( self.__G.degree()[i] )
########## Add all fields initialisation into the final list ############
for i in fields :
print "FIELD ->", i,
fields[ i ].show()
x = fields[ i ].insert_init()
if x != None :
try :
list_OB[ x[0] ].append( (x[1], x[2]) )
except KeyError :
list_OB[ x[0] ] = []
list_OB[ x[0] ].append( (x[1], x[2]) )
#.........这里部分代码省略.........
示例4: add_clases
# 需要导入模块: from networkx import DiGraph [as 别名]
# 或者: from networkx.DiGraph import predecessors [as 别名]
def add_clases(self, uml_pool, classes,
detalization_level = 0,
detalization_levels= {},
with_generalizations = True,
with_interfaces = True,
group_interfaces = True,
with_associations = False,
auto_aggregation = False,
forced_relationships = False,
):
"""
@param level detalization level
| level | Description |
|-----------------------------------------------|
| 0 | hide class |
| 1 | details suppressed |
| 2 | analysis level details |
| 3 | implementation level details |
| 4 | maximum details |
@return dictionary with node settings for graphviz
"""
for uml_class in classes:
uml_class = uml_pool.Class[uml_class]
self.add_class(uml_class, detalization_levels.get(uml_class.id, detalization_level))
if with_generalizations and uml_pool:
for uml_relationship in uml_pool.generalizations_iter():
self.add_relationship(uml_relationship, forced_relationships)
if with_associations: pass
if auto_aggregation:
self.extract_aggregations()
self._relationships = []
self.interface_groups = []
if with_interfaces:
if group_interfaces:
from networkx import DiGraph
from sets import Set
graph = DiGraph()
for uml_class in uml_pool.Class.values():
for iname in uml_class.usages:
graph.add_edge(iname, uml_class.id)
for iname in uml_class.realizations:
graph.add_edge(uml_class.id, iname)
inames = uml_pool.Interface.keys()
ivisited = [False]*len(inames)
iface_groups = []
for i1 in xrange(len(inames)-1):
iname1 = inames[i1]
if not ivisited[i1]:
uml_ifaces = Set([iname1])
for i2 in xrange(i1, len(inames)):
iname2 = inames[i2]
if not ivisited[i2] and \
graph.successors(iname1) == graph.successors(iname2) and \
graph.predecessors(iname1) == graph.predecessors(iname2) :
uml_ifaces.add(iname2)
ivisited[i2] = True
iface_groups.append(uml_ifaces)
ivisited[i1] = True
#self.interface_groups = map(lambda group: UMLInterfaceGroup(group, uml_pool), iface_groups)
for group in iface_groups:
iname = list(group)[0]
if True:
# if len(graph.successors(iname)) > 0 and \
# len(graph.predecessors(iname)) > 0:
group_id = len(self.interface_groups)
uml_igroup = UMLInterfaceGroup(group, uml_pool, group_id)
self.interface_groups.append(uml_igroup)
self.add_node(group_id)
for class_id in graph.successors(iname):
self.add_relationship(UMLUsage(uml_igroup, uml_pool.Class[class_id]))
for class_id in graph.predecessors(iname):
self.add_relationship(UMLRealization(uml_pool.Class[class_id], uml_igroup))