Skip to content

Commit b41bb7b

Browse files
committed
remake
1 parent 2c716e8 commit b41bb7b

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

src/Catalyst.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export make_empty_network, addspecies!, addparam!, addreaction!, reactionparamsm
9191
export dependants, dependents, substoichmat, prodstoichmat, netstoichmat
9292
export conservationlaws, conservedquantities, conservedequations, conservationlaw_constants
9393
export isequivalent
94-
export remake_noise_scaling
94+
export remake_noise_scaling, get_noise_scaling, has_noise_scaling
9595

9696
# depreciated functions to remove in future releases
9797
export params, numparams

src/reactionsystem.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,8 @@ end
762762
default_reaction_metadata::Vector{Pair{Symbol, T}} = Vector{Pair{Symbol, Any}}()) where {T}
763763
764764
Takes a `ReactionSystem` and remakes it, returning a modified `ReactionSystem`. Modifications depend
765-
on which additional arguments are provided. The input `ReactionSystem` is not mutated.
765+
on which additional arguments are provided. The input `ReactionSystem` is not mutated. Updating
766+
default reaction metadata is currently the only supported feature.
766767
767768
Arguments:
768769
- `rs::ReactionSystem`: The `ReactionSystem` which you wish to remake.
@@ -772,11 +773,26 @@ Arguments:
772773
have their value updated).
773774
"""
774775
function remake_ReactionSystem_internal(rs::ReactionSystem; default_reaction_metadata = [])
775-
# Updates the metadata for all reactions (equation are ignored).
776+
rs = set_default_metadata(rs; default_reaction_metadata)
777+
return rs
778+
end
779+
780+
# For a `ReactionSystem`, updates all `Reaction`'s default metadata.
781+
function set_default_metadata(rs::ReactionSystem; default_reaction_metadata = [])
782+
# Updates reaction metadata for for reactions in this specific system.
776783
eqtransform(eq) = eq isa Reaction ? set_default_metadata(eq, default_reaction_metadata) : eq
777784
updated_equations = map(eqtransform, get_eqs(rs))
778785
@set! rs.eqs = updated_equations
779786
@set! rs.rxs = Reaction[rx for rx in updated_equations if rx isa Reaction]
787+
788+
# Updates reaction metadata for all its subsystems.
789+
new_sub_systems = similar(get_systems(rs))
790+
for (i, sub_system) in enumerate(get_systems(rs))
791+
new_sub_systems[i] = set_default_metadata(sub_system; default_reaction_metadata)
792+
end
793+
@set! rs.systems = new_sub_systems
794+
795+
# Returns the updated system.
780796
return rs
781797
end
782798

@@ -790,17 +806,17 @@ end
790806
set_default_metadata(eq::Equation, default_metadata) = eq
791807

792808
"""
793-
remake_noise_scaling(rs::ReactionSystem, noise_scaling)
809+
set_default_noise_scaling(rs::ReactionSystem, noise_scaling)
794810
795811
Creates an updated `ReactionSystem`. This is the old `ReactionSystem`, but each `Reaction` that does
796-
not have a `Noise_scaling` metadata have its noise_scaling metadata updated. The input `ReactionSystem`
797-
is not mutated.
812+
not have a `noise_scaling` metadata have its noise_scaling metadata updated. The input `ReactionSystem`
813+
is not mutated. Any subsystems of `rs` have their `noise_scaling` metadata updated as well.
798814
799815
Arguments:
800816
- `rs::ReactionSystem`: The `ReactionSystem` which you wish to remake.
801817
- `noise_scaling`: The updated noise scaling terms
802818
"""
803-
function remake_noise_scaling(rs::ReactionSystem, noise_scaling)
819+
function set_default_noise_scaling(rs::ReactionSystem, noise_scaling)
804820
return remake_ReactionSystem_internal(rs, default_reaction_metadata = [:noise_scaling => noise_scaling])
805821
end
806822

test/model_simulation/simulate_SDEs.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ let
250250
@test reactions(noise_scaling_network2)[2].metadata == [:noise_scaling => 0.5]
251251
end
252252

253+
# Tests the `remake_noise_scaling` function on a hierarchical model.
254+
let
255+
# Creates hierarchical model.
256+
rn1 = @reaction_network begin
257+
p, 0 --> X, [noise_scaling=2.0]
258+
d, X --> 0
259+
end
260+
rn2 = @reaction_network begin
261+
k1, X1 --> X2, [noise_scaling=5.0]
262+
k2, X2 --> X1
263+
end
264+
rn = compose(rn1, [rn2])
265+
266+
# Checks that systems have the correct noise scaling terms.
267+
rn = Catalyst.remake_ReactionSystem_internal(rn, default_reaction_metadata = [:noise_scaling => 0.5])
268+
rn1_noise_scaling = [get_noise_scaling(rx) for rx in rn.rxs]
269+
rn2_noise_scaling = [get_noise_scaling(rx) for rx in Catalyst.get_systems(rn)[1].rxs]
270+
rn_noise_scaling = [get_noise_scaling(rx) for rx in reactions(rn)]
271+
@test issetequal(rn1_noise_scaling, [2.0, 0.5])
272+
@test issetequal(rn2_noise_scaling, [5.0, 0.5])
273+
@test issetequal(rn_noise_scaling, [2.0, 0.5, 5.0, 0.5])
274+
end
253275

254276
### Checks Simulations Don't Error ###
255277

0 commit comments

Comments
 (0)