Skip to content

Commit 832fbab

Browse files
authored
Ensure that the interpolation domain is sorted (#421)
* Ensure that the interpolation domain is sorted * Add special treatment for RegularizationSmooth We have 3 arguments here and the 3rd one has to be sorted instead of the second. * More descriptive error message * t should be monotonically increasing * add more user error tests
1 parent 44f2343 commit 832fbab

4 files changed

+28
-4
lines changed

ext/DataInterpolationsRegularizationToolsExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Abstrac
7676
cache_parameters::Bool = false)
7777
extrapolation_left, extrapolation_right = munge_extrapolation(
7878
extrapolation, extrapolation_left, extrapolation_right)
79-
u, t = munge_data(u, t)
79+
u, t = munge_data(u, t; check_sorted = t̂, sorted_arg_name = ("third", ""))
8080
M = _mapping_matrix(t̂, t)
8181
Wls½ = LA.diagm(sqrt.(wls))
8282
Wr½ = LA.diagm(sqrt.(wr))
@@ -139,7 +139,7 @@ function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Abstrac
139139
cache_parameters::Bool = false)
140140
extrapolation_left, extrapolation_right = munge_extrapolation(
141141
extrapolation, extrapolation_left, extrapolation_right)
142-
u, t = munge_data(u, t)
142+
u, t = munge_data(u, t; check_sorted = t̂, sorted_arg_name = ("third", ""))
143143
N, N̂ = length(t), length(t̂)
144144
M = _mapping_matrix(t̂, t)
145145
Wls½ = Array{Float64}(LA.I, N, N)
@@ -176,7 +176,7 @@ function RegularizationSmooth(u::AbstractVector, t::AbstractVector, t̂::Abstrac
176176
cache_parameters::Bool = false)
177177
extrapolation_left, extrapolation_right = munge_extrapolation(
178178
extrapolation, extrapolation_left, extrapolation_right)
179-
u, t = munge_data(u, t)
179+
u, t = munge_data(u, t; check_sorted = t̂, sorted_arg_name = ("third", ""))
180180
N, N̂ = length(t), length(t̂)
181181
M = _mapping_matrix(t̂, t)
182182
Wls½ = LA.diagm(sqrt.(wls))

src/interpolation_utils.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,26 @@ function quadratic_spline_params(t::AbstractVector, sc::AbstractVector)
9191
end
9292

9393
# helper function for data manipulation
94-
function munge_data(u::AbstractVector, t::AbstractVector)
94+
function munge_data(u::AbstractVector, t::AbstractVector;
95+
check_sorted = t, sorted_arg_name = ("second", "t"))
9596
Tu = nonmissingtype(eltype(u))
9697
Tt = nonmissingtype(eltype(t))
98+
9799
if Tu === eltype(u) && Tt === eltype(t)
100+
if !issorted(check_sorted)
101+
# there is likely an user error
102+
msg = "The $(sorted_arg_name[1]) argument (`$(sorted_arg_name[2])`), which is used for the interpolation domain, is not sorted."
103+
if issorted(u)
104+
msg *= "\nIt looks like the arguments `u` and `$(sorted_arg_name[2])` were inversed, make sure you used the arguments in the correct order."
105+
end
106+
throw(ArgumentError(msg))
107+
end
108+
98109
return u, t
99110
end
100111

101112
@assert length(t) == length(u)
113+
102114
non_missing_mask = map((ui, ti) -> !ismissing(ui) && !ismissing(ti), u, t)
103115
u = convert(AbstractVector{Tu}, u[non_missing_mask])
104116
t = convert(AbstractVector{Tt}, t[non_missing_mask])

test/interpolation_tests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,3 +1105,8 @@ f_cubic_spline = c -> square(CubicSpline, c)
11051105
end
11061106
end
11071107
end
1108+
1109+
@testset "user error" begin
1110+
@test_throws ArgumentError LinearInterpolation(rand(10), rand(10))
1111+
@test_throws ArgumentError LinearInterpolation(0:10, rand(10))
1112+
end

test/regularization.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,10 @@ end
191191
A = RegularizationSmooth(uₒ, tₒ; alg = :fixed)
192192
@test @inferred(A(1.0)) == A(1.0)
193193
end
194+
195+
@testset "User error" begin
196+
@test_throws ArgumentError RegularizationSmooth(tₒ, uₒ; alg = :fixed)
197+
= 20
198+
= collect(range(xmin, xmin + xspan, length = N̂))
199+
@test_throws ArgumentError RegularizationSmooth(u, t̂, t)
200+
end

0 commit comments

Comments
 (0)