ADLoc 1.0.0__tar.gz → 1.0.2__tar.gz

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,14 @@
1
+ Metadata-Version: 2.1
2
+ Name: ADLoc
3
+ Version: 1.0.2
4
+ Description-Content-Type: text/markdown
5
+ Requires-Dist: numpy
6
+ Requires-Dist: h5py
7
+ Requires-Dist: matplotlib
8
+ Requires-Dist: pandas
9
+ Requires-Dist: tqdm
10
+ Requires-Dist: pyproj
11
+ Requires-Dist: numba
12
+ Requires-Dist: scipy
13
+
14
+ ADLoc
@@ -5,3 +5,4 @@ pandas
5
5
  tqdm
6
6
  pyproj
7
7
  numba
8
+ scipy
adloc-1.0.2/PKG-INFO ADDED
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.1
2
+ Name: ADLoc
3
+ Version: 1.0.2
4
+ Description-Content-Type: text/markdown
5
+ Requires-Dist: numpy
6
+ Requires-Dist: h5py
7
+ Requires-Dist: matplotlib
8
+ Requires-Dist: pandas
9
+ Requires-Dist: tqdm
10
+ Requires-Dist: pyproj
11
+ Requires-Dist: numba
12
+ Requires-Dist: scipy
13
+
14
+ ADLoc
@@ -1,11 +1,11 @@
1
- # Author: Johannes Schönberger
2
- #
3
- # License: BSD 3 clause
1
+ # Authors: The scikit-learn developers
2
+ # SPDX-License-Identifier: BSD-3-Clause
4
3
 
5
4
  import warnings
6
5
  from numbers import Integral, Real
7
6
 
8
7
  import numpy as np
8
+
9
9
  import sklearn
