rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
281 stars 41 forks source link

Subsetting repeated dimensions #430

Closed ivirshup closed 9 months ago

ivirshup commented 1 year ago

When indexing into an array with repeated dimension names, only the first dimension is indexed. E.g.

julia> using DimensionalData
julia> A = rand(1:100, X(5), X(5))
5×5 DimArray{Int64,2} with dimensions: X, X
 19  80  85  12  28
  8  59  98  39  40
 14  87  62  69  55
 95  31  94  54  81
 17  84  87  13  69

julia> A[X(3:5)]
3×5 DimArray{Int64,2} with dimensions: X, X
 14  87  62  69  55
 95  31  94  54  81
 17  84  87  13  69

In a previous version, this would have indexed both X dimensions, see: https://github.com/rafaqz/DimensionalData.jl/issues/46#issue-557264711. This is the behavior I would have expected when order of indexing statements doesn't matter.

Is this change intentional?

(also hey, hope your pandemic went alright Raf!)

rafaqz commented 1 year ago

Hey good to hear from you, and you too! (In Denmark these days, almost forgotten about the pandemic now)

Sorry about that it's an unintentional change, so a bug. Maybe there is no test for it.

rafaqz commented 1 year ago

Now that I'm running your example... I think it was maybe a bug before? With most other operations the doubled dimension is just accessed by order.

This works:

A[X(3:5), X(3:5)]

And is maybe better? but it's up for debate. I think these behaviors are the result of lower level generalisation in sortdims or some other primitive method.

rafaqz commented 9 months ago
using DimensionalData
A = rand(1:100, X(5), X(5))

Just using the same dimension twice works, I think its better for that to be explicit anyway:

julia> A[X(3:5), X(3:5)]
3×3 DimArray{Int64,2} with dimensions: X, X
 57  60  26
 44  95  54
  8  53  39