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

Remove unnecessary helpers #9

Merged
merged 1 commit into from
Jun 20, 2024
Merged
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: 3 additions & 22 deletions src/cyclebane/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
from __future__ import annotations

from collections.abc import Generator, Hashable, Iterable
from collections.abc import Hashable, Iterable
from dataclasses import dataclass
from typing import Any
from uuid import uuid4
Expand Down Expand Up @@ -55,12 +55,6 @@ class IndexValues:
axes: tuple[IndexName, ...]
values: tuple[IndexValue, ...]

@staticmethod
def from_tuple(t: tuple[tuple[IndexName, IndexValue], ...]) -> IndexValues:
names = tuple(name for name, _ in t)
values = tuple(value for _, value in t)
return IndexValues(axes=names, values=values)

def to_tuple(self) -> tuple[tuple[IndexName, IndexValue], ...]:
return tuple(zip(self.axes, self.values, strict=True))

Expand Down Expand Up @@ -142,19 +136,6 @@ def _rename_successors(
return nx.relabel_nodes(graph, renamed_nodes, copy=True)


def _yield_index(
indices: list[tuple[IndexName, Iterable[IndexValue]]],
) -> Generator[tuple[tuple[IndexName, IndexValue], ...], None, None]:
"""Given a multi-dimensional index, yield all possible combinations."""
name, index = indices[0]
for index_value in index:
if len(indices) == 1:
yield ((name, index_value),)
else:
for rest in _yield_index(indices[1:]):
yield ((name, index_value), *rest)


class PositionalIndexer:
"""
Helper class to allow slicing a named dim of a graph using positional indexing.
Expand Down Expand Up @@ -377,9 +358,9 @@ def to_networkx(self, value_attr: str = 'value') -> nx.DiGraph:
# Make a copy for each index value
graphs = [
_rename_successors(
graph, successors=nodes, index=IndexValues.from_tuple(i)
graph, successors=nodes, index=IndexValues((index_name,), (i,))
)
for i in _yield_index([(index_name, index)])
for i in index
]
graph = nx.compose_all(graphs)
# Replace all MappingNodes with their name
Expand Down