src/Visualise.jl

changeset 9
1cffd3d07fe2
child 11
f1bbdf68f35b
equal deleted inserted replaced
8:c5aabb2c41d9 9:1cffd3d07fe2
1 ##################################################
2 # Visualising and data-collecting iteration tools
3 ##################################################
4
5 module Visualise
6
7 using Printf
8 using Distributed
9 using FileIO
10 using Setfield
11 using Images, Plots, Measures
12
13 using AlgTools.Util
14 using AlgTools.StructTools
15 using AlgTools.LinkedLists
16
17 ##############
18 # Our exports
19 ##############
20
21 export LogEntry,
22 bg_visualise,
23 visualise,
24 clip,
25 grayimg,
26 secs_ns,
27 iterate_visualise,
28 initialise_visualisation,
29 finalise_visualisation
30
31 ##################
32 # Data structures
33 ##################
34
35 struct LogEntry <: IterableStruct
36 iter :: Int
37 time :: Float64
38 function_value :: Float64
39 end
40
41 struct State
42 vis :: Union{Distributed.RemoteChannel,Bool,Nothing}
43 visproc :: Union{Nothing,Future}
44 start_time :: Union{Real,Nothing}
45 wasted_time :: Real
46 log :: LinkedList{LogEntry}
47 end
48
49 ##################
50 # Helper routines
51 ##################
52
53 @inline function secs_ns()
54 return convert(Float64, time_ns())*1e-9
55 end
56
57 clip = x -> min(max(x, 0.0), 1.0)
58 grayimg = im -> Gray.(clip.(im))
59
60 ################
61 # Visualisation
62 ################
63
64 function bg_visualise(rc)
65 while true
66 imgs=take!(rc)
67 # Take only the latest image to visualise
68 while isready(rc)
69 imgs=take!(rc)
70 end
71 # We're done if we were fed an empty image
72 if isnothing(imgs)
73 break
74 end
75 do_visualise(imgs)
76 end
77 return
78 end
79
80 function do_visualise(imgs)
81 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))
83 end
84
85 function visualise(rc_or_vis, imgs)
86 if isa(rc_or_vis, RemoteChannel)
87 rc = rc_or_vis
88 while isready(rc)
89 take!(rc)
90 end
91 put!(rc, imgs)
92 elseif isa(rc_or_vis, Bool) && rc_or_vis
93 do_visualise(imgs)
94 end
95 end
96
97 ######################################################
98 # Iterator that does visualisation and log collection
99 ######################################################
100
101 function iterate_visualise(st :: State,
102 step :: Function,
103 params :: NamedTuple) where DisplacementT
104 try
105 for iter=1:params.maxiter
106 st = step() do calc_objective
107 if isnothing(st.start_time)
108 # The Julia precompiler is a miserable joke, apparently not crossing module
109 # boundaries, so only start timing after the first iteration.
110 st = @set st.start_time=secs_ns()
111 end
112
113 verb = params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0
114
115 if verb || iter ≤ 20 || (iter ≤ 200 && mod(iter, 10) == 0)
116 verb_start = secs_ns()
117 tm = verb_start - st.start_time - st.wasted_time
118 value, x = calc_objective()
119
120 entry = LogEntry(iter, tm, value)
121
122 # (**) Collect a singly-linked list of log to avoid array resizing
123 # while iterating
124 st = @set st.log=LinkedListEntry(entry, st.log)
125
126 if verb
127 @printf("%d/%d J=%f\n", iter, params.maxiter, value)
128 visualise(st.vis, (x,))
129 end
130
131 if params.save_iterations
132 fn = t -> "$(params.save_prefix)_$(t)_iter$(iter).png"
133 save(File(format"PNG", fn("reco")), grayimg(x))
134 end
135
136 st = @set st.wasted_time += (secs_ns() - verb_start)
137 end
138
139 return st
140 end
141 end
142 catch ex
143 if isa(ex, InterruptException)
144 # If SIGINT is received (user pressed ^C), terminate computations,
145 # returning current status. Effectively, we do not call `step()` again,
146 # ending the iterations, but letting the algorithm finish up.
147 # Assuming (**) above occurs atomically, `st.log` should be valid, but
148 # any results returned by the algorithm itself may be partial, as for
149 # reasons of efficiency we do *not* store results of an iteration until
150 # the next iteration is finished.
151 printstyled("\rUser interrupt—finishing up.\n", bold=true, color=202)
152 else
153 throw(ex)
154 end
155 end
156
157 return st
158 end
159
160 ####################
161 # Launcher routines
162 ####################
163
164 function initialise_visualisation(visualise; iterator=iterate_visualise)
165 # Create visualisation
166 if visualise
167 rc = RemoteChannel()
168 visproc = @spawn bg_visualise(rc)
169 vis =rc
170 #vis = true
171
172 sleep(0.01)
173 else
174 vis = false
175 visproc = nothing
176 end
177
178 st = State(vis, visproc, nothing, 0.0, nothing)
179 iterate = curry(iterate_visualise, st)
180
181 return st, iterate
182 end
183
184 function finalise_visualisation(st)
185 if isa(st.rc, RemoteChannel)
186 # Tell subprocess to finish, and wait
187 put!(st.rc, nothing)
188 wait(st.visproc)
189 end
190 end
191
192 end # Module

mercurial