Skip to content
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

Merged
merged 9 commits into from
May 23, 2024
Merged

Noise scaling update #800

merged 9 commits into from
May 23, 2024

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Apr 1, 2024

Updates the noise scaling feature with:

  • Ensures that species/variables/parameters introduced by the noise scaling metadata is always properly added to the new system.
  • Add additional tests.

@TorkelE TorkelE mentioned this pull request Apr 3, 2024
44 tasks
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)]
Copy link
Member

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).

Copy link
Member Author

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.

Comment on lines 1237 to 1251
# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does union! not work?

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member Author

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

Comment on lines 1232 to 1251
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
Copy link
Member

@isaacsas isaacsas May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

@TorkelE TorkelE merged commit 1d1f4f8 into master May 23, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants