Skip to content

Commit 08dd56d

Browse files
committed
Refactor proximal application function name
Signed-off-by: Nicola VIGANÒ <nicola.vigano@cea.fr>
1 parent 13cb9e9 commit 08dd56d

4 files changed

Lines changed: 33 additions & 33 deletions

File tree

src/corrct/data_terms.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def update_dual(self, dual: NDArrayFloat, proj_primal: NDArrayFloat) -> None:
234234
dual += (proj_primal + self.background) * self.sigma
235235

236236
@abstractmethod
237-
def apply_proximal(self, dual: NDArrayFloat) -> None:
237+
def apply_proximal_dual(self, dual: NDArrayFloat) -> None:
238238
"""Apply the proximal in the dual domain.
239239
240240
Parameters
@@ -283,7 +283,7 @@ def assign_data(self, data: float | NDArrayFloat | None = None, sigma: float | N
283283
def compute_residual_norm(self, dual: NDArrayFloat) -> float:
284284
return float(np.linalg.norm(dual.flatten(), ord=2) ** 2)
285285

286-
def apply_proximal(self, dual: NDArrayFloat) -> None:
286+
def apply_proximal_dual(self, dual: NDArrayFloat) -> None:
287287
if self.data is not None and self.sigma_data is not None:
288288
dual -= self.sigma_data
289289
dual *= self.sigma1
@@ -364,7 +364,7 @@ def compute_residual(self, proj_primal: NDArrayFloat, mask: NDArrayFloat | None
364364
_soft_threshold(residual, self.sigma_sqrt_error)
365365
return residual
366366

367-
def apply_proximal(self, dual: NDArrayFloat) -> None:
367+
def apply_proximal_dual(self, dual: NDArrayFloat) -> None:
368368
if self.data is not None and self.sigma_data is not None:
369369
dual -= self.sigma_data
370370
_soft_threshold(dual, self.sigma_sqrt_error)
@@ -401,7 +401,7 @@ def compute_residual_norm(self, dual):
401401
l1_points = 1 - l2_points
402402
return np.linalg.norm(dual[l2_points].flatten(), ord=2) ** 2 + np.linalg.norm(dual[l1_points].flatten(), ord=1)
403403

404-
def apply_proximal(self, dual):
404+
def apply_proximal_dual(self, dual):
405405
if self.data is not None and self.sigma_data is not None:
406406
dual -= self.sigma_data
407407

@@ -437,7 +437,7 @@ def _get_inner_norm(self, dual):
437437
def _apply_threshold(self, dual):
438438
pass
439439

440-
def apply_proximal(self, dual, weight=1.0):
440+
def apply_proximal_dual(self, dual, weight=1.0):
441441
if self.data is not None:
442442
dual -= self.sigma_data
443443
self._apply_threshold(dual)
@@ -505,7 +505,7 @@ def _compute_sigma_data(self):
505505
else:
506506
return 4 * self.sigma * np.fmax(self.data, 0.0)
507507

508-
def apply_proximal(self, dual):
508+
def apply_proximal_dual(self, dual):
509509
if self.sigma_data is not None:
510510
dual[:] = (1 + dual[:] - np.sqrt((dual[:] - 1) ** 2 + self.sigma_data[:])) / 2
511511
else:
@@ -517,7 +517,7 @@ def compute_residual(self, proj_primal, mask=None):
517517
# we take the Moreau envelope here, and apply the proximal to it
518518
residual = np.fmax(proj_primal, eps) * self.sigma
519519

520-
self.apply_proximal(residual)
520+
self.apply_proximal_dual(residual)
521521

522522
if mask is not None:
523523
residual *= mask
@@ -553,7 +553,7 @@ def __init__(self, background=None, ln_axes: Sequence[int] = (1, -1), spectral_n
553553
self.spectral_norm = spectral_norm
554554
self.use_fallback = False
555555

556-
def apply_proximal(self, dual):
556+
def apply_proximal_dual(self, dual):
557557
dual_tmp = dual.copy()
558558

559559
if self.sigma_data is not None:
@@ -569,15 +569,15 @@ def apply_proximal(self, dual):
569569

570570
U, s_p, Vt = np.linalg.svd(dual_tmp, full_matrices=False)
571571

572-
self.spectral_norm.apply_proximal(s_p)
572+
self.spectral_norm.apply_proximal_dual(s_p)
573573

574574
dual_tmp = np.matmul(U, s_p[..., None] * Vt)
575575
dual_tmp = np.transpose(dual_tmp, np.argsort(t_range))
576576
else:
577577
op_svd = operators.TransformSVD(dual_tmp.shape, axes_rows=self.ln_axes[0], axes_cols=self.ln_axes[1])
578578
s_p = op_svd(dual_tmp)
579579

580-
self.spectral_norm.apply_proximal(s_p)
580+
self.spectral_norm.apply_proximal_dual(s_p)
581581

582582
dual_tmp = op_svd.T(s_p)
583583

src/corrct/regularizers.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def update_dual(self, dual: NDArray, primal: NDArray) -> None:
147147

148148
dual += self.sigma * self.op(primal)
149149

150-
def apply_proximal(self, dual: NDArray) -> None:
150+
def apply_proximal_dual(self, dual: NDArray) -> None:
151151
"""
152152
Apply the proximal operator to the dual in-place.
153153
@@ -157,9 +157,9 @@ def apply_proximal(self, dual: NDArray) -> None:
157157
The dual to be applied the proximal on.
158158
"""
159159
if isinstance(self.norm, dt.DataFidelity_l1):
160-
self.norm.apply_proximal(dual, self.weight)
160+
self.norm.apply_proximal_dual(dual, self.weight)
161161
else:
162-
self.norm.apply_proximal(dual)
162+
self.norm.apply_proximal_dual(dual)
163163

164164
def compute_update_primal(self, dual: NDArray) -> NDArray:
165165
"""
@@ -612,15 +612,15 @@ def update_dual(self, dual: NDArray, primal: NDArray) -> None:
612612
if not self.min_approx:
613613
dual[0, ...] = 0
614614

