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

Internation functions for conversion between various forms of representing states/parameters etc. (e.g. symbolics or indexes) #2404

Closed
TorkelE opened this issue Dec 30, 2023 · 5 comments

Comments

@TorkelE
Copy link
Member

TorkelE commented Dec 30, 2023

Especially the last while when I have made interfaces between MTK on various packages, I often find myself to (internally) need to convert various forms of providing e.g. parameter values.

Examples

Converting Symbolic parameters and variables to index for Bifurcation kit

BifurcationKit wants to know the index (in the parameter vector) of the bifurcation parameter (and the variable to plot). But following MTK standard the input is typically a symbolic. E.g. I need to convert a symbolic parameter k to its index in the parameter(system) vector.

Converting values given on map form to sorted vector

In many cases (BifurcationKit, HomotopyContinuation, etc.) e.g. the parameters should just be a vector. I then need to convert something like [p1 => 1.0, p2 => 3.5, ...] to [1.0, 3.5 , ...]where everything have the right index. The parameter input could also e.g. be aDict`.

Spatial Jump Processes require mapping variable index to values

Internally, when spatial jumps (and non-spatial ones) are created, you need a form like: [1 => 1.0, 2 => 3.5, ...] (denoting the rate of the mass action jump of the first and second state, respectively). Here, I need to convert something like [p1 => 1.0, p2 => 3.5, ...]to this form.

Other stuff

Sometimes I also need to go the other way, converting something on an integer (e.g. parameter j) to a symbolic form (getting the j'th parameter. Furthermore, sometimes the input uses Symbol form (e.g. some cases in PEtab). Here, I can end up with something that is either
[p1 => 1.0, p2 => 3.5, ...]
[:p1 => 1.0, :p2 => 3.5, ...]
[1.0, 3.5, ...]
And need to convert it e.g. to the last form.

What I want to create

Basically, I want to create 3 sets of functions:

  • int_representation(sys, x). Takes stuff to integer form. E.g. if you give it p1, :p1, or 1, it converts it to 1 (if p1 is the first parameter. Similarily If you have a vector of syms [p1, p2, p3] it converts these to a vector of their indexes. If you have a map/dict, it does similar (e.g. convert [p1 => 1.0, p2 => 3.5, ...] to [1 => 1.0, 2 => 3.5, ...].
  • sym_representation(sys, x). Similar, but instead replaces with symbolic values. So 1 becomes p1 and so on.
  • sorted_values_form(sys, x). Converts to a form like [1.0, 3.5, ...]. Inptut can be a map, a dict, or a vector already on the correct form.

This would work for both states and parameters (and throw error if you got mixed values, symbols not existing, etc.).

What to do

Some of these function as more or less already implemented (and some other are only in Catalyst). However, currently I have to check the input form and use the correct one (e.g. Do I already have a Int64, then do nothing. Did I get a Symbolic form, then use this function). I want to be able to have single functions ensuring a specific form.

I am happy to create this and let it live in Catalyst, and MTK would not have to worry about it (I already have 3 extensions, the spatial stuff, and some other cases, where these are useful). However, in principle, it is stuff that could exist in MTK. So if you want it here I could make the PR to MTK (or rather, appropriate internal package) as well. There are definitely stuff which can be reused already, but honestly, most of it is rather easy to write (and not very performance critical), so the writing is not big problem, the question is where to put it.

@ChrisRackauckas
Copy link
Member

This is all just the symbolic indexing interface. I would start by reading that. These functions that you are asking for are already core documented functions that are used in tutorials, except sorted_values_form is varmap_to_vars in the MTK documentation. Also, "Converting Symbolic parameters and variables to index for Bifurcation kit" is not well-defined because it's generally not an index.

@TorkelE
Copy link
Member Author

TorkelE commented Dec 30, 2023

So that is what I meant that a lot of this I would reuse internally. However, in most cases, these functions take a specific input. If you have things on some forms (e.g. [:k => 1,0], also [1 => 1,0] but that is just absurd and think is only used as input so fine) you get an error, so you have to check for that first.

I think SymbolicIndexingInterface (what I read) only defines so symbolic indexing works for stuff if they implement it that way. So we wouldn't need it if everyone adopted the SciML standard here. However, not everyone has. Maybe for some packages like JumpProcesses and StructuralIdentifiability we will be there at some point (but we need this stuff until then), and I do not expect HomotopyContinuation to support it.

@TorkelE
Copy link
Member Author

TorkelE commented Dec 30, 2023

Maybe the easiest is I just implement in Catalyst? It will probably make the code base shorter there already. Then we can always port to wider SciML if we want to have.

@ChrisRackauckas
Copy link
Member

My point is that the functions already exist so correctly using the documented functions is probably a good idea.

@TorkelE
Copy link
Member Author

TorkelE commented Dec 31, 2023

Sounds good. I will go through and use what function there are, and where there are missing functions I will build functions using available functions and out those in Catalyst.

@TorkelE TorkelE closed this as completed Dec 31, 2023
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

No branches or pull requests

2 participants