Skip to content

Commit fe90bd4

Browse files
committed
implemented AxisCollection.iter_labels
1 parent 09d45ba commit fe90bd4

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Searching
186186
AxisCollection.translate_full_key
187187
AxisCollection.axis_id
188188
AxisCollection.ids
189+
AxisCollection.iter_labels
189190

190191
Modifying/Selecting
191192
-------------------

doc/source/changes/version_0_30.rst.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ New features
138138

139139
* implemented :py:obj:`AxisCollection.rename()` to rename axes of an AxisCollection, independently of any array.
140140

141+
* implemented :py:obj:`AxisCollection.iter_labels()` to iterate over all (possible combinations of) labels of the axes
142+
of the collection.
143+
141144

142145
Miscellaneous improvements
143146
^^^^^^^^^^^^^^^^^^^^^^^^^^

larray/core/axis.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
_range_to_slice, _seq_group_to_name, _translate_group_key_hdf, remove_nested_groups)
1717
from larray.util.oset import *
1818
from larray.util.misc import (basestring, PY2, unicode, long, duplicates, array_lookup2, ReprString, index_by_id,
19-
renamed_to, common_type, LHDFStore, lazy_attribute, _isnoneslice, unique_multi)
19+
renamed_to, common_type, LHDFStore, lazy_attribute, _isnoneslice, unique_multi, Product)
2020

2121

2222
class Axis(ABCAxis):
@@ -1472,6 +1472,73 @@ def __dir__(self):
14721472
def __iter__(self):
14731473
return iter(self._list)
14741474

1475+
# TODO: move a few doctests to unit tests
1476+
def iter_labels(self, axes=None, ascending=True):
1477+
r"""Returns a view of the axes labels.
1478+
1479+
Parameters
1480+
----------
1481+
axes : int, str or Axis or tuple of them, optional
1482+
Axis or axes along which to iterate and in which order. Defaults to None (all axes in the order they are
1483+
in the collection).
1484+
ascending : bool, optional
1485+
Whether or not to iterate the axes in ascending order (from start to end). Defaults to True.
1486+
1487+
Returns
1488+
-------
1489+
Sequence
1490+
An object you can iterate (loop) on and index by position.
1491+
1492+
Examples
1493+
--------
1494+
1495+
>>> from larray import ndtest
1496+
>>> axes = ndtest((2, 2)).axes
1497+
>>> axes
1498+
AxisCollection([
1499+
Axis(['a0', 'a1'], 'a'),
1500+
Axis(['b0', 'b1'], 'b')
1501+
])
1502+
>>> axes.iter_labels()[0]
1503+
(a.i[0], b.i[0])
1504+
>>> for index in axes.iter_labels():
1505+
... print(index)
1506+
(a.i[0], b.i[0])
1507+
(a.i[0], b.i[1])
1508+
(a.i[1], b.i[0])
1509+
(a.i[1], b.i[1])
1510+
>>> axes.iter_labels(ascending=False)[0]
1511+
(a.i[1], b.i[1])
1512+
>>> for index in axes.iter_labels(ascending=False):
1513+
... print(index)
1514+
(a.i[1], b.i[1])
1515+
(a.i[1], b.i[0])
1516+
(a.i[0], b.i[1])
1517+
(a.i[0], b.i[0])
1518+
>>> axes.iter_labels(('b', 'a'))[0]
1519+
(b.i[0], a.i[0])
1520+
>>> for index in axes.iter_labels(('b', 'a')):
1521+
... print(index)
1522+
(b.i[0], a.i[0])
1523+
(b.i[0], a.i[1])
1524+
(b.i[1], a.i[0])
1525+
(b.i[1], a.i[1])
1526+
>>> axes.iter_labels('b')[0]
1527+
(b.i[0],)
1528+
>>> for index in axes.iter_labels('b'):
1529+
... print(index)
1530+
(b.i[0],)
1531+
(b.i[1],)
1532+
"""
1533+
axes = self if axes is None else self[axes]
1534+
if not isinstance(axes, AxisCollection):
1535+
axes = (axes,)
1536+
# we need .i because Product uses len and [] on axes and not iter; and [] creates LGroup and not IGroup
1537+
p = Product([axis.i for axis in axes])
1538+
if not ascending:
1539+
p = p[::-1]
1540+
return p
1541+
14751542
def __getattr__(self, key):
14761543
try:
14771544
return self._map[key]

0 commit comments

Comments
 (0)