615-
def apply_proximal(self, dual: NDArray) -> None:
615+
def apply_proximal_dual(self, dual: NDArray) -> None:
616616
if isinstance(self.norm, dt.DataFidelity_l12):
617617
tmp_dual = dual[1:]
618618
tmp_dual = tmp_dual.reshape([-1, self.level, *dual.shape[1:]])
619-
self.norm.apply_proximal(tmp_dual, self.weight)
619+
self.norm.apply_proximal_dual(tmp_dual, self.weight)
620620
tmp_dual = dual[0:1:]
621-
self.norm.apply_proximal(tmp_dual, self.weight)
621+
self.norm.apply_proximal_dual(tmp_dual, self.weight)
622622
else:
623-
super().apply_proximal(dual)
623+
super().apply_proximal_dual(dual)
624624

625625

626626
class Regularizer_l1swl(Regularizer_swl):
@@ -818,7 +818,7 @@ def update_dual(self, dual: NDArray, primal: NDArray) -> None:
818818
slices = [slice(0, x) for x in op_wl.sub_band_shapes[0]]
819819
dual[tuple(slices)] = 0
820820

821-
def apply_proximal(self, dual: NDArray) -> None:
821+
def apply_proximal_dual(self, dual: NDArray) -> None:
822822
if isinstance(self.norm, dt.DataFidelity_l12):
823823
op_wl: operators.TransformDecimatedWavelet = self.op
824824
coeffs = pywt.array_to_coeffs(dual, op_wl.slicing_info)
@@ -830,14 +830,14 @@ def apply_proximal(self, dual: NDArray) -> None:
830830
labels.append(lab)
831831
details.append(det)
832832
c_ll = np.stack(details, axis=0)
833-
self.norm.apply_proximal(c_ll, self.weight)
833+
self.norm.apply_proximal_dual(c_ll, self.weight)
834834
for ii, lab in enumerate(labels):
835835
c_l[lab] = c_ll[ii]
836836
coeffs[ii_l] = c_l
837-
self.norm.apply_proximal(coeffs[0], self.weight)
837+
self.norm.apply_proximal_dual(coeffs[0], self.weight)
838838
dual[:] = pywt.coeffs_to_array(coeffs)[0]
839839
else:
840-
super().apply_proximal(dual)
840+
super().apply_proximal_dual(dual)
841841

842842

843843
class Regularizer_l1dwl(Regularizer_dwl):
@@ -1130,7 +1130,7 @@ def _raise_pwise_norm_error(self):
11301130
+ f" Provided the following instead: derivatives={self.pwise_der_norm}, channel={self.pwise_chan_norm}"
11311131
)
11321132

