Skip to content

Commit

Permalink
fix hdf5 example
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDanisch committed Nov 6, 2024
1 parent 69d5ef4 commit 89b9f91
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 68 deletions.
85 changes: 34 additions & 51 deletions examples/hdf5.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HDF5, GLMakie
using HDF5, GLMakie, NDViewer
using LinearAlgebra


Expand All @@ -15,8 +15,21 @@ arr[1]
with_time = cat(arr...; dims=4)
x = with_time[1]
filtered = map(y -> x y ? NaN : y, with_time)
filtered0 = map(y -> x y ? 0f0 : y, tmp1)
create_plot(filtered)

layout = [
Dict(
"type" => "Axis3",
"position" => [1, 1],
"plots" => [
Dict(
"type" => "volume",
"args" => [[1, 2, 3]]
)
]
)
]

f = NDViewer.plot_data(filtered, layout)

grid = hydrodynamics["Grid"]
bathymetry = grid["Bathymetry"]
Expand All @@ -30,8 +43,6 @@ velocity_v_1 = velocity_v["velocity V_00001"]
velocity_w_1 = velocity_w["velocity W_00001"]




u, v = velocity_u_1[][:, :, end], velocity_v_1[][:, :, end]

arrows(0..10, 0..10, velocity_u_1[][:, :, end], velocity_v_1[][:, :, end], arrowsize=0)
Expand All @@ -40,50 +51,22 @@ data = Vec3f.(velocity_u_1[], velocity_v_1[], velocity_w_1[])[1:5:end, 1:5:end,
points = Point3f.(Tuple.(CartesianIndices(data)))
arrows(vec(points), vec(data), arrowsize=0.5, color=norm.(vec(data)))

vec(points)
vec(points)[1:5:end]

f = Figure()
s = Slider(f[1, 1], range=range(mini, maxi, length=100))
volume(f[2, 1], data, algorithm=:iso, isovalue=s.value)

img = map(x-> RGBf.(x...), data)
volume(img, colormap=nothing, color=nothing)
heatmap(norm.(data[:, :, 5]))
begin
f = Figure()
uv = Vec2f.(velocity_u_1[], velocity_v_1[])
s = Slider(f[2, 1], range=1:size(uv, 3))
swidth = Slider(f[3, 1], range=1:10)
uv_mat = map(s.value, swidth.value) do idx, w
vec(uv[1:w:end, 1:w:end, idx])
end
uv_vec = map(vec, uv_mat)
color = map(x-> norm.(x), uv_vec)
points = map(uv_mat) do uv
x = LinRange(1, 100, size(uv, 1)); y = LinRange(1, 100, size(uv, 2))
vec(Point2f.(x, y'))
end
arrows(f[1, 1], points, uv_vec, color=color)
f
end

begin
f = Figure()
uv = Vec3f.(velocity_u_1[], velocity_v_1[], velocity_w_1[])
s = Slider(f[2, 1], range=1:size(uv, 3))
swidth = Slider(f[3, 1], range=1:10)
uv_mat = map(s.value, swidth.value) do idx, w
vec(uv[1:w:end, 1:w:end, idx])
end
uv_vec = map(vec, uv_mat)
color = map(x -> norm.(x), uv_vec)
points = map(uv_mat) do uv
x = LinRange(1, 100, size(uv, 1))
y = LinRange(1, 100, size(uv, 2))
z = LinRange(1, 100, size(uv, 3))
vec(uv)
end
arrows(f[1, 1], points, uv_vec, color=color)
f
end
layout = [
Dict(
"type" => "Axis",
"position" => [1, 1],
"plots" => [
Dict(
"type" => "linesegments",
"args" => [[1, 2, 4 => 1], [1, 2, 4 => 2]]
)
]
)
]

u = velocity_u_1[]
v = velocity_v_1[]
data = [arr[i, j, t] for i in 1:size(u, 1), j in 1:size(u, 2), t in 1:size(u, 3), arr in (u, v)]

f = NDViewer.plot_data(data, layout)
18 changes: 8 additions & 10 deletions src/layers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ function get_dims!(arrays::Dict, widgets, target_dims::Vector{Int}, names::Vecto
end

function plot_data(data, layers; figure=(;))
size = get(figure, :size, (1200, 800))
f = Figure(; figure..., size=size)
fcolor = f[1, 1]
fplots = f[2, 1]
slices, widgets, colorrange = create_slices(layers, data)
colormaps = colormap_widget(fcolor, colorrange)
slices, widgets = create_plot(fplots, slices, colormaps)
return
f, slices, widgets, axes = create_plot(data, layers; figure=figure)
sub = f[end + 1, :]
for (i, w) in enumerate(values(widgets))
widget(sub[i, :], w)
end
return f
end


Expand Down Expand Up @@ -237,7 +235,7 @@ function remove_dicts!(f, dicts)
return result
end

function create_plot(data, layers;)
function create_plot(data, layers; figure=(;))
layers = copy(layers)
figure_kw = remove_dicts!(x -> haskey(x, "figure"), layers)
if length(figure_kw) == 1
Expand All @@ -247,7 +245,7 @@ function create_plot(data, layers;)
return Symbol(k) => v
end
else
f_kw = Dict()
f_kw = figure
end
f = Figure(; f_kw...)
fplots = f[1, 1]
Expand Down
22 changes: 15 additions & 7 deletions src/makie-widgets.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

function play_slider(figure, label, range)
l = Label(figure[1, 1], label, halign=:left)
button = Button(figure[1, 2]; label=">")
slider = Slider(figure[1, 3]; range=range)
vl = Label(figure[1, 4], map(string, slider.value), halign=:right)
button = Makie.Button(figure[1, 2]; label=">")
slider = Makie.Slider(figure[1, 3]; range=range)
vl = Makie.Label(figure[1, 4], map(string, slider.value), halign=:right)
sgrid = figure[1, 5] = GridLayout()
speed_button = [
Button(sgrid[1, 1]; label="1") => 1,
Button(sgrid[1, 2]; label="10") => 10,
Button(sgrid[1, 3]; label="24") => 24,
Button(sgrid[1, 4]; label="60") => 60,
Makie.Button(sgrid[1, 1]; label="1") => 1,
Makie.Button(sgrid[1, 2]; label="10") => 10,
Makie.Button(sgrid[1, 3]; label="24") => 24,
Makie.Button(sgrid[1, 4]; label="60") => 60,
]
playing = Threads.Atomic{Bool}(false)
fps = Threads.Atomic{Int}(24)
Expand Down Expand Up @@ -89,3 +89,11 @@ function colormap_widget(f, limits, colormaps=COLORMAPS)
colormap_widget(f, limits[], kw.colorrange, kw.lowclip, kw.highclip, kw.nan_color, kw.alpha, kw.colormap, colormaps)
return kw
end


function widget(f, ps::PlaySlider)
obs = play_slider(f, ps.name, ps.range)
on(obs) do v
ps.value[] = v
end
end

0 comments on commit 89b9f91

Please sign in to comment.