Skip to content

Commit 605b6e3

Browse files
committed
Merge branch 'main' into model-wiring-matrix-slicer
2 parents b2fe79f + cb5e49c commit 605b6e3

File tree

8 files changed

+50
-21
lines changed

8 files changed

+50
-21
lines changed

src/inversion_ideas/base/objective_function.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010

1111
import numpy as np
1212
import numpy.typing as npt
13-
from scipy.sparse import sparray
1413
from scipy.sparse.linalg import LinearOperator, aslinearoperator
1514

16-
from ..typing import Model
15+
from ..typing import Model, SparseArray
1716

1817

1918
class Objective(ABC):
@@ -51,7 +50,7 @@ def gradient(self, model: Model) -> npt.NDArray[np.float64]:
5150
@abstractmethod
5251
def hessian(
5352
self, model: Model
54-
) -> npt.NDArray[np.float64] | sparray | LinearOperator:
53+
) -> npt.NDArray[np.float64] | SparseArray | LinearOperator:
5554
"""
5655
Evaluate the hessian of the objective function for a given model.
5756
"""
@@ -150,7 +149,7 @@ def gradient(self, model: Model) -> npt.NDArray[np.float64]:
150149

151150
def hessian(
152151
self, model: Model
153-
) -> npt.NDArray[np.float64] | sparray | LinearOperator:
152+
) -> npt.NDArray[np.float64] | SparseArray | LinearOperator:
154153
"""
155154
Evaluate the hessian of the objective function for a given model.
156155
"""
@@ -243,7 +242,7 @@ def gradient(self, model: Model) -> npt.NDArray[np.float64]:
243242

244243
def hessian(
245244
self, model: Model
246-
) -> npt.NDArray[np.float64] | sparray | LinearOperator:
245+
) -> npt.NDArray[np.float64] | SparseArray | LinearOperator:
247246
"""
248247
Evaluate the hessian of the objective function for a given model.
249248
"""
@@ -362,8 +361,8 @@ def _get_n_params(functions: list) -> int:
362361

363362

364363
def _sum(
365-
operators: Iterator[npt.NDArray | sparray | LinearOperator],
366-
) -> npt.NDArray | sparray | LinearOperator:
364+
operators: Iterator[npt.NDArray | SparseArray | LinearOperator],
365+
) -> npt.NDArray | SparseArray | LinearOperator:
367366
"""
368367
Sum objects within an iterator.
369368

src/inversion_ideas/data_misfit.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import numpy as np
66
import numpy.typing as npt
7-
from scipy.sparse import dia_array, diags_array, sparray
7+
from scipy.sparse import dia_array, diags_array
88
from scipy.sparse.linalg import LinearOperator, aslinearoperator
99

1010
from .base import Objective
11-
from .typing import Model
1211
from .utils import support_model_slice
1312
from .wires import ModelSlice
13+
from .typing import Model
1414

1515

1616
class DataMisfit(Objective):
@@ -112,7 +112,7 @@ def gradient(self, model: Model) -> npt.NDArray[np.float64]:
112112
@support_model_slice
113113
def hessian(
114114
self, model: Model
115-
) -> npt.NDArray[np.float64] | sparray | LinearOperator:
115+
) -> npt.NDArray[np.float64] | SparseArray | LinearOperator:
116116
"""
117117
Hessian matrix.
118118
"""

src/inversion_ideas/minimize/_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def conjugate_gradient(
3333
Objective function to be minimized.
3434
initial_model : (n_params) array
3535
Initial model used to start the minimization.
36-
preconditioner : (n_params, n_params) array, sparray or LinearOperator or Callable, optional
36+
preconditioner : (n_params, n_params) array, sparse array or LinearOperator or Callable, optional
3737
Matrix used as preconditioner in the conjugant gradient algorithm.
3838
If None, no preconditioner will be used.
3939
A callable can be passed to build the preconditioner dynamically: such
4040
callable should take a single ``initial_model`` argument and return an
41-
array, `sparray` or a `LinearOperator`.
41+
array, sparse array or a `LinearOperator`.
4242
kwargs : dict
4343
Extra arguments that will be passed to the :func:`scipy.sparse.linalg.cg`
4444
function.

src/inversion_ideas/preconditioners.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Classes and functions to build preconditioners.
33
"""
44

