hyperdd 0.1__0-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,126 @@
1
+ import numpy as np
2
+
3
+ from ..geodesy.coordinates import delaz
4
+ from ..geodesy.delaz_vec import delaz_vec
5
+ from ..models.velocity import ttime
6
+ from ..models.velocity_numba import ttime_numba, HAS_NUMBA
7
+
8
+
9
+ # Select the fastest ttime implementation
10
+ _ttime = ttime_numba if HAS_NUMBA else ttime
11
+
12
+
13
+ def compute_partials(nsrc, src_lat, src_lon, src_dep, nsta, sta_lat, sta_lon, sta_elv,
14
+ mod_nl, mod_ratio, mod_v, mod_top, imod=1):
15
+ """Compute travel times and partial derivatives for all station-source pairs.
16
+
17
+ Uses vectorized delaz for distance/azimuth, Numba-JIT'd ttime for ray
18
+ tracing (when available), and NumPy array operations for partial derivatives.
19
+
20
+ Returns: ttp, tts, xp, yp, zp, xs, ys, zs - all shape (nsta, nsrc)
21
+ """
22
+ PI = np.float32(3.141593)
23
+ vs = mod_v / mod_ratio
24
+
25
+ ttp = np.zeros((nsta, nsrc), dtype=np.float32)
26
+ tts = np.zeros((nsta, nsrc), dtype=np.float32)
27
+ xp = np.zeros((nsta, nsrc), dtype=np.float32)
28
+ yp = np.zeros((nsta, nsrc), dtype=np.float32)
29
+ zp = np.zeros((nsta, nsrc), dtype=np.float32)
30
+ xs = np.zeros((nsta, nsrc), dtype=np.float32)
31
+ ys = np.zeros((nsta, nsrc), dtype=np.float32)
32
+ zs = np.zeros((nsta, nsrc), dtype=np.float32)
33
+
34
+ # Make sure hypocenters don't fall on layer boundaries
35
+ for j in range(nsrc):
36
+ for k in range(mod_nl):
37
+ if abs(src_dep[j] - mod_top[k]) < 0.0001:
38
+ src_dep[j] = src_dep[j] - 0.001
39
+
40
+ # Vectorized distance and azimuth for all pairs: (nsta, nsrc)
41
+ dist_all, az_all = delaz_vec(src_lat, src_lon, sta_lat, sta_lon)
42
+
43
+ if imod == 5:
44
+ # Straight-ray: fully vectorized
45
+ vp = float(mod_v[0])
46
+ vs1 = float(vs[0])
47
+
48
+ dep_elv = (src_dep[np.newaxis, :] + sta_elv[:, np.newaxis]).astype(np.float64)
49
+ r = np.sqrt(dist_all**2 + dep_elv**2)
50
+ ttp[:] = (r / vp).astype(np.float32)
51
+ tts[:] = (r / vs1).astype(np.float32)
52
+
53
+ above = src_dep[np.newaxis, :] >= -sta_elv[:, np.newaxis]
54
+ ain = np.where(above,
55
+ 180.0 - np.arctan(dist_all / np.abs(dep_elv)) * 57.2958,
56
+ np.arctan(dist_all / np.abs(dep_elv)) * 57.2958)
57
+
58
+ ain_rad = (ain * PI / 180.0).astype(np.float32)
59
+ az_rad = (az_all * PI / 180.0).astype(np.float32)
60
+ az90_rad = ((az_all - 90) * PI / 180.0).astype(np.float32)
61
+ sin_ain = np.sin(ain_rad)
62
+ cos_ain = np.cos(ain_rad)
63
+
64
+ xp[:] = sin_ain * np.cos(az90_rad) / vp
65
+ yp[:] = sin_ain * np.cos(az_rad) / vp
66
+ zp[:] = cos_ain / vp
67
+ xs[:] = sin_ain * np.cos(az90_rad) / vs1
68
+ ys[:] = sin_ain * np.cos(az_rad) / vs1
69
+ zs[:] = cos_ain / vs1
70
+
71
+ below = ~above
72
+ zp[below] = -zp[below]
73
+ zs[below] = -zs[below]
74
+ return ttp, tts, xp, yp, zp, xs, ys, zs
75
+
76
+ # 1D layered model (imod=0 or 1)
77
+ v_p = np.ascontiguousarray(mod_v, dtype=np.float64)
78
+ v_s = np.ascontiguousarray(vs, dtype=np.float64)
79
+
80
+ # Find source layer for partial derivatives (using original model, not adjusted)
81
+ k_layers = np.searchsorted(mod_top, src_dep, side='right') - 1
82
+ k_layers = np.clip(k_layers, 0, mod_nl - 1)
83
+
84
+ # Precompute all travel times: loop over stations (model varies per station)
85
+ ainp_all = np.zeros((nsta, nsrc), dtype=np.float64)
86
+ ains_all = np.zeros((nsta, nsrc), dtype=np.float64)
87
+
88
+ for i in range(nsta):
89
+ mod_top_adj = mod_top.copy()
90
+ mod_top_adj[0] = 0.0
91
+ for k in range(1, mod_nl):
92
+ mod_top_adj[k] = mod_top[k] + sta_elv[i]
93
+ top_adj = np.ascontiguousarray(mod_top_adj, dtype=np.float64)
94
+
95
+ for j in range(nsrc):
96
+ d = float(dist_all[i, j])
97
+ dep = float(src_dep[j] + sta_elv[i])
98
+ ttp[i, j], ainp_all[i, j] = _ttime(d, dep, mod_nl, v_p, top_adj)
99
+ tts[i, j], ains_all[i, j] = _ttime(d, dep, mod_nl, v_s, top_adj)
100
+
101
+ # Vectorized partial derivatives
102
+ ainp_rad = (ainp_all * PI / 180.0).astype(np.float32)
103
+ ains_rad = (ains_all * PI / 180.0).astype(np.float32)
104
+ az_rad = (az_all * PI / 180.0).astype(np.float32)
105
+ az90_rad = ((az_all - 90) * PI / 180.0).astype(np.float32)
106
+
107
+ # Layer velocities for each source: shape (nsrc,)
108
+ vp_src = mod_v[k_layers] # (nsrc,)
109
+ vs_src = vs[k_layers] # (nsrc,)
110
+
111
+ # Broadcast to (nsta, nsrc)
112
+ sin_ainp = np.sin(ainp_rad)
113
+ cos_ainp = np.cos(ainp_rad)
114
+ sin_ains = np.sin(ains_rad)
115
+ cos_ains = np.cos(ains_rad)
116
+ cos_az = np.cos(az_rad)
117
+ cos_az90 = np.cos(az90_rad)
118
+
119
+ zp[:] = cos_ainp / vp_src[np.newaxis, :]
120
+ zs[:] = cos_ains / vs_src[np.newaxis, :]
121
+ xp[:] = sin_ainp * cos_az90 / vp_src[np.newaxis, :]
122
+ xs[:] = sin_ains * cos_az90 / vs_src[np.newaxis, :]
123
+ yp[:] = sin_ainp * cos_az / vp_src[np.newaxis, :]
124
+ ys[:] = sin_ains * cos_az / vs_src[np.newaxis, :]
125
+
126
+ return ttp, tts, xp, yp, zp, xs, ys, zs
@@ -0,0 +1,243 @@
1
+ """Observation-centric travel time and partial derivative computation.
2
+
3
+ Instead of computing travel times for ALL (station, event) pairs -- O(nsta * nsrc) --
4
+ this module computes only the pairs that appear in actual observations -- O(n_unique_pairs).
5
+
6
+ For 308 events, 175 stations, 157K observations:
7
+ Old: 308 * 175 = 53,900 ttime calls
8
+ New: ~20,000 unique (station, event) pairs (from deduplication)
9
+ Savings: ~2.7x fewer ttime calls, and NO (nsta, nsrc) arrays allocated
10
+
11
+ The savings grow dramatically at scale:
12
+ 100K events, 1000 stations: old = 100M calls, new = ~2M unique pairs = 50x savings
13
+ """
14
+
15
+ import numpy as np
16
+
17
+ from ..geodesy.coordinates import delaz
18
+ from ..geodesy.delaz_vec import delaz_vec
19
+ from ..models.velocity import ttime
20
+ from ..models.velocity_numba import ttime_numba, HAS_NUMBA
21
+
22
+ _ttime = ttime_numba if HAS_NUMBA else ttime
23
+
24
+
25
+ def _delaz_pairs(alat, alon, blat, blon):
26
+ """Vectorized distance and azimuth for paired points (not broadcast).
27
+
28
+ Uses EXACTLY the same math as delaz_vec but element-wise instead of
29
+ broadcasting. dist[i] = distance(a[i], b[i]).
30
+
31
+ Returns: (dist_km, az_deg) both shape (n,)
32
+ """
33
+ rad = 0.01745329
34
+ pi2 = 1.570796
35
+ flat = 0.993231
36
+
37
+ alat = np.asarray(alat, dtype=np.float64)
38
+ alon = np.asarray(alon, dtype=np.float64)
39
+ blat = np.asarray(blat, dtype=np.float64)
40
+ blon = np.asarray(blon, dtype=np.float64)
41
+
42
+ alatr = alat * rad
43
+ alonr = alon * rad
44
+ blatr = blat * rad
45
+ blonr = blon * rad
46
+
47
+ # Geocentric colatitudes (same as delaz_vec)
48
+ acol = pi2 - np.arctan(flat * np.tan(alatr))
49
+ bcol = pi2 - np.arctan(flat * np.tan(blatr))
50
+
51
+ # Central angle
52
+ diflon = blonr - alonr
53
+ cosdel = (np.sin(acol) * np.sin(bcol) * np.cos(diflon)
54
+ + np.cos(acol) * np.cos(bcol))
55
+ cosdel = np.clip(cosdel, -1.0, 1.0)
56
+ delr = np.arccos(cosdel)
57
+
58
+ # Azimuth
59
+ top = np.sin(diflon)
60
+ den = np.sin(acol) / np.tan(bcol) - np.cos(diflon) * np.cos(acol)
61
+ azr = np.arctan2(top, den)
62
+ az = azr / rad
63
+ az = np.where(az < 0.0, 360.0 + az, az)
64
+
65
+ # Distance in km (latitude-dependent radius, same as delaz_vec)
66
+ colat = pi2 - (alatr + blatr) / 2.0
67
+ radius = 6378.140 * (1.0 - 3.37853e-3 * np.cos(colat) ** 2)
68
+ dist = delr * radius
69
+
70
+ return dist, az
71
+
72
+
73
+ def compute_partials_obs(nsrc, src_lat, src_lon, src_dep,
74
+ nsta, sta_lat, sta_lon, sta_elv,
75
+ mod_nl, mod_ratio, mod_v, mod_top,
76
+ ndt, dt_ista, dt_ic1, dt_ic2, dt_idx,
77
+ imod=1):
78
+ """Compute travel times and partials ONLY for observed (station, event) pairs.
79
+
80
+ Returns the same (nsta, nsrc) arrays as compute_partials, but only fills
81
+ the entries that are actually used by observations. Unused entries are zero.
82
+
83
+ Additional parameters vs compute_partials:
84
+ ndt, dt_ista, dt_ic1, dt_ic2, dt_idx: observation arrays
85
+ """
86
+ PI = np.float32(3.141593)
87
+ vs = mod_v / mod_ratio
88
+
89
+ ttp = np.zeros((nsta, nsrc), dtype=np.float32)
90
+ tts = np.zeros((nsta, nsrc), dtype=np.float32)
91
+ xp = np.zeros((nsta, nsrc), dtype=np.float32)
92
+ yp = np.zeros((nsta, nsrc), dtype=np.float32)
93
+ zp = np.zeros((nsta, nsrc), dtype=np.float32)
94
+ xs = np.zeros((nsta, nsrc), dtype=np.float32)
95
+ ys = np.zeros((nsta, nsrc), dtype=np.float32)
96
+ zs = np.zeros((nsta, nsrc), dtype=np.float32)
97
+
98
+ # Make sure hypocenters don't fall on layer boundaries
99
+ for j in range(nsrc):
100
+ for k in range(mod_nl):
101
+ if abs(src_dep[j] - mod_top[k]) < 0.0001:
102
+ src_dep[j] = src_dep[j] - 0.001
103
+
104
+ # Find unique (station, event) pairs from observations
105
+ ista = dt_ista[:ndt]
106
+ ic1 = dt_ic1[:ndt]
107
+ ic2 = dt_ic2[:ndt]
108
+
109
+ # Each observation needs (station, event_i) and (station, event_j)
110
+ sta_arr = np.concatenate([ista, ista])
111
+ evt_arr = np.concatenate([ic1, ic2])
112
+
113
+ # Deduplicate
114
+ pairs = np.stack([sta_arr, evt_arr], axis=1)
115
+ unique_pairs = np.unique(pairs, axis=0)
116
+ n_unique = len(unique_pairs)
117
+ u_sta = unique_pairs[:, 0]
118
+ u_evt = unique_pairs[:, 1]
119
+
120
+ if imod == 5:
121
+ # Straight-ray: vectorized over unique pairs
122
+ vp = float(mod_v[0])
123
+ vs1 = float(vs[0])
124
+
125
+ # Distance for unique pairs (vectorized)
126
+ u_dist, u_az = _delaz_pairs(
127
+ src_lat[u_evt], src_lon[u_evt], sta_lat[u_sta], sta_lon[u_sta]
128
+ )
129
+
130
+ u_dep_elv = (src_dep[u_evt] + sta_elv[u_sta]).astype(np.float64)
131
+ u_r = np.sqrt(u_dist**2 + u_dep_elv**2)
132
+
133
+ u_ttp = (u_r / vp).astype(np.float32)
134
+ u_tts = (u_r / vs1).astype(np.float32)
135
+
136
+ above = src_dep[u_evt] >= -sta_elv[u_sta]
137
+ u_ain = np.where(above,
138
+ 180.0 - np.arctan(u_dist / np.abs(u_dep_elv)) * 57.2958,
139
+ np.arctan(u_dist / np.abs(u_dep_elv)) * 57.2958)
140
+
141
+ u_ain_rad = (u_ain * PI / 180.0).astype(np.float32)
142
+ u_az_rad = (u_az * PI / 180.0).astype(np.float32)
143
+ u_az90_rad = ((u_az - 90) * PI / 180.0).astype(np.float32)
144
+ sin_ain = np.sin(u_ain_rad)
145
+ cos_ain = np.cos(u_ain_rad)
146
+
147
+ u_xp = sin_ain * np.cos(u_az90_rad) / vp
148
+ u_yp = sin_ain * np.cos(u_az_rad) / vp
149
+ u_zp = cos_ain / vp
150
+ u_xs = sin_ain * np.cos(u_az90_rad) / vs1
151
+ u_ys = sin_ain * np.cos(u_az_rad) / vs1
152
+ u_zs = cos_ain / vs1
153
+
154
+ below = ~above
155
+ u_zp[below] = -u_zp[below]
156
+ u_zs[below] = -u_zs[below]
157
+
158
+ # Scatter back to (nsta, nsrc) arrays
159
+ ttp[u_sta, u_evt] = u_ttp
160
+ tts[u_sta, u_evt] = u_tts
161
+ xp[u_sta, u_evt] = u_xp
162
+ yp[u_sta, u_evt] = u_yp
163
+ zp[u_sta, u_evt] = u_zp
164
+ xs[u_sta, u_evt] = u_xs
165
+ ys[u_sta, u_evt] = u_ys
166
+ zs[u_sta, u_evt] = u_zs
167
+
168
+ return ttp, tts, xp, yp, zp, xs, ys, zs
169
+
170
+ # 1D layered model (imod=0 or 1)
171
+ v_p = np.ascontiguousarray(mod_v, dtype=np.float64)
172
+ v_s = np.ascontiguousarray(vs, dtype=np.float64)
173
+
174
+ k_layers = np.searchsorted(mod_top, src_dep, side='right') - 1
175
+ k_layers = np.clip(k_layers, 0, mod_nl - 1)
176
+
177
+ # Precompute adjusted model tops per station (cache)
178
+ sta_top_adj = {}
179
+
180
+ # Compute distance/azimuth for unique pairs (vectorized)
181
+ u_dist, u_az = _delaz_pairs(
182
+ src_lat[u_evt], src_lon[u_evt], sta_lat[u_sta], sta_lon[u_sta]
183
+ )
184
+
185
+ # Compute travel times for unique pairs
186
+ u_ainp = np.empty(n_unique, dtype=np.float64)
187
+ u_ains = np.empty(n_unique, dtype=np.float64)
188
+ u_ttp = np.empty(n_unique, dtype=np.float32)
189
+ u_tts = np.empty(n_unique, dtype=np.float32)
190
+
191
+ for k in range(n_unique):
192
+ si = int(u_sta[k])
193
+ ei = int(u_evt[k])
194
+
195
+ # Cache adjusted model tops per station
196
+ if si not in sta_top_adj:
197
+ mod_top_adj = mod_top.copy()
198
+ mod_top_adj[0] = 0.0
199
+ for kk in range(1, mod_nl):
200
+ mod_top_adj[kk] = mod_top[kk] + sta_elv[si]
201
+ sta_top_adj[si] = np.ascontiguousarray(mod_top_adj, dtype=np.float64)
202
+
203
+ top_adj = sta_top_adj[si]
204
+ d = float(u_dist[k])
205
+ dep = float(src_dep[ei] + sta_elv[si])
206
+
207
+ u_ttp[k], u_ainp[k] = _ttime(d, dep, mod_nl, v_p, top_adj)
208
+ u_tts[k], u_ains[k] = _ttime(d, dep, mod_nl, v_s, top_adj)
209
+
210
+ # Vectorized partial derivatives for unique pairs
211
+ vp_src = mod_v[k_layers[u_evt]]
212
+ vs_src = vs[k_layers[u_evt]]
213
+
214
+ ainp_rad = (u_ainp * PI / 180.0).astype(np.float32)
215
+ ains_rad = (u_ains * PI / 180.0).astype(np.float32)
216
+ az_rad = (u_az * PI / 180.0).astype(np.float32)
217
+ az90_rad = ((u_az - 90) * PI / 180.0).astype(np.float32)
218
+
219
+ sin_ainp = np.sin(ainp_rad)
220
+ cos_ainp = np.cos(ainp_rad)
221
+ sin_ains = np.sin(ains_rad)
222
+ cos_ains = np.cos(ains_rad)
223
+ cos_az = np.cos(az_rad)
224
+ cos_az90 = np.cos(az90_rad)
225
+
226
+ u_zp = cos_ainp / vp_src
227
+ u_zs = cos_ains / vs_src
228
+ u_xp = sin_ainp * cos_az90 / vp_src
229
+ u_xs = sin_ains * cos_az90 / vs_src
230
+ u_yp = sin_ainp * cos_az / vp_src
231
+ u_ys = sin_ains * cos_az / vs_src
232
+
233
+ # Scatter back to (nsta, nsrc) arrays
234
+ ttp[u_sta, u_evt] = u_ttp
235
+ tts[u_sta, u_evt] = u_tts
236
+ xp[u_sta, u_evt] = u_xp
237
+ yp[u_sta, u_evt] = u_yp
238
+ zp[u_sta, u_evt] = u_zp
239
+ xs[u_sta, u_evt] = u_xs
240
+ ys[u_sta, u_evt] = u_ys
241
+ zs[u_sta, u_evt] = u_zs
242
+
243
+ return ttp, tts, xp, yp, zp, xs, ys, zs
@@ -0,0 +1,158 @@
1
+ """Graph partitioning for large clusters using METIS.
2
+
3
+ Splits a large cluster into smaller subgraphs with minimal edge cuts.
4
+ Events on partition boundaries are classified as "boundary" events.
5
+ The Schur complement approach then:
6
+ 1. Eliminates interior events per partition (independent, parallel)
7
+ 2. Solves a smaller boundary system
8
+ 3. Back-substitutes to recover interior solutions
9
+ """
10
+
11
+ import numpy as np
12
+
13
+ try:
14
+ import pymetis
15
+ HAS_METIS = True
16
+ except ImportError:
17
+ HAS_METIS = False
18
+
19
+
20
+ def partition_cluster(nev, ndt, dt_ic1, dt_ic2, max_partition_size=5000, n_parts=None):
21
+ """Partition event graph using METIS.
22
+
23
+ Parameters
24
+ ----------
25
+ nev : int
26
+ Number of events in cluster.
27
+ ndt : int
28
+ Number of observations.
29
+ dt_ic1, dt_ic2 : ndarray[int32]
30
+ Event pair indices (0-based) per observation.
31
+ max_partition_size : int
32
+ Maximum events per partition. If nev <= this, no partitioning.
33
+ n_parts : int or None
34
+ Number of partitions. If None, computed from max_partition_size.
35
+
36
+ Returns
37
+ -------
38
+ partition_info : dict or None
39
+ None if no partitioning needed. Otherwise:
40
+ - n_parts: number of partitions
41
+ - event_partition: ndarray[int32] (nev,) partition ID per event
42
+ - interior_masks: list of bool arrays, one per partition
43
+ - boundary_mask: bool array for boundary events
44
+ - boundary_indices: int array of boundary event indices
45
+ - interior_indices: list of int arrays, one per partition
46
+ - obs_partition: ndarray[int32] (ndt,) partition ID per observation
47
+ (-1 for cross-partition observations that go to boundary system)
48
+ """
49
+ if nev <= max_partition_size:
50
+ return None
51
+
52
+ if not HAS_METIS:
53
+ raise ImportError("pymetis required for graph partitioning. Install: conda install -c conda-forge pymetis")
54
+
55
+ if n_parts is None:
56
+ n_parts = max(2, (nev + max_partition_size - 1) // max_partition_size)
57
+
58
+ # Build adjacency list for METIS
59
+ ic1 = dt_ic1[:ndt]
60
+ ic2 = dt_ic2[:ndt]
61
+ adjacency = [set() for _ in range(nev)]
62
+ for k in range(ndt):
63
+ i, j = int(ic1[k]), int(ic2[k])
64
+ adjacency[i].add(j)
65
+ adjacency[j].add(i)
66
+
67
+ # Convert to list of numpy arrays (pymetis format)
68
+ adj_list = [np.array(sorted(neighbors), dtype=np.int32) for neighbors in adjacency]
69
+
70
+ # Run METIS
71
+ edge_cuts, partition_labels = pymetis.part_graph(n_parts, adjacency=adj_list)
72
+ event_partition = np.array(partition_labels, dtype=np.int32)
73
+
74
+ # Identify boundary events: events with neighbors in a different partition
75
+ boundary_mask = np.zeros(nev, dtype=bool)
76
+ for k in range(ndt):
77
+ i, j = int(ic1[k]), int(ic2[k])
78
+ if event_partition[i] != event_partition[j]:
79
+ boundary_mask[i] = True
80
+ boundary_mask[j] = True
81
+
82
+ boundary_indices = np.where(boundary_mask)[0]
83
+ n_boundary = len(boundary_indices)
84
+
85
+ # Interior events per partition
86
+ interior_masks = []
87
+ interior_indices = []
88
+ for p in range(n_parts):
89
+ in_partition = event_partition == p
90
+ interior = in_partition & ~boundary_mask
91
+ interior_masks.append(interior)
92
+ interior_indices.append(np.where(interior)[0])
93
+
94
+ # Classify observations
95
+ # obs belongs to partition p if both events are interior to p
96
+ # obs is "boundary" if either event is on boundary
97
+ obs_partition = np.full(ndt, -1, dtype=np.int32)
98
+ for k in range(ndt):
99
+ i, j = int(ic1[k]), int(ic2[k])
100
+ pi, pj = event_partition[i], event_partition[j]
101
+ if pi == pj and not boundary_mask[i] and not boundary_mask[j]:
102
+ obs_partition[k] = pi
103
+
104
+ n_interior = sum(len(idx) for idx in interior_indices)
105
+
106
+ return {
107
+ "n_parts": n_parts,
108
+ "edge_cuts": edge_cuts,
109
+ "event_partition": event_partition,
110
+ "interior_masks": interior_masks,
111
+ "boundary_mask": boundary_mask,
112
+ "boundary_indices": boundary_indices,
113
+ "interior_indices": interior_indices,
114
+ "obs_partition": obs_partition,
115
+ "n_boundary": n_boundary,
116
+ "n_interior": n_interior,
117
+ }
118
+
119
+
120
+ def build_reorder_map(partition_info):
121
+ """Build event reordering: [interior_1, interior_2, ..., interior_K, boundary].
122
+
123
+ Returns
124
+ -------
125
+ old_to_new : ndarray[int32] (nev,)
126
+ Maps original event index to reordered index.
127
+ new_to_old : ndarray[int32] (nev,)
128
+ Maps reordered index to original event index.
129
+ n_interior_cumsum : list[int]
130
+ Cumulative count of interior events per partition.
131
+ n_interior_cumsum[p] is the start index of partition p's interiors.
132
+ boundary_start : int
133
+ Index where boundary events start in the reordered system.
134
+ """
135
+ interior_indices = partition_info["interior_indices"]
136
+ boundary_indices = partition_info["boundary_indices"]
137
+ n_parts = partition_info["n_parts"]
138
+
139
+ nev = partition_info["n_interior"] + partition_info["n_boundary"]
140
+ old_to_new = np.empty(nev, dtype=np.int32)
141
+ new_to_old = np.empty(nev, dtype=np.int32)
142
+
143
+ idx = 0
144
+ n_interior_cumsum = [0]
145
+ for p in range(n_parts):
146
+ for old_idx in interior_indices[p]:
147
+ old_to_new[old_idx] = idx
148
+ new_to_old[idx] = old_idx
149
+ idx += 1
150
+ n_interior_cumsum.append(idx)
151
+
152
+ boundary_start = idx
153
+ for old_idx in boundary_indices:
154
+ old_to_new[old_idx] = idx
155
+ new_to_old[idx] = old_idx
156
+ idx += 1
157
+
158
+ return old_to_new, new_to_old, n_interior_cumsum, boundary_start
hyperdd/core/skip.py ADDED
@@ -0,0 +1,83 @@
1
+ import numpy as np
2
+
3
+
4
+ def skip_outliers(ndt, nev, nsrc, nsta, minwght,
5
+ dt_dt, dt_qual, dt_c1, dt_c2, dt_idx, dt_ista, dt_ic1, dt_ic2,
6
+ dt_offs, dt_cal, dt_res, dt_wt,
7
+ ev_cusp, src_cusp, src_lat, src_lon, src_dep,
8
+ src_x, src_y, src_z, src_t,
9
+ sta_lab, sta_lat, sta_lon, sta_elv,
10
+ tmp_ttp, tmp_tts, tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs):
11
+ """Skip data with weight < minwght. Compact arrays and reindex.
12
+ Returns new ndt, nev, nsrc, nsta."""
13
+
14
+ # Keep mask for observations
15
+ mask = dt_wt[:ndt] >= minwght
16
+ new_ndt = int(np.sum(mask))
17
+
18
+ if new_ndt == ndt:
19
+ return ndt, nev, nsrc, nsta # nothing to skip
20
+
21
+ # Compact observation arrays
22
+ indices = np.where(mask)[0]
23
+ for arr in [dt_dt, dt_qual, dt_c1, dt_c2, dt_idx, dt_ista, dt_ic1, dt_ic2,
24
+ dt_offs, dt_cal, dt_res, dt_wt]:
25
+ arr[:new_ndt] = arr[indices]
26
+
27
+ # Find events still in data
28
+ used_ic = np.union1d(dt_ic1[:new_ndt], dt_ic2[:new_ndt])
29
+ used_ic = np.unique(used_ic)
30
+ new_nev = len(used_ic)
31
+
32
+ if new_nev < nev:
33
+ # Create old->new index mapping
34
+ old_to_new = np.full(nev, -1, dtype=np.int32)
35
+ for new_idx, old_idx in enumerate(used_ic):
36
+ old_to_new[old_idx] = new_idx
37
+
38
+ # Compact event arrays
39
+ for arr in [ev_cusp]:
40
+ arr[:new_nev] = arr[used_ic]
41
+
42
+ # Compact source arrays
43
+ new_nsrc = new_nev if nsrc > 1 else 1
44
+ if nsrc > 1:
45
+ for arr in [src_cusp, src_lat, src_lon, src_dep, src_x, src_y, src_z, src_t]:
46
+ arr[:new_nsrc] = arr[used_ic]
47
+
48
+ # Remap ic1, ic2 indices (vectorized)
49
+ dt_ic1[:new_ndt] = old_to_new[dt_ic1[:new_ndt]]
50
+ dt_ic2[:new_ndt] = old_to_new[dt_ic2[:new_ndt]]
51
+
52
+ # Compact travel time arrays (columns)
53
+ if nsrc > 1:
54
+ for arr in [tmp_ttp, tmp_tts, tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs]:
55
+ arr[:, :new_nsrc] = arr[:, used_ic]
56
+ else:
57
+ new_nsrc = nsrc
58
+
59
+ # Find stations still in data
60
+ used_ista = np.unique(dt_ista[:new_ndt])
61
+ new_nsta = len(used_ista)
62
+
63
+ if new_nsta < nsta:
64
+ old_to_new_sta = np.full(nsta, -1, dtype=np.int32)
65
+ for new_idx, old_idx in enumerate(used_ista):
66
+ old_to_new_sta[old_idx] = new_idx
67
+
68
+ # Compact station arrays
69
+ new_labs = [sta_lab[i] for i in used_ista]
70
+ for idx_new, idx_old in enumerate(used_ista):
71
+ sta_lat[idx_new] = sta_lat[idx_old]
72
+ sta_lon[idx_new] = sta_lon[idx_old]
73
+ sta_elv[idx_new] = sta_elv[idx_old]
74
+ sta_lab[:new_nsta] = new_labs
75
+
76
+ # Compact travel time arrays (rows)
77
+ for arr in [tmp_ttp, tmp_tts, tmp_xp, tmp_yp, tmp_zp, tmp_xs, tmp_ys, tmp_zs]:
78
+ arr[:new_nsta, :] = arr[used_ista, :]
79
+
80
+ # Remap ista indices (vectorized)
81
+ dt_ista[:new_ndt] = old_to_new_sta[dt_ista[:new_ndt]]
82
+
83
+ return new_ndt, new_nev, new_nsrc, new_nsta