Skip to content

Commit d281318

Browse files
Merge pull request #24 from SciML/aj/wip
cleanup tests add script to clone the physiome
2 parents aa072b4 + a263a19 commit d281318

File tree

6 files changed

+135
-37
lines changed

6 files changed

+135
-37
lines changed

Project.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CellMLToolkit"
22
uuid = "03cb29e0-1ef4-4721-aa24-cf58a006576f"
33
authors = ["Shahriar Iravanian <siravan@svtsim.com>"]
4-
version = "2.1.1"
4+
version = "2.1.0"
55

66
[deps]
77
EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
@@ -12,16 +12,17 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1212

1313
[compat]
1414
EzXML = "1.1"
15+
JSON3 = "1.8"
1516
MathML = "0.1"
1617
ModelingToolkit = "5.13"
1718
SymbolicUtils = "0.9"
1819
julia = "1.5"
1920

2021
[extras]
21-
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
22-
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
22+
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
2323
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
24+
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
2425
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2526

2627
[targets]
27-
test = ["Test", "ModelingToolkit", "DifferentialEquations", "Plots"]
28+
test = ["Test", "ModelingToolkit", "OrdinaryDiffEq", "JSON3"]

src/CellMLToolkit.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ using SymbolicUtils: FnType, Sym, operation, arguments
66
using ModelingToolkit
77
using EzXML
88

9+
include("utils.jl")
10+
export curl_exposures
11+
912
include("cellml.jl")
1013

1114
"""
@@ -97,7 +100,7 @@ end
97100
val is the new value
98101
"""
99102
function update_list!(l, sym::Symbol, val)
100-
for (i,x) in enumerate(l)
103+
for (i, x) in enumerate(l)
101104
if first(x).name == sym
102105
l[i] = (first(x) => val)
103106
return

