Wed, 15 Dec 2021 10:36:43 +0200
Improve license phrasing
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 |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
10 | #using Plots |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
11 | #using Measures |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
12 | import GR |
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 |
16
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
17 | using AlgTools.Comms |
54
471f9f64ff2d
Add ColourTools for colourmapped images
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
18 | using ..ColourTools |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | |
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 | # Our exports |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | ############## |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | export LogEntry, |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | bg_visualise, |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | visualise, |
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 | ################ |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | # Visualisation |
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 | function bg_visualise(rc) |
16
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
63 | process_channel(do_visualise, rc) |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
64 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
65 | |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
66 | # function do_visualise(imgs) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
67 | # plt = im -> plot(grayimg(im), showaxis=false, grid=false, aspect_ratio=:equal, margin=2mm) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
68 | # display(plot([plt(imgs[i]) for i =1:length(imgs)]..., reuse=true, margin=0mm)) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
69 | # end |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
70 | |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
71 | grayGR = x -> begin |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
72 | y = round(UInt32, 0xff*clip(x)) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
73 | return 0x010101*y + 0xff000000 |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
74 | end |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
75 | |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
76 | function fill_viewport(vp, c) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
77 | GR.savestate() |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
78 | GR.selntran(0) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
79 | GR.setscale(0) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
80 | GR.setfillintstyle(GR.INTSTYLE_SOLID) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
81 | GR.setfillcolorind(c) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
82 | GR.fillrect(vp...) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
83 | GR.selntran(1) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
84 | GR.restorestate() |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
85 | end |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
86 | |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
87 | function do_visualise(imgs; refresh=true, fullscreen=false) |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
88 | n = length(imgs) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
89 | # Get device dimensions in metres and pixels |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
90 | scrw, scrh, pw, ph = GR.inqdspsize() |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
91 | imgaspect = n > 0 ? float(size(imgs[1], 1))/float(size(imgs[1], 2)) : 1 |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
92 | # Scaling to maximum size window |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
93 | sc=0.7 |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
94 | # Set up window and transformations |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
95 | GR.clearws() |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
96 | GR.setscale(0); |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
97 | GR.selntran(1) |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
98 | # - First OS window size |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
99 | if fullscreen |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
100 | w, h = float(scrw), float(scrh) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
101 | elseif scrw/n>scrh |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
102 | w, h = float(sc*scrh*n), float(sc*scrh*imgaspect) |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
103 | else |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
104 | w, h = float(sc*scrw), float(sc*scrw/n*imgaspect) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
105 | end |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
106 | GR.setwsviewport(0, w, 0, h) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
107 | # NDC to device |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
108 | if w>h |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
109 | canvas=[0, 1, 0, h/w] |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
110 | else |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
111 | canvas=[0, w/h, 0, 1] |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
112 | end |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
113 | GR.setwswindow(canvas...) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
114 | fill_viewport(canvas, 1) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
115 | # World coordinates to NDC |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
116 | if imgaspect/n<h/w |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
117 | y0 = (canvas[3]+canvas[4])/2 |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
118 | ww = (canvas[2]-canvas[1])/2 |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
119 | y1 = y0-ww*(imgaspect/n) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
120 | y2 = y0+ww*(imgaspect/n) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
121 | GR.setviewport(canvas[1], canvas[2], y1, y2) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
122 | else |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
123 | x0 = (canvas[1]+canvas[2])/2 |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
124 | hh = (canvas[4]-canvas[3])/2 |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
125 | x1 = x0-hh*(n/imgaspect) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
126 | x2 = x0+hh*(n/imgaspect) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
127 | GR.setviewport(x1, x2, canvas[3], canvas[4]) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
128 | end |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
129 | GR.setwindow(0, n, 0, 1) |
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
130 | # Clear background |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
131 | # Plot images |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
132 | for i=1:n |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
133 | im = imgs[i]' |
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
134 | sz = size(im) |
34
fe9ef03a91b2
"fullscreen" mode modulo GR being capable of it, better transformations
Tuomo Valkonen <tuomov@iki.fi>
parents:
27
diff
changeset
|
135 | GR.drawimage(i-1, i, 0, 1, sz[1], sz[2], grayGR.(im)) |
14
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
Tuomo Valkonen <tuomov@iki.fi>
parents:
13
diff
changeset
|
136 | end |
16
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
137 | if refresh |
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
138 | GR.updatews() |
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
139 | end |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
140 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
141 | |
11
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
142 | 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
|
143 | if isa(channel_or_toggle, Channel) |
16
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
144 | put_onlylatest!(channel_or_toggle, imgs) |
11
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
145 | 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
|
146 | do_visualise(imgs) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
148 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
149 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
150 | ###################################################### |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
151 | # Iterator that does visualisation and log collection |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
152 | ###################################################### |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
153 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
154 | function iterate_visualise(st :: State, |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
155 | step :: Function, |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
156 | params :: NamedTuple) where DisplacementT |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
157 | try |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
158 | for iter=1:params.maxiter |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
159 | st = step() do calc_objective |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
160 | if isnothing(st.start_time) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
161 | # 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
|
162 | # boundaries, so only start timing after the first iteration. |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
163 | st = @set st.start_time=secs_ns() |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
164 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
165 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
166 | 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
|
167 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
168 | 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
|
169 | verb_start = secs_ns() |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
170 | tm = verb_start - st.start_time - st.wasted_time |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
171 | value, x = calc_objective() |
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 | entry = LogEntry(iter, tm, value) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
174 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
175 | # (**) 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
|
176 | # while iterating |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
177 | st = @set st.log=LinkedListEntry(entry, st.log) |
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 | if verb |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
180 | @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
|
181 | visualise(st.vis, (x,)) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
182 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
183 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
184 | if params.save_iterations |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
185 | fn = t -> "$(params.save_prefix)_$(t)_iter$(iter).png" |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
186 | save(File(format"PNG", fn("reco")), grayimg(x)) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
187 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
188 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
189 | st = @set st.wasted_time += (secs_ns() - verb_start) |
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 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
192 | return st |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
193 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
194 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
195 | catch ex |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
196 | if isa(ex, InterruptException) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
197 | # If SIGINT is received (user pressed ^C), terminate computations, |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
198 | # 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
|
199 | # ending the iterations, but letting the algorithm finish up. |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
200 | # 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
|
201 | # 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
|
202 | # 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
|
203 | # the next iteration is finished. |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
204 | 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
|
205 | else |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
206 | throw(ex) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
207 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
208 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
209 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
210 | return st |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
211 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
212 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
213 | #################### |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
214 | # Launcher routines |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
215 | #################### |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
216 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
217 | function initialise_visualisation(visualise; iterator=iterate_visualise) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
218 | # Create visualisation |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
219 | if visualise |
26 | 220 | rc = Channel(1) |
11
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
221 | visproc = Threads.@spawn bg_visualise(rc) |
27 | 222 | bind(rc, visproc) |
223 | vis = rc | |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
224 | else |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
225 | vis = false |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
226 | visproc = nothing |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
227 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
228 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
229 | st = State(vis, visproc, nothing, 0.0, nothing) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
230 | iterate = curry(iterate_visualise, st) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
231 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
232 | return st, iterate |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
233 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
234 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
235 | 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
|
236 | if isa(st.vis, Channel) |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
237 | # 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
|
238 | put!(st.vis, nothing) |
27 | 239 | close(st.vis) |
9
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
240 | wait(st.visproc) |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
241 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
242 | end |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
243 | |
1cffd3d07fe2
Denoising routine for testing + visualisation tools
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
244 | end # Module |