src/Run.jl

Thu, 25 Apr 2024 14:25:27 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 25 Apr 2024 14:25:27 -0500
changeset 40
2464329e356d
parent 36
e4a8f662a1ac
child 43
b1e4b1d2ef56
permissions
-rw-r--r--

README update

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
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
97 # return "$(ig.name)_$(e.mod.identifier)_$(@sprintf "%x" hash(p))"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
98 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
99 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
100
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
101 function write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 open(texfile, "w") do io
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
103 wp = (n, v) -> println(io, "\\def\\EXPPARAM$(n){$(v)}")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
104 wf = (n, s) -> if isdefined(e_params, s)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 wp(n, getfield(e_params, s))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
106 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
107 wf("alpha", :α)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
108 wf("sigmazero", :σ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 wf("tauzero", :τ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110 wf("tildetauzero", :τ̃₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
111 wf("delta", :δ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
112 wf("lambda", :λ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
113 wf("theta", :θ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
114 wf("maxiter", :maxiter)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
115 wf("noiselevel", :noise_level)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
116 wf("shakenoiselevel", :shake_noise_level)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
117 wf("shake", :shake)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 wf("timestep", :timestep)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 wf("displacementcount", :displacementcount)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 wf("phantomrho", :phantom_ρ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 if isdefined(e_params, :σ₀)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
122 wp("sigma", (e_params.σ₀ == 1 ? "" : "$(e_params.σ₀)") * "\\sigma_{\\max}")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
123 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
124 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 function run_experiments(;visualise=true,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
128 visfn = iterate_visualise,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 datatype = OnlineData,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 recalculate=true,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
131 experiments,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 save_prefix="",
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
133 fullscreen=false,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
134 kwargs...)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
135
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
136 # Create visualisation
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
137 if visualise
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138 rc = Channel(1)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139 visproc = Threads.@spawn bg_visualise_enhanced(rc, fullscreen=fullscreen)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 bind(rc, visproc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141 vis = rc
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143 vis = false
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 # Run all experiments
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
147 for e ∈ experiments
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 # Parameters for this experiment
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150 e_params = e.params ⬿ kwargs
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 ename = name(e, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 e_params = e_params ⬿ (save_prefix = save_prefix * ename,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 dynrange = e.imgen.dynrange,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 Λ = e.imgen.Λ)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 if recalculate || !isfile(e_params.save_prefix * ".txt")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 println("Running experiment \"$(ename)\"")
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 # Start data generation task
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 datachannel = Channel{datatype{e.DisplacementT}}(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 gentask = Threads.@spawn e.imgen.f(e.DisplacementT, datachannel, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 bind(datachannel, gentask)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 # Run algorithm
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 iterate = curry(visfn, datachannel,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 State(vis, nothing, 0.0, nothing, nothing, false))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 x, y, st = e.mod.solve(e.DisplacementT;
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 dim=e.imgen.dim,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170 iterate=iterate,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 params=e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 # Clear non-saveable things
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 st = @set st.vis = nothing
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 println("Wasted_time: $(st.wasted_time)s")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 if e_params.save_results
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 println("Saving " * e_params.save_prefix * "(.txt,_hifi.txt,_params.tex)")
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 perffile = e_params.save_prefix * ".txt"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 hififile = e_params.save_prefix * "_hifi.txt"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
183 texfile = e_params.save_prefix * "_params.tex"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
184 # datafile = e_params.save_prefix * ".jld2"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
186 write_log(perffile, st.log, "# params = $(e_params)\n")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
187 #write_log(hififile, st.log_hifi, "# params = $(e_params)\n")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 #write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
189 # @save datafile x y st params
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
190 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 close(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
193 wait(gentask)
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 if st.aborted
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 break
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 println("Skipping already computed experiment \"$(ename)\"")
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 # texfile = e_params.save_prefix * "_params.tex"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 # write_tex(texfile, e_params)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
203 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
204
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 if visualise
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 # Tell subprocess to finish, and wait
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207 put!(rc, nothing)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 close(rc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 wait(visproc)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
212 return
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
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
216 ######################################################
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 # Iterator that does visualisation and log collection
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 function iterate_visualise(datachannel::Channel{OnlineData{DisplacementT}},
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 st :: State,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 step :: Function,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
223 params :: NamedTuple) where DisplacementT
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
224 try
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 sc = nothing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
227 d = take!(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
228
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 for iter=1:params.maxiter
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 dnext = take!(datachannel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 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
232 stn = st
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 if isnothing(stn.start_time)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 # The Julia precompiler is a miserable joke, apparently not crossing module
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236 # boundaries, so only start timing after the first iteration.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 stn = @set stn.start_time=secs_ns()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
239
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240 verb = params.verbose_iter!=0 && mod(iter, params.verbose_iter) == 0
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
241
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
242 # Normalise movement to image dimensions so
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
243 # our TikZ plotting code doesn't need to know
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
244 # the image pixel size.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 sc = 1.0./maximum(size(d.b_true))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
246
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
247 if verb || iter ≤ 20 || (iter ≤ 200 && mod(iter, 10) == 0)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
248 verb_start = secs_ns()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
249 tm = verb_start - stn.start_time - stn.wasted_time
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
250 value, x, v, vhist = calc_objective()
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
251
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
252 entry = LogEntry(iter, tm, value,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
253 #sc*d.v_cumul_true[1],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254 #sc*d.v_cumul_true[2],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255 #sc*v[1], sc*v[2],
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
256 assess_psnr(x, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
257 assess_ssim(x, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
258 #assess_psnr(d.b_noisy, d.b_true),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
259 #assess_ssim(d.b_noisy, d.b_true)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
260 )
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
261
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
262 # (**) Collect a singly-linked list of log to avoid array resizing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
263 # while iterating
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
264 stn = @set stn.log=LinkedListEntry(entry, stn.log)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
265
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
266 if !isnothing(vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
267 vhist=vhist.*sc
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
268 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
269
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 if verb
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
271 @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
272 iter, params.maxiter, value, entry.psnr,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
273 entry.ssim, entry.iter/entry.time)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
274 if isa(stn.vis, Channel)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
275 put_onlylatest!(stn.vis, ((d.b_noisy, x),
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 params.plot_movement,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
277 stn.log, vhist))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
278
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
279 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
280 end
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 if params.save_images && (!haskey(params, :save_images_iters)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
283 || iter ∈ params.save_images_iters)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
284 fn = (t, ext) -> "$(params.save_prefix)_$(t)_frame$(iter).$(ext)"
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
285 # save(File(format"PNG", fn("true", "png")), grayimg(d.b_true))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
286 # save(File(format"PNG", fn("data", "png")), grayimg(d.b_noisy))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
287 save(File(format"PNG", fn("reco", "png")), grayimg(x))
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
288 if !isnothing(vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
289 open(fn("movement", "txt"), "w") do io
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
290 writedlm(io, ["est_y" "est_x"])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
291 writedlm(io, vhist)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
292 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
293 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
294 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
295
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
296 stn = @set stn.wasted_time += (secs_ns() - verb_start)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
297
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298 return stn
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
299 end
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 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
302 st = @set st.log_hifi=LinkedListEntry(hifientry, st.log_hifi)
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 return st
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
305 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
306 d=dnext
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
307 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
308 catch ex
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
309 if params.handle_interrupt && isa(ex, InterruptException)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
310 # If SIGINT is received (user pressed ^C), terminate computations,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
311 # returning current status. Effectively, we do not call `step()` again,
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
312 # ending the iterations, but letting the algorithm finish up.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
313 # Assuming (**) above occurs atomically, `st.log` should be valid, but
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
314 # any results returned by the algorithm itself may be partial, as for
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
315 # reasons of efficiency we do *not* store results of an iteration until
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
316 # the next iteration is finished.
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
317 printstyled("\rUser interrupt—finishing up.\n", bold=true, color=202)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
318 st = @set st.aborted = true
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
319 else
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
320 rethrow(ex)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
321 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
322 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
323
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
324 return st
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 function bg_visualise_enhanced(rc; fullscreen=false)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
328 process_channel(rc) do d
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
329 imgs, plot_movement, log, vhist = d
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
330 do_visualise(imgs, refresh=false, fullscreen=fullscreen)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
331 # Overlay movement
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
332 GR.settextcolorind(5)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
333 GR.setcharheight(0.015)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
334 GR.settextpath(GR.TEXT_PATH_RIGHT)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
335 tx, ty = GR.wctondc(0, 1)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
336 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
337 if plot_movement
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
338 sc=1.0
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
339 p=unfold_linked_list(log)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
340 x=map(e -> 1.5+sc*e.v_cumul_true_x, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
341 y=map(e -> 0.5+sc*e.v_cumul_true_y, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
342 GR.setlinewidth(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
343 GR.setlinecolorind(2)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
344 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
345 x=map(e -> 1.5+sc*e.v_cumul_est_x, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
346 y=map(e -> 0.5+sc*e.v_cumul_est_y, p)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
347 GR.setlinecolorind(3)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
348 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
349 if vhist != nothing
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
350 GR.setlinecolorind(4)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
351 x=map(v -> 1.5+sc*v, vhist[:,2])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352 y=map(v -> 0.5+sc*v, vhist[:,1])
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353 GR.polyline(x, y)
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
354 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
355 end
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
356 GR.updatews()
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
e4a8f662a1ac Reduce code duplication.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
360 end # module

mercurial