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

feat: add all_solvable_symbols and all_symbols #18

Merged
merged 6 commits into from
Dec 27, 2023

Conversation

AayushSabharwal
Copy link
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

Copy link

codecov bot commented Dec 18, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (0e67d45) 38.77% compared to head (ae50f9d) 50.96%.

Files Patch % Lines
src/interface.jl 50.00% 2 Missing ⚠️
src/symbol_cache.jl 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master      #18       +/-   ##
===========================================
+ Coverage   38.77%   50.96%   +12.18%     
===========================================
  Files           5        5               
  Lines          98      104        +6     
===========================================
+ Hits           38       53       +15     
+ Misses         60       51        -9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ChrisRackauckas
Copy link
Member

What's the next steps here?

@AayushSabharwal
Copy link
Member Author

Merging

src/interface.jl Outdated
@@ -110,3 +110,19 @@ Check if `sys` has a constant structure. Constant structure systems do not chang
number of variables or parameters over time.
"""
constant_structure(sys) = constant_structure(symbolic_container(sys))

"""
all_solvable_symbols(sys)
Copy link
Member

Choose a reason for hiding this comment

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

This name isn't very informative. Also, I think we want it to be a singleton? sol[allvariables], sol[allsymbols]? That lowers to these functions.

And for the API, the user needs to know what the symbol the value corresponds to, since they aren't giving it to you. I would think that this would need to return Pairs between sym => value since otherwise the ordering is simply undefined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. I'll address this after fixing the RAT bugs in other repositories

Copy link
Member Author

Choose a reason for hiding this comment

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

One thing to consider: allvariables will index variables and observed quantities, which I guess should be fine. allsymbols will also include parameters, which for e.g. solutions will return a Vector{any} since sol[x] is an array but sol[p] is a single value.

I'm also documenting the singletons as returning all variables/symbols at the last timestep so for future implementation of dynamic states the interface is clear.

Copy link
Member Author

@AayushSabharwal AayushSabharwal Dec 26, 2023

Choose a reason for hiding this comment

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

Or, wait, did you mean that sol[allvariables] should return a Vector{Pair}? In which case should it be the initial value of each variable?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think so. I don't see any other way for it to be very useful. Or we should document how to easily get that pair.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. A separate allparameters?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep. And no allsymbols? Or do we keep it for the independent variables?

Copy link
Member

Choose a reason for hiding this comment

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

No allsymbols.

Copy link
Member Author

Choose a reason for hiding this comment

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

No wait, we don't need allparameters either 😅 . We already have parameter_values(sys) which returns the parameter vector. setp support requires that the returned vector be a mutable reference to the actual parameter vector, so mutating it will mutate the parameters. getp(sys, allparameters)(sol) is just parameter_values(sol). setp(sys, allparameters)(sol, new_value) is just parameter_values(sol) .= new_value

Copy link
Member

Choose a reason for hiding this comment

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

Anything like this is something that should be described in tutorials. Make a small MTK model and show all of the tricks.

@AayushSabharwal
Copy link
Member Author

Note that docs won't render properly until RAT and MTK get merged

This also works for arrays or tuples of variables, including observed quantities and
independent variables:
```@example Usage
sol[[x, y]]
Copy link
Member

Choose a reason for hiding this comment

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

BTW, is this using the optimized call now, i.e. a single call to the observed function?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it's broadcasted over the array of symbols, so each one is computed individually. A single call to observed behaves differently:

julia> SymbolicIndexingInterface.observed(sol, [x, y])(sol.u, sol.prob.p, sol.t)
2-element Vector{Vector{Float64}}:
 [1.6563910957343146, 0.13029732534611174, 0.0008464888325952026, 1.023645017393648]
 [1.9960454103704428, 0.0014132521265988688, 9.986094350108851e-8, 1.0002823510089436]
julia> SymbolicIndexingInterface.observed(sol, (x, y))(sol.u, sol.prob.p, sol.t)
(x(t), y(t))

For state variables (whose values are in sol.u) I'd imagine broadcasting is faster than the observed method? It avoids a generated function.

Copy link
Member

Choose a reason for hiding this comment

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

For state variables it's faster, then for observed it's faster to clump them up. Optimizing this a bit is probably a good idea in the future.

```@example Usage
parameter_values(prob) .= [29.0, 11.0, 2.5]
parameter_values(prob)
```
Copy link
Member

Choose a reason for hiding this comment

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

We should also mention symbolic interpolation indexing, i.e. sol(t; idxs=x), plot indexing plot(sol, idxs=(x,y))

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@ChrisRackauckas
Copy link
Member

Looking good! Let's do the merges then loop back for the docs.

@ChrisRackauckas ChrisRackauckas merged commit 0ad8caa into master Dec 27, 2023
9 of 12 checks passed
@ChrisRackauckas ChrisRackauckas deleted the as/all-symbols branch December 27, 2023 15:47
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.

2 participants