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

Compatibility with DataInterpolations.jl

Open astrobc1 opened this issue 3 years ago • 2 comments

I’m trying to use cubic spline interpolation from DataInterpolations.jl in my loss function with ModelingToolkit.jl. I’ve tried with and without the various collects. Should this already be supported?

ModelingToolkit v8.13.1 DataInterpolations v3.9.2 Julia 1.7.2

julia> using ModelingToolkit, DataInterpolations

julia> n=10
10

julia> @parameters x[1:n] data[1:n]
2-element Vector{Symbolics.Arr{Num, 1}}:
 x[1:10]
 data[1:10]

julia> function interp(x, y, xnew)
                  itp = CubicSpline(y, x)
                  return itp.(xnew)
              end
interp (generic function with 27 methods)

julia> @register interp(a, b, c)

julia> rms(x, y) = sqrt(sum((x .- y).^2) / length(x))
rms (generic function with 1 method)

julia> @variables c0 c1
2-element Vector{Num}:
 c0
 c1

julia> model(x, c0, c1) = @. c0 + c1 * x
model (generic function with 1 method)

julia> loss = rms(collect(data), interp(collect(x), model(collect(x), c0, c1), collect(x)))
ERROR: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
 [1] lu!(A::LinearAlgebra.Tridiagonal{Num, Vector{Num}}, pivot::LinearAlgebra.RowMaximum; check::Bool)
   @ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/lu.jl:515
 [2] lu(A::LinearAlgebra.Tridiagonal{Num, Vector{Num}}, pivot::LinearAlgebra.RowMaximum; check::Bool)
   @ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/lu.jl:279
 [3] lu (repeats 2 times)
   @ /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/lu.jl:278 [inlined]
 [4] \(A::LinearAlgebra.Tridiagonal{Num, Vector{Num}}, B::Vector{Real})
   @ LinearAlgebra /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl:1142
 [5] CubicSpline(u::Vector{Num}, t::Vector{Num})
   @ DataInterpolations ~/.julia/packages/DataInterpolations/iEA7Y/src/interpolation_caches.jl:156
 [6] interp(x::Vector{Num}, y::Vector{Num}, xnew::Vector{Num})
   @ Main ./REPL[16]:2
 [7] top-level scope
   @ REPL[21]:1

astrobc1 avatar May 28 '22 22:05 astrobc1

What version of DataInterpolations? Show ]st.

https://github.com/PumasAI/DataInterpolations.jl/commit/eb81f66b7ab007d5fcee25abc3d9b92c02e8c4d9#diff-ec9d52d592efcfac03054654f1ddb93fa39df2988f98c3fd169c7f6c40c0ad1b

@YingboMa do you know why that wouldn't catch it?

ChrisRackauckas avatar Jun 01 '22 00:06 ChrisRackauckas

For completeness:

(@v1.7) pkg> st
      Status `~/.julia/environments/v1.7/Project.toml`
  [0bf59076] AdvancedHMC v0.3.5
  [7d9fca2a] Arpack v0.5.3
  [5c4adb95] AstroAngles v0.1.3
  [c7932e45] AstroLib v0.4.1
  [c61b5328] AstroTime v0.7.0
  [488c2830] BSplines v0.3.3
  [4c5d9882] BasicBSpline v0.6.2
  [6e4b80f9] BenchmarkTools v1.3.1
  [a134a8b2] BlackBoxOptim v0.6.1
  [336ed68f] CSV v0.10.4
  [49dc2e85] Calculus v0.5.1
  [8f4d0f93] Conda v1.7.0
  [9c784101] CubicSplines v0.2.1
  [717857b8] DSP v0.7.5
  [a93c6f00] DataFrames v1.3.4
  [82cc6244] DataInterpolations v3.9.2
  [864edb3b] DataStructures v0.18.12
  [39dd38d3] Dierckx v0.5.2
  [2b5f629d] DiffEqBase v6.87.0
  [0c46a032] DifferentialEquations v7.1.0
  [fb2f92b1] EchelleBase v0.1.0 `../../../Development/EchelleBase#master`
  [6e4c0b07] EchelleReduce v0.1.0 `../../../Development/EchelleReduce#master`
  [21191324] EchelleSpectralModeling v0.1.0 `../../../Development/EchelleSpectralModeling#master`
  [28528f50] EchelleSpectrographs v0.1.0 `../../../Development/EchelleSpectrographs#master`
  [cfc395e8] ExtendableGrids v0.9.6
  [7a1cc6ca] FFTW v1.4.6
  [525bcba6] FITSIO v0.16.12
  [5789e2e9] FileIO v1.14.0
  [587475ba] Flux v0.13.1
  [f6369f11] ForwardDiff v0.10.30
  [08766a1d] GalacticFlux v0.1.0
  [792012fa] GalacticNLopt v0.1.0
  [a75be94c] GalacticOptim v3.3.0
  [9d3c5eb1] GalacticOptimJL v0.1.0
  [c27321d9] Glob v1.3.0
  [916415d5] Images v0.25.2
  [5903a43b] Infiltrator v1.4.0
  [a98d9a8b] Interpolations v0.13.6
  [b6b21f68] Ipopt v1.0.2
  [033835bb] JLD2 v0.4.22
  [4076af6c] JuMP v1.0.0
  [fc60dff9] LombScargle v1.0.3
  [bdcacae8] LoopVectorization v0.12.110
  [2fda8390] LsqFit v0.12.1
  [33e6dc65] MKL v0.5.0
  [ee78f7c6] Makie v0.16.6
  [961ee093] ModelingToolkit v8.13.1
  [6f286f6a] MultivariateStats v0.9.0
  [76087f3c] NLopt v0.6.5
  [15e1cf62] NPZ v0.4.2
  [77ba4419] NaNMath v0.3.7
  [b946abbf] NaNStatistics v0.6.11
  [429524aa] Optim v1.7.0
  [18e31ff7] Peaks v0.4.0
  [58dd65bb] Plotly v0.4.1
  [f0f68f2c] PlotlyJS v0.18.8
  [91a5bcdd] Plots v1.29.0
  [c3e4b0f8] Pluto v0.19.5
  [f27b6e38] Polynomials v2.0.25
  [438e738f] PyCall v1.93.1
  [d330b81b] PyPlot v2.10.0
  [189a3867] Reexport v1.2.2
  [ae029012] Requires v1.3.0
  [295af30f] Revise v3.3.3
  [ebc72ef8] SciPy v0.1.1
  [fc659fc5] SkyCoords v1.0.0
  [928aab9d] SpecialMatrices v2.0.0
  [90137ffa] StaticArrays v1.4.4
  [2913bbd2] StatsBase v0.33.16
  [0c5d862f] Symbolics v4.6.0
  [bc48ee85] Tullio v0.3.4
  [1986cc42] Unitful v1.11.0
  [6112ee07] UnitfulAstro v1.1.1
  [276b4fcb] WGLMakie v0.5.5
  [e88e6eb3] Zygote v0.6.40
  [8ba89e20] Distributed
  [37e2e46d] LinearAlgebra
  [44cfe95a] Pkg

astrobc1 avatar Jun 01 '22 01:06 astrobc1

Compatability with DataInterpolations is now done through an extension.

ChrisRackauckas avatar Feb 22 '24 16:02 ChrisRackauckas