DASPy-toolbox 1.0.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.
Files changed (49) hide show
  1. DASPy_toolbox-1.0.0.dist-info/LICENSE.txt +1 -0
  2. DASPy_toolbox-1.0.0.dist-info/METADATA +85 -0
  3. DASPy_toolbox-1.0.0.dist-info/RECORD +49 -0
  4. DASPy_toolbox-1.0.0.dist-info/WHEEL +5 -0
  5. DASPy_toolbox-1.0.0.dist-info/entry_points.txt +2 -0
  6. DASPy_toolbox-1.0.0.dist-info/top_level.txt +1 -0
  7. daspy/__init__.py +4 -0
  8. daspy/advanced_tools/__init__.py +0 -0
  9. daspy/advanced_tools/channel.py +354 -0
  10. daspy/advanced_tools/decomposition.py +165 -0
  11. daspy/advanced_tools/denoising.py +276 -0
  12. daspy/advanced_tools/fdct.py +789 -0
  13. daspy/advanced_tools/strain2vel.py +245 -0
  14. daspy/basic_tools/__init__.py +0 -0
  15. daspy/basic_tools/filter.py +257 -0
  16. daspy/basic_tools/freqattributes.py +117 -0
  17. daspy/basic_tools/preprocessing.py +238 -0
  18. daspy/basic_tools/visualization.py +186 -0
  19. daspy/core/__init__.py +4 -0
  20. daspy/core/collection.py +279 -0
  21. daspy/core/dasdatetime.py +72 -0
  22. daspy/core/example.pkl +0 -0
  23. daspy/core/make_example.py +32 -0
  24. daspy/core/read.py +544 -0
  25. daspy/core/section.py +1319 -0
  26. daspy/core/write.py +282 -0
  27. daspy/seismic_detection/__init__.py +1 -0
  28. daspy/seismic_detection/calc_travel_time.py +23 -0
  29. daspy/seismic_detection/core.py +119 -0
  30. daspy/seismic_detection/detection.py +12 -0
  31. daspy/seismic_detection/gamma/__init__.py +13 -0
  32. daspy/seismic_detection/gamma/_base.py +549 -0
  33. daspy/seismic_detection/gamma/_bayesian_mixture.py +875 -0
  34. daspy/seismic_detection/gamma/_gaussian_mixture.py +866 -0
  35. daspy/seismic_detection/gamma/app.py +192 -0
  36. daspy/seismic_detection/gamma/seismic_ops.py +478 -0
  37. daspy/seismic_detection/gamma/utils.py +512 -0
  38. daspy/seismic_detection/location.py +266 -0
  39. daspy/seismic_detection/magnitude.py +43 -0
  40. daspy/seismic_detection/phase_picking.py +67 -0
  41. daspy/structure_imaging/__init__.py +0 -0
  42. daspy/structure_imaging/ambient_noise.py +4 -0
  43. daspy/structure_imaging/dispersion.py +27 -0
  44. daspy/structure_imaging/fault_zone.py +59 -0
  45. daspy/structure_imaging/inversion.py +6 -0
  46. daspy/traffic_monitoring/JamDetection.py +6 -0
  47. daspy/traffic_monitoring/SpeedMeasurement.py +6 -0
  48. daspy/traffic_monitoring/VehicleDetection.py +6 -0
  49. daspy/traffic_monitoring/__init__.py +0 -0
