-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_without_prior_gmm_SalinasA_mod.py
1360 lines (1159 loc) · 68 KB
/
run_without_prior_gmm_SalinasA_mod.py
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 os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import arviz as az
import pandas as pd
from datetime import datetime
import json
import argparse
import torch
import torch.nn.functional as F
import pyro
import pyro.distributions as dist
from pyro.infer import MCMC, NUTS, Predictive, EmpiricalMarginal
from pyro.infer.autoguide import init_to_mean, init_to_median, init_to_value
from pyro.infer.inspect import get_dependencies
from pyro.infer import SVI, TraceEnum_ELBO, config_enumerate, infer_discrete
import gempy as gp
import gempy_engine
import gempy_viewer as gpv
from gempy_engine.core.backend_tensor import BackendTensor
from gempy_probability.plot_posterior import default_red, default_blue, PlotPosterior
import scipy.io
from scipy.stats import zscore
from sklearn.manifold import TSNE
from sklearn.mixture import GaussianMixture
from sklearn.mixture import BayesianGaussianMixture
from sklearn.cluster import KMeans
parser = argparse.ArgumentParser(description='pass values using command line')
parser.add_argument('--startval', metavar='startcol', type=int, default=19, help='start x column value')
parser.add_argument('--endval', metavar='endcol', type=int, default=21, help='end x column value')
parser.add_argument('--cluster', metavar='cluster', type=int, default=7, help='total number of cluster')
parser.add_argument('--dimred', metavar='dimred', type=str , default="pca", help='type of dimensionality reduction')
parser.add_argument('--plot_dimred', metavar='plot_dimred', type=str , default="tsne", help='type of dimensionality reduction for plotting after data is alread reduced in a smaller dimension')
parser.add_argument('--prior_number_samples', metavar='prior_number_samples', type=int , default=100, help='number of samples for prior')
parser.add_argument('--posterior_number_samples', metavar='posterior_number_samples', type=int , default=150, help='number of samples for posterior')
parser.add_argument('--posterior_warmup_steps', metavar='posterior_warmup_steps', type=int , default=50, help='number of warmup steps for posterior')
parser.add_argument('--directory_path', metavar='directory_path', type=str , default="./Results_without_prior_gmm_SalinasA_mod", help='name of the directory in which result should be stored')
def cluster_acc(Y_pred, Y, ignore_label=None):
""" Rearranging the class labels of prediction so that it maximise the
match class labels.
Args:
Y_pred (int): An array for predicted labels
Y (float): An array for true labels
ignore_label (int, optional): Laels to be ignored
Returns:
row (int): A list of index of row
column (int) : A list of index of column
accuracy (float): accuracy after we found correct label
cost_matrix (int) : cost matrix
"""
if ignore_label is not None:
index = Y!= ignore_label
Y=Y[index]
Y_pred=Y_pred[index]
from scipy.optimize import linear_sum_assignment as linear_assignment
assert Y_pred.shape == Y.shape
D = int((max(Y_pred.max(), Y.max())).item())
w = torch.zeros((D, D))
for i in range(Y_pred.shape[0]):
w[int(Y_pred[i].item())-1, int(Y[i].item())-1] += 1
ind = linear_assignment(w.max() - w)
return ind[0], ind[1], (w[ind[0], ind[1]]).sum() / Y_pred.shape[0], w
def TSNE_transformation(data, label, filename):
""" This function applies TSNE algorithms to reduce the high dimensional data into 2D
for better visualization
Args:
data (float): High dimensional Input data
label (int): Label information of each data entry
filename (str): Location to store the image after dimensionality reduction
"""
from sklearn.manifold import TSNE
model = TSNE(n_components=2, random_state=42)
transformed_data = model.fit_transform(data)
label_to_color = { 1: 'red', 2: 'blue', 3: 'green', 4: 'yellow', 5: 'orange', 6: 'purple',7:'pink'}
plt.figure(figsize=(10,8))
for label_ in np.unique(label):
idx =label ==label_
plt.scatter(transformed_data[idx][:,0],transformed_data[idx][:,1], c=label_to_color[label_],label=f' {label_}',s=50, marker='o',alpha=1.0, edgecolors='w')
# Create a legend
plt.legend()
# Add axis labels
plt.xlabel('Component 1')
plt.ylabel('Component 2')
plt.title("Data after dimensionality reduction")
plt.savefig(filename)
def create_initial_gempy_model(refinement,filename, save=True):
""" Create an initial gempy model objet
Args:
refinement (int): Refinement of grid
save (bool, optional): Whether you want to save the image
"""
geo_model_test = gp.create_geomodel(
project_name='Gempy_abc_Test',
extent=[0, 86, -10, 10, -83, 0],
resolution=[86,20,83],
refinement=refinement,
structural_frame= gp.data.StructuralFrame.initialize_default_structure()
)
gp.add_surface_points(
geo_model=geo_model_test,
x=[70.0, 80.0],
y=[0.0, 0.0],
z=[-77.0, -71.0],
elements_names=['surface1', 'surface1']
)
gp.add_orientations(
geo_model=geo_model_test,
x=[75],
y=[0.0],
z=[-74],
elements_names=['surface1'],
pole_vector=[[-5/3, 0, 1]]
)
geo_model_test.update_transform(gp.data.GlobalAnisotropy.NONE)
element2 = gp.data.StructuralElement(
name='surface2',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([40.0, 60.0]),
y=np.array([0.0, 0.0]),
z=np.array([-77, -65]),
names='surface2'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element2)
element3 = gp.data.StructuralElement(
name='surface3',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([20.0, 60.0]),
y=np.array([0.0, 0.0]),
z=np.array([-74, -52]),
names='surface3'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element3)
element4 = gp.data.StructuralElement(
name='surface4',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([0.0, 30.0, 60]),
y=np.array([0.0, 0.0,0.0]),
z=np.array([-72, -55.5, -39]),
names='surface4'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element4)
element5 = gp.data.StructuralElement(
name='surface5',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([0.0, 20.0, 60]),
y=np.array([0.0, 0.0,0.0]),
z=np.array([-61, -49, -27]),
names='surface5'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element5)
element6 = gp.data.StructuralElement(
name='surface6',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([0.0, 20.0, 40]),
y=np.array([0.0, 0.0, 0.0]),
z=np.array([-39, -28, -16]),
names='surface6'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element6)
element7 = gp.data.StructuralElement(
name='surface7',
color=next(geo_model_test.structural_frame.color_generator),
surface_points=gp.data.SurfacePointsTable.from_arrays(
x=np.array([0.0, 20.0,30]),
y=np.array([0.0, 0.0, 0.0]),
z=np.array([-21, -10, -1]),
names='surface7'
),
orientations=gp.data.OrientationsTable.initialize_empty()
)
geo_model_test.structural_frame.structural_groups[0].append_element(element7)
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[2]
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[2]=\
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[3]
geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[3]=\
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[4]
geo_model_test.structural_frame.structural_groups[0].elements[5], geo_model_test.structural_frame.structural_groups[0].elements[4]=\
geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[5]
geo_model_test.structural_frame.structural_groups[0].elements[6], geo_model_test.structural_frame.structural_groups[0].elements[5]=\
geo_model_test.structural_frame.structural_groups[0].elements[5], geo_model_test.structural_frame.structural_groups[0].elements[6]
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[2]
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[2]=\
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[3]
geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[3]=\
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[4]
geo_model_test.structural_frame.structural_groups[0].elements[5], geo_model_test.structural_frame.structural_groups[0].elements[4]=\
geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[5]
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[2]
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[2]=\
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[3]
geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[3]=\
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[4]
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[2]
geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[2]=\
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[3]
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[2]
geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1]=\
geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0]
gp.compute_model(geo_model_test)
picture_test = gpv.plot_2d(geo_model_test, cell_number=5, legend='force')
if save:
plt.savefig(filename)
return geo_model_test
def main():
"""
This function defines a model which uses hyperspectral data, applies clustering methods to find cluster information and then uses Bayesian
"""
args = parser.parse_args()
startval=args.startval
endval=args.endval
cluster = args.cluster
dimred=args.dimred
plot_dimred=args.plot_dimred
prior_number_samples = args.prior_number_samples
posterior_number_samples = args.posterior_number_samples
posterior_warmup_steps = args.posterior_warmup_steps
directory_path = args.directory_path
# Check if the directory exists
if not os.path.exists(directory_path):
# Create the directory if it does not exist
os.makedirs(directory_path)
print(f"Directory '{directory_path}' was created.")
else:
print(f"Directory '{directory_path}' already exists.")
# Load .mat file
SalinasA= np.array(scipy.io.loadmat('./HSI_Salinas/SalinasA.mat')['salinasA'])
SalinasA_corrected= np.array(scipy.io.loadmat('./HSI_Salinas/SalinasA_corrected.mat')['salinasA_corrected'])
SalinasA_gt= np.array(scipy.io.loadmat('./HSI_Salinas/SalinasA_gt.mat')['salinasA_gt'])
SalinasA_gt_mod= np.array(scipy.io.loadmat('./HSI_Salinas/SalinasA_gt_mod.mat')['salinasA_gt_mod'])
# # Arrange the label in groundtruth
# i=0
# label_data = [0,6,1,5,4,3,2]
# for ele in np.unique(SalinasA_gt):
# mask = SalinasA_gt==ele
# SalinasA_gt[mask] = label_data[i]
# i=i+1
# SalinasA_gt = 7 - SalinasA_gt
SalinasA_gt_mod = 9 - SalinasA_gt_mod
ground_truth = SalinasA_gt_mod
no_label = 8
######################################################################
## Arrange Data as concatationation of spacial co-ordinate and pixel values
###########################################################################
H, W = ground_truth.shape # get the shape of groud truth
n_features = SalinasA_corrected.shape[2]+4 # get the number of features including co-ordinates and label
# Create a dataset which has "X","Y","Z", "Label", and spectral channel information
data_hsi = torch.zeros((H*W, n_features ))
for i in range(H):
for j in range(W):
data_hsi[i*W+j,0] = j
data_hsi[i*W +j,2] = - i
data_hsi[i*W +j,3] = ground_truth[i,j]
data_hsi[i*W +j,4:] = torch.tensor(SalinasA_corrected[i,j,:])
# Create a list of column name
column_name=["X","Y","Z", "Label"]
for i in range(SalinasA_corrected.shape[2]):
column_name.append("feature_"+str(i+1))
# Create a pandas dataframe to store the database
df_hsi = pd.DataFrame(data_hsi,columns=column_name)
# Create a database by removing the non labelled pixel information
df_with_labelled_pixel = df_hsi.loc[(df_hsi['Label']!=no_label)]
# Normalise along the spectral lines
df_with_spectral_normalised = df_with_labelled_pixel.copy()
df_with_spectral_normalised.iloc[:, 4:] = df_with_spectral_normalised.iloc[:, 4:].apply(zscore,axis=1)
# column = 20
# y_obs = torch.tensor(SalinasA_gt[:,column], dtype=torch.float64)
# mask = y_obs!=0
# y_obs_label = y_obs[mask]
# y_obs_hsi = torch.tensor(SalinasA_corrected[:,column,:], dtype=torch.float64)[mask]
# normalised_hsi = zscore(y_obs_hsi, axis=1)
###########################################################################
## Obtain the preprocessed data
###########################################################################
normalised_data_1 = df_with_spectral_normalised.loc[(df_with_spectral_normalised["X"]>=startval)&(df_with_spectral_normalised["X"]<=endval)]
normalised_data_2 = df_with_spectral_normalised.loc[(df_with_spectral_normalised["X"]>=59)&(df_with_spectral_normalised["X"]<=61)]
normalised_data = pd.concat([normalised_data_1, normalised_data_2], ignore_index=True)
normalised_hsi =torch.tensor(normalised_data.iloc[:,4:].to_numpy(), dtype=torch.float64)
## It is difficult to work with data in such a high dimensions, because the covariance matrix
## determinant quickly goes to zero even if eigen-values are in the range of 1e-3. Therefore it is advisable
## to fist apply dimensionality reduction to a lower dimensions
if dimred=="pca":
from sklearn.decomposition import PCA
pca = PCA(n_components=10)
transformed_hsi = pca.fit_transform(normalised_hsi)
normalised_hsi = torch.tensor(transformed_hsi, dtype=torch.float64)
y_obs_label = torch.tensor(normalised_data.iloc[:,3].to_numpy(), dtype=torch.float64)
if dimred =="tsne":
#######################TODO#####################
################################################
print("TSNE hasn't implemented for dimensionality reduction yet")
exit()
###########################################################################
## Apply Classical clustering methods to find different cluster information our data
###########################################################################
gm = BayesianGaussianMixture(n_components=cluster, random_state=42, reg_covar=1e-6 ).fit(normalised_hsi)
# make the labels to start with 1 instead of 0
gmm_label = gm.predict(normalised_hsi) +1
gmm_label_order, y_obs_label_order, accuracy_init, _ = cluster_acc( gmm_label, y_obs_label)
# reaarange the label information so it is would be consistent with ground truth label
gmm_label_rearranged = torch.tensor([y_obs_label_order[x-1] +1 for x in gmm_label], dtype=torch.float64)
#print(gmm_label_rearranged - y_obs_label)
# gmm_label2 = torch.zeros_like(y_obs_label)
# gmm_label2[gmm_label==2]=6
# gmm_label2[gmm_label==4]=5
# gmm_label2[gmm_label==1]=4
# gmm_label2[gmm_label==3]=3
# gmm_label2[gmm_label==6]=2
# gmm_label2[gmm_label==5]=1
# rearrange the mean and covariance accordingly too
#rearrange_list = [4,5,2,0,3,1]
#rearrange_list = [3,4,2,0,5,1]
rearrange_list = y_obs_label_order
mean_init, cov_init = gm.means_[rearrange_list], gm.covariances_[rearrange_list]
####################################TODO#################################################
# Try to find the initial accuracy of classification
#########################################################################################
print("Intial accuracy\n", accuracy_init)
#################################TODO##################################################
## Apply different dimentionality reduction techniques and save the plot in Result file
#######################################################################################
if plot_dimred =="tsne":
filename_tsne = directory_path + "/tsne_gmm_label.png"
TSNE_transformation(data=normalised_data, label=gmm_label_rearranged, filename=filename_tsne)
######################################################################################
## Apply Classical clustering methods to find different cluster information our data
######################################################################################
# geo_model_test = gp.create_geomodel(
# project_name='Gempy_abc_Test',
# extent=[0, 86, -10, 10, -83, 0],
# resolution=[86,20,83],
# refinement=3,
# structural_frame= gp.data.StructuralFrame.initialize_default_structure()
# )
# gp.add_surface_points(
# geo_model=geo_model_test,
# x=[70.0, 80.0],
# y=[0.0, 0.0],
# z=[-77.0, -71.0],
# elements_names=['surface1', 'surface1']
# )
# gp.add_orientations(
# geo_model=geo_model_test,
# x=[75],
# y=[0.0],
# z=[-74],
# elements_names=['surface1'],
# pole_vector=[[-5/3, 0, 1]]
# )
# geo_model_test.update_transform(gp.data.GlobalAnisotropy.NONE)
# element2 = gp.data.StructuralElement(
# name='surface2',
# color=next(geo_model_test.structural_frame.color_generator),
# surface_points=gp.data.SurfacePointsTable.from_arrays(
# x=np.array([20.0, 60.0]),
# y=np.array([0.0, 0.0]),
# z=np.array([-74, -52]),
# names='surface2'
# ),
# orientations=gp.data.OrientationsTable.initialize_empty()
# )
# geo_model_test.structural_frame.structural_groups[0].append_element(element2)
# element3 = gp.data.StructuralElement(
# name='surface3',
# color=next(geo_model_test.structural_frame.color_generator),
# surface_points=gp.data.SurfacePointsTable.from_arrays(
# x=np.array([0.0, 30.0, 60]),
# y=np.array([0.0, 0.0,0.0]),
# z=np.array([-72, -55.5, -39]),
# names='surface3'
# ),
# orientations=gp.data.OrientationsTable.initialize_empty()
# )
# geo_model_test.structural_frame.structural_groups[0].append_element(element3)
# element4 = gp.data.StructuralElement(
# name='surface4',
# color=next(geo_model_test.structural_frame.color_generator),
# surface_points=gp.data.SurfacePointsTable.from_arrays(
# x=np.array([0.0, 20.0, 60]),
# y=np.array([0.0, 0.0,0.0]),
# z=np.array([-61, -49, -27]),
# names='surface4'
# ),
# orientations=gp.data.OrientationsTable.initialize_empty()
# )
# geo_model_test.structural_frame.structural_groups[0].append_element(element4)
# element5 = gp.data.StructuralElement(
# name='surface5',
# color=next(geo_model_test.structural_frame.color_generator),
# surface_points=gp.data.SurfacePointsTable.from_arrays(
# x=np.array([0.0, 20.0, 40]),
# y=np.array([0.0, 0.0, 0.0]),
# z=np.array([-39, -28, -16]),
# names='surface5'
# ),
# orientations=gp.data.OrientationsTable.initialize_empty()
# )
# geo_model_test.structural_frame.structural_groups[0].append_element(element5)
# element6 = gp.data.StructuralElement(
# name='surface6',
# color=next(geo_model_test.structural_frame.color_generator),
# surface_points=gp.data.SurfacePointsTable.from_arrays(
# x=np.array([0.0, 20.0,30]),
# y=np.array([0.0, 0.0, 0.0]),
# z=np.array([-21, -10, -1]),
# names='surface6'
# ),
# orientations=gp.data.OrientationsTable.initialize_empty()
# )
# geo_model_test.structural_frame.structural_groups[0].append_element(element6)
# geo_model_test.structural_frame.structural_groups[0].elements[0], geo_model_test.structural_frame.structural_groups[0].elements[1],\
# geo_model_test.structural_frame.structural_groups[0].elements[2], geo_model_test.structural_frame.structural_groups[0].elements[3],\
# geo_model_test.structural_frame.structural_groups[0].elements[4], geo_model_test.structural_frame.structural_groups[0].elements[5] = \
# geo_model_test.structural_frame.structural_groups[0].elements[1], geo_model_test.structural_frame.structural_groups[0].elements[0],\
# geo_model_test.structural_frame.structural_groups[0].elements[3], geo_model_test.structural_frame.structural_groups[0].elements[2],\
# geo_model_test.structural_frame.structural_groups[0].elements[5], geo_model_test.structural_frame.structural_groups[0].elements[4]
# gp.compute_model(geo_model_test)
# picture_test = gpv.plot_2d(geo_model_test, cell_number=5, legend='force')
# plt.savefig("./Results_without_prior_gmm/Prior_model.png")
# Create initial model with higher refinement for better resolution and save it
prior_filename= directory_path + "/prior_model.png"
geo_model_test = create_initial_gempy_model(refinement=7,filename=prior_filename, save=True)
# We can initialize again but with lower refinement because gempy solution are inddependent
geo_model_test = create_initial_gempy_model(refinement=3,filename=prior_filename, save=False)
# Label information need to be in same order as it is created in gempy model
#y_obs_label = 7 - y_obs_label
################################################################################
# Custom grid
################################################################################
# x_loc = 20
# y_loc = 0
# z_loc = np.linspace(0,-82, 83)
# xyz_coord = np.array([[x_loc, y_loc, z] for z in z_loc])[mask]
xyz_coord = normalised_data.iloc[:,:3].to_numpy()
gp.set_custom_grid(geo_model_test.grid, xyz_coord=xyz_coord)
################################################################################
geo_model_test.interpolation_options.mesh_extraction = False
sol = gp.compute_model(geo_model_test)
sp_coords_copy_test = geo_model_test.interpolation_input.surface_points.sp_coords.copy()
geo_model_test.transform.apply_inverse(sp_coords_copy_test)
gp.compute_model(geo_model_test)
sp_coords_copy_test = geo_model_test.interpolation_input.surface_points.sp_coords.copy()
################################################################################
# Store the Initial Interface data and orientation data
################################################################################
df_sp_init = geo_model_test.surface_points.df
df_or_init = geo_model_test.orientations.df
filename_initial_sp = directory_path + "/Initial_sp.csv"
filename_initial_op = directory_path + "/Initial_op.csv"
df_sp_init.to_csv(filename_initial_sp)
df_or_init.to_csv(filename_initial_op)
################################################################################
geo_model_test.transform.apply_inverse(sp_coords_copy_test)
# Change the backend to PyTorch for probabilistic modeling
BackendTensor.change_backend_gempy(engine_backend=gp.data.AvailableBackends.PYTORCH)
# geo_model_test.interpolation_options.uni_degree = 0
# geo_model_test.interpolation_options.mesh_extraction = False
geo_model_test.interpolation_options.sigmoid_slope = 40
store_accuracy=[]
@config_enumerate
def model_test(obs_data):
"""
This Pyro model represents the probabilistic aspects of the geological model.
It defines a prior distribution for the top layer's location and
computes the thickness of the geological layer as an observed variable.
"""
# Define prior for the top layer's location
prior_mean_surface_1 = sp_coords_copy_test[1, 2]
prior_mean_surface_2 = sp_coords_copy_test[4, 2]
prior_mean_surface_3 = sp_coords_copy_test[7, 2]
prior_mean_surface_4 = sp_coords_copy_test[12, 2]
prior_mean_surface_5 = sp_coords_copy_test[8, 2]
prior_mean_surface_6 = sp_coords_copy_test[11, 2]
prior_mean_surface_7 = sp_coords_copy_test[13, 2]
prior_mean_surface_8 = sp_coords_copy_test[15, 2]
mu_surface_1 = pyro.sample('mu_1', dist.Normal(prior_mean_surface_1, torch.tensor(0.2, dtype=torch.float64)))
mu_surface_2 = pyro.sample('mu_2', dist.Normal(prior_mean_surface_2, torch.tensor(0.2, dtype=torch.float64)))
mu_surface_3 = pyro.sample('mu_3', dist.Normal(prior_mean_surface_3, torch.tensor(0.2, dtype=torch.float64)))
mu_surface_4 = pyro.sample('mu_4', dist.Normal(prior_mean_surface_4, torch.tensor(0.2, dtype=torch.float64)))
mu_surface_5 = pyro.sample('mu_5', dist.Normal(prior_mean_surface_5, torch.tensor(0.2, dtype=torch.float64)))
mu_surface_6 = pyro.sample('mu_6', dist.Normal(prior_mean_surface_6 , torch.tensor(0.2, dtype=torch.float64)))
mu_surface_7 = pyro.sample('mu_7', dist.Normal(prior_mean_surface_7 , torch.tensor(0.2, dtype=torch.float64)))
mu_surface_8 = pyro.sample('mu_8', dist.Normal(prior_mean_surface_8, torch.tensor(0.2, dtype=torch.float64)))
pyro.sample('mu_1 < 0', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_1 < 3.4))
pyro.sample('mu_1 > mu_2', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_1 > mu_surface_2))
pyro.sample('mu_2 > mu_3', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_2 > mu_surface_3))
pyro.sample('mu_3 > mu_4', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_3 > mu_surface_4))
#pyro.sample('mu_3 > -61.5', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_3 > 0.625))
#pyro.sample('mu_4 < -61.5', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_4 < 0.625))
pyro.sample('mu_4 > -83', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_4 > - 0.2 ))
pyro.sample('mu_5 < 0', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_5 < 2.5))
pyro.sample('mu_5 > mu_6', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_5 > mu_surface_6))
pyro.sample('mu_6 > mu_7', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_6 > mu_surface_7))
pyro.sample('mu_7 > mu_8', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_7 > mu_surface_8))
#pyro.sample('mu_3 > -61.5', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_3 > 0.625))
#pyro.sample('mu_4 < -61.5', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_4 < 0.625))
pyro.sample('mu_8 > -83', dist.Delta(torch.tensor(1.0, dtype=torch.float64)), obs=(mu_surface_4 > -0.1 ))
# Update the model with the new top layer's location
interpolation_input = geo_model_test.interpolation_input
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([11]), torch.tensor([2])),
mu_surface_1
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([14]), torch.tensor([2])),
mu_surface_2
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([5]), torch.tensor([2])),
mu_surface_3
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([0]), torch.tensor([2])),
mu_surface_4
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([8]), torch.tensor([2])),
mu_surface_5
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([11]), torch.tensor([2])),
mu_surface_6
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([13]), torch.tensor([2])),
mu_surface_7
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([15]), torch.tensor([2])),
mu_surface_8
)
#print("interpolation_input",interpolation_input.surface_points.sp_coords)
# # Compute the geological model
geo_model_test.solutions = gempy_engine.compute_model(
interpolation_input=interpolation_input,
options=geo_model_test.interpolation_options,
data_descriptor=geo_model_test.input_data_descriptor,
geophysics_input=geo_model_test.geophysics_input,
)
# Compute and observe the thickness of the geological layer
custom_grid_values = geo_model_test.solutions.octrees_output[0].last_output_center.custom_grid_values
# accuracy_intermediate = torch.sum(torch.round(custom_grid_values) == y_obs_label) / y_obs_label.shape[0]
# store_accuracy.append(accuracy_intermediate)
lambda_ = 15.0
# loc_mean = torch.tensor(mean_init,dtype=torch.float64)
# loc_cov = torch.tensor(cov_init, dtype=torch.float64)
z_nk = F.softmax(-lambda_* (torch.linspace(1,cluster,cluster, dtype=torch.float64) - custom_grid_values.reshape(-1,1))**2, dim=1)
#class_label = torch.mean(F.softmax(-lambda_* (torch.tensor([1,2,3,4,5,6], dtype=torch.float64) - custom_grid_values.reshape(-1,1))**2, dim=1),dim=0)
N_k = torch.sum(z_nk,axis=0)
N = len(custom_grid_values)
pi_k = N_k /N
mean = []
cov = []
for i in range(z_nk.shape[1]):
mean_k = torch.sum( z_nk[:,i][:,None] * obs_data, axis=0)/ N_k[i]
#cov_k = torch.sum( (normalised_hsi - mean_k.reshape((-1,1))) (normalised_hsi - mean_k).T )
cov_k = torch.zeros((mean_k.shape[0],mean_k.shape[0]),dtype=torch.float64)
for j in range(z_nk.shape[0]):
cov_k += z_nk[j,i]* torch.matmul((obs_data[j,:] - mean_k).reshape((-1,1)) ,(obs_data[j,:] - mean_k).reshape((1,-1)))
mean.append(mean_k)
cov_k=cov_k/N_k[i] #+ 1e-3 * torch.diag(torch.ones(cov_k.shape[0],dtype=torch.float64))
cov.append(cov_k)
mean_tensor = torch.stack(mean, dim=0)
cov_tensor = torch.stack(cov,dim=0)
#cov_likelihood = 5.0 * torch.eye(loc_cov[0].shape[0], dtype=torch.float64)
with pyro.plate('N='+str(obs_data.shape[0]), obs_data.shape[0]):
assignment = pyro.sample("assignment", dist.Categorical(pi_k))
#print(obs_data.shape, mean_tensor[assignment].shape,cov_tensor[assignment].shape)
obs = pyro.sample("obs", dist.MultivariateNormal(loc=mean_tensor[assignment],covariance_matrix = cov_tensor[assignment]), obs=obs_data)
filename_bayes_graph = directory_path + "/Bayesian_graph.png"
dot = pyro.render_model(model_test, model_args=(normalised_hsi,),render_distributions=True,filename=filename_bayes_graph)
################################################################################
# Prior
################################################################################
pyro.set_rng_seed(42)
prior = Predictive(model_test, num_samples=prior_number_samples)(normalised_hsi)
# Key to avoid
#avoid_key = ['mu_1 < 0','mu_1 > mu_2','mu_2 > -38.5', 'mu_3 < -38.5','mu_3 > -61.4','mu_4 < -61.5', 'mu_4 > -83']
#avoid_key = ['mu_1 < 0','mu_1 > mu_2','mu_2 > mu_3', 'mu_3 > mu_4' , 'mu_4 > -83']
avoid_key = ['mu_1 < 0','mu_1 > mu_2','mu_2 > mu_3', 'mu_3 > mu_4' , 'mu_4 > -83','mu_5 < 0','mu_5 > mu_6','mu_6 > mu_7', 'mu_7 > mu_8' , 'mu_8 > -83' ]
# Create sub-dictionary without the avoid_key
prior = dict((key, value) for key, value in prior.items() if key not in avoid_key)
plt.figure(figsize=(8,10))
data = az.from_pyro(prior=prior)
az.plot_trace(data.prior)
filename_prior_plot = directory_path + "/prior.png"
plt.savefig(filename_prior_plot)
################################################################################
# Posterior
################################################################################
pyro.primitives.enable_validation(is_validate=True)
nuts_kernel = NUTS(model_test, step_size=0.0085, adapt_step_size=True, target_accept_prob=0.9, max_tree_depth=10, init_strategy=init_to_mean)
mcmc = MCMC(nuts_kernel, num_samples=posterior_number_samples, warmup_steps=posterior_warmup_steps, disable_validation=False)
mcmc.run(normalised_hsi)
posterior_samples = mcmc.get_samples()
posterior_predictive = Predictive(model_test, posterior_samples)(normalised_hsi)
plt.figure(figsize=(8,10))
data = az.from_pyro(posterior=mcmc, prior=prior, posterior_predictive=posterior_predictive)
az.plot_trace(data)
filename_posteriro_plot = directory_path + "/posterior.png"
plt.savefig(filename_posteriro_plot)
###############################################TODO################################
# Plot and save the file for each parameter
###################################################################################
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_1'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_1 = directory_path + "/mu_1.png"
plt.savefig(filename_mu_1)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_2'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_2 = directory_path + "/mu_2.png"
plt.savefig(filename_mu_2)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_3'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_3 = directory_path + "/mu_3.png"
plt.savefig(filename_mu_3)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_4'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_4 = directory_path + "/mu_4.png"
plt.savefig(filename_mu_4)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_5'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_5 = directory_path + "/mu_5.png"
plt.savefig(filename_mu_5)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_6'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_6 = directory_path + "/mu_6.png"
plt.savefig(filename_mu_6)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_7'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_7 = directory_path + "/mu_7.png"
plt.savefig(filename_mu_7)
plt.figure(figsize=(8,10))
az.plot_density(
data=[data.posterior, data.prior],
shade=.9,
var_names=['mu_8'],
data_labels=["Posterior Predictive", "Prior Predictive"],
colors=[default_red, default_blue],
)
filename_mu_8 = directory_path + "/mu_8.png"
plt.savefig(filename_mu_8)
###############################################TODO################################
# Find the MAP value
###################################################################################
unnormalise_posterior_value={}
unnormalise_posterior_value["log_prior_geo_list"]=[]
unnormalise_posterior_value["log_likelihood_list"]=[]
unnormalise_posterior_value["log_posterior_list"]=[]
# log_prior_geo_list=[]
# log_prior_hsi_list=[]
# log_likelihood_list=[]
# log_posterior_list=[]
keys_list = list(posterior_samples.keys())
# Define prior for the top layer's location
prior_mean_surface_1 = sp_coords_copy_test[1, 2]
prior_mean_surface_2 = sp_coords_copy_test[4, 2]
prior_mean_surface_3 = sp_coords_copy_test[7, 2]
prior_mean_surface_4 = sp_coords_copy_test[12, 2]
prior_mean_surface_5 = sp_coords_copy_test[8, 2]
prior_mean_surface_6 = sp_coords_copy_test[11, 2]
prior_mean_surface_7 = sp_coords_copy_test[13, 2]
prior_mean_surface_8 = sp_coords_copy_test[15, 2]
store_accuracy=[]
store_gmm_accuracy = []
for i in range(posterior_samples["mu_1"].shape[0]):
post_mu_1 = posterior_samples[keys_list[0]][i]
post_mu_2 = posterior_samples[keys_list[1]][i]
post_mu_3 = posterior_samples[keys_list[2]][i]
post_mu_4 = posterior_samples[keys_list[3]][i]
post_mu_5 = posterior_samples[keys_list[4]][i]
post_mu_6 = posterior_samples[keys_list[5]][i]
post_mu_7 = posterior_samples[keys_list[6]][i]
post_mu_8 = posterior_samples[keys_list[7]][i]
# Calculate the log probability of the value
log_prior_geo = dist.Normal(prior_mean_surface_1, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_1)+\
dist.Normal(prior_mean_surface_2, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_2)+\
dist.Normal(prior_mean_surface_3, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_3)+\
dist.Normal(prior_mean_surface_4, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_4)+\
dist.Normal(prior_mean_surface_5, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_5)+\
dist.Normal(prior_mean_surface_6, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_6)+\
dist.Normal(prior_mean_surface_7, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_7)+\
dist.Normal(prior_mean_surface_8, torch.tensor(0.2, dtype=torch.float64)).log_prob(post_mu_8)
# Update the model with the new top layer's location
interpolation_input = geo_model_test.interpolation_input
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([1]), torch.tensor([2])),
post_mu_1
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([4]), torch.tensor([2])),
post_mu_2
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([7]), torch.tensor([2])),
post_mu_3
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([12]), torch.tensor([2])),
post_mu_4
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([8]), torch.tensor([2])),
post_mu_5
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([11]), torch.tensor([2])),
post_mu_6
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([13]), torch.tensor([2])),
post_mu_7
)
interpolation_input.surface_points.sp_coords = torch.index_put(
interpolation_input.surface_points.sp_coords,
(torch.tensor([15]), torch.tensor([2])),
post_mu_8
)
#print("interpolation_input",interpolation_input.surface_points.sp_coords)
# # Compute the geological model
geo_model_test.solutions = gempy_engine.compute_model(
interpolation_input=interpolation_input,
options=geo_model_test.interpolation_options,
data_descriptor=geo_model_test.input_data_descriptor,
geophysics_input=geo_model_test.geophysics_input,
)
# Compute and observe the thickness of the geological layer
custom_grid_values = geo_model_test.solutions.octrees_output[0].last_output_center.custom_grid_values
accuracy_intermediate = torch.sum(torch.round(custom_grid_values) == y_obs_label) / y_obs_label.shape[0]
#print("accuracy_intermediate", accuracy_intermediate)
store_accuracy.append(accuracy_intermediate)
lambda_ = 15.0
# loc_mean = torch.tensor(mean_init,dtype=torch.float64)
# loc_cov = torch.tensor(cov_init, dtype=torch.float64)
z_nk = F.softmax(-lambda_* (torch.linspace(1,cluster,cluster, dtype=torch.float64) - custom_grid_values.reshape(-1,1))**2, dim=1)
#class_label = torch.mean(F.softmax(-lambda_* (torch.tensor([1,2,3,4,5,6], dtype=torch.float64) - custom_grid_values.reshape(-1,1))**2, dim=1),dim=0)
N_k = torch.sum(z_nk,axis=0)
N = len(custom_grid_values)
pi_k = N_k /N
mean = []
cov = []
for i in range(z_nk.shape[1]):
mean_k = torch.sum( z_nk[:,i][:,None] * normalised_hsi, axis=0)/ N_k[i]
#cov_k = torch.sum( (normalised_hsi - mean_k.reshape((-1,1))) (normalised_hsi - mean_k).T )
cov_k = torch.zeros((mean_k.shape[0],mean_k.shape[0]), dtype=torch.float64)
for j in range(z_nk.shape[0]):
cov_k += z_nk[j,i]* torch.matmul((normalised_hsi[j,:] - mean_k).reshape((-1,1)) ,(normalised_hsi[j,:] - mean_k).reshape((1,-1)))
mean.append(mean_k)
cov_k=cov_k/N_k[i] #+ 1e-3 * torch.diag(torch.ones(cov_k.shape[0],dtype=torch.float64))
cov.append(cov_k)
mean_tensor = torch.stack(mean, dim=0)
cov_tensor = torch.stack(cov,dim=0)
# We can also calculate the accuracy using the mean and covariance to see if our GMM model has imroved or not
gamma_nk = torch.zeros(z_nk.shape)
log_likelihood=torch.tensor(0.0, dtype=torch.float64)
for j in range(normalised_hsi.shape[0]):
likelihood = pi_k[0] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[0],covariance_matrix= cov_tensor[0]).log_prob(normalised_hsi[j])) +\
pi_k[1] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[1],covariance_matrix= cov_tensor[1]).log_prob(normalised_hsi[j]))+\
pi_k[2] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[2],covariance_matrix= cov_tensor[2]).log_prob(normalised_hsi[j])) +\
pi_k[2] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[3],covariance_matrix= cov_tensor[3]).log_prob(normalised_hsi[j])) +\
pi_k[4] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[4],covariance_matrix= cov_tensor[4]).log_prob(normalised_hsi[j])) +\
pi_k[5] *torch.exp(dist.MultivariateNormal(loc=mean_tensor[5],covariance_matrix= cov_tensor[5]).log_prob(normalised_hsi[j]))
for k in range(gamma_nk.shape[1]):
gamma_nk[j][k] = (pi_k[k] * torch.exp(dist.MultivariateNormal(loc=mean_tensor[k],covariance_matrix= cov_tensor[k]).log_prob(normalised_hsi[j]))) / likelihood