rafaqz / DimensionalData.jl

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

eachslice errors when `dims=()` on v1.9 #481

Closed sethaxen closed 9 months ago

sethaxen commented 1 year ago

on v1.9.0-rc2:

julia> x = randn(10);

julia> da = DimArray(x, X);

julia> eachslice(x; dims=())
0-dimensional Slices{Vector{Float64}, Tuple{Colon}, Tuple{}, SubArray{Float64, 1, Vector{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}}, true}, 0}:
[0.7891813730345938, -1.3764708206873746, -0.5592354054654276, -1.399151200456184, 1.0245087542948947, 1.0028814175526604, 1.5265892145440452, -0.3877364838824226, 0.1777686084271991, 1.5604282747062839]

julia> eachslice(da; dims=())
ERROR: MethodError: no method matching hasdim(::DimArray{Float64, 1, Tuple{X{DimensionalData.Dimensions.LookupArrays.NoLookup{Base.OneTo{Int64}}}}, Tuple{}, Vector{Float64}, DimensionalData.NoName, DimensionalData.Dimensions.LookupArrays.NoMetadata})

Closest candidates are:
  hasdim(::Any, ::Any, ::Any...)
   @ DimensionalData ~/.julia/packages/DimensionalData/7iYMt/src/Dimensions/primitives.jl:248

Stacktrace:
 [1] #eachslice#92
   @ ~/.julia/packages/DimensionalData/7iYMt/src/array/methods.jl:124 [inlined]
 [2] top-level scope
   @ REPL[7]:1
rafaqz commented 1 year ago

This is easy to fix for DimArray because it uses Slices, but hits problems with DimStack because we cant index DimIndices with [].

So this leads to a larger question: should indexing with [] be the same as [:, :, ...] for all AbstractDimArray? It would be consistent with how colons are filled in for missing dimensions, but maybe a bit weird for people that unlike base arrays da[] is not an error, but returns a new copy of da - especially when zero dimensional dim arrays will return the value.

@sethaxen I recall you also mentioned this indexing problem in the past. Any thoughts?