Skip to content

Commit 824100d

Browse files
committed
doc: added Literal to optimization
1 parent 1148f12 commit 824100d

File tree

7 files changed

+84
-68
lines changed

7 files changed

+84
-68
lines changed

pylops/optimization/basesolver.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import TYPE_CHECKING, Any
88

99
from pylops.optimization.callback import Callbacks
10-
from pylops.utils.typing import NDArray
10+
from pylops.utils.typing import NDArray, Tmemunit
1111

1212
if TYPE_CHECKING:
1313
from pylops.linearoperator import LinearOperator
@@ -157,11 +157,11 @@ def _setpreallocate(self, preallocate: bool) -> None:
157157
)
158158

159159
@abstractmethod
160-
def memory_usage(
161-
self,
162-
show: bool = False,
163-
unit: str = "B",
164-
) -> float:
160+
def memory_usage(
161+
self,
162+
show: bool = False,
163+
unit: Tmemunit = "B",
164+
) -> float:
165165
"""Compute memory usage of the solver
166166
167167
This method computes an estimate of the memory required by the solver given

pylops/optimization/cls_basic.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
to_numpy,
1818
to_numpy_conditional,
1919
)
20-
from pylops.utils.typing import NDArray
20+
from pylops.utils.typing import NDArray, Tmemunit
2121

2222
if TYPE_CHECKING:
2323
from pylops.linearoperator import LinearOperator
@@ -94,11 +94,11 @@ def _print_step(self, x: NDArray) -> None:
9494
msg = f"{self.iiter:6g} " + strx + f"{self.cost[self.iiter]:11.4e}"
9595
print(msg)
9696

97-
def memory_usage(
98-
self,
99-
show: bool = False,
100-
unit: str = "B",
101-
) -> float:
97+
def memory_usage(
98+
self,
99+
show: bool = False,
100+
unit: Tmemunit = "B",
101+
) -> float:
102102
"""Compute memory usage of the solver
103103
104104
Parameters
@@ -460,11 +460,11 @@ def _print_step(self, x: NDArray) -> None:
460460
)
461461
print(msg)
462462

463-
def memory_usage(
464-
self,
465-
show: bool = False,
466-
unit: str = "B",
467-
) -> float:
463+
def memory_usage(
464+
self,
465+
show: bool = False,
466+
unit: Tmemunit = "B",
467+
) -> float:
468468
"""Compute memory usage of the solver
469469
470470
Parameters
@@ -980,11 +980,11 @@ def _print_finalize(self) -> None:
980980
print(str5)
981981
print("-" * 90 + "\n")
982982

983-
def memory_usage(
984-
self,
985-
show: bool = False,
986-
unit: str = "B",
987-
) -> float:
983+
def memory_usage(
984+
self,
985+
show: bool = False,
986+
unit: Tmemunit = "B",
987+
) -> float:
988988
"""Compute memory usage of the solver
989989
990990
Parameters

pylops/optimization/cls_leastsquares.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pylops.optimization.basesolver import Solver, _units
1616
from pylops.optimization.basic import cg, cgls
1717
from pylops.utils.backend import get_array_module
18-
from pylops.utils.typing import NDArray
18+
from pylops.utils.typing import NDArray, Tmemunit, Tsolverengine
1919

