forked from simpeg/simpeg
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpgi_utils.py
More file actions
1853 lines (1620 loc) · 68.4 KB
/
pgi_utils.py
File metadata and controls
1853 lines (1620 loc) · 68.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from scipy import linalg
from scipy.special import logsumexp
import warnings
from simpeg.maps import IdentityMap
from discretize.utils.code_utils import requires
# sklearn is a soft dependency
try:
import sklearn
from sklearn.mixture import GaussianMixture
from sklearn.cluster import KMeans
from sklearn.utils import check_array
from sklearn.utils.validation import check_is_fitted
from sklearn.mixture._gaussian_mixture import (
_compute_precision_cholesky,
_compute_log_det_cholesky,
_estimate_gaussian_covariances_full,
_estimate_gaussian_covariances_diag,
_estimate_gaussian_covariances_spherical,
_check_means,
_check_precisions,
_check_shape,
)
from sklearn.mixture._base import check_random_state, ConvergenceWarning
except ImportError:
GaussianMixture = None
sklearn = False
else:
# Try to import `validate_data` (added in sklearn 1.6).
# We should remove these bits when we set sklearn>=1.6 as the minimum version, and
# just import `validate_data`.
try:
from sklearn.utils.validation import validate_data
except ImportError:
validate_data = None
###############################################################################
# Disclaimer: the following classes built upon the GaussianMixture class #
# from Scikit-Learn. New functionalities are added, as well as modifications #
# to existing functions, to serve the purposes pursued within SimPEG. #
# This use is allowed by the Scikit-Learn licensing (BSD-3-Clause License) #
# and we are grateful for their contributions to the open-source community. #
###############################################################################
class WeightedGaussianMixture(GaussianMixture if sklearn else object):
"""
Weighted Gaussian mixture class
This class is built upon the :class:`sklearn.mixture.GaussianMixture` class from
scikit-learn, with two main modifications:
1. Each sample/observation is given a weight, the volume of the corresponding
:class:`discretize.base.BaseMesh` cell, when fitting the Gaussian Mixture Model
(GMM). More volume gives more importance, ensuing a mesh-free evaluation of the
clusters of the geophysical model.
2. When set manually, the proportions can be set either globally (normal behavior)
or cell-by-cell (improvements).
.. attention::
This class built upon the :class:`sklearn.mixture.GaussianMixture` class from
scikit-learn. New functionalities are added, as well as modifications to
existing functions, to serve the purposes pursued within SimPEG. This use is
allowed by the scikit-learn licensing (BSD-3-Clause License) and we are grateful
for their contributions to the open-source community.
There are some additional parameters to provide to this class, compared to
:class:`sklearn.mixture.GaussianMixture`.
Parameters
----------
n_components : int
Number of components
mesh : discretize.base.BaseMesh
:class:`discretize.TensorMesh` or :class:`discretize.TreeMesh` mesh. The volume
of the cells give each sample/observations its weight in the fitting process.
actv : array, optional
Active indices.
"""
@requires({"sklearn": sklearn})
def __init__(
self,
n_components,
mesh,
actv=None,
covariance_type="full",
init_params="kmeans",
max_iter=100,
means_init=None,
n_init=10,
precisions_init=None,
random_state=None,
reg_covar=1e-06,
tol=0.001,
verbose=0,
verbose_interval=10,
warm_start=False,
weights_init=None,
# **kwargs
):
self.mesh = mesh
self.actv = actv
if self.actv is None:
self.cell_volumes = self.mesh.cell_volumes
else:
self.cell_volumes = self.mesh.cell_volumes[self.actv]
super(WeightedGaussianMixture, self).__init__(
covariance_type=covariance_type,
init_params=init_params,
max_iter=max_iter,
means_init=means_init,
n_components=n_components,
n_init=n_init,
precisions_init=precisions_init,
random_state=random_state,
reg_covar=reg_covar,
tol=tol,
verbose=verbose,
verbose_interval=verbose_interval,
warm_start=warm_start,
weights_init=weights_init,
# **kwargs
)
# set_kwargs(self, **kwargs)
def compute_clusters_precisions(self):
"""Compute and set the precisions matrices and their Cholesky decomposition.
Use this function after setting covariances manually.
"""
self.precisions_cholesky_ = _compute_precision_cholesky(
self.covariances_, self.covariance_type
)
if self.covariance_type == "full":
self.precisions_ = np.empty(self.precisions_cholesky_.shape)
for k, prec_chol in enumerate(self.precisions_cholesky_):
self.precisions_[k] = np.dot(prec_chol, prec_chol.T)
elif self.covariance_type == "tied":
self.precisions_ = np.dot(
self.precisions_cholesky_, self.precisions_cholesky_.T
)
else:
self.precisions_ = self.precisions_cholesky_**2
def compute_clusters_covariances(self):
"""Compute the precisions matrices and their Cholesky decomposition.
Use this function after setting precisions matrices manually.
"""
self.covariances_cholesky_ = _compute_precision_cholesky(
self.precisions_, self.covariance_type
)
if self.covariance_type == "full":
self.covariances_ = np.empty(self.covariances_cholesky_.shape)
for k, cov_chol in enumerate(self.covariances_cholesky_):
self.covariances_[k] = np.dot(cov_chol, cov_chol.T)
elif self.covariance_type == "tied":
self.covariances_ = np.dot(
self.covariances_cholesky_, self.covariances_cholesky_.T
)
else:
self.covariances_ = self.covariances_cholesky_**2
self.precisions_cholesky_ = _compute_precision_cholesky(
self.covariances_, self.covariance_type
)
def order_clusters_GM_weight(self, outputindex=False):
"""Order clusters by decreasing weights
Parameters
----------
outputindex : bool, default: ``True``
If ``True``, return the sorting index
Returns
-------
np.ndarray
Sorting index
"""
if self.weights_.ndim == 1:
indx = np.argsort(self.weights_, axis=0)[::-1]
self.weights_ = self.weights_[indx].reshape(self.weights_.shape)
else:
indx = np.argsort(self.weights_.sum(axis=0), axis=0)[::-1]
self.weights_ = self.weights_[:, indx].reshape(self.weights_.shape)
self.means_ = self.means_[indx].reshape(self.means_.shape)
if self.covariance_type == "tied":
pass
else:
self.precisions_ = self.precisions_[indx].reshape(self.precisions_.shape)
self.covariances_ = self.covariances_[indx].reshape(self.covariances_.shape)
self.precisions_cholesky_ = _compute_precision_cholesky(
self.covariances_, self.covariance_type
)
if outputindex:
return indx
def _check_weights(self, weights, n_components, n_samples):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
Check the user provided 'weights'.
Parameters
----------
weights : array-like, shape (n_components,) or (n_samples, n_components)
The proportions of components of each mixture.
n_components : int
Number of components.
n_samples : int or None
Number of samples.
Returns
-------
weights : (n_components,) or (n_samples, n_components) numpy.ndarray
"""
weights = np.asarray(weights)
if len(weights.shape) == 2:
weights = check_array(
weights, dtype=[np.float64, np.float32], ensure_2d=True
)
_check_shape(weights, (n_samples, n_components), "weights")
else:
weights = check_array(
weights, dtype=[np.float64, np.float32], ensure_2d=False
)
_check_shape(weights, (n_components,), "weights")
# check range
if (weights < 0.0).any() or (weights > 1.0).any():
raise ValueError(
"The parameter 'weights' should be in the range "
"[0, 1], but got max value %.5f, min value %.5f"
% (np.min(weights), np.max(weights))
)
# check normalization
if not np.allclose(np.abs(1.0 - np.sum(weights.T, axis=0)), 0.0):
raise ValueError(
"The parameter 'weights' should be normalized, "
"but got sum(weights) = %.5f" % np.sum(weights)
)
return weights
def _warn_xp_not_numpy(self, xp):
"""
Raise warning if the passed array API is not Numpy.
SimPEG's Gaussian Mixture Models don't currently support other array APIs beside
Numpy, so it's better to warn users that are intending to use another API.
"""
if xp is None or xp is np:
return
try:
module = xp.__array_namespace_info__.__module__
except AttributeError:
module = "unknown"
if module.lower() != "numpy":
warnings.warn(
"Using array API is not supported in SimPEG's Gaussian Mixture Models "
"yet. Numpy will be used instead.",
UserWarning,
stacklevel=2,
)
def _check_parameters(self, X, xp=None):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
Check the Gaussian mixture parameters are well defined.
"""
self._warn_xp_not_numpy(xp)
n_samples, n_features = X.shape
if self.covariance_type not in ["spherical", "tied", "diag", "full"]:
raise ValueError(
"Invalid value for 'covariance_type': %s "
"'covariance_type' should be in "
"['spherical', 'tied', 'diag', 'full']" % self.covariance_type
)
if self.weights_init is not None:
self.weights_init = self._check_weights(
self.weights_init,
self.n_components,
n_samples,
)
if self.means_init is not None:
self.means_init = _check_means(
self.means_init, self.n_components, n_features
)
if self.precisions_init is not None:
self.precisions_init = _check_precisions(
self.precisions_init,
self.covariance_type,
self.n_components,
n_features,
)
def _initialize_parameters(self, X, random_state, xp=None):
"""
[modified from Scikit-Learn.mixture._base]
Initialize the model parameters.
Parameters
----------
X : array-like, shape (n_samples, n_features)
random_state : RandomState
A random number generator instance.
"""
self._warn_xp_not_numpy(xp)
n_samples, _ = X.shape
if self.init_params == "kmeans":
resp = np.zeros((n_samples, self.n_components))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
label = (
KMeans(
n_clusters=self.n_components, n_init=1, random_state=random_state
)
.fit(X, sample_weight=self.cell_volumes)
.labels_
)
resp[np.arange(n_samples), label] = 1
elif self.init_params == "random":
resp = random_state.rand(n_samples, self.n_components)
resp /= resp.sum(axis=1)[:, np.newaxis]
else:
raise ValueError(
"Unimplemented initialization method '%s'" % self.init_params
)
self._initialize(X, resp)
def _m_step(self, X, log_resp, xp=None):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
M step.
Parameters
----------
X : array-like, shape (n_samples, n_features)
log_resp : array-like, shape (n_samples, n_components)
Logarithm of the posterior probabilities (or responsibilities) of
the point of each sample in X.
"""
self._warn_xp_not_numpy(xp)
n_samples, _ = X.shape
Volume = np.mean(self.cell_volumes)
weights, self.means_, self.covariances_ = self._estimate_gaussian_parameters(
X, self.mesh, np.exp(log_resp), self.reg_covar, self.covariance_type
)
weights /= n_samples * Volume
self.precisions_cholesky_ = _compute_precision_cholesky(
self.covariances_, self.covariance_type
)
if len(self.weights_.shape) == 1:
self.weights_ = weights
def _estimate_gaussian_covariances_tied(self, resp, X, nk, means, reg_covar):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
Estimate the tied covariance matrix.
Parameters
----------
resp : array-like, shape (n_samples, n_components)
X : array-like, shape (n_samples, n_features)
nk : array-like, shape (n_components,)
means : array-like, shape (n_components, n_features)
reg_covar : float
Returns
-------
covariance : array, shape (n_features, n_features)
The tied covariance matrix of the components.
"""
avg_X2 = np.dot(self.cell_volumes * X.T, X)
avg_means2 = np.dot(nk * means.T, means)
covariance = avg_X2 - avg_means2
covariance /= nk.sum()
covariance.flat[:: len(covariance) + 1] += reg_covar
return covariance
def _estimate_gaussian_parameters(self, X, mesh, resp, reg_covar, covariance_type):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
Estimate the Gaussian distribution parameters.
Parameters
----------
X : array-like, shape (n_samples, n_features)
The input data array.
resp : array-like, shape (n_samples, n_components)
The responsibilities for each data sample in X.
reg_covar : float
The regularization added to the diagonal of the covariance matrices.
covariance_type : {'full', 'tied', 'diag', 'spherical'}
The type of precision matrices.
Returns
-------
nk : array-like, shape (n_components,)
The numbers of data samples in the current components.
means : array-like, shape (n_components, n_features)
The centers of the current components.
covariances : array-like
The covariance matrix of the current components.
The shape depends of the covariance_type.
"""
respVol = self.cell_volumes.reshape(-1, 1) * resp
nk = respVol.sum(axis=0) + 10 * np.finfo(resp.dtype).eps
means = np.dot(respVol.T, X) / nk[:, np.newaxis]
covariances = {
"full": _estimate_gaussian_covariances_full,
"tied": self._estimate_gaussian_covariances_tied,
"diag": _estimate_gaussian_covariances_diag,
"spherical": _estimate_gaussian_covariances_spherical,
}[covariance_type](respVol, X, nk, means, reg_covar)
return nk, means, covariances
def _e_step(self, X, xp=None):
"""
[modified from Scikit-Learn.mixture.gaussian_mixture]
E step.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Returns
-------
log_prob_norm : float
Mean of the logarithms of the probabilities of each sample in X
log_responsibility : array, shape (n_samples, n_components)
Logarithm of the posterior probabilities (or responsibilities) of
the point of each sample in X.
"""
self._warn_xp_not_numpy(xp)
log_prob_norm, log_resp = self._estimate_log_prob_resp(X)
return np.average(log_prob_norm, weights=self.cell_volumes), log_resp
def score(self, X, y=None):
"""Compute the per-sample average log-likelihood
[modified from Scikit-Learn.mixture.gaussian_mixture]
Compute the per-sample average log-likelihood of the given data X.
Parameters
----------
X : (n_samples, n_dimensions) array-like
List of n_features-dimensional data points. Each row
corresponds to a single data point.
y : ``None``
Placeholder variable
Returns
-------
float
Log likelihood of the Gaussian mixture given X.
"""
return np.average(self.score_samples(X), weights=self.cell_volumes)
def _estimate_log_gaussian_prob_with_sensW(
self, X, sensW, means, precisions_chol, covariance_type
):
"""
[New function, modified from Scikit-Learn.mixture.gaussian_mixture._estimate_log_gaussian_prob]
Estimate the log Gaussian probability with depth or sensitivity weighting.
Parameters
----------
X : array-like, shape (n_samples, n_features)
means : array-like, shape (n_components, n_features)
sensW: array-like, Sensitvity or Depth Weighting, shape(n_samples, n_features)
precisions_chol : array-like,
Cholesky decompositions of the precision matrices.
'full' : shape of (n_components, n_features, n_features)
'tied' : shape of (n_features, n_features)
'diag' : shape of (n_components, n_features)
'spherical' : shape of (n_components,)
covariance_type : {'full', 'tied', 'diag', 'spherical'}
Returns
-------
log_prob : array, shape (n_samples, n_components)
"""
n_samples, n_features = X.shape
n_components, _ = means.shape
# det(precision_chol) is half of det(precision)
log_det = _compute_log_det_cholesky(
precisions_chol, covariance_type, n_features
)
if covariance_type == "full":
log_prob = np.empty((n_samples, n_components))
for k, (mu, prec_chol) in enumerate(zip(means, precisions_chol)):
y = np.dot(X * sensW, prec_chol) - np.dot(mu * sensW, prec_chol)
log_prob[:, k] = np.sum(np.square(y), axis=1)
elif covariance_type == "tied":
log_prob = np.empty((n_samples, n_components))
for k, mu in enumerate(means):
y = np.dot(X * sensW, precisions_chol) - np.dot(
mu * sensW, precisions_chol
)
log_prob[:, k] = np.sum(np.square(y), axis=1)
else:
log_prob = np.empty((n_samples, n_components))
for k, (mu, prec_chol) in enumerate(zip(means, precisions_chol)):
prec_chol_mat = np.eye(n_features) * prec_chol
y = np.dot(X * sensW, prec_chol_mat) - np.dot(mu * sensW, prec_chol_mat)
log_prob[:, k] = np.sum(np.square(y), axis=1)
return -0.5 * (n_features * np.log(2 * np.pi) + log_prob) + log_det
def _estimate_log_prob_with_sensW(self, X, sensW):
"""
[New function, modified from Scikit-Learn.mixture.gaussian_mixture._estimate_log_prob]
"""
return self._estimate_log_gaussian_prob_with_sensW(
X, sensW, self.means_, self.precisions_cholesky_, self.covariance_type
)
def _estimate_weighted_log_prob_with_sensW(self, X, sensW):
"""
[New function, modified from Scikit-Learn.mixture.gaussian_mixture._estimate_weighted_log_prob]
Estimate the weighted log-probabilities, log P(X | Z) + log weights.
Parameters
----------
X : array-like, shape (n_samples, n_features)
Returns
-------
weighted_log_prob : array, shape (n_samples, n_component)
"""
return (
self._estimate_log_prob_with_sensW(X, sensW) + self._estimate_log_weights()
)
def score_samples_with_sensW(self, X, sensW):
"""Compute the weighted log probabilities for each sample.
[New function, modified from Scikit-Learn.mixture.gaussian_mixture.score_samples]
Compute the weighted log probabilities for each sample.
Parameters
----------
X : (n_samples, n_features) array_like
List of n_features-dimensional data points. Each row
corresponds to a single data point.
sensW : (n_samples) array_like
Sensitivity weights
Returns
-------
(n_samples) numpy.array
Log probabilities of each data point in X.
"""
check_is_fitted(self)
# TODO: Ditch self._validate_data when setting sklearn>=1.6 as the minimum
# required version.
X = (
validate_data(self, X, reset=False)
if validate_data is not None
else self._validate_data(X, reset=False)
)
return logsumexp(self._estimate_weighted_log_prob_with_sensW(X, sensW), axis=1)
def plot_pdf(
self,
ax=None,
flag2d=False,
x_component=None,
y_component=None,
padding=0.2,
plotting_precision=100,
plot_membership=False,
contour_opts=None,
level_opts=None,
):
"""
Utils to plot the marginal PDFs of a GMM, either in 1D or 2D (1 or 2 physical properties at the time).
Parameters
----------
ax : matplotlib.Axes, optional
Matplotliv axes object. Need to be a 3-array if flag2d is True
flag2d : bool, default: ``False``
Flag to either plot a 1D or 2D probability distributions
x_component : int, optional
Physical property to plot on the X-axis, as ordered in the GMM.
y_component : int, optional
Physical property to plot on the Y-axis, as ordered in the GMM
padding : float, default: 0.2
How much relative padding around the petrophysical means for the 1D and 2D plots
plotting_precision : int, default: 100
Number of divisions for the 1D and 2D plots
plot_membership : bool, default: ``False``
Plot the membership rather than the probability
contour_opts : dict
Modify the plotting options of the contour plot (in 1D and 2D)
level_opts : dict
Modify the plotting options of the level plot (in 1D and 2D)
Returns
-------
matplotlib.Axes
Axes including the plot
"""
plotting_precision = int(plotting_precision)
if x_component is None:
x_component = 0
if y_component is None:
if flag2d and self.means_.shape[1] > 1:
y_component = 1
if (not (x_component is None)) and (not (y_component is None)):
flag2d = True
if ax is None:
if flag2d:
fig = plt.figure(figsize=(10, 10))
ax0 = plt.subplot2grid((4, 4), (3, 1), colspan=3)
ax1 = plt.subplot2grid((4, 4), (0, 1), colspan=3, rowspan=3)
ax2 = plt.subplot2grid((4, 4), (0, 0), rowspan=3)
ax = [ax0, ax1, ax2]
else:
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax = np.r_[ax]
# deal with the various possible shapes of covariances
if self.covariance_type == "tied":
covariances = np.r_[
[self.covariances_ for i in range(self.n_components)]
].reshape(self.n_components, self.n_features_in_, self.n_features_in_)
elif self.covariance_type == "diag" or self.covariance_type == "spherical":
covariances = np.r_[
[
self.covariances_[i] * np.eye(self.n_features_in_)
for i in range(self.n_components)
]
].reshape(self.n_components, self.n_features_in_, self.n_features_in_)
else:
covariances = self.covariances_
dx = padding * (
self.means_[:, x_component].max() - self.means_[:, x_component].min()
)
xmin, xmax = (
self.means_[:, x_component].min() - dx,
self.means_[:, x_component].max() + dx,
)
# create a sklearn.clustering.GaussianMixture for plotting (no influence from mesh and local weights)
meansx = self.means_[:, x_component].reshape(self.n_components, 1)
covx = covariances[:, [x_component]][:, :, [x_component]]
if len(self.weights_.shape) == 2:
weights = self.weights_.sum(axis=0)
weights /= weights.sum()
else:
weights = self.weights_
clfx = GaussianMixture(
n_components=self.n_components,
means_init=meansx,
# limit computation to minimum as we set the model parameter a posteriori
n_init=1,
max_iter=2,
# put a high tolerance to avoid warning about low model fit
tol=1e256,
)
# random fit, we set values after.
clfx.fit(np.random.randn(10, 1))
clfx.means_ = meansx
clfx.covariances_ = covx
clfx.precisions_cholesky_ = _compute_precision_cholesky(
clfx.covariances_, clfx.covariance_type
)
clfx.weights_ = weights
xplot = np.linspace(xmin, xmax, plotting_precision)[:, np.newaxis]
if plot_membership:
rvx = clfx.predict(xplot)
labelx = "membership"
else:
rvx = np.exp(clfx.score_samples(xplot))
labelx = "1D Probability\nDensity\nDistribution"
ax[0].set_xlim(xmin, xmax)
ax[0].plot(
xplot,
rvx,
linewidth=3.0,
label=labelx,
c="k",
)
ax[0].legend()
ax[0].set_xlabel("Physical property {}".format(x_component))
ax[0].set_ylabel("Probability Density values")
if flag2d:
dy = padding * (
self.means_[:, y_component].max() - self.means_[:, y_component].min()
)
ymin, ymax = (
self.means_[:, y_component].min() - dy,
self.means_[:, y_component].max() + dy,
)
# create a sklearn.clustering.GaussianMixture for plotting (no influence from mesh and local weights)
meansy = self.means_[:, y_component].reshape(self.n_components, 1)
covy = covariances[:, [y_component]][:, :, [y_component]]
clfy = GaussianMixture(
n_components=self.n_components,
means_init=meansy,
# limit computation to minimum as we set the model parameter a posteriori
n_init=1,
max_iter=2,
# put a high tolerance to avoid warning about low model fit
tol=1e256,
)
# random fit, we set values after.
clfy.fit(np.random.randn(10, 1))
clfy.means_ = meansy
clfy.covariances_ = covy
clfy.precisions_cholesky_ = _compute_precision_cholesky(
clfy.covariances_, clfy.covariance_type
)
clfy.weights_ = weights
# 1d y-plot
yplot = np.linspace(ymin, ymax, plotting_precision)[:, np.newaxis]
if plot_membership:
rvy = clfy.predict(yplot)
labely = "membership"
else:
rvy = np.exp(clfy.score_samples(yplot))
labely = "1D Probability\nDensity\nDistribution"
ax[2].plot(rvy, yplot, linewidth=3.0, c="k", label=labely)
ax[2].set_ylabel("Physical property {}".format(y_component))
ax[2].set_ylim(ymin, ymax)
ax[2].legend()
# 2d plot
mean2d = self.means_[:, [x_component, y_component]]
cov2d = covariances[:, [x_component, y_component]][
:, :, [x_component, y_component]
]
clf2d = GaussianMixture(
n_components=self.n_components,
means_init=mean2d,
n_init=1,
max_iter=2,
tol=1e256,
)
# random fit, we set values after.
clf2d.fit(np.random.randn(10, 2))
clf2d.means_ = mean2d
clf2d.covariances_ = cov2d
clf2d.precisions_cholesky_ = _compute_precision_cholesky(
clf2d.covariances_, clf2d.covariance_type
)
clf2d.weights_ = weights
x, y = np.mgrid[
xmin : xmax : (xmax - xmin) / plotting_precision,
ymin : ymax : (ymax - ymin) / plotting_precision,
]
pos = np.empty(x.shape + (2,))
pos[:, :, 0] = x
pos[:, :, 1] = y
if plot_membership:
rv2d = clf2d.predict(pos.reshape(-1, 2))
labely = "membership"
else:
rv2d = clf2d.score_samples(pos.reshape(-1, 2))
labely = "2D Probability Density Distribution"
if contour_opts is None:
contour_opts = {}
contour_opts = {"levels": 10, "cmap": "viridis", **contour_opts}
surf = ax[1].contourf(x, y, rv2d.reshape(x.shape), **contour_opts)
if level_opts is None:
level_opts = {}
level_opts = {
"levels": 10,
"colors": "k",
"linewidths": 1.0,
"linestyles": "dashdot",
**level_opts,
}
ax[1].contour(x, y, rv2d.reshape(x.shape), **level_opts)
ax[1].scatter(
meansx,
meansy,
label="Petrophysical means",
cmap="inferno_r",
c=np.linspace(0, self.n_components, self.n_components),
marker="v",
edgecolors="k",
)
axbar = inset_axes(
ax[1],
width="40%",
height="3%",
loc="upper right",
borderpad=1,
)
cbpetro = plt.colorbar(surf, cax=axbar, orientation="horizontal")
cbpetro.set_ticks([rv2d.min(), rv2d.max()])
cbpetro.set_ticklabels(["Low", "High"])
cbpetro.set_label(labely)
cbpetro.outline.set_edgecolor("k")
ax[1].set_xlim(xmin, xmax)
ax[1].set_ylim(ymin, ymax)
ax[1].legend(loc=3)
ax[1].set_ylabel("")
ax[1].set_xlabel("")
return ax
class GaussianMixtureWithPrior(WeightedGaussianMixture):
"""
This class built upon the :class:`~simpeg.utils.WeightedGaussianMixture`, which
itself built upon from the :clas::`sklearn.mixture.GaussianMixture` class from
scikit-learn.
In addition to weights samples/observations by the cells volume of the mesh, this
class uses a posterior approach to fit the GMM parameters. This means it takes prior
parameters, passed through :class:`~simpeg.utils.WeightedGaussianMixture`
``gmmref``.
The prior distribution for each parameters (proportions, means, covariances) is
defined through a conjugate or semi-conjugate approach (prior_type), to the choice
of the user. See Astic & Oldenburg 2019: A framework for petrophysically and
geologically guided geophysical inversion (https://doi.org/10.1093/gji/ggz389) for
more information.
.. attention::
This class built upon the :class:`sklearn.mixture.GaussianMixture` class from
scikit-learn. New functionalities are added, as well as modifications to
existing functions, to serve the purposes pursued within SimPEG. This use is
allowed by the scikit-learn licensing (BSD-3-Clause License) and we are grateful
for their contributions to the open-source community.
There are some additional parameters to provide to this class, compared to
:class:`~simpeg.utils.WeightedGaussianMixture`.
Parameters
----------
kappa : array
Strength of the confidence in the prior means
nu : array
Strength of the confidence in the prior covariances
zeta : array
Strength of the confidence in the prior proportions
prior_type : str
Choose from one of the following:
- "semi": semi-conjugate prior, the means and covariances priors are independent
- "full": conjugate prior, the means and covariances priors are inter-dependent
update_covariances : bool
Choose from two options:
- ``True``: semi or conjugate prior by averaging the covariances
- ``False``: alternative (not conjugate) prior: average the precisions instead
fixed_membership : array of int, optional
A 2D :class:`numpy.ndarray` to fix the membership to a chosen lithology of
particular cells.
The first column contains the numeric index of the cells, the second column the
respective lithology index.
Shape is ``(index of the fixed cell, lithology index)``.
"""
@requires({"sklearn": sklearn})
def __init__(
self,
gmmref,
kappa=0.0,
nu=0.0,
zeta=0.0,
prior_type="semi", # semi or full
update_covariances=True,
fixed_membership=None,
init_params="kmeans",
max_iter=100,
means_init=None,
n_init=10,
precisions_init=None,
random_state=None,
reg_covar=1e-06,
tol=0.001,
verbose=0,
verbose_interval=10,
warm_start=False,
weights_init=None,
# **kwargs
):
self.mesh = gmmref.mesh
self.n_components = gmmref.n_components
self.gmmref = gmmref
self.covariance_type = gmmref.covariance_type
self.kappa = kappa * np.ones((self.n_components, gmmref.means_.shape[1]))
self.nu = nu * np.ones(self.n_components)
self.zeta = zeta * np.ones_like(self.gmmref.weights_)
self.prior_type = prior_type
self.update_covariances = update_covariances
self.fixed_membership = fixed_membership
super(GaussianMixtureWithPrior, self).__init__(
covariance_type=self.covariance_type,
mesh=self.mesh,
actv=self.gmmref.actv,
init_params=init_params,
max_iter=max_iter,
means_init=means_init,
n_components=self.n_components,
n_init=n_init,
precisions_init=precisions_init,
random_state=random_state,
reg_covar=reg_covar,
tol=tol,
verbose=verbose,
verbose_interval=verbose_interval,
warm_start=warm_start,
weights_init=weights_init,
# **kwargs
)
# set_kwargs(self, **kwargs)
def order_cluster(self, outputindex=False):
"""Order cluster
Arrange the clusters of gmm in the same order as those of gmmref,
based on their relative similarities and priorizing first the most proeminent
clusters (highest proportions)
Parameters
----------
outputindex : bool, default: ``False``
If ``True``, return the ordering index for the clusters of GMM.
Returns
-------
numpy.ndarray of int
Returns the ordering index for the clusters of GMM if
*outputindex* is ``True``
"""
self.order_clusters_GM_weight()
idx_ref = np.ones(len(self.gmmref.means_), dtype=bool)
indx = []
for i in range(self.n_components):
dis = self._estimate_log_prob(
self.gmmref.means_[idx_ref].reshape(
[-1] + [d for d in self.gmmref.means_.shape[1:]]
)
)
id_dis = dis.argmax(axis=0)[i]
idrefmean = np.where(
np.all(
self.gmmref.means_ == self.gmmref.means_[idx_ref][id_dis], axis=1
)
)[0][0]
indx.append(idrefmean)
idx_ref[idrefmean] = False