修訂 | c6b282d993a0796f7229dede583ae71156ab3418 (tree) |
---|---|
時間 | 2012-06-18 04:29:59 |
作者 | lorenzo |
Commiter | lorenzo |
I added a code which calculated the Rgeo and Rg of a set of aggregates.
@@ -0,0 +1,165 @@ | ||
1 | +#! /usr/bin/env python | |
2 | + | |
3 | + | |
4 | + | |
5 | +from mayavi import mlab | |
6 | + | |
7 | +import scipy as s | |
8 | +import numpy as n | |
9 | + | |
10 | + | |
11 | +# print sys.argv | |
12 | + | |
13 | + | |
14 | +def euclidean_distances(X, Y, Y_norm_squared=None, squared=False): | |
15 | + """ | |
16 | +Considering the rows of X (and Y=X) as vectors, compute the | |
17 | +distance matrix between each pair of vectors. | |
18 | + | |
19 | +Parameters | |
20 | +---------- | |
21 | +X: array of shape (n_samples_1, n_features) | |
22 | + | |
23 | +Y: array of shape (n_samples_2, n_features) | |
24 | + | |
25 | +Y_norm_squared: array [n_samples_2], optional | |
26 | +pre-computed (Y**2).sum(axis=1) | |
27 | + | |
28 | +squared: boolean, optional | |
29 | +This routine will return squared Euclidean distances instead. | |
30 | + | |
31 | +Returns | |
32 | +------- | |
33 | +distances: array of shape (n_samples_1, n_samples_2) | |
34 | + | |
35 | +Examples | |
36 | +-------- | |
37 | +>>> from scikits.learn.metrics.pairwise import euclidean_distances | |
38 | +>>> X = [[0, 1], [1, 1]] | |
39 | +>>> # distrance between rows of X | |
40 | +>>> euclidean_distances(X, X) | |
41 | +array([[ 0., 1.], | |
42 | +[ 1., 0.]]) | |
43 | +>>> # get distance to origin | |
44 | +>>> euclidean_distances(X, [[0, 0]]) | |
45 | +array([[ 1. ], | |
46 | +[ 1.41421356]]) | |
47 | +""" | |
48 | + # should not need X_norm_squared because if you could precompute that as | |
49 | + # well as Y, then you should just pre-compute the output and not even | |
50 | + # call this function. | |
51 | + if X is Y: | |
52 | + X = Y = n.asanyarray(X) | |
53 | + else: | |
54 | + X = n.asanyarray(X) | |
55 | + Y = n.asanyarray(Y) | |
56 | + | |
57 | + if X.shape[1] != Y.shape[1]: | |
58 | + raise ValueError("Incompatible dimension for X and Y matrices") | |
59 | + | |
60 | + XX = n.sum(X * X, axis=1)[:, n.newaxis] | |
61 | + if X is Y: # shortcut in the common case euclidean_distances(X, X) | |
62 | + YY = XX.T | |
63 | + elif Y_norm_squared is None: | |
64 | + YY = Y.copy() | |
65 | + YY **= 2 | |
66 | + YY = n.sum(YY, axis=1)[n.newaxis, :] | |
67 | + else: | |
68 | + YY = n.asanyarray(Y_norm_squared) | |
69 | + if YY.shape != (Y.shape[0],): | |
70 | + raise ValueError("Incompatible dimension for Y and Y_norm_squared") | |
71 | + YY = YY[n.newaxis, :] | |
72 | + | |
73 | + # TODO: | |
74 | + # a faster cython implementation would do the dot product first, | |
75 | + # and then add XX, add YY, and do the clipping of negative values in | |
76 | + # a single pass over the output matrix. | |
77 | + distances = XX + YY # Using broadcasting | |
78 | + distances -= 2 * n.dot(X, Y.T) | |
79 | + distances = n.maximum(distances, 0) | |
80 | + if squared: | |
81 | + return distances | |
82 | + else: | |
83 | + return n.sqrt(distances) | |
84 | + | |
85 | +euclidian_distances = euclidean_distances # both spelling for backward compatibility | |
86 | + | |
87 | +###################################################### | |
88 | + | |
89 | +kf=1.3 | |
90 | +df= 1.3 # 1.8 | |
91 | + | |
92 | + | |
93 | + | |
94 | +N_clust_ini=1024 #initial number of cluster that I combine | |
95 | +N_gen=9 #number of generations of clusters from collisions | |
96 | + | |
97 | +gen_array=2**(s.arange(N_gen)+1) | |
98 | + | |
99 | +print "gen_array is, ", gen_array | |
100 | + | |
101 | +R_geo_arr=s.zeros(0) | |
102 | +R_g_arr=s.zeros(0) | |
103 | +N_clust_arr=s.zeros(0).astype("int") | |
104 | + | |
105 | +for i in xrange(N_gen): | |
106 | + N_clust_in_gen=N_clust_ini/gen_array[i] | |
107 | + print "N_clust_in_gen is, ", N_clust_in_gen | |
108 | + | |
109 | + for k in xrange(N_clust_in_gen): | |
110 | + | |
111 | + prefix="../../df-13/kf-13/aggregate_number_%01d"%(k+1) | |
112 | + prefix2="_generation_%01d"%(i+1) | |
113 | + filename="_.dat" | |
114 | + filename=prefix+prefix2+filename | |
115 | + | |
116 | + | |
117 | + | |
118 | + | |
119 | + | |
120 | + final_cluster=n.loadtxt(filename) | |
121 | + | |
122 | + dist_mat=euclidean_distances(final_cluster,final_cluster) | |
123 | + | |
124 | + | |
125 | + | |
126 | + | |
127 | + R_geo=max(s.ravel(dist_mat)) | |
128 | + | |
129 | + | |
130 | + x=final_cluster[:,0] | |
131 | + y=final_cluster[:,1] | |
132 | + z=final_cluster[:,2] | |
133 | + | |
134 | + | |
135 | + R1_agg_sq=s.var(final_cluster[:,0])+\ | |
136 | + s.var(final_cluster[:,1])+\ | |
137 | + s.var(final_cluster[:,2]) +1. | |
138 | + | |
139 | + R1_agg=s.sqrt(R1_agg_sq) | |
140 | + | |
141 | + # print "R1agg is, ", R1_agg | |
142 | + | |
143 | + | |
144 | + N_tot=len(x) | |
145 | + | |
146 | + # print "the total number of monomers is, ", N_tot | |
147 | + | |
148 | + R_g_theory=(N_tot/kf)**(1./df) | |
149 | + | |
150 | + # print "the theoretical R_g is, ", R_g_theory | |
151 | + | |
152 | + # print "R_geo is, ", R_geo | |
153 | + | |
154 | + R_g_arr=s.hstack((R_g_arr,R1_agg)) | |
155 | + | |
156 | + R_geo_arr=s.hstack((R_geo_arr,R_geo)) | |
157 | + N_clust_arr=s.hstack((N_clust_arr,N_tot)) | |
158 | + | |
159 | +n.savetxt("R_geo_all.dat", R_geo_arr) | |
160 | +n.savetxt("R_g_all.dat", R_g_arr) | |
161 | +n.savetxt("N_clust_all.dat", N_clust_arr) | |
162 | + | |
163 | + | |
164 | + | |
165 | +print "So far so good" |