Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better document floating point indexing #40

Open
Tokazama opened this issue Aug 11, 2020 · 2 comments
Open

Better document floating point indexing #40

Tokazama opened this issue Aug 11, 2020 · 2 comments
Assignees

Comments

@Tokazama
Copy link
Owner

This came up as part of #35.

You might consider revising some of the documentation, though. I haven't seen anything wrong, but it doesn't reinforce the fact that indexing by float will be error-prone. For example, the Axis documentation includes some indexing by equality to a float. If you change time1 to be time1 = Axis((1.5:.1:10)s), then time1[1.7s] errors, as does time1[==(1.7s)], both of which are nearly the same as the current examples using 1.5s.

https://tokazama.github.io/AxisIndices.jl/stable/axis/

@Tokazama
Copy link
Owner Author

Tokazama commented Nov 6, 2020

Hey @grahamas, master now has axis[closest(val)] which finds the closest key in axis to val for indexing. Let me know your thoughts and if this is helpful.

@grahamas
Copy link

grahamas commented Jun 11, 2021

Hey @Tokazama , sorry I didn't see this. This seems like a great solution, but it doesn't work in a case I'd expect:

A = AxisArray(3:6, 0.3:0.1:0.6)
A[closest(0.6)]

edit: though it looks like this is just a bug? It also doesn't work for Axis objects.

AxisIndices.Axis(0.3:0.1:0.6)[0.6 |> closest]
ERROR: MethodError: no method matching one(::Nothing)
Closest candidates are:
  one(::Union{Type{T}, T}) where T<:AbstractString at strings/basic.jl:262
  one(::Union{Type{P}, P}) where P<:Dates.Period at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Dates/src/periods.jl:54
  one(::NamedDims.NamedDimsArray{L, T, N, A} where {T, N, A<:AbstractArray{T, N}}, ::Any...) where L at /home/graham/.julia/packages/NamedDims/OBqS7/src/functions.jl:99
  ...
Stacktrace:
 [1] oneunit(x::Nothing)
   @ Base ./number.jl:318
 [2] _closest_number_forward(collection::StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}, x::Float64)
   @ AxisIndices ~/.julia/packages/AxisIndices/Eb16j/src/closest.jl:31
 [3] closest_number
   @ ~/.julia/packages/AxisIndices/Eb16j/src/closest.jl:18 [inlined]
 [4] closest
   @ ~/.julia/packages/AxisIndices/Eb16j/src/closest.jl:10 [inlined]
 [5] Fix2
   @ ./operators.jl:1002 [inlined]
 [6] to_index(#unused#::AxisIndices.IndexAxis, axis::AxisIndices.Axis{Float64, Int64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}, SimpleAxis{Int64, ArrayInterface.OptionallyStaticUnitRange{ArrayInterface.StaticInt{1}, Int64}}}, arg::Base.Fix2{typeof(closest), Float64})
   @ AxisIndices ~/.julia/packages/AxisIndices/Eb16j/src/closest.jl:69
 [7] to_index
   @ ~/.julia/packages/ArrayInterface/hIHGL/src/indexing.jl:283 [inlined]
 [8] getindex(axis::AxisIndices.Axis{Float64, Int64, StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}, SimpleAxis{Int64, ArrayInterface.OptionallyStaticUnitRange{ArrayInterface.StaticInt{1}, Int64}}}, arg::Function)
   @ AxisIndices ~/.julia/packages/AxisIndices/Eb16j/src/getindex.jl:15
 [9] top-level scope
   @ REPL[7]:1

@Tokazama Tokazama self-assigned this Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants