src/Run.jl

Fri, 03 May 2024 12:32:58 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 03 May 2024 12:32:58 -0500
changeset 55
49cc73ebf939
parent 43
b1e4b1d2ef56
permissions
-rw-r--r--

Add script for packaging for Zenodo

36
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 ##################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
2 # Experiment running and interactive visualisation
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
3 ##################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 module Run
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
7 import GR
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 using Setfield
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
9 using Printf
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
10 using ImageQualityIndexes: assess_psnr, assess_ssim
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11 using FileIO
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
12 #using JLD2
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
13 using DelimitedFiles
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
15 using AlgTools.Util
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
16 using AlgTools.StructTools
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
17 using AlgTools.LinkedLists
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
18 using AlgTools.Comms
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
19
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20 using ImageTools.Visualise: secs_ns, grayimg, do_visualise
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
21
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
22 using ..ImGenerate
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
23 using ..OpticalFlow: identifier, DisplacementFull, DisplacementConstant
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
24
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
25 export run_experiments,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
26 Experiment,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 State,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 LogEntry,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 LogEntryHiFi
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
30
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
31 ################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
32 # Experiment
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33 ################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 struct Experiment
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 mod :: Module
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 DisplacementT :: Type
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 imgen :: ImGen
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 params :: NamedTuple
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42 function Base.show(io::IO, e::Experiment)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 displacementname(::Type{DisplacementFull}) = "DisplacementFull"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 displacementname(::Type{DisplacementConstant}) = "DisplacementConstant"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 print(io, "
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 mod: $(e.mod)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 DisplacementT: $(displacementname(e.DisplacementT))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 imgen: $(e.imgen.name) $(e.imgen.dim[1])×$(e.imgen.dim[2])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
49 params: $(e.params ⬿ (kernel = "(not shown)",))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
50 ")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
51 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
52
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 ################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54 # Log
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 ################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
56
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 struct LogEntry <: IterableStruct
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58 iter :: Int
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 time :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
60 function_value :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 #v_cumul_true_y :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 #v_cumul_true_x :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63 #v_cumul_est_y :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
64 #v_cumul_est_x :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65 psnr :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 ssim :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
67 #psnr_data :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
68 #ssim_data :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
69 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
70
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
71 struct LogEntryHiFi <: IterableStruct
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
72 iter :: Int
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
73 v_cumul_true_y :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
74 v_cumul_true_x :: Float64
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
75 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77 ###############
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 # Main routine
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 ###############
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
81 struct State
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 vis :: Union{Channel,Bool,Nothing}
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 start_time :: Union{Real,Nothing}
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84 wasted_time :: Real
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 log :: LinkedList{LogEntry}
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 log_hifi :: LinkedList{LogEntryHiFi}
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 aborted :: Bool
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 function name(e::Experiment, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 ig = e.imgen
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 id = if haskey(p, :predictor) && ~isnothing(p.predictor)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93 identifier(p.predictor)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
94 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
95 e.mod.identifier
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
96 end
43
b1e4b1d2ef56 Add variant field
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
97 if haskey(p, :variant)
b1e4b1d2ef56 Add variant field
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
98 id *= "_" * p.variant
b1e4b1d2ef56 Add variant field
Tuomo Valkonen <tuomov@iki.fi>
parents: 36
diff changeset
99 end
36
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100 # return "$(ig.name)_$(e.mod.identifier)_$(@sprintf "%x" hash(p))"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 return "$(ig.name)_$(id)_$(Int64(100*p.α))_$(Int64(10000*p.σ₀))_$(Int64(10000*p.τ₀))"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 function write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 open(texfile, "w") do io
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 wp = (n, v) -> println(io, "\\def\\EXPPARAM$(n){$(v)}")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 wf = (n, s) -> if isdefined(e_params, s)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 wp(n, getfield(e_params, s))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110 wf("alpha", :α)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
111 wf("sigmazero", :σ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 wf("tauzero", :τ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 wf("tildetauzero", :τ̃₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 wf("delta", :δ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 wf("lambda", :λ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 wf("theta", :θ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117 wf("maxiter", :maxiter)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 wf("noiselevel", :noise_level)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 wf("shakenoiselevel", :shake_noise_level)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 wf("shake", :shake)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 wf("timestep", :timestep)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 wf("displacementcount", :displacementcount)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123 wf("phantomrho", :phantom_ρ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 if isdefined(e_params, :σ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 wp("sigma", (e_params.σ₀ == 1 ? "" : "$(e_params.σ₀)") * "\\sigma_{\\max}")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 function run_experiments(;visualise=true,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
131 visfn = iterate_visualise,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 datatype = OnlineData,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
133 recalculate=true,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
134 experiments,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135 save_prefix="",
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 fullscreen=false,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 kwargs...)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139 # Create visualisation
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 if visualise
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141 rc = Channel(1)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 visproc = Threads.@spawn bg_visualise_enhanced(rc, fullscreen=fullscreen)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 bind(rc, visproc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 vis = rc
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 vis = false
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
148
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
149 # Run all experiments
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150 for e ∈ experiments
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 # Parameters for this experiment
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 e_params = e.params ⬿ kwargs
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 ename = name(e, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 e_params = e_params ⬿ (save_prefix = save_prefix * ename,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 dynrange = e.imgen.dynrange,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 Λ = e.imgen.Λ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
159 if recalculate || !isfile(e_params.save_prefix * ".txt")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 println("Running experiment \"$(ename)\"")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 # Start data generation task
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 datachannel = Channel{datatype{e.DisplacementT}}(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 gentask = Threads.@spawn e.imgen.f(e.DisplacementT, datachannel, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 bind(datachannel, gentask)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 # Run algorithm
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 iterate = curry(visfn, datachannel,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 State(vis, nothing, 0.0, nothing, nothing, false))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 x, y, st = e.mod.solve(e.DisplacementT;
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 dim=e.imgen.dim,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 iterate=iterate,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 params=e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 # Clear non-saveable things
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 st = @set st.vis = nothing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 println("Wasted_time: $(st.wasted_time)s")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181 if e_params.save_results
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 println("Saving " * e_params.save_prefix * "(.txt,_hifi.txt,_params.tex)")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 perffile = e_params.save_prefix * ".txt"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 hififile = e_params.save_prefix * "_hifi.txt"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 texfile = e_params.save_prefix * "_params.tex"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187 # datafile = e_params.save_prefix * ".jld2"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189 write_log(perffile, st.log, "# params = $(e_params)\n")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 #write_log(hififile, st.log_hifi, "# params = $(e_params)\n")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 #write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 # @save datafile x y st params
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
194
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
195 close(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 wait(gentask)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198 if st.aborted
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 break
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 println("Skipping already computed experiment \"$(ename)\"")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203 # texfile = e_params.save_prefix * "_params.tex"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204 # write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 if visualise
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 # Tell subprocess to finish, and wait
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 put!(rc, nothing)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 close(rc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 wait(visproc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
213 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
214
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 return
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219 ######################################################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220 # Iterator that does visualisation and log collection
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 ######################################################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223 function iterate_visualise(datachannel::Channel{OnlineData{DisplacementT}},
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 st :: State,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 step :: Function,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226 params :: NamedTuple) where DisplacementT
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227 try
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228 sc = nothing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 d = take!(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 for iter=1:params.maxiter
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233 dnext = take!(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 st = step(d.b_noisy, d.v, dnext.b_noisy) do calc_objective
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 stn = st
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 if isnothing(stn.start_time)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 # The Julia precompiler is a miserable joke, apparently not crossing module
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
239 # boundaries, so only start timing after the first iteration.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 stn = @set stn.start_time=secs_ns()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
241 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
242
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
243 verb = params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
244
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 # Normalise movement to image dimensions so
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
246 # our TikZ plotting code doesn't need to know
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
247 # the image pixel size.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
248 sc = 1.0./maximum(size(d.b_true))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
249
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
250 if verb || iter ≤ 20 || (iter ≤ 200 && mod(iter, 10) == 0)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
251 verb_start = secs_ns()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
252 tm = verb_start - stn.start_time - stn.wasted_time
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
253 value, x, v, vhist = calc_objective()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255 entry = LogEntry(iter, tm, value,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
256 #sc*d.v_cumul_true[1],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
257 #sc*d.v_cumul_true[2],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
258 #sc*v[1], sc*v[2],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
259 assess_psnr(x, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
260 assess_ssim(x, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
261 #assess_psnr(d.b_noisy, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
262 #assess_ssim(d.b_noisy, d.b_true)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
263 )
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
264
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
265 # (**) Collect a singly-linked list of log to avoid array resizing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
266 # while iterating
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
267 stn = @set stn.log=LinkedListEntry(entry, stn.log)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
268
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
269 if !isnothing(vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 vhist=vhist.*sc
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
271 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
272
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
273 if verb
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
274 @printf("%d/%d J=%f, PSNR=%f, SSIM=%f, avg. FPS=%f\n",
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
275 iter, params.maxiter, value, entry.psnr,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 entry.ssim, entry.iter/entry.time)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
277 if isa(stn.vis, Channel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
278 put_onlylatest!(stn.vis, ((d.b_noisy, x),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
279 params.plot_movement,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
280 stn.log, vhist))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
281
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
282 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
283 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
284
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
285 if params.save_images && (!haskey(params, :save_images_iters)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
286 || iter ∈ params.save_images_iters)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
287 fn = (t, ext) -> "$(params.save_prefix)_$(t)_frame$(iter).$(ext)"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
288 # save(File(format"PNG", fn("true", "png")), grayimg(d.b_true))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
289 # save(File(format"PNG", fn("data", "png")), grayimg(d.b_noisy))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
290 save(File(format"PNG", fn("reco", "png")), grayimg(x))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
291 if !isnothing(vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
292 open(fn("movement", "txt"), "w") do io
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
293 writedlm(io, ["est_y" "est_x"])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
294 writedlm(io, vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
295 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
296 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
297 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
299 stn = @set stn.wasted_time += (secs_ns() - verb_start)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
300
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
301 return stn
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
302 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
303
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
304 hifientry = LogEntryHiFi(iter, sc*d.v_cumul_true[1], sc*d.v_cumul_true[2])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
305 st = @set st.log_hifi=LinkedListEntry(hifientry, st.log_hifi)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
306
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
307 return st
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
308 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
309 d=dnext
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
310 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
311 catch ex
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
312 if params.handle_interrupt && isa(ex, InterruptException)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
313 # If SIGINT is received (user pressed ^C), terminate computations,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
314 # returning current status. Effectively, we do not call `step()` again,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
315 # ending the iterations, but letting the algorithm finish up.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
316 # Assuming (**) above occurs atomically, `st.log` should be valid, but
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
317 # any results returned by the algorithm itself may be partial, as for
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
318 # reasons of efficiency we do *not* store results of an iteration until
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
319 # the next iteration is finished.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
320 printstyled("\rUser interrupt—finishing up.\n", bold=true, color=202)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
321 st = @set st.aborted = true
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
322 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
323 rethrow(ex)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
324 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
325 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
326
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
327 return st
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
328 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
329
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
330 function bg_visualise_enhanced(rc; fullscreen=false)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 process_channel(rc) do d
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332 imgs, plot_movement, log, vhist = d
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
333 do_visualise(imgs, refresh=false, fullscreen=fullscreen)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
334 # Overlay movement
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
335 GR.settextcolorind(5)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
336 GR.setcharheight(0.015)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
337 GR.settextpath(GR.TEXT_PATH_RIGHT)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
338 tx, ty = GR.wctondc(0, 1)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
339 GR.text(tx, ty, @sprintf "FPS %.1f, SSIM %.2f, PSNR %.1f" (log.value.iter/log.value.time) log.value.ssim log.value.psnr)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
340 if plot_movement
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
341 sc=1.0
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
342 p=unfold_linked_list(log)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
343 x=map(e -> 1.5+sc*e.v_cumul_true_x, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
344 y=map(e -> 0.5+sc*e.v_cumul_true_y, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
345 GR.setlinewidth(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
346 GR.setlinecolorind(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
347 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
348 x=map(e -> 1.5+sc*e.v_cumul_est_x, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
349 y=map(e -> 0.5+sc*e.v_cumul_est_y, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
350 GR.setlinecolorind(3)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
351 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352 if vhist != nothing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353 GR.setlinecolorind(4)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
354 x=map(v -> 1.5+sc*v, vhist[:,2])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
355 y=map(v -> 0.5+sc*v, vhist[:,1])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
356 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
357 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
358 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
359 GR.updatews()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
360 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
361 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
362
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
363 end # module

mercurial