Skip to content

Commit

Permalink
Some small tweaks to the callstack plots. (#1928)
Browse files Browse the repository at this point in the history
- Added opacity to arrows
- Removed the axes and borders
- Inverted the stroke/color of the labels
- Removed source path in legend

![Screenshot 2024-11-05 at 11 04
03](https://github.com/user-attachments/assets/bc582a98-0bee-4f70-8738-1b5049717b68)
  • Loading branch information
evetion authored Nov 6, 2024
1 parent 0f703ef commit 8cdea9f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
31 changes: 19 additions & 12 deletions docs/dev/callstacks.qmd
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
lightbox: auto
---

# Call stacks

```{julia}
Expand Down Expand Up @@ -37,14 +41,12 @@ close(db)
plot_graph(
graph;
size = (2000, 1200),
squash_methods = [
:n_neighbor_bounds_flow,
:n_neighbor_bounds_control,
:sort_by_function,
:neighbortypes
:neighbortypes,
],
xlims = (-0.4, 5.6)
)
```

Expand All @@ -59,7 +61,7 @@ model = Ribasim.Model(toml_path)
du = get_du(model.integrator)
(; u, p, t) = model.integrator
graph, verts = tracecall((Ribasim,), Ribasim.water_balance!, (du, u, p, t))
plot_graph(graph, size = (1700, 1000), xlims = (-0.4, 4.5))
plot_graph(graph; max_depth = 4)
```

## Allocation initialization
Expand All @@ -68,13 +70,16 @@ In this part of the code the data structures for allocation are set up. Most end

```{julia}
# | code-fold: true
toml_path = normpath(@__DIR__, "../../generated_testmodels/main_network_with_subnetworks/ribasim.toml")
config = Ribasim.Config(toml_path; allocation_use_allocation=false)
toml_path = normpath(
@__DIR__,
"../../generated_testmodels/main_network_with_subnetworks/ribasim.toml",
)
config = Ribasim.Config(toml_path; allocation_use_allocation = false)
db_path = Ribasim.database_path(config)
db = SQLite.DB(db_path)
p = Ribasim.Parameters(db, config)
graph, verts = tracecall((Ribasim,), Ribasim.initialize_allocation!, (p, config))
plot_graph(graph, size = (1800, 1000), xlims = (-0.5, 5.5))
plot_graph(graph)
```

## Allocation run
Expand All @@ -86,7 +91,7 @@ Running the allocation algorithm consists of running the optimization itself (wh
# | code-fold: true
model = Ribasim.Model(toml_path)
graph, verts = tracecall((Ribasim,), Ribasim.update_allocation!, (model.integrator,))
plot_graph(graph, size = (2000, 1000), xlims = (-0.4, 5.5))
plot_graph(graph)
```

## Discrete control
Expand All @@ -95,12 +100,14 @@ Discrete control works by a [`FunctionCallingCallback`](https://docs.sciml.ai/Di

```{julia}
# | code-fold: true
toml_path = normpath(@__DIR__, "../../generated_testmodels/pump_discrete_control/ribasim.toml")
toml_path =
normpath(@__DIR__, "../../generated_testmodels/pump_discrete_control/ribasim.toml")
model = Ribasim.Model(toml_path)
(; u, t) = model.integrator
model.integrator.p.basin.storage0 .= [0.1, 100.0]
graph, verts = tracecall((Ribasim,), Ribasim.apply_discrete_control!, (u, t, model.integrator))
plot_graph(graph; size = (1300, 500), prune_from = [:water_balance!], xlims = (-0.5, 3.5))
graph, verts =
tracecall((Ribasim,), Ribasim.apply_discrete_control!, (u, t, model.integrator))
plot_graph(graph; prune_from = [:water_balance!], max_depth = 3)
```

## Writing output
Expand All @@ -111,5 +118,5 @@ Writing output (currently) happens only after the full simulation is finished. F
toml_path = normpath(@__DIR__, "../../generated_testmodels/basic_transient/ribasim.toml")
model = Ribasim.Model(toml_path)
graph, verts = tracecall((Ribasim,), Ribasim.write_results, (model,))
plot_graph(graph, size = (1600, 1000), xlims = (-0.5, 4.5))
plot_graph(graph; max_depth = 4)
```
34 changes: 23 additions & 11 deletions docs/dev/scripts/plot_trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function plot_edges!(ax, graph, max_depth, nodes_per_depth; n_points = 25)
x = range(nm_src.loc[1], nm_dst.loc[1]; length = n_points)
y = @. A * cos(B * (x - nm_src.loc[1])) + C

color = RGB((0.8 * rand(3))...)
color = RGBA((0.8 * rand(3))..., 0.5)
linestyle = (nm_src.file == nm_dst.file) ? :solid : :dash
lines!(ax, x, y; color, linestyle)
end
Expand All @@ -141,10 +141,10 @@ function plot_labels!(ax, graph, max_depth, color_dict)
x,
y;
text = "$nm",
color = get(color_dict, nm.file, :black),
color = :black,
font = :bold,
strokecolor = :black,
strokewidth = 1.0,
strokecolor = get(color_dict, nm.file, :black),
strokewidth = 0.5,
label = String(nm.file),
align = (:center, :bottom),
)
Expand All @@ -154,7 +154,7 @@ end

function plot_graph(
graph_orig::MetaGraph;
size = (1000, 1000),
size = (2000, 1000),
max_depth::Int = 5,
plot_non_Ribasim::Bool = false,
squash_per_depth::Bool = true,
Expand Down Expand Up @@ -189,27 +189,39 @@ function plot_graph(
colors = distinguishable_colors(length(files) + 1)[end:-1:2]
color_dict = OrderedDict(zip(files, colors))

delete!(theme(nothing), :resolution) # Needed because of a refactor in Makie going from resolution to size
theme = theme_minimal()
set_theme!(theme)
delete!(theme, :resolution) # Needed because of a refactor in Makie going from resolution to size
f = Figure(; size = size)
ax = Axis(f[1, 1]; xlabel = "depth", xticks = 0:max_depth)
ax = Axis(f[1, 1]; xlabel = "depth", xticks = 0:max_depth)
plot_edges!(ax, graph, max_depth, nodes_per_depth)
plot_labels!(ax, graph, max_depth, color_dict)
hideydecorations!(ax)
!isnothing(xlims) && xlims!(ax, xlims...)
hidespines!(ax)
isnothing(xlims) ? xlims!(ax, -0.25, max_depth + 0.5) : xlims!(ax, xlims...)

# Build legend
elements = LegendElement[
MarkerElement(; color = c, marker = :rect) for c in values(color_dict)
]
descriptions = String.(files)
descriptions = basename.(String.(files))

push!(elements, LineElement(; color = :black, linestyle = :dash))
push!(descriptions, "between scripts")

push!(elements, LineElement(; color = :black, linestyle = :solid))
push!(descriptions, "within a script")

Legend(f[1, 2], elements, descriptions)

axislegend(
ax,
elements,
descriptions;
position = :lt,
framevisible = true,
margin = (20, 20, 20, 20),
padding = 10,
framecolor = :lightgrey,
)
resize_to_layout!(f)
f
end

0 comments on commit 8cdea9f

Please sign in to comment.