Wed, 25 Dec 2019 17:42:32 +0200
denoise_fista
| 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: 
11diff
changeset | 10 | using ColorTypes: Gray | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 11 | #using Plots | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 12 | #using Measures | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 13 | import GR | 
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 14 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 15 | using AlgTools.Util | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 16 | using AlgTools.StructTools | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 17 | using AlgTools.LinkedLists | 
| 16 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 18 | using AlgTools.Comms | 
| 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 | clip, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 28 | grayimg, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 29 | secs_ns, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 30 | iterate_visualise, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 31 | initialise_visualisation, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 32 | finalise_visualisation | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 33 | |
| 
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 | # Data structures | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 36 | ################## | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 37 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 38 | struct LogEntry <: IterableStruct | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 39 | iter :: Int | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 40 | time :: Float64 | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 41 | function_value :: Float64 | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 42 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 43 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 44 | struct State | 
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 45 | vis :: Union{Channel,Bool,Nothing} | 
| 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 46 | visproc :: Union{Nothing,Task} | 
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 47 | start_time :: Union{Real,Nothing} | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 48 | wasted_time :: Real | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 49 | log :: LinkedList{LogEntry} | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 50 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 51 | |
| 
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 | # Helper routines | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 54 | ################## | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 55 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 56 | @inline function secs_ns() | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 57 | return convert(Float64, time_ns())*1e-9 | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 58 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 59 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 60 | clip = x -> min(max(x, 0.0), 1.0) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 61 | grayimg = im -> Gray.(clip.(im)) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 62 | |
| 
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 | # Visualisation | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 65 | ################ | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 66 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 67 | function bg_visualise(rc) | 
| 16 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 68 | process_channel(do_visualise, rc) | 
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 69 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 70 | |
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 71 | # function do_visualise(imgs) | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 72 | # 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: 
13diff
changeset | 73 | # 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: 
13diff
changeset | 74 | # end | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 75 | |
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 76 | grayGR = x -> begin | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 77 | y = round(UInt32, 0xff*clip(x)) | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 78 | return 0x010101*y + 0xff000000 | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 79 | end | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 80 | |
| 16 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 81 | function do_visualise(imgs; refresh=true) | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 82 | n = length(imgs) | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 83 | # 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: 
13diff
changeset | 84 | scrw, scrh, pw, ph = GR.inqdspsize() | 
| 24 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 85 | aspect = n > 0 ? Float64(size(imgs[1], 1))/Float64(size(imgs[1], 2)) : 1 | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 86 | # Scaling to maximums ize window | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 87 | sc=0.7 | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 88 | # Set up window and transformations | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 89 | GR.clearws() | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 90 | GR.setscale(0); | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 91 | GR.selntran(1) | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 92 | # - First OS window size | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 93 | if scrw/n>scrh | 
| 24 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 94 | GR.setwsviewport(0, sc*scrh*n, 0, sc*scrh*aspect) | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 95 | else | 
| 24 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 96 | GR.setwsviewport(0, sc*scrw, 0, sc*scrw/n*aspect) | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 97 | end | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 98 | # - Part of normalised window shown | 
| 24 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 99 | GR.setwswindow(0, 1, 0, 1/n*aspect); | 
| 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 100 | GR.setviewport(0, 1, 0, 1/n*aspect); | 
| 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 101 | # World coordinates | 
| 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 102 | GR.setwindow(0, n, 0, aspect) | 
| 14 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 103 | # Plot images | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 104 | for i=1:n | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 105 | im = imgs[i]' | 
| 
8ff303666d8b
Don't depend on Plots. It's slow. Use GR directly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
13diff
changeset | 106 | sz = size(im) | 
| 24 
f6630036e588
Visualisation does aspect ratio correctly.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
16diff
changeset | 107 | GR.drawimage(i-1, i, 0, aspect, 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: 
13diff
changeset | 108 | end | 
| 16 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 109 | if refresh | 
| 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 110 | GR.updatews() | 
| 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 111 | end | 
| 9 
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 | |
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 114 | function visualise(channel_or_toggle, imgs) | 
| 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 115 | if isa(channel_or_toggle, Channel) | 
| 16 
9b0cfc67d6cb
Use Channel comms helper tools form AlgTools.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
15diff
changeset | 116 | put_onlylatest!(channel_or_toggle, imgs) | 
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 117 | 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 | 118 | do_visualise(imgs) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 119 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 120 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 121 | |
| 
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 | # Iterator that does visualisation and log collection | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 124 | ###################################################### | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 125 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 126 | function iterate_visualise(st :: State, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 127 | step :: Function, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 128 | params :: NamedTuple) where DisplacementT | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 129 | try | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 130 | for iter=1:params.maxiter | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 131 | st = step() do calc_objective | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 132 | if isnothing(st.start_time) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 133 | # 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 | 134 | # boundaries, so only start timing after the first iteration. | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 135 | st = @set st.start_time=secs_ns() | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 136 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 137 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 138 | 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 | 139 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 140 | 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 | 141 | verb_start = secs_ns() | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 142 | tm = verb_start - st.start_time - st.wasted_time | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 143 | value, x = calc_objective() | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 144 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 145 | entry = LogEntry(iter, tm, value) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 146 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 147 | # (**) 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 | 148 | # while iterating | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 149 | st = @set st.log=LinkedListEntry(entry, st.log) | 
| 
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 | if verb | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 152 | @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 | 153 | visualise(st.vis, (x,)) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 154 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 155 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 156 | if params.save_iterations | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 157 | fn = t -> "$(params.save_prefix)_$(t)_iter$(iter).png" | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 158 | save(File(format"PNG", fn("reco")), grayimg(x)) | 
| 
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 | st = @set st.wasted_time += (secs_ns() - verb_start) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 162 | end | 
| 
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 | return st | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 165 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 166 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 167 | catch ex | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 168 | if isa(ex, InterruptException) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 169 | # If SIGINT is received (user pressed ^C), terminate computations, | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 170 | # 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 | 171 | # ending the iterations, but letting the algorithm finish up. | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 172 | # 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 | 173 | # 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 | 174 | # 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 | 175 | # the next iteration is finished. | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 176 | 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 | 177 | else | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 178 | throw(ex) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 179 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 180 | end | 
| 
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 | 
| 
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 | #################### | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 186 | # Launcher routines | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 187 | #################### | 
| 
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 | function initialise_visualisation(visualise; iterator=iterate_visualise) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 190 | # Create visualisation | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 191 | if visualise | 
| 26 | 192 | rc = Channel(1) | 
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 193 | visproc = Threads.@spawn bg_visualise(rc) | 
| 27 | 194 | bind(rc, visproc) | 
| 195 | vis = rc | |
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 196 | else | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 197 | vis = false | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 198 | visproc = nothing | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 199 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 200 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 201 | st = State(vis, visproc, nothing, 0.0, nothing) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 202 | iterate = curry(iterate_visualise, st) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 203 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 204 | return st, iterate | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 205 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 206 | |
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 207 | function finalise_visualisation(st) | 
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 208 | if isa(st.vis, Channel) | 
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 209 | # Tell subprocess to finish, and wait | 
| 11 
f1bbdf68f35b
Use Threads.@spawn instead of the broken hang-prone Distributed.
 Tuomo Valkonen <tuomov@iki.fi> parents: 
9diff
changeset | 210 | put!(st.vis, nothing) | 
| 27 | 211 | close(st.vis) | 
| 9 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 212 | wait(st.visproc) | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 213 | end | 
| 
1cffd3d07fe2
Denoising routine for testing + visualisation tools
 Tuomo Valkonen <tuomov@iki.fi> parents: diff
changeset | 214 | end | 
| 
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 | end # Module |