-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdensity.py
More file actions
80 lines (70 loc) · 2.21 KB
/
Copy pathdensity.py
File metadata and controls
80 lines (70 loc) · 2.21 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
### ---------------------------------------------
### Example script that uses the UDPClust class
### to perform unsupervised density peak clustering
### ---------------------------------------------
### usage: python clustering.py input_file_name dimensionality output_file_name
###
### NB: the dimensionality is a critical parameter and sometimes not trivial to estimate
### ask to Elena Facco, for further help on this
### ---------------------------------------------
### Written by Giovanni Pinamonti, SISSA, Trieste, 2016
### ---------------------------------------------
import numpy as np
import sys
from udpclust import UDP_modules
from udpclust import UDPClust as dp
fname=sys.argv[1]
dim=int(sys.argv[2])
traj=[]
for line in open(fname,'r'):
traj.append([float(x) for x in line.split()])
traj=np.array(traj)
#np.random.shuffle(traj)
print('shape of input array =',traj.shape)
maxknn=int(sys.argv[4])
from scipy.spatial import distance
from scipy.spatial import cKDTree
tree=cKDTree(traj)
dmat,Nlist=tree.query(traj,k=maxknn+1,n_jobs=-1)
Nlist=Nlist[:,1:]
dmat=dmat[:,1:]
Nlist+=1
#print dmat[0]
#print dmat.shape
#order=np.argsort(dmat,axis=1)
#dmat=dmat[order]
#Nlist=Nlist[order]
print('dmat computed')
Npoints=traj.shape[0]
# 1) initialize quantities that will be computed by the fortran subroutine
# density for each frame in trj_sub
rho=np.zeros(Npoints)
rho_err=np.zeros(Npoints)
# filter value for each frame in trj_sub (either 0 or 1)
filt=np.zeros(Npoints,dtype=np.int32)
# error flag
id_err=np.array(0,dtype=np.int32)
# dimension
dim=np.array(dim,dtype=np.int32)
# Neighbour list within dc
#Nlist=np.ones((Npoints,maxknn),dtype=np.int32,order='F')*-1
Nlist=np.array(Nlist,dtype=np.int32,order='F')
# N. of NN taken for comp dens
Nstar=np.zeros(Npoints,dtype=np.int32)
#
# 2) call fortran subroutine
import time
print('fortran locknn')
t0=time.time()
#UDP_modules.dp_clustering.get_nstar\
# (id_err,dmat,dim,Nlist,Nstar)
UDP_modules.dp_clustering.get_densities\
(id_err,dmat,dim,rho,rho_err,Nlist,Nstar)
print('Done!'),
print(time.time()-t0),
fh=open(sys.argv[3]+'_rho.dat','w')
iframe=0
for r in rho:
fh.write('%d %d %.6e %.6e\n'%(iframe,Nstar[iframe],r,rho_err[iframe]))
iframe+=1
fh.close()