5-
from scipy.sparse import diags_array, sparray
5+
from scipy.sparse import diags_array
66

77
from .base import Objective
8-
from .typing import Model
8+
from .typing import Model, SparseArray
99

1010

1111
class JacobiPreconditioner:
@@ -30,7 +30,7 @@ class JacobiPreconditioner:
3030
def __init__(self, objective_function: Objective):
3131
self.objective_function = objective_function
3232

33-
def __call__(self, model: Model) -> sparray:
33+
def __call__(self, model: Model) -> SparseArray:
3434
"""
3535
Generate a Jacobi preconditioner as a sparse diagonal array for a given model.
3636

src/inversion_ideas/recipes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def create_l2_inversion(
6868
no limit on the total amount of iterations.
6969
cache_models : bool, optional
7070
Whether to cache models after each iteration in the inversion.
71-
preconditioner : {"jacobi"} or 2d array or sparray or LinearOperator or callable or None, optional
71+
preconditioner : {"jacobi"} or 2d array or sparse array or LinearOperator or callable or None, optional
7272
Preconditioner that will be passed to the ``minimizer`` on every call during the
7373
inversion. The preconditioner can be a predefined 2d array, a sparse array or
7474
a LinearOperator. Alternatively, it can be a callable that takes the ``model``
@@ -184,7 +184,7 @@ def create_sparse_inversion(
184184
no limit on the total amount of iterations.
185185
cache_models : bool, optional
186186
Whether to cache models after each iteration in the inversion.
187-
preconditioner : {"jacobi"} or 2d array or sparray or LinearOperator or callable or None, optional
187+
preconditioner : {"jacobi"} or 2d array or sparse array or LinearOperator or callable or None, optional
188188
Preconditioner that will be passed to the ``minimizer`` on every call during the
189189
inversion. The preconditioner can be a predefined 2d array, a sparse array or
190190
a LinearOperator. Alternatively, it can be a callable that takes the ``model``

src/inversion_ideas/typing.py

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

77
import numpy as np
88
import numpy.typing as npt
9-
from scipy.sparse import sparray
9+
from scipy.sparse import (
10+
bsr_array,
11+
bsr_matrix,
12+
coo_array,
13+
coo_matrix,
14+
csc_array,
15+
csc_matrix,
16+
csr_array,
17+
csr_matrix,
18+
dia_array,
19+
dia_matrix,
20+
)
1021
from scipy.sparse.linalg import LinearOperator
1122

23+
SparseArray: TypeAlias = (
24+
bsr_array
25+
| bsr_matrix
26+
| coo_array
27+
| coo_matrix
28+
| csc_array
29+
| csc_matrix
30+
| csr_array
31+
| csr_matrix
32+
| dia_array
33+
| dia_matrix
34+
)
35+
"""
36+
Type alias to represent sparse arrays.
37+
"""
38+
1239
Model: TypeAlias = npt.NDArray[np.float64]
1340
"""
1441
Type alias to represent models in the inversion framework as 1D arrays.
1542
"""
1643

17-
Preconditioner: TypeAlias = npt.NDArray[np.float64] | sparray | LinearOperator
44+
45+
Preconditioner: TypeAlias = npt.NDArray[np.float64] | SparseArray | LinearOperator
1846
"""
1947
Type for static preconditioners.
2048

src/inversion_ideas/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
import numpy as np
1010
import numpy.typing as npt
11-
from scipy.sparse import sparray
11+
12+
from .typing import SparseArray
1213

1314
from inversion_ideas.wires import ModelSlice
1415

@@ -123,7 +124,7 @@ def wrapper(self, model, *args, **kwargs):
123124
def get_sensitivity_weights(
124125
jacobian: npt.NDArray[np.float64],
125126
*,
126-
data_weights: npt.NDArray[np.float64] | sparray | None = None,
127+
data_weights: npt.NDArray[np.float64] | SparseArray | None = None,
127128
volumes: npt.NDArray[np.float64] | None = None,
128129
vmin: float | None = 1e-12,
129130
):

tests/test_objective_functions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Test operations for objective functions.
33
"""
4+
45
import pytest
56
import numpy as np
67
from scipy.sparse import diags_array, sparray

0 commit comments

Comments
 (0)