Interact + PyPlot: only update when releasing slider

342 views
Skip to first unread message

Andrei Berceanu

unread,
Sep 8, 2014, 7:16:19 AM9/8/14
to julia...@googlegroups.com
I have some code along the lines of

f = figure()
@manipulate for α=1:0.1:3, β=1:0.1:3, γ=1:0.1:3; withfig(f) do
        y = fun(α,β,γ)
        PyPlot.plot(x, y)
    end
end

where fun is a *very slow* function to evaluate. Is there any way to tell @manipulate to update the resulting plot only after I release the sliders? Otherwise what I get is, I release them to the desised values and then have to wait ages for all the intermediate plots to be drawn.

Tnx!

John Myles White

unread,
Sep 8, 2014, 10:10:05 AM9/8/14
to julia...@googlegroups.com
I suspect the only way to do this is to change Interact so that it exposes a minimum time threshold before it registers a state change.

— John

Andrei Berceanu

unread,
Sep 8, 2014, 11:15:05 AM9/8/14
to julia...@googlegroups.com
Another option would be to use drop-down boxes with selectable values or custom text boxes instead of sliders, at least as a temporary fix. Anyone knows how I can do that?
By the way, iirc, IPython does have the update-on-release mechanism implemented in their interactive widget functionality.

Shashi Gowda

unread,
Sep 8, 2014, 12:06:55 PM9/8/14
to julia...@googlegroups.com
Unfortunately, the @manipulate macro can only rerun the expression at every update of any of its input.

What you need here is Reactive's `sampleon` function:

using Reactive, Interact
f = figure();

α=slider(1:0.1:3)
β=slider(1:0.1:3)
γ=slider(1:0.1:3)
replot = button("Replot") # Commit your changes
map(display, α, β, γ, replot) # optional

sampled_coeffs = sampleon(redo, lift(tuple, α, β, γ))

withfig(f)

@lift plot(apply(fun, sampled_coeffs))

IPython doesn't do update on release, Interact, in fact, uses the same widgets. What it does do is have at most 4 updates at any given time in the processing pipeline (any more updates replace the last update in the queue).

Shashi Gowda

unread,
Sep 8, 2014, 12:10:59 PM9/8/14
to julia...@googlegroups.com
John's suggestion is also a good way to do this. You can sample the signals at a specific interval instead of on button clicks:

# At 2 fps, with repeats dropped.
sampled_coeffs = droprepeats(sampleon(fps(2), lift(tuple, α, β, γ)))

Andrei Berceanu

unread,
Sep 9, 2014, 4:58:37 AM9/9/14
to julia...@googlegroups.com
Thank you both for the suggestions! I am currently trying the "Replot" button approach, but ran into some errors, as follows:

using Reactive, Interact
using PyPlot

fun(α, β, γ) = cos(α + sin(β+γ)) #example function


f = figure();

α=slider(1:0.1:3)
β=slider(1:0.1:3)
γ=slider(1:0.1:3)
replot = button("Replot") # Commit your changes
map(display, α, β, γ, replot) # optional
-->  `start` has no method matching start(::Slider{Float64})


sampled_coeffs = sampleon(redo, lift(tuple, α, β, γ))
-->  redo not defined

withfig(f)
@lift plot(apply(fun, sampled_coeffs))
--> `withfig` has no method matching withfig(::Figure)

Shashi Gowda

unread,
Sep 9, 2014, 6:13:40 AM9/9/14
to julia...@googlegroups.com
using Reactive, Interact
using PyPlot

f = figure();

α=slider(1:0.1:3)
β=slider(1:0.1:3)
γ=slider(1:0.1:3)
replot = button("Replot") # Commit your changes
map(display, [α, β, γ, replot]) # optional

coeffs = sampleon(replot, lift(tuple, α, β, γ))

@manipulate for x = coeffs; withfig(f) do
        plot(fun(x...))
    end
end

Sorry, in my rush to reply, I left behind some typos and bad thinking. Tried this, it works.

Steven G. Johnson

unread,
Sep 9, 2014, 10:51:36 AM9/9/14
to julia...@googlegroups.com


On Monday, September 8, 2014 11:15:05 AM UTC-4, Andrei Berceanu wrote:
Another option would be to use drop-down boxes with selectable values or custom text boxes instead of sliders, at least as a temporary fix. Anyone knows how I can do that?

Just wrap dropdown(...) around the range to get a dropdown menu instead of a slider:

@manipulate for n in dropdown(1:5)
    ...
end

By the way, iirc, IPython does have the update-on-release mechanism implemented in their interactive widget functionality.

We are using the same JavaScript widgets as IPython.  Maybe you just need to set some option when the widget is created?
 
Reply all
Reply to author
Forward
0 new messages