Thu, 23 Jan 2025 23:35:28 +0100
Generic proximal penalty support
0 | 1 | /*! |
2 | This module provides [`RunnableExperiment`] for running chosen algorithms on a chosen experiment. | |
3 | */ | |
4 | ||
5 | use numeric_literals::replace_float_literals; | |
6 | use colored::Colorize; | |
7 | use serde::{Serialize, Deserialize}; | |
8 | use serde_json; | |
9 | use nalgebra::base::DVector; | |
10 | use std::hash::Hash; | |
11 | use chrono::{DateTime, Utc}; | |
12 | use cpu_time::ProcessTime; | |
13 | use clap::ValueEnum; | |
14 | use std::collections::HashMap; | |
15 | use std::time::Instant; | |
16 | ||
17 | use rand::prelude::{ | |
18 | StdRng, | |
19 | SeedableRng | |
20 | }; | |
21 | use rand_distr::Distribution; | |
22 | ||
23 | use alg_tools::bisection_tree::*; | |
24 | use alg_tools::iterate::{ | |
25 | Timed, | |
26 | AlgIteratorOptions, | |
27 | Verbose, | |
28 | AlgIteratorFactory, | |
35 | 29 | LoggingIteratorFactory, |
30 | TimingIteratorFactory, | |
31 | BasicAlgIteratorFactory, | |
0 | 32 | }; |
33 | use alg_tools::logger::Logger; | |
35 | 34 | use alg_tools::error::{ |
35 | DynError, | |
36 | DynResult, | |
37 | }; | |
0 | 38 | use alg_tools::tabledump::TableDump; |
39 | use alg_tools::sets::Cube; | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
40 | use alg_tools::mapping::{ |
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
41 | RealMapping, |
35 | 42 | DifferentiableMapping, |
43 | DifferentiableRealMapping, | |
44 | Instance | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
45 | }; |
0 | 46 | use alg_tools::nalgebra_support::ToNalgebraRealField; |
47 | use alg_tools::euclidean::Euclidean; | |
35 | 48 | use alg_tools::lingrid::{lingrid, LinSpace}; |
0 | 49 | use alg_tools::sets::SetOrd; |
35 | 50 | use alg_tools::linops::{RowOp, IdOp /*, ZeroOp*/}; |
51 | use alg_tools::discrete_gradient::{Grad, ForwardNeumann}; | |
52 | use alg_tools::convex::Zero; | |
53 | use alg_tools::maputil::map3; | |
54 | use alg_tools::direct_product::Pair; | |
0 | 55 | |
56 | use crate::kernels::*; | |
57 | use crate::types::*; | |
58 | use crate::measures::*; | |
59 | use crate::measures::merging::SpikeMerging; | |
60 | use crate::forward_model::*; | |
35 | 61 | use crate::forward_model::sensor_grid::{ |
62 | SensorGrid, | |
63 | SensorGridBT, | |
64 | //SensorGridBTFN, | |
65 | Sensor, | |
66 | Spread, | |
67 | }; | |
68 | ||
0 | 69 | use crate::fb::{ |
70 | FBConfig, | |
32 | 71 | FBGenericConfig, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
72 | pointsource_fb_reg, |
32 | 73 | pointsource_fista_reg, |
74 | }; | |
75 | use crate::sliding_fb::{ | |
76 | SlidingFBConfig, | |
35 | 77 | TransportConfig, |
32 | 78 | pointsource_sliding_fb_reg |
0 | 79 | }; |
35 | 80 | use crate::sliding_pdps::{ |
81 | SlidingPDPSConfig, | |
82 | pointsource_sliding_pdps_pair | |
83 | }; | |
84 | use crate::forward_pdps::{ | |
85 | ForwardPDPSConfig, | |
86 | pointsource_forward_pdps_pair | |
87 | }; | |
0 | 88 | use crate::pdps::{ |
89 | PDPSConfig, | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
90 | pointsource_pdps_reg, |
0 | 91 | }; |
92 | use crate::frank_wolfe::{ | |
93 | FWConfig, | |
94 | FWVariant, | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
95 | pointsource_fw_reg, |
35 | 96 | //WeightOptim, |
0 | 97 | }; |
35 | 98 | //use crate::subproblem::InnerSettings; |
0 | 99 | use crate::seminorms::*; |
100 | use crate::plot::*; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
101 | use crate::{AlgorithmOverrides, CommandLineArgs}; |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
102 | use crate::tolerance::Tolerance; |
32 | 103 | use crate::regularisation::{ |
104 | Regularisation, | |
105 | RadonRegTerm, | |
106 | NonnegRadonRegTerm | |
107 | }; | |
108 | use crate::dataterm::{ | |
109 | L1, | |
35 | 110 | L2Squared, |
32 | 111 | }; |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
112 | use crate::prox_penalty::{ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
113 | RadonSquared, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
114 | //ProxPenalty, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
115 | }; |
35 | 116 | use alg_tools::norms::{L2, NormExponent}; |
117 | use alg_tools::operator_arithmetic::Weighted; | |
118 | use anyhow::anyhow; | |
0 | 119 | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
120 | /// Available proximal terms |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
121 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
122 | pub enum ProxTerm { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
123 | /// Partial-to-wave operator 𝒟. |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
124 | Wave, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
125 | /// Radon-norm squared |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
126 | RadonSquared |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
127 | } |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
128 | |
0 | 129 | /// Available algorithms and their configurations |
130 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |
131 | pub enum AlgorithmConfig<F : Float> { | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
132 | FB(FBConfig<F>, ProxTerm), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
133 | FISTA(FBConfig<F>, ProxTerm), |
0 | 134 | FW(FWConfig<F>), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
135 | PDPS(PDPSConfig<F>, ProxTerm), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
136 | SlidingFB(SlidingFBConfig<F>, ProxTerm), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
137 | ForwardPDPS(ForwardPDPSConfig<F>, ProxTerm), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
138 | SlidingPDPS(SlidingPDPSConfig<F>, ProxTerm), |
0 | 139 | } |
140 | ||
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
141 | fn unpack_tolerance<F : Float>(v : &Vec<F>) -> Tolerance<F> { |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
142 | assert!(v.len() == 3); |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
143 | Tolerance::Power { initial : v[0], factor : v[1], exponent : v[2] } |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
144 | } |
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
145 | |
0 | 146 | impl<F : ClapFloat> AlgorithmConfig<F> { |
147 | /// Override supported parameters based on the command line. | |
148 | pub fn cli_override(self, cli : &AlgorithmOverrides<F>) -> Self { | |
149 | let override_fb_generic = |g : FBGenericConfig<F>| { | |
150 | FBGenericConfig { | |
151 | bootstrap_insertions : cli.bootstrap_insertions | |
152 | .as_ref() | |
153 | .map_or(g.bootstrap_insertions, | |
154 | |n| Some((n[0], n[1]))), | |
155 | merge_every : cli.merge_every.unwrap_or(g.merge_every), | |
156 | merging : cli.merging.clone().unwrap_or(g.merging), | |
157 | final_merging : cli.final_merging.clone().unwrap_or(g.final_merging), | |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
158 | tolerance: cli.tolerance.as_ref().map(unpack_tolerance).unwrap_or(g.tolerance), |
0 | 159 | .. g |
160 | } | |
161 | }; | |
35 | 162 | let override_transport = |g : TransportConfig<F>| { |
163 | TransportConfig { | |
164 | θ0 : cli.theta0.unwrap_or(g.θ0), | |
165 | tolerance_ω: cli.transport_tolerance_omega.unwrap_or(g.tolerance_ω), | |
166 | tolerance_dv: cli.transport_tolerance_dv.unwrap_or(g.tolerance_dv), | |
167 | adaptation: cli.transport_adaptation.unwrap_or(g.adaptation), | |
168 | .. g | |
169 | } | |
170 | }; | |
0 | 171 | |
172 | use AlgorithmConfig::*; | |
173 | match self { | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
174 | FB(fb, prox) => FB(FBConfig { |
0 | 175 | τ0 : cli.tau0.unwrap_or(fb.τ0), |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
176 | generic : override_fb_generic(fb.generic), |
0 | 177 | .. fb |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
178 | }, prox), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
179 | FISTA(fb, prox) => FISTA(FBConfig { |
32 | 180 | τ0 : cli.tau0.unwrap_or(fb.τ0), |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
181 | generic : override_fb_generic(fb.generic), |
32 | 182 | .. fb |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
183 | }, prox), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
184 | PDPS(pdps, prox) => PDPS(PDPSConfig { |
0 | 185 | τ0 : cli.tau0.unwrap_or(pdps.τ0), |
186 | σ0 : cli.sigma0.unwrap_or(pdps.σ0), | |
187 | acceleration : cli.acceleration.unwrap_or(pdps.acceleration), | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
188 | generic : override_fb_generic(pdps.generic), |
0 | 189 | .. pdps |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
190 | }, prox), |
0 | 191 | FW(fw) => FW(FWConfig { |
192 | merging : cli.merging.clone().unwrap_or(fw.merging), | |
20
90f77ad9a98d
Added command line option for (power) tolerance
Tuomo Valkonen <tuomov@iki.fi>
parents:
9
diff
changeset
|
193 | tolerance : cli.tolerance.as_ref().map(unpack_tolerance).unwrap_or(fw.tolerance), |
0 | 194 | .. fw |
32 | 195 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
196 | SlidingFB(sfb, prox) => SlidingFB(SlidingFBConfig { |
32 | 197 | τ0 : cli.tau0.unwrap_or(sfb.τ0), |
35 | 198 | transport : override_transport(sfb.transport), |
32 | 199 | insertion : override_fb_generic(sfb.insertion), |
200 | .. sfb | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
201 | }, prox), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
202 | SlidingPDPS(spdps, prox) => SlidingPDPS(SlidingPDPSConfig { |
35 | 203 | τ0 : cli.tau0.unwrap_or(spdps.τ0), |
204 | σp0 : cli.sigmap0.unwrap_or(spdps.σp0), | |
205 | σd0 : cli.sigma0.unwrap_or(spdps.σd0), | |
206 | //acceleration : cli.acceleration.unwrap_or(pdps.acceleration), | |
207 | transport : override_transport(spdps.transport), | |
208 | insertion : override_fb_generic(spdps.insertion), | |
209 | .. spdps | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
210 | }, prox), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
211 | ForwardPDPS(fpdps, prox) => ForwardPDPS(ForwardPDPSConfig { |
35 | 212 | τ0 : cli.tau0.unwrap_or(fpdps.τ0), |
213 | σp0 : cli.sigmap0.unwrap_or(fpdps.σp0), | |
214 | σd0 : cli.sigma0.unwrap_or(fpdps.σd0), | |
215 | //acceleration : cli.acceleration.unwrap_or(pdps.acceleration), | |
216 | insertion : override_fb_generic(fpdps.insertion), | |
217 | .. fpdps | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
218 | }, prox), |
0 | 219 | } |
220 | } | |
221 | } | |
222 | ||
35 | 223 | /// Helper struct for tagging and [`AlgorithmConfig`] or [`ExperimentV2`] with a name. |
0 | 224 | #[derive(Clone, Debug, Serialize, Deserialize)] |
225 | pub struct Named<Data> { | |
226 | pub name : String, | |
227 | #[serde(flatten)] | |
228 | pub data : Data, | |
229 | } | |
230 | ||
231 | /// Shorthand algorithm configurations, to be used with the command line parser | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
232 | #[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] |
0 | 233 | pub enum DefaultAlgorithm { |
234 | /// The μFB forward-backward method | |
235 | #[clap(name = "fb")] | |
236 | FB, | |
237 | /// The μFISTA inertial forward-backward method | |
238 | #[clap(name = "fista")] | |
239 | FISTA, | |
240 | /// The “fully corrective” conditional gradient method | |
241 | #[clap(name = "fw")] | |
242 | FW, | |
243 | /// The “relaxed conditional gradient method | |
244 | #[clap(name = "fwrelax")] | |
245 | FWRelax, | |
246 | /// The μPDPS primal-dual proximal splitting method | |
247 | #[clap(name = "pdps")] | |
248 | PDPS, | |
35 | 249 | /// The sliding FB method |
32 | 250 | #[clap(name = "sliding_fb", alias = "sfb")] |
251 | SlidingFB, | |
35 | 252 | /// The sliding PDPS method |
253 | #[clap(name = "sliding_pdps", alias = "spdps")] | |
254 | SlidingPDPS, | |
255 | /// The PDPS method with a forward step for the smooth function | |
256 | #[clap(name = "forward_pdps", alias = "fpdps")] | |
257 | ForwardPDPS, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
258 | |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
259 | // Radon variants |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
260 | |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
261 | /// The μFB forward-backward method with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
262 | #[clap(name = "radon_fb")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
263 | RadonFB, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
264 | /// The μFISTA inertial forward-backward method with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
265 | #[clap(name = "radon_fista")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
266 | RadonFISTA, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
267 | /// The μPDPS primal-dual proximal splitting method with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
268 | #[clap(name = "radon_pdps")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
269 | RadonPDPS, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
270 | /// The sliding FB method with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
271 | #[clap(name = "radon_sliding_fb", alias = "radon_sfb")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
272 | RadonSlidingFB, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
273 | /// The sliding PDPS method with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
274 | #[clap(name = "radon_sliding_pdps", alias = "radon_spdps")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
275 | RadonSlidingPDPS, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
276 | /// The PDPS method with a forward step for the smooth function with radon-norm squared proximal term |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
277 | #[clap(name = "radon_forward_pdps", alias = "radon_fpdps")] |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
278 | RadonForwardPDPS, |
0 | 279 | } |
280 | ||
281 | impl DefaultAlgorithm { | |
282 | /// Returns the algorithm configuration corresponding to the algorithm shorthand | |
283 | pub fn default_config<F : Float>(&self) -> AlgorithmConfig<F> { | |
284 | use DefaultAlgorithm::*; | |
285 | match *self { | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
286 | FB => AlgorithmConfig::FB(Default::default(), ProxTerm::Wave), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
287 | FISTA => AlgorithmConfig::FISTA(Default::default(), ProxTerm::Wave), |
0 | 288 | FW => AlgorithmConfig::FW(Default::default()), |
289 | FWRelax => AlgorithmConfig::FW(FWConfig{ | |
290 | variant : FWVariant::Relaxed, | |
291 | .. Default::default() | |
292 | }), | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
293 | PDPS => AlgorithmConfig::PDPS(Default::default(), ProxTerm::Wave), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
294 | SlidingFB => AlgorithmConfig::SlidingFB(Default::default(), ProxTerm::Wave), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
295 | SlidingPDPS => AlgorithmConfig::SlidingPDPS(Default::default(), ProxTerm::Wave), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
296 | ForwardPDPS => AlgorithmConfig::ForwardPDPS(Default::default(), ProxTerm::Wave), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
297 | |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
298 | // Radon variants |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
299 | |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
300 | RadonFB => AlgorithmConfig::FB(Default::default(), ProxTerm::RadonSquared), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
301 | RadonFISTA => AlgorithmConfig::FISTA(Default::default(), ProxTerm::RadonSquared), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
302 | RadonPDPS => AlgorithmConfig::PDPS(Default::default(), ProxTerm::RadonSquared), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
303 | RadonSlidingFB => AlgorithmConfig::SlidingFB(Default::default(), ProxTerm::RadonSquared), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
304 | RadonSlidingPDPS => AlgorithmConfig::SlidingPDPS(Default::default(), ProxTerm::RadonSquared), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
305 | RadonForwardPDPS => AlgorithmConfig::ForwardPDPS(Default::default(), ProxTerm::RadonSquared), |
0 | 306 | } |
307 | } | |
308 | ||
309 | /// Returns the [`Named`] algorithm corresponding to the algorithm shorthand | |
310 | pub fn get_named<F : Float>(&self) -> Named<AlgorithmConfig<F>> { | |
311 | self.to_named(self.default_config()) | |
312 | } | |
313 | ||
314 | pub fn to_named<F : Float>(self, alg : AlgorithmConfig<F>) -> Named<AlgorithmConfig<F>> { | |
315 | let name = self.to_possible_value().unwrap().get_name().to_string(); | |
316 | Named{ name , data : alg } | |
317 | } | |
318 | } | |
319 | ||
320 | ||
321 | // // Floats cannot be hashed directly, so just hash the debug formatting | |
322 | // // for use as file identifier. | |
323 | // impl<F : Float> Hash for AlgorithmConfig<F> { | |
324 | // fn hash<H: Hasher>(&self, state: &mut H) { | |
325 | // format!("{:?}", self).hash(state); | |
326 | // } | |
327 | // } | |
328 | ||
329 | /// Plotting level configuration | |
330 | #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, ValueEnum, Debug)] | |
331 | pub enum PlotLevel { | |
332 | /// Plot nothing | |
333 | #[clap(name = "none")] | |
334 | None, | |
335 | /// Plot problem data | |
336 | #[clap(name = "data")] | |
337 | Data, | |
338 | /// Plot iterationwise state | |
339 | #[clap(name = "iter")] | |
340 | Iter, | |
341 | } | |
342 | ||
343 | type DefaultBT<F, const N : usize> = BT< | |
344 | DynamicDepth, | |
345 | F, | |
346 | usize, | |
347 | Bounds<F>, | |
348 | N | |
349 | >; | |
350 | type DefaultSeminormOp<F, K, const N : usize> = ConvolutionOp<F, K, DefaultBT<F, N>, N>; | |
351 | type DefaultSG<F, Sensor, Spread, const N : usize> = SensorGrid::< | |
352 | F, | |
353 | Sensor, | |
354 | Spread, | |
355 | DefaultBT<F, N>, | |
356 | N | |
357 | >; | |
358 | ||
359 | /// This is a dirty workaround to rust-csv not supporting struct flattening etc. | |
360 | #[derive(Serialize)] | |
361 | struct CSVLog<F> { | |
362 | iter : usize, | |
363 | cpu_time : f64, | |
364 | value : F, | |
35 | 365 | relative_value : F, |
366 | //post_value : F, | |
0 | 367 | n_spikes : usize, |
368 | inner_iters : usize, | |
369 | merged : usize, | |
370 | pruned : usize, | |
371 | this_iters : usize, | |
372 | } | |
373 | ||
374 | /// Collected experiment statistics | |
375 | #[derive(Clone, Debug, Serialize)] | |
376 | struct ExperimentStats<F : Float> { | |
377 | /// Signal-to-noise ratio in decibels | |
378 | ssnr : F, | |
379 | /// Proportion of noise in the signal as a number in $[0, 1]$. | |
380 | noise_ratio : F, | |
381 | /// When the experiment was run (UTC) | |
382 | when : DateTime<Utc>, | |
383 | } | |
384 | ||
385 | #[replace_float_literals(F::cast_from(literal))] | |
386 | impl<F : Float> ExperimentStats<F> { | |
387 | /// Calculate [`ExperimentStats`] based on a noisy `signal` and the separated `noise` signal. | |
388 | fn new<E : Euclidean<F>>(signal : &E, noise : &E) -> Self { | |
389 | let s = signal.norm2_squared(); | |
390 | let n = noise.norm2_squared(); | |
391 | let noise_ratio = (n / s).sqrt(); | |
392 | let ssnr = 10.0 * (s / n).log10(); | |
393 | ExperimentStats { | |
394 | ssnr, | |
395 | noise_ratio, | |
396 | when : Utc::now(), | |
397 | } | |
398 | } | |
399 | } | |
400 | /// Collected algorithm statistics | |
401 | #[derive(Clone, Debug, Serialize)] | |
402 | struct AlgorithmStats<F : Float> { | |
403 | /// Overall CPU time spent | |
404 | cpu_time : F, | |
405 | /// Real time spent | |
406 | elapsed : F | |
407 | } | |
408 | ||
409 | ||
410 | /// A wrapper for [`serde_json::to_writer_pretty`] that takes a filename as input | |
411 | /// and outputs a [`DynError`]. | |
412 | fn write_json<T : Serialize>(filename : String, data : &T) -> DynError { | |
413 | serde_json::to_writer_pretty(std::fs::File::create(filename)?, data)?; | |
414 | Ok(()) | |
415 | } | |
416 | ||
417 | ||
418 | /// Struct for experiment configurations | |
419 | #[derive(Debug, Clone, Serialize)] | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
420 | pub struct ExperimentV2<F, NoiseDistr, S, K, P, const N : usize> |
0 | 421 | where F : Float, |
422 | [usize; N] : Serialize, | |
423 | NoiseDistr : Distribution<F>, | |
424 | S : Sensor<F, N>, | |
425 | P : Spread<F, N>, | |
426 | K : SimpleConvolutionKernel<F, N>, | |
427 | { | |
428 | /// Domain $Ω$. | |
429 | pub domain : Cube<F, N>, | |
430 | /// Number of sensors along each dimension | |
431 | pub sensor_count : [usize; N], | |
432 | /// Noise distribution | |
433 | pub noise_distr : NoiseDistr, | |
434 | /// Seed for random noise generation (for repeatable experiments) | |
435 | pub noise_seed : u64, | |
436 | /// Sensor $θ$; $θ * ψ$ forms the forward operator $𝒜$. | |
437 | pub sensor : S, | |
438 | /// Spread $ψ$; $θ * ψ$ forms the forward operator $𝒜$. | |
439 | pub spread : P, | |
440 | /// Kernel $ρ$ of $𝒟$. | |
441 | pub kernel : K, | |
442 | /// True point sources | |
35 | 443 | pub μ_hat : RNDM<F, N>, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
444 | /// Regularisation term and parameter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
445 | pub regularisation : Regularisation<F>, |
0 | 446 | /// For plotting : how wide should the kernels be plotted |
447 | pub kernel_plot_width : F, | |
448 | /// Data term | |
449 | pub dataterm : DataTerm, | |
450 | /// A map of default configurations for algorithms | |
451 | #[serde(skip)] | |
452 | pub algorithm_defaults : HashMap<DefaultAlgorithm, AlgorithmConfig<F>>, | |
453 | } | |
454 | ||
35 | 455 | #[derive(Debug, Clone, Serialize)] |
456 | pub struct ExperimentBiased<F, NoiseDistr, S, K, P, B, const N : usize> | |
457 | where F : Float, | |
458 | [usize; N] : Serialize, | |
459 | NoiseDistr : Distribution<F>, | |
460 | S : Sensor<F, N>, | |
461 | P : Spread<F, N>, | |
462 | K : SimpleConvolutionKernel<F, N>, | |
463 | B : Mapping<Loc<F, N>, Codomain = F> + Serialize + std::fmt::Debug, | |
464 | { | |
465 | /// Basic setup | |
466 | pub base : ExperimentV2<F, NoiseDistr, S, K, P, N>, | |
467 | /// Weight of TV term | |
468 | pub λ : F, | |
469 | /// Bias function | |
470 | pub bias : B, | |
471 | } | |
472 | ||
0 | 473 | /// Trait for runnable experiments |
474 | pub trait RunnableExperiment<F : ClapFloat> { | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
475 | /// Run all algorithms provided, or default algorithms if none provided, on the experiment. |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
476 | fn runall(&self, cli : &CommandLineArgs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
477 | algs : Option<Vec<Named<AlgorithmConfig<F>>>>) -> DynError; |
0 | 478 | |
479 | /// Return algorithm default config | |
35 | 480 | fn algorithm_defaults(&self, alg : DefaultAlgorithm) -> Option<AlgorithmConfig<F>>; |
481 | } | |
482 | ||
483 | /// Helper function to print experiment start message and save setup. | |
484 | /// Returns saving prefix. | |
485 | fn start_experiment<E, S>( | |
486 | experiment : &Named<E>, | |
487 | cli : &CommandLineArgs, | |
488 | stats : S, | |
489 | ) -> DynResult<String> | |
490 | where | |
491 | E : Serialize + std::fmt::Debug, | |
492 | S : Serialize, | |
493 | { | |
494 | let Named { name : experiment_name, data } = experiment; | |
495 | ||
496 | println!("{}\n{}", | |
497 | format!("Performing experiment {}…", experiment_name).cyan(), | |
498 | format!("{:?}", data).bright_black()); | |
499 | ||
500 | // Set up output directory | |
501 | let prefix = format!("{}/{}/", cli.outdir, experiment_name); | |
502 | ||
503 | // Save experiment configuration and statistics | |
504 | let mkname_e = |t| format!("{prefix}{t}.json", prefix = prefix, t = t); | |
505 | std::fs::create_dir_all(&prefix)?; | |
506 | write_json(mkname_e("experiment"), experiment)?; | |
507 | write_json(mkname_e("config"), cli)?; | |
508 | write_json(mkname_e("stats"), &stats)?; | |
509 | ||
510 | Ok(prefix) | |
511 | } | |
512 | ||
513 | /// Error codes for running an algorithm on an experiment. | |
514 | enum RunError { | |
515 | /// Algorithm not implemented for this experiment | |
516 | NotImplemented, | |
0 | 517 | } |
518 | ||
35 | 519 | use RunError::*; |
520 | ||
521 | type DoRunAllIt<'a, F, const N : usize> = LoggingIteratorFactory< | |
522 | 'a, | |
523 | Timed<IterInfo<F, N>>, | |
524 | TimingIteratorFactory<BasicAlgIteratorFactory<IterInfo<F, N>>> | |
525 | >; | |
526 | ||
527 | /// Helper function to run all algorithms on an experiment. | |
528 | fn do_runall<F : Float, Z, const N : usize>( | |
529 | experiment_name : &String, | |
530 | prefix : &String, | |
531 | cli : &CommandLineArgs, | |
532 | algorithms : Vec<Named<AlgorithmConfig<F>>>, | |
533 | plotgrid : LinSpace<Loc<F, N>, [usize; N]>, | |
534 | mut save_extra : impl FnMut(String, Z) -> DynError, | |
535 | mut do_alg : impl FnMut( | |
536 | &AlgorithmConfig<F>, | |
537 | DoRunAllIt<F, N>, | |
538 | SeqPlotter<F, N>, | |
539 | String, | |
540 | ) -> Result<(RNDM<F, N>, Z), RunError>, | |
541 | ) -> DynError | |
542 | where | |
543 | PlotLookup : Plotting<N>, | |
544 | { | |
545 | let mut logs = Vec::new(); | |
546 | ||
547 | let iterator_options = AlgIteratorOptions{ | |
548 | max_iter : cli.max_iter, | |
549 | verbose_iter : cli.verbose_iter | |
550 | .map_or(Verbose::Logarithmic(10), | |
551 | |n| Verbose::Every(n)), | |
552 | quiet : cli.quiet, | |
553 | }; | |
554 | ||
555 | // Run the algorithm(s) | |
556 | for named @ Named { name : alg_name, data : alg } in algorithms.iter() { | |
557 | let this_prefix = format!("{}{}/", prefix, alg_name); | |
558 | ||
559 | // Create Logger and IteratorFactory | |
560 | let mut logger = Logger::new(); | |
561 | let iterator = iterator_options.instantiate() | |
562 | .timed() | |
563 | .into_log(&mut logger); | |
564 | ||
565 | let running = if !cli.quiet { | |
566 | format!("{}\n{}\n{}\n", | |
567 | format!("Running {} on experiment {}…", alg_name, experiment_name).cyan(), | |
568 | format!("{:?}", iterator_options).bright_black(), | |
569 | format!("{:?}", alg).bright_black()) | |
570 | } else { | |
571 | "".to_string() | |
572 | }; | |
573 | // | |
574 | // The following is for postprocessing, which has been disabled anyway. | |
575 | // | |
576 | // let reg : Box<dyn WeightOptim<_, _, _, N>> = match regularisation { | |
577 | // Regularisation::Radon(α) => Box::new(RadonRegTerm(α)), | |
578 | // Regularisation::NonnegRadon(α) => Box::new(NonnegRadonRegTerm(α)), | |
579 | // }; | |
580 | //let findim_data = reg.prepare_optimise_weights(&opA, &b); | |
581 | //let inner_config : InnerSettings<F> = Default::default(); | |
582 | //let inner_it = inner_config.iterator_options; | |
583 | ||
584 | // Create plotter and directory if needed. | |
585 | let plot_count = if cli.plot >= PlotLevel::Iter { 2000 } else { 0 }; | |
586 | let plotter = SeqPlotter::new(this_prefix, plot_count, plotgrid.clone()); | |
587 | ||
588 | let start = Instant::now(); | |
589 | let start_cpu = ProcessTime::now(); | |
590 | ||
591 | let (μ, z) = match do_alg(alg, iterator, plotter, running) { | |
592 | Ok(μ) => μ, | |
593 | Err(RunError::NotImplemented) => { | |
594 | let msg = format!("Algorithm “{alg_name}” not implemented for {experiment_name}. \ | |
595 | Skipping.").red(); | |
596 | eprintln!("{}", msg); | |
597 | continue | |
598 | } | |
599 | }; | |
600 | ||
601 | let elapsed = start.elapsed().as_secs_f64(); | |
602 | let cpu_time = start_cpu.elapsed().as_secs_f64(); | |
603 | ||
604 | println!("{}", format!("Elapsed {elapsed}s (CPU time {cpu_time}s)… ").yellow()); | |
605 | ||
606 | // Save results | |
607 | println!("{}", "Saving results …".green()); | |
608 | ||
609 | let mkname = |t| format!("{prefix}{alg_name}_{t}"); | |
610 | ||
611 | write_json(mkname("config.json"), &named)?; | |
612 | write_json(mkname("stats.json"), &AlgorithmStats { cpu_time, elapsed })?; | |
613 | μ.write_csv(mkname("reco.txt"))?; | |
614 | save_extra(mkname(""), z)?; | |
615 | //logger.write_csv(mkname("log.txt"))?; | |
616 | logs.push((mkname("log.txt"), logger)); | |
617 | } | |
618 | ||
619 | save_logs(logs) | |
620 | } | |
621 | ||
622 | #[replace_float_literals(F::cast_from(literal))] | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
623 | impl<F, NoiseDistr, S, K, P, /*PreadjointCodomain, */ const N : usize> RunnableExperiment<F> for |
35 | 624 | Named<ExperimentV2<F, NoiseDistr, S, K, P, N>> |
625 | where | |
626 | F : ClapFloat + nalgebra::RealField + ToNalgebraRealField<MixedType=F>, | |
627 | [usize; N] : Serialize, | |
628 | S : Sensor<F, N> + Copy + Serialize + std::fmt::Debug, | |
629 | P : Spread<F, N> + Copy + Serialize + std::fmt::Debug, | |
630 | Convolution<S, P>: Spread<F, N> + Bounded<F> + LocalAnalysis<F, Bounds<F>, N> + Copy | |
631 | // TODO: shold not have differentiability as a requirement, but | |
632 | // decide availability of sliding based on it. | |
633 | //+ for<'b> Differentiable<&'b Loc<F, N>, Output = Loc<F, N>>, | |
634 | // TODO: very weird that rust only compiles with Differentiable | |
635 | // instead of the above one on references, which is required by | |
636 | // poitsource_sliding_fb_reg. | |
637 | + DifferentiableRealMapping<F, N> | |
638 | + Lipschitz<L2, FloatType=F>, | |
639 | for<'b> <Convolution<S, P> as DifferentiableMapping<Loc<F,N>>>::Differential<'b> : Lipschitz<L2, FloatType=F>, // TODO: should not be required generally, only for sliding_fb. | |
640 | AutoConvolution<P> : BoundedBy<F, K>, | |
641 | K : SimpleConvolutionKernel<F, N> | |
642 | + LocalAnalysis<F, Bounds<F>, N> | |
643 | + Copy + Serialize + std::fmt::Debug, | |
644 | Cube<F, N>: P2Minimise<Loc<F, N>, F> + SetOrd, | |
645 | PlotLookup : Plotting<N>, | |
646 | DefaultBT<F, N> : SensorGridBT<F, S, P, N, Depth=DynamicDepth> + BTSearch<F, N>, | |
647 | BTNodeLookup: BTNode<F, usize, Bounds<F>, N>, | |
648 | RNDM<F, N> : SpikeMerging<F>, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
649 | NoiseDistr : Distribution<F> + Serialize + std::fmt::Debug, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
650 | // DefaultSG<F, S, P, N> : ForwardModel<RNDM<F, N>, F, PreadjointCodomain = PreadjointCodomain, Observable=DVector<F::MixedType>>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
651 | // PreadjointCodomain : Space + Bounded<F> + DifferentiableRealMapping<F, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
652 | // DefaultSeminormOp<F, K, N> : ProxPenalty<F, PreadjointCodomain, RadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
653 | // DefaultSeminormOp<F, K, N> : ProxPenalty<F, PreadjointCodomain, NonnegRadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
654 | // RadonSquared : ProxPenalty<F, PreadjointCodomain, RadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
655 | // RadonSquared : ProxPenalty<F, PreadjointCodomain, NonnegRadonRegTerm<F>, N>, |
35 | 656 | { |
0 | 657 | |
35 | 658 | fn algorithm_defaults(&self, alg : DefaultAlgorithm) -> Option<AlgorithmConfig<F>> { |
659 | self.data.algorithm_defaults.get(&alg).cloned() | |
0 | 660 | } |
661 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
662 | fn runall(&self, cli : &CommandLineArgs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
663 | algs : Option<Vec<Named<AlgorithmConfig<F>>>>) -> DynError { |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
664 | // Get experiment configuration |
0 | 665 | let &Named { |
666 | name : ref experiment_name, | |
35 | 667 | data : ExperimentV2 { |
0 | 668 | domain, sensor_count, ref noise_distr, sensor, spread, kernel, |
35 | 669 | ref μ_hat, regularisation, kernel_plot_width, dataterm, noise_seed, |
0 | 670 | .. |
671 | } | |
672 | } = self; | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
673 | |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
674 | // Set up algorithms |
35 | 675 | let algorithms = match (algs, dataterm) { |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
676 | (Some(algs), _) => algs, |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
677 | (None, DataTerm::L2Squared) => vec![DefaultAlgorithm::FB.get_named()], |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
678 | (None, DataTerm::L1) => vec![DefaultAlgorithm::PDPS.get_named()], |
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
679 | }; |
0 | 680 | |
681 | // Set up operators | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
682 | let depth = DynamicDepth(8); |
0 | 683 | let opA = DefaultSG::new(domain, sensor_count, sensor, spread, depth); |
684 | let op𝒟 = DefaultSeminormOp::new(depth, domain, kernel); | |
685 | ||
686 | // Set up random number generator. | |
687 | let mut rng = StdRng::seed_from_u64(noise_seed); | |
688 | ||
689 | // Generate the data and calculate SSNR statistic | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
690 | let b_hat = opA.apply(μ_hat); |
0 | 691 | let noise = DVector::from_distribution(b_hat.len(), &noise_distr, &mut rng); |
692 | let b = &b_hat + &noise; | |
693 | // Need to wrap calc_ssnr into a function to hide ultra-lame nalgebra::RealField | |
694 | // overloading log10 and conflicting with standard NumTraits one. | |
695 | let stats = ExperimentStats::new(&b, &noise); | |
696 | ||
35 | 697 | let prefix = start_experiment(&self, cli, stats)?; |
0 | 698 | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
699 | plotall(cli, &prefix, &domain, &sensor, &kernel, &spread, |
0 | 700 | &μ_hat, &op𝒟, &opA, &b_hat, &b, kernel_plot_width)?; |
701 | ||
35 | 702 | let plotgrid = lingrid(&domain, &[if N==1 { 1000 } else { 100 }; N]); |
703 | ||
704 | let save_extra = |_, ()| Ok(()); | |
0 | 705 | |
35 | 706 | do_runall(experiment_name, &prefix, cli, algorithms, plotgrid, save_extra, |
707 | |alg, iterator, plotter, running| | |
708 | { | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
709 | let μ = match alg { |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
710 | AlgorithmConfig::FB(ref algconfig, prox) => { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
711 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
712 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 713 | print!("{running}"); |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
714 | pointsource_fb_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
715 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
716 | iterator, plotter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
717 | ) |
35 | 718 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
719 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 720 | print!("{running}"); |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
721 | pointsource_fb_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
722 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
723 | iterator, plotter |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
724 | ) |
35 | 725 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
726 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
727 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
728 | pointsource_fb_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
729 | &opA, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
730 | iterator, plotter |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
731 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
732 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
733 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
734 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
735 | pointsource_fb_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
736 | &opA, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
737 | iterator, plotter |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
738 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
739 | }), |
35 | 740 | _ => Err(NotImplemented) |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
741 | } |
0 | 742 | }, |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
743 | AlgorithmConfig::FISTA(ref algconfig, prox) => { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
744 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
745 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 746 | print!("{running}"); |
32 | 747 | pointsource_fista_reg( |
748 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
749 | iterator, plotter | |
750 | ) | |
35 | 751 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
752 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 753 | print!("{running}"); |
32 | 754 | pointsource_fista_reg( |
755 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
756 | iterator, plotter | |
757 | ) | |
35 | 758 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
759 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
35 | 760 | print!("{running}"); |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
761 | pointsource_fista_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
762 | &opA, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
763 | iterator, plotter |
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
764 | ) |
35 | 765 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
766 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
35 | 767 | print!("{running}"); |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
768 | pointsource_fista_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
769 | &opA, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
770 | iterator, plotter |
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
771 | ) |
35 | 772 | }), |
773 | _ => Err(NotImplemented), | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
774 | } |
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
775 | }, |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
776 | AlgorithmConfig::SlidingFB(ref algconfig, prox) => { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
777 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
778 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 779 | print!("{running}"); |
32 | 780 | pointsource_sliding_fb_reg( |
781 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
782 | iterator, plotter | |
783 | ) | |
35 | 784 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
785 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 786 | print!("{running}"); |
32 | 787 | pointsource_sliding_fb_reg( |
788 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
789 | iterator, plotter | |
790 | ) | |
35 | 791 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
792 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
793 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
794 | pointsource_sliding_fb_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
795 | &opA, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
796 | iterator, plotter |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
797 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
798 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
799 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
800 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
801 | pointsource_sliding_fb_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
802 | &opA, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
803 | iterator, plotter |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
804 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
805 | }), |
35 | 806 | _ => Err(NotImplemented), |
32 | 807 | } |
808 | }, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
809 | AlgorithmConfig::PDPS(ref algconfig, prox) => { |
35 | 810 | print!("{running}"); |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
811 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
812 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
813 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
814 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
815 | iterator, plotter, L2Squared |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
816 | ) |
35 | 817 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
818 | (Regularisation::Radon(α),DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
819 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
820 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
821 | iterator, plotter, L2Squared |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
822 | ) |
35 | 823 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
824 | (Regularisation::NonnegRadon(α), DataTerm::L1, ProxTerm::Wave) => Ok({ |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
825 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
826 | &opA, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
827 | iterator, plotter, L1 |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
828 | ) |
35 | 829 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
830 | (Regularisation::Radon(α), DataTerm::L1, ProxTerm::Wave) => Ok({ |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
831 | pointsource_pdps_reg( |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
832 | &opA, &b, RadonRegTerm(α), &op𝒟, algconfig, |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
833 | iterator, plotter, L1 |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
834 | ) |
35 | 835 | }), |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
836 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
837 | pointsource_pdps_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
838 | &opA, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
839 | iterator, plotter, L2Squared |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
840 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
841 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
842 | (Regularisation::Radon(α),DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
843 | pointsource_pdps_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
844 | &opA, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
845 | iterator, plotter, L2Squared |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
846 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
847 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
848 | (Regularisation::NonnegRadon(α), DataTerm::L1, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
849 | pointsource_pdps_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
850 | &opA, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
851 | iterator, plotter, L1 |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
852 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
853 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
854 | (Regularisation::Radon(α), DataTerm::L1, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
855 | pointsource_pdps_reg( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
856 | &opA, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
857 | iterator, plotter, L1 |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
858 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
859 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
860 | // _ => Err(NotImplemented), |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
861 | } |
0 | 862 | }, |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
863 | AlgorithmConfig::FW(ref algconfig) => { |
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
864 | match (regularisation, dataterm) { |
35 | 865 | (Regularisation::Radon(α), DataTerm::L2Squared) => Ok({ |
866 | print!("{running}"); | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
867 | pointsource_fw_reg(&opA, &b, RadonRegTerm(α), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
868 | algconfig, iterator, plotter) |
35 | 869 | }), |
870 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared) => Ok({ | |
871 | print!("{running}"); | |
25
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
872 | pointsource_fw_reg(&opA, &b, NonnegRadonRegTerm(α), |
79943be70720
Implement non-negativity constraints for the conditional gradient methods
Tuomo Valkonen <tuomov@iki.fi>
parents:
24
diff
changeset
|
873 | algconfig, iterator, plotter) |
35 | 874 | }), |
875 | _ => Err(NotImplemented), | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
876 | } |
35 | 877 | }, |
878 | _ => Err(NotImplemented), | |
879 | }?; | |
880 | Ok((μ, ())) | |
881 | }) | |
0 | 882 | } |
883 | } | |
35 | 884 | |
885 | ||
886 | #[replace_float_literals(F::cast_from(literal))] | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
887 | impl<F, NoiseDistr, S, K, P, B, /*PreadjointCodomain,*/ const N : usize> RunnableExperiment<F> for |
35 | 888 | Named<ExperimentBiased<F, NoiseDistr, S, K, P, B, N>> |
889 | where | |
890 | F : ClapFloat + nalgebra::RealField + ToNalgebraRealField<MixedType=F>, | |
891 | [usize; N] : Serialize, | |
892 | S : Sensor<F, N> + Copy + Serialize + std::fmt::Debug, | |
893 | P : Spread<F, N> + Copy + Serialize + std::fmt::Debug, | |
894 | Convolution<S, P>: Spread<F, N> + Bounded<F> + LocalAnalysis<F, Bounds<F>, N> + Copy | |
895 | // TODO: shold not have differentiability as a requirement, but | |
896 | // decide availability of sliding based on it. | |
897 | //+ for<'b> Differentiable<&'b Loc<F, N>, Output = Loc<F, N>>, | |
898 | // TODO: very weird that rust only compiles with Differentiable | |
899 | // instead of the above one on references, which is required by | |
900 | // poitsource_sliding_fb_reg. | |
901 | + DifferentiableRealMapping<F, N> | |
902 | + Lipschitz<L2, FloatType=F>, | |
903 | for<'b> <Convolution<S, P> as DifferentiableMapping<Loc<F,N>>>::Differential<'b> : Lipschitz<L2, FloatType=F>, // TODO: should not be required generally, only for sliding_fb. | |
904 | AutoConvolution<P> : BoundedBy<F, K>, | |
905 | K : SimpleConvolutionKernel<F, N> | |
906 | + LocalAnalysis<F, Bounds<F>, N> | |
907 | + Copy + Serialize + std::fmt::Debug, | |
908 | Cube<F, N>: P2Minimise<Loc<F, N>, F> + SetOrd, | |
909 | PlotLookup : Plotting<N>, | |
910 | DefaultBT<F, N> : SensorGridBT<F, S, P, N, Depth=DynamicDepth> + BTSearch<F, N>, | |
911 | BTNodeLookup: BTNode<F, usize, Bounds<F>, N>, | |
912 | RNDM<F, N> : SpikeMerging<F>, | |
913 | NoiseDistr : Distribution<F> + Serialize + std::fmt::Debug, | |
914 | B : Mapping<Loc<F, N>, Codomain = F> + Serialize + std::fmt::Debug, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
915 | // DefaultSG<F, S, P, N> : ForwardModel<RNDM<F, N>, F, PreadjointCodomain = PreadjointCodomain, Observable=DVector<F::MixedType>>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
916 | // PreadjointCodomain : Bounded<F> + DifferentiableRealMapping<F, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
917 | // DefaultSeminormOp<F, K, N> : ProxPenalty<F, PreadjointCodomain, RadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
918 | // DefaultSeminormOp<F, K, N> : ProxPenalty<F, PreadjointCodomain, NonnegRadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
919 | // RadonSquared : ProxPenalty<F, PreadjointCodomain, RadonRegTerm<F>, N>, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
920 | // RadonSquared : ProxPenalty<F, PreadjointCodomain, NonnegRadonRegTerm<F>, N>, |
35 | 921 | { |
922 | ||
923 | fn algorithm_defaults(&self, alg : DefaultAlgorithm) -> Option<AlgorithmConfig<F>> { | |
924 | self.data.base.algorithm_defaults.get(&alg).cloned() | |
925 | } | |
926 | ||
927 | fn runall(&self, cli : &CommandLineArgs, | |
928 | algs : Option<Vec<Named<AlgorithmConfig<F>>>>) -> DynError { | |
929 | // Get experiment configuration | |
930 | let &Named { | |
931 | name : ref experiment_name, | |
932 | data : ExperimentBiased { | |
933 | λ, | |
934 | ref bias, | |
935 | base : ExperimentV2 { | |
936 | domain, sensor_count, ref noise_distr, sensor, spread, kernel, | |
937 | ref μ_hat, regularisation, kernel_plot_width, dataterm, noise_seed, | |
938 | .. | |
939 | } | |
940 | } | |
941 | } = self; | |
942 | ||
943 | // Set up algorithms | |
944 | let algorithms = match (algs, dataterm) { | |
945 | (Some(algs), _) => algs, | |
946 | _ => vec![DefaultAlgorithm::SlidingPDPS.get_named()], | |
947 | }; | |
948 | ||
949 | // Set up operators | |
950 | let depth = DynamicDepth(8); | |
951 | let opA = DefaultSG::new(domain, sensor_count, sensor, spread, depth); | |
952 | let op𝒟 = DefaultSeminormOp::new(depth, domain, kernel); | |
953 | let opAext = RowOp(opA.clone(), IdOp::new()); | |
954 | let fnR = Zero::new(); | |
955 | let h = map3(domain.span_start(), domain.span_end(), sensor_count, | |
956 | |a, b, n| (b-a)/F::cast_from(n)) | |
957 | .into_iter() | |
958 | .reduce(NumTraitsFloat::max) | |
959 | .unwrap(); | |
960 | let z = DVector::zeros(sensor_count.iter().product()); | |
961 | let opKz = Grad::new_for(&z, h, sensor_count, ForwardNeumann).unwrap(); | |
962 | let y = opKz.apply(&z); | |
963 | let fnH = Weighted{ base_fn : L1.as_mapping(), weight : λ}; // TODO: L_{2,1} | |
964 | // let zero_y = y.clone(); | |
965 | // let zeroBTFN = opA.preadjoint().apply(&zero_y); | |
966 | // let opKμ = ZeroOp::new(&zero_y, zeroBTFN); | |
967 | ||
968 | // Set up random number generator. | |
969 | let mut rng = StdRng::seed_from_u64(noise_seed); | |
970 | ||
971 | // Generate the data and calculate SSNR statistic | |
972 | let bias_vec = DVector::from_vec(opA.grid() | |
973 | .into_iter() | |
974 | .map(|v| bias.apply(v)) | |
975 | .collect::<Vec<F>>()); | |
976 | let b_hat : DVector<_> = opA.apply(μ_hat) + &bias_vec; | |
977 | let noise = DVector::from_distribution(b_hat.len(), &noise_distr, &mut rng); | |
978 | let b = &b_hat + &noise; | |
979 | // Need to wrap calc_ssnr into a function to hide ultra-lame nalgebra::RealField | |
980 | // overloading log10 and conflicting with standard NumTraits one. | |
981 | let stats = ExperimentStats::new(&b, &noise); | |
982 | ||
983 | let prefix = start_experiment(&self, cli, stats)?; | |
984 | ||
985 | plotall(cli, &prefix, &domain, &sensor, &kernel, &spread, | |
986 | &μ_hat, &op𝒟, &opA, &b_hat, &b, kernel_plot_width)?; | |
987 | ||
988 | opA.write_observable(&bias_vec, format!("{prefix}bias"))?; | |
989 | ||
990 | let plotgrid = lingrid(&domain, &[if N==1 { 1000 } else { 100 }; N]); | |
991 | ||
992 | let save_extra = |prefix, z| opA.write_observable(&z, format!("{prefix}z")); | |
24
d29d1fcf5423
Support arbitrary regularisation terms; implement non-positivity-constrained regularisation.
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
993 | |
35 | 994 | // Run the algorithms |
995 | do_runall(experiment_name, &prefix, cli, algorithms, plotgrid, save_extra, | |
996 | |alg, iterator, plotter, running| | |
997 | { | |
998 | let Pair(μ, z) = match alg { | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
999 | AlgorithmConfig::ForwardPDPS(ref algconfig, prox) => { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1000 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1001 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 1002 | print!("{running}"); |
1003 | pointsource_forward_pdps_pair( | |
1004 | &opAext, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
1005 | iterator, plotter, | |
1006 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), | |
1007 | ) | |
1008 | }), | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1009 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 1010 | print!("{running}"); |
1011 | pointsource_forward_pdps_pair( | |
1012 | &opAext, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
1013 | iterator, plotter, | |
1014 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), | |
1015 | ) | |
1016 | }), | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1017 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1018 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1019 | pointsource_forward_pdps_pair( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1020 | &opAext, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1021 | iterator, plotter, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1022 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1023 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1024 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1025 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1026 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1027 | pointsource_forward_pdps_pair( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1028 | &opAext, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1029 | iterator, plotter, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1030 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1031 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1032 | }), |
35 | 1033 | _ => Err(NotImplemented) |
1034 | } | |
1035 | }, | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1036 | AlgorithmConfig::SlidingPDPS(ref algconfig, prox) => { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1037 | match (regularisation, dataterm, prox) { |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1038 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 1039 | print!("{running}"); |
1040 | pointsource_sliding_pdps_pair( | |
1041 | &opAext, &b, NonnegRadonRegTerm(α), &op𝒟, algconfig, | |
1042 | iterator, plotter, | |
1043 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), | |
1044 | ) | |
1045 | }), | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1046 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::Wave) => Ok({ |
35 | 1047 | print!("{running}"); |
1048 | pointsource_sliding_pdps_pair( | |
1049 | &opAext, &b, RadonRegTerm(α), &op𝒟, algconfig, | |
1050 | iterator, plotter, | |
1051 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), | |
1052 | ) | |
1053 | }), | |
37
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1054 | (Regularisation::NonnegRadon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1055 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1056 | pointsource_sliding_pdps_pair( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1057 | &opAext, &b, NonnegRadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1058 | iterator, plotter, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1059 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1060 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1061 | }), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1062 | (Regularisation::Radon(α), DataTerm::L2Squared, ProxTerm::RadonSquared) => Ok({ |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1063 | print!("{running}"); |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1064 | pointsource_sliding_pdps_pair( |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1065 | &opAext, &b, RadonRegTerm(α), &RadonSquared, algconfig, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1066 | iterator, plotter, |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1067 | /* opKμ, */ &opKz, &fnR, &fnH, z.clone(), y.clone(), |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1068 | ) |
c5d8bd1a7728
Generic proximal penalty support
Tuomo Valkonen <tuomov@iki.fi>
parents:
35
diff
changeset
|
1069 | }), |
35 | 1070 | _ => Err(NotImplemented) |
1071 | } | |
1072 | }, | |
1073 | _ => Err(NotImplemented) | |
1074 | }?; | |
1075 | Ok((μ, z)) | |
1076 | }) | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | ||
1081 | /// Calculative minimum and maximum values of all the `logs`, and save them into | |
1082 | /// corresponding file names given as the first elements of the tuples in the vectors. | |
1083 | fn save_logs<F : Float, const N : usize>( | |
1084 | logs : Vec<(String, Logger<Timed<IterInfo<F, N>>>)> | |
1085 | ) -> DynError { | |
1086 | // Process logs for relative values | |
1087 | println!("{}", "Processing logs…"); | |
1088 | ||
1089 | ||
1090 | // Find minimum value and initial value within a single log | |
1091 | let proc_single_log = |log : &Logger<Timed<IterInfo<F, N>>>| { | |
1092 | let d = log.data(); | |
1093 | let mi = d.iter() | |
1094 | .map(|i| i.data.value) | |
1095 | .reduce(NumTraitsFloat::min); | |
1096 | d.first() | |
1097 | .map(|i| i.data.value) | |
1098 | .zip(mi) | |
1099 | }; | |
1100 | ||
1101 | // Find minimum and maximum value over all logs | |
1102 | let (v_ini, v_min) = logs.iter() | |
1103 | .filter_map(|&(_, ref log)| proc_single_log(log)) | |
1104 | .reduce(|(i1, m1), (i2, m2)| (i1.max(i2), m1.min(m2))) | |
1105 | .ok_or(anyhow!("No algorithms found"))?; | |
1106 | ||
1107 | let logmap = |Timed { cpu_time, iter, data }| { | |
1108 | let IterInfo { | |
1109 | value, | |
1110 | n_spikes, | |
1111 | inner_iters, | |
1112 | merged, | |
1113 | pruned, | |
1114 | //postprocessing, | |
1115 | this_iters, | |
1116 | .. | |
1117 | } = data; | |
1118 | // let post_value = match (postprocessing, dataterm) { | |
1119 | // (Some(mut μ), DataTerm::L2Squared) => { | |
1120 | // // Comparison postprocessing is only implemented for the case handled | |
1121 | // // by the FW variants. | |
1122 | // reg.optimise_weights( | |
1123 | // &mut μ, &opA, &b, &findim_data, &inner_config, | |
1124 | // inner_it | |
1125 | // ); | |
1126 | // dataterm.value_at_residual(opA.apply(&μ) - &b) | |
1127 | // + regularisation.apply(&μ) | |
1128 | // }, | |
1129 | // _ => value, | |
1130 | // }; | |
1131 | let relative_value = (value - v_min)/(v_ini - v_min); | |
1132 | CSVLog { | |
1133 | iter, | |
1134 | value, | |
1135 | relative_value, | |
1136 | //post_value, | |
1137 | n_spikes, | |
1138 | cpu_time : cpu_time.as_secs_f64(), | |
1139 | inner_iters, | |
1140 | merged, | |
1141 | pruned, | |
1142 | this_iters | |
1143 | } | |
1144 | }; | |
1145 | ||
1146 | println!("{}", "Saving logs …".green()); | |
1147 | ||
1148 | for (name, logger) in logs { | |
1149 | logger.map(logmap).write_csv(name)?; | |
1150 | } | |
1151 | ||
1152 | Ok(()) | |
1153 | } | |
1154 | ||
0 | 1155 | |
1156 | /// Plot experiment setup | |
1157 | #[replace_float_literals(F::cast_from(literal))] | |
1158 | fn plotall<F, Sensor, Kernel, Spread, 𝒟, A, const N : usize>( | |
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1159 | cli : &CommandLineArgs, |
0 | 1160 | prefix : &String, |
1161 | domain : &Cube<F, N>, | |
1162 | sensor : &Sensor, | |
1163 | kernel : &Kernel, | |
1164 | spread : &Spread, | |
35 | 1165 | μ_hat : &RNDM<F, N>, |
0 | 1166 | op𝒟 : &𝒟, |
1167 | opA : &A, | |
1168 | b_hat : &A::Observable, | |
1169 | b : &A::Observable, | |
1170 | kernel_plot_width : F, | |
1171 | ) -> DynError | |
1172 | where F : Float + ToNalgebraRealField, | |
1173 | Sensor : RealMapping<F, N> + Support<F, N> + Clone, | |
1174 | Spread : RealMapping<F, N> + Support<F, N> + Clone, | |
1175 | Kernel : RealMapping<F, N> + Support<F, N>, | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
1176 | Convolution<Sensor, Spread> : DifferentiableRealMapping<F, N> + Support<F, N>, |
0 | 1177 | 𝒟 : DiscreteMeasureOp<Loc<F, N>, F>, |
1178 | 𝒟::Codomain : RealMapping<F, N>, | |
35 | 1179 | A : ForwardModel<RNDM<F, N>, F>, |
1180 | for<'a> &'a A::Observable : Instance<A::Observable>, | |
34
efa60bc4f743
Radon FB + sliding improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
32
diff
changeset
|
1181 | A::PreadjointCodomain : DifferentiableRealMapping<F, N> + Bounded<F>, |
0 | 1182 | PlotLookup : Plotting<N>, |
1183 | Cube<F, N> : SetOrd { | |
1184 | ||
9
21b0e537ac0e
Command line parameter passing simplifications and make `-o` required.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1185 | if cli.plot < PlotLevel::Data { |
0 | 1186 | return Ok(()) |
1187 | } | |
1188 | ||
1189 | let base = Convolution(sensor.clone(), spread.clone()); | |
1190 | ||
1191 | let resolution = if N==1 { 100 } else { 40 }; | |
35 | 1192 | let pfx = |n| format!("{prefix}{n}"); |
0 | 1193 | let plotgrid = lingrid(&[[-kernel_plot_width, kernel_plot_width]; N].into(), &[resolution; N]); |
1194 | ||
35 | 1195 | PlotLookup::plot_into_file(sensor, plotgrid, pfx("sensor")); |
1196 | PlotLookup::plot_into_file(kernel, plotgrid, pfx("kernel")); | |
1197 | PlotLookup::plot_into_file(spread, plotgrid, pfx("spread")); | |
1198 | PlotLookup::plot_into_file(&base, plotgrid, pfx("base_sensor")); | |
0 | 1199 | |
1200 | let plotgrid2 = lingrid(&domain, &[resolution; N]); | |
1201 | ||
1202 | let ω_hat = op𝒟.apply(μ_hat); | |
1203 | let noise = opA.preadjoint().apply(opA.apply(μ_hat) - b); | |
35 | 1204 | PlotLookup::plot_into_file(&ω_hat, plotgrid2, pfx("omega_hat")); |
1205 | PlotLookup::plot_into_file(&noise, plotgrid2, pfx("omega_noise")); | |
0 | 1206 | |
1207 | let preadj_b = opA.preadjoint().apply(b); | |
1208 | let preadj_b_hat = opA.preadjoint().apply(b_hat); | |
1209 | //let bounds = preadj_b.bounds().common(&preadj_b_hat.bounds()); | |
1210 | PlotLookup::plot_into_file_spikes( | |
35 | 1211 | Some(&preadj_b), |
1212 | Some(&preadj_b_hat), | |
1213 | plotgrid2, | |
1214 | &μ_hat, | |
0 | 1215 | pfx("omega_b") |
1216 | ); | |
35 | 1217 | PlotLookup::plot_into_file(&preadj_b, plotgrid2, pfx("preadj_b")); |
1218 | PlotLookup::plot_into_file(&preadj_b_hat, plotgrid2, pfx("preadj_b_hat")); | |
0 | 1219 | |
1220 | // Save true solution and observables | |
1221 | μ_hat.write_csv(pfx("orig.txt"))?; | |
1222 | opA.write_observable(&b_hat, pfx("b_hat"))?; | |
1223 | opA.write_observable(&b, pfx("b_noisy")) | |
1224 | } |