Skip to content

Commit

Permalink
Add downsampling function to Slider (#219)
Browse files Browse the repository at this point in the history
Co-authored-by: Fons van der Plas <[email protected]>
  • Loading branch information
wilsongoode and fonsp authored Sep 22, 2022
1 parent d989d63 commit 7890b37
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Builtins.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### A Pluto.jl notebook ###
# v0.18.4
# v0.19.9

using Markdown
using InteractiveUtils
Expand Down Expand Up @@ -156,11 +156,22 @@ begin
end
end

function downsample(x::AbstractVector{T}, max_steps::Integer) where T
if max_steps >= length(x)
x
else
T[
x[round(Int, i)]
for i in range(firstindex(x), stop=lastindex(x), length=max_steps)
]
end
end

function Slider(values::AbstractVector{T}; default=missing, show_value=false) where T
Slider(values, (default === missing) ? first(values) : let
function Slider(values::AbstractVector{T}; default=missing, show_value=false, max_steps=1_000) where T
new_values = downsample(values, max_steps)
Slider(values, (default === missing) ? first(new_values) : let
d = default
d values ? convert(T, d) : closest(values, d)
d new_values ? convert(T, d) : closest(new_values, d)
end, show_value)
end

Expand Down Expand Up @@ -1307,6 +1318,9 @@ bs
# ╔═╡ 7c5765ae-c10a-4677-97a3-848a423cb8b9
s1, s2, s3

# ╔═╡ f70c1f7b-f3c5-4aff-b39c-add64afbd635
@bind s4_downsampled Slider(1:10_000, show_value=true, max_steps=100)

# ╔═╡ ec870eea-36a4-48b6-95d7-f7c083e29856
bos = @bind os1 OldSlider(1:10)

Expand Down Expand Up @@ -1593,15 +1607,15 @@ export Slider, NumberField, Button, LabelButton, CounterButton, CheckBox, TextFi
# ╟─e8c5ba24-10e9-49e8-8c11-0add092637f8
# ╟─e1bbe1d7-68ef-4ee1-8174-d1ae1f822acb
# ╟─d738b448-387b-4942-af82-cc93042705a4
# ╟─81adbd39-5780-4cc6-a53f-a4472bacf1c0
# ╠═81adbd39-5780-4cc6-a53f-a4472bacf1c0
# ╠═d8f907cd-2f89-4d54-a311-998dc8ee148e
# ╠═a0fb4f28-bfe4-4877-bf07-31acb9a56d2c
# ╠═ac542b84-dbc8-47e2-8835-9e43582b6ad7
# ╠═6da84fb9-a629-4e4c-819e-dd87a3e267ce
# ╠═dc3b6628-f453-46d9-b6a1-957608a20764
# ╠═a203d9d4-cd7b-4368-9f6d-e040a5757565
# ╠═98d251ff-67e7-4b16-b2e0-3e2102918ca2
# ╟─0baae341-aa0d-42fd-9f21-d40dd5a03af9
# ╠═0baae341-aa0d-42fd-9f21-d40dd5a03af9
# ╠═c2b473f4-b56b-4a91-8377-6c86da895cbe
# ╠═5caa34e8-e501-4248-be65-ef9c6303d025
# ╠═46a90b45-8fef-493e-9bd1-a71d1f9c53f6
Expand All @@ -1610,6 +1624,7 @@ export Slider, NumberField, Button, LabelButton, CounterButton, CheckBox, TextFi
# ╠═75b008b2-afc0-4bd5-9183-e0e0d392a4c5
# ╠═9df251eb-b4f5-46cc-a4fe-ff2fa670b773
# ╠═7c5765ae-c10a-4677-97a3-848a423cb8b9
# ╠═f70c1f7b-f3c5-4aff-b39c-add64afbd635
# ╟─d088bcdb-d851-4ad7-b5a0-751c1f348995
# ╠═ec870eea-36a4-48b6-95d7-f7c083e29856
# ╠═b44f1128-32a5-4d1d-a00b-446143074056
Expand All @@ -1621,7 +1636,7 @@ export Slider, NumberField, Button, LabelButton, CounterButton, CheckBox, TextFi
# ╟─97fc914b-005f-4b4d-80cb-23016d589609
# ╟─db3aefaa-9539-4c46-ad9b-83763f9ef624
# ╟─0373d633-18bd-4936-a0ae-7a4f6f05372a
# ╟─f59eef32-4732-46db-87b0-3564433ce43e
# ╠═f59eef32-4732-46db-87b0-3564433ce43e
# ╠═f7870d7f-992d-4d64-85aa-7621ab16244f
# ╠═893e22e1-a1e1-43cb-84fe-4931f3ba35c1
# ╠═7089edb6-720d-4df5-b3ca-da17d48b107e
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,31 @@ transform(el, x) = AbstractPlutoDingetjes.Bonds.transform_value(el, x)
@test default(el) == tan



# Downsampling Slider ranges
x1 = [1,2,3]
x2 = rand(500)

@test PlutoUI.BuiltinsNotebook.downsample(x1, 3) == x1
@test PlutoUI.BuiltinsNotebook.downsample(x1, 3) === x1
@test PlutoUI.BuiltinsNotebook.downsample(x1, 30) === x1
@test PlutoUI.BuiltinsNotebook.downsample(x1, 2) == [1,3]

@test PlutoUI.BuiltinsNotebook.downsample(x2, 500) == x2
@test PlutoUI.BuiltinsNotebook.downsample(x2, 500) === x2
y2 = PlutoUI.BuiltinsNotebook.downsample(x2, 400)
@test 250 <= length(y2) <= 400
@test y2[begin] == x2[begin]
@test y2[end] == x2[end]

x3 = rand(50_000_000)
max_downsample_time = 0.001 # seconds
# this should take less than 0.1ms
@test max_downsample_time >= @elapsed PlutoUI.BuiltinsNotebook.downsample(x3, 100)




el = Scrubbable(60)
@test default(el) === 60
el = Scrubbable(60.0)
Expand Down

0 comments on commit 7890b37

Please sign in to comment.