-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Noise scaling update #800
Noise scaling update #800
Conversation
51725fb
to
96dac0c
Compare
a5cfb88
to
2dad1eb
Compare
if haskey(drm_dict, :noise_scaling) | ||
# Finds parameters, species, and variables in the noise scaling term. | ||
ns_expr = drm_dict[:noise_scaling] | ||
ns_syms = [Symbolics.unwrap(sym) for sym in get_variables(ns_expr)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unwrapping
should be handled in the Reaction
constructor. We shouldn't be storing things that aren't unwrapped as we don't want to have to think about this internally later on (at least every internal Catalyst function I've written assumes its input is unwrapped as this is the MTK convention).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now we just use the same convention for Reaction
metadata as MTK/Symbolics does for symbolic variables (i.e. metadata is stored as they are provided by the user, if the user sets a metadata as Num
it remain as a Num
).
If you want we can add a specific routine in the Reaction
constructor that checks if there is any noise_scaling
metadata, and if it is Num
unwraps it at that stage.
src/reactionsystem.jl
Outdated
# Adds parameters, species, and variables to the `ReactionSystem`. | ||
if any(!any(isequal(p1, p2) for p2 in get_ps(rs)) for p1 in ns_ps) | ||
@set! rs.ps = unique([get_ps(rs); ns_ps]) | ||
end | ||
if any(!any(isequal(sp1, sp2) for sp2 in get_species(rs)) for sp1 in ns_sps) | ||
sps_new = unique([get_species(rs); ns_sps]) | ||
vs_old = get_unknowns(rs)[length(get_species(rs))+1 : end] | ||
@set! rs.species = sps_new | ||
@set! rs.unknowns = [sps_new; vs_old] | ||
end | ||
if any(!any(isequal(v1, v2) for v2 in get_unknowns(rs)) for v1 in ns_vs) | ||
us_new = unique([get_unknowns(rs); ns_vs]) | ||
@set! rs.unknowns = us_new | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does union!
not work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can't you use generators to avoid allocating vectors to store the ps
and such?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never really knew about union!
until I discovered that you use it in a few cases, so not that familiar with it. That said I should start using it, will implement it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I can probably use generators, I think at this stage it was late and I was getting increasingly annoyed at all the effort doing this extra step. Will update to use generators though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, I am a little bit uncertain exactly how you suggest we use union!
and generators in these cases
src/reactionsystem.jl
Outdated
ns_syms = [Symbolics.unwrap(sym) for sym in get_variables(ns_expr)] | ||
ns_ps = filter(ModelingToolkit.isparameter, ns_syms) | ||
ns_sps = filter(Catalyst.isspecies, ns_syms) | ||
ns_vs = filter(sym -> !Catalyst.isspecies(sym) && !ModelingToolkit.isparameter(sym), ns_syms) | ||
|
||
# Adds parameters, species, and variables to the `ReactionSystem`. | ||
if any(!any(isequal(p1, p2) for p2 in get_ps(rs)) for p1 in ns_ps) | ||
@set! rs.ps = unique([get_ps(rs); ns_ps]) | ||
end | ||
if any(!any(isequal(sp1, sp2) for sp2 in get_species(rs)) for sp1 in ns_sps) | ||
sps_new = unique([get_species(rs); ns_sps]) | ||
vs_old = get_unknowns(rs)[length(get_species(rs))+1 : end] | ||
@set! rs.species = sps_new | ||
@set! rs.unknowns = [sps_new; vs_old] | ||
end | ||
if any(!any(isequal(v1, v2) for v2 in get_unknowns(rs)) for v1 in ns_vs) | ||
us_new = unique([get_unknowns(rs); ns_vs]) | ||
@set! rs.unknowns = us_new | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ns_syms = [Symbolics.unwrap(sym) for sym in get_variables(ns_expr)] | |
ns_ps = filter(ModelingToolkit.isparameter, ns_syms) | |
ns_sps = filter(Catalyst.isspecies, ns_syms) | |
ns_vs = filter(sym -> !Catalyst.isspecies(sym) && !ModelingToolkit.isparameter(sym), ns_syms) | |
# Adds parameters, species, and variables to the `ReactionSystem`. | |
if any(!any(isequal(p1, p2) for p2 in get_ps(rs)) for p1 in ns_ps) | |
@set! rs.ps = unique([get_ps(rs); ns_ps]) | |
end | |
if any(!any(isequal(sp1, sp2) for sp2 in get_species(rs)) for sp1 in ns_sps) | |
sps_new = unique([get_species(rs); ns_sps]) | |
vs_old = get_unknowns(rs)[length(get_species(rs))+1 : end] | |
@set! rs.species = sps_new | |
@set! rs.unknowns = [sps_new; vs_old] | |
end | |
if any(!any(isequal(v1, v2) for v2 in get_unknowns(rs)) for v1 in ns_vs) | |
us_new = unique([get_unknowns(rs); ns_vs]) | |
@set! rs.unknowns = us_new | |
end | |
end | |
ns_syms = [Symbolics.unwrap(sym) for sym in get_variables(ns_expr)] | |
ns_ps = Iterators.filter(ModelingToolkit.isparameter, ns_syms) | |
ns_sps = Iterators.filter(Catalyst.isspecies, ns_syms) | |
ns_vs = Iterators.filter(sym -> !Catalyst.isspecies(sym) && !ModelingToolkit.isparameter(sym), ns_syms) | |
# Adds parameters, species, and variables to the `ReactionSystem`. | |
@set! rs.ps = union(get_ps(rs), ns_ps]) | |
sps_new = union(get_species(rs), ns_sps) | |
@set! rs.species = sps_new | |
vs_old = @view get_unknowns(rs)[length(get_species(rs))+1 : end] | |
@set! rs.unknowns = union(sps_new, vs_old, ns_vs) | |
end | |
end |
If this is acceptable and works / tests pass I'm fine with merging after switching to this.
Updates the noise scaling feature with: