Skip to content

Commit 413acfe

Browse files
authored
Merge pull request #210 from mrava87/fix-float_conversion_warning
Bug: changes to suppress deprecation warning on float of 1-element array
2 parents 55e69a4 + 085c74a commit 413acfe

8 files changed

Lines changed: 1281 additions & 740 deletions

File tree

‎pylops_mpi/DistributedArray.py‎

Lines changed: 582 additions & 323 deletions
Large diffs are not rendered by default.

‎pylops_mpi/optimization/cls_basic.py‎

Lines changed: 84 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import List, Optional, Tuple, Union
21
import sys
32
import time
4-
import numpy as np
3+
from typing import List, Optional, Tuple, Union
54

5+
import numpy as np
66
from pylops.optimization.basesolver import Solver
77
from pylops.utils import NDArray
88

@@ -46,7 +46,11 @@ def _print_setup(self, xcomplex: bool = False) -> None:
4646
def _print_step(self, x: Union[DistributedArray, StackedDistributedArray]) -> None:
4747
if isinstance(x, StackedDistributedArray):
4848
x = x.distarrays[0]
49-
strx = f"{x[0]:1.2e} " if np.iscomplexobj(x.local_array) else f"{x[0]:11.4e} "
49+
strx = (
50+
f"{x[0]:1.2e} "
51+
if np.iscomplexobj(x.local_array)
52+
else f"{x[0]:11.4e} "
53+
)
5054
msg = f"{self.iiter:6g} " + strx + f"{self.cost[self.iiter]:11.4e}"
5155
print(msg)
5256
sys.stdout.flush()
@@ -55,12 +59,12 @@ def memory_usage(self) -> None:
5559
pass
5660

