Skip to content

Commit df6befd

Browse files
authored
Merge pull request #973 from SciML/test_expansion
Expand tests
2 parents cae7118 + ff6aa63 commit df6befd

File tree

6 files changed

+298
-9
lines changed

6 files changed

+298
-9
lines changed

src/network_analysis.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,8 @@ end
737737
iscomplexbalanced(rs::ReactionSystem, parametermap)
738738
739739
Constructively compute whether a network will have complex-balanced equilibrium
740-
solutions, following the method in van der Schaft et al., [2015](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3). Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
740+
solutions, following the method in van der Schaft et al., [2015](https://link.springer.com/article/10.1007/s10910-015-0498-2#Sec3).
741+
Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
741742
"""
742743

743744
function iscomplexbalanced(rs::ReactionSystem, parametermap::Dict)
@@ -795,12 +796,12 @@ function iscomplexbalanced(rs::ReactionSystem, parametermap::Dict)
795796
end
796797
end
797798

798-
function iscomplexbalanced(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
799+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Vector{<:Pair})
799800
pdict = Dict(parametermap)
800801
iscomplexbalanced(rs, pdict)
801802
end
802803

803-
function iscomplexbalanced(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
804+
function iscomplexbalanced(rs::ReactionSystem, parametermap::Tuple)
804805
pdict = Dict(parametermap)
805806
iscomplexbalanced(rs, pdict)
806807
end
@@ -812,7 +813,9 @@ end
812813
"""
813814
ratematrix(rs::ReactionSystem, parametermap)
814815
815-
Given a reaction system with n complexes, outputs an n-by-n matrix where R_{ij} is the rate constant of the reaction between complex i and complex j. Accepts a dictionary, vector, or tuple of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
816+
Given a reaction system with n complexes, outputs an n-by-n matrix where R_{ij} is the rate
817+
constant of the reaction between complex i and complex j. Accepts a dictionary, vector, or tuple
818+
of variable-to-value mappings, e.g. [k1 => 1.0, k2 => 2.0,...].
816819
"""
817820

818821
function ratematrix(rs::ReactionSystem, rates::Vector{Float64})
@@ -842,12 +845,12 @@ function ratematrix(rs::ReactionSystem, parametermap::Dict)
842845
ratematrix(rs, rates)
843846
end
844847

845-
function ratematrix(rs::ReactionSystem, parametermap::Vector{Pair{Symbol, Float64}})
848+
function ratematrix(rs::ReactionSystem, parametermap::Vector{<:Pair})
846849
pdict = Dict(parametermap)
847850
ratematrix(rs, pdict)
848851
end
849852

850-
function ratematrix(rs::ReactionSystem, parametermap::Tuple{Pair{Symbol, Float64}})
853+
function ratematrix(rs::ReactionSystem, parametermap::Tuple)
851854
pdict = Dict(parametermap)
852855
ratematrix(rs, pdict)
853856
end

test/miscellaneous_tests/stability_computation.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ let
9696
u = [1.0]
9797
@test_throws Exception steady_state_stability(u, rn, p; tol = 1e2)
9898
end
99+
100+
# Test that stability computation for non-autonomous (t-dependent) systems throws error.
101+
let
102+
rn = @reaction_network begin
103+
(p + 1/(1+t),d), 0 <--> X
104+
end
105+
p = [:p => 1.0, :d => 1.0]
106+
u = [1.0]
107+
@test_throws Exception steady_state_stability(u, rn, p)
108+
end

test/network_analysis/network_properties.jl

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
### Prepares Tests ###
22

33
# Fetch packages.
4-
using Catalyst, LinearAlgebra, Test, StableRNGs
4+
using Catalyst, LinearAlgebra, Test, SparseArrays
55

6+
# Sets stable rng number.
7+
using StableRNGs
68
rng = StableRNG(514)
79

810
### Basic Tests ###
911

12+
# Tests basic `ReactionComplex` properties.
13+
let
14+
rcs1 = Catalyst.ReactionComplex([1, 2], [1, 3])
15+
rcs2 = Catalyst.ReactionComplex([1, 2], [1, 3])
16+
rcs3 = Catalyst.ReactionComplex([3], [2])
17+
@test rcs1 == rcs2
18+
@test rcs2 != rcs3
19+
@test length(rcs1) == 2
20+
@test length(rcs3) == 1
21+
end
22+
1023
# Tests network analysis functions on MAPK network (by comparing to manually computed outputs).
1124
let
1225
MAPK = @reaction_network MAPK begin
@@ -203,6 +216,7 @@ let
203216
rates = Dict(zip(parameters(rn), k))
204217
@test Catalyst.iscomplexbalanced(rn, rates) == false
205218
end
219+
206220
let
207221
rn = @reaction_network begin
208222
k1, A --> B
@@ -215,6 +229,7 @@ let
215229
rates = Dict(zip(parameters(rn), k))
216230
@test Catalyst.iscomplexbalanced(rn, rates) == false
217231
end
232+
218233
let
219234
rn = @reaction_network begin
220235
k1, A --> B
@@ -229,6 +244,7 @@ let
229244
rates = Dict(zip(parameters(rn), k))
230245
@test Catalyst.iscomplexbalanced(rn, rates) == false
231246
end
247+
232248
let
233249
rn = @reaction_network begin
234250
(k2, k1), A <--> 2B
@@ -244,6 +260,7 @@ let
244260
rates = Dict(zip(parameters(rn), k))
245261
@test Catalyst.iscomplexbalanced(rn, rates) == true
246262
end
263+
247264
let
248265
rn = @reaction_network begin
249266
(k2, k1), A + E <--> AE
@@ -257,6 +274,7 @@ let
257274
rates = Dict(zip(parameters(rn), k))
258275
@test Catalyst.iscomplexbalanced(rn, rates) == false
259276
end
277+
260278
let
261279
rn = @reaction_network begin
262280
(k2, k1), A + E <--> AE
@@ -270,6 +288,7 @@ let
270288
rates = Dict(zip(parameters(rn), k))
271289
@test Catalyst.iscomplexbalanced(rn, rates) == true
272290
end
291+
273292
let
274293
rn = @reaction_network begin (k2, k1), A + B <--> 2A end
275294
rev = true
@@ -280,6 +299,7 @@ let
280299
rates = Dict(zip(parameters(rn), k))
281300
@test Catalyst.iscomplexbalanced(rn, rates) == true
282301
end
302+
283303
let
284304
rn = @reaction_network begin
285305
k1, A + B --> 3A
@@ -295,6 +315,7 @@ let
295315
rates = Dict(zip(parameters(rn), k))
296316
@test Catalyst.iscomplexbalanced(rn, rates) == true
297317
end
318+
298319
let
299320
rn = @reaction_network begin
300321
(k2, k1), A + E <--> AE
@@ -409,3 +430,114 @@ let
409430
@test issubset([[1,2], [3,4], [5,6,7]], slcs)
410431
@test issubset([[3,4], [5,6,7]], tslcs)
411432
end
433+
434+
### Other Network Properties Tests ###
435+
436+
# Tests outgoing complexes matrices (1).
437+
# Checks using dense and sparse representation.
438+
let
439+
# Declares network.
440+
rs = @reaction_network begin
441+
k1, X1 + X2 --> X3 + X4
442+
(k2,k2), X3 + X4 <--> X1
443+
k3, X1 --> X2
444+
k4, X1 + X2 --> X2
445+
end
446+
447+
# Compares to manually computed matrix.
448+
cmplx_out_mat = [
449+
-1 0 0 0 -1;
450+
0 -1 0 0 0;
451+
0 0 -1 -1 0;
452+
0 0 0 0 0;
453+
]
454+
complexoutgoingmat(rs) == cmplx_out_mat
455+
complexoutgoingmat(rs; sparse = true) == sparse(cmplx_out_mat)
456+
end
457+
458+
# Tests outgoing complexes matrices (2).
459+
# Checks using dense and sparse representation.
460+
let
461+
# Declares network.
462+
rs = @reaction_network begin
463+
k1, X1 --> X2
464+
k2, X2 --> X3
465+
k3, X3 --> X4
466+
k4, X3 --> X5
467+
k5, X2 --> X1
468+
k6, X1 --> X2
469+
end
470+
471+
# Compares to manually computed matrix.
472+
cmplx_out_mat = [
473+
-1 0 0 0 0 -1;
474+
0 -1 0 0 -1 0;
475+
0 0 -1 -1 0 0;
476+
0 0 0 0 0 0;
477+
0 0 0 0 0 0;
478+
]
479+
complexoutgoingmat(rs)
480+
complexoutgoingmat(rs; sparse = true) == sparse(cmplx_out_mat)
481+
end
482+
483+
# Tests that `iscomplexbalanced` works for different rate inputs.
484+
# Tests that non-valid rate input yields and error
485+
let
486+
# Declares network.
487+
rn = @reaction_network begin
488+
k1, 3A + 2B --> 3C
489+
k2, B + 4D --> 2E
490+
k3, 2E --> 3C
491+
(k4, k5), B + 4D <--> 3A + 2B
492+
k6, F --> B + 4D
493+
k7, 3C --> F
494+
end
495+
496+
# Declares rate alternatives.
497+
k = rand(rng, numparams(rn))
498+
rates_vec = Pair.(parameters(rn), k)
499+
rates_tup = Tuple(rates_vec)
500+
rates_dict = Dict(rates_vec)
501+
rates_invalid = k
502+
503+
# Tests that inputs are handled correctly.
504+
@test Catalyst.iscomplexbalanced(rn, rates_vec) == Catalyst.iscomplexbalanced(rn, rates_tup)
505+
@test Catalyst.iscomplexbalanced(rn, rates_tup) == Catalyst.iscomplexbalanced(rn, rates_dict)
506+
@test_throws Exception Catalyst.iscomplexbalanced(rn, k)
507+
end
508+
509+
# Tests rate matrix computation for various input types.
510+
let
511+
# Declares network and its known rate matrix.
512+
rn = @reaction_network begin
513+
(k2, k1), A1 <--> A2 + A3
514+
k3, A2 + A3 --> A4
515+
k4, A4 --> A5
516+
(k6, k5), A5 <--> 2A6
517+
k7, 2A6 --> A4
518+
k8, A4 + A5 --> A7
519+
end
520+
rate_mat = [
521+
0.0 1.0 0.0 0.0 0.0 0.0 0.0;
522+
2.0 0.0 3.0 0.0 0.0 0.0 0.0;
523+
0.0 0.0 0.0 4.0 0.0 0.0 0.0;
524+
0.0 0.0 0.0 0.0 5.0 0.0 0.0;
525+
0.0 0.0 7.0 6.0 0.0 0.0 0.0;
526+
0.0 0.0 0.0 0.0 0.0 0.0 8.0;
527+
0.0 0.0 0.0 0.0 0.0 0.0 0.0;
528+
]
529+
530+
# Declares rate alternatives.
531+
rate_vals = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
532+
rates_vec = Pair.(parameters(rn), rate_vals)
533+
rates_tup = Tuple(rates_vec)
534+
rates_dict = Dict(rates_vec)
535+
rates_invalid = reshape(rate_vals, 1, 8)
536+
537+
# Tests that all input types generates the correct rate matrix.
538+
Catalyst.ratematrix(rn, rate_vals) == rate_mat
539+
Catalyst.ratematrix(rn, rates_vec) == rate_mat
540+
Catalyst.ratematrix(rn, rates_tup) == rate_mat
541+
Catalyst.ratematrix(rn, rates_dict) == rate_mat
542+
@test_throws Exception Catalyst.iscomplexbalanced(rn, rates_invalid)
543+
end

test/reactionsystem_core/reaction.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ end
124124
# Tests creation.
125125
# Tests basic accessor functions.
126126
# Tests that repeated metadata entries are not permitted.
127+
# Tests that attempting to access non-existent metadata throws an error.
127128
let
128129
@parameters k
129130
@species X(t) X2(t)
@@ -135,6 +136,7 @@ let
135136
@test Catalyst.hasmetadata(r, :noise_scaling)
136137
@test !Catalyst.hasmetadata(r, :nonexisting_metadata)
137138
@test Catalyst.getmetadata(r, :noise_scaling) == 0.0
139+
@test_throws Exception Catalyst.getmetadata(r, :misc)
138140

139141
metadata_repeated = [:noise_scaling => 0.0, :noise_scaling => 1.0, :metadata_entry => "unused"]
140142
@test_throws Exception Reaction(k, [X], [X2], [2], [1]; metadata=metadata_repeated)

0 commit comments

Comments
 (0)