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

Fix exception in compute_mapped for partially reducing pipelines #173

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
9 changes: 3 additions & 6 deletions src/sciline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,9 @@ def get_mapped_node_names(
raise ValueError(
f"Multiple mapped nodes with name '{base_name}' found: {candidates}"
)
# Drops unrelated indices
graph = graph[candidates[0]] # type: ignore[index]
indices = graph.indices
if index_names is not None:
indices = {name: indices[name] for name in indices if name in index_names}
index_names = tuple(indices)

index_names = tuple(reversed(candidates[0].indices))
indices = {name: idx for name, idx in graph.indices.items() if name in index_names}

index = pd.MultiIndex.from_product(indices.values(), names=index_names)
keys = tuple(NodeName(base_name, IndexValues(index_names, idx)) for idx in index)
Expand Down
21 changes: 20 additions & 1 deletion tests/pipeline_map_reduce_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,26 @@ def ab_to_c(a: A, b: B) -> C:
sl.compute_mapped(pl, C)


def test_compute_mapped_indices_selects_between_multiple_candidates() -> None:
def test_compute_mapped_with_partial_reduction_identifies_correct_index() -> None:
def ab_to_c(a: A, b: B) -> C:
return C(a + b)

D = NewType('D', int)

pl = sl.Pipeline((ab_to_c,))
paramsA = pd.DataFrame(
{A: [A(10 * i) for i in range(3)]}, index=['a', 'b', 'c']
).rename_axis('x')
paramsB = pd.DataFrame(
{B: [B(i) for i in range(2)]}, index=['aa', 'bb']
).rename_axis('y')
pl = pl.map(paramsA).map(paramsB).reduce(func=max, name=D, index='x')
result = sl.compute_mapped(pl, D)
assert result['aa'] == C(20)
assert result['bb'] == C(21)


def test_compute_mapped_index_names_selects_between_multiple_candidates() -> None:
def ab_to_c(a: A, b: B) -> C:
return C(a + b)

Expand Down