JuliaMath / Interpolations.jl

Fast, continuous interpolation of discrete datasets in Julia
http://juliamath.github.io/Interpolations.jl/
Other
531 stars 109 forks source link

Support direct use of Date and DateTime types for grid/knots #298

Open CameronBieganek opened 5 years ago

CameronBieganek commented 5 years ago

It would be nice if Date and DateTime objects could be used directly in LinearInterpolation, similar to R's approx. For example, the following does not work:

julia> x = [DateTime(2014, 1, 1, 12), DateTime(2014, 1, 1, 13), DateTime(2014, 1, 1, 14)]
3-element Array{DateTime,1}:
 2014-01-01T12:00:00
 2014-01-01T13:00:00
 2014-01-01T14:00:00

julia> y = [4, 6, 8]
3-element Array{Int64,1}:
 4
 6
 8

julia> interp = LinearInterpolation(x, y)
3-element extrapolate(interpolate((::Array{DateTime,1},), ::Array{Int64,1}, Gridded(Linear())), Throw()) with element type Float64:
Error showing value of type Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}}:
ERROR: MethodError: no method matching (::Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}})(::DateTime)
Use square brackets [] for indexing an Array.
Closest candidates are:
  Extrapolation(::Number...) where {T, N} at /home/cbieganek/.julia/packages/Interpolations/0OnYb/src/extrapolation/extrapolation.jl:41
  Extrapolation(::Union{Number, AbstractArray{T,1} where T}...) where {T, N} at /home/cbieganek/.julia/packages/Interpolations/0OnYb/src/extrapolation/extrapolation.jl:48
Stacktrace:
 [1] (::getfield(Interpolations, Symbol("##46#47")){Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}}})(::Tuple{DateTime}) at ./none:0
 [2] iterate at ./generator.jl:47 [inlined]
 [3] collect at ./array.jl:606 [inlined]
 [4] show_ranged(::IOContext{REPL.Terminals.TTYTerminal}, ::Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}}, ::Tuple{Array{DateTime,1}}) at /home/cbieganek/.julia/packages/Interpolations/0OnYb/src/io.jl:107
 [5] show(::IOContext{REPL.Terminals.TTYTerminal}, ::MIME{Symbol("text/plain")}, ::Interpolations.Extrapolation{Float64,1,Interpolations.GriddedInterpolation{Float64,1,Int64,Gridded{Linear},Tuple{Array{DateTime,1}}},Gridded{Linear},Throw{Nothing}}) at /home/cbieganek/.julia/packages/Interpolations/0OnYb/src/io.jl:117
 [6] display(::REPL.REPLDisplay, ::MIME{Symbol("text/plain")}, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:131
 [7] display(::REPL.REPLDisplay, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:135
 [8] display(::Any) at ./multimedia.jl:287
 [9] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [10] invokelatest at ./essentials.jl:741 [inlined]
 [11] print_response(::IO, ::Any, ::Any, ::Bool, ::Bool, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:155
 [12] print_response(::REPL.AbstractREPL, ::Any, ::Any, ::Bool, ::Bool) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:140
 [13] (::getfield(REPL, Symbol("#do_respond#38")){Bool,getfield(REPL, Symbol("##48#57")){REPL.LineEditREPL,REPL.REPLHistoryProvider},REPL.LineEditREPL,REPL.LineEdit.Prompt})(::Any, ::Any, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:714
 [14] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [15] invokelatest at ./essentials.jl:741 [inlined]
 [16] run_interface(::REPL.Terminals.TextTerminal, ::REPL.LineEdit.ModalInterface, ::REPL.LineEdit.MIState) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/LineEdit.jl:2273
 [17] run_frontend(::REPL.LineEditREPL, ::REPL.REPLBackendRef) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:1035
 [18] run_repl(::REPL.AbstractREPL, ::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:192
 [19] (::getfield(Base, Symbol("##734#736")){Bool,Bool,Bool,Bool})(::Module) at ./client.jl:362
 [20] #invokelatest#1 at ./essentials.jl:742 [inlined]
 [21] invokelatest at ./essentials.jl:741 [inlined]
 [22] run_main_repl(::Bool, ::Bool, ::Bool, ::Bool, ::Bool) at ./client.jl:346
 [23] exec_options(::Base.JLOptions) at ./client.jl:284
 [24] _start() at ./client.jl:436
timholy commented 5 years ago

No doubt, that would be cool.

We're not far from being able to do this; the error you see is actually just in displaying the value of the object you created (try putting ; at the end of that line). But there's more work to be done; if you're sufficiently motivated, try generalizing the Number constraint here: https://github.com/JuliaMath/Interpolations.jl/blob/master/src/gridded/indexing.jl. I'm not certain how best to do that; what we want is a generic concept of a scalar.

hei06j commented 3 years ago

Hi, just wondering if there is any update on this issue? thanks.