Skip to content

Commit

Permalink
Merge pull request #852 from SciML/new_reaction_metadata
Browse files Browse the repository at this point in the history
Add two new reaction metadata
  • Loading branch information
TorkelE committed May 23, 2024
2 parents d7a18c4 + bc6c59d commit 080b89f
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 8 deletions.
101 changes: 96 additions & 5 deletions src/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,16 @@ end
"""
has_noise_scaling(reaction::Reaction)
Checks whether a specific reaction has the metadata field `noise_scaing`. If so, returns `true`, else
returns `false`.
Returns `true` if the input reaction has the `noise_scaing` metadata field assigned, else `false`.
Arguments:
- `reaction`: The reaction for which we wish to check.
- `reaction`: The reaction we wish to check for the `noise_scaing` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [noise_scaling=0.0]
has_noise_scaling(reaction)
```
"""
function has_noise_scaling(reaction::Reaction)
return hasmetadata(reaction, :noise_scaling)
Expand All @@ -488,15 +488,16 @@ end
"""
get_noise_scaling(reaction::Reaction)
Returns the noise_scaling metadata from a specific reaction.
Returns `noise_scaing` metadata field for the input reaction.
Arguments:
- `reaction`: The reaction for which we wish to retrive all metadata.
- `reaction`: The reaction we wish to retrieve the `noise_scaing` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [noise_scaling=0.0]
get_noise_scaling(reaction)
```
"""
function get_noise_scaling(reaction::Reaction)
if has_noise_scaling(reaction)
Expand All @@ -506,6 +507,96 @@ function get_noise_scaling(reaction::Reaction)
end
end

# Description.
"""
has_description(reaction::Reaction)
Returns `true` if the input reaction has the `description` metadata field assigned, else `false`.
Arguments:
- `reaction`: The reaction we wish to check for the `description` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [description="A reaction"]
has_description(reaction)
```
"""
function has_description(reaction::Reaction)
return hasmetadata(reaction, :description)
end

"""
get_description(reaction::Reaction)
Returns `description` metadata field for the input reaction.
Arguments:
- `reaction`: The reaction we wish to retrieve the `description` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [description="A reaction"]
get_description(reaction)
```
"""
function get_description(reaction::Reaction)
if has_description(reaction)
return getmetadata(reaction, :description)
else
error("Attempts to access `description` metadata field for a reaction which does not have a value assigned for this metadata.")
end
end

# Misc.
"""
has_misc(reaction::Reaction)
Returns `true` if the input reaction has the `misc` metadata field assigned, else `false`.
Arguments:
- `reaction`: The reaction we wish to check for the `misc` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [misc="A reaction"]
misc(reaction)
```
"""
function has_misc(reaction::Reaction)
return hasmetadata(reaction, :misc)
end

"""
get_misc(reaction::Reaction)
Returns `misc` metadata field for the input reaction.
Arguments:
- `reaction`: The reaction we wish to retrieve the `misc` metadata field.
Example:
```julia
reaction = @reaction k, 0 --> X, [misc="A reaction"]
get_misc(reaction)
```
Notes:
- The `misc` field can contain any valid Julia structure. This mean that Catalyst cannot check it
for symbolci variables that are added here. This means that symbolic variables (e.g. parameters of
species) that are stored here are not accessible to Catalyst. This can cause troubles when e.g.
creating a `ReactionSystem` programmatically (in which case any symbolic variables stored in the
`misc` metadata field should also be explicitly provided to the `ReactionSystem` constructor).
"""
function get_misc(reaction::Reaction)
if has_misc(reaction)
return getmetadata(reaction, :misc)
else
error("Attempts to access `misc` metadata field for a reaction which does not have a value assigned for this metadata.")
end
end


### Units Handling ###

Expand Down
34 changes: 31 additions & 3 deletions test/reactionsystem_core/reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,41 @@ let
@parameters k η
@species X(t) X2(t)

metadata = Pair{Symbol,Any}[]
push!(metadata, :noise_scaling => η)
r1 = Reaction(k, [X], [X2], [2], [1])
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=metadata)
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=[:noise_scaling => η])

@test !Catalyst.has_noise_scaling(r1)
@test Catalyst.has_noise_scaling(r2)
@test_throws Exception Catalyst.get_noise_scaling(r1)
@test isequal(Catalyst.get_noise_scaling(r2), η)
end

# Tests the description metadata.
let
@variables t
@parameters k η
@species X(t) X2(t)

r1 = Reaction(k, [X], [X2], [2], [1])
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=[:description => "A reaction"])

@test !Catalyst.has_description(r1)
@test Catalyst.has_description(r2)
@test_throws Exception Catalyst.get_description(r1)
@test isequal(Catalyst.get_description(r2), "A reaction")
end

# Tests the misc metadata.
let
@variables t
@parameters k η
@species X(t) X2(t)

r1 = Reaction(k, [X], [X2], [2], [1])
r2 = Reaction(k, [X], [X2], [2], [1]; metadata=[:misc => ('M', :M)])

@test !Catalyst.has_misc(r1)
@test Catalyst.has_misc(r2)
@test_throws Exception Catalyst.get_misc(r1)
@test isequal(Catalyst.get_misc(r2), ('M', :M))
end

0 comments on commit 080b89f

Please sign in to comment.