Skip to content

Instantly share code, notes, and snippets.

@wilsongoode
Created July 19, 2022 14:47
Show Gist options
  • Save wilsongoode/3fd25d5f3a731ecb1037bd4098eb1645 to your computer and use it in GitHub Desktop.
Save wilsongoode/3fd25d5f3a731ecb1037bd4098eb1645 to your computer and use it in GitHub Desktop.
### A Pluto.jl notebook ###
# v0.19.9
using Markdown
using InteractiveUtils
# ╔═╡ 21a41593-edb7-4f1f-9a9d-1fff791d6f3e
using Test
# ╔═╡ 0800deb4-8f1f-4f4f-9462-38250f947da2
using Markdown
# ╔═╡ c12277cc-93a2-45e3-b692-8007201c723c
md"""
[*fonsp commented on Jun 14:*](https://github.com/JuliaPluto/PlutoUI.jl/issues/212#issuecomment-1155809643)
It would be helpful if someone wrote a function that does the following 💕
```julia
function downsample(x::AbstractVector, max_size::Integer)
return something...
end
x1 = [1,2,3]
x2 = rand(500)
@test downsample(x1, 3) == [1,2,3]
@test downsample(x1, 30) == [1,2,3]
@test downsample(x1, 2) == [1,3]
@test downsample(x2, 500) == x2
y2 = downsample(x2, 400)
@test 250 <= length(y2) <= 400
@test y2[begin] == x2[begin]
@test y2[end] == x2[end]
x3 = rand(50_000_000)
# this should take less than 0.1ms
downsample(x3, 100)
```
"""
# ╔═╡ 2cb9efdd-e4ae-4a9d-acf9-282a5f5bcf39
function downsample(x::AbstractVector, max_size::Integer)
if max_size >= length(x)
return x
else
return x[ round.(Int, range(1, stop=lastindex(x), length=max_size)) ]
end
end
# ╔═╡ 3fb30a63-13b9-42f3-b8ef-d1258c557172
md"# Tests"
# ╔═╡ d58a8253-cb3b-4457-9d23-728bf14e6b4a
x1 = [1,2,3]
# ╔═╡ 66aa509a-f18c-45fb-8000-ee42cc46939b
x2 = rand(500)
# ╔═╡ 1f90f2d5-726c-48c1-a293-30b84b5f4e39
@test downsample(x1, 3) == [1,2,3]
# ╔═╡ b8033c01-e587-4b64-ac04-191b35012518
@test downsample(x1, 30) == [1,2,3]
# ╔═╡ 647dc60a-6d60-4e16-8ae4-b810fdee9a41
@test downsample(x1, 2) == [1,3]
# ╔═╡ 7935b4f8-cf9c-4d15-beb1-5cd0ee2f5ca0
@test downsample(x2, 500) == x2
# ╔═╡ 5d848b51-0064-4dcd-a00c-392552d967b4
y2 = downsample(x2, 400)
# ╔═╡ 3c470e03-0f13-4ab7-9acc-5d7e04dcd3b9
@test 250 <= length(y2) <= 400
# ╔═╡ bd167ac0-9327-48a2-bdeb-1a019c8f00f0
@test y2[begin] == x2[begin]
# ╔═╡ 77d371f9-0d27-411e-b279-19ab10056e32
@test y2[end] == x2[end]
# ╔═╡ 89380506-8ca6-4023-b687-bac359f9f901
x3 = rand(50_000_000)
# ╔═╡ 8fe41db6-609d-4f2b-901d-881cbf1e030a
# this should take less than 0.1ms
@time downsample(x3, 100)
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.8.0-beta1"
manifest_format = "2.0"
project_hash = "98fdcb07f3c3ac58a3cb1cb4319c42567d4a67bf"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
[[deps.Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[deps.Random]]
deps = ["SHA", "Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
[[deps.SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
"""
# ╔═╡ Cell order:
# ╠═21a41593-edb7-4f1f-9a9d-1fff791d6f3e
# ╠═0800deb4-8f1f-4f4f-9462-38250f947da2
# ╟─c12277cc-93a2-45e3-b692-8007201c723c
# ╠═2cb9efdd-e4ae-4a9d-acf9-282a5f5bcf39
# ╟─3fb30a63-13b9-42f3-b8ef-d1258c557172
# ╠═d58a8253-cb3b-4457-9d23-728bf14e6b4a
# ╠═66aa509a-f18c-45fb-8000-ee42cc46939b
# ╠═1f90f2d5-726c-48c1-a293-30b84b5f4e39
# ╠═b8033c01-e587-4b64-ac04-191b35012518
# ╠═647dc60a-6d60-4e16-8ae4-b810fdee9a41
# ╠═7935b4f8-cf9c-4d15-beb1-5cd0ee2f5ca0
# ╠═5d848b51-0064-4dcd-a00c-392552d967b4
# ╠═3c470e03-0f13-4ab7-9acc-5d7e04dcd3b9
# ╠═bd167ac0-9327-48a2-bdeb-1a019c8f00f0
# ╠═77d371f9-0d27-411e-b279-19ab10056e32
# ╠═89380506-8ca6-4023-b687-bac359f9f901
# ╠═8fe41db6-609d-4f2b-901d-881cbf1e030a
# ╟─00000000-0000-0000-0000-000000000001
# ╟─00000000-0000-0000-0000-000000000002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment