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

make sure plots are rendered as PNG #142

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"
DisplayAs = "0b91fe84-8a4c-11e9-3e1d-67c38462b6d6"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
LeastSquaresOptim = "0fc2ff8b-aaa3-5acd-a817-1944a5e08891"
Expand Down
7 changes: 5 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
ENV["GKSwstype"] = 322 # workaround for gr segfault on GH actions
ENV["GKSwstype"] = 100 # workaround for gr segfault on GH actions
# ENV["GKS_WSTYPE"] = 100 # workaround for gr segfault on GH actions
using Documenter, LowLevelParticleFilters

using Plots
ENV["JULIA_DEBUG"]=Documenter # Enable this for debugging

using Plots, DisplayAs
gr(format=:png)


Expand Down
16 changes: 10 additions & 6 deletions docs/src/beetle_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The beetle further has two "modes", one where it's moving towards a goal, and on
We load a single experiment from file for the purpose of this example (in practice, there may be hundreds of experiments)
```@example beetle
using LowLevelParticleFilters, LinearAlgebra, StaticArrays, Distributions, Plots, Random
using DisplayAs # hide
using DelimitedFiles
path = "../track.csv"
xyt = readdlm(path)
Expand Down Expand Up @@ -86,7 +87,8 @@ pf = AuxiliaryParticleFilter(AdvancedParticleFilter(N, dynamics, measurement, me
T = length(y)
sol = forward_trajectory(pf,u[1:T],y[1:T])
(; x,w,we,ll) = sol
plot(sol, markerstrokecolor=:auto, m=(2,0.5), format=:png)
plot(sol, markerstrokecolor=:auto, m=(2,0.5))
DisplayAs.PNG(Plots.current()) # hide
```
We can clearly see when the beetle switched mode (state variable 5). This corresponds well to annotations provided by a biologist and is the fundamental question we want to answer with the filtering procedure.

Expand All @@ -108,23 +110,25 @@ plot!(xyt[:,1],xyt[:,2], c=:red, lab="measurement")
```
as well as the angle state variable (we subsample the particles to not get sluggish plots)
```@example beetle
fig2 = scatter(to1series(ϕ.(x)'[:,1:5:end])..., m=(:black, 0.03, 2), lab="", size=(500,300), format=:png)
plot!(identity.(xh[:,4]), lab="Filtered angle", legend=:topleft, ylims=(-30, 70), format=:png)
fig2 = scatter(to1series(ϕ.(x)'[:,1:2:end])..., m=(:black, 0.03, 2), lab="", size=(500,300))
plot!(identity.(xh[:,4]), lab="Filtered angle", legend=:topleft, ylims=(-30, 70))
DisplayAs.PNG(fig2) # hide
```
The particle plot above indicate that the posterior is multimodal. This phenomenon arises due to the simple model that uses an angle that is allowed to leave the interval ``0-2\pi` rad. In this example, we are not interested in the angle, but rather when the beetle switches mode. The filtering distribution above gives a hint at when this happens, but we will not plot the mode trajectory until we have explored smoothing as well.
The particle plot above indicate that the posterior is multimodal. This phenomenon arises due to the simple model that uses an angle that is allowed to leave the interval ``0-2π`` rad. In this example, we are not interested in the angle, but rather when the beetle switches mode. The filtering distribution above gives a hint at when this happens, but we will not plot the mode trajectory until we have explored smoothing as well.

## Smoothing
The filtering results above does not use all the available information when trying to figure out the state trajectory. To do this, we may call a smoother. We use a particle smoother and compute 10 smoothing trajectories.
```@example beetle
M = 10 # Number of smoothing trajectories, NOTE: if this is set higher, the result will be better at the expense of linear scaling of the computational cost.
sb,ll = smooth(pf, M, u, y) # Sample smooting particles (b for backward-trajectory)
sb,ll = smooth(pf, M, u, y) # Sample smoothing particles (b for backward-trajectory)
sbm = smoothed_mean(sb) # Calculate the mean of smoothing trajectories
sbt = smoothed_trajs(sb) # Get smoothing trajectories
plot!(fig1, sbm[1,:],sbm[2,:], lab="xs")
```

```@example beetle
plot!(fig2, identity.(sbm'[:,4]), lab="smoothed", format=:png)
plot!(fig2, identity.(sbm'[:,4]), lab="smoothed")
DisplayAs.PNG(fig2) # hide
```
We see that the smoothed trajectory may look very different from the filter trajectory. This is an indication that it's hard to tell what state the beetle is currently in, but easier to look back and tell what state the beetle must have been in at a historical point.

Expand Down
5 changes: 5 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Defining a particle filter is straightforward, one must define the distribution

```@example lingauss
using LowLevelParticleFilters, LinearAlgebra, StaticArrays, Distributions, Plots
using DisplayAs # hide
```

Define problem
Expand Down Expand Up @@ -113,6 +114,7 @@ If you want to perform filtering using vectors of inputs and measurements, try a
sol = forward_trajectory(pf, u, y) # Filter whole vectors of signals
x̂,ll = mean_trajectory(pf, u, y)
plot(sol, xreal=xs, markersize=2)
DisplayAs.PNG(Plots.current()) # hide
```
`u` ad `y` are then assumed to be vectors of vectors. StaticArrays is recommended for maximum performance.

Expand Down Expand Up @@ -153,6 +155,7 @@ downsample = 5
plot(vecvec_to_mat(x), l=(4,), layout=(2,1), show=false)
scatter!(xbt[1, 1:downsample:end, :]', subplot=1, show=false, m=(1,:black, 0.5), lab="")
scatter!(xbt[2, 1:downsample:end, :]', subplot=2, m=(1,:black, 0.5), lab="")
DisplayAs.PNG(Plots.current()) # hide
```


Expand Down Expand Up @@ -295,13 +298,15 @@ sol = forward_trajectory(apf, u, y, p)

```@example lingauss
plot(sol, xreal=x)
DisplayAs.PNG(Plots.current()) # hide
```
We can even use this type as an AuxiliaryParticleFilter

```@example lingauss
apfa = AuxiliaryParticleFilter(apf)
sol = forward_trajectory(apfa, u, y, p)
plot(sol, dim=1, xreal=x) # Same as above, but only plots a single dimension
DisplayAs.PNG(Plots.current()) # hide
```

See the tutorials section for more advanced examples, including state estimation for DAE (Differential-Algebraic Equation) systems.
Expand Down
Loading