Skip to content

Commit

Permalink
Make gradient fixes (#446)
Browse files Browse the repository at this point in the history
*Add ColorVectorSpace for tests, resolve dependency issues
* Rectify inference
* Add tests
* Squash commits
* Fix etp issue

* # This is a combination of many commits.tree 0af115e8aa7f73b5fdfa80b70dc80da2c334b556
parent 5465957
author SomTambe <[email protected]> 1627561237 +0530
committer SomTambe <[email protected]> 1627561307 +0530
  • Loading branch information
SomTambe authored Jul 29, 2021
1 parent 617a666 commit 31f6d54
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
ColorVectorSpace = "c3611d14-8923-5661-9e6a-0046d554d3a4"

[targets]
test = ["OffsetArrays", "Unitful", "SharedArrays", "ForwardDiff", "LinearAlgebra", "DualNumbers", "Random", "Test", "Zygote"]
test = ["OffsetArrays", "Unitful", "SharedArrays", "ForwardDiff", "LinearAlgebra", "DualNumbers", "Random", "Test", "Zygote", "ColorVectorSpace"]
5 changes: 3 additions & 2 deletions src/extrapolation/filled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ end
return ret
end

@inline function Interpolations.gradient(etp::FilledExtrapolation{T}, x::Vararg{Number,N}) where {T,N}
@inline function Interpolations.gradient(etp::FilledExtrapolation{T, N, ITP}, x::Vararg{Number, N}) where {T,N,ITP}
itp = parent(etp)
check_type() = Base._return_type(gradient, Tuple{ITP, map(typeof, x)...})
if checkbounds(Bool, itp, x...)
gradient(itp, x...)
else
ptype = promote_type(eltype(itp), map(typeof, x)...)
ptype = eltype(check_type())
return zeros(SVector{ndims(etp), ptype})
end
end
Expand Down
14 changes: 10 additions & 4 deletions test/gradient.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Test, Interpolations, DualNumbers, LinearAlgebra
using Test, Interpolations, DualNumbers, LinearAlgebra, ColorVectorSpace
using ColorVectorSpace: RGB, Gray, N0f8, Colorant

@testset "Gradients" begin
nx = 10
Expand Down Expand Up @@ -204,15 +205,20 @@ using Test, Interpolations, DualNumbers, LinearAlgebra
# out of bounds extrapolation check where fillvalue=constant
dims = [1, 2, 3]
@testset "Constant Fillvalue" begin
dtypes = [Float32, Float64]
dtypes = [Float32, Float64, RGB{Float64}, RGB{Float32}, RGB{N0f8}, Gray{Float64}, Gray{N0f8}]
for dt in dtypes
for dm in dims
itp = interpolate(rand(dt, ntuple(_ -> 5, dm)...), BSpline(Linear()))
etp = extrapolate(itp, rand(dt)) # TODO: Add tests for different boundary conditions in extrapolate
etp = extrapolate(itp, rand(dt))
cs = ntuple(_->3, dm)
@test @inferred(Interpolations.gradient(etp, cs...)) == @inferred(Interpolations.gradient(itp, cs...))
for ds in [ntuple(_->-0.5, dm), ntuple(_->5.5, dm), ntuple(_->6, dm)]
@test all(iszero, @inferred(Interpolations.gradient(etp, ds...)))
if dt <: Colorant
@test eltype(Interpolations.gradient(etp, ds...)) <: Colorant
else
@test eltype(Interpolations.gradient(etp, ds...)) <: AbstractFloat
end
end
end
end
Expand All @@ -222,7 +228,7 @@ using Test, Interpolations, DualNumbers, LinearAlgebra
for dm in dims
for bc in BC
itp = interpolate(rand(5, 5), BSpline(Linear()))
etp = extrapolate(itp, bc()) # TODO: Add tests for different boundary conditions in extrapolate
etp = extrapolate(itp, bc())
cs = ntuple(_->3, 2)
@test @inferred(Interpolations.gradient(etp, cs...)) == @inferred(Interpolations.gradient(itp, cs...))
for ds in [ntuple(_->-0.5, 2), ntuple(_->5.5, 2), ntuple(_->6, 2)]
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if !isdefined(Main, :InterpolationTestUtils)
@eval using Main.InterpolationTestUtils
end

using Test, SharedArrays, Random
using Test, SharedArrays, Random, ColorVectorSpace
using StaticArrays, WoodburyMatrices
ambs = detect_ambiguities(StaticArrays, WoodburyMatrices, Base, Core)

Expand Down

0 comments on commit 31f6d54

Please sign in to comment.