Skip to content

Commit

Permalink
Fixes for compatibility with latest Catlab/ACSets (#15)
Browse files Browse the repository at this point in the history
* fixes for new versions of Catlab/ACSets

* drop Catlab dep, add ACSets; use parts rather than 1:nparts

* fix tutorials and tests
---------

Co-authored-by: Bíma, Jan <[email protected]>
  • Loading branch information
slwu89 and thevolatilebit authored Oct 10, 2023
1 parent aec5820 commit 033f202
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 144 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ uuid = "c7456e7d-545a-4b79-91ea-6e93d96dd4d4"
version = "0.2.7"

[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
AlgebraicAgents = "f6eb0ae3-10fa-40e6-88dd-9006ba45093a"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand All @@ -28,8 +28,8 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
ACSets = "0.2.6"
CSV = "0.10"
Catlab = "0.14"
ComponentArrays = "0.14"
Crayons = "4.1"
DataFrames = "1.6"
Expand Down
2 changes: 1 addition & 1 deletion docs/build/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>API Documentation · ReactiveDynamics.jl API Documentation</title><script data-outdated-warner src="assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.045/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.13.24/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="assets/documenter.js"></script><script src="siteinfo.js"></script><script src="../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><div class="docs-package-name"><span class="docs-autofit"><a href="index.html">ReactiveDynamics.jl API Documentation</a></span></div><form class="docs-search" action="search.html"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li class="is-active"><a class="tocitem" href="index.html">API Documentation</a><ul class="internal"><li><a class="tocitem" href="#Create-a-model"><span>Create a model</span></a></li><li><a class="tocitem" href="#Modify-a-model"><span>Modify a model</span></a></li><li><a class="tocitem" href="#Resource-costs"><span>Resource costs</span></a></li><li><a class="tocitem" href="#Add-reactions"><span>Add reactions</span></a></li><li><a class="tocitem" href="#Set-initial-values,-uncertainty,-and-solver-arguments"><span>Set initial values, uncertainty, and solver arguments</span></a></li><li><a class="tocitem" href="#Model-unions"><span>Model unions</span></a></li><li><a class="tocitem" href="#Model-import-and-export"><span>Model import and export</span></a></li><li><a class="tocitem" href="#Solution-import-and-export"><span>Solution import and export</span></a></li><li><a class="tocitem" href="#Problematize,sSolve,-and-plot"><span>Problematize,sSolve, and plot</span></a></li><li><a class="tocitem" href="#Optimization-and-fitting"><span>Optimization and fitting</span></a></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href="index.html">API Documentation</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href="index.html">API Documentation</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com//blob/main/docs/src/index.md" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="API-Documentation"><a class="docs-heading-anchor" href="#API-Documentation">API Documentation</a><a id="API-Documentation-1"></a><a class="docs-heading-anchor-permalink" href="#API-Documentation" title="Permalink"></a></h1><h2 id="Create-a-model"><a class="docs-heading-anchor" href="#Create-a-model">Create a model</a><a id="Create-a-model-1"></a><a class="docs-heading-anchor-permalink" href="#Create-a-model" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@ReactionNetwork" href="#ReactiveDynamics.@ReactionNetwork"><code>ReactiveDynamics.@ReactionNetwork</code></a><span class="docstring-category">Macro</span></header><section><div><p>Macro that takes an expression corresponding to a reaction network and outputs an instance of <code>TheoryReactionNetwork</code> that can be converted to a <code>DiscreteProblem</code> or solved directly.</p><p>Most arrows accepted (both right, left, and bi-drectional arrows). Use 0 or ∅ for annihilation/creation to/from nothing.</p><p>Custom functions and sampleable objects can be used as numeric parameters. Note that these have to be accessible from ReactiveDynamics&#39;s source code.</p><p><strong>Examples</strong></p><pre><code class="language-julia hljs">acs = @ReactionNetwork begin
<html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><title>API Documentation · ReactiveDynamics.jl API Documentation</title><script data-outdated-warner src="assets/warner.js"></script><link href="https://cdnjs.cloudflare.com/ajax/libs/lato-font/3.0.0/css/lato-font.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.045/juliamono.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/brands.min.css" rel="stylesheet" type="text/css"/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.13.24/katex.min.css" rel="stylesheet" type="text/css"/><script>documenterBaseURL="."</script><script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" data-main="assets/documenter.js"></script><script src="siteinfo.js"></script><script src="../versions.js"></script><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-dark.css" data-theme-name="documenter-dark" data-theme-primary-dark/><link class="docs-theme-link" rel="stylesheet" type="text/css" href="assets/themes/documenter-light.css" data-theme-name="documenter-light" data-theme-primary/><script src="assets/themeswap.js"></script></head><body><div id="documenter"><nav class="docs-sidebar"><div class="docs-package-name"><span class="docs-autofit"><a href="index.html">ReactiveDynamics.jl API Documentation</a></span></div><form class="docs-search" action="search.html"><input class="docs-search-query" id="documenter-search-query" name="q" type="text" placeholder="Search docs"/></form><ul class="docs-menu"><li class="is-active"><a class="tocitem" href="index.html">API Documentation</a><ul class="internal"><li><a class="tocitem" href="#Create-a-model"><span>Create a model</span></a></li><li><a class="tocitem" href="#Modify-a-model"><span>Modify a model</span></a></li><li><a class="tocitem" href="#Resource-costs"><span>Resource costs</span></a></li><li><a class="tocitem" href="#Add-reactions"><span>Add reactions</span></a></li><li><a class="tocitem" href="#Set-initial-values,-uncertainty,-and-solver-arguments"><span>Set initial values, uncertainty, and solver arguments</span></a></li><li><a class="tocitem" href="#Model-unions"><span>Model unions</span></a></li><li><a class="tocitem" href="#Model-import-and-export"><span>Model import and export</span></a></li><li><a class="tocitem" href="#Solution-import-and-export"><span>Solution import and export</span></a></li><li><a class="tocitem" href="#Problematize,sSolve,-and-plot"><span>Problematize,sSolve, and plot</span></a></li><li><a class="tocitem" href="#Optimization-and-fitting"><span>Optimization and fitting</span></a></li></ul></li></ul><div class="docs-version-selector field has-addons"><div class="control"><span class="docs-label button is-static is-size-7">Version</span></div><div class="docs-selector control is-expanded"><div class="select is-fullwidth is-size-7"><select id="documenter-version-selector"></select></div></div></div></nav><div class="docs-main"><header class="docs-navbar"><nav class="breadcrumb"><ul class="is-hidden-mobile"><li class="is-active"><a href="index.html">API Documentation</a></li></ul><ul class="is-hidden-tablet"><li class="is-active"><a href="index.html">API Documentation</a></li></ul></nav><div class="docs-right"><a class="docs-edit-link" href="https://github.com//blob/main/docs/src/index.md" title="Edit on GitHub"><span class="docs-icon fab"></span><span class="docs-label is-hidden-touch">Edit on GitHub</span></a><a class="docs-settings-button fas fa-cog" id="documenter-settings-button" href="#" title="Settings"></a><a class="docs-sidebar-button fa fa-bars is-hidden-desktop" id="documenter-sidebar-button" href="#"></a></div></header><article class="content" id="documenter-page"><h1 id="API-Documentation"><a class="docs-heading-anchor" href="#API-Documentation">API Documentation</a><a id="API-Documentation-1"></a><a class="docs-heading-anchor-permalink" href="#API-Documentation" title="Permalink"></a></h1><h2 id="Create-a-model"><a class="docs-heading-anchor" href="#Create-a-model">Create a model</a><a id="Create-a-model-1"></a><a class="docs-heading-anchor-permalink" href="#Create-a-model" title="Permalink"></a></h2><article class="docstring"><header><a class="docstring-binding" id="ReactiveDynamics.@ReactionNetwork" href="#ReactiveDynamics.@ReactionNetwork"><code>ReactiveDynamics.@ReactionNetwork</code></a><span class="docstring-category">Macro</span></header><section><div><p>Macro that takes an expression corresponding to a reaction network and outputs an instance of <code>TheoryReactionNetwork</code> that can be converted to a <code>DiscreteProblem</code> or solved directly.</p><p>Most arrows accepted (both right, left, and bi-drectional arrows). Use 0 or ∅ for annihilation/creation to/from nothing.</p><p>Custom functions and sampleable objects can be used as numeric parameters. Note that these have to be accessible from ReactiveDynamics&#39;s source code.</p><p><strong>Examples</strong></p><pre><code class="language-julia hljs">acs = @ReactionNetworkSchema begin
1.0, X ⟶ Y
1.0, X ⟶ Y, priority=&gt;6., prob=&gt;.7, capacity=&gt;3.
1.0, ∅ --&gt; (Poisson(.3γ)X, Poisson(.5)Y)
Expand Down
2 changes: 1 addition & 1 deletion docs/build/search_index.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Follow the SIR model's reactions:
using ReactiveDynamics

# model dynamics
sir_acs = @ReactionNetwork begin
sir_acs = @ReactionNetworkSchema begin
α*S*I, S+I --> 2I, name=>I2R
β*I, I --> R, name=>R2S
end
Expand Down Expand Up @@ -196,7 +196,7 @@ To harness the capabilities of **GeneratedExpressions.jl**, let us first declare
end

# generate submodel dynamics
push!(rd_models, @ReactionNetwork begin
push!(rd_models, @ReactionNetworkSchema begin
M[$i][$m, $n], state[$m] + {demand[$i][$m, $n, $l]*resource[$l], l=1:$r, dlm=+} --> state[$n] +
{production[$i][$m, $n, $l]*resource[$l], l=1:$r, dlm=+}, cycle_time=>cycle_times[$i][$m, $n], probability_of_success=>$m*$n/(n[$i])^2
end m=1:ReactiveDynamics.ns[$i] n=1:ReactiveDynamics.ns[$i]
Expand Down Expand Up @@ -292,7 +292,7 @@ end
Next we set up a simple dynamics and supply initial parameters.

```julia
acs = @ReactionNetwork begin
acs = @ReactionNetworkSchema begin
function_to_learn(A, B, C, params), A --> B+C
1., B --> C
2., C --> B
Expand Down
106 changes: 51 additions & 55 deletions src/ReactiveDynamics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ReactiveDynamics

using Catlab, Catlab.CategoricalAlgebra, Catlab.Present
using ACSets
using Reexport
using MacroTools
using ComponentArrays
Expand All @@ -27,50 +27,50 @@ Base.@kwdef mutable struct FoldedObservable
on::Vector{SampleableValues} = SampleableValues[]
end

@present TheoryReactionNetwork(FreeSchema) begin
(S, T)::Ob # species, transitions

(
SymbolicAttributeT,
DescriptiveAttributeT,
SampleableAttributeT,
ModalityAttributeT,
PcsOptT,
PrmAttributeT,
)::AttrType

specName::Attr(S, SymbolicAttributeT)
specModality::Attr(S, ModalityAttributeT)
specInitVal::Attr(S, SampleableAttributeT)
specInitUncertainty::Attr(S, SampleableAttributeT)
(specCost, specReward, specValuation)::Attr(S, SampleableAttributeT)

trans::Attr(T, SampleableAttributeT)
transPriority::Attr(T, SampleableAttributeT)
transRate::Attr(T, SampleableAttributeT)
transCycleTime::Attr(T, SampleableAttributeT)
transProbOfSuccess::Attr(T, SampleableAttributeT)
transCapacity::Attr(T, SampleableAttributeT)
transMaxLifeTime::Attr(T, SampleableAttributeT)
transPostAction::Attr(T, SampleableAttributeT)
transMultiplier::Attr(T, SampleableAttributeT)
transName::Attr(T, DescriptiveAttributeT)

E::Ob # events
(eventTrigger, eventAction)::Attr(E, SampleableAttributeT)

obs::Ob # processes (observables)
obsName::Attr(obs, SymbolicAttributeT)
obsOpts::Attr(obs, PcsOptT)

(P, M)::Ob # model params, solver args

prmName::Attr(P, SymbolicAttributeT)
prmVal::Attr(P, PrmAttributeT)

metaKeyword::Attr(M, SymbolicAttributeT)
metaVal::Attr(M, SampleableAttributeT)
end
TheoryReactionNetwork = BasicSchema(
[:S, :T, :E, :obs, :P, :M], # species, transitions, events, processes (observables), model params, solver args
[], # no homs
[
:SymbolicAttributeT,
:DescriptiveAttributeT,
:SampleableAttributeT,
:ModalityAttributeT,
:PcsOptT,
:PrmAttributeT,
], # AttrTypes
[
# species
(:specName, :S, :SymbolicAttributeT),
(:specModality, :S, :ModalityAttributeT),
(:specInitVal, :S, :SampleableAttributeT),
(:specInitUncertainty, :S, :SampleableAttributeT),
(:specCost, :S, :SampleableAttributeT),
(:specReward, :S, :SampleableAttributeT),
(:specValuation, :S, :SampleableAttributeT),
# transitions
(:trans, :T, :SampleableAttributeT),
(:transPriority, :T, :SampleableAttributeT),
(:transRate, :T, :SampleableAttributeT),
(:transCycleTime, :T, :SampleableAttributeT),
(:transProbOfSuccess, :T, :SampleableAttributeT),
(:transCapacity, :T, :SampleableAttributeT),
(:transMaxLifeTime, :T, :SampleableAttributeT),
(:transPostAction, :T, :SampleableAttributeT),
(:transMultiplier, :T, :SampleableAttributeT),
(:transName, :T, :DescriptiveAttributeT),
# events
(:eventTrigger, :E, :SampleableAttributeT),
(:eventAction, :E, :SampleableAttributeT),
# observables
(:obsName, :obs, :SymbolicAttributeT),
(:obsOpts, :obs, :PcsOptT),
# params, args
(:prmName, :P, :SymbolicAttributeT),
(:prmVal, :P, :PrmAttributeT),
(:metaKeyword, :M, :SymbolicAttributeT),
(:metaVal, :M, :SampleableAttributeT),
],
)

@acset_type FoldedReactionNetworkType(TheoryReactionNetwork)

Expand All @@ -93,6 +93,7 @@ Base.convert(::Type{Union{String,Symbol,Missing}}, ex::String) =
end

Base.convert(::Type{SampleableValues}, ex::String) = MacroTools.striplines(Meta.parse(ex))

Base.convert(::Type{Set{Symbol}}, ex::String) = eval(Meta.parse(ex))
Base.convert(::Type{FoldedObservable}, ex::String) = eval(Meta.parse(ex))

Expand Down Expand Up @@ -137,23 +138,18 @@ species_modalities = [:nonblock, :conserved, :rate]

function assign_defaults!(acs::ReactionNetworkSchema)
for (_, v_) in defargs, (k, v) in v_
for i = 1:length(subpart(acs, k))
isnothing(acs[i, k]) && (subpart(acs, k)[i] = v)
for i in dom_parts(acs, k)
isnothing(acs[i, k]) && (acs[i, k] = v)
end
end

foreach(
i ->
!isnothing(acs[i, :specModality]) ||
(subpart(acs, :specModality)[i] = Set{Symbol}()),
1:nparts(acs, :S),
i -> !isnothing(acs[i, :specModality]) || (acs[i, :specModality] = Set{Symbol}()),
parts(acs, :S),
)
k = [:specCost, :specReward, :specValuation]
foreach(
k -> foreach(
i -> !isnothing(acs[i, k]) || (subpart(acs, k)[i] = 0.0),
1:nparts(acs, :S),
),
k -> foreach(i -> !isnothing(acs[i, k]) || (acs[i, k] = 0.0), parts(acs, :S)),
k,
)

Expand Down
2 changes: 1 addition & 1 deletion src/compilers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function compile_attrs(acs::ReactionNetworkSchema)
transitions[:transActivated] = fill(true, nparts(acs, :T))
transitions[:transToSpawn] = zeros(nparts(acs, :T))
transitions[:transHash] =
[coalesce(acs[i, :transName], gensym()) for i = 1:nparts(acs, :T)]
[coalesce(acs[i, :transName], gensym()) for i in parts(acs, :T)]

return attrs, transitions, wrap_fun
end
Expand Down
2 changes: 1 addition & 1 deletion src/interface/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ macro name_transition(acsex, exs...)
acs = $(esc(acsex))
ixs = findall(
i -> string(acs[i, :transName]) == $(string(ex.args[1])),
1:nparts(acs, :T),
parts(acs, :T),
)
foreach(i -> acs[i, :transName] = $(string(ex.args[2])), ixs)
end
Expand Down
11 changes: 7 additions & 4 deletions src/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function export_network(acs::ReactionNetworkSchema)
dict = Dict()
for (key, val) in objects_aliases
push!(dict, val => [])
for i = 1:nparts(acs, key)
for i in parts(acs, key)
dict_ = Dict()
for attr in get_attrs(val)
attr_val = acs[i, Symbol(attr)]
Expand All @@ -44,13 +44,16 @@ function export_network(acs::ReactionNetworkSchema)
end

function load_network(dict::Dict)
acs = ReactionNetwork()
acs = ReactionNetworkSchema()
for (key, val) in objects_aliases
val == "prm" && continue
for row in get(dict, val, [])
i = add_part!(acs, key)
for (attr, attrval) in row
set_subpart!(acs, i, Symbol(attr), attrval)
if (acs[i, Symbol(attr)] isa String && !(contains(attr, "name")))
acs[i, Symbol(attr)] = MacroTools.striplines(Meta.parse(attrval))
end
end
end
end
Expand Down Expand Up @@ -230,10 +233,10 @@ Export a solution as a `DataFrame`.
```
"""
macro export_solution_as_table(solex, pathex = "sol.jld2")
return :(DataFrame($(esc(solex))))
return :(DataFrame($(esc(solex)).sol))
end

get_DataFrame(sol) = sol isa EnsembleSolution ? DataFrame(sol)[!, [:u, :t]] : DataFrame(sol)
get_DataFrame(sol) = sol.sol

"""
@export_solution_as_csv sol
Expand Down
2 changes: 1 addition & 1 deletion src/operators/equalize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function equalize!(acs::ReactionNetworkSchema, eqs = [])
block_alias = findfirst(e -> e[1] == :alias, block)
block_alias = !isnothing(block_alias) ? block[block_alias][2] : first(block)[2]
species_ixs = Int64[]
for e in block, i = 1:nparts(acs, :S)
for e in block, i in parts(acs, :S)
(
(i == e[2]) ||
(
Expand Down
Loading

0 comments on commit 033f202

Please sign in to comment.