loop-interpolation 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- loop_interpolation/__init__.py +152 -0
- loop_interpolation/_builders.py +149 -0
- loop_interpolation/_constant_norm.py +232 -0
- loop_interpolation/_diagnostics.py +102 -0
- loop_interpolation/_discrete_fold_interpolator.py +242 -0
- loop_interpolation/_discrete_interpolator.py +1405 -0
- loop_interpolation/_fd_fold_interpolator.py +237 -0
- loop_interpolation/_finite_difference_interpolator.py +1348 -0
- loop_interpolation/_fold_event.py +185 -0
- loop_interpolation/_fold_norm_alignment.py +118 -0
- loop_interpolation/_fold_setup.py +39 -0
- loop_interpolation/_geological_interpolator.py +920 -0
- loop_interpolation/_interpolator_builder.py +251 -0
- loop_interpolation/_interpolator_factory.py +167 -0
- loop_interpolation/_interpolatortype.py +20 -0
- loop_interpolation/_operator.py +44 -0
- loop_interpolation/_p1interpolator.py +299 -0
- loop_interpolation/_p2interpolator.py +317 -0
- loop_interpolation/_regularisation.py +110 -0
- loop_interpolation/_solver_pipeline.py +138 -0
- loop_interpolation/_solver_strategy.py +218 -0
- loop_interpolation/_surfe_wrapper.py +211 -0
- loop_interpolation/_svariogram.py +133 -0
- loop_interpolation/_validation.py +604 -0
- loop_interpolation/constraints.py +473 -0
- loop_interpolation/fold_function/__init__.py +39 -0
- loop_interpolation/fold_function/_base_fold_rotation_angle.py +219 -0
- loop_interpolation/fold_function/_fourier_series_fold_rotation_angle.py +131 -0
- loop_interpolation/fold_function/_lambda_fold_rotation_angle.py +53 -0
- loop_interpolation/loopsolver/__init__.py +2 -0
- loop_interpolation/loopsolver/admm_constant_norm.py +118 -0
- loop_interpolation/loopsolver/admm_method.py +25 -0
- loop_interpolation/loopsolver/admm_solver.py +463 -0
- loop_interpolation/loopsolver/version.py +1 -0
- loop_interpolation-0.0.1.dist-info/METADATA +34 -0
- loop_interpolation-0.0.1.dist-info/RECORD +39 -0
- loop_interpolation-0.0.1.dist-info/WHEEL +5 -0
- loop_interpolation-0.0.1.dist-info/licenses/LICENSE +21 -0
- loop_interpolation-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import inspect
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
from scipy.sparse import csr_matrix, diags, vstack
|
|
9
|
+
from scipy.sparse.linalg import LinearOperator, cg, lsmr, lsqr
|
|
10
|
+
|
|
11
|
+
from .admm_method import ADMM
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class Config:
|
|
16
|
+
verbose: bool = False
|
|
17
|
+
progress: bool = True
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
progressbar = lambda x: x
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _normal_equations_operator(matrix: csr_matrix) -> LinearOperator:
|
|
24
|
+
"""Return a LinearOperator representing ``matrix.T @ matrix`` (the Gram/normal-
|
|
25
|
+
equations matrix) without ever materializing that product.
|
|
26
|
+
|
|
27
|
+
CG only ever needs the *action* of the normal-equations matrix on a vector,
|
|
28
|
+
so we compose the two matvecs (``matrix @ x`` then ``matrix.T @ (...)``)
|
|
29
|
+
instead of forming the (denser, fill-in prone) sparse-sparse product.
|
|
30
|
+
"""
|
|
31
|
+
n = matrix.shape[1]
|
|
32
|
+
|
|
33
|
+
def matvec(x):
|
|
34
|
+
return matrix.T @ (matrix @ x)
|
|
35
|
+
|
|
36
|
+
return LinearOperator((n, n), matvec=matvec, rmatvec=matvec, dtype=float)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _normal_equations_diagonal(matrix: csr_matrix) -> np.ndarray:
|
|
40
|
+
"""Compute ``diag(matrix.T @ matrix)`` directly as a column sum of squares,
|
|
41
|
+
i.e. ``diag[j] = sum_i matrix[i, j] ** 2``, without materializing the full
|
|
42
|
+
matrix product.
|
|
43
|
+
"""
|
|
44
|
+
return np.asarray(matrix.multiply(matrix).sum(axis=0), dtype=float).ravel()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
tqdm_module = importlib.import_module("tqdm")
|
|
49
|
+
|
|
50
|
+
if Config.progress:
|
|
51
|
+
progressbar = tqdm_module.tqdm
|
|
52
|
+
else:
|
|
53
|
+
progressbar = lambda x: x
|
|
54
|
+
except ModuleNotFoundError:
|
|
55
|
+
Config.progress = False
|
|
56
|
+
progressbar = lambda x: x
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def admm_solve(
|
|
60
|
+
A: csr_matrix,
|
|
61
|
+
b: np.ndarray,
|
|
62
|
+
Q: csr_matrix,
|
|
63
|
+
bounds: np.ndarray,
|
|
64
|
+
x0: np.ndarray,
|
|
65
|
+
admm_weight: float = 0.1,
|
|
66
|
+
nmajor=200,
|
|
67
|
+
linsys_solver_kwargs=None,
|
|
68
|
+
linsys_solver="lsmr",
|
|
69
|
+
reuse_inner_solve: bool = True,
|
|
70
|
+
adaptive_rho: bool = False,
|
|
71
|
+
adaptive_rho_mu: float = 10.0,
|
|
72
|
+
adaptive_rho_tau: float = 2.0,
|
|
73
|
+
adaptive_rho_min: float = 1e-4,
|
|
74
|
+
adaptive_rho_max: float = 1e3,
|
|
75
|
+
admm_weight_final: float | None = None,
|
|
76
|
+
admm_weight_schedule: str = "geometric",
|
|
77
|
+
active_set: bool = False,
|
|
78
|
+
active_set_padding: float = 1e-3,
|
|
79
|
+
active_set_min_size: int = 0,
|
|
80
|
+
active_set_max_size: int = 0,
|
|
81
|
+
active_set_hysteresis: bool = True,
|
|
82
|
+
active_set_refresh_interval: int = 1,
|
|
83
|
+
cg_preconditioner: bool = True,
|
|
84
|
+
cg_preconditioner_shift: float = 1e-12,
|
|
85
|
+
matrix_free: bool = False,
|
|
86
|
+
inner_rtol_start: float | None = None,
|
|
87
|
+
inner_rtol_end: float | None = None,
|
|
88
|
+
inner_atol_start: float | None = None,
|
|
89
|
+
inner_atol_end: float | None = None,
|
|
90
|
+
inner_maxiter_start: int | None = None,
|
|
91
|
+
inner_maxiter_end: int | None = None,
|
|
92
|
+
inner_maxiter_schedule: str = "linear",
|
|
93
|
+
admm_abs_tol: float = 1e-4,
|
|
94
|
+
admm_rel_tol: float = 1e-3,
|
|
95
|
+
model_update_tol: float = 0.0,
|
|
96
|
+
min_iterations: int = 5,
|
|
97
|
+
return_history: bool = False,
|
|
98
|
+
):
|
|
99
|
+
if A.shape[1] != x0.shape[0]:
|
|
100
|
+
raise ValueError("Number of columns in interpolation matrix does not match x0")
|
|
101
|
+
if A.shape[1] != Q.shape[1]:
|
|
102
|
+
raise ValueError(
|
|
103
|
+
"Number of columns in interpolation matrix and inequality matrix are different "
|
|
104
|
+
)
|
|
105
|
+
if Q.shape[0] != bounds.shape[0]:
|
|
106
|
+
raise ValueError("Number of rows in inequality matrix and bounds are different")
|
|
107
|
+
if bounds.shape[1] == 2:
|
|
108
|
+
bounds = np.hstack([bounds, np.ones((bounds.shape[0], 1))])
|
|
109
|
+
if bounds.shape[1] != 3:
|
|
110
|
+
raise ValueError("Bounds must have two columns")
|
|
111
|
+
if A.shape[0] != b.shape[0]:
|
|
112
|
+
raise ValueError("Number of rows in interpolation matrix and b are different")
|
|
113
|
+
n_ie = bounds.shape[0]
|
|
114
|
+
qx_val = np.zeros((Q.shape[0], 1))
|
|
115
|
+
model = np.zeros(A.shape[1])
|
|
116
|
+
model[:] = x0[:]
|
|
117
|
+
# initialise the admm method, sets up the u and v matrices as 0s
|
|
118
|
+
admm_method = ADMM(n_ie)
|
|
119
|
+
b0 = np.zeros(b.shape)
|
|
120
|
+
b0[:] = b[:]
|
|
121
|
+
# the b vector used for the lsqr soln is the size of A + Q
|
|
122
|
+
b = np.zeros(A.shape[0] + Q.shape[0])
|
|
123
|
+
A_size = A.shape[0]
|
|
124
|
+
xmin = bounds[:, [0]]
|
|
125
|
+
xmax = bounds[:, [1]]
|
|
126
|
+
x0_ADMM = np.zeros(Q.shape[0])
|
|
127
|
+
Q_base = Q.tocsr().copy()
|
|
128
|
+
if linsys_solver_kwargs is None:
|
|
129
|
+
linsys_solver_kwargs = {}
|
|
130
|
+
solver_name = linsys_solver.lower() if isinstance(linsys_solver, str) else linsys_solver
|
|
131
|
+
|
|
132
|
+
nmajor = int(nmajor)
|
|
133
|
+
if nmajor <= 0:
|
|
134
|
+
raise ValueError("nmajor must be positive")
|
|
135
|
+
if model_update_tol < 0:
|
|
136
|
+
raise ValueError("model_update_tol must be >= 0")
|
|
137
|
+
if inner_maxiter_schedule not in ("linear", "geometric"):
|
|
138
|
+
raise ValueError("inner_maxiter_schedule must be 'linear' or 'geometric'")
|
|
139
|
+
|
|
140
|
+
if admm_weight_final is not None:
|
|
141
|
+
if admm_weight_final <= 0:
|
|
142
|
+
raise ValueError("admm_weight_final must be > 0")
|
|
143
|
+
if admm_weight <= 0:
|
|
144
|
+
raise ValueError("admm_weight must be > 0")
|
|
145
|
+
if admm_weight_schedule == "linear":
|
|
146
|
+
rho_schedule = np.linspace(float(admm_weight), float(admm_weight_final), nmajor)
|
|
147
|
+
else:
|
|
148
|
+
rho_schedule = np.geomspace(float(admm_weight), float(admm_weight_final), nmajor)
|
|
149
|
+
else:
|
|
150
|
+
rho_schedule = np.full(nmajor, float(admm_weight), dtype=float)
|
|
151
|
+
|
|
152
|
+
adaptive_rho_enabled = bool(adaptive_rho) and admm_weight_final is None
|
|
153
|
+
|
|
154
|
+
def _schedule(start, end, n, geometric=False):
|
|
155
|
+
if start is None or end is None:
|
|
156
|
+
return None
|
|
157
|
+
if geometric:
|
|
158
|
+
if start <= 0 or end <= 0:
|
|
159
|
+
raise ValueError("Geometric schedule endpoints must be > 0")
|
|
160
|
+
return np.geomspace(float(start), float(end), n)
|
|
161
|
+
return np.linspace(float(start), float(end), n)
|
|
162
|
+
|
|
163
|
+
def _schedule_int(start, end, n, geometric=False):
|
|
164
|
+
if start is None and end is None:
|
|
165
|
+
return None
|
|
166
|
+
if start is None:
|
|
167
|
+
start = end
|
|
168
|
+
if end is None:
|
|
169
|
+
end = start
|
|
170
|
+
start = int(start)
|
|
171
|
+
end = int(end)
|
|
172
|
+
if start <= 0 or end <= 0:
|
|
173
|
+
raise ValueError("inner_maxiter_start/end must be positive when provided")
|
|
174
|
+
if geometric:
|
|
175
|
+
vals = np.geomspace(float(start), float(end), n)
|
|
176
|
+
else:
|
|
177
|
+
vals = np.linspace(float(start), float(end), n)
|
|
178
|
+
return np.maximum(np.rint(vals).astype(int), 1)
|
|
179
|
+
|
|
180
|
+
rtol_sched = _schedule(inner_rtol_start, inner_rtol_end, nmajor, geometric=True)
|
|
181
|
+
atol_sched = _schedule(inner_atol_start, inner_atol_end, nmajor, geometric=True)
|
|
182
|
+
if rtol_sched is not None:
|
|
183
|
+
if solver_name in ("lsmr", "lsqr"):
|
|
184
|
+
linsys_solver_kwargs.setdefault("btol", rtol_sched.tolist())
|
|
185
|
+
else:
|
|
186
|
+
linsys_solver_kwargs.setdefault("rtol", rtol_sched.tolist())
|
|
187
|
+
if atol_sched is not None:
|
|
188
|
+
linsys_solver_kwargs.setdefault("atol", atol_sched.tolist())
|
|
189
|
+
maxiter_sched = _schedule_int(
|
|
190
|
+
inner_maxiter_start,
|
|
191
|
+
inner_maxiter_end,
|
|
192
|
+
nmajor,
|
|
193
|
+
geometric=(inner_maxiter_schedule == "geometric"),
|
|
194
|
+
)
|
|
195
|
+
if maxiter_sched is not None:
|
|
196
|
+
maxiter_key = "iter_lim" if solver_name == "lsqr" else "maxiter"
|
|
197
|
+
linsys_solver_kwargs.setdefault(maxiter_key, maxiter_sched.tolist())
|
|
198
|
+
|
|
199
|
+
lsmr_params = set(inspect.signature(lsmr).parameters.keys())
|
|
200
|
+
lsqr_params = set(inspect.signature(lsqr).parameters.keys())
|
|
201
|
+
cg_params = set(inspect.signature(cg).parameters.keys())
|
|
202
|
+
|
|
203
|
+
def _inject_x0_if_supported(kwargs, x0_guess, supported_params):
|
|
204
|
+
if x0_guess is None:
|
|
205
|
+
return kwargs
|
|
206
|
+
if "x0" in supported_params and "x0" not in kwargs:
|
|
207
|
+
kwargs = dict(kwargs)
|
|
208
|
+
kwargs["x0"] = x0_guess
|
|
209
|
+
return kwargs
|
|
210
|
+
|
|
211
|
+
def _active_rows(qx: np.ndarray, xmin_arr: np.ndarray, xmax_arr: np.ndarray, prev_active=None):
|
|
212
|
+
lower_gap = xmin_arr[:, 0] - qx
|
|
213
|
+
upper_gap = qx - xmax_arr[:, 0]
|
|
214
|
+
violation = np.maximum(lower_gap, 0.0) + np.maximum(upper_gap, 0.0)
|
|
215
|
+
distance_to_bounds = np.minimum(np.abs(qx - xmin_arr[:, 0]), np.abs(qx - xmax_arr[:, 0]))
|
|
216
|
+
|
|
217
|
+
active = np.logical_or(violation > 0.0, distance_to_bounds <= active_set_padding)
|
|
218
|
+
if prev_active is not None and active_set_hysteresis:
|
|
219
|
+
active = np.logical_or(active, prev_active)
|
|
220
|
+
|
|
221
|
+
if active_set_min_size > 0 and np.sum(active) < active_set_min_size:
|
|
222
|
+
score = violation + np.maximum(active_set_padding - distance_to_bounds, 0.0)
|
|
223
|
+
k = int(min(active_set_min_size, qx.shape[0]))
|
|
224
|
+
if k > 0:
|
|
225
|
+
topk = np.argpartition(score, -k)[-k:]
|
|
226
|
+
active[topk] = True
|
|
227
|
+
if active_set_max_size > 0 and np.sum(active) > active_set_max_size:
|
|
228
|
+
score = violation + np.maximum(active_set_padding - distance_to_bounds, 0.0)
|
|
229
|
+
k = int(min(active_set_max_size, qx.shape[0]))
|
|
230
|
+
keep = np.argpartition(score, -k)[-k:]
|
|
231
|
+
constrained = np.zeros_like(active, dtype=bool)
|
|
232
|
+
constrained[keep] = True
|
|
233
|
+
active = constrained
|
|
234
|
+
if np.sum(active) == 0 and qx.shape[0] > 0:
|
|
235
|
+
active[np.argmax(violation)] = True
|
|
236
|
+
return active
|
|
237
|
+
|
|
238
|
+
matrix = None
|
|
239
|
+
matrix_transpose = None
|
|
240
|
+
precomputed_lhs = None
|
|
241
|
+
precomputed_M = None
|
|
242
|
+
|
|
243
|
+
def _update_system(rho_value: float):
|
|
244
|
+
nonlocal matrix, matrix_transpose, precomputed_lhs, precomputed_M
|
|
245
|
+
Q_scaled = (Q_base * rho_value).tocsr()
|
|
246
|
+
matrix = vstack([A, Q_scaled]).tocsr()
|
|
247
|
+
matrix_transpose = matrix.T
|
|
248
|
+
precomputed_lhs = None
|
|
249
|
+
precomputed_M = None
|
|
250
|
+
if solver_name == "cg" and not active_set:
|
|
251
|
+
if matrix_free:
|
|
252
|
+
precomputed_lhs = _normal_equations_operator(matrix)
|
|
253
|
+
if cg_preconditioner:
|
|
254
|
+
diag_vals = _normal_equations_diagonal(matrix)
|
|
255
|
+
diag_vals = np.maximum(diag_vals, cg_preconditioner_shift)
|
|
256
|
+
inv_diag = 1.0 / diag_vals
|
|
257
|
+
precomputed_M = diags(inv_diag)
|
|
258
|
+
else:
|
|
259
|
+
precomputed_lhs = matrix_transpose @ matrix
|
|
260
|
+
if cg_preconditioner:
|
|
261
|
+
diag_vals = np.asarray(precomputed_lhs.diagonal(), dtype=float)
|
|
262
|
+
diag_vals = np.maximum(diag_vals, cg_preconditioner_shift)
|
|
263
|
+
inv_diag = 1.0 / diag_vals
|
|
264
|
+
precomputed_M = diags(inv_diag)
|
|
265
|
+
|
|
266
|
+
rho = float(rho_schedule[0])
|
|
267
|
+
_update_system(rho)
|
|
268
|
+
|
|
269
|
+
def _solve_linsys(system_matrix, rhs, kwargs):
|
|
270
|
+
x0_guess = kwargs.pop("_x0_guess", None)
|
|
271
|
+
if callable(solver_name):
|
|
272
|
+
try:
|
|
273
|
+
return np.asarray(
|
|
274
|
+
solver_name(system_matrix, rhs, x0=x0_guess, **kwargs), dtype=float
|
|
275
|
+
)
|
|
276
|
+
except TypeError:
|
|
277
|
+
return np.asarray(solver_name(system_matrix, rhs, **kwargs), dtype=float)
|
|
278
|
+
if solver_name == "lsmr":
|
|
279
|
+
kwargs = _inject_x0_if_supported(kwargs, x0_guess, lsmr_params)
|
|
280
|
+
res = lsmr(system_matrix, rhs, **kwargs)
|
|
281
|
+
return res[0]
|
|
282
|
+
if solver_name == "lsqr":
|
|
283
|
+
kwargs = _inject_x0_if_supported(kwargs, x0_guess, lsqr_params)
|
|
284
|
+
res = lsqr(system_matrix, rhs, **kwargs)
|
|
285
|
+
return res[0]
|
|
286
|
+
if solver_name == "cg":
|
|
287
|
+
cg_kwargs = dict(kwargs)
|
|
288
|
+
if "btol" in cg_kwargs and "rtol" not in cg_kwargs:
|
|
289
|
+
cg_kwargs["rtol"] = cg_kwargs.pop("btol")
|
|
290
|
+
cg_kwargs = _inject_x0_if_supported(cg_kwargs, x0_guess, cg_params)
|
|
291
|
+
lhs = cg_kwargs.pop("_lhs", None)
|
|
292
|
+
rhs_normal = cg_kwargs.pop("_rhs_normal", None)
|
|
293
|
+
M = cg_kwargs.pop("_M", None)
|
|
294
|
+
if lhs is None:
|
|
295
|
+
if matrix_free and not active_set:
|
|
296
|
+
lhs = _normal_equations_operator(system_matrix)
|
|
297
|
+
else:
|
|
298
|
+
lhs = system_matrix.T @ system_matrix
|
|
299
|
+
if rhs_normal is None:
|
|
300
|
+
rhs_normal = system_matrix.T @ rhs
|
|
301
|
+
if M is None and cg_preconditioner:
|
|
302
|
+
if isinstance(lhs, LinearOperator):
|
|
303
|
+
diag_vals = _normal_equations_diagonal(system_matrix)
|
|
304
|
+
else:
|
|
305
|
+
diag_vals = np.asarray(lhs.diagonal(), dtype=float)
|
|
306
|
+
diag_vals = np.maximum(diag_vals, cg_preconditioner_shift)
|
|
307
|
+
inv_diag = 1.0 / diag_vals
|
|
308
|
+
M = diags(inv_diag)
|
|
309
|
+
if M is not None and "M" not in cg_kwargs:
|
|
310
|
+
if isinstance(M, LinearOperator):
|
|
311
|
+
cg_kwargs["M"] = M
|
|
312
|
+
else:
|
|
313
|
+
cg_kwargs["M"] = M
|
|
314
|
+
dx, info = cg(lhs, rhs_normal, **cg_kwargs)
|
|
315
|
+
if info < 0:
|
|
316
|
+
raise ValueError("CG inner solve failed")
|
|
317
|
+
return dx
|
|
318
|
+
raise ValueError(
|
|
319
|
+
f"Unknown linsys_solver '{linsys_solver}'. Use one of: 'lsmr', 'lsqr', 'cg', or a callable."
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
history = []
|
|
323
|
+
inner_x0 = None
|
|
324
|
+
active_rows_prev = None
|
|
325
|
+
use_cached_normal_eq = solver_name == "cg" and not active_set
|
|
326
|
+
for k in linsys_solver_kwargs:
|
|
327
|
+
if (
|
|
328
|
+
not hasattr(linsys_solver_kwargs[k], "__len__")
|
|
329
|
+
or len(linsys_solver_kwargs[k]) != nmajor
|
|
330
|
+
):
|
|
331
|
+
linsys_solver_kwargs[k] = [linsys_solver_kwargs[k]] * nmajor
|
|
332
|
+
for _i in progressbar(range(nmajor)):
|
|
333
|
+
target_rho = float(rho_schedule[_i])
|
|
334
|
+
if target_rho != rho:
|
|
335
|
+
rho_old = rho
|
|
336
|
+
rho = target_rho
|
|
337
|
+
if rho_old > 0:
|
|
338
|
+
admm_method.u *= rho_old / rho
|
|
339
|
+
_update_system(rho)
|
|
340
|
+
|
|
341
|
+
z_prev = admm_method.z.copy()
|
|
342
|
+
# current model value
|
|
343
|
+
Mx = matrix @ model # np.dot(A, model)
|
|
344
|
+
b[:A_size] = b0[:A_size] - Mx[:A_size]
|
|
345
|
+
|
|
346
|
+
if Q.shape[0] > 0:
|
|
347
|
+
qx_val[:, 0] = Mx[A_size:,] / rho
|
|
348
|
+
x0_ADMM = admm_method.admm_method_iterate_admm_array(xmin, xmax, qx_val)
|
|
349
|
+
# print(x0_ADMM, qx_val.shape)
|
|
350
|
+
# raise Exception
|
|
351
|
+
b[A_size:] = -rho * (qx_val[:, 0] - x0_ADMM)
|
|
352
|
+
# cost_data1 = np.linalg.norm(b[:A_size])
|
|
353
|
+
# cost_data2 = np.linalg.norm(b0[A_size:])
|
|
354
|
+
# model_norm = np.linalg.norm(model)
|
|
355
|
+
if Config.verbose:
|
|
356
|
+
cost_data1 = np.linalg.norm(b[:A_size])
|
|
357
|
+
cost_data2 = np.linalg.norm(b0[:A_size])
|
|
358
|
+
model_norm = np.linalg.norm(model)
|
|
359
|
+
cost_data = -1.0
|
|
360
|
+
cost_data_model = 0.0
|
|
361
|
+
if cost_data2 > 0:
|
|
362
|
+
cost_data = cost_data1 / cost_data2
|
|
363
|
+
if model_norm > 0:
|
|
364
|
+
cost_data_model = cost_data1 / model_norm
|
|
365
|
+
cost_admm1 = np.linalg.norm(qx_val - admm_method.z)
|
|
366
|
+
cost_admm2 = np.linalg.norm(admm_method.z)
|
|
367
|
+
cost_admm = -1.0
|
|
368
|
+
if cost_admm2 > 0:
|
|
369
|
+
cost_admm = cost_admm1 / cost_admm2
|
|
370
|
+
print("----------------------------------------")
|
|
371
|
+
print(f"it = {_i}")
|
|
372
|
+
print("cost_data = ", cost_data)
|
|
373
|
+
print("cost_data_model = ", cost_data_model)
|
|
374
|
+
print("cost_admm = ", cost_admm)
|
|
375
|
+
print("----------------------------------------")
|
|
376
|
+
linsys_kwargs = {k: v[_i] for k, v in linsys_solver_kwargs.items()}
|
|
377
|
+
rhs = b
|
|
378
|
+
solve_matrix = matrix
|
|
379
|
+
if active_set and Q.shape[0] > 0:
|
|
380
|
+
refresh = (
|
|
381
|
+
active_rows_prev is None
|
|
382
|
+
or int(active_set_refresh_interval) <= 1
|
|
383
|
+
or (_i % int(active_set_refresh_interval) == 0)
|
|
384
|
+
)
|
|
385
|
+
if refresh:
|
|
386
|
+
active_rows_prev = _active_rows(
|
|
387
|
+
qx_val[:, 0], xmin, xmax, prev_active=active_rows_prev
|
|
388
|
+
)
|
|
389
|
+
q_active = (Q_base * rho)[active_rows_prev]
|
|
390
|
+
solve_matrix = vstack([A, q_active]).tocsr()
|
|
391
|
+
rhs = np.hstack([b[:A_size], b[A_size:][active_rows_prev]])
|
|
392
|
+
|
|
393
|
+
if use_cached_normal_eq:
|
|
394
|
+
linsys_kwargs["_lhs"] = precomputed_lhs
|
|
395
|
+
linsys_kwargs["_rhs_normal"] = matrix_transpose @ rhs
|
|
396
|
+
if precomputed_M is not None:
|
|
397
|
+
linsys_kwargs["_M"] = precomputed_M
|
|
398
|
+
if reuse_inner_solve and inner_x0 is not None:
|
|
399
|
+
linsys_kwargs["_x0_guess"] = inner_x0
|
|
400
|
+
dx = _solve_linsys(solve_matrix, rhs, linsys_kwargs)
|
|
401
|
+
dx_norm = float(np.linalg.norm(dx))
|
|
402
|
+
model_norm_before = float(np.linalg.norm(model))
|
|
403
|
+
model += dx
|
|
404
|
+
if reuse_inner_solve:
|
|
405
|
+
inner_x0 = np.asarray(dx, dtype=float)
|
|
406
|
+
|
|
407
|
+
if Q.shape[0] > 0:
|
|
408
|
+
primal_residual = qx_val[:, 0] - admm_method.z
|
|
409
|
+
primal_norm = float(np.linalg.norm(primal_residual))
|
|
410
|
+
dual_norm = float(rho * np.linalg.norm(admm_method.z - z_prev))
|
|
411
|
+
qx_norm = float(np.linalg.norm(qx_val[:, 0]))
|
|
412
|
+
z_norm = float(np.linalg.norm(admm_method.z))
|
|
413
|
+
u_norm = float(np.linalg.norm(rho * admm_method.u))
|
|
414
|
+
n_ie_sqrt = float(np.sqrt(n_ie))
|
|
415
|
+
eps_pri = float(n_ie_sqrt * admm_abs_tol + admm_rel_tol * max(qx_norm, z_norm))
|
|
416
|
+
eps_dual = float(n_ie_sqrt * admm_abs_tol + admm_rel_tol * u_norm)
|
|
417
|
+
|
|
418
|
+
if return_history:
|
|
419
|
+
history.append(
|
|
420
|
+
{
|
|
421
|
+
"iteration": int(_i + 1),
|
|
422
|
+
"primal_norm": primal_norm,
|
|
423
|
+
"dual_norm": dual_norm,
|
|
424
|
+
"eps_pri": eps_pri,
|
|
425
|
+
"eps_dual": eps_dual,
|
|
426
|
+
"rho": float(rho),
|
|
427
|
+
"active_count": int(np.sum(active_rows_prev))
|
|
428
|
+
if active_set and active_rows_prev is not None
|
|
429
|
+
else int(n_ie),
|
|
430
|
+
}
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
if (_i + 1) >= min_iterations and primal_norm <= eps_pri and dual_norm <= eps_dual:
|
|
434
|
+
break
|
|
435
|
+
|
|
436
|
+
if model_update_tol > 0.0 and (_i + 1) >= min_iterations:
|
|
437
|
+
model_scale = max(model_norm_before, 1.0)
|
|
438
|
+
if dx_norm <= model_update_tol * model_scale:
|
|
439
|
+
if return_history:
|
|
440
|
+
history[-1]["stopped_on_model_update"] = True
|
|
441
|
+
history[-1]["dx_norm"] = dx_norm
|
|
442
|
+
history[-1]["model_norm"] = model_norm_before
|
|
443
|
+
break
|
|
444
|
+
|
|
445
|
+
if adaptive_rho_enabled and (_i + 1) >= min_iterations:
|
|
446
|
+
rho_new = rho
|
|
447
|
+
if primal_norm > adaptive_rho_mu * dual_norm:
|
|
448
|
+
rho_new = min(rho * adaptive_rho_tau, adaptive_rho_max)
|
|
449
|
+
elif dual_norm > adaptive_rho_mu * primal_norm:
|
|
450
|
+
rho_new = max(rho / adaptive_rho_tau, adaptive_rho_min)
|
|
451
|
+
if rho_new != rho:
|
|
452
|
+
if rho > 0:
|
|
453
|
+
admm_method.u *= rho / rho_new
|
|
454
|
+
rho = float(rho_new)
|
|
455
|
+
_update_system(rho)
|
|
456
|
+
elif model_update_tol > 0.0 and (_i + 1) >= min_iterations:
|
|
457
|
+
model_scale = max(model_norm_before, 1.0)
|
|
458
|
+
if dx_norm <= model_update_tol * model_scale:
|
|
459
|
+
break
|
|
460
|
+
|
|
461
|
+
if return_history:
|
|
462
|
+
return model, history
|
|
463
|
+
return model
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.1"
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: loop-interpolation
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Interpolation utilities for LoopStructural
|
|
5
|
+
Author-email: Lachlan Grose <lachlan.grose@monash.edu>
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
8
|
+
Classifier: Intended Audience :: Science/Research
|
|
9
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
12
|
+
Classifier: Operating System :: POSIX
|
|
13
|
+
Classifier: Operating System :: MacOS
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: loop-common
|
|
22
|
+
Requires-Dist: numpy
|
|
23
|
+
Requires-Dist: scipy
|
|
24
|
+
Requires-Dist: pydantic
|
|
25
|
+
Provides-Extra: tests
|
|
26
|
+
Requires-Dist: pytest; extra == "tests"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# loop-interpolation
|
|
30
|
+
|
|
31
|
+
Interpolation utilities for LoopStructural, including piecewise linear (P1) and
|
|
32
|
+
piecewise quadratic (P2) discrete interpolators, finite difference
|
|
33
|
+
interpolators, and the fold interpolator used across the LoopStructural
|
|
34
|
+
workspace packages.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
loop_interpolation/__init__.py,sha256=Jt3EvVKMytVcXiy3-Fkru2HcXU6Tw0kb73u37_I_gLw,5033
|
|
2
|
+
loop_interpolation/_builders.py,sha256=B49KsxB8RRN6IHDfGT43nXWe_Av1SVVT8vm2Nh1oEiQ,6758
|
|
3
|
+
loop_interpolation/_constant_norm.py,sha256=HfO3NOOYBivI75KpWtzUh_h6P4uSuQSl0a9SmULnTaE,8050
|
|
4
|
+
loop_interpolation/_diagnostics.py,sha256=fqbbw0hAhlZMYITCoZbjejav6zJuEqc_4yYCtjRgEFc,3808
|
|
5
|
+
loop_interpolation/_discrete_fold_interpolator.py,sha256=QDyrB3ugtyErXznsy36cXJTxwCSZ9uPe-bbmAUS9Ok0,8656
|
|
6
|
+
loop_interpolation/_discrete_interpolator.py,sha256=wntZ60GSWmgDg2KyvLZEPL105jtmQzM2nwEmfvbTNTs,53622
|
|
7
|
+
loop_interpolation/_fd_fold_interpolator.py,sha256=rE4kpBhOo360v-ip24x-yF9Mt-iAy5EAMGH2pdidrBI,9999
|
|
8
|
+
loop_interpolation/_finite_difference_interpolator.py,sha256=XJIIbIN7kageNChXT8Wy6VQrWgDPTzJSQbVufhTcGDY,53847
|
|
9
|
+
loop_interpolation/_fold_event.py,sha256=QTimKE1ESHJlV29wwBzIxdsW1wObxWlMbmvgZpnerSQ,7445
|
|
10
|
+
loop_interpolation/_fold_norm_alignment.py,sha256=5DDWK8nBoRVSpKtL3ds0Y-HTNux_SXLuFnTzn6HqpN4,4049
|
|
11
|
+
loop_interpolation/_fold_setup.py,sha256=XtMT58-w7r4793cXvjW2NgKZs_1R9DdFblvQnZ3L1M4,1096
|
|
12
|
+
loop_interpolation/_geological_interpolator.py,sha256=xk5Su45Zh8Q1lKw2G33WCPuWyL9mhQBYFWRkJLGnAcg,32705
|
|
13
|
+
loop_interpolation/_interpolator_builder.py,sha256=1W4gc-kCiadTmHC16jQNsRKjVguwLLpEEIrPJZm-1gY,8406
|
|
14
|
+
loop_interpolation/_interpolator_factory.py,sha256=e9cxxJBEc6hs_N06jfpM1LqwvZWJ7k4gYDy8PQhKgzk,6594
|
|
15
|
+
loop_interpolation/_interpolatortype.py,sha256=CYHiXkAxcSb6w4iVk2k1H5cG-G4MjQY9Z2GG203Rkkc,591
|
|
16
|
+
loop_interpolation/_operator.py,sha256=CF4NhmvRvdjnVimc-lkDJG3n-Whvt1yfAVVMThAKN0A,1535
|
|
17
|
+
loop_interpolation/_p1interpolator.py,sha256=9sUSz7-Sr9lGYVNUOFSwPy-j5ncFmrmpMvs6sMAnQGk,11757
|
|
18
|
+
loop_interpolation/_p2interpolator.py,sha256=T6F7wxxvKfCiI6SS3BB82V5c-kUE4EdjD8TEghFW64k,11722
|
|
19
|
+
loop_interpolation/_regularisation.py,sha256=SCG2dqOnrIaox0zpjkxx57pneuJwi0oPFKOjSYaZaz4,3871
|
|
20
|
+
loop_interpolation/_solver_pipeline.py,sha256=zqjZHL12yo7FwShxqfRzOo_OfDtG6uezCMcHS66laoE,5445
|
|
21
|
+
loop_interpolation/_solver_strategy.py,sha256=Cwt_9TdmRzAGXWH5nKNYwpYX1twCuxcdEXjDXJXArYk,8273
|
|
22
|
+
loop_interpolation/_surfe_wrapper.py,sha256=xUgzZx9BnkDT7O8yHnPLH2FhJsiv2BYTpZvTdu3zsbA,7057
|
|
23
|
+
loop_interpolation/_svariogram.py,sha256=iKdY6nsNtxwJbaPJUiVj2bDgAQAzDvxdYmVWr-9zEcE,5220
|
|
24
|
+
loop_interpolation/_validation.py,sha256=O7iBmTju1581lNC5m83L45kMcqFeM4meHjRx487XmV8,17985
|
|
25
|
+
loop_interpolation/constraints.py,sha256=6mqu4YoZX7ua59Hi97aeSETcaL5NF2Kqv7MCEnTsX20,19113
|
|
26
|
+
loop_interpolation/fold_function/__init__.py,sha256=TxiSUEl7kWl1ZWfGE6JU5DR4V1TlUa6PIRhmnaor6RQ,1141
|
|
27
|
+
loop_interpolation/fold_function/_base_fold_rotation_angle.py,sha256=DOFUNA3Zi_nPUWvXk44i3G8G0lRpm7zQ3oEAqOMyZWA,8469
|
|
28
|
+
loop_interpolation/fold_function/_fourier_series_fold_rotation_angle.py,sha256=bRJRZk_WGuec3i24-J9IkYDGfdKVLKnIokTSWc8dH_c,3902
|
|
29
|
+
loop_interpolation/fold_function/_lambda_fold_rotation_angle.py,sha256=nGrnwCGTs1BM9f4Jy2-RhpS6QGJEeJrq5LPTSHkW4NI,1673
|
|
30
|
+
loop_interpolation/loopsolver/__init__.py,sha256=vaRHNEAW8T8X6yUlPZ3lu521Gmqpm5zDM62d8geNY7s,101
|
|
31
|
+
loop_interpolation/loopsolver/admm_constant_norm.py,sha256=EICy7UJoA-4LEUGSZbjE1GM1C5I3A8lr3glBPXdgprU,4018
|
|
32
|
+
loop_interpolation/loopsolver/admm_method.py,sha256=1LEHH9AmAWJjoYO06SV5szsE2jvzdOydYGxl3lUwZEg,810
|
|
33
|
+
loop_interpolation/loopsolver/admm_solver.py,sha256=lXWxHGme4DQwQXlbeZssdj5OfgXVsNEzQ5PMUm80a4U,18872
|
|
34
|
+
loop_interpolation/loopsolver/version.py,sha256=sXLh7g3KC4QCFxcZGBTpG2scR7hmmBsMjq6LqRptkRg,22
|
|
35
|
+
loop_interpolation-0.0.1.dist-info/licenses/LICENSE,sha256=ZqGeNFOgmYevj7Ld7Q-kR4lAxWXuBRUdUmPC6XM_py8,1071
|
|
36
|
+
loop_interpolation-0.0.1.dist-info/METADATA,sha256=mTu_CZvQGBr2fKTn8nf4tytcORyd74_kLvohaBBL6Dw,1263
|
|
37
|
+
loop_interpolation-0.0.1.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
38
|
+
loop_interpolation-0.0.1.dist-info/top_level.txt,sha256=1vDe6-FrFDF289K3e7pQlp3bTb3GxW5ZMbUOoqjbU98,19
|
|
39
|
+
loop_interpolation-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Lachlan Grose
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
loop_interpolation
|