Skip to content

Commit 05b4b9f

Browse files
authored
Merge pull request #241 from JuliaDynamics/hw/fixgpplot
fixes behavior of colors in inspector graphplot
2 parents 8282e6a + 3ed2fd8 commit 05b4b9f

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

NetworkDynamicsInspector/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "NetworkDynamicsInspector"
22
uuid = "0a4713f2-d58f-43f2-b63b-1b5d5ee4e65a"
33
authors = ["Hans Würfel <[email protected]>"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
Bonito = "824d6782-a2ef-11e9-3a09-e5662e0c26f8"

NetworkDynamicsInspector/src/appstate.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ function set_graphplot!(; nstate = NotSpecified(),
140140
ncolorrange = NotSpecified(),
141141
ecolorrange = NotSpecified())
142142
sync()
143+
estate = estate isa Symbol ? [estate] : estate
144+
nstate = nstate isa Symbol ? [nstate] : nstate
143145
gp = appstate().graphplot
144146
set_maybe!(gp.nstate, nstate)
145147
set_maybe!(gp.estate, estate)

NetworkDynamicsInspector/src/graphplot.jl

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,35 @@ function gpstate_control_card(app, type)
323323
r = Float32.(_maxrange(_sol, idxs, _rel))
324324

325325
if r[1] == r[2]
326-
r = r[1] < 0 ? (r[1], 0.0f0) : (0.0f0, r[2])
326+
r = if r[1] < 0
327+
(r[1], 0.0f0)
328+
elseif r[2] > 0
329+
(0.0f0, r[2])
330+
else # both zero
331+
(0.0f0, 1.0f0)
332+
end
327333
colorscheme[] = ColorScheme([colorant"gray"])
328334
elseif r[1] < 0 && r[2] > 0
329335
r = (-maximum(abs.(r)), maximum(abs.(r)))
330336
colorscheme[] = ColorSchemes.coolwarm
331337
elseif r[1] 0
332338
r = (0.0f0, r[2])
333-
colorscheme.val = ColorSchemes.thermal
339+
colorscheme[] = ColorSchemes.thermal
340+
elseif r[2] 0
341+
r = (r[1], 0.0f0)
342+
colorscheme[] = reverse(ColorSchemes.thermal)
334343
end
335344

336345
maxrange[] = r
337346
# adjust thumb position
338347
new_thumbs = get(thumb_pos_cache, thumb_pos_key(), r)
339-
thumb_l[], thumb_r[] = new_thumbs
348+
# first set both without notify, otherwise they might be set to the same value
349+
# which leads to invalid color range
350+
thumb_l.val = max(r[1], new_thumbs[1])
351+
thumb_r.val = min(r[2], new_thumbs[2])
352+
# we need to notify both for gui, even if this leads to multi update on colorrange
353+
notify(thumb_l); notify(thumb_r)
354+
# thumb_r[] = new_thumbs # update both for slider
340355
else
341356
error("More than one state for maxrange calculation...")
342357
end
@@ -390,10 +405,12 @@ function _maxrange(sol, idxs, rel)
390405

391406
u_for_t = sol(sol.t; idxs=idxs[mask]).u
392407
if rel
408+
u0 = copy(u_for_t[1])
393409
for u in u_for_t
394-
u .-= u_for_t[1]
410+
u .-= u0
395411
end
396412
end
413+
397414
extrema(Iterators.flatten(u_for_t))
398415
end
399416

NetworkDynamicsInspector/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ end
133133
end
134134
end
135135

136+
@testset "change colorscheme" begin
137+
sol = get_sol()
138+
inspect(sol; display=ElectronDisp(), restart=true, reset=true)
139+
140+
@test NetworkDynamicsInspector._maxrange(sol, vidxs(sol, :, :M), false) == (1.0, 1.0)
141+
@test NetworkDynamicsInspector._maxrange(sol, vidxs(sol, :, :M), true) == (0.0, 0.0)
142+
@test NetworkDynamicsInspector._maxrange(sol, eidxs(sol, :, :active), false) == (0.0, 1.0)
143+
@test NetworkDynamicsInspector._maxrange(sol, eidxs(sol, :, :active), true) == (-1.0, 0.0)
144+
145+
set_graphplot!(; estate=:active)
146+
set_graphplot!(; estate_rel=true)
147+
set_graphplot!(; estate_rel=false)
148+
end
149+
136150
@testset "Widget Tests" begin
137151
@safetestset "Multiselect Tests" begin include("multiselect_test.jl") end
138152
@safetestset "Slider Tests" begin include("slider_test.jl") end

0 commit comments

Comments
 (0)