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

ataxis should return valid indices

Open Petr-Hlavenka opened this issue 7 years ago • 1 comments

I find the ataxis function very convenient. I'd like the function to be always safe to use:

A = AxisArray(collect(1:11), Axis{:time}(0:0.1:1));
A[-Inf .. 0.5] #works and indices are clamped correctly
A[atindex(-0.2..0.2, 5)] #works but one has to be careful not to set the index too low or high to hit the boundary
A[atindex(-0.2..0.2, 1)] #BoundsError: attempt to access 11-element Array{Int64,1} at index [[-1, 0, 1, 2, 3]] 

It would be great if the indexing with atindex would clamp the indices the same way as for A[-Inf .. 0.5]

Also, i'd like to have a function that would be used like this:

A[atrange(-0.2..0.2, 0.4)] # would return the same subarray as A[atindex(-0.2..0.2, 5)]
#now I go with the "naive implementation" 
srange(a, width, value; kw...) = atindex(-width..width, AxisArrays.to_index(a, atvalue(value,kw...))[1])

Unfortunately, one ends up inside @generated function to_index(A::AxisArray{T,N,D,Ax}, I...) where {T,N,D,Ax} that is too meta for me to get a clue what it does, nor fix the issue myself. Petr

Petr-Hlavenka avatar Jan 03 '18 12:01 Petr-Hlavenka

Ah, interesting! Thanks for the report. For my own purposes, I really have valued the immediate error there, especially since the nonscalar case (like atindex(-0.2 .. 0.2, [1, 5])) would try to construct a matrix with missing data.

I think your second request could be implemented as a method of the new atvalue function.

mbauman avatar Jan 03 '18 18:01 mbauman