-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimize.py
executable file
·283 lines (218 loc) · 6.33 KB
/
optimize.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
#!/usr/bin/env python2.7
print
print '# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #'
print '# #'
print '# Nonlinear 3-dimentional inversion for fault geometry #'
print '# and slip through the entire seismic cycle #'
print '# #'
print '# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #'
import numpy as np
#from numpy.lib.stride_tricks import as_strided
import math
from os import path, environ
from sys import argv,exit,stdin,stdout
import getopt
import time
import scipy.optimize as opt
from pylab import *
#import multiprocessing
import pymc
def usage():
print ''
print 'optimize.py : Nonlinear 3-dimentional inversion of fault geometry and associated'
print 'slip rates throught the entire seismic cycle'
print 'usage: optmize.py input_file'
print ''
print
print "---------------------------------------------------------------------------"
print 'Read input file'
print "---------------------------------------------------------------------------"
print
try:
opts,args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.GetoptError, err:
print str(err)
print "for help use --help"
sys.exit()
for o in opts:
if o in ("-h","--help"):
usage()
sys.exit()
else:
assert False, "unhandled option"
sys.exit()
if 1 == len(argv):
usage()
assert False, "no input file"
elif 2 == len(argv):
fname = sys.argv[1]
print
print 'input file :', fname
sys.path.append(path.dirname(path.abspath(fname)))
exec ("from " + path.basename(fname)+ " import *")
else:
assert False, "too many arguments"
# Create directories for output files
inv.outdir = outdir
if not os.path.exists(inv.outdir):
os.makedirs(inv.outdir)
outgps = inv.outdir+'gps/'
if not os.path.exists(outgps):
os.makedirs(outgps)
outinsar = inv.outdir+'insar/'
if not os.path.exists(outinsar):
os.makedirs(outinsar)
outmap = inv.outdir+'map/'
if not os.path.exists(outmap):
os.makedirs(outmap)
outpro = inv.outdir+'profiles/'
if not os.path.exists(outpro):
os.makedirs(outpro)
outstat = inv.outdir+'stat/'
if not os.path.exists(outstat):
os.makedirs(outstat)
outwave = inv.outdir+'wave/'
if not os.path.exists(outwave):
os.makedirs(outwave)
inv = inversion(
kernels=kernels,
basis=basis,
timeseries=timeseries,
stacks=stacks,
seismo=seismo,
profiles=profiles,
store_path=store_path,
store=store,
gmtfiles=gmtfiles,
outdir=outdir,
bounds=bounds,
ref=reference,
)
# build data matrix
inv.d = inv.build_data()
# sys.exit()
#load kernels and build priors
print inv.info()
for i in xrange(inv.Mker):
for j in xrange(inv.kernels[i].Mseg):
inv.kernels[i].segments[j].build_prior()
print inv.kernels[i].segments[j].info()
# build prior model
m_init = inv.build_prior()
### TESTING ###
inv.build_gm()
inv.residual()
# # plots
# nfigure=0
# inv.plot_stations(nfigure)
# nfigure=2
# inv.plot_traces(nfigure)
# nfigure=10
# inv.plot_ts_GPS(nfigure)
# nfigure=20
# inv.plot_InSAR_maps(nfigure)
# nfigure=40
# plt.show()
# # inv.plot_snuffler()
# sys.exit()
print
print "---------------------------------------------------------------------------"
print ' Optimization'
print "---------------------------------------------------------------------------"
print
print 'Lenght of the data vector:', len(inv.d)
print 'Number of Free parameters:', len(inv.priors)
print
print 'Optmized parameters:'
bnd=column_stack((inv.mmin,inv.mmax))
for i in xrange(len(bnd)):
print 'bounds for parameter {}: {}'.format(inv.sampled[i],bnd[i])
print
if short_optim:
t = time.time()
# res = opt.minimize(inv.residualscalar,inv.priors,method='SLSQP',bounds=bnd)
# res = opt.minimize(inv.residualscalar,inv.priors,method='L-BFGS-B',bounds=bnd)
# res = opt.fmin_slsqp(inv.residualscalar,inv.priors,bounds=bnd)
res = opt.differential_evolution(inv.residualscalar, bounds=bnd,maxiter=niter,polish=False,disp=True)
elapsed = time.time() - t
print
print "Time elapsed:", elapsed
print
print res
if bayesian:
stochastic = pymc.Normal('Data',
mu = pymc.Deterministic(eval = inv.foward,
name = 'Foward model',
parents = {'theta': inv.priors},
doc = 'Deterministic function',
verbose = 0,
plot=True),
tau = inv.Cov(),
value = inv.build_data(),
observed = True,
)
# Parameters = locals()
Parameters = pymc.Model(inv.priors)
model = pymc.MCMC(Parameters)
if MAP:
map_ = pymc.MAP(Parameters)
map_.fit()
t = time.time()
for p in inv.priors:
# model.use_step_method(pymc.Metropolis, p)
model.use_step_method(pymc.AdaptiveMetropolis, p)
model.sample(iter = niter, burn = nburn, thin=1)
elapsed = time.time() - t
print
print "Time elapsed:", elapsed
print
# with model:
# foward = inv.foward(inv.priors)
# stochastic = pymc.Normal(
# 'Data',
# mu = foward,
# sd = inv.Cov(),
# observed = inv.build_data(),
# )
# if MAP:
# map_estimate = pymc.find_MAP(model=model)
# print map_estimates
# sys.exit()
# t = time.time()
# trace = pm.sample(iter = 1000, burn = 500,progressbar=True)
# elapsed = time.time() - t
# print
# print "Time elapsed:", elapsed
# print
# pymc.summary(trace)
# _ = pymc.traceplot(trace,format = 'eps')
print
print "---------------------------------------------------------------------------"
print ' Posterior model'
print "---------------------------------------------------------------------------"
print
# compute residual for plots
inv.residual()
# print results
for i in xrange(inv.Nmanif):
print 'Network:', inv.manifolds[i].network
inv.manifolds[i].printbase()
print
for i in xrange(inv.Mker):
for j in xrange(inv.kernels[i].Mseg):
print inv.kernels[i].segments[j].info()
# plot results
nfigure=0
inv.plot_stations(nfigure)
nfigure=2
inv.plot_traces(nfigure)
nfigure=10
inv.plot_ts_GPS(nfigure)
nfigure=20
inv.plot_InSAR_maps(nfigure)
nfigure=40
if bayesian:
for i in xrange(len(inv.faults)):
pymc.Matplot.plot(model.trace(inv.faults[i][:]),format = 'eps',path = outstat)
plt.show()
inv.plot_snuffler()