Skip to content

Commit

Permalink
encode load-time graphically, close #86. remap, lerp, `time_to_si…
Browse files Browse the repository at this point in the history
…ze`, `sqrt`, `relsize`

added undocument kwargs: relsize & fontsize for todotstr, and loadtime for depsasdot (and depweb,depimg)

è carino
  • Loading branch information
tfiers committed Jan 29, 2023
1 parent 7f92062 commit 6de4bd5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/DotString/DotString.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ function to_dot_str(
emptymsg = nothing,
faded = nothing,
nodeinfo = nothing,
fontsize = 14, # 14 is dot default
relsize = nothing,
)
lines = ["digraph {"] # DIrected graph
tab = " "^indent
addline(l) = push!(lines, tab * l)
bgcolor = "bgcolor = \"$bg\""
nodefontsize = "node [fontsize=$fontsize]"
colourscheme = dark ? darkmode : lightmode
for str in [bgcolor; colourscheme; style]
for str in [bgcolor; colourscheme; nodefontsize; style]
addline(str)
end
for (m, n) in edges
Expand All @@ -47,6 +50,13 @@ function to_dot_str(
addline("$node [label=\"$node\\n$info\"]")
end
end
isnothing(relsize) || for node in vertices(edges)
if node in keys(relsize)
relsize_ = relsize[node]
fontsize_ = relsize_ * fontsize
addline("$node [fontsize=$fontsize_]")
end
end
if !isnothing(emptymsg) && isempty(edges)
addline(single_node(emptymsg))
end
Expand All @@ -58,7 +68,7 @@ single_node(text) = "onlynode [label=\" $text \", shape=plaintext]"
# ↪ the extra spaces around the text are for some padding in the output png (eg)

default_style() = [
"node [fontname=\"sans-serif\", fontsize=14]", # 14 is default
"node [fontname=\"sans-serif\"]",
"node [color=none, width=1, height=0.3]",
# Default width is 0.75 (but expands for label), height 0.5
# Larger width spaces nodes out more
Expand Down
47 changes: 44 additions & 3 deletions src/deps-as-dot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ results in the graph. (Julia 1.8 and higher only).
"""
depgraph_as_dotstr(
pkgname;
emptymsg = "($pkgname has no dependencies)",
faded = is_in_stdlib(pkgname) ? nothing : is_in_stdlib,
time = false,
emptymsg = "($pkgname has no dependencies)",
faded = is_in_stdlib(pkgname) ? nothing : is_in_stdlib,
time = false,
loadtimes = nothing, # For if precalculated (quick iteration in dev)
kw...
) = begin
edges = depgraph(pkgname; select(kw, depgraph)...)
if time
loadtimes = time_imports(pkgname)
end
if !isnothing(loadtimes)
nodeinfo = Dict(
pkgname => "[$time ms]"
for (pkgname, time) in loadtimes
)
fontsize, relsize = time_to_size(loadtimes)
else
nodeinfo = nothing
fontsize = 14
relsize = nothing
end
dotstr = to_dot_str(
edges;
select(kw, to_dot_str)...,
emptymsg,
faded,
nodeinfo,
fontsize,
relsize,
)
end

Expand All @@ -68,3 +76,36 @@ select(kw, f) = begin
if name in kwargnames_f
]
end

function time_to_size(loadtimes)
min_fontsize = 7
max_fontsize = 24
max_rel = max_fontsize / min_fontsize
max_time = maximum(x.time_ms for x in loadtimes)
input_range = 0:max_time
output_range = 1:max_rel
f = sqrt
relsize = Dict(
pkgname => remap(time, input_range, output_range, f)
for (pkgname, time) in loadtimes
)
(; fontsize = min_fontsize, relsize)
end

function remap(x, input_range, output_range, f=identity)
# 24 and (20,30) becomes 0.4
y = fraction(x, input_range)
# `f` is a function mapping [0, 1] to [0, 1]
z = f(y)
# 0.2 and (100,200) becomes 120
w = lerp(z, output_range)
end

"Linearly interpolate ('lerp') between `a` (`t = 0`) and `b` (`t = 1`)"
lerp(t, a, b) = a + t * (b - a)
lerp(t, range) = lerp(t, first(range), last(range))

"Map `v` ∈ [`a`, `b`] to a a fraction ∈ [0, 1]"
fraction(v, a, b) = (v - a) / (b - a)
fraction(v, range) = fraction(v, first(range), last(range))
# aka 'inverse lerp'
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Graphviz_jll = "3c863552-8265-54e4-a6dc-903eb78fde85"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
Expand Down
4 changes: 3 additions & 1 deletion test/integration/deps-as-dot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
bgcolor = "transparent"
node [fontcolor="black"]
edge [color="black"]
node [fontname="sans-serif", fontsize=14]
node [fontsize=14]
node [fontname="sans-serif"]
node [color=none, width=1, height=0.3]
edge [arrowsize=0.8]
TOML -> Dates
Expand All @@ -22,6 +23,7 @@
bgcolor = "transparent"
node [fontcolor="black"]
edge [color="black"]
node [fontsize=14]
onlynode [label=\" (URIs has no dependencies) \", shape=plaintext]
}
"""
Expand Down
2 changes: 2 additions & 0 deletions test/unit/DotString.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Test
bgcolor = "blue"
node [fontcolor="black"]
edge [color="black"]
node [fontsize=14]
node [color="red"]
A -> B
yes -> no
Expand All @@ -26,6 +27,7 @@ using Test
bgcolor = "transparent"
node [fontcolor="white"]
edge [color="white"]
node [fontsize=14]
onlynode [label=\" (empty graph) \", shape=plaintext]
}
"""
Expand Down

0 comments on commit 6de4bd5

Please sign in to comment.