10
10
  from sklearn.base import (
11
11
  BaseEstimator,
@@ -17,7 +17,8 @@ from sklearn.base import (
17
17
  )
18
18
  from sklearn.exceptions import ConvergenceWarning
19
19
  from sklearn.linear_model._base import LinearRegression
20
- from sklearn.utils import check_consistent_length, check_random_state
20
+ from sklearn.utils import check_consistent_length, check_random_state, get_tags
21
+ from sklearn.utils._bunch import Bunch
21
22
  from sklearn.utils._param_validation import (
22
23
  HasMethods,
23
24
  Interval,
@@ -26,14 +27,19 @@ from sklearn.utils._param_validation import (
26
27
  StrOptions,
27
28
  )
28
29
  from sklearn.utils.metadata_routing import (
29
- _raise_for_unsupported_routing,
30
- _RoutingNotSupportedMixin,
30
+ MetadataRouter,
31
+ MethodMapping,
32
+ _raise_for_params,
33
+ _routing_enabled,
34
+ process_routing,
31
35
  )
32
36
  from sklearn.utils.random import sample_without_replacement
33
37
  from sklearn.utils.validation import (
38
+ _check_method_params,
34
39
  _check_sample_weight,
35
40
  check_is_fitted,
36
41
  has_fit_parameter,
42
+ validate_data,
37
43
  )
38
44
 
39
45
  _EPSILON = np.spacing(1)
@@ -74,7 +80,6 @@ def _dynamic_max_trials(n_inliers, n_samples, min_samples, probability):
74
80
 
75
81
 
76
82
  class RANSACRegressor(
77
- _RoutingNotSupportedMixin,
78
83
  MetaEstimatorMixin,
79
84
  RegressorMixin,
80
85
  MultiOutputMixin,
@@ -92,13 +97,13 @@ class RANSACRegressor(
92
97
  estimator : object, default=None
93
98
  Base estimator object which implements the following methods:
94
99
 
95
- * `fit(X, y)`: Fit model to given training data and target values.
96
- * `score(X, y)`: Returns the mean accuracy on the given test data,
97
- which is used for the stop criterion defined by `stop_score`.
98
- Additionally, the score is used to decide which of two equally
99
- large consensus sets is chosen as the better one.
100
- * `predict(X)`: Returns predicted values using the linear model,
101
- which is used to compute residual error using loss function.
100
+ * `fit(X, y)`: Fit model to given training data and target values.
101
+ * `score(X, y)`: Returns the mean accuracy on the given test data,
102
+ which is used for the stop criterion defined by `stop_score`.
103
+ Additionally, the score is used to decide which of two equally
104
+ large consensus sets is chosen as the better one.
105
+ * `predict(X)`: Returns predicted values using the linear model,
106
+ which is used to compute residual error using loss function.
102
107
 
103
108
  If `estimator` is None, then
104
109
  :class:`~sklearn.linear_model.LinearRegression` is used for
@@ -187,7 +192,8 @@ class RANSACRegressor(
187
192
  Attributes
188
193
  ----------
189
194
  estimator_ : object
190
- Best fitted model (copy of the `estimator` object).
195
+ Final model fitted on the inliers predicted by the "best" model found
196
+ during RANSAC sampling (copy of the `estimator` object).
191
197
 
192
198
  n_trials_ : int
193
199
  Number of random selection trials until one of the stop criteria is
@@ -234,7 +240,7 @@ class RANSACRegressor(
234
240
  ----------
235
241
  .. [1] https://en.wikipedia.org/wiki/RANSAC
236
242
  .. [2] https://www.sri.com/wp-content/uploads/2021/12/ransac-publication.pdf
237
- .. [3] http://www.bmva.org/bmvc/2009/Papers/Paper355/Paper355.pdf
243
+ .. [3] https://bmva-archive.org.uk/bmvc/2009/Papers/Paper355/Paper355.pdf
238
244
 
239
245
  Examples
240
246
  --------
@@ -244,10 +250,13 @@ class RANSACRegressor(
244
250
  ... n_samples=200, n_features=2, noise=4.0, random_state=0)
245
251
  >>> reg = RANSACRegressor(random_state=0).fit(X, y)
246
252
  >>> reg.score(X, y)
247
- 0.9885...
253
+ 0.9885
248
254
  >>> reg.predict(X[:1,])
249
- array([-31.9417...])
250
- """ # noqa: E501
255
+ array([-31.9417])
256
+
257
+ For a more detailed example, see
258
+ :ref:`sphx_glr_auto_examples_linear_model_plot_ransac.py`
259
+ """
251
260
 
252
261
  _parameter_constraints: dict = {
253
262
  "estimator": [HasMethods(["fit", "score", "predict"]), None],
@@ -310,7 +319,7 @@ class RANSACRegressor(
310
319
  # RansacRegressor.estimator is not validated yet
311
320
  prefer_skip_nested_validation=False
312
321
  )
313
- def fit(self, X, y, sample_weight=None, **kwargs):
322
+ def fit(self, X, y, sample_weight=None, **fit_params):
314
323
  """Fit estimator using RANSAC algorithm.
315
324
 
316
325
  Parameters
@@ -328,6 +337,17 @@ class RANSACRegressor(
328
337
 
329
338
  .. versionadded:: 0.18
330
339
 
340
+ **fit_params : dict
341
+ Parameters routed to the `fit` method of the sub-estimator via the
342
+ metadata routing API.
343
+
344
+ .. versionadded:: 1.5
345
+
346
+ Only available if
347
+ `sklearn.set_config(enable_metadata_routing=True)` is set. See
348
+ :ref:`Metadata Routing User Guide <metadata_routing>` for more
349
+ details.
350
+
331
351
  Returns
332
352
  -------
333
353
  self : object
@@ -340,13 +360,13 @@ class RANSACRegressor(
340
360
  `is_data_valid` and `is_model_valid` return False for all
341
361
  `max_trials` randomly chosen sub-samples.
342
362
  """
343
- _raise_for_unsupported_routing(self, "fit", sample_weight=sample_weight)
344
363
  # Need to validate separately here. We can't pass multi_output=True
345
364
  # because that would allow y to be csr. Delay expensive finiteness
346
365
  # check to the estimator's own input validation.
347
- check_X_params = dict(accept_sparse="csr", force_all_finite=False)
366
+ _raise_for_params(fit_params, self, "fit")
367
+ check_X_params = dict(accept_sparse="csr", ensure_all_finite=False)
348
368
  check_y_params = dict(ensure_2d=False)
349
- X, y = self._validate_data(X, y, validate_separately=(check_X_params, check_y_params))
369
+ X, y = validate_data(self, X, y, validate_separately=(check_X_params, check_y_params))
350
370
  check_consistent_length(X, y)
351
371
 
352
372
  if self.estimator is not None:
@@ -402,12 +422,22 @@ class RANSACRegressor(
402
422
  estimator_name = type(estimator).__name__
403
423
  if sample_weight is not None and not estimator_fit_has_sample_weight:
404
424
  raise ValueError(
405
- "%s does not support sample_weight. Samples"
425
+ "%s does not support sample_weight. Sample"
406
426
  " weights are only used for the calibration"
407
427
  " itself." % estimator_name
408
428
  )
429
+
409
430
  if sample_weight is not None:
410
- sample_weight = _check_sample_weight(sample_weight, X)
431
+ fit_params["sample_weight"] = sample_weight
432
+
433
+ if _routing_enabled():
434
+ routed_params = process_routing(self, "fit", **fit_params)
435
+ else:
436
+ routed_params = Bunch()
437
+ routed_params.estimator = Bunch(fit={}, predict={}, score={})
438
+ if sample_weight is not None:
439
+ sample_weight = _check_sample_weight(sample_weight, X)
440
+ routed_params.estimator.fit = {"sample_weight": sample_weight}
411
441
 
412
442
  n_inliers_best = 1
413
443
  score_best = -np.inf
@@ -422,6 +452,7 @@ class RANSACRegressor(
422
452
  # number of data samples
423
453
  n_samples = X.shape[0]
424
454
  sample_idxs = np.arange(n_samples)
455
+ prob = X[:, 2] / np.sum(X[:, 2]) # X: idx_sta, type, score, amp
425
456
 
426
457
  self.n_trials_ = 0
427
458
  max_trials = self.max_trials
@@ -432,7 +463,8 @@ class RANSACRegressor(
432
463
  break
433
464
 
434
465
  # choose random sample set
435
- subset_idxs = sample_without_replacement(n_samples, min_samples, random_state=random_state)
466
+ # subset_idxs = sample_without_replacement(n_samples, min_samples, random_state=random_state)
467
+ subset_idxs = np.random.choice(sample_idxs, min_samples, replace=False, p=prob)
436
468
  X_subset = X[subset_idxs]
437
469
  y_subset = y[subset_idxs]
438
470
 
@@ -441,11 +473,11 @@ class RANSACRegressor(
441
473
  self.n_skips_invalid_data_ += 1
442
474
  continue
443
475
 
476
+ # cut `fit_params` down to `subset_idxs`
477
+ fit_params_subset = _check_method_params(X, params=routed_params.estimator.fit, indices=subset_idxs)
478
+
444
479
  # fit model for current random sample set
445
- if sample_weight is None:
446
- estimator.fit(X_subset, y_subset, **kwargs)
447
- else:
448
- estimator.fit(X_subset, y_subset, sample_weight=sample_weight[subset_idxs])
480
+ estimator.fit(X_subset, y_subset, **fit_params_subset)
449
481
 
450
482
  # check if estimated model is valid
451
483
  if self.is_model_valid is not None and not self.is_model_valid(estimator, X_subset, y_subset):
@@ -453,7 +485,7 @@ class RANSACRegressor(
453
485
  continue
454
486
 
455
487
  # residuals of all data for current random sample model
456
- y_pred = estimator.predict(X, **kwargs)
488
+ y_pred = estimator.predict(X)
457
489
  residuals_subset = loss_function(y, y_pred)
458
490
 
459
491
  # classify data into inliers and outliers
@@ -471,8 +503,17 @@ class RANSACRegressor(
471
503
  X_inlier_subset = X[inlier_idxs_subset]
472
504
  y_inlier_subset = y[inlier_idxs_subset]
473
505
 
506
+ # cut `fit_params` down to `inlier_idxs_subset`
507
+ score_params_inlier_subset = _check_method_params(
508
+ X, params=routed_params.estimator.score, indices=inlier_idxs_subset
509
+ )
510
+
474
511
  # score of inlier data set
475
- score_subset = estimator.score(X_inlier_subset, y_inlier_subset, **kwargs)
512
+ score_subset = estimator.score(
513
+ X_inlier_subset,
514
+ y_inlier_subset,
515
+ **score_params_inlier_subset,
516
+ )
476
517
 
477
518
  # same number of inliers but worse score -> skip current random
478
519
  # sample
@@ -487,9 +528,6 @@ class RANSACRegressor(
487
528
  y_inlier_best = y_inlier_subset
488
529
  inlier_best_idxs_subset = inlier_idxs_subset
489
530
 
490
- ## ADLoc
491
- # estimator_best = clone(estimator)
492
-
493
531
  max_trials = min(
494
532
  max_trials,
495
533
  _dynamic_max_trials(n_inliers_best, n_samples, min_samples, self.stop_probability),
@@ -529,30 +567,17 @@ class RANSACRegressor(
529
567
  )
530
568
 
531
569
  # estimate final model using all inliers
532
- if sample_weight is None:
533
- estimator.fit(X_inlier_best, y_inlier_best, **kwargs)
534
- else:
535
- estimator.fit(
536
- X_inlier_best,
537
- y_inlier_best,
538
- sample_weight=sample_weight[inlier_best_idxs_subset],
539
- )
570
+ fit_params_best_idxs_subset = _check_method_params(
571
+ X, params=routed_params.estimator.fit, indices=inlier_best_idxs_subset
572
+ )
540
573
 
541
- # self.estimator_ = estimator
542
- # self.inlier_mask_ = inlier_mask_best
574
+ estimator.fit(X_inlier_best, y_inlier_best, **fit_params_best_idxs_subset)
543
575
 
544
- # ADLoc
545
- y_pred = estimator.predict(X, **kwargs)
546
- residuals = loss_function(y, y_pred)
547
- inlier_mask = residuals <= residual_threshold
548
- inlier_mask = inlier_mask.all(axis=-1) ## for time + amplitude
549
- # self.estimator_ = estimator_best
550
576
  self.estimator_ = estimator
551
- self.inlier_mask_ = inlier_mask
552
-
577
+ self.inlier_mask_ = inlier_mask_best
553
578
  return self
554
579
 
555
- def predict(self, X, **kwargs):
580
+ def predict(self, X, **params):
556
581
  """Predict using the estimated model.
557
582
 
558
583
  This is a wrapper for `estimator_.predict(X)`.
@@ -562,21 +587,41 @@ class RANSACRegressor(
562
587
  X : {array-like or sparse matrix} of shape (n_samples, n_features)
563
588
  Input data.
564
589
 
590
+ **params : dict
591
+ Parameters routed to the `predict` method of the sub-estimator via
592
+ the metadata routing API.
593
+
594
+ .. versionadded:: 1.5
595
+
596
+ Only available if
597
+ `sklearn.set_config(enable_metadata_routing=True)` is set. See
598
+ :ref:`Metadata Routing User Guide <metadata_routing>` for more
599
+ details.
600
+
565
601
  Returns
566
602
  -------
567
603
  y : array, shape = [n_samples] or [n_samples, n_targets]
568
604
  Returns predicted values.
569
605
  """
570
606
  check_is_fitted(self)
571
- X = self._validate_data(
607
+ X = validate_data(
608
+ self,
572
609
  X,
573
- force_all_finite=False,
610
+ ensure_all_finite=False,
574
611
  accept_sparse=True,
575
612
  reset=False,
576
613
  )
577
- return self.estimator_.predict(X, **kwargs)
578
614
 
579
- def score(self, X, y, **kwargs):
615
+ _raise_for_params(params, self, "predict")
616
+
617
+ if _routing_enabled():
618
+ predict_params = process_routing(self, "predict", **params).estimator["predict"]
619
+ else:
620
+ predict_params = {}
621
+
622
+ return self.estimator_.predict(X, **predict_params)
623
+
624
+ def score(self, X, y, **params):
580
625
  """Return the score of the prediction.
581
626
 
582
627
  This is a wrapper for `estimator_.score(X, y)`.
@@ -589,23 +634,67 @@ class RANSACRegressor(
589
634
  y : array-like of shape (n_samples,) or (n_samples, n_targets)
590
635
  Target values.
591
636
 
637
+ **params : dict
638
+ Parameters routed to the `score` method of the sub-estimator via
639
+ the metadata routing API.
640
+
641
+ .. versionadded:: 1.5
642
+
643
+ Only available if
644
+ `sklearn.set_config(enable_metadata_routing=True)` is set. See
645
+ :ref:`Metadata Routing User Guide <metadata_routing>` for more
646
+ details.
647
+
592
648
  Returns
593
649
  -------
594
650
  z : float
595
651
  Score of the prediction.
596
652
  """
597
653
  check_is_fitted(self)
598
- X = self._validate_data(
654
+ X = validate_data(
655
+ self,
599
656
  X,
600
- force_all_finite=False,
657
+ ensure_all_finite=False,
601
658
  accept_sparse=True,
602
659
  reset=False,
603
660
  )
604
- return self.estimator_.score(X, y, **kwargs)
605
-
606
- def _more_tags(self):
607
- return {
608
- "_xfail_checks": {
609
- "check_sample_weights_invariance": ("zero sample_weight is not equivalent to removing samples"),
610
- }
611
- }
661
+
662
+ _raise_for_params(params, self, "score")
663
+ if _routing_enabled():
664
+ score_params = process_routing(self, "score", **params).estimator["score"]
665
+ else:
666
+ score_params = {}
667
+
668
+ return self.estimator_.score(X, y, **score_params)
669
+
670
+ def get_metadata_routing(self):
671
+ """Get metadata routing of this object.
672
+
673
+ Please check :ref:`User Guide <metadata_routing>` on how the routing
674
+ mechanism works.
675
+
676
+ .. versionadded:: 1.5
677
+
678
+ Returns
679
+ -------
680
+ routing : MetadataRouter
681
+ A :class:`~sklearn.utils.metadata_routing.MetadataRouter` encapsulating
682
+ routing information.
683
+ """
684
+ router = MetadataRouter(owner=self.__class__.__name__).add(
685
+ estimator=self.estimator,
686
+ method_mapping=MethodMapping()
687
+ .add(caller="fit", callee="fit")
688
+ .add(caller="fit", callee="score")
689
+ .add(caller="score", callee="score")
690
+ .add(caller="predict", callee="predict"),
691
+ )
692
+ return router
693
+
694
+ def __sklearn_tags__(self):
695
+ tags = super().__sklearn_tags__()
696
+ if self.estimator is None:
697
+ tags.input_tags.sparse = True # default estimator is LinearRegression
698
+ else:
699
+ tags.input_tags.sparse = get_tags(self.estimator).input_tags.sparse
700
+ return tags
@@ -3,6 +3,50 @@ import numpy as np
3
3
  import torch
4
4
  import torch.nn as nn
5
5
  import torch.nn.functional as F
6
+ from torch.autograd import Function
7
+
8
+ from .eikonal2d import _interp
9
+ import scipy.sparse
10
+
11
+
12
+ class CalcTravelTime(Function):
13
+ @staticmethod
14
+ def forward(r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h):
15
+ tt = _interp(timetable, r.numpy(), z.numpy(), rgrid0, zgrid0, nr, nz, h)
16
+ tt = torch.from_numpy(tt)
17
+ return tt
18
+
19
+ @staticmethod
20
+ def setup_context(ctx, inputs, output):
21
+ r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h = inputs
22
+ ctx.save_for_backward(r, z)
23
+ ctx.timetable = timetable
24
+ ctx.timetable_grad_r = timetable_grad_r
25
+ ctx.timetable_grad_z = timetable_grad_z
26
+ ctx.rgrid0 = rgrid0
27
+ ctx.zgrid0 = zgrid0
28
+ ctx.nr = nr
29
+ ctx.nz = nz
30
+ ctx.h = h
31
+
32
+ @staticmethod
33
+ def backward(ctx, grad_output):
34
+ timetable_grad_r = ctx.timetable_grad_r
35
+ timetable_grad_z = ctx.timetable_grad_z
36
+ rgrid0 = ctx.rgrid0
37
+ zgrid0 = ctx.zgrid0
38
+ nr = ctx.nr
39
+ nz = ctx.nz
40
+ h = ctx.h
41
+ r, z = ctx.saved_tensors
42
+
43
+ grad_r = _interp(timetable_grad_r, r.numpy(), z.numpy(), rgrid0, zgrid0, nr, nz, h)
44
+ grad_z = _interp(timetable_grad_z, r.numpy(), z.numpy(), rgrid0, zgrid0, nr, nz, h)
45
+
46
+ grad_r = torch.from_numpy(grad_r) * grad_output
47
+ grad_z = torch.from_numpy(grad_z) * grad_output
48
+
49
+ return grad_r, grad_z, None, None, None, None, None, None, None, None
6
50
 
7
51
 
8
52
  # %%
@@ -64,6 +108,8 @@ class TravelTime(nn.Module):
64
108
  station_dt=None,
65
109
  event_loc=None,
66
110
  event_time=None,
111
+ event_loc0=None,
112
+ event_time0=None,
67
113
  velocity={"P": 6.0, "S": 6.0 / 1.73},
68
114
  eikonal=None,
69
115
  zlim=[0, 30],
@@ -72,11 +118,25 @@ class TravelTime(nn.Module):
72
118
  ):
73
119
  super().__init__()
74
120
  self.num_event = num_event
121
+ self.event_loc0 = nn.Embedding(num_event, 3)
122
+ self.event_time0 = nn.Embedding(num_event, 1)
75
123
  self.event_loc = nn.Embedding(num_event, 3)
76
124
  self.event_time = nn.Embedding(num_event, 1)
77
125
  self.station_loc = nn.Embedding(num_station, 3)
78
126
  self.station_dt = nn.Embedding(num_station, 1) # same statioin term for P and S
79
127
 
128
+ ## initialize event_loc0 and event_time0
129
+ if event_loc0 is not None:
130
+ event_loc0 = torch.tensor(event_loc0, dtype=dtype).contiguous()
131
+ else:
132
+ event_loc0 = torch.zeros(num_event, 3, dtype=dtype).contiguous()
133
+ if event_time0 is not None:
134
+ event_time0 = torch.tensor(event_time0, dtype=dtype).contiguous()
135
+ else:
136
+ event_time0 = torch.zeros(num_event, 1, dtype=dtype).contiguous()
137
+ self.event_loc0.weight = torch.nn.Parameter(event_loc0, requires_grad=False)
138
+ self.event_time0.weight = torch.nn.Parameter(event_time0, requires_grad=False)
139
+
80
140
  ## check initialization
81
141
  station_loc = torch.tensor(station_loc, dtype=dtype)
82
142
  if station_dt is not None:
@@ -97,7 +157,8 @@ class TravelTime(nn.Module):
97
157
  self.event_loc.weight = torch.nn.Parameter(event_loc, requires_grad=True)
98
158
  self.event_time.weight = torch.nn.Parameter(event_time, requires_grad=True)
99
159
 
100
- self.velocity = [velocity["P"], velocity["S"]]
160
+ # self.velocity = [velocity["P"], velocity["S"]]
161
+ self.velocity = velocity
101
162
  self.eikonal = eikonal
102
163
  self.zlim = zlim
103
164
  self.grad_type = grad_type
@@ -125,25 +186,25 @@ class TravelTime(nn.Module):
125
186
  z = event_loc[:, 2] - station_loc[:, 2]
126
187
  r = torch.sqrt(x**2 + y**2)
127
188
 
128
- # timetable = self.eikonal["up"] if phase_type == 0 else self.eikonal["us"]
129
- # timetable_grad = self.eikonal["grad_up"] if phase_type == 0 else self.eikonal["grad_us"]
130
- # timetable_grad_r = timetable_grad[0]
131
- # timetable_grad_z = timetable_grad[1]
132
- # rgrid0 = self.eikonal["rgrid"][0]
133
- # zgrid0 = self.eikonal["zgrid"][0]
134
- # nr = self.eikonal["nr"]
135
- # nz = self.eikonal["nz"]
136
- # h = self.eikonal["h"]
137
- # tt = CalcTravelTime.apply(r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h)
138
-
139
- if phase_type in [0, "P"]:
140
- timetable = self.timetable_p
141
- elif phase_type in [1, "S"]:
142
- timetable = self.timetable_s
143
- else:
144
- raise ValueError("phase_type should be 0 or 1. for P and S, respectively.")
145
-
146
- tt = interp2d(timetable, r, z, self.rgrid, self.zgrid, self.h)
189
+ timetable = self.eikonal["up"] if phase_type in [0, "P"] else self.eikonal["us"]
190
+ timetable_grad = self.eikonal["grad_up"] if phase_type in [0, "P"] else self.eikonal["grad_us"]
191
+ timetable_grad_r = timetable_grad[0]
192
+ timetable_grad_z = timetable_grad[1]
193
+ rgrid0 = self.eikonal["rgrid"][0]
194
+ zgrid0 = self.eikonal["zgrid"][0]
195
+ nr = self.eikonal["nr"]
196
+ nz = self.eikonal["nz"]
197
+ h = self.eikonal["h"]
198
+ tt = CalcTravelTime.apply(r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h)
199
+
200
+ # if phase_type in [0, "P"]:
201
+ # timetable = self.timetable_p
202
+ # elif phase_type in [1, "S"]:
203
+ # timetable = self.timetable_s
204
+ # else:
205
+ # raise ValueError("phase_type should be 0 or 1. for P and S, respectively.")
206
+
207
+ # tt = interp2d(timetable, r, z, self.rgrid, self.zgrid, self.h)
147
208
 
148
209
  tt = tt.float().unsqueeze(-1)
149
210
 
@@ -188,8 +249,10 @@ class TravelTime(nn.Module):
188
249
  phase_time_ = phase_time[phase_type == type]
189
250
  resisudal[phase_type == type] = phase_time_ - t_
190
251
  loss += torch.sum(F.huber_loss(t_, phase_time_, reduction="none") * phase_weight_)
252
+ # loss += torch.sum(F.l1_loss(t_, phase_time_, reduction="none") * phase_weight_)
253
+ # loss += torch.sum(torch.abs(t_ - phase_time_) * phase_weight_)
191
254
 
192
- return {"phase_time": pred_time, "residual_s": resisudal, "loss": loss}
255
+ return {"phase_time": pred_time, "residual": resisudal, "loss": loss}
193
256
 
194
257
 
195
258
  # %%
@@ -203,6 +266,8 @@ class TravelTimeDD(TravelTime):
203
266
  station_dt=None,
204
267
  event_loc=None,
205
268
  event_time=None,
269
+ event_loc0=None,
270
+ event_time0=None,
206
271
  velocity={"P": 6.0, "S": 6.0 / 1.73},
207
272
  eikonal=None,
208
273
  zlim=[0, 30],
@@ -216,6 +281,8 @@ class TravelTimeDD(TravelTime):
216
281
  station_dt=station_dt,
217
282
  event_loc=event_loc,
218
283
  event_time=event_time,
284
+ event_loc0=event_loc0,
285
+ event_time0=event_time0,
219
286
  velocity=velocity,
220
287
  eikonal=eikonal,
221
288
  zlim=zlim,
@@ -225,8 +292,10 @@ class TravelTimeDD(TravelTime):
225
292
 
226
293
  self.event_loc.weight.requires_grad = True
227
294
  self.event_time.weight.requires_grad = False
295
+ # self.event_time.weight.requires_grad = True
228
296
 
229
297
  def calc_time(self, event_loc, station_loc, phase_type):
298
+
230
299
  if self.eikonal is None:
231
300
  dist = torch.linalg.norm(event_loc - station_loc, axis=-1, keepdim=True)
232
301
  tt = dist / self.velocity[phase_type]
@@ -246,25 +315,25 @@ class TravelTimeDD(TravelTime):
246
315
  z = event_loc[:, 2] - station_loc[:, 2]
247
316
  r = torch.sqrt(x**2 + y**2)
248
317
 
249
- # timetable = self.eikonal["up"] if phase_type == 0 else self.eikonal["us"]
250
- # timetable_grad = self.eikonal["grad_up"] if phase_type == 0 else self.eikonal["grad_us"]
251
- # timetable_grad_r = timetable_grad[0]
252
- # timetable_grad_z = timetable_grad[1]
253
- # rgrid0 = self.eikonal["rgrid"][0]
254
- # zgrid0 = self.eikonal["zgrid"][0]
255
- # nr = self.eikonal["nr"]
256
- # nz = self.eikonal["nz"]
257
- # h = self.eikonal["h"]
258
- # tt = CalcTravelTime.apply(r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h)
259
-
260
- if phase_type in [0, "P"]:
261
- timetable = self.timetable_p
262
- elif phase_type in [1, "S"]:
263
- timetable = self.timetable_s
264
- else:
265
- raise ValueError("phase_type should be 0 or 1. for P and S, respectively.")
266
-
267
- tt = interp2d(timetable, r, z, self.rgrid, self.zgrid, self.h)
318
+ timetable = self.eikonal["up"] if phase_type in [0, "P"] else self.eikonal["us"]
319
+ timetable_grad = self.eikonal["grad_up"] if phase_type in [0, "P"] else self.eikonal["grad_us"]
320
+ timetable_grad_r = timetable_grad[0]
321
+ timetable_grad_z = timetable_grad[1]
322
+ rgrid0 = self.eikonal["rgrid"][0]
323
+ zgrid0 = self.eikonal["zgrid"][0]
324
+ nr = self.eikonal["nr"]
325
+ nz = self.eikonal["nz"]
326
+ h = self.eikonal["h"]
327
+ tt = CalcTravelTime.apply(r, z, timetable, timetable_grad_r, timetable_grad_z, rgrid0, zgrid0, nr, nz, h)
328
+
329
+ # if phase_type in [0, "P"]:
330
+ # timetable = self.timetable_p
331
+ # elif phase_type in [1, "S"]:
332
+ # timetable = self.timetable_s
333
+ # else:
334
+ # raise ValueError("phase_type should be 0 or 1. for P and S, respectively.")
335
+
336
+ # tt = interp2d(timetable, r, z, self.rgrid, self.zgrid, self.h)
268
337
 
269
338
  tt = tt.float().view(nb1, ne1, 1)
270
339
 
@@ -294,12 +363,18 @@ class TravelTimeDD(TravelTime):
294
363
  station_loc_ = self.station_loc(station_index_) # (nb, 3)
295
364
  station_dt_ = self.station_dt(station_index_) # (nb, 1)
296
365
 
297
- event_loc_ = self.event_loc(event_index_) # (nb, 2, 3)
298
- event_time_ = self.event_time(event_index_) # (nb, 2, 1)
366
+ delta_event_loc_ = self.event_loc(event_index_) # (nb, 2, 3)
367
+ delta_event_time_ = self.event_time(event_index_) # (nb, 2, 1)
368
+
369
+ event_loc0_ = self.event_loc0(event_index_) # (nb, 2, 3)
370
+ event_time0_ = self.event_time0(event_index_) # (nb, 2, 1)
299
371
 
300
372
  station_loc_ = station_loc_.unsqueeze(1) # (nb, 1, 3)
301
373
  station_dt_ = station_dt_.unsqueeze(1) # (nb, 1, 1)
302
374
 
375
+ event_loc_ = event_loc0_ + delta_event_loc_
376
+ event_time_ = event_time0_ + delta_event_time_
377
+
303
378
  tt_ = self.calc_time(event_loc_, station_loc_, type) # (nb, 2)
304
379
 
305
380
  t_ = event_time_ + tt_ + station_dt_ # (nb, 2, 1)
@@ -312,10 +387,121 @@ class TravelTimeDD(TravelTime):
312
387
  if phase_time is not None:
313
388
  phase_time_ = phase_time[phase_type == type]
314
389
  loss += torch.sum(F.huber_loss(t_, phase_time_, reduction="none") * phase_weight_)
390
+ # loss += torch.sum(F.l1_loss(t_, phase_time_, reduction="none") * phase_weight_)
391
+ # loss += torch.sum(torch.abs(t_ - phase_time_) * phase_weight_)
315
392
 
393
+ if loss == 0.0:
394
+ return None
316
395
  return {"phase_time": pred_time, "loss": loss}
317
396
 
318
397
 
398
+ def hypodd(pairs, event_loc, event_time, station_loc, config):
399
+
400
+ num_event = len(event_loc)
401
+ num_pair = len(pairs)
402
+ pair_index = np.arange(num_pair)
403
+ idx = np.ones(num_pair, dtype=bool)
404
+ event_index = pairs[["idx_eve1", "idx_eve2"]].values
405
+ station_index = pairs["idx_sta"].values
406
+ phase_type = pairs["phase_type"].values
407
+ phase_time = pairs["phase_dtime"].values
408
+ phase_weight = pairs["phase_score"].values
409
+
410
+ rgrid0 = config["eikonal"]["rgrid"][0]
411
+ zgrid0 = config["eikonal"]["zgrid"][0]
412
+ nr = config["eikonal"]["nr"]
413
+ nz = config["eikonal"]["nz"]
414
+ h = config["eikonal"]["h"]
415
+
416
+ max_std = 10.0
417
+ min_std = 3.0
418
+ max_iter = 10
419
+ for iter in range(max_iter):
420
+
421
+ std = max_std - (max_std - min_std) * iter / max_iter
422
+
423
+ rows = []
424
+ cols = []
425
+ values = []
426
+ d = np.zeros(num_pair)
427
+
428
+ station_loc_ = station_loc[station_index]
429
+ event_loc_ = event_loc[event_index]
430
+ event_time_ = event_time[event_index]
431
+
432
+ station_loc_ = station_loc_[:, np.newaxis, :]
433
+ x = event_loc_[:, :, 0] - station_loc_[:, :, 0]
434
+ y = event_loc_[:, :, 1] - station_loc_[:, :, 1]
435
+ z = event_loc_[:, :, 2] - station_loc_[:, :, 2]
436
+ r = np.sqrt(x**2 + y**2)
437
+
438
+ tt = np.zeros([len(phase_time), 2])
439
+ grad_r = np.zeros([len(phase_time), 2])
440
+ grad_z = np.zeros([len(phase_time), 2])
441
+
442
+ for ph in np.unique(phase_type):
443
+ timetable = config["eikonal"]["up"] if ph in [0, "P"] else config["eikonal"]["us"]
444
+ timetable_grad = config["eikonal"]["grad_up"] if ph in [0, "P"] else config["eikonal"]["grad_us"]
445
+ timetable_grad_r = timetable_grad[0]
446
+ timetable_grad_z = timetable_grad[1]
447
+
448
+ ii = phase_type == ph
449
+ tt[ii] = _interp(timetable, r[ii], z[ii], rgrid0, zgrid0, nr, nz, h)
450
+ grad_r[ii] = _interp(timetable_grad_r, r[ii], z[ii], rgrid0, zgrid0, nr, nz, h)
451
+ grad_z[ii] = _interp(timetable_grad_z, r[ii], z[ii], rgrid0, zgrid0, nr, nz, h)
452
+
453
+ grad_x = grad_r * x / (r + 1e-6)
454
+ grad_y = grad_r * y / (r + 1e-6)
455
+ dt = (tt[:, 0] + event_time_[:, 0] - tt[:, 1] - event_time_[:, 1]) - phase_time
456
+
457
+ idx = np.abs(dt) < np.std(dt[idx]) * std
458
+ dt = dt[idx]
459
+ grad_x = grad_x[idx]
460
+ grad_y = grad_y[idx]
461
+ grad_z = grad_z[idx]
462
+ num_pair_ = len(dt)
463
+ pair_index_ = np.arange(num_pair_)
464
+ event_index_ = event_index[idx]
465
+ w = phase_weight[idx]
466
+
467
+ rows = np.tile(pair_index_, 8)
468
+ cols = np.transpose(event_index_)[:, np.newaxis, :] * 4 + np.arange(4)[np.newaxis, :, np.newaxis]
469
+ values = np.concatenate(
470
+ [
471
+ grad_x[:, 0] * w,
472
+ grad_y[:, 0] * w,
473
+ grad_z[:, 0] * w,
474
+ np.ones_like(w) * w,
475
+ -grad_x[:, 1] * w,
476
+ -grad_y[:, 1] * w,
477
+ -grad_z[:, 1] * w,
478
+ -np.ones_like(w) * w,
479
+ ]
480
+ )
481
+
482
+ print(f"Number of pairs: {num_pair_} ({num_pair_/num_pair*100:.2f}%)")
483
+
484
+ rows = rows.reshape(-1)
485
+ cols = cols.reshape(-1)
486
+ values = values.reshape(-1)
487
+
488
+ G = scipy.sparse.coo_matrix((values, (rows, cols)), shape=(num_pair_, num_event * 4))
489
+ d = dt * w
490
+
491
+ x, istop, itn, r1norm, r2norm, anorm, acond, arnorm, xnorm, _ = scipy.sparse.linalg.lsqr(
492
+ G, d, damp=0.0, conlim=100
493
+ )
494
+ x = x.reshape(num_event, 4)
495
+ event_loc = event_loc - x[:, :3]
496
+ event_time = event_time - x[:, 3]
497
+
498
+ print(
499
+ f"LSQR: {istop = }, {itn = }, {r1norm = :.3f}, {r2norm = :.3f}, {anorm = :.3f}, {acond = :.3f}, {arnorm = :.3f}, {xnorm = :.3f}"
500
+ )
501
+
502
+ return event_loc, event_time
503
+
504
+
319
505
  # %%
320
506
  if __name__ == "__main__":
321
507
 
@@ -522,8 +708,8 @@ if __name__ == "__main__":
522
708
  num_station,
523
709
  stations[["x_km", "y_km", "z_km"]].values,
524
710
  stations[["dt_s"]].values,
525
- event_loc,
526
- event_time,
711
+ event_loc0=event_loc,
712
+ event_time0=event_time,
527
713
  eikonal=eikonal_config,
528
714
  )
529
715
 
@@ -195,14 +195,15 @@ class PhaseDataset(Dataset):
195
195
  return self.meta[i]
196
196
 
197
197
 
198
- class PhaseDatasetDD(Dataset):
198
+ class PhaseDatasetDT(Dataset):
199
199
  def __init__(
200
200
  self,
201
201
  pairs,
202
202
  picks,
203
203
  events,
204
204
  stations,
205
- batch_size=1000,
205
+ batch_size=10000,
206
+ valid_index=None,
206
207
  config=None,
207
208
  rank=0,
208
209
  world_size=1,
@@ -213,6 +214,9 @@ class PhaseDatasetDD(Dataset):
213
214
  self.stations = stations
214
215
  self.config = config
215
216
  self.batch_size = batch_size
217
+ if valid_index is None:
218
+ valid_index = np.ones(len(self.pairs), dtype=bool)
219
+ self.valid_index = valid_index
216
220
 
217
221
  self.idx_batch = np.array_split(np.arange(len(self.pairs)), (len(self.pairs) - 1) // self.batch_size + 1)[
218
222
  rank::world_size
@@ -226,14 +230,40 @@ class PhaseDatasetDD(Dataset):
226
230
 
227
231
  def __getitem__(self, i):
228
232
 
229
- idx = self.idx_batch[i]
230
- idx1_eve = self.pairs["event_index1"][idx]
231
- idx2_eve = self.pairs["event_index2"][idx]
233
+ idx = self.idx_batch[i][self.valid_index[self.idx_batch[i]]]
234
+
235
+ if len(idx) == 0:
236
+ return None # skip empty batch
237
+
238
+ idx1_eve = self.pairs["idx_eve1"][idx].values
239
+ idx2_eve = self.pairs["idx_eve2"][idx].values
240
+ idx_eve = np.stack([idx1_eve, idx2_eve], axis=1)
241
+ idx_sta = self.pairs["idx_sta"][idx].values
242
+ phase_weight = self.pairs["phase_score"][idx].values
243
+ phase_type = self.pairs["phase_type"][idx].values
244
+ phase_time = self.pairs["phase_dtime"][idx].values
245
+
246
+ return {
247
+ "idx_eve": torch.tensor(idx_eve, dtype=torch.long),
248
+ "idx_sta": torch.tensor(idx_sta, dtype=torch.long),
249
+ "phase_type": phase_type,
250
+ "phase_weight": torch.tensor(phase_weight, dtype=torch.float32),
251
+ "phase_time": torch.tensor(phase_time, dtype=torch.float32),
252
+ }
253
+
254
+
255
+ class PhaseDatasetDTCC(PhaseDatasetDT):
256
+
257
+ def __getitem__(self, i):
258
+
259
+ idx = self.idx_batch[i][self.valid_index[self.idx_batch[i]]]
260
+ idx1_eve = self.pairs["idx_eve1"][idx].values
261
+ idx2_eve = self.pairs["idx_eve2"][idx].values
232
262
  idx_eve = np.stack([idx1_eve, idx2_eve], axis=1)
233
- idx_sta = self.pairs["station_index"][idx]
234
- phase_weight = self.pairs["phase_score"][idx]
235
- phase_type = self.pairs["phase_type"][idx]
236
- phase_time = self.pairs["dd_time"][idx]
263
+ idx_sta = self.pairs["idx_sta"][idx].values
264
+ phase_weight = self.pairs["weight"][idx].values
265
+ phase_type = self.pairs["phase_type"][idx].values
266
+ phase_time = self.pairs["dt"][idx].values
237
267
 
238
268
  return {
239
269
  "idx_eve": torch.tensor(idx_eve, dtype=torch.long),
@@ -112,6 +112,10 @@ def test_get_index():
112
112
 
113
113
  @njit
114
114
  def _interp(time_table, r, z, rgrid0, zgrid0, nr, nz, h):
115
+ shape = r.shape
116
+ r = r.reshape(-1)
117
+ z = z.reshape(-1)
118
+
115
119
  ir0 = np.floor((r - rgrid0) / h).clip(0, nr - 2).astype(np.int64)
116
120
  iz0 = np.floor((z - zgrid0) / h).clip(0, nz - 2).astype(np.int64)
117
121
  ir1 = ir0 + 1
@@ -140,7 +144,7 @@ def _interp(time_table, r, z, rgrid0, zgrid0, nr, nz, h):
140
144
  )
141
145
  )
142
146
 
143
- return t
147
+ return t.reshape(shape)
144
148
 
145
149
 
146
150
  # def traveltime(event_loc, station_loc, phase_type, eikonal):
@@ -48,7 +48,7 @@ class ADLoc(BaseEstimator):
48
48
  self.use_amplitude = False
49
49
 
50
50
  @staticmethod
51
- def l2_loss_grad(event, X, y, vel={0: 6.0, 1: 6.0 / 1.75}, stations=None, eikonal=None):
51
+ def l2_loss_grad(event, X, y, vel={0: 6.0, 1: 6.0 / 1.75}, stations=None, eikonal=None, reduce_loss=False):
52
52
  """
53
53
  X: data_frame with columns ["timestamp", "x_km", "y_km", "z_km", "type"]
54
54
  """
@@ -61,7 +61,11 @@ class ADLoc(BaseEstimator):
61
61
  tt = np.linalg.norm(event[:3] - stations[station_index, :3], axis=-1) / v + event[3]
62
62
  else:
63
63
  tt = traveltime(0, station_index, phase_type, event[np.newaxis, :3], stations, eikonal) + event[3]
64
- loss = 0.5 * np.sum((tt - y) ** 2 * phase_weight)
64
+
65
+ if reduce_loss:
66
+ loss = 0.5 * np.sum((tt - y) ** 2 * phase_weight)
67
+ else:
68
+ loss = (tt - y) * phase_weight ## For LSQR
65
69
 
66
70
  J = np.ones((len(X), 4))
67
71
  if eikonal is None:
@@ -74,7 +78,11 @@ class ADLoc(BaseEstimator):
74
78
  grad = grad_traveltime(0, station_index, phase_type, event[np.newaxis, :3], stations, eikonal)
75
79
  J[:, :3] = grad
76
80
 
77
- J = np.sum((tt - y)[:, np.newaxis] * J * phase_weight[:, np.newaxis], axis=0)
81
+ if reduce_loss:
82
+ J = np.sum((tt - y)[:, np.newaxis] * J * phase_weight[:, np.newaxis], axis=0)
83
+ else:
84
+ J = J * phase_weight[:, np.newaxis] ## For LSQR
85
+
78
86
  return loss, J
79
87
 
80
88
  @staticmethod
@@ -124,8 +132,8 @@ class ADLoc(BaseEstimator):
124
132
  yt = y[:, 0]
125
133
 
126
134
  opt = scipy.optimize.minimize(
127
- # self.huber_loss_grad,
128
- self.l2_loss_grad,
135
+ self.huber_loss_grad,
136
+ # self.l2_loss_grad,
129
137
  x0=self.events[event_index],
130
138
  method="L-BFGS-B",
131
139
  jac=True,
@@ -151,6 +159,35 @@ class ADLoc(BaseEstimator):
151
159
 
152
160
  return self
153
161
 
162
+ # def fit(self, X, y=None, event_index=0, damp=1.0, atol=1e-3, max_iter=100): ## For LSQR
163
+
164
+ # if self.use_amplitude:
165
+ # yt = y[:, 0] # time
166
+ # ya = y[:, 1] # amplitude
167
+ # else:
168
+ # yt = y[:, 0]
169
+
170
+ # event0 = self.events[event_index]
171
+ # delta = np.inf
172
+ # for i in range(max_iter):
173
+ # loss, grad = self.l2_loss_grad(event0, X, yt, self.vel, self.stations, self.eikonal)
174
+ # A, b = grad, loss
175
+ # dx = scipy.sparse.linalg.lsqr(A, b, damp=damp)[0]
176
+ # self.events[event_index] -= dx
177
+ # delta = np.linalg.norm(dx)
178
+ # if delta < atol:
179
+ # self.is_fitted_ = True
180
+ # break
181
+
182
+ # if self.use_amplitude:
183
+ # station_index = X[:, 0].astype(int)
184
+ # phase_weight = X[:, 2:3]
185
+ # self.magnitudes[event_index] = calc_mag(
186
+ # ya[:, np.newaxis], self.events[event_index, :3], self.stations[station_index, :3], phase_weight
187
+ # )
188
+
189
+ # return self
190
+
154
191
  def predict(self, X, event_index=0):
155
192
  """
156
193
  X: data_frame with columns ["timestamp", "x_km", "y_km", "z_km", "type"]
@@ -178,10 +215,25 @@ class ADLoc(BaseEstimator):
178
215
  else:
179
216
  return np.array([tt]).T
180
217
 
181
- def score(self, X, y=None, event_index=0):
218
+ def weight(self, X, event_index=0):
182
219
  """
183
220
  X: data_frame with columns ["timestamp", "x_km", "y_km", "z_km", "type"]
184
221
  """
222
+ station_index = X[:, 0].astype(int)
223
+ phase_type = X[:, 1].astype(int)
224
+
225
+ if self.eikonal is None:
226
+ v = np.array([self.vel[t] for t in phase_type])
227
+ tt = np.linalg.norm(self.events[event_index, :3] - self.stations[station_index, :3], axis=-1) / v
228
+ else:
229
+ tt = traveltime(event_index, station_index, phase_type, self.events, self.stations, self.eikonal)
230
+
231
+ return np.array([tt]).T
232
+
233
+ def score(self, X, y=None, event_index=0):
234
+ """
235
+ X: idx_sta, type, score, amp
236
+ """
185
237
  if len(y) <= 1:
186
238
  return -np.inf
187
239
 
@@ -193,13 +245,20 @@ class ADLoc(BaseEstimator):
193
245
  ya = y[:, 1]
194
246
  R2_t = 1 - np.sum((yt - tt) ** 2) / (np.sum((y[:, 0] - np.mean(y[:, 0])) ** 2) + 1e-6)
195
247
  R2_a = 1 - np.sum((ya - amp) ** 2) / (np.sum((y[:, 1] - np.mean(y[:, 1])) ** 2) + 1e-6)
248
+ # weight = X[:, 2]
249
+ # mu_yt = np.average(yt, weights=weight)
250
+ # mu_ya = np.average(ya, weights=weight)
251
+ # R2_t = 1 - np.sum(weight * (yt - tt) ** 2) / (np.sum(weight * (yt - mu_yt) ** 2) + 1e-6)
252
+ # R2_a = 1 - np.sum(weight * (ya - amp) ** 2) / (np.sum(weight * (ya - mu_ya) ** 2) + 1e-6)
196
253
  R2 = (R2_t + R2_a) / 2
197
254
  else:
198
255
  tt = output
199
256
  yt = y
200
257
  R2 = 1 - np.sum((yt - tt) ** 2) / (np.sum((y - np.mean(y)) ** 2) + 1e-6)
258
+ # weight = X[:, 2]
259
+ # mu_y = np.average(yt, weights=weight)
260
+ # R2 = 1 - np.sum(weight * (yt - tt) ** 2) / (np.sum(weight * (yt - mu_y) ** 2) + 1e-6)
201
261
 
202
- # print(f"{R2=}")
203
262
  return R2
204
263
 
205
264
 
@@ -501,7 +560,11 @@ if __name__ == "__main__":
501
560
  X["type"] = X["type"].apply(lambda x: mapping_int[x.upper()])
502
561
 
503
562
  estimator = ADLoc(
504
- config, stations=stations[["x_km", "y_km", "z_km"]].values, num_event=num_event, eikonal=eikonal_config
563
+ config,
564
+ # events=events[["x_km", "y_km", "z_km", "event_time"]].values + np.random.rand(num_event, 4) * 10,
565
+ stations=stations[["x_km", "y_km", "z_km"]].values,
566
+ num_event=num_event,
567
+ eikonal=eikonal_config,
505
568
  )
506
569
  output = estimator.predict(X[["idx_sta", "type"]].values, event_index=event_index)
507
570
  tt_init = output[:, 0]
@@ -14,19 +14,33 @@ def invert(picks, stations, config, estimator, event_index, event_init):
14
14
  """
15
15
 
16
16
  def is_model_valid(estimator, X, y):
17
- ## TODO: Implement a good way to check if the model is valid
18
- # score = estimator.score(X, y)
19
- # return score > config["min_score"]
20
- # events [[x, y, z, t]]
21
- return estimator.events[0][2] > 1.0 # depth > 1.0 km
17
+ """
18
+ X: idx_sta, type, score, amp
19
+ """
20
+ # n0 = np.sum(X[:, 1] == 0) # P
21
+ # n1 = np.sum(X[:, 1] == 1) # S
22
+ n0 = np.sum(X[X[:, 1] == 0, 2])
23
+ n1 = np.sum(X[X[:, 1] == 1, 2])
24
+ if not (n0 >= config["min_p_picks"] and n1 >= config["min_s_picks"]):
25
+ return False
26
+
27
+ if estimator.events[0][2] < 1.0: # depth > 1.0 km
28
+ return False
29
+
30
+ if estimator.score(X, y) < config["min_score"]:
31
+ return False
32
+
33
+ return True
22
34
 
23
35
  def is_data_valid(X, y):
24
36
  """
25
- X: idx_sta, type, score
37
+ X: idx_sta, type, score, amp
26
38
  y: t_s
27
39
  """
28
- n0 = np.sum(X[:, 1] == 0) # P
29
- n1 = np.sum(X[:, 1] == 1) # S
40
+ # n0 = np.sum(X[:, 1] == 0) # P
41
+ # n1 = np.sum(X[:, 1] == 1) # S
42
+ n0 = np.sum(X[X[:, 1] == 0, 2])
43
+ n1 = np.sum(X[X[:, 1] == 1, 2])
30
44
  return n0 >= config["min_p_picks"] and n1 >= config["min_s_picks"] # At least min P and S picks
31
45
 
32
46
  MIN_PICKS = config["min_picks"]
@@ -36,28 +50,43 @@ def invert(picks, stations, config, estimator, event_index, event_init):
36
50
  else:
37
51
  MAX_RESIDUAL = config["max_residual_time"]
38
52
 
39
- if "station_term_time" not in stations.columns:
40
- stations["station_term_time"] = 0.0
53
+ if "station_term_time_p" not in stations.columns:
54
+ stations["station_term_time_p"] = 0.0
55
+ if "station_term_time_s" not in stations.columns:
56
+ stations["station_term_time_s"] = 0.0
41
57
  if config["use_amplitude"] and ("station_term_amplitude" not in stations.columns):
42
58
  stations["station_term_amplitude"] = 0.0
43
59
 
44
60
  if config["use_amplitude"]:
45
61
  X = picks.merge(
46
- stations[["x_km", "y_km", "z_km", "station_id", "station_term_time", "station_term_amplitude"]],
47
- # stations[["x_km", "y_km", "z_km", "station_id", "station_term_p", "station_term_s"]], ## Separate P and S station term
62
+ # stations[["x_km", "y_km", "z_km", "station_id", "station_term_time", "station_term_amplitude"]],
63
+ stations[
64
+ [
65
+ "x_km",
66
+ "y_km",
67
+ "z_km",
68
+ "station_id",
69
+ "station_term_time_p",
70
+ "station_term_time_s",
71
+ "station_term_amplitude",
72
+ ]
73
+ ], ## Separate P and S station term
48
74
  on="station_id",
49
75
  )
50
76
  else:
51
77
  X = picks.merge(
52
- stations[["x_km", "y_km", "z_km", "station_id", "station_term_time"]],
78
+ # stations[["x_km", "y_km", "z_km", "station_id", "station_term_time"]],
79
+ stations[
80
+ ["x_km", "y_km", "z_km", "station_id", "station_term_time_p", "station_term_time_s"]
81
+ ], ## Separate P and S station term
53
82
  on="station_id",
54
83
  )
55
84
  t0 = X["phase_time"].min()
56
85
 
57
- if event_init is not None:
58
- event_init = np.array([[event_init[0], event_init[1], event_init[2], (event_init[3] - t0).total_seconds()]])
59
- else:
86
+ if (event_init is None) or len(X) == 0:
60
87
  event_init = np.array([[np.median(X["x_km"]), np.median(X["y_km"]), 5.0, 0.0]])
88
+ else:
89
+ event_init = np.array([[event_init[0], event_init[1], event_init[2], (event_init[3] - t0).total_seconds()]])
61
90
 
62
91
  # xstd = np.std(X["x_km"])
63
92
  # ystd = np.std(X["y_km"])
@@ -65,21 +94,22 @@ def invert(picks, stations, config, estimator, event_index, event_init):
65
94
 
66
95
  X.rename(columns={"phase_type": "type", "phase_score": "score", "phase_time": "t_s"}, inplace=True)
67
96
  X["t_s"] = (X["t_s"] - t0).dt.total_seconds()
97
+ X["station_term_time"] = X.apply(
98
+ lambda x: x["station_term_time_p"] if x["type"] == 0 else x["station_term_time_s"], axis=1
99
+ ) ## Separate P and S station term
68
100
  X["t_s"] = X["t_s"] - X["station_term_time"]
69
101
  if config["use_amplitude"]:
70
102
  X.rename(columns={"phase_amplitude": "amp"}, inplace=True)
71
103
  X["amp"] = X["amp"] - X["station_term_amplitude"]
72
- # X["t_s"] = X.apply(
73
- # lambda x: x["t_s"] - x["station_term_p"] if x["type"] == 0 else x["t_s"] - x["station_term_s"], axis=1
74
- # ) ## Separate P and S station term
75
- # X = X[["idx_sta", "type", "score", "t_s", "amp"]]
76
- # X["type"] = X["type"].apply(lambda x: mapping_phase_type_int[x])
77
104
 
78
105
  estimator.set_params(**{"events": event_init})
79
106
 
80
107
  # ## Location using ADLoc
81
- # estimator.fit(X[["idx_sta", "type", "score"]].values, y=X["t_s"].values)
82
- # inlier_mask = np.ones(len(picks_by_event)).astype(bool)
108
+ # if config["use_amplitude"]:
109
+ # estimator.fit(X[["idx_sta", "type", "score", "amp"]].values, y=X[["t_s", "amp"]].values)
110
+ # else:
111
+ # estimator.fit(X[["idx_sta", "type", "score"]].values, y=X[["t_s"]].values)
112
+ # inlier_mask = np.ones(len(X)).astype(bool)
83
113
 
84
114
  ## Location using RANSAC
85
115
  num_picks = len(X)
@@ -98,10 +128,11 @@ def invert(picks, stations, config, estimator, event_index, event_init):
98
128
  else:
99
129
  reg.fit(X[["idx_sta", "type", "score"]].values, X[["t_s"]].values)
100
130
  except Exception as e:
101
- print(f"No valid model for event_index {event_index}.")
131
+ # print(f"No valid model for event_index {event_index}.")
102
132
  message = "RANSAC could not find a valid consensus set."
103
133
  if str(e)[: len(message)] != message:
104
134
  print(e)
135
+ raise
105
136
  picks["mask"] = 0
106
137
  picks["residual_time"] = 0.0
107
138
  if config["use_amplitude"]:
@@ -132,6 +163,8 @@ def invert(picks, stations, config, estimator, event_index, event_init):
132
163
  else:
133
164
  mean_residual_amp = 0.0
134
165
  x, y, z, t = estimator.events[0]
166
+ if config["use_amplitude"]:
167
+ mag = estimator.magnitudes[0]
135
168
 
136
169
  event = {
137
170
  "idx_eve": event_index, ## inside adloc, idx_eve is used which starts from 0 to N events
@@ -144,6 +177,7 @@ def invert(picks, stations, config, estimator, event_index, event_init):
144
177
  "num_picks": np.sum(inlier_mask),
145
178
  }
146
179
  if config["use_amplitude"]:
180
+ event["magnitude"] = mag
147
181
  event["adloc_residual_amplitude"] = mean_residual_amp
148
182
  event = pd.DataFrame([event])
149
183
  else:
@@ -193,19 +227,22 @@ def invert_location(picks, stations, config, estimator, events_init=None, iter=0
193
227
  if "ncpu" in config:
194
228
  NCPU = config["ncpu"]
195
229
  else:
196
- NCPU = min(32, mp.cpu_count() - 1)
230
+ NCPU = min(64, mp.cpu_count() * 2 - 1)
197
231
 
198
232
  jobs = []
233
+ num_events = len(picks["idx_eve"].unique())
199
234
  events_inverted = []
200
235
  picks_inverted = []
201
- pbar = tqdm(total=len(picks.groupby("idx_eve")), desc=f"Iter {iter}")
236
+ pbar = tqdm(total=num_events, desc=f"Iter {iter}")
202
237
  with mp.get_context("spawn").Pool(NCPU) as pool:
203
238
  for idx_eve, picks_by_event in picks.groupby("idx_eve"):
204
239
  if events_init is not None:
205
240
  event_init = events_init[events_init["idx_eve"] == idx_eve]
206
- if len(event_init) == 1:
241
+ if len(event_init) == 0:
242
+ event_init = None
243
+ elif len(event_init) == 1:
207
244
  event_init = event_init[["x_km", "y_km", "z_km", "time"]].values[0]
208
- elif len(event_init) > 1:
245
+ else:
209
246
  event_init = event_init[["x_km", "y_km", "z_km", "time"]].values[0]
210
247
  print(f"Multiple initial locations for event_index {idx_eve}.")
211
248
  else:
@@ -241,7 +278,8 @@ def invert_location(picks, stations, config, estimator, events_init=None, iter=0
241
278
  if events_init is not None:
242
279
  events_inverted = events_inverted.merge(events_init[["event_index", "idx_eve"]], on="idx_eve")
243
280
 
244
- print(f"ADLoc using {len(picks_inverted[picks_inverted['mask'] == 1])} picks outof {len(picks_inverted)} picks")
281
+ print(f"ADLoc locates {len(events_inverted)} events outof {num_events} events")
282
+ print(f"using {len(picks_inverted[picks_inverted['mask'] == 1])} picks outof {len(picks_inverted)} picks")
245
283
 
246
284
  return picks_inverted, events_inverted
247
285
 
@@ -2,9 +2,9 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name="ADLoc",
5
- version="1.0.0",
5
+ version="1.0.2",
6
6
  long_description="ADLoc",
7
7
  long_description_content_type="text/markdown",
8
8
  packages=["adloc"],
9
- install_requires=["numpy", "h5py", "matplotlib", "pandas", "tqdm", "pyproj", "numba"],
9
+ install_requires=["numpy", "h5py", "matplotlib", "pandas", "tqdm", "pyproj", "numba", "scipy"],
10
10
  )
@@ -1,6 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: ADLoc
3
- Version: 1.0.0
4
- Description-Content-Type: text/markdown
5
-
6
- ADLoc
ADLoc-1.0.0/PKG-INFO DELETED
@@ -1,6 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: ADLoc
3
- Version: 1.0.0
4
- Description-Content-Type: text/markdown
5
-
6
- ADLoc
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes