JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 41 forks source link

Function 'atvalue' cannot handle negative ranges #215

Open EminentCoder opened 1 year ago

EminentCoder commented 1 year ago

Dear all,

I recently noticed an inconvenient issue with the atvalue function, namely that it does not work for negatively iterated ranges/units. Here's an example:

using AxisArrays
using Unitful: mm

AA = AxisArray(reshape(1:11^2, (11, 11)),
    Axis{:x}(-5mm:1mm:5mm),     # positive
    Axis{:y}(5mm:-1mm:-5mm))    # negative

# positive direction
println(AA[x = atvalue(1mm)])
# negative direction
println(AA[y = atvalue(1mm)])

While the instantiation of the AxisArray and the first example in the positive direction (x-axis) work perfectly fine, the latter in the negative direction (y-axis) fails and prints the following error message:

ERROR: LoadError: BoundsError: attempt to access 11-element StepRange{Unitful.Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}, Unitful.Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}} at index [TolValue(1 mm, tol=0 mm)]
Stacktrace:
  [1] axisindexes(#unused#::Type{AxisArrays.Dimensional}, ax::StepRange{Unitful.Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}, Unitful.Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}}, idx::AxisArrays.TolValue{Unitful.Quantity{Int64, 𝐋, Unitful.FreeUnits{(mm,), 𝐋, nothing}}})
    @ AxisArrays ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:234
  [2] axisindexes
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:199 [inlined]
  [3] macro expansion
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:394 [inlined]
  [4] _to_index
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:349 [inlined]
  [5] to_index
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:347 [inlined]
  [6] macro expansion
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:153 [inlined]
  [7] to_index
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:141 [inlined]
  [8] getindex
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:123 [inlined]
  [9] #getindex#38
    @ ~/.julia/packages/AxisArrays/v7vf4/src/indexing.jl:164 [inlined]
 [10] top-level scope

I wonder if there is a simple solution to that problem (am I doing something wrong?) or if this undesired behavior needs to be fixed inside of the package.

Kindly, EminentCoder

PS: Unitful does not contribute to the error, the problem remains without the units as well.