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.
hyperdd/solvers/svd.py ADDED
@@ -0,0 +1,312 @@
1
+ """SVD least-squares solver for hypoDD.
2
+
3
+ Reimplementation of lsfit_svd.f from hypoDD using numpy.linalg.svd.
4
+ """
5
+
6
+ import numpy as np
7
+
8
+ from .stats import resstat
9
+
10
+
11
+ def _add_fix_constraint_rows(G, d, nndt, nev, ev_fix, damp):
12
+ """Add constraint rows for fixed event parameters.
13
+
14
+ Parameters
15
+ ----------
16
+ G : np.ndarray
17
+ Design matrix to extend, shape (max_rows, 4*nev).
18
+ d : np.ndarray
19
+ Data vector to extend.
20
+ nndt : int
21
+ Current number of rows (ndt + 4 mean-shift rows).
22
+ nev : int
23
+ Number of events.
24
+ ev_fix : np.ndarray
25
+ Fix flags per event, shape (nev,).
26
+ damp : float
27
+ Damping weight for constraints.
28
+
29
+ Returns
30
+ -------
31
+ nndt : int
32
+ Updated number of rows.
33
+ """
34
+ k = 0 # counter for added rows
35
+ for i in range(nev):
36
+ fix = ev_fix[i]
37
+ if fix == 0:
38
+ continue
39
+
40
+ # Fortran 1-indexed: i*4-3 -> Python 0-indexed: i*4+0
41
+ # Map fix values to the parameter offsets they constrain:
42
+ # fix=1: z
43
+ # fix=2: z, t
44
+ # fix=3: x, y, z
45
+ # fix=4: x, y, z, t
46
+ # fix=5: t
47
+ # fix=6: x, y
48
+ if fix == 1:
49
+ offsets = [2] # z
50
+ elif fix == 2:
51
+ offsets = [2, 3] # z, t
52
+ elif fix == 3:
53
+ offsets = [0, 1, 2] # x, y, z
54
+ elif fix == 4:
55
+ offsets = [0, 1, 2, 3] # x, y, z, t
56
+ elif fix == 5:
57
+ offsets = [3] # t
58
+ elif fix == 6:
59
+ offsets = [0, 1] # x, y
60
+ else:
61
+ continue
62
+
63
+ for off in offsets:
64
+ row = nndt + k
65
+ d[row] = 0.0
66
+ G[row, :] = 0.0
67
+ # Fortran: g(nndt+k, i*4-1) = damp for z (i*4-1 is col for z of event i)
68
+ # In 0-indexed: event i -> columns i*4+0..i*4+3
69
+ G[row, i * 4 + off] = damp
70
+ k += 1
71
+
72
+ nndt += k
73
+ return nndt
74
+
75
+
76
+ def solve_svd(ndt, nev, nsrc, damp, idata, ev_cusp, ev_fix, src_cusp,
77
+ dt_res, dt_wt, dt_ista, dt_ic1, dt_ic2, dt_idx,
78
+ tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs, iter_num=1):
79
+ """SVD least-squares solver for hypoDD.
80
+
81
+ Parameters
82
+ ----------
83
+ ndt : int
84
+ Number of differential time observations.
85
+ nev : int
86
+ Number of events.
87
+ nsrc : int
88
+ Number of sources (if 1, use single source index).
89
+ damp : float
90
+ Damping value for fixed-event constraints.
91
+ idata : int
92
+ Data type flag (1=CC, 2=CT, 3=both).
93
+ ev_cusp : np.ndarray
94
+ Event IDs, shape (nev,). 0-indexed event list.
95
+ ev_fix : np.ndarray
96
+ Fix flags per event, shape (nev,).
97
+ src_cusp : np.ndarray
98
+ Source event IDs, shape (nev,).
99
+ dt_res : np.ndarray
100
+ Residuals in seconds, shape (>=ndt,). MODIFIED in-place.
101
+ dt_wt : np.ndarray
102
+ Weights, shape (>=ndt,).
103
+ dt_ista : np.ndarray
104
+ Station indices (0-indexed), shape (>=ndt,).
105
+ dt_ic1 : np.ndarray
106
+ Event-1 indices (0-indexed into nev), shape (>=ndt,).
107
+ dt_ic2 : np.ndarray
108
+ Event-2 indices (0-indexed into nev), shape (>=ndt,).
109
+ dt_idx : np.ndarray
110
+ Data type index (1=CC-P, 2=CC-S, 3=CT-P, 4=CT-S), shape (>=ndt,).
111
+ tmp_xp, tmp_yp, tmp_zp : np.ndarray
112
+ P-wave partial derivatives, shape (nsta, nsrc).
113
+ tmp_xs, tmp_ys, tmp_zs : np.ndarray
114
+ S-wave partial derivatives, shape (nsta, nsrc).
115
+ iter_num : int, optional
116
+ Iteration number (for statistics bookkeeping).
117
+
118
+ Returns
119
+ -------
120
+ result : dict
121
+ Dictionary with keys:
122
+ src_dx, src_dy, src_dz, src_dt : np.ndarray (nev,)
123
+ Location/time adjustments in m and ms.
124
+ src_ex, src_ey, src_ez, src_et : np.ndarray (nev,)
125
+ 95% confidence errors in m and ms.
126
+ rms_cc, rms_ct, rms_cc0, rms_ct0 : float
127
+ Residual RMS statistics.
128
+ src_cusp : np.ndarray (nev,)
129
+ Event IDs (copied from ev_cusp).
130
+ """
131
+ ncol = 4 * nev
132
+
133
+ # Count maximum constraint rows:
134
+ # 4 for mean-shift + up to 4*nev for ev_fix
135
+ max_extra = 4
136
+ for i in range(nev):
137
+ fix = ev_fix[i]
138
+ if fix == 1 or fix == 5:
139
+ max_extra += 1
140
+ elif fix == 2 or fix == 6:
141
+ max_extra += 2
142
+ elif fix == 3:
143
+ max_extra += 3
144
+ elif fix == 4:
145
+ max_extra += 4
146
+ max_rows = ndt + max_extra
147
+
148
+ # Allocate G matrix (double precision) and data vector
149
+ G = np.zeros((max_rows, ncol), dtype=np.float64)
150
+ d = np.zeros(max_rows, dtype=np.float64)
151
+
152
+ # ---- Step 1: Build G matrix ----
153
+ for i in range(ndt):
154
+ if nsrc == 1:
155
+ k1 = 0
156
+ k2 = 0
157
+ else:
158
+ k1 = dt_ic1[i]
159
+ k2 = dt_ic2[i]
160
+
161
+ sta = dt_ista[i]
162
+ ic1 = dt_ic1[i]
163
+ ic2 = dt_ic2[i]
164
+
165
+ if dt_idx[i] == 2 or dt_idx[i] == 4:
166
+ # S-phase partials
167
+ G[i, ic1 * 4 + 0] = tmp_xs[sta, k1]
168
+ G[i, ic1 * 4 + 1] = tmp_ys[sta, k1]
169
+ G[i, ic1 * 4 + 2] = tmp_zs[sta, k1]
170
+ G[i, ic1 * 4 + 3] = 1.0
171
+ G[i, ic2 * 4 + 0] = -tmp_xs[sta, k2]
172
+ G[i, ic2 * 4 + 1] = -tmp_ys[sta, k2]
173
+ G[i, ic2 * 4 + 2] = -tmp_zs[sta, k2]
174
+ G[i, ic2 * 4 + 3] = -1.0
175
+ else:
176
+ # P-phase partials
177
+ G[i, ic1 * 4 + 0] = tmp_xp[sta, k1]
178
+ G[i, ic1 * 4 + 1] = tmp_yp[sta, k1]
179
+ G[i, ic1 * 4 + 2] = tmp_zp[sta, k1]
180
+ G[i, ic1 * 4 + 3] = 1.0
181
+ G[i, ic2 * 4 + 0] = -tmp_xp[sta, k2]
182
+ G[i, ic2 * 4 + 1] = -tmp_yp[sta, k2]
183
+ G[i, ic2 * 4 + 2] = -tmp_zp[sta, k2]
184
+ G[i, ic2 * 4 + 3] = -1.0
185
+
186
+ # ---- Step 2: Weight data ----
187
+ wt = dt_wt[:ndt].copy()
188
+ d[:ndt] = dt_res[:ndt] * 1000.0 * wt # residuals in ms, weighted
189
+
190
+ # ---- Step 3: Weight G matrix rows ----
191
+ # Fortran matmult3: g(i,j) = wt(i) * g(i,j)
192
+ G[:ndt, :] *= wt[:, np.newaxis]
193
+
194
+ # ---- Step 4: Add 4 mean-shift constraint rows ----
195
+ # Fortran: g(ndt+i, j*4-4+i) = 1.0 for i=1..4, j=1..nev
196
+ # 0-indexed: G[ndt+p, j*4+p] = 1.0 for p=0..3, j=0..nev-1
197
+ for p in range(4):
198
+ d[ndt + p] = 0.0
199
+ G[ndt + p, :] = 0.0
200
+ for j in range(nev):
201
+ G[ndt + p, j * 4 + p] = 1.0
202
+ nndt = ndt + 4
203
+
204
+ # ---- Step 5: Add ev_fix constraint rows ----
205
+ nndt = _add_fix_constraint_rows(G, d, nndt, nev, ev_fix, damp)
206
+
207
+ # Trim to actual size
208
+ G_full = G[:nndt, :]
209
+ d_full = d[:nndt]
210
+
211
+ # ---- Step 6: Column scaling ----
212
+ norm = np.sqrt(np.sum(G_full ** 2, axis=0) / nndt)
213
+ # Guard against zero norms
214
+ norm[norm == 0] = 1.0
215
+ G_full /= norm[np.newaxis, :]
216
+
217
+ # ---- Step 7: SVD ----
218
+ # numpy svd returns U (nndt x k), S (k,), Vt (k x ncol) where k = min(nndt, ncol)
219
+ U, S, Vt = np.linalg.svd(G_full, full_matrices=False)
220
+
221
+ # ---- Step 8: Zero small singular values ----
222
+ # Fortran: qmin = qmax * 1e-7
223
+ smax = np.max(S)
224
+ smin = smax * 1e-7
225
+ S_inv = np.where(S > smin, S, 0.0)
226
+
227
+ # ---- Step 9: Back-substitution ----
228
+ # x = V @ diag(1/S) @ U^T @ d
229
+ # Fortran: tmp(j) = sum(u(:,j)*d(:)) / q(j) then x(i) = sum(v(i,j)*tmp(j))
230
+ # numpy Vt = V^T, so V = Vt.T
231
+ tmp = np.zeros(len(S), dtype=np.float64)
232
+ for j in range(len(S)):
233
+ if S_inv[j] != 0.0:
234
+ tmp[j] = np.dot(U[:, j], d_full) / S_inv[j]
235
+ else:
236
+ tmp[j] = 0.0
237
+
238
+ # x = V @ tmp = Vt.T @ tmp
239
+ x = Vt.T @ tmp
240
+
241
+ # ---- Step 10: Rescale solution and G ----
242
+ x /= norm
243
+
244
+ # Rescale G back for residual computation
245
+ G_full *= norm[np.newaxis, :]
246
+
247
+ # Unweight G for the data rows (first ndt rows)
248
+ # Fortran matmult3 with wtinv: g(i,j) = wtinv(i) * g(i,j)
249
+ wtinv = np.where(wt != 0, 1.0 / wt, 1.0)
250
+ G_full[:ndt, :] *= wtinv[:, np.newaxis]
251
+
252
+ # ---- Step 11: Update residuals ----
253
+ # dd = G_unweighted @ x (only first ndt rows matter)
254
+ dd = G_full[:ndt, :] @ x
255
+ dt_res[:ndt] -= dd / 1000.0
256
+
257
+ # ---- Step 12: Covariance and errors ----
258
+ # cvm = V @ diag(1/S^2) @ V^T
259
+ # Fortran: cvm(i,j) = sum_k v(i,k)*v(j,k)/q(k)^2 where q(k)!=0
260
+ V = Vt.T # (ncol, k)
261
+ s2_inv = np.where(S_inv != 0, 1.0 / (S_inv ** 2), 0.0)
262
+ # cvm = V @ diag(s2_inv) @ V^T
263
+ cvm_diag = np.sum(V ** 2 * s2_inv[np.newaxis, :], axis=1)
264
+
265
+ # Get residual statistics
266
+ rms_cc, rms_ct, rms_cc0, rms_ct0, resvar = resstat(
267
+ ndt, nev, dt_res, dt_wt, dt_idx, idata
268
+ )
269
+
270
+ # Errors at 95% confidence level (factor = 1.96)
271
+ factor = 1.96
272
+ se = np.sqrt(cvm_diag) * np.sqrt(abs(resvar)) * factor
273
+
274
+ # Rescale errors
275
+ se /= norm
276
+
277
+ # ---- Step 13: Store results (note negation!) ----
278
+ src_dx = np.zeros(nev, dtype=np.float64)
279
+ src_dy = np.zeros(nev, dtype=np.float64)
280
+ src_dz = np.zeros(nev, dtype=np.float64)
281
+ src_dt = np.zeros(nev, dtype=np.float64)
282
+ src_ex = np.zeros(nev, dtype=np.float64)
283
+ src_ey = np.zeros(nev, dtype=np.float64)
284
+ src_ez = np.zeros(nev, dtype=np.float64)
285
+ src_et = np.zeros(nev, dtype=np.float64)
286
+
287
+ for i in range(nev):
288
+ # Fortran: src_dx(i) = -x(4*i-3) -> 0-indexed: -x[4*i+0]
289
+ src_dx[i] = -x[4 * i + 0]
290
+ src_dy[i] = -x[4 * i + 1]
291
+ src_dz[i] = -x[4 * i + 2]
292
+ src_dt[i] = -x[4 * i + 3]
293
+ src_ex[i] = se[4 * i + 0]
294
+ src_ey[i] = se[4 * i + 1]
295
+ src_ez[i] = se[4 * i + 2]
296
+ src_et[i] = se[4 * i + 3]
297
+
298
+ return {
299
+ 'src_dx': src_dx,
300
+ 'src_dy': src_dy,
301
+ 'src_dz': src_dz,
302
+ 'src_dt': src_dt,
303
+ 'src_ex': src_ex,
304
+ 'src_ey': src_ey,
305
+ 'src_ez': src_ez,
306
+ 'src_et': src_et,
307
+ 'src_cusp': ev_cusp[:nev].copy(),
308
+ 'rms_cc': rms_cc,
309
+ 'rms_ct': rms_ct,
310
+ 'rms_cc0': rms_cc0,
311
+ 'rms_ct0': rms_ct0,
312
+ }
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.4
2
+ Name: hyperdd
3
+ Version: 0.1.0
4
+ Summary: Scalable double-difference earthquake relocation with sparse graph optimization
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/AI4EPS/HyperDD
7
+ Requires-Python: >=3.10
8
+ Requires-Dist: numpy>=1.22
9
+ Requires-Dist: scipy>=1.8
10
+ Requires-Dist: numba>=0.56
11
+ Provides-Extra: fast
12
+ Requires-Dist: scikit-sparse>=0.4; extra == "fast"
13
+ Provides-Extra: metis
14
+ Requires-Dist: pymetis>=2023.1; extra == "metis"
15
+ Provides-Extra: test
16
+ Requires-Dist: pytest>=7.0; extra == "test"
@@ -0,0 +1,35 @@
1
+ hyperdd/__init__.py,sha256=FF4boxFrngX8PzsyUgi6Ih970nM-cu71gRm9-69wq6w,88
2
+ hyperdd/config.py,sha256=qmDqxT46EU21vBjsSjl7BtON2rEDzhG2S6mqUkuJ58g,6465
3
+ hyperdd/hypoDD.py,sha256=gTxtykiqyTEwgwcq-4WhfVvBSGRKMUHwg_8TSRNJ144,43948
4
+ hyperdd/core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ hyperdd/core/cluster.py,sha256=HsHX36GvSLTl9ybwHmwq1AQdbilK4NR6lK9gyZyrrBg,1948
6
+ hyperdd/core/datatypes.py,sha256=jVRBKoCxrNGV1vDehCRa92za4-mjKZ5rewJh7Xr_MF8,2257
7
+ hyperdd/core/dtres.py,sha256=VDJXNio06JEUEIvsn87kjtoz9h8YluHaoI071hvzPRc,1450
8
+ hyperdd/core/jacobian.py,sha256=XrmwdY5N9KXyxpVjnZPain64meBCwdYzp12AySDB9XE,5975
9
+ hyperdd/core/kernels.py,sha256=ok0iMyjSOAe5bzmBhZCdHrGEmmR0x-5iEqiX4Mx_gPs,1931
10
+ hyperdd/core/partials.py,sha256=P37OJL0G5cEhnOpTAwrT_KvljM-KtyI2LkCon_QQD_c,4849
11
+ hyperdd/core/partials_obs.py,sha256=S2Up0tvVD54UImcWrlw-qktZXamogHXl-8Q-dRxpbk8,8303
12
+ hyperdd/core/partition.py,sha256=YQqOoiY2pKdtKZjPH9abPOH0d6gYweOec_bomXxZj8c,5534
13
+ hyperdd/core/skip.py,sha256=fr7aHsSk5Ikvfhf14eKnOkvv_QPulKdIrHDKo4TGuK8,3029
14
+ hyperdd/core/weighting.py,sha256=VxbDh1qFl5Sc79a9iaHmF3GGkkF9DCW3TUW1k4n1TN0,3261
15
+ hyperdd/geodesy/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
+ hyperdd/geodesy/coordinates.py,sha256=zFSd7UKzZb5m_gLGBzAvzOYfm6fV8RBef79S6Mu0suc,10300
17
+ hyperdd/geodesy/delaz_vec.py,sha256=55JaRwJ0g-yja5vRLK7noQqtP57qnaUf20V0eizQ6xg,1986
18
+ hyperdd/io/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
+ hyperdd/io/readers.py,sha256=lHY9kBShtjo7KqNGBqEMrxmXts1-NJ6UcKus4qRrDjA,8529
20
+ hyperdd/io/writers.py,sha256=MewwoGeS8XJ8wjN-Ws_kCl0N3seKy1WKTTnXJahA3N4,5426
21
+ hyperdd/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
+ hyperdd/models/velocity.py,sha256=CbfrvVqdq-kaXhwwQ2fdLo8dMrxlfUpuaE0loAwCZD4,13134
23
+ hyperdd/models/velocity_numba.py,sha256=jKGxX7LuKEKCGp92ANPOIGCQMoX5aSTKWT05XpxQkHg,6923
24
+ hyperdd/models/velocity_sr.py,sha256=MuRALvNFfUDcBpZ8PxJyXBSI_7Y3Jzy0CkQm1Gb7mI8,6630
25
+ hyperdd/solvers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
+ hyperdd/solvers/lsqr.py,sha256=CNttgBYuUXrokKjasil8s3oZBJ-Rtv76GvpUz_EO4cs,13517
27
+ hyperdd/solvers/schur.py,sha256=8jmPVRG4jqjeW8VyKJYdRh6sl3wYyneU_0q4-3yUxRg,4188
28
+ hyperdd/solvers/sparse_chol.py,sha256=itf7rnCSejrQk-CpkAgheofGk5x4_pL8nKKSO4e5TmA,7347
29
+ hyperdd/solvers/stats.py,sha256=krLQZ_KOwBpsxhnw1bZ8DLHuGYgHKpIuhWb1CjzIxBk,3300
30
+ hyperdd/solvers/svd.py,sha256=QR0HcZQALA0pUn5nHiGXx1Pg0wmtrbEETM_qqVfo67g,9732
31
+ hyperdd-0.1.0.dist-info/METADATA,sha256=X7yf2a2fEN-mkrbeh4_k8XnVVjSZDCuOzFUkn5Q37WY,522
32
+ hyperdd-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
33
+ hyperdd-0.1.0.dist-info/entry_points.txt,sha256=kITuQXJg6wT6XDQ-01M1m9S6l-TIHSFNV5pb9WE2U8c,48
34
+ hyperdd-0.1.0.dist-info/top_level.txt,sha256=3kbPhhomE0SUwaIeCCEGpxKgitY8RF9iZv_e9hY0yaE,8
35
+ hyperdd-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ hyperdd = hyperdd.hypoDD:main
@@ -0,0 +1 @@
1
+ hyperdd