@@ -0,0 +1,549 @@
1
+ """Base class for mixture models."""
2
+
3
+ # Author: Wei Xue <xuewei4d@gmail.com>
4
+ # Modified by Thierry Guillemot <thierry.guillemot.work@gmail.com>
5
+ # License: BSD 3 clause
6
+
7
+ import warnings
8
+ from abc import ABCMeta, abstractmethod
9
+ from time import time
10
+ import numpy as np
11
+ from scipy.special import logsumexp
12
+ from sklearn import cluster
13
+ from sklearn.base import BaseEstimator
14
+ from sklearn.base import DensityMixin
15
+ from sklearn.exceptions import ConvergenceWarning
16
+ from sklearn.utils import check_array, check_random_state
17
+ from sklearn.utils.validation import check_is_fitted
18
+
19
+ from .seismic_ops import initialize_centers
20
+
21
+ def _check_shape(param, param_shape, name):
22
+ """Validate the shape of the input parameter 'param'.
23
+
24
+ Parameters
25
+ ----------
26
+ param : array
27
+
28
+ param_shape : tuple
29
+
30
+ name : string
31
+ """
32
+ param = np.array(param)
33
+ if param.shape != param_shape:
34
+ raise ValueError("The parameter '%s' should have the shape of %s, "
35
+ "but got %s" % (name, param_shape, param.shape))
36
+
37
+
38
+ def _check_X(X, n_components=None, n_features=None, ensure_min_samples=1):
39
+ """Check the input data X.
40
+
41
+ Parameters
42
+ ----------
43
+ X : array-like of shape (n_samples, n_features)
44
+
45
+ n_components : int
46
+
47
+ Returns
48
+ -------
49
+ X : array, shape (n_samples, n_features)
50
+ """
51
+ X = check_array(X, dtype=[np.float64, np.float32],
52
+ ensure_min_samples=ensure_min_samples)
53
+ if n_components is not None and X.shape[0] < n_components:
54
+ raise ValueError('Expected n_samples >= n_components '
55
+ 'but got n_components = %d, n_samples = %d'
56
+ % (n_components, X.shape[0]))
57
+ if n_features is not None and X.shape[-1] != n_features:
58
+ raise ValueError("Expected the input data X have %d features, "
59
+ "but got %d features"
60
+ % (n_features, X.shape[-1]))
61
+ return X
62
+
63
+
64
+ class BaseMixture(DensityMixin, BaseEstimator, metaclass=ABCMeta):
65
+ """Base class for mixture models.
66
+
67
+ This abstract class specifies an interface for all mixture classes and
68
+ provides basic common methods for mixture models.
69
+ """
70
+
71
+ def __init__(self, n_components, tol, reg_covar,
72
+ max_iter, n_init, init_params, random_state, warm_start,
73
+ verbose, verbose_interval,
74
+ dummy_comp=False, dummy_prob=0.01, dummy_quantile=0.1):
75
+ self.n_components = n_components
76
+ self.tol = tol
77
+ self.reg_covar = reg_covar
78
+ self.max_iter = max_iter
79
+ self.n_init = n_init
80
+ self.init_params = init_params
81
+ self.random_state = random_state
82
+ self.warm_start = warm_start
83
+ self.verbose = verbose
84
+ self.verbose_interval = verbose_interval
85
+ self.dummy_comp = dummy_comp
86
+ self.dummy_prob = dummy_prob
87
+ self.dummy_quantile = dummy_quantile
88
+
89
+ def _check_initial_parameters(self, X):
90
+ """Check values of the basic parameters.
91
+
92
+ Parameters
93
+ ----------
94
+ X : array-like of shape (n_samples, n_features)
95
+ """
96
+ if self.n_components < 1:
97
+ raise ValueError("Invalid value for 'n_components': %d "
98
+ "Estimation requires at least one component"
99
+ % self.n_components)
100
+
101
+ if self.tol < 0.:
102
+ raise ValueError("Invalid value for 'tol': %.5f "
103
+ "Tolerance used by the EM must be non-negative"
104
+ % self.tol)
105
+
106
+ if self.n_init < 1:
107
+ raise ValueError("Invalid value for 'n_init': %d "
108
+ "Estimation requires at least one run"
109
+ % self.n_init)
110
+
111
+ if self.max_iter < 1:
112
+ raise ValueError("Invalid value for 'max_iter': %d "
113
+ "Estimation requires at least one iteration"
114
+ % self.max_iter)
115
+
116
+ if self.reg_covar < 0.:
117
+ raise ValueError("Invalid value for 'reg_covar': %.5f "
118
+ "regularization on covariance must be "
119
+ "non-negative"
120
+ % self.reg_covar)
121
+
122
+ # Check all the parameters values of the derived class
123
+ self._check_parameters(X)
124
+
125
+ @abstractmethod
126
+ def _check_parameters(self, X):
127
+ """Check initial parameters of the derived class.
128
+
129
+ Parameters
130
+ ----------
131
+ X : array-like of shape (n_samples, n_features)
132
+ """
133
+ pass
134
+
135
+ def _initialize_parameters(self, X, random_state):
136
+ """Initialize the model parameters.
137
+
138
+ Parameters
139
+ ----------
140
+ X : array-like of shape (n_samples, n_features)
141
+
142
+ random_state : RandomState
143
+ A random number generator instance that controls the random seed
144
+ used for the method chosen to initialize the parameters.
145
+ """
146
+ n_samples, _ = X.shape
147
+
148
+ if self.init_params == 'kmeans':
149
+ resp = np.zeros((n_samples, self.n_components))
150
+ if self.dummy_comp:
151
+ label = cluster.KMeans(n_clusters=self.n_components-1, n_init=1,
152
+ random_state=random_state).fit(X).labels_
153
+ else:
154
+ label = cluster.KMeans(n_clusters=self.n_components, n_init=1,
155
+ random_state=random_state).fit(X).labels_
156
+ resp[np.arange(n_samples), label] = 1
157
+ elif self.init_params == 'random':
158
+ resp = random_state.rand(n_samples, self.n_components)
159
+ resp /= resp.sum(axis=1)[:, np.newaxis]
160
+ elif self.init_params == 'centers':
161
+ # resp = self._initialize_centers(X, random_state)
162
+ resp, centers, means = initialize_centers(X, self.phase_type, self.centers_init, self.station_locs, random_state)
163
+ self.centers_init = centers
164
+ else:
165
+ raise ValueError("Unimplemented initialization method '%s'"
166
+ % self.init_params)
167
+
168
+ self._initialize(X, resp)
169
+
170
+ @abstractmethod
171
+ def _initialize(self, X, resp):
172
+ """Initialize the model parameters of the derived class.
173
+
174
+ Parameters
175
+ ----------
176
+ X : array-like of shape (n_samples, n_features)
177
+
178
+ resp : array-like of shape (n_samples, n_components)
179
+ """
180
+ pass
181
+
182
+ def fit(self, X, y=None):
183
+ """Estimate model parameters with the EM algorithm.
184
+
185
+ The method fits the model ``n_init`` times and sets the parameters with
186
+ which the model has the largest likelihood or lower bound. Within each
187
+ trial, the method iterates between E-step and M-step for ``max_iter``
188
+ times until the change of likelihood or lower bound is less than
189
+ ``tol``, otherwise, a ``ConvergenceWarning`` is raised.
190
+ If ``warm_start`` is ``True``, then ``n_init`` is ignored and a single
191
+ initialization is performed upon the first call. Upon consecutive
192
+ calls, training starts where it left off.
193
+
194
+ Parameters
195
+ ----------
196
+ X : array-like of shape (n_samples, n_features)
197
+ List of n_features-dimensional data points. Each row
198
+ corresponds to a single data point.
199
+
200
+ Returns
201
+ -------
202
+ self
203
+ """
204
+ self.fit_predict(X, y)
205
+ return self
206
+
207
+ def fit_predict(self, X, y=None):
208
+ """Estimate model parameters using X and predict the labels for X.
209
+
210
+ The method fits the model n_init times and sets the parameters with
211
+ which the model has the largest likelihood or lower bound. Within each
212
+ trial, the method iterates between E-step and M-step for `max_iter`
213
+ times until the change of likelihood or lower bound is less than
214
+ `tol`, otherwise, a :class:`~sklearn.exceptions.ConvergenceWarning` is
215
+ raised. After fitting, it predicts the most probable label for the
216
+ input data points.
217
+
218
+ .. versionadded:: 0.20
219
+
220
+ Parameters
221
+ ----------
222
+ X : array-like of shape (n_samples, n_features)
223
+ List of n_features-dimensional data points. Each row
224
+ corresponds to a single data point.
225
+
226
+ Returns
227
+ -------
228
+ labels : array, shape (n_samples,)
229
+ Component labels.
230
+ """
231
+ X = _check_X(X, self.n_components, ensure_min_samples=2)
232
+ self._check_n_features(X, reset=True)
233
+ self._check_initial_parameters(X)
234
+
235
+ # if we enable warm_start, we will have a unique initialisation
236
+ do_init = not(self.warm_start and hasattr(self, 'converged_'))
237
+ n_init = self.n_init if do_init else 1
238
+
239
+ max_lower_bound = -np.infty
240
+ self.converged_ = False
241
+
242
+ random_state = check_random_state(self.random_state)
243
+
244
+ n_samples, _ = X.shape
245
+ for init in range(n_init):
246
+ self._print_verbose_msg_init_beg(init)
247
+
248
+ if do_init:
249
+ self._initialize_parameters(X, random_state)
250
+
251
+ lower_bound = (-np.infty if do_init else self.lower_bound_)
252
+
253
+ for n_iter in range(1, self.max_iter + 1):
254
+ prev_lower_bound = lower_bound
255
+
256
+ log_prob_norm, log_resp = self._e_step(X)
257
+ self._m_step(X, log_resp)
258
+ lower_bound = self._compute_lower_bound(
259
+ log_resp, log_prob_norm)
260
+
261
+ change = lower_bound - prev_lower_bound
262
+ self._print_verbose_msg_iter_end(n_iter, change)
263
+
264
+ if abs(change) < self.tol:
265
+ self.converged_ = True
266
+ break
267
+
268
+ self._print_verbose_msg_init_end(lower_bound)
269
+
270
+ if lower_bound > max_lower_bound:
271
+ max_lower_bound = lower_bound
272
+ best_params = self._get_parameters()
273
+ best_n_iter = n_iter
274
+
275
+ # if not self.converged_:
276
+ # warnings.warn('Initialization %d did not converge. '
277
+ # 'Try different init parameters, '
278
+ # 'or increase max_iter, tol '
279
+ # 'or check for degenerate data.'
280
+ # % (init + 1), ConvergenceWarning)
281
+ if not self.converged_:
282
+ print(f"\nInitialization {init + 1} did not converge.")
283
+
284
+ self._set_parameters(best_params)
285
+ self.n_iter_ = best_n_iter
286
+ self.lower_bound_ = max_lower_bound
287
+
288
+ # Always do a final e-step to guarantee that the labels returned by
289
+ # fit_predict(X) are always consistent with fit(X).predict(X)
290
+ # for any value of max_iter and tol (and any random_state).
291
+ _, log_resp = self._e_step(X)
292
+
293
+ return log_resp.argmax(axis=1)
294
+
295
+ def _e_step(self, X):
296
+ """E step.
297
+
298
+ Parameters
299
+ ----------
300
+ X : array-like of shape (n_samples, n_features)
301
+
302
+ Returns
303
+ -------
304
+ log_prob_norm : float
305
+ Mean of the logarithms of the probabilities of each sample in X
306
+
307
+ log_responsibility : array, shape (n_samples, n_components)
308
+ Logarithm of the posterior probabilities (or responsibilities) of
309
+ the point of each sample in X.
310
+ """
311
+ log_prob_norm, log_resp = self._estimate_log_prob_resp(X)
312
+ return np.mean(log_prob_norm), log_resp
313
+
314
+ @abstractmethod
315
+ def _m_step(self, X, log_resp):
316
+ """M step.
317
+
318
+ Parameters
319
+ ----------
320
+ X : array-like of shape (n_samples, n_features)
321
+
322
+ log_resp : array-like of shape (n_samples, n_components)
323
+ Logarithm of the posterior probabilities (or responsibilities) of
324
+ the point of each sample in X.
325
+ """
326
+ pass
327
+
328
+ @abstractmethod
329
+ def _get_parameters(self):
330
+ pass
331
+
332
+ @abstractmethod
333
+ def _set_parameters(self, params):
334
+ pass
335
+
336
+ def score_samples(self, X):
337
+ """Compute the weighted log probabilities for each sample.
338
+
339
+ Parameters
340
+ ----------
341
+ X : array-like of shape (n_samples, n_features)
342
+ List of n_features-dimensional data points. Each row
343
+ corresponds to a single data point.
344
+
345
+ Returns
346
+ -------
347
+ log_prob : array, shape (n_samples,)
348
+ Log probabilities of each data point in X.
349
+ """
350
+ check_is_fitted(self)
351
+ X = _check_X(X, None, self.means_.shape[-1])
352
+
353
+ return logsumexp(self._estimate_weighted_log_prob(X), axis=1)
354
+
355
+ def score(self, X, y=None):
356
+ """Compute the per-sample average log-likelihood of the given data X.
357
+
358
+ Parameters
359
+ ----------
360
+ X : array-like of shape (n_samples, n_dimensions)
361
+ List of n_features-dimensional data points. Each row
362
+ corresponds to a single data point.
363
+
364
+ Returns
365
+ -------
366
+ log_likelihood : float
367
+ Log likelihood of the Gaussian mixture given X.
368
+ """
369
+ return self.score_samples(X).mean()
370
+
371
+ def predict(self, X):
372
+ """Predict the labels for the data samples in X using trained model.
373
+
374
+ Parameters
375
+ ----------
376
+ X : array-like of shape (n_samples, n_features)
377
+ List of n_features-dimensional data points. Each row
378
+ corresponds to a single data point.
379
+
380
+ Returns
381
+ -------
382
+ labels : array, shape (n_samples,)
383
+ Component labels.
384
+ """
385
+ check_is_fitted(self)
386
+ X = _check_X(X, None, self.means_.shape[-1])
387
+ return self._estimate_weighted_log_prob(X).argmax(axis=1)
388
+
389
+ def predict_proba(self, X):
390
+ """Predict posterior probability of each component given the data.
391
+
392
+ Parameters
393
+ ----------
394
+ X : array-like of shape (n_samples, n_features)
395
+ List of n_features-dimensional data points. Each row
396
+ corresponds to a single data point.
397
+
398
+ Returns
399
+ -------
400
+ resp : array, shape (n_samples, n_components)
401
+ Returns the probability each Gaussian (state) in
402
+ the model given each sample.
403
+ """
404
+ check_is_fitted(self)
405
+ X = _check_X(X, None, self.means_.shape[-1])
406
+ _, log_resp = self._estimate_log_prob_resp(X)
407
+ return np.exp(log_resp)
408
+
409
+ def sample(self, n_samples=1):
410
+ """Generate random samples from the fitted Gaussian distribution.
411
+
412
+ Parameters
413
+ ----------
414
+ n_samples : int, default=1
415
+ Number of samples to generate.
416
+
417
+ Returns
418
+ -------
419
+ X : array, shape (n_samples, n_features)
420
+ Randomly generated sample
421
+
422
+ y : array, shape (nsamples,)
423
+ Component labels
424
+
425
+ """
426
+ check_is_fitted(self)
427
+
428
+ if n_samples < 1:
429
+ raise ValueError(
430
+ "Invalid value for 'n_samples': %d . The sampling requires at "
431
+ "least one sample." % (self.n_components))
432
+
433
+ _, _, n_features = self.means_.shape
434
+ rng = check_random_state(self.random_state)
435
+ n_samples_comp = rng.multinomial(n_samples, self.weights_)
436
+
437
+ if self.covariance_type == 'full':
438
+ X = np.vstack([
439
+ rng.multivariate_normal(mean, covariance, int(sample))
440
+ for (mean, covariance, sample) in zip(
441
+ self.means_, self.covariances_, n_samples_comp)])
442
+ elif self.covariance_type == "tied":
443
+ X = np.vstack([
444
+ rng.multivariate_normal(mean, self.covariances_, int(sample))
445
+ for (mean, sample) in zip(
446
+ self.means_, n_samples_comp)])
447
+ else:
448
+ X = np.vstack([
449
+ mean + rng.randn(sample, n_features) * np.sqrt(covariance)
450
+ for (mean, covariance, sample) in zip(
451
+ self.means_, self.covariances_, n_samples_comp)])
452
+
453
+ y = np.concatenate([np.full(sample, j, dtype=int)
454
+ for j, sample in enumerate(n_samples_comp)])
455
+
456
+ return (X, y)
457
+
458
+ def _estimate_weighted_log_prob(self, X):
459
+ """Estimate the weighted log-probabilities, log P(X | Z) + log weights.
460
+
461
+ Parameters
462
+ ----------
463
+ X : array-like of shape (n_samples, n_features)
464
+
465
+ Returns
466
+ -------
467
+ weighted_log_prob : array, shape (n_samples, n_component)
468
+ """
469
+ return self._estimate_log_prob(X) + self._estimate_log_weights()
470
+
471
+ @abstractmethod
472
+ def _estimate_log_weights(self):
473
+ """Estimate log-weights in EM algorithm, E[ log pi ] in VB algorithm.
474
+
475
+ Returns
476
+ -------
477
+ log_weight : array, shape (n_components, )
478
+ """
479
+ pass
480
+
481
+ @abstractmethod
482
+ def _estimate_log_prob(self, X):
483
+ """Estimate the log-probabilities log P(X | Z).
484
+
485
+ Compute the log-probabilities per each component for each sample.
486
+
487
+ Parameters
488
+ ----------
489
+ X : array-like of shape (n_samples, n_features)
490
+
491
+ Returns
492
+ -------
493
+ log_prob : array, shape (n_samples, n_component)
494
+ """
495
+ pass
496
+
497
+ def _estimate_log_prob_resp(self, X):
498
+ """Estimate log probabilities and responsibilities for each sample.
499
+
500
+ Compute the log probabilities, weighted log probabilities per
501
+ component and responsibilities for each sample in X with respect to
502
+ the current state of the model.
503
+
504
+ Parameters
505
+ ----------
506
+ X : array-like of shape (n_samples, n_features)
507
+
508
+ Returns
509
+ -------
510
+ log_prob_norm : array, shape (n_samples,)
511
+ log p(X)
512
+
513
+ log_responsibilities : array, shape (n_samples, n_components)
514
+ logarithm of the responsibilities
515
+ """
516
+ weighted_log_prob = self._estimate_weighted_log_prob(X)
517
+ log_prob_norm = logsumexp(weighted_log_prob, axis=1)
518
+ with np.errstate(under='ignore'):
519
+ # ignore underflow
520
+ log_resp = weighted_log_prob - log_prob_norm[:, np.newaxis]
521
+ return log_prob_norm, log_resp
522
+
523
+ def _print_verbose_msg_init_beg(self, n_init):
524
+ """Print verbose message on initialization."""
525
+ if self.verbose == 1:
526
+ print("Initialization %d" % n_init)
527
+ elif self.verbose >= 2:
528
+ print("Initialization %d" % n_init)
529
+ self._init_prev_time = time()
530
+ self._iter_prev_time = self._init_prev_time
531
+
532
+ def _print_verbose_msg_iter_end(self, n_iter, diff_ll):
533
+ """Print verbose message on initialization."""
534
+ if n_iter % self.verbose_interval == 0:
535
+ if self.verbose == 1:
536
+ print(" Iteration %d" % n_iter)
537
+ elif self.verbose >= 2:
538
+ cur_time = time()
539
+ print(" Iteration %d\t time lapse %.5fs\t ll change %.5f" % (
540
+ n_iter, cur_time - self._iter_prev_time, diff_ll))
541
+ self._iter_prev_time = cur_time
542
+
543
+ def _print_verbose_msg_init_end(self, ll):
544
+ """Print verbose message on the end of iteration."""
545
+ if self.verbose == 1:
546
+ print("Initialization converged: %s" % self.converged_)
547
+ elif self.verbose >= 2:
548
+ print("Initialization converged: %s\t time lapse %.5fs\t ll %.5f" %
549
+ (self.converged_, time() - self._init_prev_time, ll))