Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Example: Measuring activation time distribution #1210

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Apr 1, 2025

Combines a decent couple of concepts from various parts of the docs, and also shows how to use EnsembleProblem output functions.

@@ -40,6 +40,7 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Copy link
Member Author

Choose a reason for hiding this comment

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

A bit annoying to add a dependency for a tutorial. However, the density function feels quite important for stochastic -related analysis, so probably something users should know about and which we might want to use in other tutorials as well. Also, it is part of the Plots system, which should make it less of an addition.

Copy link
Member

Choose a reason for hiding this comment

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

We probably should at some point just switch everything to Makie-verse given we are now pulling it in, but that is a longer-term goal I guess.

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed

Copy link
Member

@isaacsas isaacsas left a comment

Choose a reason for hiding this comment

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

Feel free to merge after you've updated per my comments as you see fit.

Comment on lines +33 to +35
condition(u, t, integrator) = integrator[:X] > 100.0
affect!(integrator) = terminate!(integrator)
callback = DiscreteCallback(condition, affect!)
Copy link
Member

Choose a reason for hiding this comment

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

Can you not use a functional affect and store this in the system?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does system events store non-trivial affects like termiante? If it is easier to do I am happy to change, however, it doesn't hurt to show how more complicated events can be implemented through callbacks.

Comment on lines +39 to +43
```@example activation_time_distribution_measurement
function output_func(sol, i)
(sol.t[end] == tspan[2]) && @warn "A simulation did not activate during the given time span."
return (sol.t[end], false)
end
Copy link
Member

Choose a reason for hiding this comment

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

A fancier approach would be to use the integrator interface and keep stepping until a callback calls terminate! (assuming it will always be called eventually, but could with some non-zero probability be after tspan[2] -- I haven't checked the problem carefully enough to understand if that is the case 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.

I agree that this would be a cooler way. Maybe add a note about it for now? Now in principle that this is doable, but haven't actually seen it in practise.

Comment on lines 48 to 52
Finally, we can plot the distribution of activation times. For this, we will use the [StatsPlots.jl](https://docs.juliaplots.org/latest/generated/statsplots/) package's `density` function (essentially a smoothed histogram). The input to `density` is the activation times (which our output function has saved to `esol.u`).
```@example activation_time_distribution_measurement
using StatsPlots
density(esol.u; label = "Activation time distribution", xlabel = "Activation time")
```
Copy link
Member

Choose a reason for hiding this comment

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

Why not just use a normal histogram with the option to have it normalize to a pdf? That shows the actual sample noise in the bins instead of smoothing everything out, and avoids the StatsPlots dependency.

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 generally have preferred to use density as I feel those plots are better and easier to work with. But if avoiding StatsPlots is priority I can plot a histogram and then link to the desnity function suggesting that readers should try it out as well?

TorkelE and others added 2 commits April 9, 2025 10:52
…n_measurement.md

Co-authored-by: Sam Isaacson <isaacsas@users.noreply.github.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants