Skip to content

Commit

Permalink
Increase code coverage (#206)
Browse files Browse the repository at this point in the history
* Remove more conditionals based on package version

* Remove dead code

* Exclude test folder from codecov: Lines including `@testset` don't get recognized as run, reducing coverage metric by a lot.

* Update DataInterpolations compat entry
  • Loading branch information
adrhill authored Oct 9, 2024
1 parent ff9461a commit 5dfb8af
Showing 13 changed files with 170 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ jobs:
JULIA_SCT_TEST_GROUP: ${{ matrix.group }}
- uses: julia-actions/julia-processcoverage@v1
with:
directories: src,ext,test,benchmark
directories: src,ext,benchmark
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ SparseConnectivityTracerSpecialFunctionsExt = "SpecialFunctions"

[compat]
ADTypes = "1"
DataInterpolations = "6.2"
DataInterpolations = "6.4.2"
DocStringExtensions = "0.9"
FillArrays = "1"
LinearAlgebra = "<0.0.1, 1"
10 changes: 4 additions & 6 deletions src/overloads/arrays.jl
Original file line number Diff line number Diff line change
@@ -204,10 +204,8 @@ end
# On Tracers, `iszero` and `!iszero` don't return a boolean,
# but we need a function that does to handle the structure of the array.

if VERSION >= v"1.9" # _iszero was added in JuliaSparse/SparseArrays.jl#177
SparseArrays._iszero(t::AbstractTracer) = isemptytracer(t)
SparseArrays._iszero(d::Dual) = isemptytracer(tracer(d)) && iszero(primal(d))
SparseArrays._iszero(t::AbstractTracer) = isemptytracer(t)
SparseArrays._iszero(d::Dual) = isemptytracer(tracer(d)) && iszero(primal(d))

SparseArrays._isnotzero(t::AbstractTracer) = !isemptytracer(t)
SparseArrays._isnotzero(d::Dual) = !isemptytracer(tracer(d)) || !iszero(primal(d))
end
SparseArrays._isnotzero(t::AbstractTracer) = !isemptytracer(t)
SparseArrays._isnotzero(d::Dual) = !isemptytracer(tracer(d)) || !iszero(primal(d))
7 changes: 0 additions & 7 deletions src/overloads/gradient_tracer.jl
Original file line number Diff line number Diff line change
@@ -10,14 +10,7 @@ function gradient_tracer_1_to_1(t::T, is_der1_zero::Bool) where {T<:GradientTrac
end
end

function gradient_tracer_1_to_1_inner(
p::P, is_der1_zero::Bool
) where {P<:IndexSetGradientPattern}
return P(gradient_tracer_1_to_1_inner(gradient(p), is_der1_zero)) # return pattern
end

# This is only required because it is called by HessianTracer with IndexSetHessianPattern
# Otherwise, we would just have the method on IndexSetGradientPattern above.
function gradient_tracer_1_to_1_inner(
s::S, is_der1_zero::Bool
) where {S<:AbstractSet{<:Integer}}
101 changes: 49 additions & 52 deletions src/overloads/ifelse_global.jl
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
@static if VERSION >= v"1.8"
function Base.ifelse(::AbstractTracer, x, y)
size(x) != size(y) && throw(
DimensionMismatch(
"Output sizes of x and y in `ifelse(condition, x, y)` don't match in size.",
),
)
return output_union(x, y)
end
function Base.ifelse(::AbstractTracer, x, y)
size(x) != size(y) && throw(
DimensionMismatch(
"Output sizes of x and y in `ifelse(condition, x, y)` don't match in size."
),
)
return output_union(x, y)
end

## output union on scalar outputs
function output_union(tx::T, ty::T) where {T<:GradientTracer}
return T(output_union(pattern(tx), pattern(ty))) # return tracer
end
function output_union(px::P, py::P) where {P<:IndexSetGradientPattern}
return P(union(gradient(px), gradient(py))) # return pattern
end
## output union on scalar outputs
function output_union(tx::T, ty::T) where {T<:GradientTracer}
return T(output_union(pattern(tx), pattern(ty))) # return tracer
end
function output_union(px::P, py::P) where {P<:IndexSetGradientPattern}
return P(union(gradient(px), gradient(py))) # return pattern
end

function output_union(tx::T, ty::T) where {T<:HessianTracer}
return T(output_union(pattern(tx), pattern(ty))) # return tracer
end
function output_union(px::P, py::P) where {P<:AbstractHessianPattern}
return output_union(px, py, shared(P)) # return pattern
end
function output_union(px::P, py::P, ::Shared) where {P<:AbstractHessianPattern}
g_out = union(gradient(px), gradient(py))
hx, hy = hessian(px), hessian(py)
hx !== hy && error("Expected shared Hessians, got $hx, $hy.")
return P(g_out, hx) # return pattern
end
function output_union(tx::T, ty::T) where {T<:HessianTracer}
return T(output_union(pattern(tx), pattern(ty))) # return tracer
end
function output_union(px::P, py::P) where {P<:AbstractHessianPattern}
return output_union(px, py, shared(P)) # return pattern
end
function output_union(px::P, py::P, ::Shared) where {P<:AbstractHessianPattern}
g_out = union(gradient(px), gradient(py))
hx, hy = hessian(px), hessian(py)
hx !== hy && error("Expected shared Hessians, got $hx, $hy.")
return P(g_out, hx) # return pattern
end

function output_union(px::P, py::P, ::NotShared) where {P<:IndexSetHessianPattern}
g_out = union(gradient(px), gradient(py))
h_out = union(hessian(px), hessian(py))
return P(g_out, h_out) # return pattern
end
function output_union(px::P, py::P, ::NotShared) where {P<:DictHessianPattern}
g_out = union(gradient(px), gradient(py))
h_out = myunion!(deepcopy(hessian(px)), hessian(py))
return P(g_out, h_out) # return pattern
end
function output_union(px::P, py::P, ::NotShared) where {P<:IndexSetHessianPattern}
g_out = union(gradient(px), gradient(py))
h_out = union(hessian(px), hessian(py))
return P(g_out, h_out) # return pattern
end
function output_union(px::P, py::P, ::NotShared) where {P<:DictHessianPattern}
g_out = union(gradient(px), gradient(py))
h_out = myunion!(deepcopy(hessian(px)), hessian(py))
return P(g_out, h_out) # return pattern
end

output_union(tx::AbstractTracer, y) = tx
output_union(x, ty::AbstractTracer) = ty
output_union(tx::AbstractTracer, y) = tx
output_union(x, ty::AbstractTracer) = ty

## output union on AbstractArray outputs
function output_union(
tx::AbstractArray{T}, ty::AbstractArray{T}
) where {T<:AbstractTracer}
return output_union.(tx, ty)
end
function output_union(tx::AbstractArray{T}, y::AbstractArray) where {T<:AbstractTracer}
return tx
end
function output_union(x::AbstractArray, ty::AbstractArray{T}) where {T<:AbstractTracer}
return ty
end
## output union on AbstractArray outputs
# TODO: add test
function output_union(tx::AbstractArray{T}, ty::AbstractArray{T}) where {T<:AbstractTracer}
return output_union.(tx, ty)
end
function output_union(tx::AbstractArray{T}, y::AbstractArray) where {T<:AbstractTracer}
return tx
end
function output_union(x::AbstractArray, ty::AbstractArray{T}) where {T<:AbstractTracer}
return ty
end
11 changes: 0 additions & 11 deletions src/tracers.jl
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@ end

GradientTracer{P}(::Real) where {P} = myempty(GradientTracer{P})
GradientTracer{P}(t::GradientTracer{P}) where {P} = t
GradientTracer(t::GradientTracer) = t

isemptytracer(t::GradientTracer) = t.isempty
pattern(t::GradientTracer) = t.pattern
@@ -70,7 +69,6 @@ end

HessianTracer{P}(::Real) where {P} = myempty(HessianTracer{P})
HessianTracer{P}(t::HessianTracer{P}) where {P} = t
HessianTracer(t::HessianTracer) = t

isemptytracer(t::HessianTracer) = t.isempty
pattern(t::HessianTracer) = t.pattern
@@ -122,8 +120,6 @@ end

shared(::Type{T}) where {P,T<:HessianTracer{P}} = shared(P)

myempty(::T) where {T<:AbstractTracer} = myempty(T)

myempty(::Type{GradientTracer{P}}) where {P} = GradientTracer{P}(myempty(P), true)
myempty(::Type{HessianTracer{P}}) where {P} = HessianTracer{P}(myempty(P), true)

@@ -146,10 +142,3 @@ function create_tracers(
tracers = create_tracers(T, xs, indices)
return D.(xs, tracers)
end

# Pretty-printing of Dual tracers
name(::Type{T}) where {T<:GradientTracer} = "GradientTracer"
name(::Type{T}) where {T<:HessianTracer} = "HessianTracer"
name(::Type{D}) where {P,T,D<:Dual{P,T}} = "Dual-$(name(T))"
name(::T) where {T<:AbstractTracer} = name(T)
name(::D) where {D<:Dual} = name(D)
2 changes: 1 addition & 1 deletion test/ext/test_DataInterpolations.jl
Original file line number Diff line number Diff line change
@@ -260,7 +260,7 @@ for N in (2, 5)
InterpolationTest(
N, ConstantInterpolation(um, t); is_der1_zero=true, is_der2_zero=true
),
# InterpolationTest(N, LinearInterpolation(um, t); is_der2_zero=true), # TODO: include once https://github.com/SciML/DataInterpolations.jl/pull/335 is settled
InterpolationTest(N, LinearInterpolation(um, t); is_der2_zero=true),
InterpolationTest(N, QuadraticInterpolation(um, t)),
InterpolationTest(N, LagrangeInterpolation(um, t)),
## The following interpolations appear to not be supported on N dimensions as of DataInterpolations v6.2.0:
18 changes: 7 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -18,11 +18,9 @@ GROUP = get(ENV, "JULIA_SCT_TEST_GROUP", "Core")

@testset verbose = true "SparseConnectivityTracer.jl" begin
if GROUP in ("Core", "All")
if VERSION >= v"1.10"
@testset verbose = true "Linting" begin
@info "Testing linting..."
include("linting.jl")
end
@testset verbose = true "Linting" begin
@info "Testing linting..."
include("linting.jl")
end
end
if GROUP in ("Core", "All")
@@ -78,12 +76,10 @@ GROUP = get(ENV, "JULIA_SCT_TEST_GROUP", "Core")
end
end
# Some extensions are only loaded in newer Julia releases
if VERSION >= v"1.10"
for ext in (:DataInterpolations,)
@testset "$ext" begin
@info "...$ext"
include("ext/test_$ext.jl")
end
for ext in (:DataInterpolations,)
@testset "$ext" begin
@info "...$ext"
include("ext/test_$ext.jl")
end
end
end
Loading

0 comments on commit 5dfb8af

Please sign in to comment.