You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Indexing sometimes treats booleans as indices #3909
Integer-array indexing is along a single dim (which we allow to be omitted, just like when indexing with a single int). Boolean-array indexing is different as one needs to have an exact match of the indexed array (dims and shape). We do not want to support deducing dims as that would be error prone, e.g., when you have a NxN array, going against a core concept of Scipp.
You argue for deducing the dim it in 1D, but I think discriminating from integer-array indexing via the dtype is a dangerous idea. You might similarly suggest to discriminate between positional and label-based indexing by looking if an index matches the dtype of a coord. For example, while in the code you wrote above it is obvious the the indices are booleans, as soon as they originate from a helper function it will be much less obvious.
As you have found, interpreting boolean as integer-indices is also surprising (and thus error prone). Given that there are likely few genuine cases where someone wants to index with boolean we can consider raising an exception when a bool dtype shows up in the integer-array indexing methods.
If we change this to support lower-dim masks, we should also support deducing the dim for 1D arrays.
I think discriminating from integer-array indexing via the dtype is a dangerous idea.
Isn't this exactly what we do? The dtype (and shape) completely change the meaning of the operation. I think this this is just a point I raised a while back that overloading __getitem__ as heavily as we do is confusing.
We do not want to support deducing dims as that would be error prone, e.g., when you have a NxN array, going against a core concept of Scip
You might similarly suggest to discriminate between positional and label-based indexing by looking if an index matches the dtype of a coord.
Neither of those are what I suggested and they don't follow from this.
I think discriminating from integer-array indexing via the dtype is a dangerous idea.
Isn't this exactly what we do? The dtype (and shape) completely change the meaning of the operation. I think this this is just a point I raised a while back that overloading __getitem__ as heavily as we do is confusing.
I can't think of a case where we discriminate based on dtype in scipp? The type of the index is used, but the dtype?
I can't think of a case where we discriminate based on dtype in scipp? The type of the index is used, but the dtype?
Right, I thought a[sc.array(dims=['x'], values=[1, 0])] would be allowed. But it isn't.
But isn't it strange that a[var] and a['x', var] mean different things while a[int] and a['x', int] are the same (for 1D)?
But isn't it strange that a[var] and a['x', var] mean different things
You mean boolean indexing vs. label-based indexing? Yes, we have likely overloaded __getitem__ to much.
But are we forced to live with that decision or can we still improve the interface? Why can't we support lower-dimensional bool arrays and simply broadcast to the shape of the array? Or support a['x', mask]?
But are we forced to live with that decision or can we still improve the interface?
Difficult to remove existing overloads without break many user's scripts or workflows.
Maybe we are broadcasting already, I can't remember?
Or support a['x', mask]
So you'd write some code that
Checks if mask has dtype=bool
Checks if there is an 'x' coord
If mask is 1D or higher, interpret it as a boolean mask.
If mask is 0D and there is a coord, do label-based indexing
If mask is 0D and there is no coord, broadcast
Or alternatively not support label-based indexing with a bool coord?
In either case this seems odd and highly error prone (many tests and docs needed?). I would still argue for simply raising an exception when attempting to do integer-array-indexing with dtype=bool index arrays or lists.
For data arrays, indexing with a sc.Variable is treated as label-based indexing regardless of dimensions. For variables, it is treated as indexing by an index list.
Activity
SimonHeybrock commentedon Jun 19, 2026
Integer-array indexing is along a single dim (which we allow to be omitted, just like when indexing with a single int). Boolean-array indexing is different as one needs to have an exact match of the indexed array (dims and shape). We do not want to support deducing dims as that would be error prone, e.g., when you have a NxN array, going against a core concept of Scipp.
You argue for deducing the dim it in 1D, but I think discriminating from integer-array indexing via the dtype is a dangerous idea. You might similarly suggest to discriminate between positional and label-based indexing by looking if an index matches the dtype of a coord. For example, while in the code you wrote above it is obvious the the indices are booleans, as soon as they originate from a helper function it will be much less obvious.
As you have found, interpreting boolean as integer-indices is also surprising (and thus error prone). Given that there are likely few genuine cases where someone wants to index with boolean we can consider raising an exception when a bool dtype shows up in the integer-array indexing methods.
jl-wynen commentedon Jun 22, 2026
Why, though? Numpy doesn't have that restriction:
If we change this to support lower-dim masks, we should also support deducing the dim for 1D arrays.
Isn't this exactly what we do? The dtype (and shape) completely change the meaning of the operation. I think this this is just a point I raised a while back that overloading
__getitem__as heavily as we do is confusing.Neither of those are what I suggested and they don't follow from this.
SimonHeybrock commentedon Jun 22, 2026
I can't think of a case where we discriminate based on dtype in scipp? The type of the index is used, but the dtype?
jl-wynen commentedon Jun 22, 2026
Right, I thought
a[sc.array(dims=['x'], values=[1, 0])]would be allowed. But it isn't.But isn't it strange that
a[var]anda['x', var]mean different things whilea[int]anda['x', int]are the same (for 1D)?SimonHeybrock commentedon Jun 22, 2026
You mean boolean indexing vs. label-based indexing? Yes, we have likely overloaded
__getitem__to much.jl-wynen commentedon Jun 23, 2026
But are we forced to live with that decision or can we still improve the interface? Why can't we support lower-dimensional bool arrays and simply broadcast to the shape of the array? Or support
a['x', mask]?SimonHeybrock commentedon Jun 23, 2026
Difficult to remove existing overloads without break many user's scripts or workflows.
Maybe we are broadcasting already, I can't remember?
So you'd write some code that
maskhas dtype=bool'x'coordmaskis 1D or higher, interpret it as a boolean mask.maskis 0D and there is a coord, do label-based indexingmaskis 0D and there is no coord, broadcastOr alternatively not support label-based indexing with a
boolcoord?In either case this seems odd and highly error prone (many tests and docs needed?). I would still argue for simply raising an exception when attempting to do integer-array-indexing with
dtype=boolindex arrays or lists.jl-wynen commentedon Jun 23, 2026
I just tested something. We treat array-indexing inconsistently:
For data arrays, indexing with a
sc.Variableis treated as label-based indexing regardless of dimensions. For variables, it is treated as indexing by an index list.SimonHeybrock commentedon Jun 23, 2026
Seems like a bug. Is it some pybind11 conversion path or do we actually support variables for integer-array indexing on the C++ side?
jl-wynen commentedon Jun 23, 2026
It calls this overload:
[](T &self, std::tuple<std::string, std::vector<scipp::index>> indices) { ... }So pybind11 seems to convert the index variable to a vector, including the dtype.