forked from alistairboyce11/RJ_MCMC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess_util.py
1149 lines (948 loc) · 39.5 KB
/
postprocess_util.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 25 13:18:47 2022
@author: dorian
"""
import numpy as np
import glob
import time
import warnings
import concurrent.futures
from mpi4py import MPI
import os
warnings.filterwarnings("error")
class Probsum1(Exception):
pass
def get_dispersion(directories, clusters, points=[], filename='dispersion_all.in'):
'''
Parameters
----------
directory : str
directory containing all the results of the inversion.
filename : str, optional
file used for the dispersion curve for MCMc inversions. The default is 'dispersion_all.in'.
Returns
-------
params_dispersion : dict
dictionary containing the metadata of the dispersion curves.
dispersion_ref : dict
dictionary containing the dispersion curve of the cluster centre.
dispersion_all : dict
dictionary containing the individual dispersion curves of the cluster members
and additional information such as cluster they are members of, latitude and longitude of individual points.
All dictionaries are, when relevant, divided into Love (L) and Rayleigh (R) data
'''
# initialising dicts
dispersion_ref = {}
dispersion_ref['R'] = {}
dispersion_ref['L'] = {}
dispersion_all = {}
dispersion_all['R'] = {}
dispersion_all['L'] = {}
params_dispersion = {}
params_dispersion['R'] = {}
params_dispersion['L'] = {}
params_dispersion['clusterlist'] = []
params_dispersion['directories'] = {}
dispersion_all['cluster'] = {}
for ind_cluster in range(len(clusters)):
if clusters[ind_cluster] in dispersion_all['cluster']:
params_dispersion['directories'][clusters[ind_cluster]].append(directories[ind_cluster])
continue
params_dispersion['clusterlist'].append(clusters[ind_cluster])
params_dispersion['directories'][clusters[ind_cluster]] = [directories[ind_cluster]]
dispersion_ref[clusters[ind_cluster]]={}
dispersion_ref[clusters[ind_cluster]]['R']={}
dispersion_ref[clusters[ind_cluster]]['L']={}
# different references for different clusters
file = open(directories[ind_cluster] + '/' + filename, 'r')
numdis_tmp = int(file.readline())
if type(points) == str and points == 'all':
numdis = numdis_tmp
elif len(points) == 0:
numdis = 0
else:
numdis = len(points)
dispersion_all['numdis'] = numdis
dispersion_all['lat'] = np.array(file.readline().split()).astype('float')
dispersion_all['lon'] = np.array(file.readline().split()).astype('float')
clusterlist = np.array(file.readline().split()).astype('float')
clusterlist = clusterlist.astype(np.int64)
dispersion_all['cluster'][clusters[ind_cluster]] = np.where(clusterlist == clusters[ind_cluster])[0]
# Read rayleigh data
ndatad_R = int(file.readline())
params_dispersion['R']['ndatad'] = ndatad_R
mode_rayl = int(file.readline())
mode_R = np.zeros((ndatad_R))
period_R = np.zeros((ndatad_R))
dispersion_R = np.zeros((ndatad_R, numdis))
error_R = np.zeros((ndatad_R, numdis))
dispersion_R_ref = np.zeros((ndatad_R))
error_R_ref = np.zeros((ndatad_R))
j = 0
for i in range(mode_rayl):
file.readline()
ind_k = int(file.readline()) + 1
for k in range(ind_k):
data = file.readline().split()
mode_R[j] = int(data[0])
period_R[j] = float(data[1])
dispersion_R_ref[j] = float(data[2])
error_R_ref[j] = float(data[3])
for l in range(numdis_tmp):
line = file.readline()
# print(line)
data = line.split()
# print(data)
if type(points) == str and points == 'all':
dispersion_R[j, l] = float(data[0])
error_R[j, l] = float(data[1])
else:
if l not in points:
pass
else:
l_ind = list(points).index(l)
dispersion_R[j, l_ind] = float(data[0])
error_R[j, l_ind] = float(data[1])
j += 1
# fill reference dispersion curves: one line per cluster
dispersion_ref[clusters[ind_cluster]]['R']['dispersion'] = dispersion_R_ref
dispersion_ref[clusters[ind_cluster]]['R']['error'] = error_R_ref
dispersion_ref[clusters[ind_cluster]]['R']['error_sum'] = np.sum(np.log(error_R_ref))
# individual data, identical every time
dispersion_all['R']['dispersion'] = dispersion_R
dispersion_all['R']['error'] = error_R
dispersion_all['R']['error_sum'] = np.sum(np.log(error_R), axis=0)
params_dispersion['R']['periods'] = period_R
params_dispersion['R']['modes'] = mode_R
# read love data
ndatad_L = int(file.readline())
params_dispersion['L']['ndatad'] = ndatad_L
mode_love = int(file.readline())
mode_L = np.zeros((ndatad_L))
period_L = np.zeros((ndatad_L))
dispersion_L = np.zeros((ndatad_L, numdis))
error_L = np.zeros((ndatad_L, numdis))
dispersion_L_ref = np.zeros((ndatad_L))
error_L_ref = np.zeros((ndatad_L))
j = 0
for i in range(mode_love):
file.readline()
ind_k = int(file.readline()) + 1
for k in range(ind_k):
data = file.readline().split()
mode_L[j] = int(data[0])
period_L[j] = float(data[1])
dispersion_L_ref[j] = float(data[2])
error_L_ref[j] = float(data[3])
for l in range(numdis_tmp):
data = file.readline().split()
if type(points) == str and points == 'all':
dispersion_L[j, l] = float(data[0])
error_L[j, l] = float(data[1])
else:
if l not in points:
continue
else:
l_ind = list(points).index(l)
dispersion_L[j, l_ind] = float(data[0])
error_L[j, l_ind] = float(data[1])
j += 1
dispersion_ref[clusters[ind_cluster]]['L']['dispersion'] = dispersion_L_ref
dispersion_ref[clusters[ind_cluster]]['L']['error'] = error_L_ref
dispersion_ref[clusters[ind_cluster]]['L']['error_sum'] = np.sum(np.log(error_L_ref))
dispersion_all['L']['dispersion'] = dispersion_L
dispersion_all['L']['error'] = error_L
dispersion_all['L']['error_sum'] = np.sum(np.log(error_L), axis=0)
params_dispersion['L']['periods'] = period_L
params_dispersion['L']['modes'] = mode_L
file.close()
params_dispersion['clusterlist'] = np.array(params_dispersion['clusterlist'])
return params_dispersion, dispersion_ref, dispersion_all
def get_metadata(directories, widenings):
'''
gets the metadata/parameters of the inversion
For this, it reads the first of the output files and extracts the metadata from it
metadata is:
burn-in: length of the burn-in
nsample: number of samples
widening: widening, or temperature, of the inversion.
For a regular inversion, this can be set to one or even better skipped altogether
d_min,d_max: depth boundaries of the inversion
width_vsv: boundaries of the vsv prior (+- deviation from PREM)
xi_min,xi_max: boundaries of the prior for radial anisotropy
vpvs: reference vpv/vsv ratio
vpvs_min,vpvs_max: maximum deviations from the reference vpv/vsv ratio
Ad_R_min,Ad_R_max: boundaries of the prior for Rayleigh noise parameter
Ad_L_min,Ad_L_max: boundaries of the prior for Love noise parameter
Parameters
----------
directory : str
see get_dispersion.
prepare : bool, optional
see get_alpha_max. The default is False.
widening : float, optional
see get_alpha_max. The default is 1.0.
Returns
-------
params_inversion : dict
dict containing all the metadata of the inversion.
'''
params_inversion = {}
files_all = []
for i in range(len(directories)):
directory = directories[i]
for j in range(len(widenings)):
files_all.extend(glob.glob(directory + '/All_models_processed_prepare_*%4.2f.out' % widenings[j]))
for file in files_all:
f = open(file, 'r')
lines = []
for i in range(9):
lines.append(f.readline())
# print(file)
params_inversion = read_header(lines)
f.close()
if not params_inversion:
continue
else:
# print('found data! ')
return params_inversion
return
def process_file_alphamax_all(file, widenings, cluster, widening, params_dispersion, dispersion_ref,
dispersion_all, maxpercent, num_to_store, num_models_cur=np.exp(1.), file_probsum=None,
thin_target=None):
'''processes one file to get alphamax
input: input_dict
input_dict contains:
file: filename of the file to process
Parameters
----------
input_dict : dict
Contains file (file name), params_inversion, params_dispersion, dispersion_ref, dispersion_all and maxpercent
Returns
-------
alpha_ref_max : float
max of alpha_ref, eventually capped
alpha_max : numpy array, length numdis
max of alpha, eventually capped
'''
# reading the file
f = open(file, 'r')
lines = f.readlines()
if file_probsum is None:
pass
elif os.path.isfile(file_probsum):
compute_probsum = False
f_probsum = open(file_probsum, 'r')
lines_probsum = f_probsum.readlines()
f_probsum.close()
i_line_probsum = 0
line = lines_probsum[i_line_probsum]
num_models_prop = {}
while len(line.split()) == 3:
data = line.split()
cluster_tmp = int(data[0])
widening_tmp = float(data[1])
num_models_tmp = int(data[2])
if cluster_tmp in num_models_prop:
num_models_prop[cluster_tmp][widening_tmp] = num_models_tmp
else:
num_models_prop[cluster_tmp] = {}
num_models_prop[cluster_tmp][widening_tmp] = num_models_tmp
i_line_probsum += 1
line = lines_probsum[i_line_probsum]
compute_probsum = params_dispersion['num_models'] != num_models_prop
else:
compute_probsum = True
dir = file_probsum.rpartition('/')[0]
os.makedirs(dir, exist_ok=True)
num_model = 0
numdis = dispersion_all['numdis']
alpha_max = np.ones((num_to_store, numdis)) * float('-inf')
params_inversion = read_header(lines[:9])
# thinning to get all files to same 'effective thinning'
if thin_target is None:
thin_true = 1
else:
thin_true = int(round(thin_target / params_inversion['thin']))
print('thin_true',thin_true)
if not params_inversion:
f.close()
return
widening = params_inversion['widening']
# loop on all models
valid_model = True
cnt = 0
i_model = 0
# lines=f.readlines()
probsums_out = []
maxlines = len(lines)
while valid_model:
if 9 + 11 * (i_model) > maxlines:
valid_model = False
continue
lines_model = lines[9 + 11 * i_model:9 + 11 * (i_model + 1)]
i_model += 1
# thinning to get all files to same 'effective thinning'
if (i_model // params_inversion['num_proc']) % thin_true != 0:
continue
valid_model, dispersion_one, model = read_model(lines_model, widening, cluster)
if not valid_model:
continue
elif len(dispersion_one.keys()) == 0:
continue
elif len(dispersion_one['R']['dispersion']) == 0:
continue
# elif model['npt_true']<8:
# continue
cnt += 1
if file_probsum is None:
probsum_prop = None
elif compute_probsum:
probsum_prop = None
else:
probsum_prop = float(lines_probsum[i_line_probsum])
i_line_probsum += 1
# if num_model==999:
# valid_model=False
# calculate alpha
alpha, probsum = get_alpha_all(dispersion_one, widenings, params_dispersion, dispersion_ref,
dispersion_all,
num_models_cur, probsum_prop)
probsums_out.append(probsum)
if np.any(np.amin(alpha_max, axis=0) < alpha):
min_indices = np.argmin(alpha_max, axis=0)
alpha_max[min_indices, np.arange(numdis)] = np.maximum(alpha, np.amin(alpha_max, axis=0))
num_model += 1
# if (num_model%10000)==0:
# print(num_model)
f.close()
if file_probsum is None:
pass
elif compute_probsum:
dir = file_probsum.rpartition('/')[0]
os.makedirs(dir, exist_ok=True)
f_probsum = open(file_probsum, 'w')
for cluster_probsum in params_dispersion['num_models']:
for widening_probsum in params_dispersion['num_models'][cluster_probsum]:
f_probsum.write(str(cluster_probsum) + ' ' + str(widening_probsum) + ' ' + str(
params_dispersion['num_models'][cluster_probsum][widening_probsum]) + '\n')
for probsum in probsums_out:
f_probsum.write(str(probsum) + '\n')
f_probsum.close()
return alpha_max, num_model
def get_alpha_all(dispersion_one, widenings, params_dispersion, dispersion_ref, dispersion_all,
num_models_cur=np.exp(1), probsum=None):
'''
Gets alpha for one dispersion curve
Parameters
----------
dispersion_one : dict
dispersion curve for one model
params_dispersion : dict
dispersion_ref : dict
dispersion_all : dict
Returns
-------
alpha_ref : float
alpha for the reference dispersion curve.
alpha : numpy array, length numdis
alpha for all cluster members.
'''
clusters = params_dispersion['clusterlist']
ndatad_R = params_dispersion['R']['ndatad']
ndatad_L = params_dispersion['L']['ndatad']
dispersion_R = dispersion_all['R']['dispersion']
dispersion_L = dispersion_all['L']['dispersion']
dispersion_R_one = dispersion_one['R']['dispersion']
dispersion_L_one = dispersion_one['L']['dispersion']
Ad_R = dispersion_one['R']['Ad']
Ad_L = dispersion_one['L']['Ad']
error_R = dispersion_all['R']['error']
error_L = dispersion_all['L']['error']
# error_R_sum=dispersion_all['R']['error_sum']
# error_L_sum=dispersion_all['L']['error_sum']
numdis = dispersion_all['numdis']
if np.any(2 * Ad_R ** 2 * np.square(np.multiply(error_R, dispersion_R / 100.)) == 0):
print('error division by zero', 'Ad_R', Ad_R, 'min error_R', np.amin(error_R), 'min dispersion_R',
np.amin(dispersion_R))
if np.any(2 * Ad_L ** 2 * np.square(np.multiply(error_L, dispersion_L / 100.)) == 0):
print('error division by zero', 'Ad_L', Ad_L, 'min error_L', np.amin(error_L), 'min dispersion_L',
np.amin(dispersion_L))
like_alt = -np.sum(np.divide(
np.square(dispersion_R - np.transpose(np.tile(dispersion_R_one, (numdis, 1)))),
2 * Ad_R ** 2 *
np.square(
np.multiply(
error_R, dispersion_R / 100.)))
, axis=0)
like_alt -= np.sum(np.divide(
np.square(dispersion_L - np.transpose(np.tile(dispersion_L_one, (numdis, 1)))),
2 * Ad_L ** 2 *
np.square(
np.multiply(
error_L, dispersion_L / 100.)))
, axis=0)
like_alt -= ndatad_R * np.log(Ad_R)
like_alt -= ndatad_L * np.log(Ad_L)
if probsum is None:
probsum = 0.
shift = 0
not_processed = True
while not_processed:
try:
numprob = 0.
probsum = 0.
for cluster in clusters:
for widening in widenings:
if params_dispersion['num_models'][cluster][widening] == 0:
continue
dispersion_R_ref = dispersion_ref[cluster]['R']['dispersion']
dispersion_L_ref = dispersion_ref[cluster]['L']['dispersion']
error_R_ref = dispersion_ref[cluster]['R']['error']
error_L_ref = dispersion_ref[cluster]['L']['error']
error_R_ref_sum=dispersion_ref[cluster]['R']['error_sum']
error_L_ref_sum=dispersion_ref[cluster]['L']['error_sum']
if np.shape(dispersion_R_ref) == (0,):
print('here')
if np.shape(dispersion_R_one) == (0,):
print('here2')
print(dispersion_R_one)
like_one = -np.sum(np.divide(np.square(dispersion_R_ref - dispersion_R_one),
2 * Ad_R ** 2 * np.square(
np.multiply(error_R_ref / 100., dispersion_R_ref))), axis=0)
like_one -= np.sum(np.divide(np.square(dispersion_L_ref - dispersion_L_one),
2 * Ad_L ** 2 * np.square(
np.multiply(error_L_ref / 100., dispersion_L_ref))), axis=0)
like_one -= error_R_ref_sum + 150 # static shift to avoid exponential explosion
like_one -= error_L_ref_sum + 150 # static shift to avoid exponential explosion
like_one -= ndatad_R * np.log(Ad_R)
like_one -= ndatad_L * np.log(Ad_L)
like_one /= widening
like_one += shift
probsum += np.exp(like_one)
numprob += 1
not_processed = False
if probsum == 0.:
raise Probsum1
except RuntimeWarning:
not_processed = True
probsum = 0.
shift -= 100
print('too big probsum')
except Probsum1:
not_processed = True
probsum = 0.
shift += 1000
print('too small probsum')
probsum /= numprob
probsum = np.log(probsum) - shift
alpha = like_alt - probsum
alpha -= np.log(num_models_cur)
return alpha, probsum
def read_header(lines_header):
params_inversion = {}
d = lines_header.pop(
0) # f.readline() # contains rank of processor, number of file for the processor and number of models stored at most. Not used or tested currently
params_inversion['num_proc'] = 112
params_inversion['everyall'] = int(float(d.split()[1]))
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['burn-in'] = float(data[0])
params_inversion['widening'] = float(data[1])
params_inversion['thin'] = float(data[2])
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['d_min'] = float(data[0])
params_inversion['d_max'] = float(data[1])
params_inversion['width_vsv'] = float(lines_header.pop(0)) # f.readline())
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['xi_min'] = float(data[0])
params_inversion['xi_max'] = float(data[1])
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['vp_min'] = float(data[0])
params_inversion['vp_max'] = float(data[1])
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['Ad_R_min'] = float(data[0])
params_inversion['Ad_R_max'] = float(data[1])
data = lines_header.pop(0).split() # f.readline().split()
params_inversion['Ad_L_min'] = float(data[0])
params_inversion['Ad_L_max'] = float(data[1])
data = lines_header.pop(0).split() # f.readline().split()
if len(data) == 0:
return
params_inversion['milay'] = int(data[0])
params_inversion['malay'] = int(data[1])
return params_inversion
def read_model(lines_model, widening, cluster):
dispersion_one = {}
dispersion_one['R'] = {}
dispersion_one['L'] = {}
model = {}
if len(lines_model) != 11:
return False, {}, {}
line = lines_model.pop(0) # f.readline()
# if not line:
# return False,{},{}
data = line.split()
npt_true = int(data[0])
if npt_true == 0:
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
line = lines_model.pop(0) # f.readline()
return True, {}, {}
# try:
model['npt_true'] = npt_true
model['npt'] = int(data[1])
model['npt_ani'] = int(data[2])
data = lines_model.pop(0).split() # f.readline().split()
dispersion_one['R']['Ad'] = float(data[0])
dispersion_one['L']['Ad'] = float(data[1])
model['Ad_R'] = dispersion_one['R']['Ad']
model['Ad_L'] = dispersion_one['L']['Ad']
# new output
d = np.array(lines_model.pop(0).split()).astype('float') # f.readline().split()).astype('float')
vsv = np.array(lines_model.pop(0).split()).astype('float') # np.array(f.readline().split()).astype('float')
xi = np.array(lines_model.pop(0).split()).astype('float') # np.array(f.readline().split()).astype('float')
vp = np.array(lines_model.pop(0).split()).astype('float') # np.array(f.readline().split()).astype('float')
model['depth'] = d
model['vsv'] = vsv
model['xi'] = xi
model['vp'] = vp
dispersion_one['like_w'] = float(lines_model.pop(0)) # f.readline())
model['like_w'] = dispersion_one['like_w']
dispersion_one['cluster'] = cluster
len_R = int(lines_model.pop(0)) # f.readline())
dispersion_R_one = np.array(lines_model.pop(0).split()).astype(
'float') # np.array(f.readline().split()).astype('float')
dispersion_one['R']['dispersion'] = dispersion_R_one
len_L = int(lines_model.pop(0)) # f.readline())
dispersion_L_one = np.array(lines_model.pop(0).split()).astype(
'float') # np.array(f.readline().split()).astype('float')
dispersion_one['L']['dispersion'] = dispersion_L_one
# except:
# return True, {}, {}
if len(dispersion_L_one) != len_L or len(dispersion_R_one) != len_R or len(d) != npt_true or len(
vsv) != npt_true or len(xi) != npt_true or len(vp) != npt_true:
return True, {}, {}
if len(dispersion_L_one) == 0 or len(dispersion_R_one) == 0 or len(d) == 0 or len(vsv) == 0 or len(xi) == 0 or len(
vp) == 0:
return True, {}, {}
if model['Ad_R'] == 0 or model['Ad_L'] == 0:
return True, {}, {}
return True, dispersion_one, model
def get_model_ref(filename='Modified_PREM_GLOBAL.in'):
'''
reads the reference model, puts into a dict
contains:
npt_true: number of points
depth: depth of interfaces
vpv, vph, vsv, vsh: velocities
Parameters
----------
filename : str
file containing the reference model. Default 'Model_PREM_SIMPLE.in'.
Returns
-------
model_ref : dict
contains the data of the reference model.
'''
file = filename
model_ref = {}
f = open(file, 'r')
lines = f.readlines()
f.close()
data = lines[0].split()
ntot = int(data[0])
nic = int(data[1])
noc = int(data[2])
npt_true = ntot - noc
rearth = float(lines[-1].split()[0])
d = np.zeros((npt_true))
vsv = np.zeros_like(d)
vsh = np.zeros_like(d)
vpv = np.zeros_like(d)
vph = np.zeros_like(d)
for i in range(npt_true):
data = lines[ntot - i].split()
d[i] = (rearth - float(data[0])) / 1000.
vpv[i] = float(data[2]) / 1000.
vsv[i] = float(data[3]) / 1000.
vph[i] = float(data[6]) / 1000.
vsh[i] = float(data[7]) / 1000.
model_ref['npt_true'] = npt_true
model_ref['depth'] = d
model_ref['vpv'] = vpv
model_ref['vph'] = vph
model_ref['vsv'] = vsv
model_ref['vsh'] = vsh
return model_ref
def process_one_file_all(file, cluster, widenings_in, functions, params_dispersion, dispersion_ref,
dispersion_all, model_ref, outputs_all={}, file_probsum=None, thin_target=None):
'''
Processes one file, reading the podels in the file, applying the functions to them and stacking the results
Parameters
----------
input_dict : dict
input dictionary containing the file name (file), the list of functions (functions),
params_inversion, params_dispersion, dispersion_ref, dispersion_all and model_ref.
Returns
-------
outputs : dict
has one subdict for each function, called with the function name.
Each of those has a subdict 'stack' and a subdict 'nostack' which will respectively stacked and kept as in the first model
'''
time_read = 0
time_alpha = 0
time_apply = 0
numdis = dispersion_all['numdis']
alpha_sum = np.zeros((numdis + 1))
exp_sum = np.zeros((numdis + 1))
t1 = time.time()
f = open(file, 'r')
lines = f.readlines()
params_inversion = read_header(lines[:9])
# thinning to get all files to same 'effective thinning'
if thin_target is None:
thin_true = 1
else:
thin_true = int(round(thin_target / params_inversion['thin']))
widening = params_inversion['widening']
t2 = time.time()
time_read += t2 - t1
t1 = time.time()
if file_probsum is None:
pass
elif os.path.isfile(file_probsum):
compute_probsum = False
f_probsum = open(file_probsum, 'r')
lines_probsum = f_probsum.readlines()
f_probsum.close()
i_line_probsum = 0
line = lines_probsum[i_line_probsum]
num_models_prop = {}
while len(line.split()) == 3:
data = line.split()
cluster_tmp = int(data[0])
widening_tmp = float(data[1])
num_models_tmp = int(data[2])
if cluster_tmp in num_models_prop:
num_models_prop[cluster_tmp][widening_tmp] = num_models_tmp
else:
num_models_prop[cluster_tmp] = {}
num_models_prop[cluster_tmp][widening_tmp] = num_models_tmp
i_line_probsum += 1
line = lines_probsum[i_line_probsum]
compute_probsum = params_dispersion['num_models'] != num_models_prop
else:
compute_probsum = True
dir = file_probsum.rpartition('/')[0]
os.makedirs(dir, exist_ok=True)
probsums_out = []
t2 = time.time()
time_alpha += t2 - t1
numtot = 0
i_model = 0
valid_model = True
maxlines = len(lines)
while valid_model:
if 9 + 11 * (i_model) > maxlines:
valid_model = False
continue
lines_model = lines[9 + 11 * i_model:9 + 11 * (i_model + 1)]
i_model += 1
# thinning to get all files to same 'effective thinning'
if (i_model // params_inversion['num_proc']) % thin_true != 0:
continue
t1 = time.time()
valid_model, dispersion_one, model = read_model(lines_model, widening, cluster)
# if numtot>1000:
# valid_model=False
if not valid_model:
t2 = time.time()
time_read += t2 - t1
continue
elif len(dispersion_one.keys()) == 0:
t2 = time.time()
time_read += t2 - t1
continue
t2 = time.time()
time_read += t2 - t1
t1 = time.time()
if file_probsum is None:
probsum_prop = None
elif compute_probsum:
probsum_prop = None
else:
probsum_prop = float(lines_probsum[i_line_probsum])
i_line_probsum += 1
alpha, probsum = get_alpha_all(dispersion_one, widenings_in, params_dispersion, dispersion_ref,
dispersion_all,
num_models_cur=params_dispersion['num_models'][cluster][widening],
probsum=probsum_prop)
probsums_out.append(probsum)
t2 = time.time()
time_alpha += t2 - t1
t1 = time.time()
# apply functions
for function in functions:
function(file, model, model_ref, dispersion_one, params_dispersion, params_inversion, dispersion_all, alpha,
outputs=outputs_all[function.__name__])
numtot += 1
alpha_sum[:numdis] += np.minimum(np.zeros_like(alpha), alpha - dispersion_all['alpha_max'])
exp_sum[:numdis] += np.exp(np.minimum(np.zeros_like(alpha), alpha - dispersion_all['alpha_max']))
exp_sum[numdis] += 1.
t2 = time.time()
time_apply += t2 - t1
# if numtot%10000==0:
# print(numtot)
# testing
# if numtot==10:
# f.close()
# print('read: ',time_read)
# print('get alpha: ',time_alpha)
# print('apply: ',time_apply)
# return alpha_sum,numtot,exp_sum,exp_ssq
f.close()
if file_probsum is None:
pass
elif compute_probsum:
dir = file_probsum.rpartition('/')[0]
os.makedirs(dir, exist_ok=True)
f_probsum = open(file_probsum, 'w')
for cluster_probsum in params_dispersion['num_models']:
for widening_probsum in params_dispersion['num_models'][cluster_probsum]:
f_probsum.write(str(cluster_probsum) + ' ' + str(widening_probsum) + ' ' + str(
params_dispersion['num_models'][cluster_probsum][widening_probsum]) + '\n')
for probsum in probsums_out:
f_probsum.write(str(probsum) + '\n')
f_probsum.close()
print('read: ', time_read)
print('get alpha: ', time_alpha)
print('apply: ', time_apply)
return alpha_sum, numtot, exp_sum
def get_alpha_norm(dispersion_one, widenings, params_dispersion, dispersion_ref, dispersion_all):
'''
Gets alpha for one dispersion curve
Parameters
----------
dispersion_one : dict
dispersion curve for one model
params_dispersion : dict
dispersion_ref : dict
dispersion_all : dict
Returns
-------
alpha_ref : float
alpha for the reference dispersion curve.
alpha : numpy array, length numdis
alpha for all cluster members.
'''
clusters = params_dispersion['clusterlist']
ndatad_R = params_dispersion['R']['ndatad']
ndatad_L = params_dispersion['L']['ndatad']
dispersion_R = dispersion_all['R']['dispersion']
dispersion_L = dispersion_all['L']['dispersion']
dispersion_R_one = dispersion_one['R']['dispersion']
dispersion_L_one = dispersion_one['L']['dispersion']
Ad_R = dispersion_one['R']['Ad']
Ad_L = dispersion_one['L']['Ad']
error_R = dispersion_all['R']['error']
error_L = dispersion_all['L']['error']
error_R_ref_sum = dispersion_ref['R']['error_sum']
error_L_ref_sum = dispersion_ref['L']['error_sum']
numdis = dispersion_all['numdis']
like_alt = -np.sum(np.divide(
np.square(dispersion_R - np.transpose(np.tile(dispersion_R_one, (numdis, 1)))),
2 * Ad_R ** 2 *
np.square(
np.multiply(
error_R, dispersion_R / 100.)))
, axis=0)
like_alt -= np.sum(np.divide(
np.square(dispersion_L - np.transpose(np.tile(dispersion_L_one, (numdis, 1)))),
2 * Ad_L ** 2 *
np.square(
np.multiply(
error_L, dispersion_L / 100.)))
, axis=0)
# static shift
# like_alt-=error_R_sum
# like_alt-=error_L_sum
like_alt -= ndatad_R * np.log(Ad_R)
like_alt -= ndatad_L * np.log(Ad_L)
probsum = 0.
shift = 0
while probsum == 0.:
try:
numprob = 0.
probsum = 0.
for i in range(len(clusters)):
for widening in widenings:
dispersion_R_ref = dispersion_ref['R']['dispersion'][i, :]
dispersion_L_ref = dispersion_ref['L']['dispersion'][i, :]
error_R_ref = dispersion_ref['R']['error'][i, :]
error_L_ref = dispersion_ref['L']['error'][i, :]
if np.shape(dispersion_R_ref) == (0,):
print('here')
if np.shape(dispersion_R_one) == (0,):
print('here2')
print(dispersion_R_one)
# print('new')
like_one = -np.sum(np.divide(np.square(dispersion_R_ref - dispersion_R_one),
2 * Ad_R ** 2 * np.square(
np.multiply(error_R_ref / 100., dispersion_R_ref))), axis=0)
# print(like_one)
like_one -= np.sum(np.divide(np.square(dispersion_L_ref - dispersion_L_one),
2 * Ad_L ** 2 * np.square(
np.multiply(error_L_ref / 100., dispersion_L_ref))), axis=0)
# print(like_one)
like_one -= error_R_ref_sum[i] + 150 # static shift to avoid exponential explosion
# print(like_one)
like_one -= error_L_ref_sum[i] + 150 # static shift to avoid exponential explosion
# print(like_one)
like_one -= ndatad_R * np.log(Ad_R)
# print(like_one)
like_one -= ndatad_L * np.log(Ad_L)
# print(like_one)
like_one /= widening
# print(like_one)
# like_one+=1000
# print(like_one)
like_one += shift
like_one -= np.log(params_dispersion['num_models'][clusters[i]][widening])
probsum += np.exp(like_one)
numprob += 1
# print(probsum)
if probsum == 0.:
raise Probsum1
except RuntimeWarning: