Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Jan 23, 2024
1 parent 6c53d05 commit 73c35cb
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ version = "0.1.0"
[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
nlminb_jll = "c04a2911-6870-53d6-aa47-4d7d5aff1028"

Expand All @@ -18,4 +20,3 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]

1 change: 0 additions & 1 deletion src/CurveFit/CurveFit.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
include("DataTypes.jl")
include("init_param.jl")
include("doubleLogistics.jl")

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/VegCurveFit.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module VegCurveFit

using LinearAlgebra
import Libdl
using Libdl
using Parameters
using Dates
using Statistics

include("DataTypes.jl")
include("tools.jl")

include("Optim/Optim.jl")
Expand Down
3 changes: 0 additions & 3 deletions src/smooth_SG/SG.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using LinearAlgebra


multiply_col!(S::AbstractArray{T1,2}, w::AbstractArray{T}) where {
T1<:Real,T<:Real} = begin
nrow, ncol = size(S)
Expand Down
3 changes: 0 additions & 3 deletions src/smooth_SG/SG_helper.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using StaticArrays


function sgmat_S(halfwin::Int=1, d::Int=2)
frame = 2 * halfwin + 1
mat = zeros(Int, frame, d + 1)
Expand Down
3 changes: 3 additions & 0 deletions src/smooth_SG/main_SG.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using LinearAlgebra
using StaticArrays

include("SG_helper.jl")
include("SG.jl")
include("SG_weighted.jl")
Expand Down
File renamed without changes.
18 changes: 4 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
using Test
using VegCurveFit

@testset "nlminb" begin
# Write your own tests here.
start = [0.0] # [1, 2, 3, 4]
lower = [-4.0]
upper = [4.0]

function f2(x::Array{Float64,1})::Float64
x[1] - cos(x[1])
end

f(x) = x[1] - cos(x[1])
r = nlminb(start, f, verbose=false)
@test r["par"][1] == -1.5701006351106073
end
include("test-lambda_init.jl")
include("test-smooth_SG.jl")
include("test-smooth_whit.jl")
include("test-Optim.jl")
14 changes: 14 additions & 0 deletions test/test-Optim.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@testset "nlminb" begin
# Write your own tests here.
start = [0.0] # [1, 2, 3, 4]
lower = [-4.0]
upper = [4.0]

function f2(x::Array{Float64,1})::Float64
x[1] - cos(x[1])
end

f(x) = x[1] - cos(x[1])
r = nlminb(start, f, verbose=false)
@test r["par"][1] == -1.5701006351106073
end
11 changes: 11 additions & 0 deletions test/test-lambda_init.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# using Random

@testset "lambda init" begin
# @examples
# Random.seed!(1234)
y = rand(100)
coef = [0.9809, 0.7247, -2.6752, -0.3854, -0.0604]
lamb = lambda_init(y, coef)
@test lamb > 0
end
# methods(lambda_init)
57 changes: 57 additions & 0 deletions test/test-smooth_SG.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Test

@testset "Savitzky Golay filter" begin
# works
@test wSG([2., 3, 5, 7]; halfwin=1, d=2) [2, 3, 5, 7]

y = 1.0:10 |> collect
@test wSG(y; halfwin=1, d=1) y

# 跟R语言版本基本一致
y = [2.0, 3, 4, 10, 6, 7]
w = [1.0, 1, 1, 0.2, 1, 1]
z = wSG(y, w; halfwin=1, d=1)
@test round.(z, digits=2) [2, 3, 5, 5.45, 7, 6.5]

# wSG对权重调整没有这么敏感
z = wSG(y, w; halfwin=1, d=2)
@test round.(z, digits=2) y
end


@testset "Savitzky Golay filter" begin
y = [1.0, 2, 5, 4, 3, 6]
w = collect(1.0:7)
halfwin = 3
S = sgmat_S(halfwin)
B = sgmat_B(S)
sgmat_wB(S, w)
z = SG(y; halfwin=1)
@test z y
end


@testset "Savitzky Golay filter" begin
y = rand(100)
w = rand(100)
SG(y; halfwin=5)
wSG(y, w; halfwin=5)
end


# using Plots
# p = plot(y)
# plot!(p, z1)
# plot!(p, z2)
# n = Int(1e5)
# y = rand(n)
# using BenchmarkTools

## test the used memory
# @time for i=1:1e3
# z = SG(y)
# end
# using Plots
# gr()
# plot(y)
# plot!(z)
48 changes: 48 additions & 0 deletions test/test-smooth_whit.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Test

@testset "whittaker smoother" begin
y = [5.0, 8, 9, 10, 12, 10, 15, 10, 9, 19, 19, 17, 13, 14, 18, 19, 18, 12, 18,
24, 0, 1, 18, 17, 6, 13, 12, 10, 9, 6, 6, 3, 4, 3, 3, 3, 2, 3, 4, 4, 3, 2, 3, 3, 1, 3]
y = [y; y; y; y]
m = length(y)
FT = Float32

w = ones(Float32, m)
c = zeros(Float32, m)
d = zeros(Float32, m)
e = zeros(Float32, m)

lambda = 2.0
z = ones(m)
interm = interm_whit{FT}(; n=length(y))
cve = whit2!(y, w, lambda, interm; include_cve=true)
z, cve2 = whit2(y, w, lambda)

@test cve == cve2
lamb_cv = lambda_cv(y, w, is_plot=false)
lamb_vcurve = lambda_vcurve(y, w)

z1, cve_cv = whit2(y, w, lamb_cv)
z2, cve_vcurve = whit2(y, w, lamb_vcurve)
@test cve_cv < cve
@test cve_vcurve < cve
@test cve_cv < cve_vcurve
end

# using BenchmarkTools
# @benchmark
# 4 times faster than R
# @time for i in 1:1e4
# lamb_vcurve = lambda_vcurve(y, w)
# end
# @time lamb_vcurve = lambda_vcurve(y, w)

# @time @benchmark lamb_cv = lambda_cv(y, w)
# BenchmarkTools.Trial:
# memory estimate: 412.55 KiB
# allocs estimate: 374
# --------------
# minimum time: 172.600 μs (0.00% GC)
# median time: 179.300 μs (0.00% GC)
# mean time: 198.880 μs (3.88% GC)
# maximum time: 1.882 ms (70.58% GC)
19 changes: 19 additions & 0 deletions test/test_wTSM.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @profview
n = 1000
y = rand(n)
w = rand(n)
yfit = rand(n)

# @testset "weights updating" begin
# w_ts = wTSM(y, yfit, w, iter=2)
# w_bi = wBisquare(y, yfit, w, iter=2)

# @test length(w_ts) == n
# @test length(w_bi) == n
# end

# using BenchmarkTools
# @benchmark wnew = wTSM(y, yfit, w, iter = 2)

# # Debugger.@run
# @benchmark w2 = wBisquare(y, yfit, w, iter = 2)

0 comments on commit 73c35cb

Please sign in to comment.