1133-
def apply_proximal(self, dual: NDArray) -> None:
1133+
def apply_proximal_dual(self, dual: NDArray) -> None:
11341134
# Following assignments will detach the local array from the original one
11351135
dual_tmp = dual.copy()
11361136

@@ -1281,7 +1281,7 @@ def initialize_sigma_tau(self, primal: NDArray) -> float | NDArray:
12811281

12821282
return tau
12831283

1284-
def apply_proximal(self, dual: NDArray) -> None:
1284+
def apply_proximal_dual(self, dual: NDArray) -> None:
12851285
dual_tmp = dual.copy()
12861286

12871287
if self.q_ref is not None:
@@ -1405,9 +1405,9 @@ def initialize_sigma_tau(self, primal: NDArray) -> float | NDArray:
14051405
def update_dual(self, dual: NDArray, primal: NDArray) -> None:
14061406
dual += primal - self.limit
14071407

1408-
def apply_proximal(self, dual: NDArray) -> None:
1408+
def apply_proximal_dual(self, dual: NDArray) -> None:
14091409
dual[dual > 0.0] = 0.0
1410-
self.norm.apply_proximal(dual)
1410+
self.norm.apply_proximal_dual(dual)
14111411

14121412

14131413
class Constraint_UpperLimit(BaseRegularizer):
@@ -1453,6 +1453,6 @@ def initialize_sigma_tau(self, primal: NDArray) -> float | NDArray:
14531453
def update_dual(self, dual: NDArray, primal: NDArray) -> None:
14541454
dual += primal - self.limit
14551455

1456-
def apply_proximal(self, dual: NDArray) -> None:
1456+
def apply_proximal_dual(self, dual: NDArray) -> None:
14571457
dual[dual < 0.0] = 0.0
1458-
self.norm.apply_proximal(dual)
1458+
self.norm.apply_proximal_dual(dual)

src/corrct/solvers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ def __call__( # noqa: C901
840840
q = [reg.initialize_dual() for reg in self.regularizer]
841841
for q_r, reg in zip(q, self.regularizer):
842842
reg.update_dual(q_r, x)
843-
reg.apply_proximal(q_r)
843+
reg.apply_proximal_dual(q_r)
844844

845845
upd = A.T(res * sigma)
846846
for q_r, reg in zip(q, self.regularizer):
@@ -1093,14 +1093,14 @@ def __call__( # noqa: C901
10931093

10941094
Ax_rlx = A(x_relax)
10951095
self.data_term.update_dual(p, Ax_rlx)
1096-
self.data_term.apply_proximal(p)
1096+
self.data_term.apply_proximal_dual(p)
10971097

10981098
if b_mask is not None:
10991099
p *= b_mask
11001100

11011101
for q_r, reg in zip(q, self.regularizer):
11021102
reg.update_dual(q_r, x_relax)
1103-
reg.apply_proximal(q_r)
1103+
reg.apply_proximal_dual(q_r)
11041104

11051105
upd = A.T(p)
11061106
for q_r, reg in zip(q, self.regularizer):

tests/test_regularizers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ def _test_Regularizer_l1(self, vol):
4545

4646
dual += weight / 2
4747
copy_dual = dual.copy()
48-
reg.apply_proximal(dual)
48+
reg.apply_proximal_dual(dual)
4949
assert np.all(np.isclose(dual, np.fmin(weight, copy_dual)))
5050

5151
dual = copy_dual - weight * 2
5252
copy_dual = dual.copy()
53-
reg.apply_proximal(dual)
53+
reg.apply_proximal_dual(dual)
5454
assert np.all(np.isclose(dual, np.fmax(-weight, copy_dual)))
5555

5656
upd = reg.compute_update_primal(dual)
@@ -78,7 +78,7 @@ def _test_Regularizer_l1swl(self, vol):
7878
assert np.all(np.isclose(upd, reg.op.T(dual)))
7979

8080
copy_dual = dual.copy()
81-
reg.apply_proximal(dual)
81+
reg.apply_proximal_dual(dual)
8282
assert np.all(np.isclose(dual, np.fmax(np.fmin(weight, copy_dual), -weight)))
8383

8484
def _test_Regularizer_TV(self, vol):

0 commit comments

Comments
 (0)