Skip to content

Commit

Permalink
Merge pull request #155 from asmeurer/iter_indices-multi-skip_axes
Browse files Browse the repository at this point in the history
Support multiple skip axes in iter_indices and broadcast_shapes
  • Loading branch information
asmeurer authored Feb 1, 2024
2 parents bdb7edb + 72b71ca commit 2fb49e9
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 377 deletions.
10 changes: 10 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ the index objects.

.. autofunction:: ndindex.broadcast_shapes

Exceptions
==========

These are some custom exceptions that are raised by a few functions in
ndindex. Note that most functions in ndindex will raise `IndexError`
(if the index would be invalid), or `TypeError` or `ValueError` (if the input
types or values are incorrect).

.. autoexception:: ndindex.BroadcastError

.. autoexception:: ndindex.AxisError
Expand Down Expand Up @@ -106,3 +114,5 @@ relied on as they may be removed or changed.
.. autofunction:: ndindex.shapetools.remove_indices

.. autofunction:: ndindex.shapetools.unremove_indices

.. autofunction:: ndindex.shapetools.normalize_skip_axes
5 changes: 2 additions & 3 deletions ndindex/booleanarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def _raise_indexerror(self, shape, axis=0):

for i in range(axis, axis+self.ndim):
if self.shape[i-axis] != 0 and shape[i] != self.shape[i-axis]:

raise IndexError(f"boolean index did not match indexed array along dimension {i}; dimension is {shape[i]} but corresponding boolean dimension is {self.shape[i-axis]}")
raise IndexError(f'boolean index did not match indexed array along axis {i}; size of axis is {shape[i]} but size of corresponding boolean axis is {self.shape[i-axis]}')

def reduce(self, shape=None, *, axis=0, negative_int=False):
"""
Expand All @@ -125,7 +124,7 @@ def reduce(self, shape=None, *, axis=0, negative_int=False):
>>> idx.reduce((3,))
Traceback (most recent call last):
...
IndexError: boolean index did not match indexed array along dimension 0; dimension is 3 but corresponding boolean dimension is 2
IndexError: boolean index did not match indexed array along axis 0; size of axis is 3 but size of corresponding boolean axis is 2
>>> idx.reduce((2,))
BooleanArray([True, False])
Expand Down
11 changes: 9 additions & 2 deletions ndindex/integer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .ndindex import NDIndex, operator_index
from .shapetools import asshape
from .shapetools import AxisError, asshape

class Integer(NDIndex):
"""
Expand Down Expand Up @@ -62,7 +62,7 @@ def _raise_indexerror(self, shape, axis=0):
size = shape[axis]
raise IndexError(f"index {self.raw} is out of bounds for axis {axis} with size {size}")

def reduce(self, shape=None, *, axis=0, negative_int=False):
def reduce(self, shape=None, *, axis=0, negative_int=False, axiserror=False):
"""
Reduce an Integer index on an array of shape `shape`.
Expand Down Expand Up @@ -96,7 +96,14 @@ def reduce(self, shape=None, *, axis=0, negative_int=False):
if shape is None:
return self

if axiserror:
if not isinstance(shape, int): # pragma: no cover
raise TypeError("axiserror=True requires shape to be an integer")
if not self.isvalid(shape):
raise AxisError(self.raw, shape)

shape = asshape(shape, axis=axis)

self._raise_indexerror(shape, axis)

if self.raw < 0 and not negative_int:
Expand Down
Loading

0 comments on commit 2fb49e9

Please sign in to comment.