5761
def setup(
58-
self,
59-
y: Union[DistributedArray, StackedDistributedArray],
60-
x0: Union[DistributedArray, StackedDistributedArray],
61-
niter: Optional[int] = None,
62-
tol: float = 1e-4,
63-
show: bool = False,
62+
self,
63+
y: Union[DistributedArray, StackedDistributedArray],
64+
x0: Union[DistributedArray, StackedDistributedArray],
65+
niter: Optional[int] = None,
66+
tol: float = 1e-4,
67+
show: bool = False,
6468
) -> Union[DistributedArray, StackedDistributedArray]:
6569
r"""Setup solver
6670
@@ -92,7 +96,7 @@ def setup(
9296
self.r = self.y - self.Op.matvec(x)
9397
self.rank = x.rank
9498
self.c = self.r.copy()
95-
self.kold = float(np.abs(self.r.dot(self.r.conj())).item())
99+
self.kold = float(np.abs(self.r.dot(self.r.conj())))
96100

97101
# create variables to track the residual norm and iterations
98102
self.cost: List = []
@@ -107,9 +111,9 @@ def setup(
107111
self._print_setup(np.iscomplexobj(x.local_array))
108112
return x
109113

110-
def step(self, x: Union[DistributedArray, StackedDistributedArray],
111-
show: bool = False
112-
) -> Union[DistributedArray, StackedDistributedArray]:
114+
def step(
115+
self, x: Union[DistributedArray, StackedDistributedArray], show: bool = False
116+
) -> Union[DistributedArray, StackedDistributedArray]:
113117
r"""Run one step of solver
114118
115119
Parameters
@@ -127,10 +131,10 @@ def step(self, x: Union[DistributedArray, StackedDistributedArray],
127131
"""
128132
Opc = self.Op.matvec(self.c)
129133
cOpc = np.abs(self.c.dot(Opc.conj()))
130-
a = float((self.kold / cOpc).item())
134+
a = float(self.kold / cOpc)
131135
x += a * self.c
132136
self.r -= a * Opc
133-
k = float(np.abs(self.r.dot(self.r.conj())).item())
137+
k = float(np.abs(self.r.dot(self.r.conj())))
134138
b = float(k / self.kold)
135139
self.c = self.r + b * self.c
136140
self.kold = k
@@ -141,11 +145,11 @@ def step(self, x: Union[DistributedArray, StackedDistributedArray],
141145
return x
142146

143147
def run(
144-
self,
145-
x: Union[DistributedArray, StackedDistributedArray],
146-
niter: Optional[int] = None,
147-
show: bool = False,
148-
itershow: Tuple[int, int, int] = (10, 10, 10),
148+
self,
149+
x: Union[DistributedArray, StackedDistributedArray],
150+
niter: Optional[int] = None,
151+
show: bool = False,
152+
itershow: Tuple[int, int, int] = (10, 10, 10),
149153
) -> Union[DistributedArray, StackedDistributedArray]:
150154
r"""Run solver
151155
@@ -205,13 +209,13 @@ def finalize(self, show: bool = False) -> None:
205209
self._print_finalize(nbar=55)
206210

207211
def solve(
208-
self,
209-
y: Union[DistributedArray, StackedDistributedArray],
210-
x0: Union[DistributedArray, StackedDistributedArray],
211-
niter: int = 10,
212-
tol: float = 1e-4,
213-
show: bool = False,
214-
itershow: Tuple[int, int, int] = (10, 10, 10),
212+
self,
213+
y: Union[DistributedArray, StackedDistributedArray],
214+
x0: Union[DistributedArray, StackedDistributedArray],
215+
niter: int = 10,
216+
tol: float = 1e-4,
217+
show: bool = False,
218+
itershow: Tuple[int, int, int] = (10, 10, 10),
215219
) -> Tuple[Union[DistributedArray, StackedDistributedArray], int, NDArray]:
216220
r"""Run entire solver
217221
@@ -293,7 +297,11 @@ def _print_setup(self, xcomplex: bool = False) -> None:
293297
def _print_step(self, x: Union[DistributedArray, StackedDistributedArray]) -> None:
294298
if isinstance(x, StackedDistributedArray):
295299
x = x.distarrays[0]
296-
strx = f"{x[0]:1.2e} " if np.iscomplexobj(x.local_array) else f"{x[0]:11.4e} "
300+
strx = (
301+
f"{x[0]:1.2e} "
302+
if np.iscomplexobj(x.local_array)
303+
else f"{x[0]:11.4e} "
304+
)
297305
msg = (
298306
f"{self.iiter:6g} "
299307
+ strx
@@ -305,14 +313,15 @@ def _print_step(self, x: Union[DistributedArray, StackedDistributedArray]) -> No
305313
def memory_usage(self) -> None:
306314
pass
307315

308-
def setup(self,
309-
y: Union[DistributedArray, StackedDistributedArray],
310-
x0: Union[DistributedArray, StackedDistributedArray],
311-
niter: Optional[int] = None,
312-
damp: float = 0.0,
313-
tol: float = 1e-4,
314-
show: bool = False,
315-
) -> Union[DistributedArray, StackedDistributedArray]:
316+
def setup(
317+
self,
318+
y: Union[DistributedArray, StackedDistributedArray],
319+
x0: Union[DistributedArray, StackedDistributedArray],
320+
niter: Optional[int] = None,
321+
damp: float = 0.0,
322+
tol: float = 1e-4,
323+
show: bool = False,
324+
) -> Union[DistributedArray, StackedDistributedArray]:
316325
r"""Setup solver
317326
318327
Parameters
@@ -338,7 +347,7 @@ def setup(self,
338347
339348
"""
340349
self.y = y
341-
self.damp = damp ** 2
350+
self.damp = damp**2
342351
self.tol = tol
343352
self.niter = niter
344353

@@ -349,13 +358,15 @@ def setup(self,
349358
self.rank = x.rank
350359
self.c = r.copy()
351360
self.q = self.Op.matvec(self.c)
352-
self.kold = float(np.abs(r.dot(r.conj())).item())
361+
self.kold = float(np.abs(r.dot(r.conj())))
353362

354363
# create variables to track the residual norm and iterations
355364
self.cost = []
356365
self.cost1 = []
357-
self.cost.append(float(self.s.norm().item()))
358-
self.cost1.append(np.sqrt(float(self.cost[0] ** 2 + damp * np.abs(x.dot(x.conj())).item())))
366+
self.cost.append(float(self.s.norm()))
367+
self.cost1.append(
368+
np.sqrt(float(self.cost[0] ** 2 + damp * np.abs(x.dot(x.conj()))))
369+
)
359370
self.iiter = 0
360371

361372
# print setup
@@ -367,9 +378,9 @@ def setup(self,
367378
self._print_setup(np.iscomplexobj(x.local_array))
368379
return x
369380

370-
def step(self, x: Union[DistributedArray, StackedDistributedArray],
371-
show: bool = False
372-
) -> Union[DistributedArray, StackedDistributedArray]:
381+
def step(
382+
self, x: Union[DistributedArray, StackedDistributedArray], show: bool = False
383+
) -> Union[DistributedArray, StackedDistributedArray]:
373384
r"""Run one step of solver
374385
375386
Parameters
@@ -386,28 +397,39 @@ def step(self, x: Union[DistributedArray, StackedDistributedArray],
386397
387398
"""
388399

389-
a = float(np.abs(self.kold / (self.q.dot(self.q.conj()) + self.damp * self.c.dot(self.c.conj()))).item())
400+
a = float(
401+
np.abs(
402+
self.kold
403+
/ (self.q.dot(self.q.conj()) + self.damp * self.c.dot(self.c.conj()))
404+
)
405+
)
390406
x += a * self.c
391407
self.s -= a * self.q
392408
damped_x = self.damp * x
393409
r = self.Op.rmatvec(self.s) - damped_x
394-
k = float(np.abs(r.dot(r.conj())).item())
410+
k = float(np.abs(r.dot(r.conj())))
395411
b = float(k / self.kold)
396412
self.c = r + b * self.c
397413
self.q = self.Op.matvec(self.c)
398414
self.kold = k
399415
self.iiter += 1
400-
self.cost.append(float(self.s.norm().item()))
401-
self.cost1.append(np.sqrt(float(self.cost[self.iiter] ** 2 + self.damp * np.abs(x.dot(x.conj())).item())))
416+
self.cost.append(float(self.s.norm()))
417+
self.cost1.append(
418+
np.sqrt(
419+
float(self.cost[self.iiter] ** 2 + self.damp * np.abs(x.dot(x.conj())))
420+
)
421+
)
402422
if show and self.rank == 0:
403423
self._print_step(x)
404424
return x
405425

406-
def run(self,
407-
x: Union[DistributedArray, StackedDistributedArray],
408-
niter: Optional[int] = None,
409-
show: bool = False,
410-
itershow: Tuple[int, int, int] = (10, 10, 10), ) -> Union[DistributedArray, StackedDistributedArray]:
426+
def run(
427+
self,
428+
x: Union[DistributedArray, StackedDistributedArray],
429+
niter: Optional[int] = None,
430+
show: bool = False,
431+
itershow: Tuple[int, int, int] = (10, 10, 10),
432+
) -> Union[DistributedArray, StackedDistributedArray]:
411433
r"""Run solver
412434
413435
Parameters
@@ -468,15 +490,16 @@ def finalize(self, show: bool = False, **kwargs) -> None:
468490
self._print_finalize(nbar=65)
469491
self.cost = np.array(self.cost)
470492

471-
def solve(self,
472-
y: Union[DistributedArray, StackedDistributedArray],
473-
x0: Union[DistributedArray, StackedDistributedArray],
474-
niter: int = 10,
475-
damp: float = 0.0,
476-
tol: float = 1e-4,
477-
show: bool = False,
478-
itershow: Tuple[int, int, int] = (10, 10, 10),
479-
) -> Tuple[DistributedArray, int, int, float, float, NDArray]:
493+
def solve(
494+
self,
495+
y: Union[DistributedArray, StackedDistributedArray],
496+
x0: Union[DistributedArray, StackedDistributedArray],
497+
niter: int = 10,
498+
damp: float = 0.0,
499+
tol: float = 1e-4,
500+
show: bool = False,
501+
itershow: Tuple[int, int, int] = (10, 10, 10),
502+
) -> Tuple[DistributedArray, int, int, float, float, NDArray]:
480503
r"""Run entire solver
481504
482505
Parameters

0 commit comments

Comments
 (0)