Skip to content

Commit e577f2c

Browse files
committed
Regression testing integration method
1 parent 7f54e8d commit e577f2c

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1212
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
1313
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
1414
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
15+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1516

1617
[compat]
1718
CSV = "^0.10"

src/DynamicModelTestUtils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module DynamicModelTestUtils
22
using DataFrames, StatsBase, LinearAlgebra
33
using ModelingToolkit, SciMLBase, SymbolicIndexingInterface
4+
using CSV, Test
45
abstract type DiscreteEvaluation end
56
abstract type Metric end
67
include("test/measured.jl")
@@ -10,4 +11,5 @@ include("test/compare.jl")
1011
include("problem_config/problem.jl")
1112
include("deploy/deploy.jl")
1213
include("deploy/restore.jl")
14+
include("integration/regression.jl")
1315
end

src/integration/regression.jl

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
function default_comparison(name, sol, df; tol=1e-3, show_results=true, kwargs...)
2+
cmp_df = compare(sol, df; kwargs...)
3+
if show_results
4+
@show cmp_df
5+
end
6+
7+
# this is lame but is better than the alternatives
8+
for (idx, var) in enumerate(cmp_df[:, :var])
9+
for (cidx, col) in enumerate(names(cmp_df))
10+
if col ["observed", "var"]
11+
continue
12+
end
13+
@test cmp_df[idx, cidx] < tol # "Solution $name differs from regression data using metric $col on variable $var by $(cmp_df[idx, cidx])."
14+
end
15+
end
16+
return cmp_df
17+
end
18+
19+
20+
function regression_test(
21+
runs::Dict{Symbol, <:SciMLBase.AbstractTimeseriesSolution},
22+
rootdir=currentdir(),
23+
data_dir="data",
24+
should_deploy="--deploy_data" ARGS;
25+
make_output_dir=false,
26+
clean_output_dir=false,
27+
deploy_only=false,
28+
restore=(go, root) -> load_data(go; root=root),
29+
cmp=(name, sol, df) -> default_comparison(name, sol, df),
30+
do_load=(filename) -> CSV.read(filename, DataFrame),
31+
discretize=(sol) -> discretize_solution(sol),
32+
save=(filename, df) -> CSV.write(filename, df),
33+
do_deploy=deploy,
34+
mk_filename=(symbol) -> string(symbol) * ".csv")
35+
should_save = false
36+
data_fulldir = joinpath(rootdir, data_dir)
37+
if !isfile(data_fulldir) && make_output_dir
38+
mkpath(data_fulldir)
39+
should_save = true
40+
elseif isfile(data_fulldir) && make_output_dir
41+
if clean_output_dir
42+
if !isempty(readdir(data_fulldir))
43+
for rmfile in readdir(data_fulldir)
44+
@warn "Removing existing data file $rmfile"
45+
rm(rmfile)
46+
end
47+
end
48+
end
49+
should_save = true
50+
end
51+
52+
53+
if !deploy_only
54+
restore(rootdir) do restored
55+
cd(restored) do
56+
for (name, new_sol) in runs
57+
@assert isfile(mk_filename(name))
58+
data = do_load(mk_filename(name))
59+
cmp(name, new_sol, data)
60+
end
61+
end
62+
end
63+
end
64+
if should_save
65+
for (name, new_sol) in runs
66+
save(joinpath(data_fulldir, mk_filename(name)), discretize(new_sol))
67+
end
68+
end
69+
if should_save && should_deploy
70+
do_deploy(rootdir, data_dir,
71+
deploy_type = DynamicModelTestUtils.FilesystemDeployConfig(rootdir, "."),
72+
repo = rootdir)
73+
elseif !should_save && should_deploy
74+
mktempdir() do temp
75+
cd(temp) do
76+
mkdir(data_dir)
77+
do_deploy(temp, data_dir,
78+
deploy_type = DynamicModelTestUtils.FilesystemDeployConfig(rootdir, "."),
79+
repo = rootdir)
80+
81+
end
82+
end
83+
end
84+
end

test/deployment.jl

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ using Git, CSV
5858
full_repo_path = joinpath(pwd(), "repo")
5959
mkdir("data")
6060
CSV.write("data/output.csv", d1)
61-
println(readdir())
6261
DynamicModelTestUtils.deploy(
6362
pwd(), "data",
6463
deploy_type = DynamicModelTestUtils.FilesystemDeployConfig(full_repo_path, "."),
@@ -72,4 +71,54 @@ using Git, CSV
7271
end
7372
end
7473
end
74+
@testset "RC regression" begin
75+
R = 1.0
76+
C = 1.0
77+
V = 1.0
78+
@variables t
79+
@named resistor = Resistor(R = R)
80+
@named capacitor = Capacitor(C = C)
81+
@named source = Voltage()
82+
@named constant = Constant(k = V)
83+
@named ground = Ground()
84+
85+
@named sensor = PotentialSensor()
86+
@named key_parameter = MeasureComponent()
87+
88+
rc_eqs = [connect(constant.output, source.V)
89+
connect(source.p, resistor.p)
90+
connect(resistor.n, capacitor.p)
91+
connect(capacitor.n, source.n, ground.g)
92+
connect(sensor.p, capacitor.p)
93+
sensor.phi ~ key_parameter.value]
94+
95+
@named rc_model = ODESystem(rc_eqs, t,
96+
systems = [resistor, capacitor, constant, source, ground, sensor, key_parameter])
97+
sys = structural_simplify(rc_model)
98+
prob1 = ODEProblem(sys, Pair[], (0, 10.0))
99+
sol = solve(prob1, Tsit5())
100+
prob2 = ODEProblem(sys, Pair[capacitor.C => 0.5], (0, 10.0))
101+
sol2 = solve(prob2, Tsit5())
102+
103+
println("\n\n\nstart deploy test")
104+
mktempdir() do dir
105+
cd(dir) do
106+
mkdir("repo")
107+
run(`$(git()) -C repo init -q --bare`)
108+
full_repo_path = joinpath(pwd(), "repo")
109+
DynamicModelTestUtils.regression_test(Dict(:rc => sol), full_repo_path, "data", true;
110+
deploy_only = true,
111+
make_output_dir = true)
112+
println("deployed! to $full_repo_path")
113+
run(`$(git()) clone -q -b data $(full_repo_path) worktree`)
114+
println(readdir("worktree/"))
115+
DynamicModelTestUtils.regression_test(Dict(
116+
:rc => sol
117+
), full_repo_path)
118+
#DynamicModelTestUtils.regression_test(Dict(
119+
# :rc => sol2
120+
#), full_repo_path)
121+
end
122+
end
123+
end
75124
end

0 commit comments

Comments
 (0)