Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prototype] Node grouping #4427

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions kedro/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,31 @@ def grouped_nodes(self) -> list[list[Node]]:

return [list(group) for group in self._toposorted_groups]

@property
def grouped_nodes_by_namespace(self) -> dict[str, dict[str, Any]]:
"""Return a dictionary of the pipeline nodes grouped by namespace with
information about the nodes, their type, and dependencies."""
grouped_nodes: dict[str, dict[str, Any]] = defaultdict(dict)
for node in self.nodes:
key = node.namespace or node.name
if key not in grouped_nodes:
grouped_nodes[key] = {}
grouped_nodes[key]["name"] = key
grouped_nodes[key]["type"] = "namespace" if node.namespace else "node"
grouped_nodes[key]["nodes"] = [*grouped_nodes[key].get("nodes", []), node]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have O(Nˆ2) complexity here, as every time we add a node, we recreate a list based on what was already there. So maybe it makes sense to initialize grouped_nodes[key]["nodes"] and then just append to it.

dependencies = set()
for parent in self.node_dependencies[node]:
if parent.namespace and parent.namespace != key:
dependencies.add(parent.namespace)
elif parent.namespace and parent.namespace == key:
continue
else:
dependencies.add(parent.name)
grouped_nodes[key]["dependencies"] = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here is the same as the above

grouped_nodes[key].get("dependencies", set()) | dependencies
)
return grouped_nodes

def only_nodes(self, *node_names: str) -> Pipeline:
"""Create a new ``Pipeline`` which will contain only the specified
nodes by name.
Expand Down
31 changes: 31 additions & 0 deletions tests/pipeline/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,37 @@ def test_node_dependencies(self, complex_pipeline):
}
assert actual == expected

def test_node_grouping_by_namespace(self):
pipeline = modular_pipeline(
[
node(identity, "A", "B", name="node1", namespace="name_1"),
node(identity, "B", "C", name="node2", namespace="name_1"),
node(identity, "C", "D", name="node3", namespace="name_2"),
node(identity, "D", "E", name="node4", namespace="name_2"),
node(identity, "E", "G", name="node5"),
node(identity, "G", "H", name="node6"),
]
)
grouped = pipeline.grouped_nodes_by_namespace
# Validate keys for namespace groups
for key in ["name_1", "name_2"]:
assert key in grouped
assert grouped[key]["name"] == key
assert grouped[key]["type"] == "namespace"
assert len(grouped[key]["nodes"]) == 2

# Validate dependencies for namespace groups
assert grouped["name_1"]["dependencies"] == set()
assert grouped["name_2"]["dependencies"] == {"name_1"}

# Validate nodes for namespace groups
assert grouped["node5"]["type"] == "node"
assert grouped["node5"]["name"] == "node5"
assert len(grouped["node5"]["nodes"]) == 1
assert grouped["node5"]["dependencies"] == {"name_2"}
# Validate when node depends on node
assert grouped["node6"]["dependencies"] == {"node5"}


@pytest.fixture
def pipeline_with_circle():
Expand Down
Loading