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

Rename overlapping_axes -> intersect_axes & make overlapping_axes instead the intersection of names #110

Merged
merged 4 commits into from
Sep 8, 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
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ Occasionally, an axis size can be inferred in some circumstances but not others.
::: haliax.axis.axis_name
::: haliax.axis.concat_axes
::: haliax.axis.union_axes
::: haliax.axis.intersect_axes
::: haliax.axis.eliminate_axes
::: haliax.axis.without_axes
::: haliax.axis.overlapping_axes
::: haliax.axis.selects_axis
::: haliax.axis.overlapping_axes
::: haliax.axis.is_axis_compatible


Expand Down
26 changes: 22 additions & 4 deletions src/haliax/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,21 @@ def replace_axis(axis_spec: AxisSelection, old: AxisSelector, new: AxisSelection


@overload
def overlapping_axes(ax1: AxisSpec, ax2: AxisSelection) -> Tuple[Axis, ...]:
def intersect_axes(ax1: AxisSpec, ax2: AxisSelection) -> Tuple[Axis, ...]:
...


@overload
def overlapping_axes(ax1: AxisSelection, ax2: AxisSpec) -> Tuple[Axis, ...]:
def intersect_axes(ax1: AxisSelection, ax2: AxisSpec) -> Tuple[Axis, ...]:
...


@overload
def overlapping_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[AxisSelector, ...]:
def intersect_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[AxisSelector, ...]:
...


def overlapping_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[AxisSelector, ...]:
def intersect_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[AxisSelector, ...]:
"""Returns a tuple of axes that are present in both ax1 and ax2.
The returned order is the same as ax1.
"""
Expand Down Expand Up @@ -339,6 +339,23 @@ def overlapping_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[AxisSelect
return tuple(out)


def overlapping_axes(ax1: AxisSelection, ax2: AxisSelection) -> Tuple[str, ...]:
"""
Like intersect_axes, but returns the names instead of the axes themselves.
Unlike intersect_axes, it does not throw an error if the sizes of a common axis are
different.

The returned order is the same as in ax1.
"""
ax1 = ensure_tuple(ax1)
ax2 = ensure_tuple(ax2)
ax1_names = map(axis_name, ax1)
ax2_names = set(map(axis_name, ax2))

out = tuple(name for name in ax1_names if name in ax2_names)
return out


@overload
def axis_name(ax: AxisSelector) -> str: # type: ignore
...
Expand Down Expand Up @@ -555,6 +572,7 @@ def replace_missing_with_ellipsis(ax1: AxisSelection, ax2: AxisSelection) -> Par
"dslice",
"dblock",
"eliminate_axes",
"intersect_axes",
"is_axis_compatible",
"overlapping_axes",
"replace_axis",
Expand Down
4 changes: 2 additions & 2 deletions src/haliax/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ def take(array: NamedArray, axis: AxisSelector, index: Union[int, NamedArray]) -
remaining_axes = eliminate_axes(array.axes, axis)
# axis order is generally [array.axes[:axis_index], index.axes, array.axes[axis_index + 1 :]]
# except that index.axes may overlap with array.axes
overlapping_axes: AxisSpec = haliax.axis.overlapping_axes(remaining_axes, index.axes)
intersecting_axes: AxisSpec = haliax.axis.intersect_axes(remaining_axes, index.axes)

if overlapping_axes:
if intersecting_axes:
# if the eliminated axis is also in the index, we rename it to a dummy axis that we can broadcast over it
need_to_use_dummy_axis = index._lookup_indices(axis.name) is not None
if need_to_use_dummy_axis:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_axis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from haliax.axis import Axis, eliminate_axes, make_axes, rearrange_for_partial_order
from haliax.axis import Axis, eliminate_axes, make_axes, overlapping_axes, rearrange_for_partial_order


def test_eliminate_axes():
Expand Down Expand Up @@ -133,3 +133,17 @@ def test_duplicate_elements_errors():

with pytest.raises(ValueError):
rearrange_for_partial_order(partial_order, candidates)


def test_overlapping_axes_with_different_sizes():
A1 = Axis("A", 10)
A2 = Axis("A", 12)
B = Axis("B", 14)
C = Axis("C", 16)
D = Axis("D", 18)

ax1 = (A1, B, C)
ax2 = (A2, C, D)

overlapping_names = overlapping_axes(ax1, ax2) # Should not error
assert overlapping_names == ("A", "C")
Loading