|
1 | 1 | from samplers import * |
2 | 2 | from solvers import * |
3 | 3 | from plots import * |
| 4 | +from multiprocessing import Pool |
4 | 5 |
|
5 | 6 | # set parameters |
6 | 7 | tests = 50 # number of tests |
7 | | -n = 4 # number of parameters of the polynomial (degree + 1) |
8 | | -f = 1.0 # distance between the origin and the image plane |
9 | | -b = 1.0 # intersection between camera axis and the surface |
10 | | -slopes = np.linspace(-np.pi/6, np.pi/6, 13) |
| 8 | +pools = 5 |
| 9 | +n = 6 # number of parameters of the polynomial (degree + 1) |
| 10 | +ovs = 1 # oversampling |
| 11 | +f = 1.0 # distance between the origin and the image plane |
| 12 | +b = 1.0 # intersection between camera axis and the surface |
| 13 | +slopes = np.linspace(-np.pi / 9, np.pi / 9, 13) |
| 14 | +# slopes = slopes[1:-1] |
| 15 | +noise_scale = 0 |
| 16 | +noise_ampl = 0.0 |
| 17 | +if noise_scale is not 0: |
| 18 | + noise_ampl = 10.0 ** (-noise_scale) |
| 19 | + |
11 | 20 |
|
12 | 21 | # containers for results: |
13 | | -errors = [] |
14 | | -nsr = [] |
15 | | - |
16 | | -for test in range(tests): |
17 | | - print("test: ", test) |
18 | | - |
19 | | - tmp_err = [] |
20 | | - start_param = nr.randn(n, 1) |
21 | | - |
22 | | - print(start_param) |
23 | | - |
24 | | - for slope in slopes: |
25 | | - polynomial = SecondSurfacePolynomial(start_param) |
26 | | - sampler = SurfaceSampler(polynomial, 2*n, [slope, b, f], interval_length=2, sigma=0.0, beg=-1) |
27 | | - noise = 1e-1*nr.randn(2*n) |
28 | | - nsr.append(np.linalg.norm(noise) / np.linalg.norm(sampler.sample_values)) |
29 | | - sample_values = sampler.sample_values + noise |
30 | | - |
31 | | - solver = ConstrainedALS( |
32 | | - sample_values, |
33 | | - polynomial.model_size, |
34 | | - SecondSurfacePolynomial, |
35 | | - start_pos=sampler.sample_positions, |
36 | | - stopping_error=1e-10, |
37 | | - beta=0.1, |
38 | | - show_plots=False, |
39 | | - max_iter=10000, |
40 | | - fl=f) |
41 | | - |
42 | | - true_error = 0 |
43 | | - try: |
44 | | - solver.solve() |
45 | | - true_error = abs(slope - solver.tr_param[0]) |
46 | | - except AssertionError as as_err: |
47 | | - print("assertion error:", as_err.args[0]) |
48 | | - true_error = np.NAN |
49 | | - |
50 | | - tmp_err.append(true_error) |
51 | | - errors.append(tmp_err) |
52 | | - |
53 | | -errors = np.array(errors) |
54 | | -nsr = np.array(nsr) |
55 | | - |
56 | | -print("mean:", np.nanmean(errors)*180/np.pi) |
57 | | -print("median:", np.nanmedian(errors)) |
58 | | -print("std:", np.nanstd(errors)) |
59 | | -print("NANS:", str(np.count_nonzero(np.isnan(errors))/len(errors.flatten())*100)+"%") |
| 22 | + |
| 23 | +def test_block(beginning): |
| 24 | + errors = [] |
| 25 | + nsr = [] |
| 26 | + params = [] |
| 27 | + np.random.seed(beginning) |
| 28 | + for test in range(int(tests / pools)): |
| 29 | + print("test: ", int(tests / pools) * beginning + test) |
| 30 | + |
| 31 | + tmp_err = [] |
| 32 | + start_param = nr.randn(n, 1) |
| 33 | + start_param[0] = 1 |
| 34 | + |
| 35 | + # print(start_param) |
| 36 | + params.append(start_param) |
| 37 | + |
| 38 | + for slope in slopes: |
| 39 | + polynomial = SecondSurfacePolynomial(start_param) |
| 40 | + sampler = SurfaceSampler(polynomial, 2 * ovs * n, [slope, b, f], interval_length=2, sigma=0.0, beg=0) |
| 41 | + noise = noise_ampl * nr.randn(2 * ovs * n) |
| 42 | + nsr.append(np.linalg.norm(noise) / np.linalg.norm(sampler.sample_values)) |
| 43 | + sample_values = sampler.sample_values + noise |
| 44 | + |
| 45 | + solver = ConstrainedALS( |
| 46 | + sample_values, |
| 47 | + polynomial.model_size, |
| 48 | + SecondSurfacePolynomial, |
| 49 | + start_pos=sampler.sample_positions, |
| 50 | + stopping_error=1e-10, |
| 51 | + beta=0.1, |
| 52 | + show_plots=False, |
| 53 | + max_iter=10000, |
| 54 | + fl=f, |
| 55 | + verbose=False) |
| 56 | + |
| 57 | + true_error = 0 |
| 58 | + try: |
| 59 | + solver.solve() |
| 60 | + true_error = abs(slope - solver.tr_param[0]) |
| 61 | + except AssertionError as as_err: |
| 62 | + print("assertion error:", as_err.args[0]) |
| 63 | + true_error = np.NAN |
| 64 | + |
| 65 | + tmp_err.append(true_error) |
| 66 | + errors.append(np.array(tmp_err)) |
| 67 | + return (errors, nsr, params) |
| 68 | + |
| 69 | + |
| 70 | +p = Pool(pools) |
| 71 | +sth = p.map(test_block, range(pools)) |
| 72 | +sth = np.array(sth) |
| 73 | +errors = np.concatenate(sth[:, 0]) |
| 74 | +nsr = np.concatenate(sth[:, 1]).reshape(tests, len(slopes)) |
| 75 | +params = np.concatenate(sth[:, 2]).reshape(tests, n) |
| 76 | + |
| 77 | +print("mean:", np.degrees(np.nanmean(errors))) |
| 78 | +print("median:", np.degrees(np.nanmedian(errors))) |
| 79 | +print("std:", np.degrees(np.nanstd(errors))) |
| 80 | +print("NANS:", str(np.count_nonzero(np.isnan(errors)) / len(errors.flatten()) * 100) + "%") |
60 | 81 | print("noise to signal", np.nanmean(nsr)) |
| 82 | + |
| 83 | +version = str(n)+"_"+str(ovs)+"_"+str(noise_scale) |
| 84 | +np.save("errors_"+version, errors) |
| 85 | +np.save("nsr_"+version, nsr) |
| 86 | +np.save("params_"+version, params) |
0 commit comments