def _build_graph(self, target, commands, outs): import networkx from dvc.stage import Stage from dvc.repo.graph import get_pipeline target_stage = Stage.load(self.repo, target) G = get_pipeline(self.repo.pipelines, target_stage) nodes = set() for stage in networkx.dfs_preorder_nodes(G, target_stage): if commands: if stage.cmd is None: continue nodes.add(stage.cmd) elif outs: for out in stage.outs: nodes.add(str(out)) else: nodes.add(stage.relpath) edges = [] for from_stage, to_stage in networkx.edge_dfs(G, target_stage): if commands: if to_stage.cmd is None: continue edges.append((from_stage.cmd, to_stage.cmd)) elif outs: for from_out in from_stage.outs: for to_out in to_stage.outs: edges.append((str(from_out), str(to_out))) else: edges.append((from_stage.relpath, to_stage.relpath)) return list(nodes), edges, networkx.is_tree(G)
def _build_graph(self, target, commands=False, outs=False): import networkx from dvc import dvcfile from dvc.repo.graph import get_pipeline from dvc.utils import parse_target path, name = parse_target(target) target_stage = dvcfile.Dvcfile(self.repo, path).stages[name] G = get_pipeline(self.repo.pipelines, target_stage) nodes = set() for stage in networkx.dfs_preorder_nodes(G, target_stage): if commands: if stage.cmd is None: continue nodes.add(stage.cmd) elif not outs: nodes.add(stage.addressing) edges = [] for from_stage, to_stage in networkx.edge_dfs(G, target_stage): if commands: if to_stage.cmd is None: continue edges.append((from_stage.cmd, to_stage.cmd)) elif not outs: edges.append((from_stage.addressing, to_stage.addressing)) if outs: nodes, edges = self._build_output_graph(G, target_stage) return list(nodes), edges, networkx.is_tree(G)
def _build(G, target=None, full=False, outs=False): import networkx as nx from dvc.repo.graph import get_pipeline, get_pipelines if target: H = get_pipeline(get_pipelines(G), target) if not full: descendants = nx.descendants(G, target) descendants.add(target) H.remove_nodes_from(set(G.nodes()) - descendants) else: H = G if outs: G = nx.DiGraph() for stage in H.nodes: G.add_nodes_from(stage.outs) for from_stage, to_stage in nx.edge_dfs(H): G.add_edges_from([(from_out, to_out) for from_out in from_stage.outs for to_out in to_stage.outs]) H = G def _relabel(node): from dvc.stage import Stage return node.addressing if isinstance(node, Stage) else str(node) return nx.relabel_nodes(H, _relabel, copy=False)
def _build(G, target=None, full=False): import networkx as nx from dvc.repo.graph import get_pipeline, get_pipelines if target: H = get_pipeline(get_pipelines(G), target) if not full: descendants = nx.descendants(G, target) descendants.add(target) H.remove_nodes_from(set(G.nodes()) - descendants) else: H = G def _relabel(stage): return stage.addressing return nx.relabel_nodes(H, _relabel, copy=False)
def __build_graph(self, target, commands, outs): import networkx from dvc.stage import Stage from dvc.repo.graph import get_pipeline stage = Stage.load(self.repo, target) node = relpath(stage.path, self.repo.root_dir) G = get_pipeline(self.repo.pipelines, node) stages = networkx.get_node_attributes(G, "stage") nodes = [] for n in G: stage = stages[n] if commands: if stage.cmd is None: continue nodes.append(stage.cmd) elif outs: for out in stage.outs: nodes.append(str(out)) else: nodes.append(stage.relpath) edges = [] for e in G.edges(): from_stage = stages[e[0]] to_stage = stages[e[1]] if commands: if to_stage.cmd is None: continue edges.append((from_stage.cmd, to_stage.cmd)) elif outs: for from_out in from_stage.outs: for to_out in to_stage.outs: edges.append((str(from_out), str(to_out))) else: edges.append((from_stage.relpath, to_stage.relpath)) return nodes, edges, networkx.is_tree(G)