Skip to content

Commit

Permalink
style: pep8 and print format
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeff committed Jul 31, 2016
1 parent da5c604 commit b020b54
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 100 deletions.
8 changes: 4 additions & 4 deletions doc/tutorials/compressed_sensing_douglas_rachford.rst
Expand Up @@ -23,9 +23,9 @@ reconstruction. See :cite:`candes2007CSperfect` for details.
>>> S = 100
>>> import numpy as np
>>> m = int(np.ceil(S * np.log(n)))
>>> print('Number of measurements: %d' % (m,))
>>> print('Number of measurements: {}'.format(m))
Number of measurements: 852
>>> print('Compression ratio: %3.2f' % (float(n)/m,))
>>> print('Compression ratio: {:3.2f}'.format(float(n) / m))
Compression ratio: 5.87

We generate a random measurement matrix `A`:
Expand Down Expand Up @@ -101,9 +101,9 @@ follows:

>>> x0 = np.zeros(n)
>>> ret = solvers.solve([f1, f2], x0, solver, rtol=1e-4, maxit=300)
Solution found after 56 iterations :
Solution found after 56 iterations:
objective function f(sol) = 7.590460e+00
stopping criterion : RTOL
stopping criterion: RTOL

Let's display the results:

Expand Down
14 changes: 7 additions & 7 deletions doc/tutorials/compressed_sensing_forward_backward.rst
Expand Up @@ -24,9 +24,9 @@ reconstruction. See :cite:`candes2007CSperfect` for details.
>>> S = 100
>>> import numpy as np
>>> m = int(np.ceil(S * np.log(n)))
>>> print('Number of measurements: %d' % (m,))
>>> print('Number of measurements: {}'.format(m))
Number of measurements: 852
>>> print('Compression ratio: %3.2f' % (float(n)/m,))
>>> print('Compression ratio: {:3.2f}'.format(float(n) / m))
Compression ratio: 5.87

We generate a random measurement matrix `A`:
Expand Down Expand Up @@ -85,9 +85,9 @@ or alternatively as follows:
.. plot::
:context:

>>> A_ = lambda x: np.dot(A, x)
>>> At_ = lambda x: np.dot(A.T, x)
>>> f3 = functions.norm_l2(y=y, A=A_, At=At_)
>>> f3 = functions.norm_l2(y=y)
>>> f3.A = lambda x: np.dot(A, x)
>>> f3.At = lambda x: np.dot(A.T, x)

.. note:: In this case the forward and adjoint operators were passed as
functions not as matrices.
Expand Down Expand Up @@ -134,9 +134,9 @@ follows:

>>> x0 = np.zeros(n)
>>> ret = solvers.solve([f1, f2], x0, solver, rtol=1e-4, maxit=300)
Solution found after 152 iterations :
Solution found after 152 iterations:
objective function f(sol) = 7.668195e+00
stopping criterion : RTOL
stopping criterion: RTOL

.. note:: A complete description of the parameters, their default values and
the returned values is given by the solving function
Expand Down
8 changes: 4 additions & 4 deletions doc/tutorials/denoising.rst
Expand Up @@ -19,7 +19,7 @@ Create a white circle on a black background

>>> import numpy as np
>>> N = 650
>>> im_original = np.resize(np.linspace(-1, 1, N), (N,N))
>>> im_original = np.resize(np.linspace(-1, 1, N), (N, N))
>>> im_original = np.sqrt(im_original**2 + im_original.T**2)
>>> im_original = im_original < 0.7

Expand Down Expand Up @@ -87,17 +87,17 @@ and the problem solved with

>>> x0 = np.array(im_noisy) # Make a copy to preserve y aka im_noisy.
>>> ret = solvers.solve([f1, f2], x0, solver)
Solution found after 25 iterations :
Solution found after 25 iterations:
objective function f(sol) = 2.080376e+03
stopping criterion : RTOL
stopping criterion: RTOL

Let's display the results:

.. plot::
:context:

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(figsize=(8,2.5))
>>> fig = plt.figure(figsize=(8, 2.5))
>>> ax1 = fig.add_subplot(1, 3, 1)
>>> _ = ax1.imshow(im_original, cmap='gray')
>>> _ = ax1.axis('off')
Expand Down
6 changes: 3 additions & 3 deletions doc/tutorials/reconstruction.rst
Expand Up @@ -88,17 +88,17 @@ and the problem solved with

>>> x0 = np.array(im_masked) # Make a copy to preserve im_masked.
>>> ret = solvers.solve([f1, f2], x0, solver, maxit=100)
Solution found after 94 iterations :
Solution found after 94 iterations:
objective function f(sol) = 4.268147e+03
stopping criterion : RTOL
stopping criterion: RTOL

Let's display the results:

