-
Notifications
You must be signed in to change notification settings - Fork 97
Implement interactive 2D dynamical system clicker #252
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
4c0764b
143eaae
68c0988
61e3f77
02b413f
676e70a
eda2ee3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
function DynamicalSystems.interactive_clicker(dds; | ||
GabrielMajeri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# DynamicalSystems kwargs: | ||
tfinal = (1000.0, 10.0^4), | ||
complete = (x, y) -> [x, y], | ||
project = identity, | ||
Comment on lines
+4
to
+5
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. Why are these keywords provided here? Shouldn't we expect a 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. Hmm, but then I don't think we could use it directly with |
||
# Makie kwargs: | ||
color = randomcolor, | ||
scatterkwargs = (), | ||
labels = ("x", "y") | ||
) | ||
|
||
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. Shouldn't you check here that 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. No, because this function can be used with higher-dimensional dynamical systems, as long as the |
||
u0 = DynamicalSystems.get_state(dds) | ||
|
||
figure = Figure(size = (1000, 800), backgroundcolor = :white) | ||
|
||
T_slider, m_slider = _add_clicker_controls!(figure, tfinal) | ||
ax = figure[0, :] = Axis(figure) | ||
|
||
# Compute the initial section | ||
tr, = trajectory(dds, T_slider[]; t0 = 0) | ||
length(tr) == 0 && error("Initial computed trajectory is empty!") | ||
|
||
data = project(tr) | ||
length(data[1]) != 2 && error("(Projected) trajectory is not 2D") | ||
|
||
positions_node = Observable(data) | ||
colors = (c = color(u0); [c for _ in 1:length(data)]) | ||
colors_node = Observable(colors) | ||
scatter!( | ||
ax, positions_node, color = colors_node, | ||
markersize = lift(o -> o*px, m_slider), marker = :circle, scatterkwargs... | ||
) | ||
|
||
ax.xlabel, ax.ylabel = labels | ||
laststate = Observable(u0) | ||
|
||
# Interactive clicking on the phase space: | ||
Makie.deactivate_interaction!(ax, :rectanglezoom) | ||
spoint = select_point(ax.scene) | ||
on(spoint) do pos | ||
x, y = pos; | ||
newstate = try | ||
complete(x, y) | ||
catch err | ||
@error "Could not complete state, got error: " exception=err | ||
return | ||
end | ||
|
||
tr, = trajectory(dds, T_slider[], newstate; t0 = 0) | ||
data = project(tr) | ||
|
||
positions = positions_node[]; colors = colors_node[] | ||
append!(positions, data) | ||
c = color(newstate) | ||
append!(colors, fill(c, length(data))) | ||
# Update all the observables with Array as value: | ||
positions_node[], colors_node[], laststate[] = positions, colors, newstate | ||
end | ||
|
||
display(figure) | ||
|
||
return figure, laststate | ||
end | ||
|
||
function _add_clicker_controls!(figure, tfinal) | ||
sg1 = SliderGrid(figure[1, :][1, 1], | ||
(label = "T", range = range(tfinal[1], tfinal[2], length = 1000), | ||
format = x -> string(round(x)), ) | ||
) | ||
sg2 = SliderGrid(figure[1, :][1, 2], | ||
(label = "ms", range = 10.0 .^ range(0, 2, length = 100), | ||
format = x -> string(round(x)), startvalue = 10) | ||
) | ||
return sg1.sliders[1].value, sg2.sliders[1].value | ||
end |
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.
Sorry, to be clear: this works for any 2D system, right? This should be 2D maps, projected 2D systems, poincare maps, whatever. Right? If so, do we even need the interactive poincare map function anymore?
Additionally please rename this section to reflect the generality of the function.
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.
I've updated the docs.
I don't know if we need it, but I think it's pretty useful (and one of the most common use cases, I believe). It would help from a backwards compatibility perspective to preserve the existing
interactive_poincaresos
API (even though now that function is now a wrapper for this one).