Revision | 6b09f38516dcef8ae9c06ffd7d90a8aa67e0a102 (tree) |
---|---|
Zeit | 2011-03-16 06:51:29 |
Autor | lorenzo |
Commiter | lorenzo |
This code now iterates the calculations on a set of clusters.
@@ -0,0 +1,256 @@ | ||
1 | +#! /usr/bin/env python | |
2 | + | |
3 | +# from enthought.mayavi import mlab | |
4 | + | |
5 | +import scipy as s | |
6 | +import numpy as n | |
7 | +import scipy.linalg as sl | |
8 | + | |
9 | +import sys | |
10 | + | |
11 | + | |
12 | +def inertia_tensor(cluster): | |
13 | + | |
14 | + x=cluster[:,0] | |
15 | + y=cluster[:,1] | |
16 | + z=cluster[:,2] | |
17 | + | |
18 | + | |
19 | + | |
20 | + oneone=s.sum(y**2.+z**2.) | |
21 | + onetwo=-s.sum(x*y) | |
22 | + onethree=-s.sum(x*z) | |
23 | + | |
24 | + twoone= -s.sum(x*y) | |
25 | + twotwo=s.sum(x**2.+z**2.) | |
26 | + twothree=-s.sum(y*z) | |
27 | + | |
28 | + threeone=-s.sum(x*z) | |
29 | + threetwo=-s.sum(y*z) | |
30 | + threethree=s.sum(x**2.+y**2.) | |
31 | + | |
32 | + | |
33 | + my_mat=s.zeros(9).reshape((3,3)) | |
34 | + | |
35 | + my_mat[0,0]=oneone | |
36 | + my_mat[0,1]=onetwo | |
37 | + my_mat[0,2]=onethree | |
38 | + | |
39 | + my_mat[1,0]=twoone | |
40 | + my_mat[1,1]=twotwo | |
41 | + my_mat[1,2]=twothree | |
42 | + | |
43 | + my_mat[2,0]=threeone | |
44 | + my_mat[2,1]=threetwo | |
45 | + my_mat[2,2]=threethree | |
46 | + | |
47 | + return my_mat | |
48 | + | |
49 | + | |
50 | + | |
51 | + | |
52 | +def find_CM(cluster): | |
53 | + CM=s.mean(cluster, axis=0) | |
54 | + return CM | |
55 | + | |
56 | + | |
57 | +def relocate_cluster(cluster): | |
58 | + cluster_shift=find_CM(cluster) | |
59 | + cluster[:,0]=cluster[:,0]-cluster_shift[0] | |
60 | + cluster[:,1]=cluster[:,1]-cluster_shift[1] | |
61 | + cluster[:,2]=cluster[:,2]-cluster_shift[2] | |
62 | + | |
63 | + return cluster | |
64 | + | |
65 | + | |
66 | + | |
67 | +def move_cluster(cluster, vector): | |
68 | + cluster_new=s.copy(cluster) | |
69 | + cluster_new[:,0]=cluster_new[:,0]+vector[0] | |
70 | + cluster_new[:,1]=cluster_new[:,1]+vector[1] | |
71 | + cluster_new[:,2]=cluster_new[:,2]+vector[2] | |
72 | + | |
73 | + return cluster_new | |
74 | + | |
75 | + | |
76 | + | |
77 | +def calc_rg(cluster): | |
78 | + x=cluster[:,0] | |
79 | + y=cluster[:,1] | |
80 | + z=cluster[:,2] | |
81 | + | |
82 | + rg=s.var(x)+s.var(y)+s.var(z) | |
83 | + rg=s.sqrt(rg) | |
84 | + | |
85 | + return rg | |
86 | + | |
87 | + | |
88 | +def euclidean_distances(X, Y, Y_norm_squared=None, squared=False): | |
89 | + """ | |
90 | +Considering the rows of X (and Y=X) as vectors, compute the | |
91 | +distance matrix between each pair of vectors. | |
92 | + | |
93 | +Parameters | |
94 | +---------- | |
95 | +X: array of shape (n_samples_1, n_features) | |
96 | + | |
97 | +Y: array of shape (n_samples_2, n_features) | |
98 | + | |
99 | +Y_norm_squared: array [n_samples_2], optional | |
100 | +pre-computed (Y**2).sum(axis=1) | |
101 | + | |
102 | +squared: boolean, optional | |
103 | +This routine will return squared Euclidean distances instead. | |
104 | + | |
105 | +Returns | |
106 | +------- | |
107 | +distances: array of shape (n_samples_1, n_samples_2) | |
108 | + | |
109 | +Examples | |
110 | +-------- | |
111 | +>>> from scikits.learn.metrics.pairwise import euclidean_distances | |
112 | +>>> X = [[0, 1], [1, 1]] | |
113 | +>>> # distrance between rows of X | |
114 | +>>> euclidean_distances(X, X) | |
115 | +array([[ 0., 1.], | |
116 | +[ 1., 0.]]) | |
117 | +>>> # get distance to origin | |
118 | +>>> euclidean_distances(X, [[0, 0]]) | |
119 | +array([[ 1. ], | |
120 | +[ 1.41421356]]) | |
121 | +""" | |
122 | + # should not need X_norm_squared because if you could precompute that as | |
123 | + # well as Y, then you should just pre-compute the output and not even | |
124 | + # call this function. | |
125 | + if X is Y: | |
126 | + X = Y = n.asanyarray(X) | |
127 | + else: | |
128 | + X = n.asanyarray(X) | |
129 | + Y = n.asanyarray(Y) | |
130 | + | |
131 | + if X.shape[1] != Y.shape[1]: | |
132 | + raise ValueError("Incompatible dimension for X and Y matrices") | |
133 | + | |
134 | + XX = n.sum(X * X, axis=1)[:, n.newaxis] | |
135 | + if X is Y: # shortcut in the common case euclidean_distances(X, X) | |
136 | + YY = XX.T | |
137 | + elif Y_norm_squared is None: | |
138 | + YY = Y.copy() | |
139 | + YY **= 2 | |
140 | + YY = n.sum(YY, axis=1)[n.newaxis, :] | |
141 | + else: | |
142 | + YY = n.asanyarray(Y_norm_squared) | |
143 | + if YY.shape != (Y.shape[0],): | |
144 | + raise ValueError("Incompatible dimension for Y and Y_norm_squared") | |
145 | + YY = YY[n.newaxis, :] | |
146 | + | |
147 | + # TODO: | |
148 | + # a faster cython implementation would do the dot product first, | |
149 | + # and then add XX, add YY, and do the clipping of negative values in | |
150 | + # a single pass over the output matrix. | |
151 | + distances = XX + YY # Using broadcasting | |
152 | + distances -= 2 * n.dot(X, Y.T) | |
153 | + distances = n.maximum(distances, 0) | |
154 | + if squared: | |
155 | + return distances | |
156 | + else: | |
157 | + return n.sqrt(distances) | |
158 | + | |
159 | + | |
160 | + | |
161 | +def calc_anisotropy(final_cluster): | |
162 | + N=len(final_cluster[:,0]) | |
163 | + #print "N is, ", N | |
164 | + | |
165 | + #ensure its CM is in (0,0,0) | |
166 | + final_cluster=relocate_cluster(final_cluster) | |
167 | + | |
168 | + inertia_mat=inertia_tensor(final_cluster) | |
169 | + | |
170 | + #print "inertia_mat is, ", inertia_mat | |
171 | + | |
172 | + eigenvalues=s.real(sl.eigvals(inertia_mat)/N) | |
173 | + | |
174 | + eigenvalues=s.sort(eigenvalues)[::-1] | |
175 | + | |
176 | + #print "eigenvalues are, ", eigenvalues | |
177 | + | |
178 | + #r123=s.sqrt(eigenvalues) | |
179 | + | |
180 | + #print "r123, ", r123 | |
181 | + | |
182 | + #Rg=calc_rg(final_cluster) | |
183 | + | |
184 | + #print "Rg is, ", Rg | |
185 | + #print "Rg**2. is, ", Rg**2. | |
186 | + | |
187 | + | |
188 | + #print "0.5*(sum(r123**2.)) is, ", 0.5*(sum(r123**2.)) | |
189 | + | |
190 | + anisotropy=s.zeros(3) | |
191 | + anisotropy[0]=eigenvalues[0]/eigenvalues[2] | |
192 | + anisotropy[1]=eigenvalues[1]/eigenvalues[2] | |
193 | + anisotropy[2]=N | |
194 | + | |
195 | + | |
196 | + #print "anisotropy is, ", anisotropy | |
197 | + | |
198 | + return anisotropy | |
199 | + | |
200 | + | |
201 | + | |
202 | + | |
203 | +###################################################################### | |
204 | + | |
205 | +kf=1. | |
206 | +df= 1.78 # 1.8 | |
207 | + | |
208 | +print sys.argv | |
209 | + | |
210 | +N_iter=sys.argv[-1] | |
211 | +gen_number=sys.argv[-2] | |
212 | + | |
213 | + | |
214 | +# if (len(sys.argv)==2): | |
215 | + | |
216 | +# prefix="aggregate_number_" | |
217 | +# middle=sys.argv[-1] | |
218 | +# filename="_.dat" | |
219 | +# filename=prefix+middle+filename | |
220 | +# elif (len(sys.argv)==3): | |
221 | +# prefix="aggregate_number_" | |
222 | +# middle=sys.argv[-2] | |
223 | +# prefix2="_generation_" | |
224 | +# middle2=sys.argv[-1] | |
225 | +# filename="_.dat" | |
226 | +# filename=prefix+middle+prefix2+middle2+filename | |
227 | + | |
228 | + | |
229 | + | |
230 | +prefix2="_generation_" | |
231 | +middle2=sys.argv[-2] | |
232 | +filename_end="_.dat" | |
233 | + | |
234 | +# print "middle2+filename is, ",middle2+filename_end | |
235 | + | |
236 | +overall=s.zeros(3) | |
237 | + | |
238 | +for i in xrange(int(N_iter)): | |
239 | + middle=i+1 | |
240 | + prefix="aggregate_number_%01d"%(middle) | |
241 | + # print "prefix is,", prefix | |
242 | + filename=prefix+prefix2+middle2+filename_end | |
243 | + | |
244 | + final_cluster=n.loadtxt(filename) | |
245 | + anis=calc_anisotropy(final_cluster) | |
246 | + | |
247 | + print "filename is, ", filename | |
248 | + print "anisotropy is, ", anis | |
249 | + overall=s.vstack((overall, anis)) | |
250 | + | |
251 | + | |
252 | +overall=overall[1:, :] | |
253 | + | |
254 | +n.savetxt("anisotropy_data.dat", overall) | |
255 | + | |
256 | +print "So far so good" |