2020
if TYPE_CHECKING:
2121
from pylops.linearoperator import LinearOperator
@@ -104,7 +104,7 @@ def memory_usage(
104104
self,
105105
nopRegs: Optional[Tuple[int]] = None,
106106
show: bool = False,
107-
unit: str = "B",
107+
unit: Tmemunit = "B",
108108
) -> float:
109109
"""Compute memory usage of the solver
110110
@@ -256,7 +256,7 @@ def step(self) -> None:
256256
def run(
257257
self,
258258
x: NDArray,
259-
engine: str = "scipy",
259+
engine: Tsolverengine = "scipy",
260260
show: bool = False,
261261
**kwargs_solver,
262262
) -> Tuple[NDArray, int]:
@@ -324,7 +324,7 @@ def solve(
324324
epsRs: Optional[Sequence[float]] = None,
325325
NRegs: Optional[Sequence["LinearOperator"]] = None,
326326
epsNRs: Optional[Sequence[float]] = None,
327-
engine: str = "scipy",
327+
engine: Tsolverengine = "scipy",
328328
show: bool = False,
329329
**kwargs_solver,
330330
) -> Tuple[NDArray, int]:
@@ -521,7 +521,7 @@ def memory_usage(
521521
self,
522522
nopRegs: Optional[Tuple[int]] = None,
523523
show: bool = False,
524-
unit: str = "B",
524+
unit: Tmemunit = "B",
525525
) -> float:
526526
"""Compute memory usage of the solver
527527
@@ -659,7 +659,7 @@ def step(self) -> None:
659659
def run(
660660
self,
661661
x: NDArray,
662-
engine: str = "scipy",
662+
engine: Tsolverengine = "scipy",
663663
show: bool = False,
664664
**kwargs_solver,
665665
) -> Tuple[NDArray, int, int, float, float]:
@@ -730,7 +730,7 @@ def solve(
730730
Weight: Optional["LinearOperator"] = None,
731731
dataregs: Optional[Sequence[NDArray]] = None,
732732
epsRs: Optional[Sequence[float]] = None,
733-
engine: str = "scipy",
733+
engine: Tsolverengine = "scipy",
734734
show: bool = False,
735735
**kwargs_solver,
736736
) -> Tuple[NDArray, int, int, float, float]:
@@ -848,7 +848,7 @@ def _print_finalize(self) -> None:
848848
def memory_usage(
849849
self,
850850
show: bool = False,
851-
unit: str = "B",
851+
unit: Tmemunit = "B",
852852
) -> float:
853853
"""Compute memory usage of the solver
854854
@@ -930,7 +930,7 @@ def step(self) -> None:
930930
def run(
931931
self,
932932
x: NDArray,
933-
engine: str = "scipy",
933+
engine: Tsolverengine = "scipy",
934934
show: bool = False,
935935
**kwargs_solver,
936936
) -> Tuple[NDArray, int, int, float, float]:
@@ -1004,7 +1004,7 @@ def solve(
10041004
y: NDArray,
10051005
P: "LinearOperator",
10061006
x0: Optional[NDArray] = None,
1007-
engine: str = "scipy",
1007+
engine: Tsolverengine = "scipy",
10081008
show: bool = False,
10091009
**kwargs_solver,
10101010
) -> Tuple[NDArray, int, int, float, float]:

pylops/optimization/cls_sparsity.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
get_real_dtype,
3030
inplace_set,
3131
)
32-
from pylops.utils.typing import InputDimsLike, NDArray, SamplingLike
32+
from pylops.utils.typing import (
33+
InputDimsLike,
34+
NDArray,
35+
SamplingLike,
36+
Tirlskind,
37+
Tmemunit,
38+
Tsolverengine,
39+
Tthreshkind,
40+
)
3341

3442
spgl1_message = deps.spgl1_import("the spgl1 solver")
3543

@@ -363,9 +371,9 @@ def _print_step(self, x: NDArray) -> None:
363371

364372
def memory_usage(
365373
self,
366-
kind: str = "data",
374+
kind: Tirlskind = "data",
367375
show: bool = False,
368-
unit: str = "B",
376+
unit: Tmemunit = "B",
369377
) -> float:
370378
"""Compute memory usage of the solver
371379
@@ -415,7 +423,7 @@ def setup(
415423
epsI: float = 1e-10,
416424
tolIRLS: float = 1e-10,
417425
warm: bool = False,
418-
kind: str = "data",
426+
kind: Tirlskind = "data",
419427
preallocate: bool = False,
420428
show: bool = False,
421429
) -> None:
@@ -498,7 +506,9 @@ def setup(
498506
if show:
499507
self._print_setup()
500508

501-
def _step_data(self, x: NDArray, engine: str = "scipy", **kwargs_solver) -> NDArray:
509+
def _step_data(
510+
self, x: NDArray, engine: Tsolverengine = "scipy", **kwargs_solver
511+
) -> NDArray:
502512
r"""Run one step of solver with L1 data term"""
503513
# add preallocate to keywords of solver
504514
if self.preallocate and (engine == "pylops" or self.ncp != np):
@@ -552,7 +562,7 @@ def _step_data(self, x: NDArray, engine: str = "scipy", **kwargs_solver) -> NDAr
552562
return x
553563

554564
def _step_model(
555-
self, x: NDArray, engine: str = "scipy", **kwargs_solver
565+
self, x: NDArray, engine: Tsolverengine = "scipy", **kwargs_solver
556566
) -> NDArray:
557567
r"""Run one step of solver with L1 model term"""
558568
# add preallocate to keywords of solver
@@ -615,7 +625,7 @@ def _step_model(
615625
def step(
616626
self,
617627
x: NDArray,
618-
engine: str = "scipy",
628+
engine: Tsolverengine = "scipy",
619629
show: bool = False,
620630
**kwargs_solver,
621631
) -> NDArray:
@@ -666,7 +676,7 @@ def run(
666676
self,
667677
x: Optional[NDArray],
668678
nouter: int = 10,
669-
engine: str = "scipy",
679+
engine: Tsolverengine = "scipy",
670680
show: bool = False,
671681
itershow: Tuple[int, int, int] = (10, 10, 10),
672682
**kwargs_solver,
@@ -760,9 +770,9 @@ def solve(
760770
epsR: float = 1e-10,
761771
epsI: float = 1e-10,
762772
tolIRLS: float = 1e-10,
763-
kind: str = "data",
773+
kind: Tirlskind = "data",
764774
warm: bool = False,
765-
engine: str = "scipy",
775+
engine: Tsolverengine = "scipy",
766776
preallocate: bool = False,
767777
show: bool = False,
768778
itershow: Tuple[int, int, int] = (10, 10, 10),
@@ -969,7 +979,7 @@ def _print_step(self, x: NDArray) -> None:
969979
def memory_usage(
970980
self,
971981
show: bool = False,
972-
unit: str = "B",
982+
unit: Tmemunit = "B",
973983
) -> float:
974984
"""Compute memory usage of the solver
975985
@@ -1085,7 +1095,7 @@ def step(
10851095
self,
10861096
x: NDArray,
10871097
cols: InputDimsLike,
1088-
engine: str = "scipy",
1098+
engine: Tsolverengine = "scipy",
10891099
show: bool = False,
10901100
**kwargs_solver,
10911101
) -> NDArray:
@@ -1208,7 +1218,7 @@ def run(
12081218
self,
12091219
x: NDArray,
12101220
cols: InputDimsLike,
1211-
engine: str = "scipy",
1221+
engine: Tsolverengine = "scipy",
12121222
show: bool = False,
12131223
itershow: Tuple[int, int, int] = (10, 10, 10),
12141224
) -> Tuple[NDArray, InputDimsLike]:
@@ -1302,7 +1312,7 @@ def solve(
13021312
normalizecols: bool = False,
13031313
Opbasis: Optional["LinearOperator"] = None,
13041314
optimal_coeff: bool = False,
1305-
engine: str = "scipy",
1315+
engine: Tsolverengine = "scipy",
13061316
preallocate: bool = False,
13071317
show: bool = False,
13081318
itershow: Tuple[int, int, int] = (10, 10, 10),
@@ -1549,7 +1559,7 @@ def _print_step(
15491559
def memory_usage(
15501560
self,
15511561
show: bool = False,
1552-
unit: str = "B",
1562+
unit: Tmemunit = "B",
15531563
) -> float:
15541564
"""Compute memory usage of the solver
15551565
@@ -1591,7 +1601,7 @@ def setup(
15911601
alpha: Optional[float] = None,
15921602
eigsdict: Optional[Dict[str, Any]] = None,
15931603
tol: float = 1e-10,
1594-
threshkind: str = "soft",
1604+
threshkind: Tthreshkind = "soft",
15951605
perc: Optional[float] = None,
15961606
decay: Optional[NDArray] = None,
15971607
monitorres: bool = False,
@@ -1983,7 +1993,7 @@ def solve(
19831993
alpha: Optional[float] = None,
19841994
eigsdict: Optional[Dict[str, Any]] = None,
19851995
tol: float = 1e-10,
1986-
threshkind: str = "soft",
1996+
threshkind: Tthreshkind = "soft",
19871997
perc: Optional[float] = None,
19881998
decay: Optional[NDArray] = None,
19891999
monitorres: bool = False,
@@ -2189,7 +2199,7 @@ class FISTA(ISTA):
21892199
def memory_usage(
21902200
self,
21912201
show: bool = False,
2192-
unit: str = "B",
2202+
unit: Tmemunit = "B",
21932203
) -> float:
21942204
"""Compute memory usage of the solver
21952205
@@ -2473,7 +2483,7 @@ def _print_finalize(self) -> None:
24732483
def memory_usage(
24742484
self,
24752485
show: bool = False,
2476-
unit: str = "B",
2486+
unit: Tmemunit = "B",
24772487
) -> float:
24782488
pass
24792489

@@ -2820,7 +2830,7 @@ def memory_usage(
28202830
nopRegsL1: Optional[Tuple[int]] = None,
28212831
nopRegsL2: Optional[Tuple[int]] = None,
28222832
show: bool = False,
2823-
unit: str = "B",
2833+
unit: Tmemunit = "B",
28242834
) -> float:
28252835
"""Compute memory usage of the solver
28262836
@@ -3008,7 +3018,7 @@ def setup(
30083018
def step(
30093019
self,
30103020
x: NDArray,
3011-
engine: str = "scipy",
3021+
engine: Tsolverengine = "scipy",
30123022
show: bool = False,
30133023
show_inner: bool = False,
30143024
**kwargs_solver,
@@ -3116,7 +3126,7 @@ def step(
31163126
def run(
31173127
self,
31183128
x: NDArray,
3119-
engine: str = "scipy",
3129+
engine: Tsolverengine = "scipy",
31203130
show: bool = False,
31213131
itershow: Tuple[int, int, int] = (10, 10, 10),
31223132
show_inner: bool = False,
@@ -3208,7 +3218,7 @@ def solve(
32083218
tol: float = 1e-10,
32093219
tau: float = 1.0,
32103220
restart: bool = False,
3211-
engine: str = "scipy",
3221+
engine: Tsolverengine = "scipy",
32123222
preallocate: bool = False,
32133223
show: bool = False,
32143224
itershow: Tuple[int, int, int] = (10, 10, 10),

pylops/optimization/eigs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
from pylops import LinearOperator
88
from pylops.utils.backend import get_module
9-
from pylops.utils.typing import NDArray
9+
from pylops.utils.typing import NDArray, Tbackend
1010

1111

1212
def power_iteration(
1313
Op: LinearOperator,
1414
niter: int = 10,
1515
tol: float = 1e-5,
1616
dtype: str = "float32",
17-
backend: str = "numpy",
17+
backend: Tbackend = "numpy",
1818
) -> Tuple[float, NDArray, int]:
1919
"""Power iteration algorithm.
2020

0 commit comments

Comments
 (0)