Skip to content

Commit 68c0664

Browse files
authored
Merge pull request #975 from vyudu/cache-incidence
Cache incidence matrix
2 parents 8dbb398 + 6571e84 commit 68c0664

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

src/network_analysis.jl

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,19 @@ end
260260
Construct a directed simple graph where nodes correspond to reaction complexes and directed
261261
edges to reactions converting between two complexes.
262262
263-
Notes:
264-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
265-
`reactioncomplexes`.
266-
267263
For example,
268264
```julia
269265
sir = @reaction_network SIR begin
270266
β, S + I --> 2I
271267
ν, I --> R
272268
end
273-
complexes,incidencemat = reactioncomplexes(sir)
274269
incidencematgraph(sir)
275270
```
276271
"""
277272
function incidencematgraph(rn::ReactionSystem)
278273
nps = get_networkproperties(rn)
279274
if Graphs.nv(nps.incidencegraph) == 0
280-
isempty(nps.incidencemat) &&
281-
error("Please call reactioncomplexes(rn) first to construct the incidence matrix.")
275+
isempty(nps.incidencemat) && reactioncomplexes(rn)
282276
nps.incidencegraph = incidencematgraph(nps.incidencemat)
283277
end
284278
nps.incidencegraph
@@ -329,17 +323,12 @@ Given the incidence graph of a reaction network, return a vector of the
329323
connected components of the graph (i.e. sub-groups of reaction complexes that
330324
are connected in the incidence graph).
331325
332-
Notes:
333-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
334-
`reactioncomplexes`.
335-
336326
For example,
337327
```julia
338328
sir = @reaction_network SIR begin
339329
β, S + I --> 2I
340330
ν, I --> R
341331
end
342-
complexes,incidencemat = reactioncomplexes(sir)
343332
linkageclasses(sir)
344333
```
345334
gives
@@ -371,17 +360,12 @@ Here the deficiency, ``\delta``, of a network with ``n`` reaction complexes,
371360
\delta = n - \ell - s
372361
```
373362
374-
Notes:
375-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
376-
`reactioncomplexes`.
377-
378363
For example,
379364
```julia
380365
sir = @reaction_network SIR begin
381366
β, S + I --> 2I
382367
ν, I --> R
383368
end
384-
rcs,incidencemat = reactioncomplexes(sir)
385369
δ = deficiency(sir)
386370
```
387371
"""
@@ -419,17 +403,12 @@ end
419403
420404
Find subnetworks corresponding to each linkage class of the reaction network.
421405
422-
Notes:
423-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
424-
`reactioncomplexes`.
425-
426406
For example,
427407
```julia
428408
sir = @reaction_network SIR begin
429409
β, S + I --> 2I
430410
ν, I --> R
431411
end
432-
complexes,incidencemat = reactioncomplexes(sir)
433412
subnetworks(sir)
434413
```
435414
"""
@@ -456,17 +435,12 @@ end
456435
457436
Calculates the deficiency of each sub-reaction network within `network`.
458437
459-
Notes:
460-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
461-
`reactioncomplexes`.
462-
463438
For example,
464439
```julia
465440
sir = @reaction_network SIR begin
466441
β, S + I --> 2I
467442
ν, I --> R
468443
end
469-
rcs,incidencemat = reactioncomplexes(sir)
470444
linkage_deficiencies = linkagedeficiencies(sir)
471445
```
472446
"""
@@ -487,17 +461,12 @@ end
487461
488462
Given a reaction network, returns if the network is reversible or not.
489463
490-
Notes:
491-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
492-
`reactioncomplexes`.
493-
494464
For example,
495465
```julia
496466
sir = @reaction_network SIR begin
497467
β, S + I --> 2I
498468
ν, I --> R
499469
end
500-
rcs,incidencemat = reactioncomplexes(sir)
501470
isreversible(sir)
502471
```
503472
"""
@@ -511,30 +480,27 @@ end
511480
512481
Determine if the reaction network with the given subnetworks is weakly reversible or not.
513482
514-
Notes:
515-
- Requires the `incidencemat` to already be cached in `rn` by a previous call to
516-
`reactioncomplexes`.
517-
518483
For example,
519484
```julia
520485
sir = @reaction_network SIR begin
521486
β, S + I --> 2I
522487
ν, I --> R
523488
end
524-
rcs,incidencemat = reactioncomplexes(sir)
525489
subnets = subnetworks(rn)
526490
isweaklyreversible(rn, subnets)
527491
```
528492
"""
529493
function isweaklyreversible(rn::ReactionSystem, subnets)
530-
im = get_networkproperties(rn).incidencemat
531-
isempty(im) &&
532-
error("Error, please call reactioncomplexes(rn::ReactionSystem) to ensure the incidence matrix has been cached.")
533-
sparseig = issparse(im)
494+
nps = get_networkproperties(rn)
495+
isempty(nps.incidencemat) && reactioncomplexes(rn)
496+
sparseig = issparse(nps.incidencemat)
497+
534498
for subnet in subnets
535-
nps = get_networkproperties(subnet)
536-
isempty(nps.incidencemat) && reactioncomplexes(subnet; sparse = sparseig)
499+
subnps = get_networkproperties(subnet)
500+
isempty(subnps.incidencemat) && reactioncomplexes(subnet; sparse = sparseig)
537501
end
502+
503+
# A network is weakly reversible if all of its subnetworks are strongly connected
538504
all(Graphs.is_strongly_connected incidencematgraph, subnets)
539505
end
540506

test/network_analysis/network_properties.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,3 @@ let
325325
rates = Dict(zip(parameters(rn), k))
326326
@test Catalyst.iscomplexbalanced(rn, rates) == true
327327
end
328-

test/reactionsystem_core/reactionsystem.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,42 @@ end
816816
let
817817
@test_nowarn Catalyst.reactionsystem_uptodate_check()
818818
end
819+
820+
# Test that functions using the incidence matrix properly cache it
821+
let
822+
rn = @reaction_network begin
823+
k1, A --> B
824+
k2, B --> C
825+
k3, C --> A
826+
end
827+
828+
nps = Catalyst.get_networkproperties(rn)
829+
@test isempty(nps.incidencemat) == true
830+
831+
img = incidencematgraph(rn)
832+
@test size(nps.incidencemat) == (3,3)
833+
834+
Catalyst.reset!(nps)
835+
lcs = linkageclasses(rn)
836+
@test size(nps.incidencemat) == (3,3)
837+
838+
Catalyst.reset!(nps)
839+
sns = subnetworks(rn)
840+
@test size(nps.incidencemat) == (3,3)
841+
842+
Catalyst.reset!(nps)
843+
δ = deficiency(rn)
844+
@test size(nps.incidencemat) == (3,3)
845+
846+
Catalyst.reset!(nps)
847+
δ_l = linkagedeficiencies(rn)
848+
@test size(nps.incidencemat) == (3,3)
849+
850+
Catalyst.reset!(nps)
851+
rev = isreversible(rn)
852+
@test size(nps.incidencemat) == (3,3)
853+
854+
Catalyst.reset!(nps)
855+
weakrev = isweaklyreversible(rn, sns)
856+
@test size(nps.incidencemat) == (3,3)
857+
end

0 commit comments

Comments
 (0)