/
dlaClass.py
236 lines (187 loc) · 7.6 KB
/
dlaClass.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
import numpy as np
import joblib
import matplotlib as plt
import matplotlib.image as mpimg
class DLA():
def __init__(self, size, k):
self.size = size
self.state = np.zeros((size, size), dtype = int)
self.state[size / 2, size / 2] = 1
self.xBounds = (size/2, size/2)
self.yBounds = (size/2, size/2)
self.radius = 0
self.xcenter = size/2
self.ycenter = size/2
self.k = k
def printState(self):
'''
Uses matplotlib to display current image inline
'''
plt.pyplot.imshow(1 - self.state, cmap='gray')
def writeState(self, fileName):
'''
Dumps np matrix to fileName
'''
with open(fileName, 'w') as fp:
joblib.dump(self.state, fp)
def loadState(self, fileName):
'''
Loads np matrix from fileName
'''
with open(fileName, 'r') as fp:
self.state = joblib.load(fp)
def getSeed(self):
'''
Returns a randomly sampled initial position
for a particle.
Returns (x, y) tuple
'''
boundingCircle = self.getBoundingCircle()
p = np.random.randint(len(boundingCircle))
return boundingCircle[p]
def isValid(self, curr):
'''
Checks wether (x, y) is in grid
Returns True/False
'''
x, y = curr
return x > -1 and x < self.size and y > -1 and y < self.size
def getAdjacentPoints(self, curr):
'''
Returns points adjacent to curr within image bounds
Assumption : Adjacent includes diagonal neighbors (max 8)
A|A|A
A|X|A
A|A|A
Input args:
curr : (x, y) tuple
Returns:
List of adjacent points
'''
x, y = curr
adjacentPoints = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1),
(x, y - 1), (x, y + 1),
(x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
# Remove points outside the image
adjacentPoints = filter(lambda x : x[0] > -1 and x[0] < self.size and \
x[1] > -1 and x[1] < self.size, adjacentPoints)
return adjacentPoints
def getBoundingCircle(self):
'''
Gets bounding circle of current image
Returns list of points in bounding circle
'''
points = {}
y1, y2 = self.ycenter, self.ycenter
for x in range(self.xcenter - self.radius - 10, self.xcenter + 1):
while (y1 - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius)**2:
y1 -= 1
k = y1
while (k - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius + 1)**2:
if self.isValid((x, k)):
points[(x, k)] = True
k -= 1
while (y2 - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius)**2:
y2 += 1
k = y2
while (k - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius + 1)**2:
if self.isValid((x, k)):
points[(x, k)] = True
k += 1
y1, y2 = self.ycenter, self.ycenter
for x in range(self.xcenter + self.radius + 10, self.xcenter, -1):
while (y1 - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius)**2:
y1 -= 1
k = y1
while (k - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius + 1)**2:
if self.isValid((x, k)):
points[(x, k)] = True
k -= 1
while (y2 - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius)**2:
y2 += 1
k = y2
while (k - self.ycenter) ** 2 + (x - self.xcenter) ** 2 <= (self.radius + 1)**2:
if self.isValid((x, k)):
points[(x, k)] = True
k += 1
if any(map(lambda x : self.state[x] == 1, points.keys())):
print 'can spawn at marked'
return points.keys()
def checkIfTerminate(self, curr):
'''
Check if curr sticks in image.
Will happen with prob k if any adjacent block is 1
Input args:
curr : (x, y) tuple
Returns True/False
'''
adjacentPoints = self.getAdjacentPoints(curr)
return any(map(lambda x : self.state[x] == 1, adjacentPoints)) and np.random.rand() < self.k
def getNextPosition(self, curr):
'''
Get next point. Brownian motion is order 1 markov process
Input args:
curr : (x, y) tuple
Returns:
(x, y) coordinate of next position
'''
adjacentPoints = self.getAdjacentPoints(curr) # List of adjacent points
adjacentPoints = filter(lambda x : self.state[x] == 0, adjacentPoints)
s = np.random.randint(len(adjacentPoints)) # Get random point
return adjacentPoints[s]
def getSurfaceArea(self):
'''
Get surface area of all points.
Presently O(m**2), can be done better
Returns integer
'''
area = 0
for i in range(self.size):
for j in range(self.size):
if self.state[i, j] == 1:
adj = self.getAdjacentPoints((i, j))
adj = filter(lambda x : self.state[x] == 0, adj)
area += len(adj)
return area
def getNeighbourCount(self):
'''
For each cell with 1, count neghbouring cells with 1
Returns integer
'''
count = 0
for i in range(self.size):
for j in range(self.size):
if self.state[i, j] == 1:
adj = self.getAdjacentPoints((i, j))
adj = filter(lambda x : self.state[x] == 1, adj)
count += len(adj)
return count
def addPoint(self, numPoints = 1):
'''
Adds a new particle to the matrix
'''
count = 0
while count < numPoints:
if (count + 1) % 100 == 0:
print count
curr = self.getSeed() # Get initial position
while not self.checkIfTerminate(curr):
curr = self.getNextPosition(curr)
if (curr[0] - self.xcenter) ** 2 + (curr[1] - self.ycenter) ** 2 > (self.radius + 15) ** 2:
curr = self.getSeed() # Go back to random point in bounding circle
self.state[curr] = 1
# Update bounds
x, y = curr
xmin, xmax = self.xBounds
xmin = min(xmin, x)
xmax = max(xmax, x)
self.xBounds = (xmin, xmax)
ymin, ymax = self.yBounds
ymin = min(ymin, y)
ymax = max(ymax, y)
self.yBounds = (ymin, ymax)
# Calculate new radius
self.xcenter = (xmax + xmin) / 2
self.ycenter = (ymax + ymin) / 2
self.radius = int(((xmax - self.xcenter) ** 2 + (ymax - self.ycenter) ** 2) ** 0.5) + 1
count += 1