Skip to content

Commit 1758892

Browse files
committed
removed yada exceptions (closes #711)
this also slightly simplify _key_to_igroups
1 parent 5cd2e0d commit 1758892

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

larray/core/axis.py

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,19 +2486,6 @@ def _translate_axis_key(self, axis_key):
24862486
if isinstance(key, Group):
24872487
axis_key = key
24882488

2489-
if isinstance(axis_key, ExprNode):
2490-
# FIXME: it is still possible to get here if we use a dict key in getitem !
2491-
raise Exception('yada')
2492-
axis_key = axis_key.evaluate(self)
2493-
2494-
# XXX: this is probably not necessary anymore
2495-
if isinstance(axis_key, LArray) and np.issubdtype(axis_key.dtype, np.bool_):
2496-
raise Exception('yada')
2497-
if len(axis_key.axes) > 1:
2498-
raise ValueError("mixing ND boolean filters with other filters in getitem is not currently supported")
2499-
else:
2500-
return IGroup(axis_key.nonzero()[0], axis=axis_key.axes[0])
2501-
25022489
# translate Axis keys to LGroup keys
25032490
# FIXME: this should be simply:
25042491
# if isinstance(axis_key, Axis):
@@ -2561,47 +2548,44 @@ def _key_to_igroups(self, key):
25612548
"""
25622549
from .array import LArray
25632550

2564-
# convert scalar keys to 1D keys
2565-
if not isinstance(key, (tuple, dict)):
2551+
if isinstance(key, dict):
2552+
# key axes could be strings or axis references and we want real axes
2553+
key = tuple(self[axis][axis_key] for axis, axis_key in key.items())
2554+
elif not isinstance(key, tuple):
2555+
# convert scalar keys to 1D keys
25662556
key = (key,)
25672557

2568-
# always the case except if key is a dict
2569-
if isinstance(key, tuple):
2570-
key = tuple(axis_key.evaluate(self) if isinstance(axis_key, ExprNode) else axis_key
2571-
for axis_key in key)
2572-
2573-
nonboolkey = []
2574-
for axis_key in key:
2575-
if isinstance(axis_key, np.ndarray) and np.issubdtype(axis_key.dtype, np.bool_):
2576-
if axis_key.shape != self.shape:
2577-
raise ValueError("boolean key with a different shape ({}) than array ({})"
2578-
.format(axis_key.shape, self.shape))
2579-
axis_key = LArray(axis_key, self)
2580-
2581-
if isinstance(axis_key, LArray) and np.issubdtype(axis_key.dtype, np.bool_):
2582-
extra_key_axes = axis_key.axes - self
2583-
if extra_key_axes:
2584-
raise ValueError("subset key contains more axes ({}) than array ({})"
2585-
.format(axis_key.axes, self))
2586-
nonboolkey.extend(axis_key.nonzero())
2587-
else:
2588-
nonboolkey.append(axis_key)
2589-
key = tuple(nonboolkey)
2590-
2591-
# drop slice(None) and Ellipsis since they are meaningless because of guess_axis.
2592-
# XXX: we might want to raise an exception when we find Ellipses or (most) slice(None) because except for
2593-
# a single slice(None) a[:], I don't think there is any point.
2594-
key = [axis_key for axis_key in key
2595-
if not _isnoneslice(axis_key) and axis_key is not Ellipsis]
2558+
# handle ExprNode
2559+
key = tuple(axis_key.evaluate(self) if isinstance(axis_key, ExprNode) else axis_key
2560+
for axis_key in key)
2561+
2562+
nonboolkey = []
2563+
for axis_key in key:
2564+
if isinstance(axis_key, np.ndarray) and np.issubdtype(axis_key.dtype, np.bool_):
2565+
if axis_key.shape != self.shape:
2566+
raise ValueError("boolean key with a different shape ({}) than array ({})"
2567+
.format(axis_key.shape, self.shape))
2568+
axis_key = LArray(axis_key, self)
2569+
2570+
if isinstance(axis_key, LArray) and np.issubdtype(axis_key.dtype, np.bool_):
2571+
extra_key_axes = axis_key.axes - self
2572+
if extra_key_axes:
2573+
raise ValueError("subset key contains more axes ({}) than array ({})"
2574+
.format(axis_key.axes, self))
2575+
# nonzero (currently) returns a tuple of IGroups containing 1D LArrays (one IGroup per axis)
2576+
nonboolkey.extend(axis_key.nonzero())
2577+
else:
2578+
nonboolkey.append(axis_key)
2579+
key = tuple(nonboolkey)
25962580

2597-
# translate all keys to IGroup
2598-
return tuple(self._translate_axis_key(axis_key) for axis_key in key)
2599-
else:
2600-
assert isinstance(key, dict)
2581+
# drop slice(None) and Ellipsis since they are meaningless because of guess_axis.
2582+
# XXX: we might want to raise an exception when we find Ellipses or (most) slice(None) because except for
2583+
# a single slice(None) a[:], I don't think there is any point.
2584+
key = [axis_key for axis_key in key
2585+
if not _isnoneslice(axis_key) and axis_key is not Ellipsis]
26012586

2602-
# key axes could be strings or axis references and we want real axes
2603-
return tuple(self._translate_axis_key(self[axis][axis_key])
2604-
for axis, axis_key in key.items())
2587+
# translate all keys to IGroup
2588+
return tuple(self._translate_axis_key(axis_key) for axis_key in key)
26052589

26062590
def _translated_key(self, key):
26072591
"""

0 commit comments

Comments
 (0)