From 015c2afa0b38385e7d43427b9009ae0a6e51113e Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 5 Mar 2024 10:32:22 +0100 Subject: [PATCH] update --- examples/earth-sphere.yaml | 17 +++++++++++ examples/generic.jl | 46 ++++++------------------------ examples/speedy-volume.yaml | 15 ++++++++++ examples/speedyweather-tyler.yaml | 2 +- examples/speedyweather.yaml | 2 +- examples/using Bonito.jl | 21 ++++++++++++++ src/NDViewer.jl | 4 +-- src/layers.jl | 26 +++++++++-------- src/makie-converts.jl | 12 ++++++++ src/{loading.jl => yaml-viewer.jl} | 31 ++++++++++++++++++++ 10 files changed, 123 insertions(+), 53 deletions(-) create mode 100644 examples/earth-sphere.yaml create mode 100644 examples/speedy-volume.yaml create mode 100644 examples/using Bonito.jl rename src/{loading.jl => yaml-viewer.jl} (57%) diff --git a/examples/earth-sphere.yaml b/examples/earth-sphere.yaml new file mode 100644 index 0000000..4f31851 --- /dev/null +++ b/examples/earth-sphere.yaml @@ -0,0 +1,17 @@ +data: + path: "./dev/NDViewer/examples/speedyweather.nc" +layers: + - figure: + size: [1000, 800] + - type: LScene + position: [1, 1] + attributes: + show_axis: false + plots: + - type: sphereplot + args: [[1, 2]] + - type: Axis + position: [1, 2] + plots: + - type: image + args: [[1, 2]] diff --git a/examples/generic.jl b/examples/generic.jl index 1ff585a..7287249 100644 --- a/examples/generic.jl +++ b/examples/generic.jl @@ -1,37 +1,9 @@ -using Bonito, WGLMakie, NDViewer - - -function create_app_from_yaml(file) - yaml_str = read(file, String) - viewer = NDViewer.load_from_yaml(yaml_str) - app = App() do - editor = CodeEditor("yaml"; initial_source=yaml_str, width=300, height=600, foldStyle="manual") - css = DOM.style(""" - .ace_scrollbar-v, - .ace_scrollbar-h { - display: none !important; - } - """) - set_editor = js""" - const editor = ace.edit($(editor.element)) - editor.setReadOnly(true); - """ - yaml_display = DOM.div(css, Card(editor; width="fit-content"), set_editor) - style = Styles("word-wrap" => "break-word") - app_dom = Grid( - yaml_display, viewer; - justify_content="center", - # align_items="center", - style=Styles("grid-auto-flow" => "column") - ) - return Centered(app_dom; style=Styles("width" => "100%")) - end - return app, viewer -end - -app1, viewer = create_app_from_yaml(joinpath(@__DIR__, "speedyweather.yaml")); app1 -NDViewer.add_slice_view(viewer, 1, 1, 1, :black) -NDViewer.add_slice_view(viewer, 1, 1, 2, :blue) - -app2, viewer = create_app_from_yaml(joinpath(@__DIR__, "speedyweather-tyler.yaml")); app2 -app3, viewer = create_app_from_yaml(joinpath(@__DIR__, "tas-gn-64gb.yaml")); app3 +using Bonito, WGLMakie, NDViewer, GeometryBasics +using NDViewer: yaml_viewer +yaml_path(name) = joinpath(@__DIR__, name) +WGLMakie.activate!() +app1 = yaml_viewer(yaml_path("speedyweather.yaml")) +app2 = yaml_viewer(yaml_path("speedy-volume.yaml")) +app3 = yaml_viewer(yaml_path("speedyweather-tyler.yaml")) +app4 = yaml_viewer(yaml_path("tas-gn-64gb.yaml")) +app5 = yaml_viewer(yaml_path("earth-sphere.yaml")) diff --git a/examples/speedy-volume.yaml b/examples/speedy-volume.yaml new file mode 100644 index 0000000..1cb25ce --- /dev/null +++ b/examples/speedy-volume.yaml @@ -0,0 +1,15 @@ +data: + path: "./dev/NDViewer/examples/speedyweather.nc" +layers: + - figure: + size: [1000, 800] + - type: Axis3 + position: [1, 1] + plots: + - type: volume + args: [[1, 2, 3]] + - type: Axis + position: [1, 2] + plots: + - type: image + args: [[1, 2]] diff --git a/examples/speedyweather-tyler.yaml b/examples/speedyweather-tyler.yaml index 3b445ba..d888d88 100644 --- a/examples/speedyweather-tyler.yaml +++ b/examples/speedyweather-tyler.yaml @@ -1,5 +1,5 @@ data: - path: "c:\\Users\\sdani\\Programmieren\\MakieDev\\dev\\NDViewer\\examples\\speedyweather.nc" + path: ".\\dev\\NDViewer\\examples\\speedyweather.nc" layers: - figure: size: [1000, 1000] diff --git a/examples/speedyweather.yaml b/examples/speedyweather.yaml index e4a41e6..42618a8 100644 --- a/examples/speedyweather.yaml +++ b/examples/speedyweather.yaml @@ -1,5 +1,5 @@ data: - path: "c:\\Users\\sdani\\Programmieren\\MakieDev\\dev\\NDViewer\\examples\\speedyweather.nc" + path: ".\\dev\\NDViewer\\examples\\speedyweather.nc" layers: - layout: rowsize: [0.8, 0.2] diff --git a/examples/using Bonito.jl b/examples/using Bonito.jl new file mode 100644 index 0000000..8a1667d --- /dev/null +++ b/examples/using Bonito.jl @@ -0,0 +1,21 @@ +using Bonito +using DelimitedFiles +volcano = readdlm(Makie.assetpath("volcano.csv"), ',', Float64) +App() do + f = contourf(volcano, levels=10; axis=(; title="normal")) + f2 = contourf(volcano, levels=10; axis=(; title="resize_to=:parent")) + r = WGLMakie.WithConfig(f2; resize_to=:parent) + DOM.div(Grid(f, r; columns="50% 50%"); style=Styles("height" => "700px")) + +end + +using GeoMakie, GLMakie +fig = Figure() +ga = GeoAxis( + fig[1, 1]; # any cell of the figure's layout + dest="+proj=wintri", # the CRS in which you want to plot +) +lines!(ga, GeoMakie.coastlines()) # plot coastlines from Natural Earth as a reference +# You can plot your data the same way you would in Makie +scatter!(ga, -120:15:120, -60:7.5:60; color=-60:7.5:60, strokecolor=(:black, 0.2)) +fig diff --git a/src/NDViewer.jl b/src/NDViewer.jl index 568ab25..5eb05b1 100644 --- a/src/NDViewer.jl +++ b/src/NDViewer.jl @@ -1,10 +1,10 @@ module NDViewer -using Makie, DimensionalData, LinearAlgebra +using Makie, DimensionalData, LinearAlgebra, Bonito include("array-interface.jl") include("makie-converts.jl") -include("loading.jl") +include("yaml-viewer.jl") include("widgets.jl") include("bonito-widgets.jl") include("makie-widgets.jl") diff --git a/src/layers.jl b/src/layers.jl index ee19f63..11eed2a 100644 --- a/src/layers.jl +++ b/src/layers.jl @@ -154,13 +154,15 @@ function layer_to_plot!(ax::Makie.AbstractAxis, sliced_arrays, dict, fcolor, cma args = replace_slices(sliced_arrays, dict["args"]) attr = get(dict, "attributes", Dict()) attributes = replace_slices(sliced_arrays, attr) - if plotfunc in (heatmap, image, surface) + if (plotfunc in (heatmap, image, surface, volume)) || dict["type"] == "sphereplot" crange = get(attributes, :colorrange, nothing) if crange === nothing - crange = map(Makie.extrema_nan, last(args)) - idx = length(cmaps) - colormaps = colormap_widget(fcolor[idx+1, 1], crange) - push!(cmaps, colormaps) + cmap_slice = last(dict["args"]) + colormaps = get!(cmaps, cmap_slice) do + idx = length(cmaps) + crange = map(Makie.extrema_nan, last(args)) + colormap_widget(fcolor[idx+1, 1], crange) + end for (k, v) in pairs(colormaps) attributes[k] = v end @@ -183,6 +185,8 @@ function resolve_symbol(s::String) end if hasproperty(Makie, name) return getfield(Makie, name) + elseif hasproperty(NDViewer, name) + return getfield(NDViewer, name) else return s end @@ -251,7 +255,7 @@ function create_plot(data, layers;) fcolor = f[3, 1] layouts = remove_dicts!(x-> haskey(x, "layout"), layers) sliced_arrays, widgets = create_slices(layers, data) - cmaps = [] + cmaps = Dict() axes = map(layers) do axlayer layer_to_axis!(fplots, sliced_arrays, axlayer, fcolor, cmaps) end @@ -268,13 +272,14 @@ function create_plot(data, layers;) end end end - for (i, colormaps) in enumerate(cmaps) - cmaps = Base.structdiff(colormaps, (; nan_color=0, alpha=0)) - Colorbar(fcbar[i, 1]; vertical=false, tellheight=true, tellwidth=true, cmaps...) + for (i, colormaps) in enumerate(values(cmaps)) + cmap_attr = Base.structdiff(colormaps, (; nan_color=0, alpha=0)) + Colorbar(fcbar[i, 1]; vertical=false, tellheight=true, tellwidth=true, cmap_attr...) end return f, sliced_arrays, widgets, axes end + struct DataViewerApp layers data @@ -298,14 +303,11 @@ function wgl_create_plot(data, layers) return DataViewerApp(layers, data, f, slices, widgets, axes) end - function Base.display(viewer::DataViewerApp) app = App(viewer; title="DataViewer") display(app) end - - function add_index_slice(axis::Makie.AbstractAxis, plot, index_obs, dim, color) dim_data = dim == 2 ? plot.y : plot.x dim_slice = map(getindex, dim_data, index_obs) diff --git a/src/makie-converts.jl b/src/makie-converts.jl index c617c21..5d4574d 100644 --- a/src/makie-converts.jl +++ b/src/makie-converts.jl @@ -50,3 +50,15 @@ function Makie.convert_arguments(::Type{<:LineSegments}, u_matrix::AbstractDimAr end return PlotSpec(:LineSegments, convert_arguments(LineSegments, vec(points))..., cycle=[], color=radiance) end + +@recipe(SpherePlot, image) do scene + attr = Attributes() + Makie.MakieCore.colormap_attributes!(attr, :viridis) + attr +end + +function Makie.plot!(p::SpherePlot) + GB = Makie.GeometryBasics + sm = GB.uv_normal_mesh(GB.Tesselation(Sphere(Point3f(0), 1.0f0), 100)) + mesh!(p, sm, color=map(x -> x', p[3]); Makie.MakieCore.colormap_attributes(p)...) +end diff --git a/src/loading.jl b/src/yaml-viewer.jl similarity index 57% rename from src/loading.jl rename to src/yaml-viewer.jl index 57558cb..27f7ef9 100644 --- a/src/loading.jl +++ b/src/yaml-viewer.jl @@ -38,3 +38,34 @@ function load_from_yaml(yaml_str) layers = data["layers"] wgl_create_plot(data_cube, layers) end + + +function yaml_viewer(file) + yaml_str = read(file, String) + create_app_from_yaml(yaml_str) +end + +function create_app_from_yaml(yaml_str) + return App() do + viewer = NDViewer.load_from_yaml(yaml_str) + editor = CodeEditor("yaml"; initial_source=yaml_str, width=300, height=600, foldStyle="manual") + css = DOM.style(""" + .ace_scrollbar-v, + .ace_scrollbar-h { + display: none !important; + } + """) + set_editor = js""" + const editor = ace.edit($(editor.element)) + editor.setReadOnly(true); + """ + yaml_display = DOM.div(css, Card(editor; width="fit-content"), set_editor) + app_dom = Grid( + yaml_display, viewer; + justify_content="center", + # align_items="center", + style=Styles("grid-auto-flow" => "column") + ) + return Centered(app_dom; style=Styles("width" => "100%")) + end +end