Skip to content

Commit 7dbb02c

Browse files
committed
up
1 parent 99507e6 commit 7dbb02c

File tree

6 files changed

+82
-64
lines changed

6 files changed

+82
-64
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ rn = @reaction_network begin
2222
@parameters η
2323
k, 2X --> X2, [noise_scaling=η]
2424
end
25-
getnoisescaling(rn)
25+
get_noise_scaling(rn)
2626
```
2727
- `SDEProblem` no longer takes the `noise_scaling` argument (she above for new approach to handle noise scaling).
2828
- Changed fields of internal `Reaction` structure. `ReactionSystems`s saved using `serialize` on previous Catalyst versions cannot be loaded using this (or later) versions.

src/reaction_network.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ end
644644

645645
# Takes a reaction line and creates reaction(s) from it and pushes those to the reaction array.
646646
# Used to create multiple reactions from, for instance, `k, (X,Y) --> 0`.
647-
# Handles metadata, e.g. `1.0, Z --> 0, [noisescaling=η]`.
647+
# Handles metadata, e.g. `1.0, Z --> 0, [noise_scaling=η]`.
648648
function push_reactions!(reactions::Vector{ReactionStruct}, sub_line::ExprValues, prod_line::ExprValues,
649649
rate::ExprValues, metadata::ExprValues, arrow::Symbol; default_reaction_metadata::Expr)
650650
# The rates, substrates, products, and metadata may be in a tupple form (e.g. `k, (X,Y) --> 0`).

src/reactionsystem.jl

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -864,29 +864,49 @@ end
864864
######################## Other accessors ##############################
865865

866866
"""
867-
getnoisescaling(reaction::Reaction)
867+
has_noise_scaling(reaction::Reaction)
868868
869-
Returns the noise scaling associated with a specific reaction. If the `:noise_scaling` metadata has
870-
set, returns that. Else, returns `1.0`.
869+
Checks whether a specific reaction has the metadata field `noise_scaing`. If so, returns `true`, else
870+
returns `false`.
871+
872+
Arguments:
873+
- `reaction`: The reaction for which we wish to check.
874+
875+
Example:
876+
```julia
877+
reaction = @reaction k, 0 --> X, [noise_scaling=0.0]
878+
has_noise_scaling(reaction)
879+
"""
880+
function has_noise_scaling(reaction::Reaction)
881+
return hasmetadata(reaction, :noise_scaling)
882+
end
883+
884+
"""
885+
get_noise_scaling(reaction::Reaction)
886+
887+
Returns the noise_scaling metadata from a specific reaction.
871888
872889
Arguments:
873890
- `reaction`: The reaction for which we wish to retrive all metadata.
874891
875892
Example:
876893
```julia
877894
reaction = @reaction k, 0 --> X, [noise_scaling=0.0]
878-
getnoisescaling(reaction)
895+
get_noise_scaling(reaction)
879896
"""
880-
function getnoisescaling(reaction::Reaction)
881-
has_metadata(reaction, :noise_scaling) && (return get_metadata(reaction, :noise_scaling))
882-
return 1.0
897+
function get_noise_scaling(reaction::Reaction)
898+
if has_noise_scaling(reaction)
899+
return getmetadata(reaction, :noise_scaling)
900+
else
901+
error("Attempts to access noise_scaling metadata field for a reaction which does not have a value assigned for this metadata.")
902+
end
883903
end
884904

885905

886-
# These are currently considered internal, but can be used by public accessor functions like getnoisescaling.
906+
# These are currently considered internal, but can be used by public accessor functions like get_noise_scaling.
887907

888908
"""
889-
get_metadata_dict(reaction::Reaction)
909+
getmetadata_dict(reaction::Reaction)
890910
891911
Retrives the `ImmutableDict` containing all of the metadata associated with a specific reaction.
892912
@@ -896,15 +916,15 @@ Arguments:
896916
Example:
897917
```julia
898918
reaction = @reaction k, 0 --> X, [description="Production reaction"]
899-
get_metadata_dict(reaction)
919+
getmetadata_dict(reaction)
900920
```
901921
"""
902-
function get_metadata_dict(reaction::Reaction)
922+
function getmetadata_dict(reaction::Reaction)
903923
return reaction.metadata
904924
end
905925

906926
"""
907-
has_metadata(reaction::Reaction, md_key::Symbol)
927+
hasmetadata(reaction::Reaction, md_key::Symbol)
908928
909929
Checks if a `Reaction` have a certain metadata field. If it does, returns `true` (else returns `false`).
910930
@@ -915,15 +935,15 @@ Arguments:
915935
Example:
916936
```julia
917937
reaction = @reaction k, 0 --> X, [description="Production reaction"]
918-
has_metadata(reaction, :description)
938+
hasmetadata(reaction, :description)
919939
```
920940
"""
921-
function has_metadata(reaction::Reaction, md_key::Symbol)
922-
return any(isequal(md_key, entry[1]) for entry in get_metadata_dict(reaction))
941+
function hasmetadata(reaction::Reaction, md_key::Symbol)
942+
return any(isequal(md_key, entry[1]) for entry in getmetadata_dict(reaction))
923943
end
924944

925945
"""
926-
get_metadata(reaction::Reaction, md_key::Symbol)
946+
getmetadata(reaction::Reaction, md_key::Symbol)
927947
928948
Retrives a certain metadata value from a `Reaction`. If the metadata does not exists, throws an error.
929949
@@ -934,15 +954,15 @@ Arguments:
934954
Example:
935955
```julia
936956
reaction = @reaction k, 0 --> X, [description="Production reaction"]
937-
get_metadata(reaction, :description)
957+
getmetadata(reaction, :description)
938958
```
939959
"""
940-
function get_metadata(reaction::Reaction, md_key::Symbol)
941-
if !has_metadata(reaction, md_key)
942-
error("The reaction does not have a metadata field $md_key. It does have the following metadata fields: $(keys(get_metadata_dict(reaction))).")
960+
function getmetadata(reaction::Reaction, md_key::Symbol)
961+
if !hasmetadata(reaction, md_key)
962+
error("The reaction does not have a metadata field $md_key. It does have the following metadata fields: $(keys(getmetadata_dict(reaction))).")
943963
end
944-
metadata = get_metadata_dict(reaction)
945-
return metadata[findfirst(isequal(md_key, entry[1]) for entry in get_metadata_dict(reaction))][2]
964+
metadata = getmetadata_dict(reaction)
965+
return metadata[findfirst(isequal(md_key, entry[1]) for entry in getmetadata_dict(reaction))][2]
946966
end
947967

948968

@@ -1065,7 +1085,7 @@ function assemble_diffusion(rs, sts, ispcs; combinatoric_ratelaws = true,
10651085

10661086
for (j, rx) in enumerate(get_rxs(rs))
10671087
rlsqrt = sqrt(abs(oderatelaw(rx; combinatoric_ratelaw = combinatoric_ratelaws)))
1068-
rlsqrt *= getnoisescaling(rx)
1088+
has_noise_scaling(rx) && (rlsqrt *= get_noise_scaling(rx))
10691089
remove_conserved && (rlsqrt = substitute(rlsqrt, depspec_submap))
10701090

10711091
for (spec, stoich) in rx.netstoich
@@ -1485,7 +1505,6 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
14851505
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
14861506
include_zero_odes = true, checks = false, remove_conserved = false,
14871507
default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)),
1488-
noise_scaling = nothing, # To be removed (used for deprication message only).
14891508
kwargs...)
14901509
spatial_convert_err(rs::ReactionSystem, SDESystem)
14911510

@@ -1535,7 +1554,6 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
15351554
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
15361555
remove_conserved = nothing, checks = false,
15371556
default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)),
1538-
noise_scaling = nothing, # To be removed (used for deprication message only).
15391557
kwargs...)
15401558
spatial_convert_err(rs::ReactionSystem, JumpSystem)
15411559

test/dsl/dsl_basics.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,30 +169,30 @@ let
169169
# Checks DSL reactions are correct.
170170
rxs = reactions(rs)
171171
@test isequal([r1, r2, r3], rxs)
172-
@test isequal(Catalyst.get_metadata_dict(r1), Catalyst.get_metadata_dict(rxs[1]))
173-
@test isequal(Catalyst.get_metadata_dict(r2), Catalyst.get_metadata_dict(rxs[2]))
174-
@test isequal(Catalyst.get_metadata_dict(r3), Catalyst.get_metadata_dict(rxs[3]))
172+
@test isequal(Catalyst.getmetadata_dict(r1), Catalyst.getmetadata_dict(rxs[1]))
173+
@test isequal(Catalyst.getmetadata_dict(r2), Catalyst.getmetadata_dict(rxs[2]))
174+
@test isequal(Catalyst.getmetadata_dict(r3), Catalyst.getmetadata_dict(rxs[3]))
175175

176176
# Checks that accessor functions works on the DSL.
177-
@test Catalyst.has_metadata(rxs[1], :noise_scaling)
178-
@test !Catalyst.has_metadata(rxs[1], :md_1)
179-
@test !Catalyst.has_metadata(rxs[2], :noise_scaling)
180-
@test Catalyst.has_metadata(rxs[2], :md_1)
181-
@test !Catalyst.has_metadata(rxs[3], :noise_scaling)
182-
@test !Catalyst.has_metadata(rxs[3], :md_1)
177+
@test Catalyst.hasmetadata(rxs[1], :noise_scaling)
178+
@test !Catalyst.hasmetadata(rxs[1], :md_1)
179+
@test !Catalyst.hasmetadata(rxs[2], :noise_scaling)
180+
@test Catalyst.hasmetadata(rxs[2], :md_1)
181+
@test !Catalyst.hasmetadata(rxs[3], :noise_scaling)
182+
@test !Catalyst.hasmetadata(rxs[3], :md_1)
183183

184-
@test isequal(Catalyst.get_metadata(rxs[1], :noise_scaling), η)
185-
@test isequal(Catalyst.get_metadata(rxs[2], :md_1), 1.0)
184+
@test isequal(Catalyst.getmetadata(rxs[1], :noise_scaling), η)
185+
@test isequal(Catalyst.getmetadata(rxs[2], :md_1), 1.0)
186186

187187
# Test that metadata works for @reaction macro.
188188
rx1 = @reaction k, 2X --> X2, [noise_scaling=$η]
189189
rx2 = @reaction k, 2X --> X2, [md_1=1.0, md_2=false, md_3="Hello world", md_4=:sym, md_5=X+X2^k-1, md_6=(0.1,2.0)]
190190
rx3 = @reaction k, 2X --> X2
191191

192192
@test isequal([rx1, rx2, rx3], rxs)
193-
@test isequal(Catalyst.get_metadata_dict(rx1), Catalyst.get_metadata_dict(rxs[1]))
194-
@test isequal(Catalyst.get_metadata_dict(rx2), Catalyst.get_metadata_dict(rxs[2]))
195-
@test isequal(Catalyst.get_metadata_dict(rx3), Catalyst.get_metadata_dict(rxs[3]))
193+
@test isequal(Catalyst.getmetadata_dict(rx1), Catalyst.getmetadata_dict(rxs[1]))
194+
@test isequal(Catalyst.getmetadata_dict(rx2), Catalyst.getmetadata_dict(rxs[2]))
195+
@test isequal(Catalyst.getmetadata_dict(rx3), Catalyst.getmetadata_dict(rxs[3]))
196196
end
197197

198198
# Checks that repeated metadata throws errors.

test/reactionsystem_structure/reactions.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ let
1616
metadata = [:noise_scaling => 0.0]
1717
r = Reaction(k, [X], [X2], [2], [1]; metadata=metadata)
1818

19-
@test Catalyst.get_metadata_dict(r) == [:noise_scaling => 0.0]
20-
@test Catalyst.has_metadata(r, :noise_scaling)
21-
@test !Catalyst.has_metadata(r, :nonexisting_metadata)
22-
@test Catalyst.get_metadata(r, :noise_scaling) == 0.0
19+
@test Catalyst.getmetadata_dict(r) == [:noise_scaling => 0.0]
20+
@test Catalyst.hasmetadata(r, :noise_scaling)
21+
@test !Catalyst.hasmetadata(r, :nonexisting_metadata)
22+
@test Catalyst.getmetadata(r, :noise_scaling) == 0.0
2323

2424
metadata_repeated = [:noise_scaling => 0.0, :noise_scaling => 1.0, :metadata_entry => "unused"]
2525
@test_throws Exception Reaction(k, [X], [X2], [2], [1]; metadata=metadata_repeated)
@@ -36,8 +36,8 @@ let
3636
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=metadata)
3737

3838
@test isequal(r1, r2)
39-
@test Catalyst.get_metadata_dict(r1) == Pair{Symbol,Any}[]
40-
@test !Catalyst.has_metadata(r1, :md)
39+
@test Catalyst.getmetadata_dict(r1) == Pair{Symbol,Any}[]
40+
@test !Catalyst.hasmetadata(r1, :md)
4141
end
4242

4343
# Tests creation.
@@ -57,21 +57,21 @@ let
5757
push!(metadata, :md_6 => (0.1, 2.0))
5858
r = Reaction(k, [X], [X2], [2], [1]; metadata=metadata)
5959

60-
@test Catalyst.get_metadata_dict(r) isa Vector{Pair{Symbol,Any}}
61-
@test Catalyst.has_metadata(r, :md_1)
62-
@test Catalyst.has_metadata(r, :md_2)
63-
@test Catalyst.has_metadata(r, :md_3)
64-
@test Catalyst.has_metadata(r, :md_4)
65-
@test Catalyst.has_metadata(r, :md_5)
66-
@test Catalyst.has_metadata(r, :md_6)
67-
@test !Catalyst.has_metadata(r, :md_8)
60+
@test Catalyst.getmetadata_dict(r) isa Vector{Pair{Symbol,Any}}
61+
@test Catalyst.hasmetadata(r, :md_1)
62+
@test Catalyst.hasmetadata(r, :md_2)
63+
@test Catalyst.hasmetadata(r, :md_3)
64+
@test Catalyst.hasmetadata(r, :md_4)
65+
@test Catalyst.hasmetadata(r, :md_5)
66+
@test Catalyst.hasmetadata(r, :md_6)
67+
@test !Catalyst.hasmetadata(r, :md_8)
6868

69-
@test isequal(Catalyst.get_metadata(r, :md_1), 1.0)
70-
@test isequal(Catalyst.get_metadata(r, :md_2), false)
71-
@test isequal(Catalyst.get_metadata(r, :md_3), "Hello world")
72-
@test isequal(Catalyst.get_metadata(r, :md_4), :sym)
73-
@test isequal(Catalyst.get_metadata(r, :md_5), X + X2^k -1)
74-
@test isequal(Catalyst.get_metadata(r, :md_6), (0.1, 2.0))
69+
@test isequal(Catalyst.getmetadata(r, :md_1), 1.0)
70+
@test isequal(Catalyst.getmetadata(r, :md_2), false)
71+
@test isequal(Catalyst.getmetadata(r, :md_3), "Hello world")
72+
@test isequal(Catalyst.getmetadata(r, :md_4), :sym)
73+
@test isequal(Catalyst.getmetadata(r, :md_5), X + X2^k -1)
74+
@test isequal(Catalyst.getmetadata(r, :md_6), (0.1, 2.0))
7575
end
7676

7777
# Noise scaling metadata.
@@ -85,6 +85,6 @@ let
8585
r1 = Reaction(k, [X], [X2], [2], [1])
8686
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=metadata)
8787

88-
@test isequal(Catalyst.getnoisescaling(r1), 1.0)
89-
@test isequal(Catalyst.getnoisescaling(r2), η)
88+
@test isequal(Catalyst.get_noise_scaling(r1), 1.0)
89+
@test isequal(Catalyst.get_noise_scaling(r2), η)
9090
end

test/reactionsystem_structure/reactionsystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,6 @@ end
707707

708708
# Tests metadata.
709709
let
710-
@test isnothing(ModelingToolkit.get_metadata(rs))
710+
@test isnothing(ModelingToolkit.getmetadata(rs))
711711
end
712712

0 commit comments

Comments
 (0)