AxisKeys.jl icon indicating copy to clipboard operation
AxisKeys.jl copied to clipboard

indexed assignment does not work with dimension name specification

Open takbal opened this issue 4 years ago • 2 comments

Indexing by dimension names does not seem to work for LHS assignments:

julia> K = wrapdims(rand(2,2), x=1:2, y=1:2);
julia> K[x=1] .= 0
ERROR: MethodError: no method matching dotview(::KeyedArray{Float64,2,NamedDimsArray{(:x, :y),Float64,2,Array{Float64,2}},Tuple{UnitRange{Int64},UnitRange{Int64}}}; x=1)
Closest candidates are:
  dotview(::KeyedArray, ::Any...) at /home/takbal/.julia/packages/AxisKeys/G3Okw/src/struct.jl:137 got unsupported keyword argument "x"
  dotview(::Any...) at broadcast.jl:1160 got unsupported keyword argument "x"
  dotview(::BitArray, ::BitArray) at broadcast.jl:1130 got unsupported keyword argument "x"
  ...
Stacktrace:
 [1] top-level scope at REPL[182]:1

It works for NamedDims though:

julia> parent(K)[x=1] .= 0
2-element NamedDimsArray(view(::Array{Float64,2}, 1, :), (:y,)):
↓ y  0.0
     0.0

Looks like the reason is that the Base.dotview() specialization does not pass keyword arguments upwards.

takbal avatar May 29 '21 10:05 takbal

That looks like a bug, indeed. I think there ought to be a method Base.dotview(A::KaNda; kw...) which looks up the names.

mcabbott avatar May 29 '21 14:05 mcabbott

I believe the same goes for setindex!()

takbal avatar Jun 03 '21 21:06 takbal