• R/O
  • SSH

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

修订版d1bd54aad8204ba385a9b1e6d913dbe43b55d86d (tree)
时间2011-03-16 06:50:44
作者lorenzo
Commiterlorenzo

Log Message

I added a code to calculate the anisotropy coefficients of a cluster.

更改概述

差异

diff -r 6f131a48a3ab -r d1bd54aad820 Python-codes/cluster_anysotropy.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Python-codes/cluster_anysotropy.py Tue Mar 15 21:50:44 2011 +0000
@@ -0,0 +1,223 @@
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+kf=1.
162+df= 1.78 # 1.8
163+
164+print sys.argv
165+
166+
167+
168+if (len(sys.argv)==2):
169+
170+ prefix="aggregate_number_"
171+ middle=sys.argv[-1]
172+ filename="_.dat"
173+ filename=prefix+middle+filename
174+elif (len(sys.argv)==3):
175+ prefix="aggregate_number_"
176+ middle=sys.argv[-2]
177+ prefix2="_generation_"
178+ middle2=sys.argv[-1]
179+ filename="_.dat"
180+ filename=prefix+middle+prefix2+middle2+filename
181+
182+
183+
184+
185+
186+
187+final_cluster=n.loadtxt(filename)
188+N=len(final_cluster[:,0])
189+print "N is, ", N
190+
191+#ensure its CM is in (0,0,0)
192+final_cluster=relocate_cluster(final_cluster)
193+
194+inertia_mat=inertia_tensor(final_cluster)
195+
196+print "inertia_mat is, ", inertia_mat
197+
198+eigenvalues=s.real(sl.eigvals(inertia_mat)/N)
199+
200+eigenvalues=s.sort(eigenvalues)[::-1]
201+
202+print "eigenvalues are, ", eigenvalues
203+
204+r123=s.sqrt(eigenvalues)
205+
206+print "r123, ", r123
207+
208+Rg=calc_rg(final_cluster)
209+
210+print "Rg is, ", Rg
211+print "Rg**2. is, ", Rg**2.
212+
213+
214+print "0.5*(sum(r123**2.)) is, ", 0.5*(sum(r123**2.))
215+
216+anisotropy=s.zeros(2)
217+anisotropy[0]=eigenvalues[0]/eigenvalues[2]
218+anisotropy[1]=eigenvalues[1]/eigenvalues[2]
219+
220+
221+print "anisotropy is, ", anisotropy
222+
223+print "So far so good"