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()
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()
def _compute_dependencies(blocks): ''' Given a sequence of blocks, compute the aggregate inputs, outputs, and dependency graph. Parameters ---------- blocks : List(Block) A list of blocks in order of execution to "tie-up" into a larger, single block. Returns ------- inputs : Set(Str) The input parameters to the new block. outputs : Set(Str) The output parameters to the new block. conditional_outputs : Set(Str) The conditional output parameters to the new block, i.e. names that might or might not be defined by an arbitrary execution of the block. dep_graph : Instance(Graph) The dependency graph (directed, acyclic) relating the blocks from the given sequence: block A depends on block B iff an output from B is used as an input to A. Additionally, the names of the inputs and outputs for the new block are included in the graph to capture their dependency relations to the contained blocks: name X depends on block A iff X is an output of A, and block A depends on name X iff X is an input to A. (Alternative: make each Block track its own dependencies) ''' # Deferred computations deferred = set() # Build dep_graph: a not transitively closed dependency graph that # relates blocks to the blocks and inputs they depend on, and outputs # to the last block that modifies or creates them inputs, outputs, conditional_outputs = set(), set(), set() dep_graph, env = DiGraph(), {} for b in blocks: # 'b' depends on the provider for each of its inputs or, if none # exists, it depends on the inputs themselves (as inputs to the # aggregate block). If a name is provided only conditionally, then # 'b' depends on both the provider and the input. for i in b.inputs: if i in env: dep_graph.add_edge(b, env[i]) if i not in env or i in conditional_outputs: inputs.add(i) dep_graph.add_edge(b, i) for c in b.conditional_outputs: # 'b's outputs depend only on 'b' if c in dep_graph: for n in dep_graph[c]: dep_graph.delete_edge(c, n) dep_graph.add_edge(c, b) # 'b' depends on the provider for each of its conditional # outputs or, if none exists and the end result has an input of # the same name, 'b' depends on that input. (We defer the # latter test to check against the final set of inputs rather # than just the inputs we've found so far.) if c in env: dep_graph.add_edge(b, env[c]) else: def f(b=b, c=c): if c in inputs: dep_graph.add_edge(b, c) deferred.add(f) # 'b' contributes conditional outputs to the aggregate block # unless they are already unconditional if c not in outputs: conditional_outputs.add(c) # 'b' becomes the provider for its conditional outputs env[c] = b for o in b.outputs: # 'b's outputs depend only on 'b' if o in dep_graph: for n in dep_graph[o]: dep_graph.delete_edge(o, n) dep_graph.add_edge(o, b) # 'b' contributes its outputs to the aggregate block -- as # unconditional outputs outputs.add(o) conditional_outputs.discard(o) # 'b' becomes the provider for its outputs env[o] = b # Run deferred computations for f in deferred: f() return inputs, outputs, conditional_outputs, dep_graph