-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualise.py
1384 lines (1032 loc) · 56.1 KB
/
visualise.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
#Main Contributers: Rohitash Chandra and Ratneel Deo Email: c.rohitash@gmail.com, deo.ratneel@gmail.com
# Bayeslands II: Parallel tempering for multi-core systems - Badlands
from __future__ import print_function, division
# mpl.use('Agg')
import os
import shutil
import sys
import random
import time
import operator
import math
import copy
import fnmatch
import collections
import numpy as np
import matplotlib as mpl
import re
import pandas as pd
import h5py
import json
# import matplotlib.pyplot as plt
from matplotlib import pyplot as plt
import matplotlib.mlab as mlab
import multiprocessing
import itertools
import chart_studio
import chart_studio.plotly as py
import pandas
import argparse
import pandas as pd
# import seaborn as sns
import scipy.ndimage as ndimage
#plotly.offline.init_notebook_mode()
from plotly.graph_objs import *
from pylab import rcParams
from copy import deepcopy
from pylab import rcParams
from scipy import special
from PIL import Image
from io import StringIO
from cycler import cycler
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from scipy.spatial import cKDTree
from scipy import stats
from badlands.model import Model as badlandsModel
from mpl_toolkits.axes_grid1 import make_axes_locatable
from mpl_toolkits.mplot3d import Axes3D
from IPython.display import HTML
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from scipy.ndimage import filters
from scipy.ndimage import gaussian_filter
from ProblemSetup import ProblemSetup
import subprocess
# from dbfpy import dbf
from GenUplift import edit_Uplift, process_Uplift
from GenInitTopo import process_inittopoGMT, edit_DBF
#Initialise and parse inputs
parser=argparse.ArgumentParser(description='PTBayeslands modelling')
parser.add_argument('-p','--problem', help='Problem Number 1-crater-fast,2-crater,3-etopo-fast,4-etopo,5-null,6-mountain', required=True, dest="problem",type=int)
parser.add_argument('-s','--samples', help='Number of samples', default=10000, dest="samples",type=int)
parser.add_argument('-r','--replicas', help='Number of chains/replicas, best to have one per availble core/cpu', default=10,dest="num_chains",type=int)
parser.add_argument('-t','--temperature', help='Demoninator to determine Max Temperature of chains (MT=no.chains*t) ', default=10,dest="mt_val",type=int)
parser.add_argument('-swap','--swap', help='Swap interval', dest="swap_interval",default= 2,type=int)
parser.add_argument('-b','--burn', help='How many samples to discard before determing posteriors', dest="burn_in",default=0.25,type=float)
parser.add_argument('-pt','--ptsamples', help='Ratio of PT vs straight MCMC samples to run', dest="pt_samples",default=0.5,type=float)
parser.add_argument('-rain_intervals','--rain_intervals', help='rain_intervals', dest="rain_intervals",default=4,type=int)
parser.add_argument('-epsilon','--epsilon', help='epsilon for inital topo', dest="epsilon",default=0.5,type=float)
parser.add_argument('-cov','--covariance', help='flag for covariance', dest="covariance",default=0,type=int)
parser.add_argument('-inittopo', '--initialtopo', help='flag for init topo inference', dest="inittopo", default=1, type=int)
parser.add_argument('-uplift', '--uplift', help='flag for uplift inference', dest="uplift", default=1, type=int)
args = parser.parse_args()
#parameters for Parallel Tempering
problem = args.problem
samples = args.samples
num_chains = args.num_chains
swap_interval = args.swap_interval
burn_in=args.burn_in
#maxtemp = int(num_chains * 5)/args.mt_val
maxtemp = args.mt_val
num_successive_topo = 4
pt_samples = args.pt_samples
epsilon = args.epsilon
rain_intervals = args.rain_intervals
covariance = args.covariance
inittopo = args.inittopo
uplift = args.uplift
method = 1 # type of formaltion for inittopo construction (Method 1 showed better results than Method 2)
class results_visualisation:
def __init__(self, vec_parameters, sea_level, ocean_t, inittopo_expertknow, inittopo_estimated, rain_regiongrid, rain_timescale, len_grid, wid_grid, num_chains, maxtemp, samples,swap_interval,fname, num_param , groundtruth_elev, groundtruth_erodep_pts , erodep_coords, elev_coords, simtime, sim_interval, resolu_factor, xmlinput, run_nb_str, init_elev ):
self.ID = 0
self.swap_interval = swap_interval
self.folder = fname
self.maxtemp = maxtemp
self.num_swap = 0
self.num_chains = num_chains
self.chains = []
self.temperatures = []
self.NumSamples = samples
self.sub_sample_size = max(1, int( 0.05* self.NumSamples))
self.show_fulluncertainity = False # needed in cases when you reall want to see full prediction of 5th and 95th percentile of topo. takes more space
self.real_erodep_pts = groundtruth_erodep_pts
self.real_elev = groundtruth_elev
self.resolu_factor = resolu_factor
self.num_param = num_param
self.elev_coords = elev_coords
self.erodep_coords = erodep_coords
self.simtime = simtime
self.sim_interval = sim_interval
self.xmlinput = xmlinput
self.run_nb_str = run_nb_str
self.vec_parameters = vec_parameters
#self.realvalues = realvalues_vec
self.burn_in = burn_in
# self.input = ['Examples/australia/AUSB001.xml','Examples/australia/AUSP1307.xml', 'Examples/australia/AUSP1310.xml',
# 'Examples/australia/AUSP1311.xml','Examples/australia/AUSP1312.xml', 'Examples/australia/AUSP1313.xml', 'Examples/australia/AUSP1314.xml',
# 'Examples/australia/AUSP1315.xml']
self.input = ['Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml'
,'Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml','Examples/australia_gda94/AUSB004.xml']
# create queues for transfer of parameters between process chain
self.geometric = True
self.total_swap_proposals = 0
self.rain_region = rain_regiongrid
self.rain_time = rain_timescale
self.len_grid = len_grid
self.wid_grid = wid_grid
self.inittopo_expertknow = inittopo_expertknow
self.inittopo_estimated = inittopo_estimated
self.init_elev = init_elev
self.ocean_t = ocean_t
self.Bayes_inittopoknowledge = True
self.sea_level = sea_level
def init_show(self, zData, fname, replica_id):
fig = plt.figure()
im = plt.imshow(zData, cmap='hot', interpolation='nearest')
plt.colorbar(im)
plt.savefig(self.folder + fname+ str(int(replica_id))+'.png')
plt.close()
def results_current (self ):
#pos_param, likelihood_rep, accept_list, pred_topo, combined_erodep, accept, pred_topofinal, list_xslice, list_yslice, rmse_elev, rmse_erodep = self.show_results('chain_')
posterior, likelihood_vec, accept_list, xslice, yslice, rmse_elev, rmse_erodep, erodep_pts = self.show_results('chain_')
self.view_crosssection_uncertainity(xslice, yslice)
optimal_para, para_5thperc, para_95thperc = self.get_uncertainity(likelihood_vec, posterior)
np.savetxt(self.folder+'/optimal_percentile_para.txt', np.array([optimal_para, para_5thperc, para_95thperc]) )
#for s in range(self.num_param):
for s in range(self.vec_parameters.size): # change this if you want to see all pos plots
self.plot_figure(posterior[s,:], 'pos_distri_'+str(s) )
rain_regiontime = self.rain_region * self.rain_time # number of parameters for rain based on region and time
num_sealevel_coef =10
geoparam = rain_regiontime+12 + num_sealevel_coef
mean_pos = posterior.mean(axis=1)
std_pos = posterior.std(axis=1)
np.savetxt(self.folder+'/mean_pos.txt', mean_pos)
np.savetxt(self.folder+'/std_pos.txt', std_pos)
percentile_95th = np.percentile(posterior, 95, axis=1)
percentile_5th = np.percentile(posterior, 5, axis=1)
init = True # when you need to estimate initial topo
if init == True:
init_topo_mean = self.process_inittopo(mean_pos[geoparam:], 'post_mean')
init_topo_95th = self.process_inittopo(percentile_95th[geoparam:], 'post_95th')
init_topo_5th = self.process_inittopo(percentile_5th[geoparam:], 'post_5th')
#print(mean_pos[geoparam:] - percentile_95th[geoparam:], 'init_topo_mean - init_topo_95th')
#print(init_topo_mean[geoparam:] - init_topo_95th[geoparam:], 'init_topo_mean - init_topo_95th')
self.plot3d_plotly(init_topo_mean, 'mean_init')
self.plot3d_plotly(init_topo_95th, 'percentile95_init')
self.plot3d_plotly(init_topo_5th, 'percentile5_init')
init_topo_mean = init_topo_mean[0:self.real_elev.shape[0], 0:self.real_elev.shape[1]] # just to ensure that the size is exact
init_topo_95th = init_topo_95th[0:self.real_elev.shape[0], 0:self.real_elev.shape[1]] # just to ensure that the size is exact
init_topo_5th = init_topo_5th[0:self.real_elev.shape[0], 0:self.real_elev.shape[1]] # just to ensure that the size is exact
xmid = int(init_topo_mean.shape[0]/2)
inittopo_real = init_topo_mean[xmid, :] # ground-truth init topo mid (synthetic)
#inittopo_real = self.inittopo_estimated[xmid, :] # ground-truth init topo mid (synthetic)
lower_mid = init_topo_5th[xmid, :]
higher_mid = init_topo_95th[xmid, :]
mean_mid = init_topo_mean[xmid, :]
x = np.linspace(0, self.real_elev.shape[1] * self.resolu_factor, num= self.real_elev.shape[1])
rmse_slice_init = self.cross_section(x, mean_mid, inittopo_real, lower_mid , higher_mid , 'init_x_ymid_cross') # not needed in Australia problem
mean_sealevel = self.process_sealevel(mean_pos[rain_regiontime+11 :geoparam+num_sealevel_coef])
#print(mean_pos[0:geoparam+num_sealevel_coef], ' mean_pos[geoparam:geoparam+num_sealevel_coef]')
#print(mean_sealevel, ' mean_sealevel')
sealevel_95th = self.process_sealevel(percentile_95th[rain_regiontime+11 :geoparam+num_sealevel_coef])
sealevel_5th = self.process_sealevel(percentile_5th[rain_regiontime+11 :geoparam+num_sealevel_coef])
timeframes = self.sea_level[:,0]
fig, ax = plt.subplots()
size = 12
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
fnameplot = self.folder + '/sealevel_data_recons.pdf'
ax.plot(timeframes, self.sea_level[:,1], label='expert knowledge')
ax.plot(timeframes, mean_sealevel[:,1], label='mean prediction')
ax.plot(timeframes, sealevel_95th[:,1], label='95th percentile')
ax.plot(timeframes, sealevel_5th[:,1], label='5th percentile')
ax.fill_between(timeframes, sealevel_95th[:,1], sealevel_5th[:,1], alpha=0.5)
ax.legend()
plt.grid(alpha=0.75)
plt.gcf().subplots_adjust(bottom=0.15)
#plt.tight_layout()
plt.xlabel('Timeframe (Ma)', fontsize = size)
plt.ylabel('Sealevel (meters)', fontsize = size)
plt.savefig(fnameplot)
plt.clf()
else:
rmse_slice_init = 0
return posterior, likelihood_vec, accept_list, xslice, yslice, rmse_elev, rmse_erodep, erodep_pts, rmse_slice_init
def process_sealevel(self, coeff):
y = self.sea_level[:,1].copy()
timeframes = self.sea_level[:,0]
first = y[0:50] # sea leavel for 0 - 49 Ma to be untouched
second = y[50:] # this will be changed by sea level coeefecients proposed by MCMC
second_mat = np.reshape(second, (10, 10))
updated_mat = second_mat
for l in range(0,second_mat.shape[0]):
for w in range(0,second_mat.shape[1]):
updated_mat[l][w] = (second_mat[l][w] * coeff[l]) + second_mat[l][w]
reformed_sl = updated_mat.flatten()
combined_sl = np.concatenate([first, reformed_sl])
yhat = self.smooth(combined_sl, 10)
proposed_sealevel = np.vstack([timeframes, yhat])
return proposed_sealevel.T
def smooth(self,y, box_pts):
#https://stackoverflow.com/questions/20618804/how-to-smooth-a-curve-in-the-right-way
#print(y.shape, y, ' ++ y ')
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
return y_smooth
def full_crosssection(self, simulated_topo, real_elev):
ymid = int( real_elev.shape[1]/2)
x = np.linspace(0, real_elev.shape[0], num=real_elev.shape[0])
x_m = np.arange(0,real_elev.shape[0], 10)
for i in x_m:
xmid = i
real = real_elev[0:real_elev.shape[0], i]
pred = simulated_topo[0:real_elev.shape[0], i]
size = 15
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.plot(x, real, label='Ground Truth')
plt.plot(x, pred, label='Badlands Pred.')
#plt.plot(x, init, label = 'Initial Topo')
plt.grid(alpha=0.75)
plt.legend(loc='best')
plt.title("Topography cross section ", fontsize = size)
plt.xlabel(' Distance (x 50 km) ', fontsize = size)
plt.ylabel(' Height (m)', fontsize = size)
plt.tight_layout()
plt.savefig(self.folder+'/cross_section/'+str(i)+'_cross-sec_postcompare.pdf')
plt.clf()
fnameplot = self.folder + '/cross_section/realmap_postcompare.png'
im = plt.imshow(real_elev, cmap='hot', interpolation='nearest')
plt.colorbar(im)
plt.savefig(fnameplot)
plt.clf()
fnameplot = self.folder + '/cross_section/predmap_postcompare.png'
im = plt.imshow(simulated_topo, cmap='hot', interpolation='nearest')
plt.colorbar(im)
plt.savefig(fnameplot)
plt.clf()
fnameplot = self.folder + '/cross_section/diffmap_postcompare.png'
im = plt.imshow(real_elev- simulated_topo, cmap='hot', interpolation='nearest')
plt.colorbar(im)
plt.savefig(fnameplot)
plt.clf()
def plot3d_plotly(self, zData, fname): # same method from previous class - ptReplica
fig = plt.figure()
im = plt.imshow(zData, cmap='hot', interpolation='nearest')
plt.colorbar(im)
plt.savefig(self.folder + '/recons_initialtopo/'+fname+'.png')
plt.close()
np.savetxt(self.folder + '/recons_initialtopo/'+fname+'_.txt', zData, fmt='%1.2f' )
def process_inittopo(self, inittopo_vec, filename):
length = self.real_elev.shape[0]
width = self.real_elev.shape[1]
len_grid = self.len_grid
wid_grid = self.wid_grid
#print('\n\nlength, width, len_grid, wid_grid ',length, width, len_grid, wid_grid)
sub_gridlen = 20 #int(length/len_grid) # 25
sub_gridwidth = 20 #int(width/wid_grid) # 25
new_length =len_grid * sub_gridlen
new_width =wid_grid * sub_gridwidth
if problem == 1:
reconstructed_topo = self.real_elev.copy() # to define the size
groundtruth_topo = self.real_elev.copy()
else:
reconstructed_topo = self.init_elev.copy() # to define the size
groundtruth_topo = self.init_elev.copy()
'''if problem == 1:
inittopo_vec = self.inittopo_expertknow.flatten() + inittopo_vec
else:
inittopo_vec = inittopo_vec
print(inittopo_vec.shape, ' inittopo_vec ** ---------------------------------------------------- ** ')
v_ = np.reshape(inittopo_vec, (sub_gridlen, -1) )#np.random.rand(len_grid,wid_grid)
for l in range(0,sub_gridlen-1):
for w in range(0,sub_gridwidth-1):
for m in range(l * len_grid,(l+1) * len_grid):
for n in range(w * wid_grid, (w+1) * wid_grid):
reconstructed_topo[m][n] = (reconstructed_topo[m][n]) + (v_[l][w])
width = reconstructed_topo.shape[0]
length = reconstructed_topo.shape[1]
for l in range(0,sub_gridlen -1 ):
w = sub_gridwidth-1
for m in range(l * len_grid,(l+1) * len_grid):
for n in range(w * wid_grid, length):
groundtruth_topo[m][n] = (groundtruth_topo[m][n]) + (v_[l][w])
# groundtruth_topo[m][n] += v_[l][w]
for w in range(0,sub_gridwidth -1):
l = sub_gridlen-1
for m in range(l * len_grid,width):
for n in range(w * wid_grid, (w+1) * wid_grid):
# groundtruth_topo[m][n] += v_[l][w]
groundtruth_topo[m][n] = (groundtruth_topo[m][n]) + (v_[l][w])
inside = reconstructed_topo[ 0 : sub_gridlen-2 * len_grid,0: (sub_gridwidth-2 * wid_grid) ]
for m in range(0 , inside.shape[0]):
for n in range(0 , inside.shape[1]):
groundtruth_topo[m][n] = inside[m][n] '''
groundtruth_topo = gaussian_filter(reconstructed_topo, sigma=(1 ,1 )) # change sigma to higher values if needed
return groundtruth_topo
def view_crosssection_uncertainity(self, list_xslice, list_yslice):
ymid = int(self.real_elev.shape[1]/2)
xmid = int(self.real_elev.shape[0]/2)
self.real_elev_ = self.real_elev
x_ymid_real = self.real_elev_[xmid, :]
y_xmid_real = self.real_elev_[:, ymid ]
#x_ymid_init = self.init_elev[xmid, :]
#y_xmid_init = self.init_elev[:, ymid]
x_ymid_mean = list_xslice.mean(axis=1)
y_xmid_mean = list_yslice.mean(axis=1)
x_ymid_5th = np.percentile(list_xslice, 5, axis=1)
x_ymid_95th= np.percentile(list_xslice, 95, axis=1)
y_xmid_5th = np.percentile(list_yslice, 5, axis=1)
y_xmid_95th= np.percentile(list_yslice, 95, axis=1)
x = np.linspace(0, x_ymid_mean.size * self.resolu_factor, num=x_ymid_mean.size)
x_ = np.linspace(0, y_xmid_mean.size * self.resolu_factor, num=y_xmid_mean.size)
#ax.set_xlim(-width,len(ind)+width)
self.cross_section(x, x_ymid_mean, x_ymid_real, x_ymid_5th, x_ymid_95th, 'x_ymid_cross_%s_%s' %(xmid,ymid))
self.cross_section(x_, y_xmid_mean, y_xmid_real, y_xmid_5th, y_xmid_95th, 'y_xmid_cross_%s_%s'%(xmid,ymid))
def cross_section(self, x, pred, real, lower, higher, fname):
init =[]
size = 15
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.plot(x, real, label='Ground Truth')
plt.plot(x, pred, label='Badlands Pred.')
#plt.plot(x, init, label = 'Initial Topo')
plt.grid(alpha=0.75)
rmse_init = np.sqrt(np.sum(np.square(pred - real)) / real.size)
plt.fill_between(x, lower , higher, facecolor='g', alpha=0.2, label = 'Uncertainty')
#plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.legend(loc='best')
#plt.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3, fancybox=True, shadow=True)
plt.title("Topography cross section ", fontsize = size)
plt.xlabel(' Distance (x 50 km) ', fontsize = size)
plt.ylabel(' Height (m)', fontsize = size)
plt.tight_layout()
plt.savefig(self.folder+'/'+fname+'.pdf')
plt.clf()
return rmse_init
def get_synthetic_initopo(self):
model = badlandsModel()
# Load the XmL input file
model.load_xml(str(self.run_nb_str), self.xmlinput, muted=True)
#Update the initial topography
#Use the coordinates from the original dem file
xi=int(np.shape(model.recGrid.rectX)[0]/model.recGrid.nx)
yi=int(np.shape(model.recGrid.rectY)[0]/model.recGrid.ny)
#And put the demfile on a grid we can manipulate easily
elev=np.reshape(model.recGrid.rectZ,(xi,yi))
return elev
# Merge different MCMC chains y stacking them on top of each other
def show_results(self, filename):
path = self.folder +'/posterior/pos_parameters/'
x = [] # first get the size of the files
files = os.listdir(path)
print('path ', path)
print ('files', files)
for name in files:
dat = np.loadtxt(path+name)
x.append(dat.shape[0])
print('x', x)
size_pos = min(x)
self.num_chains = len(x)
self.NumSamples = int((self.num_chains * size_pos)/ self.num_chains)
burnin = int((self.NumSamples * self.burn_in)/self.num_chains)
coverage = self.NumSamples - burnin
pos_param = np.zeros((self.num_chains, self.NumSamples , self.num_param))
list_xslice = np.zeros((self.num_chains, self.NumSamples , self.real_elev.shape[1]))
list_yslice = np.zeros((self.num_chains, self.NumSamples , self.real_elev.shape[0]))
likehood_rep = np.zeros((self.num_chains, self.NumSamples)) # index 1 for likelihood posterior and index 0 for Likelihood proposals. Note all likilihood proposals plotted only
#accept_percent = np.zeros((self.num_chains, 1))
accept_list = np.zeros((self.num_chains, self.NumSamples ))
topo = self.real_elev
print('self.real_erodep_pts.shape[1]', self.real_erodep_pts.shape[0])
edp_pts_time = self.real_erodep_pts.shape[0]#*self.sim_interval.size
print(self.real_erodep_pts.shape[0], self.real_erodep_pts.shape, ' ------------------------------------ ')
erodep_pts = np.zeros(( self.num_chains, self.NumSamples , edp_pts_time ))
combined_erodep = np.zeros((self.num_chains, self.NumSamples, self.real_erodep_pts.shape[0] ))
timespan_erodep = np.zeros(( (self.NumSamples - burnin) * self.num_chains, self.real_erodep_pts.shape[0] ))
rmse_elev = np.zeros((self.num_chains, self.NumSamples))
rmse_erodep = np.zeros((self.num_chains, self.NumSamples))
path = self.folder +'/posterior/pos_parameters/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
# print(dat.shape, pos_param.shape, v, burnin, size_pos, coverage)
pos_param[v, :, :] = dat[ :pos_param.shape[1],:]
#print (dat)
# print(v, name, ' is v')
v = v +1
posterior = pos_param.transpose(2,0,1).reshape(self.num_param,-1)
path = self.folder +'/posterior/predicted_topo/x_slice/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
list_xslice[v, :, :] = dat[ : list_xslice.shape[1],: ]
v = v +1
list_xslice = list_xslice[:, burnin:, :]
xslice = list_xslice.transpose(2,0,1).reshape(self.real_elev.shape[1],-1)
path = self.folder +'/posterior/predicted_topo/y_slice/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
list_yslice[v, :, :] = dat[ : list_yslice.shape[1],: ]
v = v +1
list_yslice = list_yslice[:, burnin:, :]
yslice = list_yslice.transpose(2,0,1).reshape(self.real_elev.shape[0],-1)
path = self.folder +'/posterior/predicted_topo/sed/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
print(dat.shape, ' dat.shape')
erodep_pts[v, :, :] = dat[ : erodep_pts.shape[1],: ]
v = v +1
erodep_pts = erodep_pts[:, burnin:, :]
erodep_pts = erodep_pts.transpose(2,0,1).reshape(edp_pts_time,-1)
print(erodep_pts.shape, ' ed ***')
path = self.folder +'/performance/lhood/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
likehood_rep[v, : ] = dat[ : likehood_rep.shape[1]]
v = v +1
#likehood_rep = likehood_rep[:, burnin: ]
path = self.folder +'/performance/accept/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
accept_list[v, : ] = dat[ : accept_list.shape[1]]
v = v +1
#accept_list = accept_list[:, burnin: ]
path = self.folder +'/performance/rmse_erdp/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
rmse_erodep[v, : ] = dat[ : rmse_erodep.shape[1]]
v = v +1
rmse_erodep = rmse_erodep[:, burnin: ]
path = self.folder +'/performance/rmse_elev/'
files = os.listdir(path)
v = 0
for name in files:
dat = np.loadtxt(path+name)
rmse_elev[v, : ] = dat[ : rmse_elev.shape[1]]
v = v +1
rmse_elev = rmse_elev[:, burnin: ]
likelihood_vec = likehood_rep
accept_list = accept_list
rmse_elev = rmse_elev.reshape(self.num_chains*(self.NumSamples -burnin ),1)
rmse_erodep = rmse_erodep.reshape(self.num_chains*(self.NumSamples -burnin ),1)
#print( ' .... need print file names --------------------------------------------')
np.savetxt(self.folder + '/pos_param.txt', posterior.T)
np.savetxt(self.folder + '/likelihood.txt', likelihood_vec.T, fmt='%1.5f')
np.savetxt(self.folder + '/accept_list.txt', accept_list, fmt='%1.2f')
return posterior, likelihood_vec, accept_list, xslice, yslice, rmse_elev, rmse_erodep, erodep_pts
def find_nearest(self, array,value): # just to find nearest value of a percentile (5th or 9th from pos likelihood)
idx = (np.abs(array-value)).argmin()
return array[idx], idx
def get_uncertainity(self, likehood_rep, pos_param ):
likelihood_pos = likehood_rep[:,1]
a = np.percentile(likelihood_pos, 5)
lhood_5thpercentile, index_5th = self.find_nearest(likelihood_pos,a)
b = np.percentile(likelihood_pos, 95)
lhood_95thpercentile, index_95th = self.find_nearest(likelihood_pos,b)
max_index = np.argmax(likelihood_pos) # find max of pos liklihood to get the max or optimal pos value
optimal_para = pos_param[:, max_index]
para_5thperc = pos_param[:, index_5th]
para_95thperc = pos_param[:, index_95th]
return optimal_para, para_5thperc, para_95thperc
def plot_figure(self, list, title):
list_points = list
fname = self.folder
size = 15
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.grid(alpha=0.75)
plt.hist(list_points, bins = 20, color='#0504aa',
alpha=0.7)
plt.title("Posterior distribution ", fontsize = size)
plt.xlabel(' Parameter value ', fontsize = size)
plt.ylabel(' Frequency ', fontsize = size)
plt.tight_layout()
plt.savefig(fname + '/pos_plots/' + title + '_posterior.pdf')
plt.clf()
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.grid(alpha=0.75)
listx = np.asarray(np.split(list_points, self.num_chains ))
plt.plot(listx.T)
plt.title("Parameter trace plot", fontsize = size)
plt.xlabel(' Number of Samples ', fontsize = size)
plt.ylabel(' Parameter value ', fontsize = size)
plt.tight_layout()
plt.savefig(fname + '/pos_plots/' + title + '_trace.pdf')
plt.clf()
def plot_sed(self, list, title):
list_points = list
fname = self.folder
size = 15
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.grid(alpha=0.75)
plt.hist(list_points, bins = 20, color='#0504aa',
alpha=0.7)
plt.title("Sediment distribution ", fontsize = size)
plt.xlabel(' Elevation (meters) ', fontsize = size)
plt.ylabel(' Frequency ', fontsize = size)
plt.tight_layout()
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.grid(alpha=0.75)
plt.savefig(fname + '/sed_visual/' + title + '_sed_distri.pdf')
plt.clf()
def heatmap_sed(self, sed_data, title):
size = 15
# plt.imshow(sed_data, cmap='hot', interpolation='nearest')
plt.colorbar()
plt.title("Sediment heatmap ", fontsize = size)
plt.xlabel(' Northings ', fontsize = size)
plt.ylabel(' Eastings ', fontsize = size)
plt.tick_params(labelsize=size)
params = {'legend.fontsize': size, 'legend.handlelength': 2}
plt.rcParams.update(params)
plt.savefig(self.folder+ '/sed_visual/' + title + '_sed_heatmap.pdf')
plt.clf()
def visualize_sediments(self, sediment_timedic):
# print(" sediments visualize .... ")
sediment=sediment_timedic[self.simtime]
# print(sediment, ' sediment grid .')
length = sediment.shape[0]
width = sediment.shape[1]
len_grid = self.len_grid
wid_grid = self.wid_grid
sub_gridlen = 30 #int(length/len_grid) # 25
sub_gridwidth = 30 # int(width/wid_grid) # 25
sed = sediment.copy()
grid = sediment
len_num = 4
wid_num = 4
len_grid = int(sediment.shape[0]/len_num) # take care of left over
wid_grid = int(sediment.shape[1]/wid_num) # take care of left over
i = 0
sed_list = grid.flatten()
def vis_badlands_timestep(self, folder, timestep):
# Load the last time step
file = folder+"/AUSP1306_output/h5"
stepCounter = len(glob.glob1(folder+"/AUSP1306_output/xmf/","tin.time*"))-1
print(stepCounter)
# stepCounter = 50
# Get the elevation, cumulative elevation change, flow discharge, and sea level
# tin,flow,sea = visu.loadStep(folder+"/AUSP1306_output",stepCounter)
# visu.view1Step(folder+"/AUSP1306_output", tin, flow, sea, scaleZ=20, maxZ=2500, maxED=200, flowlines=False)
print ('file : ', file)
strat = strata.stratalSection(file,1)
strat.loadStratigraphy(stepCounter)
strat.loadTIN(stepCounter)
cs=np.zeros((2,2))
cs[0,:] = [2137110.46715,7087591.94151] # point 1
cs[1,:] = [-112889.532847,7087591.94151] # point 2
# Interpolation parameters
nbpts = 500
gfilt = 2
strat.plotSectionMap(folder + '/strat_plots/', title='Topography map', xlegend='Distance (m)', ylegend='Distance (m)',
color=cmo.cm.delta, crange=[-2000,2000], cs=None, size=(6,6))
strat.buildSection(xo = cs[0,0], yo = cs[0,1], xm = cs[1,0], ym = cs[1,1], pts = nbpts, gfilter = gfilt)
strata.viewSection(folder + '/strat_plots/', width = 800, height = 500, cs = strat,
dnlay = 2, rangeX=[2000, 10000], rangeY=[-400,200],
linesize = 0.5, title='Stratal stacking pattern coloured by time')
# Specify the range of water depth for the depositional environments, see the table above
depthID = [0, -25, -100, -200, -500]
# Define colors for depositional environments, with number of colors equals to len(depthID) + 2
colorDepoenvi = ['white','limegreen','darkkhaki','sandybrown','khaki','c','teal']
# 'White' colors where either no deposition or deposited sediemnt thickness < 0.01 m.
# Build an array of depositional environment ID (enviID)
enviID = np.zeros((strat.nz, len(strat.dist)))
enviID = strata.buildEnviID(cs = strat, depthID = depthID)
strata.viewDepoenvi(folder+ '/strat_plots/',width = 8, height = 5, cs = strat, enviID = enviID, dnlay = 2, color = colorDepoenvi,
rangeX=[2000, 12000], rangeY=[-500,100], savefig = 'Yes', figname = 'delta_strata_depoenvi')
start_time = 0. # the start time of the model run [a]
disptime = 50000. # the layer interval of the strata module [a]
end_time = start_time + disptime * timestep # the time of the loaded output [a]
layertime = np.linspace(start_time,end_time,strat.nz) # time of the layers
# Plot Wheeler diagram
# strata.viewWheeler(width = 7, height = 4, cs = strat, enviID = enviID, time = layertime, dnlay = 3, color = colorDepoenvi,
# rangeX=[2000, 12000], rangeY = None, savefig = 'Yes', figname = 'delta_Wheeler_diagram')
# Location of the core on the cross-section (m)
posit = 7000
# Plot the core
strata.viewCore(folder+ '/strat_plots/',width = 2, height = 5, cs = strat, enviID = enviID, posit = posit, time = layertime,
color = colorDepoenvi, rangeX = None, rangeY = None, savefig = 'Yes', figname = 'delta_core')
#---------------------------------------
def vis_badlands_successive(self, folder):
folder = 'case3_aus/output/h5' # output folder path
# Read specific outputs
# outputID = np.array([1, 4, 49, 83, 93, 115, 126, 143, 146, 149]).astype(int) # in Myr or Ma
# Or read outputs with the same time step
nbout = 100 # time index of the last output that will be loaded
nstep = 5 # time step of reading multiple outputs
outputID = np.append(np.arange(0,nbout,nstep),nbout)
outputID[0] = 1 # change the index of the first output from 0 to 1
# Time structure of the model, corresponding to the Time structure in the input.xml file
start_time = 0 # the start time of the model run [a]
dispTime = 5000 # the display time interval [a], can be obtained from the time structure in input.xml file
end_time = start_time + dispTime * nbout # the time of the loaded output [a]
layTime = 2500 # the layer time interval [a], can be obtained from the strata structure in input.xml file
layID = outputID * (dispTime/layTime)
outputTime = start_time + outputID * dispTime
layerTime = start_time + layID * layTime
print ('Loaded output index: '+str(outputID))
print ('Corresponding to the time at: '+str(outputTime)+' years')
# Define an array to store the multiple outputs
strat_all = {}
# Use a for loop to load multiple outputs
k = 0
for i in outputID:
strat_all[k] = strata.stratalSection(folder,1)
strat_all[k].loadStratigraphy(i)
k += 1
# Also load TIN files at the last timestep
strat_all[k-1].loadTIN(i)
cs=np.zeros((2,2))
cs[0,:] = [2137110.46715,7087591.94151] # point 1
cs[1,:] = [-112889.532847,7087591.94151] # point 2
# Interpolation parameters
nbpts = 500
gfilt = 2
# Show the location of the cross-section on the final topography map
strat_all[k-1].plotSectionMap(folder + '/strat_plots/',title='Topography map', color=cmo.cm.delta, colorcs='magenta', crange=[-2000,2000], cs=cs, size=(6,6))
# Build cross-sections
nbcs = len(strat_all) # number of outputs loaded. Build cross-section for each output.
for i in range(0,nbcs):
strat_all[i].buildSection(xo = cs[0,0], yo = cs[0,1], xm = cs[1,0], ym = cs[1,1], pts = nbpts, gfilter = gfilt)
# Visualize the stratal stacking pattern at the last timestep
strata.viewSection(folder + '/strat_plots/',width = 800, height = 500, cs = strat_all[nbcs-1], dnlay = 2,
rangeX=[0, 12000], rangeY=[-700,700], linesize = 0.5, title='Stratal stacking pattern coloured by time')
# Plot the temporal stratal layers
strata.strataAnimate(width = 7, height = 3, cs = strat_all, nstep = nstep, time = layerTime, rangeX = [2000, 11000],
rangeY = [-500,200], folder = 'temporal_strata')
def process_inittopoGMT(self, inittopo_vec, lhood_type):
self.edit_DBF(inittopo_vec, lhood_type)
bashcommand = 'sh ptopo_150.sh %s' %(lhood_type)
process = subprocess.Popen(bashcommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
return
def edit_DBF(self, inittopo_vec, lhood_type): #edit shape file for init topo reconstruction
expert_know = np.loadtxt('init_topo_polygon/dbf_polygon.txt')
# print(expert_know, ' is loaded expert knowledge for Chain %s' %(self.ID))
db = dbf.Dbf("init_topo_polygon/data/%s/Paleotopo_P400.dbf"%(lhood_type))
for i,rec in enumerate(db):
if rec[0] == "Uplands":
rec["ELEVATION"] = (inittopo_vec[i]*(0.25*1500)) + expert_know[i]
rec.store()
del rec
elif rec[0] == "Land unclassified":
rec["ELEVATION"] = (inittopo_vec[i]*(0.25*700)) + expert_know[i]
rec.store()
del rec
elif rec[0] == "Land":
rec["ELEVATION"] = (inittopo_vec[i]*(0.25*600)) + expert_know[i]
rec.store()
del rec
elif rec[0] == "Land erosional":
rec["ELEVATION"] = (inittopo_vec[i]*(0.25*1500)) + expert_know[i]
rec.store()
del rec
else:
pass
# Do Nothing
db.close()
return
def interpolateArray(self,coords=None, z=None, dz=None):
"""
Interpolate the irregular spaced dataset from badlands on a regular grid.
"""
x, y = np.hsplit(coords, 2)
dx = (x[1]-x[0])[0]
nx = int((x.max() - x.min())/dx+1 - 2)
ny = int((y.max() - y.min())/dx+1 - 2)
xi = np.linspace(x.min(), x.max(), nx)
yi = np.linspace(y.min(), y.max(), ny)
xi, yi = np.meshgrid(xi, yi)
xyi = np.dstack([xi.flatten(), yi.flatten()])[0]
XY = np.column_stack((x,y))
tree = cKDTree(XY)
distances, indices = tree.query(xyi, k=3)
if len(z[indices].shape) == 3:
z_vals = z[indices][:,:,0]
dz_vals = dz[indices][:,:,0]
else:
z_vals = z[indices]
dz_vals = dz[indices]
zi = np.average(z_vals,weights=(1./distances), axis=1)
dzi = np.average(dz_vals,weights=(1./distances), axis=1)
onIDs = np.where(distances[:,0] == 0)[0]
if len(onIDs) > 0:
zi[onIDs] = z[indices[onIDs,0]]
dzi[onIDs] = dz[indices[onIDs,0]]
zreg = np.reshape(zi,(ny,nx))
dzreg = np.reshape(dzi,(ny,nx))
return zreg,dzreg
def run_badlands(self, input_vector):
#Runs a badlands model with the specified inputs
print(self.real_elev.shape, ' real evel sh')
rain_regiontime = self.rain_region * self.rain_time # number of parameters for rain based on region and time
#Create a badlands model instance
model = badlandsModel()
if uplift == 1:
xml_id = int(self.ID)
else:
xml_id = 0
print(xml_id, input_vector[11], ' xml_id input_vector[11]')
xmlinput = self.input[xml_id]
#----------------------------------------------------------------
# Load the XmL input file
model.load_xml(str(self.run_nb_str), xmlinput, verbose =False, muted = False)
num_sealevel_coef = 10
if inittopo == 1:
geoparam = num_sealevel_coef + rain_regiontime+13 # note 10 parameter space is for erod, c-marine etc etc, some extra space ( taking out time dependent rainfall)
inittopo_vec = input_vector[geoparam:]
filename=xmlinput.split("/")
problem_folder=filename[0]+"/"+filename[1]+"/"
#Use the coordinates from the original dem file
#Update the initial topography
xi=int(np.shape(model.recGrid.rectX)[0]/model.recGrid.nx)
yi=int(np.shape(model.recGrid.rectY)[0]/model.recGrid.ny)
#And put the demfile on a grid we can manipulate easily