Skip to content

Commit 131c7ad

Browse files
committed
update
1 parent 8203687 commit 131c7ad

File tree

5 files changed

+21
-31
lines changed

5 files changed

+21
-31
lines changed

src/Catalyst.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export ODEProblem,
7777
const ExprValues = Union{Expr, Symbol, Float64, Int, Bool}
7878
include("expression_utils.jl")
7979
include("reaction_network.jl")
80-
export @reaction_network, @add_reactions, @reaction, @species
80+
export @reaction_network, @add_reactions, @reaction, @species, @noise_scaling_parameters
8181

8282
# registers CRN specific functions using Symbolics.jl
8383
include("registered_functions.jl")

src/networkapi.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ end
504504

505505
# convert symbol of the form :sys.a.b.c to a symbolic a.b.c
506506
function _symbol_to_var(sys, sym)
507+
if hasproperty(sys, sym)
507508
var = getproperty(sys, sym, namespace = false)
508509
else
509510
strs = split(String(sym), "") # need to check if this should be split of not!!!

src/reaction_network.jl

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ macro species(ex...)
153153
# put back the vector of the new species symbols
154154
vars.args[idx] = lastarg
155155

156+
println(vars)
156157
esc(vars)
157158
end
158159

@@ -369,6 +370,9 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
369370
parameters_declared = extract_syms(options, :parameters)
370371
variables = extract_syms(options, :variables)
371372

373+
# Handles noise scaling parameters.
374+
noise_scaling_pexpr = handle_noise_scaling_parameters!(parameters_declared, options)
375+
372376
# handle independent variables
373377
if haskey(options, :ivs)
374378
ivs = Tuple(extract_syms(options, :ivs))
@@ -405,6 +409,7 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
405409
sexprs = get_sexpr(species_extracted, options; iv_symbols = ivs)
406410
vexprs = haskey(options, :variables) ? options[:variables] : :()
407411
pexprs = get_pexpr(parameters_extracted, options)
412+
ns_ps, ns_pssym = scalarize_macro(haskey(options, :noise_scaling_parameters), noise_scaling_pexpr, "ns_ps")
408413
ps, pssym = scalarize_macro(!isempty(parameters), pexprs, "ps")
409414
vars, varssym = scalarize_macro(!isempty(variables), vexprs, "vars")
410415
sps, spssym = scalarize_macro(!isempty(species), sexprs, "specs")
@@ -414,11 +419,14 @@ function make_reaction_system(ex::Expr; name = :(gensym(:ReactionSystem)))
414419
push!(rxexprs.args, get_rxexprs(reaction))
415420
end
416421

417-
#append!(ps.args, noise_scaling_p_args)
418-
422+
println(ps)
423+
println(ns_ps)
424+
println(sps)
425+
419426
# Returns the rephrased expression.
420427
quote
421428
$ps
429+
$ns_ps
422430
$ivexpr
423431
$vars
424432
$sps
@@ -559,26 +567,8 @@ function get_pexpr(parameters_extracted, options)
559567
pexprs = (haskey(options, :parameters) ? options[:parameters] :
560568
(isempty(parameters_extracted) ? :() : :(@parameters)))
561569
foreach(p -> push!(pexprs.args, p), parameters_extracted)
562-
append!(pexprs.args, get_noise_scaling_pexpr(options))
563570
pexprs
564571
end
565-
# Extracts any decalred noise scaling parameters.
566-
function get_noise_scaling_pexpr(options)
567-
haskey(options, :noise_scaling_parameters) || return []
568-
ns_expr = options[:noise_scaling_parameters]
569-
for idx = length(ns_expr.args):-1:3
570-
if (ns_expr.args[idx] isa Symbol) || # Parameter on form η.
571-
(ns_expr.args[idx] isa Expr) && (ns_expr.args[idx].head == :ref) || # Parameter on form η[1:3].
572-
(ns_expr.args[idx] isa Expr) && (ns_expr.args[idx].head == :(=)) # Parameter on form η=0.1.
573-
if idx < length(ns_expr.args) && (ns_expr.args[idx+1] isa Expr) && (ns_expr.args[idx+1].head == :vect)
574-
push!(ns_expr.args[idx+1].args,:(noisescalingparameter=true))
575-
else
576-
insert!(ns_expr.args, idx+1, :([noisescalingparameter=true]))
577-
end
578-
end
579-
end
580-
return ns_expr.args[3:end]
581-
end
582572

583573
# Creates the reaction vector declaration statement.
584574
function get_rxexprs(rxstruct)

src/reactionsystem.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ drop_dynamics(s) = isconstant(s) || isbc(s) || (!isspecies(s))
5858

5959
# Denotes that a parameter controls the scaling of noise in the CLE.
6060
struct NoiseScalingParameter end
61-
Symbolics.option_to_metadata_type(::Val{:noisescalingparameter}) = NoiseScalingParameter
61+
Symbolics.option_to_metadata_type(::Val{:noise_scaling_parameter}) = NoiseScalingParameter
6262

63-
isnoisescalingparameter(s::Num) = isnoisescalingparameter(MT.value(s))
64-
function isnoisescalingparameter(s)
63+
is_noise_scaling_parameter(s::Num) = is_noise_scaling_parameter(MT.value(s))
64+
function is_noise_scaling_parameter(s)
6565
MT.getmetadata(s, NoiseScalingParameter, false)
6666
end
6767

@@ -1506,10 +1506,13 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
15061506
default_u0 = Dict(), default_p = Dict(), defaults = _merge(Dict(default_u0), Dict(default_p)),
15071507
kwargs...)
15081508
spatial_convert_err(rs::ReactionSystem, SDESystem)
1509-
isnothing(noise_scaling) && (noise_scaling = get_noise_scaling(rs)) # Required before final deprication of previous noise scaling functionality.
15101509

15111510
flatrs = Catalyst.flatten(rs)
15121511
error_if_constraints(SDESystem, flatrs)
1512+
if any(isnoisescalingparameter, get_ps(flatrs))
1513+
any(is_noise_scaling_parameter.(parameters(rs))) && error("You have declared some paraemters as noise scaling parameters, and also given a \"noise_scaling\" argument to SDEProblem. Please remove the \"noise_scaling\", as this way of scaling CLE noise is being depricated.")
1514+
@warn "Passing noise scaling input into SDEProblem will be deprecated. New standard is to declare one (or several) paraemter as noise scaling parameters when the ReactionSystem is created. Please read https://docs.sciml.ai/Catalyst/stable/catalyst_applications/advanced_simulations/#Scaling-the-noise-magnitude-in-the-chemical-Langevin-equations."
1515+
end
15131516

15141517
if noise_scaling isa AbstractArray
15151518
(length(noise_scaling) != numreactions(flatrs)) &&
@@ -1547,7 +1550,7 @@ end
15471550

15481551
# Extracts any noise scaling parameters from a reaction system.
15491552
function get_noise_scaling(rs::ReactionSystem)
1550-
ns_params = filter(p -> isnoisescalingparameter(p), parameters(rs))
1553+
ns_params = filter(p -> is_noise_scaling_parameter(p), parameters(rs))
15511554
if isempty(ns_params)
15521555
return nothing
15531556
elseif length(ns_params) == 1
@@ -1647,10 +1650,6 @@ function DiffEqBase.SDEProblem(rs::ReactionSystem, u0, tspan,
16471650
include_zero_odes = true, checks = false,
16481651
check_length = false,
16491652
remove_conserved = false, kwargs...)
1650-
if !isnothing(noise_scaling)
1651-
any(isnoisescalingparameter.(parameters(rs))) && error("You have declared some paraemters as noise scaling parameters, and also given a \"noise_scaling\" argument to SDEProblem. Please remove the \"noise_scaling\", as this way of scaling CLE noise is being depricated.")
1652-
@warn "Passing noise scaling input into SDEProblem will be depricated. New standard is to declare one (or several) paraemter as noise scaling parameters when the ReactionSystem is created. Please read https://docs.sciml.ai/Catalyst/stable/catalyst_applications/advanced_simulations/#Scaling-the-noise-magnitude-in-the-chemical-Langevin-equations."
1653-
end
16541653
u0map = symmap_to_varmap(rs, u0)
16551654
pmap = symmap_to_varmap(rs, p)
16561655
sde_sys = convert(SDESystem, rs; noise_scaling, name, combinatoric_ratelaws,

test/model_simulation/simulate_SDEs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ end
158158
# Complicated test with many combinations of options.
159159
let
160160
noise_scaling_network = @reaction_network begin
161-
@parameters k1 par1 [description="Parameter par1"] par2 η1 [noisescalingparameter=true]
161+
@parameters k1 par1 [description="Parameter par1"] par2 η1 [noise_scaling_parameter=true]
162162
@noise_scaling_parameters η2=0.0 [description="Parameter η2"] η3=1.0 η4
163163
(p, d), 0 X1
164164
(k1, k2), X1 X2

0 commit comments

Comments
 (0)