-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering.py
358 lines (258 loc) · 10.4 KB
/
clustering.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import matplotlib
# matplotlib.use('GTKAgg')
import numpy as np
import matplotlib.pyplot as plt
import subprocess
import argparse
import time
import sys
from mpl_toolkits.mplot3d import Axes3D
sys.setrecursionlimit(50000)
def system(cmd, verbose=1):
"""Run system command cmd using subprocess module."""
if verbose >= 1:
print(cmd)
try:
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
if output:
if verbose >= 2:
print(output)
except subprocess.CalledProcessError as e:
if verbose >= 1:
print("""Command: \n%s \nfailed""" % cmd)
print('Return code:', e.returncode)
print(e.output)
class CHalo:
def __init__(self, ind=None, ID=-1):
if ind is None:
self.indices = []
self.size = 0
else:
self.indices = ind
self.size = len(ind)
self.ID = ID
def __add__(self, other):
if type(other) == type(self):
self.indices.extend(other.indices)
else:
self.indices.append(other)
self.size += other.size
def __radd__(self, other):
if type(other) == type(self):
self.indices.extend(other.indices)
else:
self.indices.append(other)
self.size += other.size
def __str__(self):
return "".join(str(i) + " " for i in self.indices)
class CHalos:
def __init__(self, name, b=0.28, minNrHalos=5, f=0.7):
self.name = name
self.halos = []
self.nrHalos = len(self.halos)
self.minNrHalos = minNrHalos
self.b = b
self.f = f
self.nrLinking = 5000
self.average_distance = 0
self.loadPositions()
self.nrParticlesInHalos = 0
self.percentageInHalos = 0
self.nrLinking = self.nrParticles
def initialize(self):
def limits(i):
return (np.min(self.positions[:, i]), np.max(self.positions[:, i]))
#Simulation details
self.xLimits = limits(0)
self.yLimits = limits(1)
self.zLimits = limits(2)
self.diffX = self.xLimits[1] - self.xLimits[0]
self.diffY = self.yLimits[1] - self.yLimits[0]
self.diffZ = self.zLimits[1] - self.zLimits[0]
self.volume = self.diffX*self.diffY*self.diffZ
self.nrParticles = self.positions.shape[0]
self.average_distance = pow(self.volume/self.nrParticles, 1./3)
self.linkingLength = self.b*pow(self.volume/self.nrParticles, 1./3)
def loadPositions(self):
print("Loading: " + self.name)
if self.name.split(".")[-1] == "xls":
system("libreoffice --headless --convert-to csv " + self.name.replace(" ", "\\ "), 0)
self.positions = np.loadtxt(self.name[:-3] + "csv", delimiter=",", skiprows=2, dtype="string")
self.positions[:, 3] = "-1"
self.positions = self.positions[:, :4].astype(float, copy=False)
self.initialize()
def distance2(self, particle1, particle2):
return np.sum((self.positions[particle2, :3] - self.positions[particle1, :3])**2)
def distance(self, particle1, particle2):
return np.sqrt(np.sum((self.positions[particle2, :3] - self.positions[particle1, :3])**2))
def findNeighbors(self, particle, allFriends):
allFriends.append(particle)
friends = []
#Write this using numpy
for nextParticle in self.nextParticles:
if self.distance(particle, nextParticle) < self.linkingLength:
friends.append(nextParticle)
for friend in friends:
self.nextParticles.remove(friend) # Might slow down
for friend in friends:
self.findNeighbors(friend, allFriends)
def getSortedSizes(self):
sizes = []
for halo in self.halos:
sizes.append(halo.size)
return np.sort(sizes)[::-1]
def FOF(self):
"""
Finding halos with the FOF-method
"""
print("Calculating FOF")
self.nextParticles = range(0, self.positions.shape[0])
while (len(self.nextParticles) > 0):
allFriends = []
nextParticle = self.nextParticles[0]
del self.nextParticles[0]
self.findNeighbors(nextParticle, allFriends)
if len(allFriends) >= self.minNrHalos:
self.halos.append(CHalo(allFriends, len(self.halos)))
self.update()
def update(self):
self.nrHalos = len(self.halos)
for halo in xrange(self.nrHalos):
self.positions[self.halos[halo].indices, -1] = halo
self.calculateNrParticlesInHalos()
self.percentageInHalos = float(self.nrParticlesInHalos)/self.nrParticles
def calculateLinkingLength(self):
"""
The linking length is chosen such that a fraction f of all particles
is linked together with atleast one other particle
For large groups > NrLinking we only calculate this for NrLinking particles
By defult NrLinking = 10000.
"""
tmpNrParticles = self.nrParticles
delta = 1
if self.nrParticles > self.nrLinking:
tmpNrParticles = self.nrLinking
delta = self.nrParticles/self.nrLinking
LinkingLengths = []
# Use all other particles or only the nrlinking subset
next_particles = range(0, self.positions.shape[0])
for i in xrange(delta, tmpNrParticles, delta):
prevTmpLinkingLength = self.distance(0, i)
for next_particle in next_particles:
if next_particle != i:
tmpLinkingLength = self.distance(next_particle, i)
if tmpLinkingLength < prevTmpLinkingLength:
prevTmpLinkingLength = tmpLinkingLength
LinkingLengths.append(prevTmpLinkingLength)
LinkingLengths.sort()
return LinkingLengths[int(len(LinkingLengths)*self.f)]
def calculateNrParticlesInHalos(self):
s = 0
for halo in self.halos:
s += halo.size
self.nrParticlesInHalos = s
def save(self, name, delimiter=" "):
f = open(name, "w")
for halo in self.halos:
for particle in halo.indices:
line = ""
for position in self.positions[particle]:
line += delimiter + str(position)
line += "\n"
f.write(line)
f.close()
def saveHalosBlender(self, name, delimiter=" "):
f = open(name, "w")
f.write(str(self.nrParticlesInHalos) + "\n")
f.write("Halos\n")
for halo in self.halos:
for particle in halo.indices:
# line = str(self.positions[particle][-1]) + delimiter
line = str("Be") + delimiter
for position in self.positions[particle][:-1]:
line += delimiter + str(position)
line += "\n"
f.write(line)
f.close()
def saveParticlesBlender(self, name):
f = open(name, "w")
f.write(str(self.nrParticles) + "\n")
f.write("Particles\n")
for particle in self.positions:
# line = str(self.positions[particle][-1]) + delimiter
line = str("C ")
for position in particle:
line += " " + str(position)
line += "\n"
f.write(line)
f.close()
def printInformation(self):
print("--------------------------------------------------------")
print(self.name)
print("--------------------------------------------------------")
print("Nr of Particles: ", self.nrParticles)
print("Nr of Halos: ", self.nrHalos)
print("Nr of Particles in Halos: ", self.nrParticlesInHalos)
print("Percentage of Particles in Halos: ", self.percentageInHalos)
print("--------------------------------------------------------")
def plotHalos(self, name):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
tmpHalo = CHalo()
for halo in self.halos:
tmpHalo + halo
ax.scatter(self.positions[tmpHalo.indices, 0],
self.positions[tmpHalo.indices, 1],
self.positions[tmpHalo.indices, 2],
s=8,
c=self.positions[tmpHalo.indices, -1],
linewidths=0.1)
ax.set_xlabel('X-position [um]')
ax.set_ylabel('Y-position [um]')
ax.set_zlabel('Z-position [um]')
ax.set_title("Halos")
ax.set_xlim(self.positions[:, 0].min(), self.positions[:, 0].max())
ax.set_ylim(self.positions[:, 1].min(), self.positions[:, 1].max())
ax.set_zlim(self.positions[:, 2].min(), self.positions[:, 2].max())
plt.savefig(name)
# plt.show()
def plotParticles(self, name):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
color = self.positions[:, -1].copy()
color[self.positions[:, -1] >= 0] = 0
ax.scatter(self.positions[:, 0],
self.positions[:, 1],
self.positions[:, 2],
s=8,
c=color,
linewidths=0.1)
ax.set_xlabel('X-position [um]')
ax.set_ylabel('Y-position [um]')
ax.set_zlabel('Z-position [um]')
ax.set_title("Particles")
ax.set_xlim(self.positions[:, 0].min(), self.positions[:, 0].max())
ax.set_ylim(self.positions[:, 1].min(), self.positions[:, 1].max())
ax.set_zlim(self.positions[:, 2].min(), self.positions[:, 2].max())
plt.savefig(name)
# plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="FOF analysis on a single file")
parser.add_argument("filename")
args = parser.parse_args()
b = 0.28
minNrHalos = 10
linkingLength = 2
halos = CHalos(args.filename, b, minNrHalos)
start_time = time.time()
halos.linkingLength = linkingLength
halos.FOF()
print("--- %s seconds ---" % (time.time() - start_time))
savename = args.filename
savename = savename.replace(" ", "-")
savename = savename.replace("/", "-")
halos.saveHalosBlender("halos.xyz")
halos.saveParticlesBlender("particles.xyz")
halos.printInformation()
halos.plotParticles(savename + "-particles.png")
halos.plotHalos(savename + "-halos.png")