-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_psi_operator.py
189 lines (147 loc) · 5.46 KB
/
test_psi_operator.py
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
import numpy as np
from numpy.testing import assert_array_almost_equal
from pfb.prox.prox_21 import prox_21
from pfb.prox.prox_21m import prox_21m
from pfb.prox.prox_21m import prox_21m_numba, dual_update, dual_update_numba
import pywt
import pytest
from pfb.operators.psi import Psi
pmp = pytest.mark.parametrize
@pmp("nx", [128, 250])
@pmp("ny", [64, 78])
@pmp("nband", [1, 3, 6])
@pmp("nlevels", [1, 2])
def test_psi(nx, ny, nband, nlevels):
"""
Check that decomposition + reconstruction is the identity
"""
np.random.seed(420)
# image = pywt.data.aero()
image = np.random.randn(nx, ny)
nu = 1.0 + 0.1 * np.arange(nband)
x = image[None, 0:nx, 0:ny] * nu[:, None, None] ** (-0.7)
# set up dictionary
bases = ['self','db1','db2','db3','db4','db5']
nbasis = len(bases)
psi = Psi(nband, nx, ny, bases, nlevels, 1)
nxmax = psi.Nxmax
nymax = psi.Nymax
# make sure this works even when output arrays are randomly populated
alpha = np.random.randn(nband, nbasis, nymax, nxmax) #, dtype=x.dtype)
xrec = np.random.randn(nband, nx, ny) #, dtype=x.dtype)
# decompose
psi.dot(x, alpha)
# reconstruct
psi.hdot(alpha, xrec)
# the nbasis is required here because the operator is not normalised
assert_array_almost_equal(nbasis*x, xrec, decimal=12)
@pmp("nx", [120, 240])
@pmp("ny", [64, 150])
@pmp("nband", [1, 3, 6])
@pmp("nlevels", [1, 2])
def test_prox21(nx, ny, nband, nlevels):
"""
Check that applying the prox with zero step size is the identity
"""
np.random.seed(420)
image = pywt.data.aero()
nu = 1.0 + 0.1 * np.arange(nband)
x = image[None, 0:nx, 0:ny] * nu[:, None, None] ** (-0.7)
# set up dictionary
bases = ['self','db1','db2','db3','db4','db5']
nbasis = len(bases)
psi = Psi(nband, nx, ny, bases, nlevels, 1)
nxmax = psi.Nxmax
nymax = psi.Nymax
weights_21 = np.random.random(nbasis*nymax*nxmax).reshape(nbasis, nymax, nxmax)
sig_21 = 0.0
alpha = np.zeros((nband, nbasis, nymax, nxmax), dtype=x.dtype)
xrec = np.zeros((nband, nx, ny), dtype=x.dtype)
psi.dot(x, alpha)
y = prox_21(alpha, sig_21, weights_21)
psi.hdot(y, xrec)
# the nbasis is required here because the operator is not normalised
assert_array_almost_equal(nbasis*x, xrec, decimal=12)
@pmp("nx", [1202, 240])
@pmp("ny", [324, 1506])
@pmp("nband", [1, 3, 6])
@pmp("nlevels", [1, 2])
def test_prox21m(nx, ny, nband, nlevels):
"""
Check that applying the prox with zero step size is the identity
"""
np.random.seed(420)
image = np.random.randn(nx, ny)
nu = 1.0 + 0.1 * np.arange(nband)
x = image[None, :, :] * nu[:, None, None] ** (-0.7)
# set up dictionary
bases = ['self','db1','db2','db3','db4','db5']
nbasis = len(bases)
psi = Psi(nband, nx, ny, bases, nlevels, 1)
nxmax = psi.Nxmax
nymax = psi.Nymax
weights_21 = np.random.random(nbasis*nymax*nxmax).reshape(nbasis, nymax, nxmax)
sig_21 = 0.0
alpha = np.zeros((nband, nbasis, nymax, nxmax), dtype=x.dtype)
xrec = np.zeros((nband, nx, ny), dtype=x.dtype)
psi.dot(x, alpha)
y = prox_21m(alpha, sig_21, weights_21)
psi.hdot(y, xrec)
# the nbasis is required here because the operator is not normalised
assert_array_almost_equal(nbasis*x, xrec, decimal=12)
@pmp("nymax", [1234, 240])
@pmp("nxmax", [134, 896])
@pmp("nbasis", [1, 5])
@pmp("nband", [1, 3, 6])
@pmp("lam", [1.0, 1e-1, 1e-3])
@pmp("sigma", [75.0, 1.0, 1e-3])
def test_prox21m_numba(nband, nbasis, nymax, nxmax, lam, sigma):
np.random.seed(420)
# check numba implementation matches numpy even when output contains random
# numbers initially
v = np.random.randn(nband, nbasis, nymax, nxmax)
vout = np.random.randn(nband, nbasis, nymax, nxmax)
l1weight = np.random.random(nbasis*nymax*nxmax).reshape(nbasis, nymax, nxmax)
res = prox_21m(v, lam, weight=l1weight)
prox_21m_numba(v, vout, lam, weight=l1weight)
assert_array_almost_equal(res, vout, decimal=12)
res = prox_21m(v/sigma, lam/sigma, weight=l1weight)
prox_21m_numba(v, vout, lam, sigma=sigma, weight=l1weight)
assert_array_almost_equal(res, vout, decimal=8)
@pmp("nx", [120, 240])
@pmp("ny", [324, 150])
@pmp("nband", [1, 3, 6])
@pmp("nlevels", [1, 2])
@pmp("lam", [1.0, 1e-1, 1e-3])
@pmp("sigma", [75.0, 1.0, 1e-3])
def test_dual_update(nx, ny, nband, nlevels, lam, sigma):
"""
Compare numpy to numba optimised dual update
"""
np.random.seed(420)
image = np.random.randn(nx, ny)
nu = 1.0 + 0.1 * np.arange(nband)
x = image[None, :, :] * nu[:, None, None] ** (-0.7)
xp = x.copy()
# set up dictionary
bases = ['self','db1','db2','db3','db4','db5']
nbasis = len(bases)
psi = Psi(nband, nx, ny, bases, nlevels, 1)
nxmax = psi.Nxmax
nymax = psi.Nymax
weight21 = np.random.random(nbasis*nymax*nxmax).reshape(nbasis, nymax, nxmax)
# lam21 = 0.1
# sigma = 1.75
# can't initialise v randomly because edges won't agree
# vout in dual_update is initialsed with zeros
# v = np.random.randn(nband, nbasis, nymax, nxmax)
v = np.zeros((nband, nbasis, nymax, nxmax))
x2 = np. random.randn(nband, nx, ny)
psi.dot(x2, v)
vp = v.copy()
res1 = dual_update(v, x, psi.dot, lam, sigma=sigma, weight=weight21)
# initialise v with psiH(x)
psi.dot(xp, v)
dual_update_numba(vp, v, lam, sigma=sigma, weight=weight21)
# TODO - why the low accuracy?
assert_array_almost_equal(1 + res1,1 + v, decimal=9)