-
Notifications
You must be signed in to change notification settings - Fork 39
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
Support makie backends #181
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d254b7
Add multiple backend support
BambOoxX fd53c55
Add nans instead of doubling calls to be compatible with CairoMakie
BambOoxX bb13ca8
move RecipesBase from deps to weakdeps
BambOoxX 1e3a05a
generalize `isplit`
BambOoxX 3323ae6
Correct whitespaces
BambOoxX a0e3249
handle empty special points
BambOoxX 0c7128a
introduce `set_plot_backend!`
BambOoxX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,172 @@ | ||
using GLMakie: Point2f0 | ||
using Makie: Point2f0 | ||
|
||
function GLMakie.convert_arguments(::PointBased, contres::AbstractBranchResult, vars = nothing, applytoY = identity, applytoX = identity) | ||
function Makie.convert_arguments(::PointBased, contres::AbstractBranchResult, vars=nothing, applytoY=identity, applytoX=identity) | ||
ind1, ind2 = get_plot_vars(contres, vars) | ||
return ([Point2f0(i, j) for (i, j) in zip(map(applytoX, getproperty(contres.branch, ind1)), map(applytoY, getproperty(contres.branch, ind2)))],) | ||
end | ||
|
||
function isplit(x::AbstractVector{T}, indices::AbstractVector{<:Integer}, splitval::Bool = true) where {T<:Real} | ||
if isdefined(Main,:CairoMakie) && Makie.current_backend() == Main.CairoMakie | ||
xx = similar(x, length(x) + 2 * (length(indices) - 1)) | ||
for (i, ind) in enumerate(indices) | ||
if i == 1 | ||
xx[1:ind+1] .= @views x[1:ind+1] | ||
else | ||
xx[(2*(i-1)+1).+(indices[i-1]-1:ind)] .= @views x[(indices[i-1]-1:ind).+1] | ||
end | ||
if ind != last(indices) | ||
xx[2*(i-1)+ind] = xx[2*(i-1)+ind-1] | ||
xx[2*(i-1)+ind+1] = splitval ? NaN : xx[2*(i-1)+ind-1] | ||
end | ||
end | ||
|
||
return xx | ||
else | ||
return x | ||
end | ||
end | ||
|
||
function plot!(ax1, contres::AbstractBranchResult; | ||
plotfold = false, | ||
plotstability = true, | ||
plotspecialpoints = true, | ||
putspecialptlegend = true, | ||
filterspecialpoints = false, | ||
vars = nothing, | ||
linewidthunstable = 1.0, | ||
linewidthstable = 3.0linewidthunstable, | ||
plotcirclesbif = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My coding style is to have spaces around = |
||
branchlabel = "", | ||
applytoY = identity, | ||
applytoX = identity) | ||
plotfold=false, | ||
plotstability=true, | ||
plotspecialpoints=true, | ||
putspecialptlegend=true, | ||
filterspecialpoints=false, | ||
vars=nothing, | ||
linewidthunstable=1.0, | ||
linewidthstable=3.0linewidthunstable, | ||
plotcirclesbif=true, | ||
branchlabel=nothing, | ||
applytoY=identity, | ||
applytoX=identity) | ||
|
||
# names for axis labels | ||
ind1, ind2 = get_plot_vars(contres, vars) | ||
xlab, ylab = get_axis_labels(ind1, ind2, contres) | ||
|
||
# stability linewidth | ||
linewidth = linewidthunstable | ||
indices = getproperty.(contres.specialpoint, :idx) | ||
# isplit required to work with CairoMakie due to change of linewidth for stability | ||
if _hasstability(contres) && plotstability | ||
linewidth = map(x -> isodd(x) ? linewidthstable : linewidthunstable, contres.stable) | ||
end | ||
if branchlabel == "" | ||
lines!(ax1, map(applytoX, getproperty(contres.branch, ind1)), map(applytoY, getproperty(contres.branch, ind2)); linewidth) | ||
else | ||
lines!(ax1, map(applytoX, getproperty(contres.branch, ind1)), map(applytoY, getproperty(contres.branch, ind2)), linewidth = linewidth, label = branchlabel) | ||
linewidth = isplit(map(x -> x ? linewidthstable : linewidthunstable, contres.stable), indices, false) | ||
end | ||
xbranch = isplit(map(applytoX, getproperty(contres.branch, ind1)), indices) | ||
ybranch = isplit(map(applytoY, getproperty(contres.branch, ind2)), indices) | ||
lines!(ax1, xbranch, ybranch, linewidth=linewidth, label=branchlabel) | ||
ax1.xlabel = xlab | ||
ax1.ylabel = ylab | ||
|
||
# display bifurcation points | ||
bifpt = filter(x -> (x.type != :none) && (x.type != :endpoint) && (plotfold || x.type != :fold) && (x.idx <= length(contres)-1), contres.specialpoint) | ||
bifpt = filter(x -> (x.type != :none) && (x.type != :endpoint) && (plotfold || x.type != :fold) && (x.idx <= length(contres) - 1), contres.specialpoint) | ||
if length(bifpt) >= 1 && plotspecialpoints #&& (ind1 == :param) | ||
if filterspecialpoints == true | ||
bifpt = filterBifurcations(bifpt) | ||
end | ||
scatter!(ax1, | ||
[applytoX(getproperty(contres[pt.idx], ind1)) for pt in bifpt], | ||
[applytoY(getproperty(contres[pt.idx], ind2)) for pt in bifpt]; | ||
marker = map(x -> (x.status == :guess) && (plotcirclesbif==false) ? :rect : :circle, bifpt), | ||
markersize = 10, | ||
color = map(x -> get_color(x.type), bifpt), | ||
scatter!(ax1, | ||
[applytoX(getproperty(contres[pt.idx], ind1)) for pt in bifpt], | ||
[applytoY(getproperty(contres[pt.idx], ind2)) for pt in bifpt]; | ||
marker=map(x -> (x.status == :guess) && (plotcirclesbif == false) ? :rect : :circle, bifpt), | ||
markersize=10, | ||
color=map(x -> get_color(x.type), bifpt), | ||
) | ||
end | ||
|
||
# add legend for bifurcation points | ||
if putspecialptlegend && length(bifpt) >= 1 | ||
bps = unique(x -> x.type, [pt for pt in bifpt if (pt.type != :none && (plotfold || pt.type != :fold))]) | ||
(length(bps) == 0) && return | ||
for pt in bps | ||
scatter!(ax1, | ||
[applytoX(getproperty(contres[pt.idx], ind1))], | ||
[applytoY(getproperty(contres[pt.idx], ind2))]; | ||
color = get_color(pt.type), | ||
markersize = 10, | ||
label = "$(pt.type)") | ||
scatter!(ax1, | ||
[applytoX(getproperty(contres[pt.idx], ind1))], | ||
[applytoY(getproperty(contres[pt.idx], ind2))]; | ||
color=get_color(pt.type), | ||
markersize=10, | ||
label="$(pt.type)") | ||
end | ||
GLMakie.axislegend(ax1, merge = true, unique = true) | ||
Makie.axislegend(ax1, merge=true, unique=true) | ||
end | ||
ax1 | ||
end | ||
|
||
function plot_branch_cont(contres::ContResult, | ||
state, | ||
iter, | ||
plotuserfunction; | ||
plotfold = false, | ||
plotstability = true, | ||
plotspecialpoints = true, | ||
putspecialptlegend = true, | ||
filterspecialpoints = false, | ||
linewidthunstable = 1.0, | ||
linewidthstable = 3.0linewidthunstable, | ||
plotcirclesbif = true, | ||
applytoY = identity, | ||
applytoX = identity) | ||
state, | ||
iter, | ||
plotuserfunction; | ||
plotfold=false, | ||
plotstability=true, | ||
plotspecialpoints=true, | ||
putspecialptlegend=true, | ||
filterspecialpoints=false, | ||
linewidthunstable=1.0, | ||
linewidthstable=3.0linewidthunstable, | ||
plotcirclesbif=true, | ||
applytoY=identity, | ||
applytoX=identity) | ||
sol = getsolution(state) | ||
if length(contres) == 0; return ; end | ||
|
||
if length(contres) == 0 | ||
return | ||
end | ||
|
||
# names for axis labels | ||
ind1, ind2 = get_plot_vars(contres, nothing) | ||
xlab, ylab = get_axis_labels(ind1, ind2, contres) | ||
|
||
# stability linewidth | ||
linewidth = linewidthunstable | ||
if _hasstability(contres) && plotstability | ||
linewidth = map(x -> isodd(x) ? linewidthstable : linewidthunstable, contres.stable) | ||
end | ||
fig = Figure(size = (1200, 700)) | ||
ax1 = fig[1:2, 1] = Axis(fig, xlabel = String(xlab), ylabel = String(ylab), tellheight = true) | ||
ax2 = fig[1, 2] = Axis(fig, xlabel = "step [$(state.step)]", ylabel = String(xlab)) | ||
lines!(ax2, contres.step, contres.param, linewidth = linewidth) | ||
|
||
fig = Figure(size=(1200, 700)) | ||
ax1 = fig[1:2, 1] = Axis(fig, xlabel=String(xlab), ylabel=String(ylab), tellheight=true) | ||
|
||
ax2 = fig[1, 2] = Axis(fig, xlabel="step [$(state.step)]", ylabel=String(xlab)) | ||
lines!(ax2, contres.step, contres.param, linewidth=linewidth) | ||
|
||
if compute_eigenelements(iter) | ||
eigvals = contres.eig[end].eigenvals | ||
ax_ev = fig[3, 1:2] = Axis(fig, xlabel = "ℜ", ylabel = "ℑ") | ||
scatter!(ax_ev, real.(eigvals), imag.(eigvals), strokewidth = 0, markersize = 10, color = :black) | ||
ax_ev = fig[3, 1:2] = Axis(fig, xlabel="ℜ", ylabel="ℑ") | ||
scatter!(ax_ev, real.(eigvals), imag.(eigvals), strokewidth=0, markersize=10, color=:black) | ||
# add stability boundary | ||
maxIm = maximum(imag, eigvals) | ||
minIm = minimum(imag, eigvals) | ||
if maxIm-minIm < 1e-6 | ||
if maxIm - minIm < 1e-6 | ||
maxIm, minIm = 1, -1 | ||
end | ||
lines!(ax_ev, [0, 0], [maxIm, minIm], color = :blue, linewidth = linewidthunstable) | ||
lines!(ax_ev, [0, 0], [maxIm, minIm], color=:blue, linewidth=linewidthunstable) | ||
end | ||
|
||
# plot arrow to indicate the order of computation | ||
if length(contres) > 1 | ||
x = contres.branch[end].param | ||
y = getproperty(contres.branch,1)[end] | ||
y = getproperty(contres.branch, 1)[end] | ||
u = contres.branch[end].param - contres.branch[end-1].param | ||
v = getproperty(contres.branch,1)[end] - getproperty(contres.branch,1)[end-1] | ||
GLMakie.arrows!(ax1, [x], [y], [u], [v], color = :green, arrowsize = 20,) | ||
v = getproperty(contres.branch, 1)[end] - getproperty(contres.branch, 1)[end-1] | ||
Makie.arrows!(ax1, [x], [y], [u], [v], color=:green, arrowsize=20,) | ||
end | ||
|
||
plot!(ax1, contres; plotfold, plotstability, plotspecialpoints, putspecialptlegend, filterspecialpoints, linewidthunstable, linewidthstable, plotcirclesbif, applytoY, applytoX) | ||
|
||
if isnothing(plotuserfunction) == false | ||
ax_perso = fig[2, 2] = Axis(fig, tellheight = true) | ||
plotuserfunction(ax_perso, sol.u, sol.p; ax1 = ax1) | ||
ax_perso = fig[2, 2] = Axis(fig, tellheight=true) | ||
plotuserfunction(ax_perso, sol.u, sol.p; ax1=ax1) | ||
end | ||
|
||
display(fig) | ||
fig | ||
end | ||
|
||
function plot(contres::AbstractBranchResult; kP...) | ||
if length(contres) == 0; return ;end | ||
if length(contres) == 0 | ||
return | ||
end | ||
|
||
ind1, ind2 = get_plot_vars(contres, nothing) | ||
xlab, ylab = get_axis_labels(ind1, ind2, contres) | ||
|
||
fig = Figure() | ||
ax1 = fig[1, 1] = Axis(fig, xlabel = String(xlab), ylabel = String(ylab), tellheight = true) | ||
ax1 = fig[1, 1] = Axis(fig, xlabel=String(xlab), ylabel=String(ylab), tellheight=true) | ||
|
||
plot!(ax1, contres; kP...) | ||
display(fig) | ||
|
@@ -150,17 +175,19 @@ | |
|
||
plot(brdc::DCResult; kP...) = plot(brdc.branches...; kP...) | ||
|
||
function plot(brs::AbstractBranchResult...; | ||
branchlabel = ["$i" for i=1:length(brs)], | ||
kP...) | ||
if length(brs) == 0; return ;end | ||
function plot(brs::AbstractBranchResult...; | ||
branchlabel=["$i" for i = 1:length(brs)], | ||
kP...) | ||
if length(brs) == 0 | ||
return | ||
end | ||
fig = Figure() | ||
ax1 = fig[1, 1] = Axis(fig) | ||
|
||
for (id, contres) in pairs(brs) | ||
plot!(ax1, contres; branchlabel = branchlabel[id], kP...) | ||
plot!(ax1, contres; branchlabel=branchlabel[id], kP...) | ||
end | ||
GLMakie.axislegend(ax1, merge = true, unique = true) | ||
Makie.axislegend(ax1, merge=true, unique=true) | ||
display(fig) | ||
fig, ax1 | ||
end | ||
|
@@ -182,18 +209,18 @@ | |
# end | ||
# end | ||
|
||
function plot_periodic_potrap(outpof, n, M; ratio = 2) | ||
function plot_periodic_potrap(outpof, n, M; ratio=2) | ||
@assert ratio > 0 "You need at least one component" | ||
outpo = reshape(outpof[1:end-1], ratio * n, M) | ||
if ratio == 1 | ||
heatmap(outpo[1:n,:]', ylabel="Time", color=:viridis) | ||
heatmap(outpo[1:n, :]', ylabel="Time", color=:viridis) | ||
else | ||
fig = GLMakie.Figure() | ||
ax1 = Axis(fig[1,1], ylabel="Time") | ||
ax2 = Axis(fig[1,2], ylabel="Time") | ||
# GLMakie.heatmap!(ax1, rand(2,2)) | ||
GLMakie.heatmap!(ax1, outpo[1:n,:]') | ||
GLMakie.heatmap!(ax2, outpo[n+2:end,:]') | ||
fig = Makie.Figure() | ||
ax1 = Axis(fig[1, 1], ylabel="Time") | ||
ax2 = Axis(fig[1, 2], ylabel="Time") | ||
# Makie.heatmap!(ax1, rand(2,2)) | ||
Makie.heatmap!(ax1, outpo[1:n, :]') | ||
Makie.heatmap!(ax2, outpo[n+2:end, :]') | ||
fig | ||
end | ||
end | ||
|
@@ -210,8 +237,10 @@ | |
# end | ||
#################################################################################################### | ||
# plot recipes for the bifurcation diagram | ||
function plot(bd::BifDiagNode; code = (), level = (-Inf, Inf), k...) | ||
if ~hasbranch(bd); return; end | ||
function plot(bd::BifDiagNode; code=(), level=(-Inf, Inf), k...) | ||
if ~hasbranch(bd) | ||
return | ||
end | ||
|
||
fig = Figure() | ||
ax = fig[1, 1] = Axis(fig) | ||
|
@@ -222,30 +251,32 @@ | |
fig, ax | ||
end | ||
|
||
function _plot_bifdiag_makie!(ax, bd::BifDiagNode; code = (), level = (-Inf, Inf), k...) | ||
if ~hasbranch(bd); return; end | ||
function _plot_bifdiag_makie!(ax, bd::BifDiagNode; code=(), level=(-Inf, Inf), k...) | ||
if ~hasbranch(bd) | ||
return | ||
end | ||
|
||
_bd = get_branch(bd, code) | ||
_plot_bifdiag_makie!(ax, _bd.child; code = (), level = level, k...) | ||
_plot_bifdiag_makie!(ax, _bd.child; code=(), level=level, k...) | ||
|
||
# !! plot root branch in last so the bifurcation points do not alias, for example a 2d BP would be plot as a 1d BP if the order were reversed | ||
if level[1] <= _bd.level <= level[2] | ||
plot!(ax, _bd.γ; k...) | ||
end | ||
end | ||
|
||
function _plot_bifdiag_makie!(ax, bd::Vector{BifDiagNode}; code = (), level = (-Inf, Inf), k...) | ||
function _plot_bifdiag_makie!(ax, bd::Vector{BifDiagNode}; code=(), level=(-Inf, Inf), k...) | ||
for b in bd | ||
_plot_bifdiag_makie!(ax, b; code, level, k... ) | ||
_plot_bifdiag_makie!(ax, b; code, level, k...) | ||
end | ||
end | ||
#################################################################################################### | ||
plotAllDCBranch(branches) = plot(branches...) | ||
|
||
function plot_DCont_branch(::BK_Makie, | ||
branches, | ||
nbrs::Int, | ||
nactive::Int, | ||
nstep::Int) | ||
branches, | ||
nbrs::Int, | ||
nactive::Int, | ||
nstep::Int) | ||
plot(branches...) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My coding style is to have spaces around =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, sorry about that, I hit auto indent and forgot to check it. I just don't know yet in vscode how to change the formatting settings. Would you know ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really