src/Visualise.jl

Fri, 06 Dec 2019 21:19:21 +0200

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 06 Dec 2019 21:19:21 +0200
changeset 13
bda471ea9d67
parent 11
f1bbdf68f35b
child 14
8ff303666d8b
permissions
-rw-r--r--

Do not depend on Images, just ColorTypes

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

mercurial