/
runmcmc.py
289 lines (258 loc) · 9.77 KB
/
runmcmc.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
"""
An code for running MCMC simulation to determine the confidence range of tempo parameters. Code in use:
runmcmc.py : the main driving program
plotmc.py : the plotting program
ProgressBar.py : for plotting the progress bar
"""
from tempo import tempofit, tempo2fit, touchparfile, uniquename, PARfile #, model, TOAfile
from math import *
from decimal import *
import os
from copy import *
from numpy.random import normal , uniform ,seed
import numpy as np
import time
from multiprocessing import Pool, Process, Manager
def randomnew(pf, stepsize): #special for 1713
twopi = 6.283185307179586
fac = 1.536e-16 * 1.e12
x = float(str(pf.A1[0]))
sini = float(str(pf.SINI[0]))
cosi = -1. * np.sqrt(1 - sini**2)
Omega = float(str(pf.PAASCNODE))
m2 = float(str(pf.M2[0])) + normal(0,0.05*stepsize)
cosi = cosi + uniform(-0.5, 0.5, 1)[0]*0.001*stepsize #normal(0, 0.001*stepsize)
Omega = Omega + normal(0, 4.0*stepsize)
mu = np.sqrt(float(str(pf.PMRA[0]**2+pf.PMDEC[0]**2)))
#print 'mu:', mu
#sini = sqrt(1 - cosi**2)
thetamu = 180. + np.arctan(float(str(pf.PMRA[0]/pf.PMDEC[0])))/np.pi*180
xdot = -1.* fac * x * mu * (cosi/sini) * sin((thetamu-Omega)*twopi/360.)
sini = np.sqrt(1 - cosi**2)
pf.SINI[0] = Decimal(str(sini))
pf.XDOT[0] = Decimal(str(xdot))
pf.PAASCNODE = Decimal(str(Omega))
pf.M2[0] = Decimal(str(m2))
#print np.arcsin(sini)*180/np.pi, Omega, thetamu, xdot
return pf
def probcal(pf):
global smallestchisq
pf.write()
#m2 = float(str(pf.M2[0]))
#Omega = float(str(pf.PAASCNODE))
#sini = float(str(pf.SINI[0]))
#if m2 <= 0 or Omega > 360 or Omega < -360 or sini > 1.:
#return 0
chisq, dof = tempofit(parfile, toafile = toafile, pulsefile = pulsefile)
pf.chisq = chisq
#print dof, chisq
if chisq < smallestchisq: smallestchisq = chisq
try:
#return exp((smallestchisq - chisq)/2.) #Ingrid/Paul?
return (smallestchisq - chisq)/2. #Ingrid/Paul?
except OverflowError:
print chisq, smallestchisq
print pf.parfile
raise OverflowError
#print probcal(90, 0.25, 0.28)
from itertools import count
import cPickle as pickle
import os,sys
from tempfile import mkdtemp
#print probcal(iOmega, icosi, im2)
manager = Manager()
class MChain(object):
def __enter__(self):
self.Chain = []
self.cwd = os.getcwd()
return self
def __exit__(self, exc_type, exc_value, exc_tb):
os.chdir(self.cwd)
try:
os.remove(self.cwd+'/MChain.p'+str(os.getpid()))
except:pass
if exc_type is KeyboardInterrupt:
print '\nManually Stopped\n'
return True
else:
return exc_type is None
print '\nFinish running\n'
def save(self):
try:
MarkovChain.extend(self.Chain)
self.Chain = []
except IOError:
MarkovChain = self.Chain
except EOFError:
print 'encounter EOFError here', os.getpid(), cwd
#time.sleep(10)
self.save()
return
if len(MarkovChain)>2:
if len(MarkovChain[-1]) < len(MarkovChain[-2]):
MarkovChain = MarkovChain[:-1]
dit = {'Chain':np.array(MarkovChain)}
f = open(self.cwd+'/MChain.p'+str(os.getpid()), 'wb', 0)
pickle.dump(dit, f, protocol=2)
f.flush()
f.close()
del dit
#print 'saved per 100 points', len(MarkovChain)
def motifile(file, cwd, tmpdir):
os.system('cp %s/%s %s/%s' % (cwd, file, tmpdir, file))
text = ''
f = open(file, 'rw')
for l in f.readlines():
if not l.find('INCLUDE') == -1:
a = l.split()
if a[0] == 'C' or a[0] =='#':
continue
if not open(cwd+'/'+a[1],'r').read().find('INCLUDE') == -1:
motifile(a[1], '..', '.')
l = a[0] +' '+a[1]
else:
l = a[0] + ' '+cwd+'/'+a[1]
if not l[-1] == '\n':
l += '\n'
text += l
else:
if not l[-1] == '\n':
l += '\n'
text += l
f.close()
f = open(file, 'w')
f.write(text)
f.close() #motify the tim file to make sure INCLUDE follow the right files.
from ProgressBar import progressBar
def mcmc(Chain, runtime, MarkovChain, mixingtime=1000, stepsize=1, seed=0 ):
pb = progressBar(maxValue = runtime + mixingtime)
cwd=os.getcwd()
tmpdir = cwd+'/.'+uniquename()
if not tmpdir == None:
if os.path.exists(tmpdir):
os.chdir(tmpdir)
else:
os.mkdir(tmpdir)
os.chdir(tmpdir)
os.system('cp %s/%s %s/%s' % (cwd, parfile, tmpdir, parfile))
os.system('cp %s/%s %s/%s' % (cwd, pulsefile, tmpdir, pulsefile))
motifile(toafile, cwd, tmpdir)
touchparfile(parfile, NITS=1)
pf = PARfile(parfile)
#chisq, dof = tempofit(parfile, toafile = toafile, pulsefile = pulsefile)
pf.matrix(toafile)
pf.freezeall()
#pf.thawall('JUMP_')
#pf.write()
#plist = [x for x in pf.manifest if x in pf.parameters.keys() ]
#dit = {'BEST':[pf.__dict__[p][0] for p in plist] + [ chisq], 'parfile':pf.parfile, 'parameters':plist + [ 'chisq']}
#pickle.dump(dit, open('%s/bestpar.p' % cwd, 'w', 0), protocol=2)
p0 = probcal(pf)
pmax = p0
ThisChain = []
c = 0
randomlist = uniform(0,1,size=runtime)
while c <= mixingtime + runtime - 1:
c+=1
npf = pf.randomnew(stepsize=stepsize)
#randomnew(npf, stepsize) #only use this for 1713
p1 = probcal(npf)
if c % 30 == 0:pb(c)
if c > mixingtime:
t = randomlist[c-mixingtime-1]
if t < exp(p1-p0):
Chain.Chain.append([npf.__dict__[p][0] for p in plist] + [ npf.chisq])
pf = npf
p0 = p1
if p1 > pmax:
pmax = p1
bestpar['BEST'] = [npf.__dict__[p][0] for p in plist] + [ npf.chisq]
pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'wb', 0), protocol=2)
else:
Chain.Chain.append([pf.__dict__[p][0] for p in plist] + [ npf.chisq])
if c % (100+(seed%100)) == 0:
MarkovChain.extend(Chain.Chain)
ThisChain.extend(Chain.Chain)
Chain.Chain = [] #empty the list
#try:
TC = np.array(ThisChain)
dit = {'Chain':TC}
pid = str(os.getpid())
try:
os.remove(cwd+'/MChain.p'+pid)
except:pass
f = open(cwd+'/MChain.p'+pid, 'wb', 0)
pickle.dump(dit, f, protocol=2)
f.flush()
f.close()
del dit
del TC
MarkovChain.extend(Chain.Chain)
os.chdir(cwd)
from optparse import OptionParser
if __name__ == '__main__':
#main()
usage = "usage: %prog [options] arg"
parser = OptionParser()
parser.add_option("-f", '--parfile', dest="parfile", help="par file")
parser.add_option("-t", '--timfile', dest="toafile", help="toa file")
parser.add_option("-n", '--pulsefile', dest="pulsefile", help="pulse number file", default=None)
parser.add_option("-i", '--iter', type='int', nargs=1, dest='steps', help="number of steps")
parser.add_option("-m", '--mixing', type='int', nargs=1, dest='mixing', help="number of mixing steps", default=1000)
parser.add_option("-p", '--parallel', type='int', nargs=1, dest='paral', help="number of parallel processes")
parser.add_option("-s", '--seed', type='int', nargs=1, dest='seed', default=int(os.getpid()), help="random number seed")
parser.add_option("-z", '--stepsize', type='float', nargs=1, dest='stepsize', default=1., help="step size")
(options, args) = parser.parse_args(args=sys.argv[1:])
print options
parfile = options.parfile
toafile = options.toafile
pulsefile = options.pulsefile
steps = options.steps
mixing = options.mixing
rseed = options.seed
stepsize = options.stepsize
px = options.paral
pf =PARfile(parfile)
#pf = model(parfile)
pf.freezeall()
pf.thawall('JUMP_')
pf.write('mcmc.par')
touchparfile('mcmc.par', NITS=1)
chisq, dof = tempofit('mcmc.par', toafile = toafile, pulsefile = pulsefile)
#pf.tempofit(TOAfile(toafile), pulsefile = pulsefile)
smallestchisq = chisq
#print 'smallestchisq', smallestchisq, dof
cwd=os.getcwd()
plist = [x for x in pf.manifest if x in pf.parameters.keys() if not x.startswith('DMX') and not x.startswith('JUMP')]
bestpar = {'BEST':[pf.__dict__[p][0] for p in plist] + [chisq], 'parfile':pf.parfile, 'parameters':plist + ['chisq']}
pickle.dump(bestpar, open('%s/bestpar.p' % cwd, 'w', 0), protocol=2)
def run(argv):
s, MarkovChain = argv
seed(s) # assigning different initial seed for the random number generator in different threads.
#steps = 50000
with MChain() as Chain:
#print type(Chain)
#print steps, tmpdir
mcmc(Chain, steps, MarkovChain, mixingtime=mixing, stepsize=stepsize, seed=s)
return Chain
MarkovChain = manager.list()
cwd = os.getcwd()
if px == None:
run([rseed, MarkovChain])
else:
px = options.paral
p = Pool(px)
p.map(run, [(rseed+100*s, MarkovChain) for s in range(px)])
#print 'generated %s pints' % len(MarkovChain)
try:
f = open('./MChain.p', 'rb')
oldChain = list(pickle.load(f)['Chain'])
f.close()
#oldChain.extend(MarkovChain)
MarkovChain = oldChain + list(MarkovChain)
except IOError:pass
dit = {'Chain':np.array(MarkovChain)}
f = open(cwd+'/MChain.p', 'wb')
pickle.dump(dit, f, protocol=2)
#f.flush()
f.close()