hyperdd 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.
@@ -0,0 +1,136 @@
1
+ """Schur complement solver for partitioned clusters.
2
+
3
+ Solves H dx = b where H has block-arrow structure from METIS partitioning:
4
+
5
+ H = [ H_11 | E_1 ]
6
+ [ H_22 | E_2 ]
7
+ [ ... | ... ]
8
+ [ E_1^T E_2^T ... | H_BB ]
9
+
10
+ 1. Factor each H_ii independently (parallel-ready)
11
+ 2. Form Schur complement: S = H_BB - sum(E_i^T H_ii^{-1} E_i)
12
+ 3. Solve S dx_B = b_B - sum(E_i^T H_ii^{-1} b_i)
13
+ 4. Back-substitute: dx_i = H_ii^{-1} (b_i - E_i dx_B)
14
+ """
15
+
16
+ import numpy as np
17
+ from scipy.sparse import csc_matrix, diags, eye as speye
18
+ from scipy.sparse.linalg import spsolve
19
+
20
+ try:
21
+ from sksparse.cholmod import cholesky as cholmod_cholesky
22
+ HAS_CHOLMOD = True
23
+ except ImportError:
24
+ HAS_CHOLMOD = False
25
+
26
+
27
+ def _factor_and_solve(H_block, b_block):
28
+ """Factor a sparse SPD block and solve."""
29
+ H_csc = csc_matrix(H_block)
30
+ if HAS_CHOLMOD:
31
+ factor = cholmod_cholesky(H_csc)
32
+ return factor(b_block), factor
33
+ else:
34
+ return spsolve(H_csc, b_block), None
35
+
36
+
37
+ def _factor_solve_with_rhs(factor, H_block, b):
38
+ """Solve with existing factor or direct."""
39
+ if factor is not None:
40
+ return factor(b)
41
+ else:
42
+ return spsolve(csc_matrix(H_block), b)
43
+
44
+
45
+ def solve_schur(H, b, partition_info, old_to_new, n_interior_cumsum, boundary_start):
46
+ """Solve H dx = b using Schur complement with METIS partitioning.
47
+
48
+ Parameters
49
+ ----------
50
+ H : sparse matrix (4*nev, 4*nev)
51
+ Normal equation matrix in REORDERED event indices.
52
+ b : ndarray (4*nev,)
53
+ Right-hand side in REORDERED event indices.
54
+ partition_info : dict from partition_cluster()
55
+ old_to_new : ndarray - event reordering map
56
+ n_interior_cumsum : list - cumulative interior counts (in 4x block units)
57
+ boundary_start : int - where boundary events start (in event units)
58
+
59
+ Returns
60
+ -------
61
+ dx : ndarray (4*nev,) - solution in REORDERED indices
62
+ """
63
+ n_parts = partition_info["n_parts"]
64
+ n_boundary_4 = (len(b) - boundary_start * 4)
65
+ boundary_start_4 = boundary_start * 4
66
+ nev_4 = len(b)
67
+
68
+ # Extract H_BB (boundary-boundary block)
69
+ H_BB = H[boundary_start_4:, boundary_start_4:]
70
+ b_B = b[boundary_start_4:].copy()
71
+
72
+ # Accumulate Schur complement contributions
73
+ S = H_BB.copy().toarray() if n_boundary_4 < 50000 else H_BB.copy()
74
+ rhs_B = b_B.copy()
75
+
76
+ factors = []
77
+
78
+ for p in range(n_parts):
79
+ i_start = n_interior_cumsum[p] * 4
80
+ i_end = n_interior_cumsum[p + 1] * 4
81
+ n_i = i_end - i_start
82
+
83
+ if n_i == 0:
84
+ factors.append(None)
85
+ continue
86
+
87
+ # Extract blocks
88
+ H_ii = H[i_start:i_end, i_start:i_end]
89
+ E_i = H[i_start:i_end, boundary_start_4:]
90
+ b_i = b[i_start:i_end]
91
+
92
+ # Factor H_ii
93
+ x_i, factor_i = _factor_and_solve(H_ii, b_i)
94
+ factors.append((factor_i, H_ii))
95
+
96
+ # Schur contribution: S -= E_i^T H_ii^{-1} E_i
97
+ # Compute H_ii^{-1} E_i column by column (or use factor)
98
+ if E_i.nnz > 0:
99
+ E_dense = E_i.toarray()
100
+ HinvE = np.zeros_like(E_dense)
101
+ for col in range(E_dense.shape[1]):
102
+ if np.any(E_dense[:, col] != 0):
103
+ HinvE[:, col] = _factor_solve_with_rhs(
104
+ factor_i, H_ii, E_dense[:, col])
105
+
106
+ S -= E_dense.T @ HinvE
107
+ rhs_B -= E_dense.T @ x_i
108
+
109
+ # Solve boundary system
110
+ if isinstance(S, np.ndarray):
111
+ # Dense solve for small boundary
112
+ dx_B = np.linalg.solve(S, rhs_B)
113
+ else:
114
+ dx_B = spsolve(csc_matrix(S), rhs_B)
115
+
116
+ # Back-substitute for interior events
117
+ dx = np.zeros(nev_4, dtype=np.float64)
118
+ dx[boundary_start_4:] = dx_B
119
+
120
+ for p in range(n_parts):
121
+ i_start = n_interior_cumsum[p] * 4
122
+ i_end = n_interior_cumsum[p + 1] * 4
123
+ n_i = i_end - i_start
124
+
125
+ if n_i == 0:
126
+ continue
127
+
128
+ E_i = H[i_start:i_end, boundary_start_4:]
129
+ b_i = b[i_start:i_end]
130
+
131
+ # dx_i = H_ii^{-1} (b_i - E_i dx_B)
132
+ rhs_i = b_i - E_i @ dx_B
133
+ factor_i, H_ii = factors[p]
134
+ dx[i_start:i_end] = _factor_solve_with_rhs(factor_i, H_ii, rhs_i)
135
+
136
+ return dx
@@ -0,0 +1,221 @@
1
+ """Multi-backend sparse solver for graphDD.
2
+
3
+ Supports: lsqr, lsmr, cholesky, pcg.
4
+ Uses shared Jacobian assembly from core/jacobian.py.
5
+ """
6
+
7
+ import numpy as np
8
+ from scipy.sparse import csc_matrix, diags
9
+ from scipy.sparse.linalg import spsolve, lsqr as scipy_lsqr, cg as scipy_cg, lsmr as scipy_lsmr
10
+
11
+ from ..core.jacobian import build_dd_system, update_residuals
12
+ from .stats import resstat
13
+
14
+ try:
15
+ from sksparse.cholmod import cholesky as cholmod_cholesky
16
+ HAS_CHOLMOD = True
17
+ except ImportError:
18
+ HAS_CHOLMOD = False
19
+
20
+
21
+ def solve_sparse_chol(ndt, nev, nsrc, damp, idata, ev_cusp, ev_fix, src_cusp,
22
+ dt_res, dt_wt, dt_ista, dt_ic1, dt_ic2, dt_idx,
23
+ tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs,
24
+ iter_num=1, use_float32=True,
25
+ gnc_kernel=None, gnc_mu=None, gnc_threshold=3.0,
26
+ backend="lsqr", robust_loss=None):
27
+ """Sparse solver with selectable backend."""
28
+ ncol = 4 * nev
29
+
30
+ # Apply GNC kernel weights if requested
31
+ if gnc_kernel and gnc_kernel != "l2":
32
+ from ..core.kernels import kernel_weights
33
+ res = dt_res[:ndt]
34
+ valid = dt_wt[:ndt] > 0
35
+ if np.any(valid):
36
+ med = np.median(res[valid])
37
+ mad = np.median(np.abs(res[valid] - med))
38
+ c = max(gnc_threshold * mad / 0.6745, 1e-6)
39
+ else:
40
+ c = 1.0
41
+ kw = kernel_weights(res, c, kernel=gnc_kernel, mu=gnc_mu)
42
+ dt_wt[:ndt] = dt_wt[:ndt] * kw.astype(dt_wt.dtype)
43
+
44
+ # Apply robust loss reweighting if requested
45
+ if robust_loss:
46
+ _apply_robust_loss(ndt, dt_res, dt_wt, robust_loss)
47
+
48
+ # Build weighted, column-scaled Jacobian
49
+ G_scaled, d_vec, norm, xp1, yp1, zp1, xp2, yp2, zp2 = build_dd_system(
50
+ ndt, nev, nsrc, dt_res, dt_wt, dt_ista, dt_ic1, dt_ic2, dt_idx,
51
+ tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs,
52
+ ev_fix, use_float32=use_float32,
53
+ )
54
+
55
+ # Dispatch to solver backend
56
+ _dtype = np.float32 if use_float32 else np.float64
57
+ if backend == "lsqr":
58
+ x_scaled = _solve_lsqr(G_scaled, d_vec, damp, _dtype, ncol)
59
+ elif backend == "lsmr":
60
+ x_scaled = _solve_lsmr(G_scaled, d_vec, damp, _dtype, ncol)
61
+ elif backend == "cholesky":
62
+ x_scaled = _solve_cholesky(G_scaled, d_vec, damp, ncol)
63
+ elif backend == "pcg":
64
+ x_scaled = _solve_pcg(G_scaled, d_vec, damp, ncol)
65
+ elif backend == "schur":
66
+ x_scaled = _solve_schur(G_scaled, d_vec, damp, ncol, nev,
67
+ ndt, dt_ic1, dt_ic2)
68
+ else:
69
+ raise ValueError(f"Unknown backend: {backend}")
70
+
71
+ # Unscale
72
+ x = x_scaled / norm
73
+
74
+ # Update residuals
75
+ ic1 = dt_ic1[:ndt]
76
+ ic2 = dt_ic2[:ndt]
77
+ update_residuals(ndt, dt_res, x, ic1, ic2, xp1, yp1, zp1, xp2, yp2, zp2)
78
+
79
+ # Residual statistics
80
+ rms_cc, rms_ct, rms_cc0, rms_ct0, resvar = resstat(
81
+ ndt, nev, dt_res, dt_wt, dt_idx, idata
82
+ )
83
+
84
+ # Store results (same interface as solve_lsqr)
85
+ src_dx = np.zeros(nev, dtype=np.float64)
86
+ src_dy = np.zeros(nev, dtype=np.float64)
87
+ src_dz = np.zeros(nev, dtype=np.float64)
88
+ src_dt = np.zeros(nev, dtype=np.float64)
89
+
90
+ for i in range(nev):
91
+ src_dx[i] = -x[4 * i + 0]
92
+ src_dy[i] = -x[4 * i + 1]
93
+ src_dz[i] = -x[4 * i + 2]
94
+ src_dt[i] = -x[4 * i + 3]
95
+
96
+ return {
97
+ 'src_dx': src_dx, 'src_dy': src_dy, 'src_dz': src_dz, 'src_dt': src_dt,
98
+ 'src_ex': np.zeros(nev), 'src_ey': np.zeros(nev),
99
+ 'src_ez': np.zeros(nev), 'src_et': np.zeros(nev),
100
+ 'src_cusp': ev_cusp[:nev].copy(),
101
+ 'rms_cc': rms_cc, 'rms_ct': rms_ct,
102
+ 'rms_cc0': rms_cc0, 'rms_ct0': rms_ct0,
103
+ 'acond': 0.0,
104
+ }
105
+
106
+
107
+ # --- Solver backends ---
108
+
109
+ def _solve_lsqr(G_scaled, d_vec, damp, _dtype, ncol):
110
+ result = scipy_lsqr(
111
+ G_scaled, d_vec, damp=_dtype(damp),
112
+ atol=1e-6, btol=1e-6, conlim=1e5, iter_lim=100 * ncol,
113
+ )
114
+ return result[0]
115
+
116
+
117
+ def _solve_lsmr(G_scaled, d_vec, damp, _dtype, ncol):
118
+ result = scipy_lsmr(
119
+ G_scaled, d_vec, damp=_dtype(damp),
120
+ atol=1e-6, btol=1e-6, conlim=1e5, maxiter=100 * ncol,
121
+ )
122
+ return result[0]
123
+
124
+
125
+ def _solve_cholesky(G_scaled, d_vec, damp, ncol):
126
+ G64 = G_scaled.astype(np.float64)
127
+ d64 = d_vec.astype(np.float64)
128
+ H = G64.T @ G64 + diags(np.full(ncol, float(damp) ** 2))
129
+ b = G64.T @ d64
130
+ H_csc = csc_matrix(H)
131
+ if HAS_CHOLMOD:
132
+ return cholmod_cholesky(H_csc)(b)
133
+ else:
134
+ return spsolve(H_csc, b)
135
+
136
+
137
+ def _solve_pcg(G_scaled, d_vec, damp, ncol):
138
+ G64 = G_scaled.astype(np.float64)
139
+ d64 = d_vec.astype(np.float64)
140
+ H = G64.T @ G64 + diags(np.full(ncol, float(damp) ** 2))
141
+ b = G64.T @ d64
142
+ H_diag = H.diagonal()
143
+ M_inv = 1.0 / np.maximum(H_diag, 1e-10)
144
+ from scipy.sparse.linalg import LinearOperator
145
+ M = LinearOperator((ncol, ncol), matvec=lambda v: M_inv * v)
146
+ x, info = scipy_cg(csc_matrix(H), b, M=M, rtol=1e-8, maxiter=500)
147
+ return x
148
+
149
+
150
+ def _solve_schur(G_scaled, d_vec, damp, ncol, nev, ndt, dt_ic1, dt_ic2,
151
+ max_partition_size=5000):
152
+ """Schur complement solver with METIS partitioning.
153
+
154
+ Falls back to direct Cholesky if cluster is small enough or METIS unavailable.
155
+ """
156
+ from ..core.partition import partition_cluster, build_reorder_map
157
+
158
+ # Try to partition
159
+ partition_info = partition_cluster(
160
+ nev, ndt, dt_ic1[:ndt], dt_ic2[:ndt],
161
+ max_partition_size=max_partition_size
162
+ )
163
+
164
+ if partition_info is None:
165
+ # Small cluster, use direct Cholesky
166
+ return _solve_cholesky(G_scaled, d_vec, damp, ncol)
167
+
168
+ # Build reorder map
169
+ old_to_new, new_to_old, n_interior_cumsum, boundary_start = \
170
+ build_reorder_map(partition_info)
171
+
172
+ # Form normal equations in original ordering
173
+ G64 = G_scaled.astype(np.float64)
174
+ d64 = d_vec.astype(np.float64)
175
+ H_orig = G64.T @ G64 + diags(np.full(ncol, float(damp) ** 2))
176
+ b_orig = G64.T @ d64
177
+
178
+ # Reorder H and b: permute rows/cols by old_to_new (in 4-block units)
179
+ perm_4 = np.empty(ncol, dtype=np.int32)
180
+ for i in range(nev):
181
+ new_i = old_to_new[i]
182
+ perm_4[4*i:4*i+4] = np.arange(4*new_i, 4*new_i+4)
183
+
184
+ # Apply permutation: H_reord[perm, perm] = H_orig
185
+ from scipy.sparse import csc_matrix as csc
186
+ P = csc((np.ones(ncol), (perm_4, np.arange(ncol))), shape=(ncol, ncol))
187
+ H_reord = P @ H_orig @ P.T
188
+ b_reord = P @ b_orig
189
+
190
+ # Solve with Schur complement
191
+ from .schur import solve_schur
192
+ dx_reord = solve_schur(H_reord, b_reord, partition_info,
193
+ old_to_new, n_interior_cumsum, boundary_start)
194
+
195
+ # Un-reorder: dx_orig = P^T @ dx_reord
196
+ dx = P.T @ dx_reord
197
+
198
+ return dx
199
+
200
+
201
+ def _apply_robust_loss(ndt, dt_res, dt_wt, loss_type):
202
+ res = dt_res[:ndt]
203
+ wt = dt_wt[:ndt]
204
+ valid = wt > 0
205
+ if not np.any(valid):
206
+ return
207
+ med = np.median(res[valid])
208
+ mad = np.median(np.abs(res[valid] - med)) / 0.6745
209
+ c = max(3.0 * mad, 1e-6)
210
+ r = np.abs(res[:ndt])
211
+ if loss_type == "huber":
212
+ scale = np.ones(ndt)
213
+ mask = r > c
214
+ scale[mask] = c / r[mask]
215
+ wt[:] *= scale
216
+ elif loss_type == "cauchy":
217
+ wt[:] *= 1.0 / (1.0 + (r / c) ** 2)
218
+ elif loss_type == "tukey":
219
+ scale = (1.0 - (r / c) ** 2) ** 2
220
+ scale[r > c] = 0.0
221
+ wt[:] *= scale
@@ -0,0 +1,116 @@
1
+ """Residual statistics for hypoDD solvers.
2
+
3
+ Reimplementation of resstat.f from hypoDD.
4
+ """
5
+
6
+ import numpy as np
7
+
8
+
9
+ def resstat(ndt, nev, dt_res, dt_wt, dt_idx, idata):
10
+ """Compute residual statistics.
11
+
12
+ Parameters
13
+ ----------
14
+ ndt : int
15
+ Number of differential time observations.
16
+ nev : int
17
+ Number of events.
18
+ dt_res : np.ndarray
19
+ Residuals in seconds, shape (>=ndt,).
20
+ dt_wt : np.ndarray
21
+ Weights, shape (>=ndt,).
22
+ dt_idx : np.ndarray
23
+ Data type index (1=CC-P, 2=CC-S, 3=CT-P, 4=CT-S), shape (>=ndt,).
24
+ idata : int
25
+ Data type flag (1=CC, 2=CT, 3=both).
26
+
27
+ Returns
28
+ -------
29
+ rms_cc : float
30
+ Weighted RMS of cross-correlation data [s].
31
+ rms_ct : float
32
+ Weighted RMS of catalog data [s].
33
+ rms_cc0 : float
34
+ Unweighted RMS of cross-correlation data [s].
35
+ rms_ct0 : float
36
+ Unweighted RMS of catalog data [s].
37
+ resvar : float
38
+ Weighted residual variance [ms^2], for error estimation.
39
+ """
40
+ d = dt_res[:ndt]
41
+ w = dt_wt[:ndt]
42
+ idx = dt_idx[:ndt]
43
+
44
+ cc_mask = idx <= 2
45
+ ct_mask = idx >= 3
46
+ n_cc = int(np.sum(cc_mask))
47
+ n_ct = int(np.sum(ct_mask))
48
+
49
+ # Weight scaling factors (Fortran: f_cc = j/sw_cc, f_ct = (ndt-j)/sw_ct)
50
+ sw_cc = np.sum(w[cc_mask]) if n_cc > 0 else 1.0
51
+ sw_ct = np.sum(w[ct_mask]) if n_ct > 0 else 1.0
52
+ f_cc = n_cc / sw_cc if n_cc > 0 and sw_cc > 0 else 1.0
53
+ f_ct = n_ct / sw_ct if n_ct > 0 and sw_ct > 0 else 1.0
54
+
55
+ # --- Unweighted RMS ---
56
+ # Fortran resstat.f: computes av=sum/n first, then sqrt((sum_sq - av**2/n) / (n-1))
57
+ # This differs from the standard formula sqrt((sum_sq - sum**2/n) / (n-1)).
58
+ if n_cc > 1:
59
+ cc_d = d[cc_mask]
60
+ sum_sq = np.sum(cc_d ** 2)
61
+ av_cc0 = np.sum(cc_d) / n_cc
62
+ rms_cc0 = np.sqrt((sum_sq - av_cc0 ** 2 / n_cc) / (n_cc - 1))
63
+ else:
64
+ rms_cc0 = 0.0
65
+
66
+ if n_ct > 1:
67
+ ct_d = d[ct_mask]
68
+ sum_sq = np.sum(ct_d ** 2)
69
+ av_ct0 = np.sum(ct_d) / n_ct
70
+ rms_ct0 = np.sqrt((sum_sq - av_ct0 ** 2 / n_ct) / (n_ct - 1))
71
+ else:
72
+ rms_ct0 = 0.0
73
+
74
+ # --- Weighted RMS (same Fortran pattern) ---
75
+ if n_cc > 1:
76
+ cc_d = d[cc_mask]
77
+ cc_w = w[cc_mask]
78
+ wres = f_cc * cc_w * cc_d
79
+ sum_sq = np.sum(wres ** 2)
80
+ av_cc = np.sum(wres) / n_cc
81
+ rms_cc = np.sqrt((sum_sq - av_cc ** 2 / n_cc) / (n_cc - 1))
82
+ else:
83
+ rms_cc = 0.0
84
+
85
+ if n_ct > 1:
86
+ ct_d = d[ct_mask]
87
+ ct_w = w[ct_mask]
88
+ wres = f_ct * ct_w * ct_d
89
+ sum_sq = np.sum(wres ** 2)
90
+ av_ct = np.sum(wres) / n_ct
91
+ rms_ct = np.sqrt((sum_sq - av_ct ** 2 / n_ct) / (n_ct - 1))
92
+ else:
93
+ rms_ct = 0.0
94
+
95
+ # --- Weighted variance for error estimation ---
96
+ # Fortran: f = ndt/sw; s = w(i)*d(i)*1000 - dav0*1000; dvar1 = ...
97
+ sw = np.sum(w)
98
+ if sw == 0:
99
+ sw = 1.0
100
+ f = ndt / sw
101
+
102
+ # Weighted, unscaled average (dav0 in Fortran)
103
+ dav0 = np.sum(w * d) / ndt
104
+
105
+ # Weighted residuals in ms
106
+ s1 = w * d * 1000.0 - dav0 * 1000.0
107
+ ss1 = np.sum(s1)
108
+ dvar1_sum = np.sum(s1 ** 2)
109
+
110
+ dof = ndt - 4 * nev
111
+ if dof > 0:
112
+ resvar = (dvar1_sum - ss1 ** 2 / ndt) / dof
113
+ else:
114
+ resvar = dvar1_sum
115
+
116
+ return rms_cc, rms_ct, rms_cc0, rms_ct0, resvar