src/utils.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""queries the cellml repo api for links to cellml model repo"""
2+
function curl_exposures()
3+
run(`curl -sL -H 'Accept: application/vnd.physiome.pmr2.json.1'
4+
https://models.physiomeproject.org/search -d '{
5+
"template": {"data": [
6+
{"name": "Subject", "value": "CellML Model"},
7+
{"name": "portal_type", "value": "ExposureFile"}
8+
]}
9+
}' -o cellml.json`)
10+
end
11+
12+
"""
13+
downloads the cellml model repository
14+
todo use Base.Downloads to speed this up
15+
"""
16+
function grab(ls)
17+
!ispath("data") && mkpath("data")
18+
@sync Threads.@threads for l in ls
19+
fn = splitdir(l)[end]
20+
download(l, "data/$(fn)")
21+
end
22+
nothing
23+
end

test/beeler.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
path = @__DIR__
2+
ml = CellModel(path * "/../models/beeler_reuter_1977.cellml.xml")
3+
4+
# @test length(ml.eqs) == 8
5+
# @test ml.iv.op.name == :time
6+
7+
# eqs, vs = CellMLToolkit.flat_equations(ml)
8+
# @test length(vs) == 8
9+
10+
# @test find_V(ml).op.name == :V
11+
12+
prob = ODEProblem(ml, (0,10000.0))
13+
sol1 = solve(prob, Euler(), dt=0.01, saveat=1.0)
14+
sol2 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
15+
V1 = map(x -> x[1], sol1.u)
16+
V2 = map(x -> x[1], sol2.u)
17+
err = sum(abs.(V1 .- V2)) / length(V1)
18+
@test err < 0.1
19+
20+
# prob = ODEProblem(ml, (0,10000.0); jac=true)
21+
# sol3 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
22+
# V3 = map(x -> x[1], sol2.u)
23+
# err = sum(abs.(V1 .- V3)) / length(V1)
24+
# @test err < 0.1
25+
26+
p = list_params(ml)
27+
update_list!(p, "IstimPeriod", 280.0)
28+
prob = ODEProblem(ml, (0,10000.0); jac=false, p=p)
29+
sol4 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
30+
V4 = map(x -> x[1], sol2.u)
31+
err = sum(abs.(V1[1:250] .- V4[1:250])) / 250
32+
@test err < 0.1

test/runtests.jl

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
11
using Test
22
using CellMLToolkit
3-
using DifferentialEquations
3+
using OrdinaryDiffEq
4+
using JSON3, Base.Threads
5+
using ModelingToolkit
46

5-
path = @__DIR__
6-
ml = CellModel(path * "/../models/beeler_reuter_1977.cellml.xml")
7+
@testset "CellMLToolkit.jl" begin
8+
@testset "beeler.jl" begin include("beeler.jl") end
79

8-
# @test length(ml.eqs) == 8
9-
# @test ml.iv.op.name == :time
10-
11-
# eqs, vs = CellMLToolkit.flat_equations(ml)
12-
# @test length(vs) == 8
13-
14-
# @test find_V(ml).op.name == :V
15-
16-
prob = ODEProblem(ml, (0,10000.0))
17-
sol1 = solve(prob, Euler(), dt=0.01, saveat=1.0)
18-
sol2 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
19-
V1 = map(x -> x[1], sol1.u)
20-
V2 = map(x -> x[1], sol2.u)
21-
err = sum(abs.(V1 .- V2)) / length(V1)
22-
@test err < 0.1
23-
24-
# prob = ODEProblem(ml, (0,10000.0); jac=true)
25-
# sol3 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
26-
# V3 = map(x -> x[1], sol2.u)
27-
# err = sum(abs.(V1 .- V3)) / length(V1)
28-
# @test err < 0.1
29-
30-
p = list_params(ml)
31-
update_list!(p, "IstimPeriod", 280.0)
32-
prob = ODEProblem(ml, (0,10000.0); jac=false, p=p)
33-
sol4 = solve(prob, TRBDF2(), dtmax=0.5, saveat=1.0)
34-
V4 = map(x -> x[1], sol2.u)
35-
err = sum(abs.(V1[1:250] .- V4[1:250])) / 250
36-
@test err < 0.1
10+
# mainly a used as a bin/ don't want to test every PR
11+
# @testset "physiome.jl" begin include("test_physiome.jl") end
12+
end

test/test_physiome.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"
2+
in place modifies a matrix with data about how far to `solve`
3+
we get for a given set of cellml files
4+
uses @threads
5+
"
6+
function test_cellmls!(mat, fns)
7+
@sync Threads.@threads for i in eachindex(fns)
8+
@show i fns[i]
9+
mat[i, :] = test_cellml!(mat[i, :], fns[i])
10+
end
11+
mat
12+
end
13+
14+
"
15+
in place modifies a row with data about how far to `solve`
16+
we get for a given cellml file
17+
"
18+
function test_cellml!(row, fn)
19+
row[1] = fn
20+
try
21+
ml = CellModel(fn)
22+
sys = ml.sys
23+
row[2] = true
24+
row[5] = length(states(sys))
25+
row[6] = length(parameters(sys))
26+
prob = ODEProblem(ml, tspan)
27+
row[3] = true
28+
sol = solve(prob)
29+
row[4] = true
30+
catch e
31+
row[end] = e
32+
end
33+
row
34+
end
35+
36+
"""
37+
a
38+
"""
39+
function main(dir="data/")
40+
tspan = (0., 1.)
41+
fns = readdir(dir; join=true)
42+
names = [:filename, :to_system, :to_problem, :to_solve, :states, :parameters, :error]
43+
n = length(names)
44+
mat = Array{Any,2}(nothing, length(fns), n)
45+
test_cellmls!(mat, fns)
46+
replace!(mat, nothing => missing)
47+
names .=> eachcol(mat) # used with DataFrame()
48+
# CSV.write("aggregate_stats.csv", df)
49+
# print(df)
50+
end
51+
52+
function json_to_cellml_links()
53+
s = read("cellml.json", String);
54+
j = JSON3.read(s);
55+
x = j.collection.links
56+
map(x -> x.href[1:end - 5], x) # remove `/view` from urls
57+
end
58+
59+
ls = json_to_cellml_links(curl_exposures())
60+
@test length(ls) == 2379
61+
CellMLToolkit.grab(ls[1:10])
62+
df = main()
63+
@test eltype(df) == Pair

0 commit comments

Comments
 (0)