-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL-curve meth.py
303 lines (240 loc) · 9.72 KB
/
L-curve meth.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
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
import numpy as np
import matplotlib.pyplot as plt
import glob
from rldeconvolution.fix_inputs import fix_inputs
# Define paths to kernel and measurement data
psf_path = './data/kernel'
psf_files = sorted(glob.glob(psf_path + "/*.csv"))
orig_path = './data/original'
orig_files = glob.glob(orig_path + "/*.csv")
# Choose which file to use
file_index = 0
# Load kernel and measurement data
psf_data = np.loadtxt(psf_files[file_index], delimiter=';', skiprows=63)
orig_data = np.loadtxt(orig_files[file_index], delimiter=';', skiprows=63)
lb, b, lM, M = fix_inputs(psf_data[0, :], psf_data[1, :], orig_data[0, :], orig_data[1, :])
# b = b/np.sum(b)
# signal mesuré optimal
mes_sim = np.convolve(b, M, mode='same')/np.sum(b)
def rlMeth(signal: np.ndarray, psf: np.ndarray, num_iter: int = 1500, autostop: bool = True):
flipped_psf = psf[::-1]
SH = np.zeros((num_iter + 1, signal.size))
# SH[0] = signal
SH[0] = np.ones_like(signal)
residual = np.zeros((num_iter + 1, signal.size))
residual[0] = signal - np.convolve(psf, SH[0], mode='same')/np.sum(psf)
for r in range(1, num_iter + 1):
temp1 = np.convolve(SH[r - 1], psf, mode='same')/np.sum(psf)
temp1 = np.divide(signal, temp1, out=np.zeros_like(signal), where=temp1 != 0)
# Check for zero division errors
count_bad = np.count_nonzero(temp1 == 0)
if count_bad > 0:
print(f'After convolution in RL iteration {r}, {count_bad} estimated values were set to zero.')
temp1[np.logical_or(np.isnan(temp1), np.isinf(temp1))] = 0
temp2 = np.convolve(temp1, flipped_psf, mode='same')/np.sum(flipped_psf)
tempSH = SH[r - 1] * temp2
tempSH[np.logical_or(np.isnan(tempSH), np.isinf(tempSH))] = 0
SH[r] = tempSH
residual[r] = signal - np.convolve(psf, SH[r], mode='same')/np.sum(psf)
"""
# Critère d'arrêt
if autostop and r > 1:
delta = np.linalg.norm(SH[r] - SH[r - 1]) / np.linalg.norm(SH[r - 1])
if delta < 1e-6:
print(f'Convergence atteinte à l\'itération {r}')
SH = SH[:r + 1]
break
"""
return SH, residual
def compute_L_curve(SH, residual):
# num_iter = SH.shape[0] - 1
solution_norms = np.linalg.norm(SH, ord=2, axis=1)
residual_norms = np.linalg.norm(residual, ord=2, axis=1)
"""
for k in range(num_iter + 1):
solution_norms[k] = solution_norms.append(np.linalg.norm(SH[k], ord=2))
residual_norms[k] = residual_norms.append(np.linalg.norm(residual[k], ord=2))
# solution_norms = np.where(solution_norms > 0, solution_norms, 1e-10)
# residual_norms = np.where(residual_norms > 0, residual_norms, 1e-10)
"""
x_vals = np.log(solution_norms)
y_vals = np.log(residual_norms)
return solution_norms, residual_norms, x_vals, y_vals
def compute_curvature(x_vals, y_vals):
iters = np.arange(1, len(x_vals) + 1)
log_iters = np.log(iters)
dlog_iters = log_iters[1] - log_iters[0]
dx = np.gradient(x_vals, dlog_iters)
dy = np.gradient(y_vals, dlog_iters)
d2x = np.gradient(dx, dlog_iters)
d2y = np.gradient(dy, dlog_iters)
denominator = np.abs((dx**2 + dy**2)**1.5)
numerator = dx * d2y - dy * d2x
#print(numerator.shape)
#print(denominator.shape)
curvature = np.where(denominator != 0, numerator / denominator, 0)
return curvature
# Richardson-Lucy
num_iterations = 2500
m = mes_sim
SH, residual = rlMeth(signal=m, psf=b, num_iter=num_iterations, autostop=False)
# données L-curve
solution_norms, residual_norms, x_vals, y_vals = compute_L_curve(SH, residual)
# courbure du L-curve
curvatures = compute_curvature(x_vals, y_vals)
"""
# itération optimal (maximum de courbure)
optimal_index = np.argmax(curvatures)
optimal_iteration = optimal_index + 1
print(f"Optimal iteration at: {optimal_iteration}")
# signal estimé optimal
optimal_spectrum = SH[optimal_iteration]
error_signal_2 = np.linalg.norm(optimal_spectrum - M)
relative_error_signal_2 = error_signal_2 / np.linalg.norm(M)
print(f"Error between optimal spectrum and original signal: {error_signal_2}")
print(f"Relative error: {relative_error_signal_2}")
# Plot L-curve et le point optimal
plt.figure()
plt.plot(x_vals, y_vals, label='L-curve')
plt.scatter(x_vals[optimal_iteration], y_vals[optimal_iteration], color='red', label='Optimal point')
plt.xlabel('log(||s_k||₂)')
plt.ylabel('log(||m - b * s_k||₂)')
plt.legend()
plt.title('L-curve with Optimal Point')
plt.show()
"""
# Plot courbure vs itération
iterations = np.arange(1, len(curvatures) + 1)
plt.figure()
plt.plot(iterations, curvatures)
plt.xlabel('Iteration')
plt.ylabel('Curvature')
plt.title('Curvature vs Iteration')
plt.show()
"""
# Plot du signal estimé à l'itération optimal
plt.figure()
plt.plot(lM, optimal_spectrum, label='Optimal Spectrum')
plt.xlabel('Wavelength')
plt.ylabel('Amplitude')
plt.title('Estimated Original Spectrum at Optimal Iteration')
plt.legend()
plt.show()
"""
# Calcul de la fonction objectif F(k, lambda)
# les valeurs de lambda prises
lambdas = np.array([1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
# max_residual_norm = np.max(residual_norms)
# max_solution_norm = np.max(solution_norms)
# calcul F(k, lambda)
F_values = np.zeros((len(lambdas), num_iterations + 1))
for i, lam in enumerate(lambdas):
F_values[i, :] = residual_norms + lam * solution_norms
# indices des F(k, lambda) minimal
min_index = np.unravel_index(np.argmin(F_values, axis=None), F_values.shape)
optimal_lambda = lambdas[min_index[0]]
optimal_k = min_index[1]
print(f"Optimal lambda: {optimal_lambda}")
print(f"Optimal iteration (k): {optimal_k}")
# signal estimé optimal
optimal_spectrum_lambda = SH[optimal_k]
"""
# error / erreur relative entre le signal optimal et le signal original
error_signal = np.linalg.norm(optimal_spectrum_lambda - M)
relative_error_signal = error_signal / np.linalg.norm(M)
print(f"Error between optimal spectrum (lambda) and original signal: {error_signal}")
print(f"Relative error: {relative_error_signal}")
"""
# Plot du heatmap de F(k, lambda)
plt.figure(figsize=(10, 6))
plt.imshow(F_values, aspect='auto', origin='lower',
extent=[0, num_iterations, lambdas[0], lambdas[-1]],
cmap='viridis')
plt.colorbar(label='F(k, lambda)')
plt.xlabel('Iteration k')
plt.ylabel('Lambda')
plt.title('Objective Function F(k, lambda)')
plt.scatter(optimal_k, optimal_lambda, color='red', label='Optimal (k, lambda)')
plt.legend()
plt.yscale('log')
plt.show()
# Plot du signal optimal
plt.figure(figsize=(10, 4))
plt.plot(lM, optimal_spectrum_lambda, label='Optimal Estimated Signal (lambda)')
plt.plot(lM, M, label='Original Signal', alpha=0.5)
plt.legend()
plt.title('Optimal Estimated Signal vs Original Signal')
plt.xlabel('Wavelength')
plt.ylabel('Amplitude')
plt.show()
# Plot des solutions / résidus vs itérations
plt.figure()
plt.plot(solution_norms, label='Norme de la solution ||s_k||₂')
plt.plot(residual_norms, label='Norme du résidu ||m - b * s_k||₂')
plt.xlabel('Itération k')
plt.ylabel('Norme L2')
plt.legend()
plt.title('Évolution des normes en fonction des itérations')
plt.show()
# Plot solution_norms vs residual_norms
plt.plot(residual_norms, solution_norms, 'o-')
plt.xlabel('residual_norms')
plt.ylabel('solution_norms')
plt.title('residual_norms vs solution_norms')
plt.show()
"""
lambda_values = np.array([1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1])
optimal_iterations = []
solution_norms_lambda = []
residual_norms_lambda = []
for lam in lambda_values:
# Calculer F(k, lambda) pour toutes les itérations k
F_lambda = (residual_norms / max_residual_norm) + lam * (solution_norms / max_solution_norm)
# Trouver l'itération k qui minimise F(k, lambda) pour ce lambda
k_opt = np.argmin(F_lambda)
optimal_iterations.append(k_opt)
# Récupérer les normes pour k_opt
solution_norm = solution_norms[k_opt]
residual_norm = residual_norms[k_opt]
solution_norms_lambda.append(solution_norm)
residual_norms_lambda.append(residual_norm)
print(f"Lambda: {lam}, Optimal iteration: {k_opt}, Solution norm: {solution_norm}, Residual norm: {residual_norm}")
# Convertir les listes en tableaux numpy pour faciliter le traçage
solution_norms_lambda = np.array(solution_norms_lambda)
residual_norms_lambda = np.array(residual_norms_lambda)
plt.figure(figsize=(8, 6))
plt.plot(residual_norms_lambda, solution_norms_lambda, 'o-', label='Points (λ, k_opt)')
plt.xlabel('Norme L2 du résidu ||m - b * s_k||₂')
plt.ylabel('Norme L2 de la solution ||s_k||₂')
plt.title('Norme de la solution vs Norme du résidu pour différentes valeurs de λ')
plt.grid(True)
plt.legend()
plt.show()
error_signals = np.linalg.norm(SH - M, axis=1)
min_error = np.min(error_signals)
min_index = np.argmin(error_signals)
print(f"Erreur minimale : {min_error}")
print(f"Indice de l'erreur minimale : {min_index}")
"""
"""
def calculer_differences(SH):
differences = []
for k in range(len(SH) - 1):
difference = SH[k+1] - SH[k]
differences.append(difference)
return differences
"""
"""
R = np.zeros((num_iterations + 1, SH.shape[1]))
cr = np.zeros(1499)
for i in range (num_iterations + 1):
R[i] = m - SH[i]
for j in range(1, num_iterations - 1):
cr[j] = (np.linalg.norm(R[j]) - np.linalg.norm(R[j+1])) / np.linalg.norm(R[j])
"""
"""
differences = []
for k in range(len(SH) - 1):
difference = np.linalg.norm(SH[k+1] - SH[k])
differences.append(difference)
"""