-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSamplerStrips.py
More file actions
executable file
·221 lines (204 loc) · 10.7 KB
/
Copy pathtreeSamplerStrips.py
File metadata and controls
executable file
·221 lines (204 loc) · 10.7 KB
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
"""treeSampler.py
Sample from 2D constrained channel using tree sampling + larger blocks/strips.
"""
from optparse import OptionParser
import numpy as np
from scipy import misc
from numpy.random import random_sample
import time
def main():
# Parse command-line arguments
parser = OptionParser()
parser.add_option("--nx", type=int, help="number of rows")
parser.add_option("--ny", type=int, help="number of columns")
parser.add_option("--iter", type=int, help="iterations to run")
parser.add_option("--chains", type=int, help="number of independent chains")
parser.add_option("--stripWidth", type=int, help="width of the strips")
(args, options) = parser.parse_args()
# Initialize arrays/matrices
nx = args.nx
ny = args.ny
nrChains = args.chains
stripWidth = args.stripWidth
iterNr = args.iter
output = np.zeros( (nrChains, nx, ny) )
fileName = str(nx) + 'x' + str(ny) + 'TSstripw' + str(stripWidth) + '.txt'
# Block structure
index = np.arange(ny)
nrInd1 = int(np.ceil(float(ny)/float(stripWidth)/2))
nrInd2 = ny/stripWidth - nrInd1
#print nrInd1, nrInd2
block1 = np.zeros( (nrInd1,stripWidth), dtype=np.uint64 )
block2 = np.zeros( (nrInd2,stripWidth), dtype=np.uint64 )
temp = 0
for iIndex in range(nrInd1):
block1[iIndex,:] = index[stripWidth*iIndex+temp:stripWidth*(iIndex+1)+temp]
temp = temp + stripWidth
temp = stripWidth
for iIndex in range(nrInd2):
block2[iIndex,:] = index[stripWidth*iIndex+temp:stripWidth*(iIndex+1)+temp]
temp = temp + stripWidth
#print block1, block2
# Interaction potentials
intPot = np.ones( (2,2), dtype=np.uint64 )
intPot[1,1] = 0
nrComb = 2**stripWidth
xDomain = np.zeros((nrComb,stripWidth), dtype=np.uint64)
for iIter in range(nrComb):
tempStr = np.binary_repr(iIter, width=stripWidth)
for iStrip in range(stripWidth):
xDomain[iIter, iStrip] = int(tempStr[iStrip])
#nrDimArray = '(' + '2,'*stripWidth + str(nx) + ')'
#print xDomain
# Create file
f = open(fileName, 'w')
f.write('nx ny\n')
f.write(str(nx) + ' ' + str(ny) + '\n')
f.write('iterNr timeElapsed zA zB\n')
f.close()
# Main loop
for iIter in np.arange(iterNr):
print iIter
startTS = time.time()
fMarginalA = np.zeros( (nrChains, nrInd1) )
fMarginalB = np.zeros( (nrChains, nrInd2) )
for iChain in range(nrChains):
# --------------
# First tree
# --------------
#print output[iChain,:,:].reshape( (nx,ny) )
for iBlock in range(nrInd1):
messages = np.ones( (nrComb, nx-1) )
normConstMessages = np.zeros( nx )
# Forward filtering
for iRow in range(nx-1):
for iCur in range(nrComb):
tempDist = np.ones(nrComb)
for iPrev in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iPrev, iStrip+1]]
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iCur, iStrip]]
tempDist[iPrev] *= intPot[xDomain[iPrev,stripWidth-1], xDomain[iCur, stripWidth-1]]
if block1[iBlock, 0] > 0:
tempDist[iPrev] *= intPot[xDomain[iPrev,0], output[iChain, iRow, block1[iBlock, 0]-1]]
if block1[iBlock, stripWidth-1] < ny-1:
tempDist[iPrev] *= intPot[xDomain[iPrev,-1], output[iChain, iRow, block1[iBlock, stripWidth-1]+1]]
if iRow > 0:
tempDist[iPrev] *= messages[iPrev, iRow-1]
messages[iCur,iRow] = np.sum(tempDist)
normConstMessages[iRow] = np.sum(messages[:,iRow])
messages[:,iRow] = messages[:,iRow] / normConstMessages[iRow]
# Column sum
tempDist = np.ones(nrComb)
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if block1[iBlock, 0] > 0:
tempDist[iCur] *= intPot[xDomain[iCur,0], output[iChain, nx-1, block1[
iBlock, 0]-1]]
if block1[iBlock, -1] < ny-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain, nx-1, block1[iBlock, -1]+1]]
tempDist[iCur] *= messages[iCur, nx-2]
normConstMessages[-1] = np.sum( tempDist )
fMarginalA[iChain,iBlock] = np.sum( np.log(normConstMessages) )
#raw_input()
# Backward sampling
for iRow in range(nx)[::-1]:
tempDist = np.ones( nrComb )
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], output[iChain,iRow+1, block1[iBlock, iStrip]]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain,iRow+1, block1[iBlock, stripWidth-1]]]
if block1[iBlock, 0] > 0:
tempDist[iCur] *= intPot[xDomain[iCur,0], output[iChain, iRow, block1[iBlock, 0]-1]]
if block1[iBlock, stripWidth-1] < ny-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain, iRow, block1[iBlock, stripWidth-1]+1]]
if iRow > 0:
tempDist[iCur] *= messages[iCur, iRow-1]
tempDist = tempDist / np.sum(tempDist)
curInd = discreteSampling( tempDist, range(nrComb), 1 )
for iStrip in range(stripWidth):
output[iChain,iRow,block1[iBlock, iStrip]] = xDomain[curInd,iStrip]
# --------------
# Second block
# -------------
for iBlock in range(nrInd2):
messages = np.ones( (nrComb, nx-1) )
normConstMessages = np.zeros( nx )
# Forward filtering
for iRow in range(nx-1):
for iCur in range(nrComb):
tempDist = np.ones(nrComb)
for iPrev in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iPrev, iStrip+1]]
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iCur, iStrip]]
tempDist[iPrev] *= intPot[xDomain[iPrev,-1], xDomain[iCur, -1]]
if block2[iBlock, 0] > 0:
tempDist[iPrev] *= intPot[xDomain[iPrev,0], output[iChain, iRow, block2[iBlock, 0]-1]]
if block2[iBlock, -1] < ny-1:
tempDist[iPrev] *= intPot[xDomain[iPrev,-1], output[iChain, iRow, block2[iBlock, -1]+1]]
if iRow > 0:
tempDist[iPrev] *= messages[iPrev, iRow-1]
messages[iCur,iRow] = np.sum(tempDist)
normConstMessages[iRow] = np.sum(messages[:,iRow])
messages[:,iRow] = messages[:,iRow] / normConstMessages[iRow]
#print messages[:,iRow]
tempDist = np.ones(nrComb)
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if block2[iBlock, 0] > 0:
tempDist[iCur] *= intPot[xDomain[iCur,0], output[iChain, nx-1, block2[iBlock, 0]-1]]
if block2[iBlock, -1] < ny-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain, nx-1, block2[iBlock, -1]+1]]
tempDist[iCur] *= messages[iCur, nx-2]
normConstMessages[-1] = np.sum( tempDist )
fMarginalB[iChain,iBlock] = np.sum( np.log(normConstMessages) )
#print fMarginalB[iChain,iBlock]
#raw_input()
# Backward sampling
for iRow in range(nx)[::-1]:
tempDist = np.ones( nrComb )
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], output[iChain,iRow+1, block2[iBlock, iStrip]]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain,iRow+1, block2[iBlock, -1]]]
if block2[iBlock, 0] > 0:
tempDist[iCur] *= intPot[xDomain[iCur,0], output[iChain, iRow, block2[iBlock, 0]-1]]
if block2[iBlock, -1] < ny-1:
tempDist[iCur] *= intPot[xDomain[iCur,-1], output[iChain, iRow, block2[iBlock, -1]+1]]
if iRow > 0:
tempDist[iCur] *= messages[iCur, iRow-1]
tempDist = tempDist / np.sum(tempDist)
curInd = discreteSampling( tempDist, range(nrComb), 1 )
for iStrip in range(stripWidth):
output[iChain,iRow, block2[iBlock, iStrip]] = xDomain[curInd,iStrip]
#print fMarginal
#raw_input()
#print output[0,:,:].reshape( (nx,ny) )
#raw_input()
fMarginal = np.zeros( (nrChains,nrInd1+nrInd2) )
fMarginal[:,:len(block1)] = fMarginalA
fMarginal[:,len(block1):len(block1)+len(block2)] = fMarginalB
f = open(fileName, 'a')
f.write(str(iIter) + ' ' + str(time.time()-startTS) + ' ')
np.savetxt(f, fMarginal.reshape( (1,nrChains*(nrInd1+nrInd2)) ) )
f.close()
def discreteSampling(weights, domain, nrSamples):
bins = np.cumsum(weights)
return domain[np.digitize(random_sample(nrSamples), bins)]
def ravel_multi_index(coord, shape):
return coord[0] * shape[1] + coord[1]
def unravel_index(coord, shape):
iy = np.remainder(coord, shape[1])
ix = (coord - iy) / shape[1]
return ix, iy
if __name__ == "__main__":
main()