-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_script.py
164 lines (126 loc) · 4.59 KB
/
run_script.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
# %%
import sys
# local dir
sys.path.append('/Volumes/GoogleDrive/My Drive/GemPhy/GP_old/')
sys.path.append('/Volumes/GoogleDrive/My Drive/')
# cluster dir
sys.path.append('/home/ib012512/Documents/GemPhy/GP_old')
sys.path.append('/home/ib012512/Documents/')
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
# %%
from GemPhy.Geophysics.utils.util import constant64,dotdict
from gempy.assets.geophysics import Receivers
from GemPhy.Geophysics.utils.ILT import *
from gempy.core.grid_modules.grid_types import CenteredRegGrid
# import gempy as gp
# from gempy.core.tensor.modeltf_var import ModelTF
from UQ import UQ_Patua
from operator import add
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
from Patua import PutuaModel
from LoadInputDataUtility import loadData
#########################
# %%
args = dotdict({
"foldername": "Patua",
'resolution':[15,15,10],
'num_data': 50,
})
Bayesargs = dotdict({
'prior_sfp_std': 50,
'prior_den_std': 0.2,
'likelihood_std': 2,
# 'likelihood_std':0.09, #the gravity data has an error range approximately between 0.5 mGal to 2.5 mGal. - Pollack, A, 2021
})
MCMCargs = dotdict({
'RMH':False,
'HMC':False,
'NUTS':True,
'num_results': 2,
'number_burnin':0,
'RMH_step_size': 0.2,
'HMC_step_size': 0.01,
'leapfrogs':4,
})
# %%
# Load the data
P_model = PutuaModel()
init_model = P_model.init_model()
# %%
init_model.compute_model()# TODO: Check if necessary. precompute the model to order the surfaces
ObsData = loadData(P_model.P, number_data = args.num_data)
Data_obs = P_model.P['Grav']['Obs'] - (np.mean(P_model.P['Grav']['Obs']))
Data_measurement = tf.cast(Data_obs,init_model.dtype)
# Define the receivers for gravity
model_extent = [None]*(6)
model_extent[::2] = P_model.P['xy_origin']
model_extent[1::2] = list( map(add, P_model.P['xy_origin'], P_model.P['xy_extent'] ) )
X_r = P_model.P['Grav']['xObs']
Y_r = P_model.P['Grav']['yObs']
Z_r = [model_extent[-1]]*P_model.P['Grav']['nObsPoints']
xyz = np.stack((X_r,Y_r,Z_r)).T
radius = [1000,1000,2000]
receivers = Receivers(radius,model_extent,xyz,kernel_resolution = args.resolution)
Reg_kernel = CenteredRegGrid(receivers.xy_ravel,radius=receivers.model_radius,resolution=receivers.kernel_resolution)
# %%
all_points = init_model.surface_points.df[['X','Y','Z']].to_numpy()
df = init_model.geo_data.surface_points.df
num_fault_points = len(df[df['surface'].str.startswith('fault')])
num_intrusion_points = len(df[df['surface'] == 'intrusion'])
num_GT_points = len(df[df['surface'] == 'Volconic_felsic'])
num_fix_points = num_fault_points + num_intrusion_points + num_GT_points # keep all the intrusion, faults and GT points fixed
############################################
# Define the statistic problem
############################################
# Be very careful here
fix_points = tf.concat([all_points[:num_intrusion_points+num_fault_points],all_points[-num_GT_points:]],axis = 0)
static_xy = all_points[:,0:2]
strata_points = all_points[num_intrusion_points+num_fault_points:-num_GT_points]
all_points_shape = all_points.shape
num_sf_var = strata_points.shape[0]
sfp_mean = strata_points[:,2]
sfp_std = constant64([Bayesargs.prior_sfp_std]*num_sf_var)
num_den_var = 5
den_mean = constant64([2.9,2.1,2.2,2.3,2.8])
den_std = constant64([0.2,0.17,0.1,0.14,0.1])
prior_mean = tf.concat([sfp_mean,den_mean],axis = 0)
prior_std = tf.concat([sfp_std,den_std],axis = 0)
num_para_total = prior_mean.shape[0]
### Define the bounds for parameters, bounds has to be normalized first
lowerBound = prior_mean - 3*prior_std
upperBound = prior_mean + 3*prior_std
# invertible logarithmic transform
ilt = ILT(lowerBound,upperBound)
# %%
uq_P = UQ_Patua(gp_model = init_model,
Reg_kernel = Reg_kernel,
receivers = receivers,
transformer = ilt,
num_para_total = num_para_total,
delta = 2.,
fix_points = fix_points,
static_xy = static_xy,
Data_Obs = Data_measurement,
args = args,
Bayesargs = Bayesargs,
num_fault_points = num_fault_points,
num_intrusion_points = num_intrusion_points,
num_GT_points = num_GT_points,
)
# %%
# mu = ilt.transform(prior_mean)
# %%
# uq_P.forward_function(mu)
# %%
# uq_P.stat_model.log_likelihood(mu)
# %%
mu_list = uq_P.stat_model.mvn_prior.sample(5)
uq_P.set_initial_status(mu_list)
if __name__ == '__main__':
# uq_P.forward_function(mu)
uq_P.run_mcmc(MCMCargs)
# %%