1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/* Copyright 2019-2020. Martin Uecker.
* All rights reserved. Use of this source code is governed by
* a BSD-style license which can be found in the LICENSE file.
*
* Authors:
* 2017-2020 Martin Uecker <martin.uecker@med.uni-goettingen.de>
*/
#include <complex.h>
#include "num/multind.h"
#include "num/flpmath.h"
#include "num/ops_p.h"
#include "num/ops.h"
#include "misc/misc.h"
#include "misc/debug.h"
#include "misc/types.h"
#include "iter/italgos.h"
#include "iter/iter3.h"
#include "iter/iter4.h"
#include "iter/lsqr.h"
#include "iter/thresh.h"
#include "linops/someops.h"
#include "nlops/zexp.h"
#include "nlops/nlop.h"
#include "nlops/cast.h"
#include "utest.h"
static bool test_iter_irgnm0(bool v2, bool ref)
{
enum { N = 3 };
long dims[N] = { 10, 7, 3 };
complex float* dst1 = md_alloc(N, dims, CFL_SIZE);
complex float* src1 = md_alloc(N, dims, CFL_SIZE);
complex float* src2 = md_alloc(N, dims, CFL_SIZE);
md_zfill(N, dims, src1, 1.);
struct nlop_s* zexp = nlop_zexp_create(N, dims);
nlop_apply(zexp, N, dims, dst1, N, dims, src1);
md_zfill(N, dims, src2, 0.);
(v2 ? iter4_irgnm2 : iter4_irgnm)(CAST_UP(&iter3_irgnm_defaults), zexp,
2 * md_calc_size(N, dims), (float*)src2, ref ? (const float*)src1 : NULL,
2 * md_calc_size(N, dims), (const float*)dst1, NULL,
(struct iter_op_s){ NULL, NULL });
double err = md_znrmse(N, dims, src2, src1);
nlop_free(zexp);
md_free(src1);
md_free(dst1);
md_free(src2);
UT_ASSERT(err < (ref ? 1.E-7 : 0.01));
}
static bool test_iter_irgnm_lsqr0(bool ref)
{
enum { N = 3 };
long dims[N] = { 10, 7, 3 };
complex float* dst1 = md_alloc(N, dims, CFL_SIZE);
complex float* src1 = md_alloc(N, dims, CFL_SIZE);
complex float* src2 = md_alloc(N, dims, CFL_SIZE);
complex float* src3 = md_alloc(N, dims, CFL_SIZE);
md_zfill(N, dims, src1, 1.);
struct nlop_s* zexp = nlop_zexp_create(N, dims);
nlop_apply(zexp, N, dims, dst1, N, dims, src1);
md_zfill(N, dims, src2, 0.);
md_zfill(N, dims, src3, 0.);
const struct operator_p_s* lsqr = NULL;
iter4_irgnm2(CAST_UP(&iter3_irgnm_defaults), zexp,
2 * md_calc_size(N, dims), (float*)src2, ref ? (const float*)src1 : NULL,
2 * md_calc_size(N, dims), (const float*)dst1, lsqr,
(struct iter_op_s){ NULL, NULL });
struct iter_conjgrad_conf conf = iter_conjgrad_defaults;
conf.maxiter = 100;
conf.l2lambda = 1.;
conf.tol = 0.1;
auto der = linop_clone(&zexp->derivative[0][0]);
lsqr = lsqr2_create(&lsqr_defaults,
iter2_conjgrad, CAST_UP(&conf),
NULL, der, NULL,
0, NULL, NULL, NULL);
linop_free(der);
iter4_irgnm2(CAST_UP(&iter3_irgnm_defaults), zexp,
2 * md_calc_size(N, dims), (float*)src3, ref ? (const float*)src1 : NULL,
2 * md_calc_size(N, dims), (const float*)dst1, lsqr,
(struct iter_op_s){ NULL, NULL });
double err = md_znrmse(N, dims, src2, src3);
operator_p_free(lsqr);
nlop_free(zexp);
md_free(src1);
md_free(dst1);
md_free(src2);
md_free(src3);
UT_ASSERT(err < 1.E-10);
}
static bool test_iter_irgnm_lsqr1(bool ref, bool regu)
{
enum { N = 3 };
long dims[N] = { 10, 7, 3 };
long dims1[N] = { 10, 7, 1 };
long dims2[N] = { 10, 7, 2 };
complex float* dst1 = md_alloc(N, dims, CFL_SIZE);
complex float* src1 = md_alloc(N, dims, CFL_SIZE);
complex float* src2 = md_alloc(N, dims, CFL_SIZE);
md_zfill(N, dims, src1, 1.);
struct nlop_s* zexp = nlop_zexp_create(N, dims);
nlop_apply(zexp, N, dims, dst1, N, dims, src1);
md_zfill(N, dims, src2, 0.);
const struct operator_p_s* lsqr = NULL;
struct iter_admm_conf conf = iter_admm_defaults;
conf.rho = 1.E-5;
auto p1 = prox_thresh_create(3, dims1, 0.5, 0u);
auto p2 = prox_thresh_create(3, dims2, 0.5, 0u);
const struct operator_p_s* prox_ops[1] = { operator_p_stack(2, 2, p1, p2) };
operator_p_free(p1);
operator_p_free(p2);
auto der = linop_clone(&zexp->derivative[0][0]);
const struct linop_s* trafos[1] = { linop_identity_create(3, dims) };
lsqr = lsqr2_create(&lsqr_defaults,
iter2_admm, CAST_UP(&conf),
NULL, der, NULL,
regu ? 1 : 0,
regu ? prox_ops : NULL,
regu ? trafos : NULL,
NULL);
linop_free(der);
linop_free(trafos[0]);
operator_p_free(prox_ops[0]);
struct iter3_irgnm_conf irgnm_conf = iter3_irgnm_defaults;
irgnm_conf.iter = 4;
iter4_irgnm2(CAST_UP(&irgnm_conf), zexp,
2 * md_calc_size(N, dims), (float*)src2, ref ? (const float*)src1 : NULL,
2 * md_calc_size(N, dims), (const float*)dst1, lsqr,
(struct iter_op_s){ NULL, NULL });
double err = md_znrmse(N, dims, src1, src2);
operator_p_free(lsqr);
nlop_free(zexp);
md_free(src1);
md_free(dst1);
md_free(src2);
UT_ASSERT(err < 1.E-3);
}
static bool test_iter_irgnm(void)
{
return test_iter_irgnm0(false, false)
&& test_iter_irgnm0(false, true);
}
UT_REGISTER_TEST(test_iter_irgnm);
static bool test_iter_irgnm2(void)
{
return test_iter_irgnm0(true, false)
&& test_iter_irgnm0(true, true);
}
UT_REGISTER_TEST(test_iter_irgnm2);
static bool test_iter_irgnm_lsqr(void)
{
return test_iter_irgnm_lsqr0(false)
&& test_iter_irgnm_lsqr0(true);
}
UT_REGISTER_TEST(test_iter_irgnm_lsqr);
static bool test_iter_irgnm_lsqr_l1(void)
{
return test_iter_irgnm_lsqr1(false, true)
&& test_iter_irgnm_lsqr1(false, false)
&& test_iter_irgnm_lsqr1(true, true)
&& test_iter_irgnm_lsqr1(true, false);
}
UT_REGISTER_TEST(test_iter_irgnm_lsqr_l1);
static bool test_iter_irgnm_l1(void)
{
enum { N = 3 };
long dims[N] = { 4, 2, 3 };
long dims1[N] = { 4, 2, 1 };
long dims2[N] = { 4, 2, 2 };
complex float* dst1 = md_alloc(N, dims, CFL_SIZE);
complex float* src1 = md_alloc(N, dims, CFL_SIZE);
complex float* src2 = md_alloc(N, dims, CFL_SIZE);
complex float* src3 = md_alloc(N, dims, CFL_SIZE);
md_zfill(N, dims, src1, 1.);
const struct linop_s* id = linop_identity_create(3, dims);
struct nlop_s* nlid = nlop_from_linop(id);
nlop_apply(nlid, N, dims, dst1, N, dims, src1);
md_zfill(N, dims, src2, 0.975);
md_zfill(N, dims, src3, 0.);
const struct operator_p_s* lsqr = NULL;
struct iter_fista_conf conf = iter_fista_defaults;
conf.maxiter = 10;
const struct linop_s* trafos[1] = { id };
auto p1 = prox_thresh_create(3, dims1, 0.5, 0u);
auto p2 = prox_thresh_create(3, dims2, 0.5, 0u);
const struct operator_p_s* prox_ops[1] = { operator_p_stack(2, 2, p1, p2) };
operator_p_free(p1);
operator_p_free(p2);
lsqr = lsqr2_create(&lsqr_defaults,
iter2_fista, CAST_UP(&conf),
NULL, &nlid->derivative[0][0], NULL,
1, prox_ops, trafos, NULL);
struct iter3_irgnm_conf conf2 = iter3_irgnm_defaults;
conf2.redu = 1.;
conf2.iter = 1.;
iter4_irgnm2(CAST_UP(&conf2), nlid,
2 * md_calc_size(N, dims), (float*)src3, NULL,
2 * md_calc_size(N, dims), (const float*)dst1, lsqr,
(struct iter_op_s){ NULL, NULL });
double err = md_znrmse(N, dims, src2, src3);
nlop_free(nlid);
linop_free(id);
operator_p_free(prox_ops[0]);
operator_p_free(lsqr);
md_free(src1);
md_free(dst1);
md_free(src2);
md_free(src3);
UT_ASSERT(err < 1.E-10);
}
UT_REGISTER_TEST(test_iter_irgnm_l1);
static bool test_iter_lsqr_warmstart(void)
{
enum { N = 3 };
long dims[N] = { 4, 2, 3 };
complex float* src = md_alloc(N, dims, CFL_SIZE);
complex float* dst = md_alloc(N, dims, CFL_SIZE);
md_zfill(N, dims, src, 1.66);
md_zfill(N, dims, dst, 0.66);
const struct linop_s* id = linop_identity_create(3, dims);
struct lsqr_conf conf = lsqr_defaults;
conf.warmstart = true;
struct iter_conjgrad_conf cg_conf = iter_conjgrad_defaults;
cg_conf.maxiter = 0;
const struct operator_p_s* lsqr = lsqr2_create(&conf,
iter2_conjgrad, CAST_UP(&cg_conf),
NULL, id, NULL,
0, NULL, NULL, NULL);
operator_p_apply(lsqr, 0.3, N, dims, dst, N, dims, src);
md_zfill(N, dims, src, 0.66);
double err = md_znrmse(N, dims, src, dst);
linop_free(id);
md_free(src);
md_free(dst);
operator_p_free(lsqr);
UT_ASSERT(err < UT_TOL);
}
UT_REGISTER_TEST(test_iter_lsqr_warmstart);
|