Skip to content

Commit f8cd41d

Browse files
committed
simplified AxisCollection._flat_lookup by using np.unravel_index
1 parent 83d9adc commit f8cd41d

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

larray/core/axis.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,14 +3171,12 @@ def _flat_lookup(self, flat_indices):
31713171
from larray.core.array import aslarray, LArray, stack
31723172

31733173
flat_indices = aslarray(flat_indices)
3174-
shape = self.shape
3175-
divisors = np.roll(np.cumprod(shape[::-1])[::-1], -1)
3176-
divisors[-1] = 1
3177-
axes_indices = [(flat_indices // div) % length for div, length in zip(divisors, shape)]
3174+
axes_indices = np.unravel_index(flat_indices, self.shape)
31783175
# This could return an LArray with object dtype because axes labels can have different types (but not length)
31793176
# TODO: this should be:
31803177
# return stack([(axis.name, axis.i[inds]) for axis, inds in zip(axes, axes_indices)], axis='axis')
3181-
return stack([(axis.name, LArray(axis.labels[inds], inds.axes)) for axis, inds in zip(self, axes_indices)],
3178+
flat_axes = flat_indices.axes
3179+
return stack([(axis.name, LArray(axis.labels[inds], flat_axes)) for axis, inds in zip(self, axes_indices)],
31823180
axis='axis')
31833181

31843182
def _adv_keys_to_combined_axis_la_keys(self, key, wildcard=False, sep='_'):

0 commit comments

Comments
 (0)