src/Visualise.jl

changeset 11
f1bbdf68f35b
parent 9
1cffd3d07fe2
child 13
bda471ea9d67
equal deleted inserted replaced
10:fdf2f44be973 11:f1bbdf68f35b
3 ################################################## 3 ##################################################
4 4
5 module Visualise 5 module Visualise
6 6
7 using Printf 7 using Printf
8 using Distributed
9 using FileIO 8 using FileIO
10 using Setfield 9 using Setfield
11 using Images, Plots, Measures 10 using Images, Plots, Measures
12 11
13 using AlgTools.Util 12 using AlgTools.Util
37 time :: Float64 36 time :: Float64
38 function_value :: Float64 37 function_value :: Float64
39 end 38 end
40 39
41 struct State 40 struct State
42 vis :: Union{Distributed.RemoteChannel,Bool,Nothing} 41 vis :: Union{Channel,Bool,Nothing}
43 visproc :: Union{Nothing,Future} 42 visproc :: Union{Nothing,Task}
44 start_time :: Union{Real,Nothing} 43 start_time :: Union{Real,Nothing}
45 wasted_time :: Real 44 wasted_time :: Real
46 log :: LinkedList{LogEntry} 45 log :: LinkedList{LogEntry}
47 end 46 end
48 47
80 function do_visualise(imgs) 79 function do_visualise(imgs)
81 plt = im -> plot(grayimg(im), showaxis=false, grid=false, aspect_ratio=:equal, margin=2mm) 80 plt = im -> plot(grayimg(im), showaxis=false, grid=false, aspect_ratio=:equal, margin=2mm)
82 display(plot([plt(imgs[i]) for i =1:length(imgs)]..., reuse=true, margin=0mm)) 81 display(plot([plt(imgs[i]) for i =1:length(imgs)]..., reuse=true, margin=0mm))
83 end 82 end
84 83
85 function visualise(rc_or_vis, imgs) 84 function visualise(channel_or_toggle, imgs)
86 if isa(rc_or_vis, RemoteChannel) 85 if isa(channel_or_toggle, Channel)
87 rc = rc_or_vis 86 rc = channel_or_toggle
88 while isready(rc) 87 while isready(rc)
89 take!(rc) 88 take!(rc)
90 end 89 end
91 put!(rc, imgs) 90 put!(rc, imgs)
92 elseif isa(rc_or_vis, Bool) && rc_or_vis 91 elseif isa(channel_or_toggle, Bool) && channel_or_toggle
93 do_visualise(imgs) 92 do_visualise(imgs)
94 end 93 end
95 end 94 end
96 95
97 ###################################################### 96 ######################################################
162 #################### 161 ####################
163 162
164 function initialise_visualisation(visualise; iterator=iterate_visualise) 163 function initialise_visualisation(visualise; iterator=iterate_visualise)
165 # Create visualisation 164 # Create visualisation
166 if visualise 165 if visualise
167 rc = RemoteChannel() 166 rc = Channel()
168 visproc = @spawn bg_visualise(rc) 167 visproc = Threads.@spawn bg_visualise(rc)
169 vis =rc 168 vis =rc
170 #vis = true 169 #vis = true
171 170
172 sleep(0.01) 171 sleep(0.01)
173 else 172 else
180 179
181 return st, iterate 180 return st, iterate
182 end 181 end
183 182
184 function finalise_visualisation(st) 183 function finalise_visualisation(st)
185 if isa(st.rc, RemoteChannel) 184 if isa(st.vis, Channel)
186 # Tell subprocess to finish, and wait 185 # Tell subprocess to finish, and wait
187 put!(st.rc, nothing) 186 put!(st.vis, nothing)
188 wait(st.visproc) 187 wait(st.visproc)
189 end 188 end
190 end 189 end
191 190
192 end # Module 191 end # Module

mercurial