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

Add TaskGraph.keys #126

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions src/sciline/task_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from __future__ import annotations

from html import escape
from typing import Any, Optional, Sequence, Tuple, TypeVar, Union
from typing import Any, Generator, Optional, Sequence, Tuple, TypeVar, Union

from .scheduler import DaskScheduler, NaiveScheduler, Scheduler
from .typing import Graph, Item
from .typing import Graph, Item, Key
from .utils import keyname

T = TypeVar("T")
Expand Down Expand Up @@ -113,6 +113,19 @@ def compute(
else:
return self._scheduler.get(self._graph, [keys])[0]

def keys(self) -> Generator[Key, None, None]:
"""Iterate over all keys of the graph.

Yields all keys, i.e., the types of values that can be computed or are
provided as parameters.
Copy link
Member

Choose a reason for hiding this comment

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

Erm, this is odd, because we have self._keys, and the keys passed in the __init__, with a quite different meaning from what you implemented here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed the args in other methods to targets to match the name in transform_coords.


Returns
-------
:
Iterable over keys.
"""
yield from self._graph.keys()

def visualize(
self, **kwargs: Any
) -> graphviz.Digraph: # type: ignore[name-defined] # noqa: F821
Expand Down
25 changes: 25 additions & 0 deletions tests/task_graph_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
from typing import NewType, TypeVar

import pytest

import sciline as sl
from sciline.task_graph import TaskGraph
from sciline.typing import Graph

A = NewType('A', int)
B = NewType('B', int)
T = TypeVar('T', A, B)


class Str(sl.Scope[T, str], str):
...


def to_string(x: T) -> Str[T]:
return Str[T](str(x))


def repeat(a: A, s: Str[B]) -> list[str]:
return [s] * a


def as_float(x: int) -> float:
return 0.5 * x
Expand Down Expand Up @@ -53,3 +71,10 @@ def test_compute_raises_when_provided_with_key_not_in_graph() -> None:
tg.compute(str)
with pytest.raises(KeyError):
tg.compute((str, float))


def test_keys_iter() -> None:
pl = sl.Pipeline([to_string, repeat], params={A: 3, B: 4})
tg = pl.get(list[str])
assert len(list(tg.keys())) == 4 # there are no duplicates
assert set(tg.keys()) == {A, B, Str[B], list[str]}
Loading