.. plot::
:context:

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(figsize=(8,2.5))
>>> fig = plt.figure(figsize=(8, 2.5))
>>> ax1 = fig.add_subplot(1, 3, 1)
>>> _ = ax1.imshow(im_original, cmap='gray')
>>> _ = ax1.axis('off')
Expand Down
70 changes: 35 additions & 35 deletions doc/tutorials/simple.rst
Expand Up @@ -66,52 +66,52 @@ And finally solve the problem :

>>> x0 = [0., 0., 0., 0.]
>>> ret = solvers.solve([f2, f1], x0, solver, atol=1e-5, verbosity='HIGH')
func evaluation : 0.000000e+00
norm_l2 evaluation : 1.260000e+02
INFO: Forward-backward method : FISTA
Iteration 1 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 1.400000e+01
func evaluation: 0.000000e+00
norm_l2 evaluation: 1.260000e+02
INFO: Forward-backward method: FISTA
Iteration 1 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 1.400000e+01
objective = 1.40e+01
Iteration 2 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 1.555556e+00
Iteration 2 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 1.555556e+00
objective = 1.56e+00
Iteration 3 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 3.293044e-02
Iteration 3 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 3.293044e-02
objective = 3.29e-02
Iteration 4 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 8.780588e-03
Iteration 4 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 8.780588e-03
objective = 8.78e-03
Iteration 5 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 6.391406e-03
Iteration 5 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 6.391406e-03
objective = 6.39e-03
Iteration 6 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 5.713369e-04
Iteration 6 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 5.713369e-04
objective = 5.71e-04
Iteration 7 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 1.726501e-05
Iteration 7 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 1.726501e-05
objective = 1.73e-05
Iteration 8 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 6.109470e-05
Iteration 8 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 6.109470e-05
objective = 6.11e-05
Iteration 9 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 1.212636e-05
Iteration 9 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 1.212636e-05
objective = 1.21e-05
Iteration 10 of forward_backward :
func evaluation : 0.000000e+00
norm_l2 evaluation : 7.460428e-09
Iteration 10 of forward_backward:
func evaluation: 0.000000e+00
norm_l2 evaluation: 7.460428e-09
objective = 7.46e-09
Solution found after 10 iterations :
Solution found after 10 iterations:
objective function f(sol) = 7.460428e-09
stopping criterion : ATOL
stopping criterion: ATOL

The solving function returns several values, one is the found solution :

Expand Down
4 changes: 2 additions & 2 deletions pyunlocbox/__init__.py
Expand Up @@ -28,9 +28,9 @@
>>> f2 = pyunlocbox.functions.dummy()
>>> solver = pyunlocbox.solvers.forward_backward()
>>> ret = pyunlocbox.solvers.solve([f1, f2], [0., 0, 0, 0], solver, atol=1e-5)
Solution found after 10 iterations :
Solution found after 10 iterations:
objective function f(sol) = 7.460428e-09
stopping criterion : ATOL
stopping criterion: ATOL
>>> ret['sol']
array([ 3.99996922, 4.99996153, 5.99995383, 6.99994614])
Expand Down
16 changes: 9 additions & 7 deletions pyunlocbox/functions.py
Expand Up @@ -198,7 +198,8 @@ def eval(self, x):
"""
sol = self._eval(np.asarray(x))
if self.verbosity in ['LOW', 'HIGH']:
print(' %s evaluation : %e' % (self.__class__.__name__, sol))
name = self.__class__.__name__
print(' {} evaluation: {:e}'.format(name, sol))
return sol

def _eval(self, x):
Expand Down Expand Up @@ -538,7 +539,7 @@ class norm_tv(norm):
>>> x = np.arange(0, 16)
>>> x = x.reshape(4, 4)
>>> f.eval(x) # doctest:+ELLIPSIS
norm_tv evaluation : 5.210795e+01
norm_tv evaluation: 5.210795e+01
52.10795063...
"""
Expand Down Expand Up @@ -858,9 +859,9 @@ def _prox(self, x, T):
norm_res = np.linalg.norm(res, 2)

if self.verbosity is 'HIGH':
print(' proj_b2 iteration %3d : epsilon = %.2e, '
'||y-A(z)||_2 = %.2e'
% (niter, self.epsilon, norm_res))
print(' proj_b2 iteration {:3d}: epsilon = {:.2e}, '
'||y-A(z)||_2 = {:.2e}'.format(niter, self.epsilon,
norm_res))

# Scaling for projection.
res += u * self.nu
Expand Down Expand Up @@ -888,7 +889,8 @@ def _prox(self, x, T):

if self.verbosity in ['LOW', 'HIGH']:
norm_res = np.linalg.norm(self.y() - self.A(sol), 2)
print(' proj_b2 : epsilon = %.2e, ||y-A(z)||_2 = %.2e, '
'%s, niter = %d' % (self.epsilon, norm_res, crit, niter))
print(' proj_b2: epsilon = {:.2e}, ||y-A(z)||_2 = {:.2e}, '
'{}, niter = {}'.format(self.epsilon, norm_res, crit,
niter))

return sol

0 comments on commit b020b54

Please sign in to comment.