Skip to content

Commit

Permalink
fix plotting, solution, log
Browse files Browse the repository at this point in the history
Signed-off-by: Bíma, Jan <[email protected]>
  • Loading branch information
thevolatilebit committed Sep 16, 2023
1 parent e5ade9f commit efea660
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 408 deletions.
16 changes: 8 additions & 8 deletions src/ReactiveDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ end

@acset_type FoldedReactionNetworkType(TheoryReactionNetwork)

const ReactionNetwork = FoldedReactionNetworkType{
const ReactionNetworkSchema = FoldedReactionNetworkType{
Symbol,
Union{String,Symbol,Missing},
SampleableValues,
Expand Down Expand Up @@ -131,11 +131,11 @@ defargs = Dict(
)

compilable_attrs =
filter(attr -> eltype(attr) == SampleableValues, propertynames(ReactionNetwork()))
filter(attr -> eltype(attr) == SampleableValues, propertynames(ReactionNetworkSchema()))

species_modalities = [:nonblock, :conserved, :rate]

function assign_defaults!(acs::ReactionNetwork)
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)
Expand All @@ -160,12 +160,12 @@ function assign_defaults!(acs::ReactionNetwork)
return acs
end

function ReactionNetwork(transitions, reactants, obs, events)
return merge_acs!(ReactionNetwork(), transitions, reactants, obs, events)
function ReactionNetworkSchema(transitions, reactants, obs, events)
return merge_acs!(ReactionNetworkSchema(), transitions, reactants, obs, events)
end

function ReactionNetwork(transitions, reactants, obs)
return merge_acs!(ReactionNetwork(), transitions, reactants, obs, [])
function ReactionNetworkSchema(transitions, reactants, obs)
return merge_acs!(ReactionNetworkSchema(), transitions, reactants, obs, [])
end

function add_obs!(acs, obs)
Expand Down Expand Up @@ -194,7 +194,7 @@ function add_obs!(acs, obs)
return acs
end

function merge_acs!(acs::ReactionNetwork, transitions, reactants, obs, events)
function merge_acs!(acs::ReactionNetworkSchema, transitions, reactants, obs, events)
foreach(
t -> add_part!(acs, :T; trans = t[1][2], transRate = t[1][1], t[2]...),
transitions,
Expand Down
8 changes: 4 additions & 4 deletions src/compilers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ function wrap_expr(fex, species_names, prm_names, varmap)
)
push!(letex.args[2].args, fex)

# the function shall be a function of the dynamic ReactiveNetwork structure: letex -> :(state -> $letex)
# the function shall be a function of the dynamic ReactionNetworkSchema structure: letex -> :(state -> $letex)
# eval the expression to a Julia function, save that function into the "compiled" acset
return eval(:(state -> $letex))
end

function get_wrap_fun(acs::ReactionNetwork)
function get_wrap_fun(acs::ReactionNetworkSchema)
species_names = collect(acs[:, :specName])
prm_names = collect(acs[:, :prmName])
varmap = Dict([name => :(state.u[$i]) for (i, name) in enumerate(species_names)])
Expand All @@ -133,7 +133,7 @@ function skip_compile(attr)
(string(attr) == "trans")
end

function compile_attrs(acs::ReactionNetwork)
function compile_attrs(acs::ReactionNetworkSchema)
species_names = collect(acs[:, :specName])
prm_names = collect(acs[:, :prmName])
varmap = Dict([name => :(state.u[$i]) for (i, name) in enumerate(species_names)])
Expand Down Expand Up @@ -169,7 +169,7 @@ function compile_attrs(acs::ReactionNetwork)
return attrs, transitions, wrap_fun
end

function remove_choose(acs::ReactionNetwork)
function remove_choose(acs::ReactionNetworkSchema)
acs = deepcopy(acs)
pcs = []
for attr in propertynames(acs.subparts)
Expand Down
14 changes: 7 additions & 7 deletions src/interface/create.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# reaction network DSL: CREATE part; reaction line and event parsing

export @ReactionNetwork
export @ReactionNetworkSchema

using MacroTools: prewalk, postwalk, striplines, isexpr
using Symbolics: build_function, get_variables
Expand Down Expand Up @@ -45,7 +45,7 @@ Custom functions and sampleable objects can be used as numeric parameters. Note
# Examples
```julia
acs = @ReactionNetwork begin
acs = @ReactionNetworkSchema begin
1.0, X ⟶ Y
1.0, X ⟶ Y, priority => 6.0, prob => 0.7, capacity => 3.0
1.0, ∅ --> (Poisson(0.3γ)X, Poisson(0.5)Y)
Expand All @@ -57,17 +57,17 @@ end
@solve_and_plot acs
```
"""
macro ReactionNetwork end
macro ReactionNetworkSchema end

macro ReactionNetwork()
macro ReactionNetworkSchema()
return make_ReactionNetwork(:())
end

macro ReactionNetwork(ex)
macro ReactionNetworkSchema(ex)
return make_ReactionNetwork(ex; eval_module = __module__)
end

macro ReactionNetwork(ex, args...)
macro ReactionNetworkSchema(ex, args...)
return make_ReactionNetwork(
generate(Expr(:braces, ex, args...); eval_module = __module__);
eval_module = __module__,
Expand All @@ -78,7 +78,7 @@ function make_ReactionNetwork(ex::Expr; eval_module = @__MODULE__)
blockex = generate(ex; eval_module)
blockex = unblock_shallow!(blockex)

return :(ReactionNetwork(get_data($(QuoteNode(blockex)))...))
return :(ReactionNetworkSchema(get_data($(QuoteNode(blockex)))...))
end

### Functions that process the input and rephrase it as a reaction system ###
Expand Down
19 changes: 19 additions & 0 deletions src/interface/plots.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Plots

function plot_df(df::DataFrames.DataFrame, t_ix = 1)
data = Matrix(df)
t = @view data[:, t_ix]
data_ = @view data[:, setdiff(1:size(data, 2), (t_ix,))]
colnames = reshape(DataFrames.names(df)[setdiff(1:size(data, 2), (t_ix,))], 1, :)

Plots.plot(t, data_, labels = colnames, xlabel = "t")
end

# plot reduction
function AlgebraicAgents._draw(prob::ReactionNetworkProblem, vars = string.(prob.acs[:, :specName]); kwargs...)
p = plot()
for var in vars
p = plot!(p, prob.sol[!, "t"], prob.sol[!, var]; label="$var", xlabel="time", ylabel="quantity", kwargs...)
end
p
end
2 changes: 1 addition & 1 deletion src/interface/reaction_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function recursively_choose(r_line, state)
end
end

function extract_reactants(r_line, state::ReactiveNetwork)
function extract_reactants(r_line, state::ReactionNetworkProblem)
r_line = recursively_choose(r_line, state)

return recursive_find_reactants!(
Expand Down
50 changes: 1 addition & 49 deletions src/interface/solve.jl
Original file line number Diff line number Diff line change
@@ -1,56 +1,8 @@
export @agentize, @solve, @plot
export @optimize, @fit, @fit_and_plot, @build_solver
export @agentize

import MacroTools
import Plots

"""
Convert a model to a `ReactiveNetwork`. If passed a problem instance, return the instance.
# Examples
```julia
@agentize acs tspan = 1:100
```
"""
macro agentize(acsex, args...)
args, kwargs = args_kwargs(args)
quote
if $(esc(acsex)) isa ReactiveNetwork
$(esc(acsex))
else
ReactiveNetwork($(esc(acsex)), $(args...); $(kwargs...))
end
end
end

"""
Solve the problem. Solverargs passed at the calltime take precedence.
# Examples
```julia
@solve prob
@solve prob tspan = 1:100
@solve prob tspan = 100
```
"""
macro solve(probex, args...)
args, kwargs = args_kwargs(args)
mode = find_kwargex_delete!(kwargs, :mode, nothing)
!isnothing(findfirst(el -> el.args[1] == :trajectories, kwargs)) && (mode = :ensemble)

quote
prob = if $(esc(probex)) isa ReactiveNetwork
$(esc(probex))
else
ReactiveNetwork($(esc(probex)), $(args...); $(kwargs...))
end

simulate(prob)
end
end

# auxiliary plotting functions
function plot_summary(s, labels, ixs; kwargs...)
isempty(ixs) && return @warn "Set of species to plot must be non-empty!"
Expand Down
6 changes: 3 additions & 3 deletions src/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const objects_aliases = Dict(
:obs => "obs",
)

const RN_attrs = string.(propertynames(ReactionNetwork().subparts))
const RN_attrs = string.(propertynames(ReactionNetworkSchema().subparts))

function get_attrs(object)
object = object isa Symbol ? objects_aliases[object] : object

return filter(x -> occursin(object, x), RN_attrs)
end

function export_network(acs::ReactionNetwork)
function export_network(acs::ReactionNetworkSchema)
dict = Dict()
for (key, val) in objects_aliases
push!(dict, val => [])
Expand Down Expand Up @@ -113,7 +113,7 @@ function import_network(path::AbstractString)
end
end

function export_network(acs::ReactionNetwork, path::AbstractString)
function export_network(acs::ReactionNetworkSchema, path::AbstractString)
if splitext(path)[2] == ".csv"
exported_network = export_network(acs)
paths = DataFrame(; type = [], path = [])
Expand Down
2 changes: 1 addition & 1 deletion src/operators/equalize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function get_eqs_ff(eq)
end
end

function equalize!(acs::ReactionNetwork, eqs = [])
function equalize!(acs::ReactionNetworkSchema, eqs = [])
specmap = Dict()
for block in eqs
block_alias = findfirst(e -> e[1] == :alias, block)
Expand Down
4 changes: 2 additions & 2 deletions src/operators/joins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end
"""
Prepend species names with a model identifier (unless a global species name).
"""
function prepend!(acs::ReactionNetwork, name = gensym("acs"), eqs = [])
function prepend!(acs::ReactionNetworkSchema, name = gensym("acs"), eqs = [])
specmap = Dict()
for i = 1:nparts(acs, :S)
new_name = normalize_name(name, i, acs[i, :specName], eqs)
Expand Down Expand Up @@ -199,7 +199,7 @@ Model variables / parameter values and metadata are propagated; the last model t
macro join(exs...)
callex = :(
begin
acs_new = ReactionNetwork()
acs_new = ReactionNetworkSchema()
end
)
exs = collect(exs)
Expand Down
Loading

0 comments on commit efea660

Please sign in to comment.