annotate src/flock2.c @ 2:311a4f16c483 draft

Deleted selected files
author immport-devteam
date Mon, 27 Feb 2017 13:27:43 -0500
parents 7eab80f86779
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1 ///////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2 // Changes made:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3 // 1. Added another parameter: number of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
4 // 2. Added a hierarchical merging step based on density change between centroids of two hyper-regions
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
5 // 3. Picked the longest dimensions between the population centroids to judge whether the two parts should be merged
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
6 // 4. Removed checking time parameter
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
7 // 5. Output error to stderr
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
8 // 6. Fixed the bug of density threshold always = 3
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
9 // 7. Added another error (select_num_bin<min_grid) || (select_num_bin>max_grid) to STDERR
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
10 // 8. Fixed a bug for 2D data by using K=e*K
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
11 // 9. Added some header files, may not be necessary
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
12 // 10. Added a lower bound (at least two) for number of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
13 /***************************************************************************************************************************************
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
14
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
15 FLOCK: FLOw cytometry Clustering without K (Named by: Jamie A. Lee and Richard H. Scheuermann)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
16
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
17 Author: (Max) Yu Qian, Ph.D.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
18
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
19 Copyright: Scheuermann Lab, Dept. of Pathology, UTSW
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
20
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
21 Development: November 2005 ~ Forever
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
22
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
23 Status: July 2010: Release 2.0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
24
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
25 Usage: flock data_file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
26 Note: the input file format must be channel values and the delimiter between two values must be a tab.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
27
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
28
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
29
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
30 ****************************************************************************************************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
31
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
32 #include <time.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
33 #include <stdio.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
34 #include <stdlib.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
35 #include <math.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
36 #include <string.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
37 #include <sys/stat.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
38 #include <unistd.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
39 #include <assert.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
40
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
41
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
42
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
43 #define DEBUG 0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
44 #define LINE_LEN 1024
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
45 #define FILE_NAME_LEN 128
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
46 #define PARA_NAME_LEN 64
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
47 #define MAX_VALUE 1000000000
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
48 #define MIN_GRID 6
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
49 #define MAX_GRID 50
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
50 #define E_T 1.0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
51
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
52 #define NORM_METHOD 2 //2 if z-score; 0 if no normalization; 1 if min-max
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
53 #define KMEANS_TERM 10
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
54 #define MAX_POP_NUM 128
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
55
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
56 #ifndef max
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
57 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
58 #endif
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
59
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
60 #ifndef min
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
61 #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
62 #endif
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
63
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
64 static long **Gr=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
65 static long *gcID = 0; /* grid cluster IDs */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
66 static long *cluster_count=0; /* count of nodes per cluster */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
67 static long ndense=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
68 static long ndim=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
69 /* cid changes between depth-first searches, but is constant within a
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
70 single search, so it goes here. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
71 static long cid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
72 /* Do a depth-first search for a single connected component in graph
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
73 * G. Start from node, tag the nodes found with cid, and record
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
74 * the tags in grid_clusterID. Also, record the node count in
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
75 * cluster_count. If we find a node that has already been assigned to
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
76 * a cluster, that means we're merging two clusters, so zero out the
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
77 * old cid's node count.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
78 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
79 * Note that our graph is constructed as a DAG, so we can be
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
80 * guaranteed to terminate without checking for cycles.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
81 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
82 * Note2: this function can potentially recurse to depth = ndense.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
83 * Plan stack size accordingly.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
84 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
85 * Output:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
86 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
87 * grid_clusterID[] -- array where we tag the nodes.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
88 * cluster_count[] -- count of the number of nodes per cluster.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
89 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
90 static void merge_cluster(long from, long into)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
91 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
92 int i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
93
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
94 for(i=0; i<ndense; ++i)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
95 if(gcID[i] == from)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
96 gcID[i] = into;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
97 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
98
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
99 void depth_first(long node)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
100 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
101 long i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
102
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
103 if(gcID[node] == cid) // we're guaranteed no cycles, but it is possible to reach a node
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
104 return; // through two different paths in the same cluster. This early
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
105 // return saves us some unneeded work.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
106
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
107 /* Check to see if we are merging a cluster */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
108 if(gcID[node] >= 0) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
109 /* We are, so zero the count for the old cluster. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
110 cluster_count[ gcID[node] ] = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
111 merge_cluster(gcID[node], cid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
112 return;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
113 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
114
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
115 /* Update for this node */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
116 gcID[node] = cid;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
117 cluster_count[cid]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
118
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
119 /* Recursively search the child nodes */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
120 for(i=0; i<ndim; ++i)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
121 if(Gr[node][i] >= 0) /* This is a child node */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
122 depth_first(Gr[node][i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
123 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
124
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
125 void bail(const char *msg)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
126 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
127 fprintf(stderr,"%s",msg);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
128 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
129 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
130
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
131
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
132 static void check_clusters(long *gcID, int ndense)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
133 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
134 int i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
135
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
136 for(i=0; i<ndense; ++i)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
137 if(gcID[i] < 0) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
138 fprintf(stderr,"faulty cluster id at i= %d\n",i);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
139 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
140 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
141 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
142
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
143
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
144
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
145 long find_connected(long **G, long n_dense_grids, long num_dm, long *grid_clusterID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
146 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
147 long nclust=0; /* number of clusters found */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
148 long i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
149 long *subfac;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
150 long clustid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
151 int subval=0,nempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
152
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
153 int sz = n_dense_grids*sizeof(long);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
154 cluster_count = malloc(sz);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
155 if(!cluster_count)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
156 bail("find_connected: Unable to allocate %zd bytes.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
157 memset(cluster_count,0,sz);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
158
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
159 /* set up the statics that will be used in the DFS */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
160 Gr=G;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
161 gcID = grid_clusterID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
162 ndense = n_dense_grids;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
163 ndim = num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
164
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
165 for(i=0;i<ndense;++i)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
166 grid_clusterID[i] = -1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
167
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
168 for(i=0;i<ndense;++i) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
169 if(grid_clusterID[i] < 0) { /* grid hasn't been assigned yet */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
170 cid = nclust++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
171 depth_first(i);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
172 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
173 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
174
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
175
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
176
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
177 /* At this point we probably have some clusters that are empty due to merging.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
178 We want to compact the cluster numbering to eliminate the empty clusters. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
179
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
180 subfac = malloc(sz);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
181 if(!subfac)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
182 bail("find_connected: Unable to allocate %zd bytes.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
183 subval=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
184 nempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
185
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
186 /* cluster #i needs to have its ID decremented by 1 for each empty cluster with
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
187 ID < i. Precaclulate the decrements in this loop: */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
188 for(i=0;i<nclust;++i) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
189 //clustid = grid_clusterID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
190 if(cluster_count[i] == 0) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
191 subval++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
192 nempty++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
193 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
194 subfac[i] = subval;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
195 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
196
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
197 //printf("nempty is %d\n",nempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
198
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
199 /* Now apply the decrements to all of the dense grids */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
200 for(i=0;i<ndense;++i) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
201 clustid = grid_clusterID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
202 grid_clusterID[i] -= subfac[clustid];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
203 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
204
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
205
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
206
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
207 /* correct the number of clusters found */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
208 nclust -= nempty;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
209
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
210 //printf("nclust is %d\n",nclust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
211
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
212 return nclust;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
213 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
214
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
215 /************************************* Read basic info of the source file **************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
216 /************************************* Read basic info of the source file **************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
217 void getfileinfo(FILE *f_src, long *file_Len, long *num_dm, char *name_string, long *time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
218 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
219 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
220 char current_name[64];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
221 char prv;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
222
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
223 long num_rows=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
224 long num_columns=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
225 long ch='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
226 long prev='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
227 long time_pos=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
228 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
229 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
230 int sw=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
231
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
232 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
233 fgets(src, LINE_LEN, f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
234
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
235 name_string[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
236 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
237 prv='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
238
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
239 while ((src[i]==' ') || (src[i]=='\t')) //skip space and tab characters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
240 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
241
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
242 while ((src[i]!='\0') && (src[i]!='\n')) //repeat until the end of the line
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
243 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
244 current_name[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
245
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
246 if ((src[i]=='\t') && (prv!='\t')) //a complete word
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
247 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
248 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
249
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
250 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
251 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
252 num_columns++; //num_columns does not inlcude the column of Time
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
253 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
254 if (sw) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
255 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
256 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
257 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
258 sw = 1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
259 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
260 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
261 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
262 *time_ID=time_pos;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
263 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
264
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
265 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
266 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
267 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
268
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
269 if ((src[i]=='\t') && (prv=='\t')) //a duplicate tab or space
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
270 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
271 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
272 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
273 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
274
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
275 if (src[i]!='\t')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
276 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
277
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
278 prv=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
279 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
280 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
281
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
282 //name_string[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
283 if (prv!='\t') //the last one hasn't been retrieved
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
284 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
285 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
286 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
287 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
288 num_columns++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
289 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
290 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
291 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
292 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
293 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
294 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
295 *time_ID=time_pos;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
296 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
297 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
298 if (DEBUG==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
299 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
300 printf("time_ID is %ld\n",*time_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
301 printf("name_string is %s\n",name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
302 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
303
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
304 //start computing # of rows
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
305
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
306 while ((ch = fgetc(f_src))!= EOF )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
307 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
308 if (ch == '\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
309 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
310 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
311 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
312 prev = ch;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
313 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
314 if (prev!='\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
315 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
316
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
317 if (num_rows<50)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
318 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
319 fprintf(stderr,"Number of events in the input file is too few and should not be processed!\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
320 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
321 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
322
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
323 *file_Len=num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
324 *num_dm=num_columns;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
325
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
326 printf("original file size is %ld; number of dimensions is %ld\n", *file_Len, *num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
327 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
328
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
329
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
330
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
331 /************************************* Read the source file into uncomp_data **************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
332 void readsource(FILE *f_src, long file_Len, long num_dm, double **uncomp_data, long time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
333 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
334 //long time_pass=0; //to mark whether the time_ID has been passed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
335 long index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
336
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
337 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
338 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
339 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
340
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
341 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
342 char xc[LINE_LEN/10];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
343
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
344 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
345 fgets(src,LINE_LEN, f_src); //skip the first line about parameter names
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
346
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
347 while (!feof(f_src) && (index<file_Len)) //index = 0, 1, ..., file_Len-1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
348 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
349 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
350 fgets(src,LINE_LEN,f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
351 i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
352 //time_pass=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
353
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
354 //if (time_ID==-1) //there is no time_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
355 // {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
356 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
357 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
358 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
359 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
360 while ((src[i]!='\0') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
361 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
362 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
363 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
364 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
365 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
367 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
368 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
369
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
370 uncomp_data[index][t]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
371 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
372 // }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
373 /*else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
374 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
375 for (t=0;t<=num_dm;t++) //the time column needs to be skipped, so there are num_dm+1 columns
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
376 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
377 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
378 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
379 while ((src[i]!='\0') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
380 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
381 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
382 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
383 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
384 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
385
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
386 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
387 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
388
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
389 if (t==time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
390 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
391 time_pass=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
392 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
393 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
394
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
395 if (time_pass)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
396 uncomp_data[index][t-1]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
397 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
398 uncomp_data[index][t]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
399 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
400 }*/ //commented by Yu Qian on Aug 31, 2010 as we do not want to check time_ID anymore
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
401 index++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
402 //fprintf(fout_ID,"%s",src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
403 } //end of while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
404
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
405 if (DEBUG == 1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
406 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
407 printf("the last line of the source data is:\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
408 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
409 printf("%f ",uncomp_data[index-1][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
410 printf("\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
411 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
412 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
413
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
414
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
415 /**************************************** Normalization ******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
416 void tran(double **orig_data, long file_Len, long num_dm, long norm_used, double **matrix_to_cluster)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
417 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
418 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
419 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
420
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
421 double biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
422 double smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
423
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
424 double *aver; //average of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
425 double *std; //standard deviation of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
426
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
427 aver=(double*)malloc(sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
428 memset(aver,0,sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
429
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
430 std=(double*)malloc(sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
431 memset(std,0,sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
432
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
433 if (norm_used==2) //z-score normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
434 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
435 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
436 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
437 aver[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
438 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
439 aver[j]=aver[j]+orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
440 aver[j]=aver[j]/(double)file_Len;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
441
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
442 std[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
443 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
444 std[j]=std[j]+((orig_data[i][j]-aver[j])*(orig_data[i][j]-aver[j]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
445 std[j]=sqrt(std[j]/(double)file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
446
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
447 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
448 matrix_to_cluster[i][j]=(orig_data[i][j]-aver[j])/std[j]; //z-score normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
449 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
450 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
451
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
452 if (norm_used==1) //0-1 min-max normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
453 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
454 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
455 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
456 biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
457 smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
458 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
459 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
460 if (orig_data[i][j]>biggest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
461 biggest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
462 if (orig_data[i][j]<smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
463 smallest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
464 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
465
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
466 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
467 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
468 if (biggest==smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
469 matrix_to_cluster[i][j]=biggest;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
470 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
471 matrix_to_cluster[i][j]=(orig_data[i][j]-smallest)/(biggest-smallest);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
472 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
473 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
474 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
475
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
476 if (norm_used==0) //no normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
477 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
478 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
479 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
480 matrix_to_cluster[i][j]=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
481 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
482
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
483 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
484
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
485
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
486
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
487 /********************************************** RadixSort *******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
488 /* Perform a radix sort using each dimension from the original data as a radix.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
489 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
490 * sorted_seq -- a permutation vector mapping the ordered list onto the original data.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
491 * (sorted_seq[i] -> index in the original data of the ith element of the ordered list)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
492 * grid_ID -- mapping between the original data and the "grids" (see below) found as a byproduct
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
493 * of the sorting procedure.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
494 * num_nonempty -- the number of grids that occur in the data (= the number of distinct values assumed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
495 * by grid_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
496 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
497
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
498 void radixsort_flock(long **position,long file_Len,long num_dm,long num_bin,long *sorted_seq,long *num_nonempty,long *grid_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
499 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
500 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
501 long length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
502 long start=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
503 long prev_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
504 long curr_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
505
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
506 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
507 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
508 long p=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
509 long loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
510 long temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
511 long equal=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
512
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
513 long *count; //count[i]=j means there are j numbers having value i at the processing digit
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
514 long *index; //index[i]=j means the starting position of grid i is j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
515 long *cp; //current position
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
516 long *mark; //mark[i]=0 means it is not an ending point of a part, 1 means it is (a "part" is a group of items with identical bins for all dimensions)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
517 long *seq; //temporary sequence
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
518
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
519 count=(long*)malloc(sizeof(long)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
520 memset(count,0,sizeof(long)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
521
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
522 cp=(long*)malloc(sizeof(long)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
523 memset(cp,0,sizeof(long)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
524
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
525 index=(long*)malloc(sizeof(long)*num_bin); // initialized below
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
526
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
527 seq=(long*)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
528 memset(seq,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
529
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
530 mark=(long*)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
531 memset(mark,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
532
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
533 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
534 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
535 sorted_seq[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
536 mark[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
537 seq[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
538 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
539 for (i=0;i<num_bin;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
540 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
541 index[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
542 cp[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
543 count[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
544 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
545
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
546 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
547 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
548 if (j==0) //compute the initial values of mark
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
549 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
550 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
551 count[position[i][j]]++; // initialize the count to the number of items in each bin of the 0th dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
552
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
553 index[0] = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
554 for (i=0;i<num_bin-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
555 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
556 index[i+1]=index[i]+count[i]; //index[k]=x means k segment starts at x (in the ordered list)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
557 if ((index[i+1]>0) && (index[i+1]<=file_Len))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
558 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
559 mark[index[i+1]-1]=1; // Mark the end of the segment in the ordered list
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
560 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
561 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
562 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
563 printf("out of myboundary for mark at index[i+1]-1.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
564 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
565 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
566 mark[file_Len-1]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
567
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
568 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
569 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
570 /* Build a permutation vector for the partially ordered data. Store the PV in sorted_seq */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
571 loc=position[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
572 temp=index[loc]+cp[loc]; //cp[i]=j means the offset from the starting position of grid i is j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
573 sorted_seq[temp]=i; //sorted_seq[i]=temp is also another way to sort
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
574 cp[loc]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
575 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
576 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
577 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
578 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
579 //reset count, index, loc, temp, cp, start, and length
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
580 length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
581 loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
582 temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
583 start=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
584 for (p=0;p<num_bin;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
585 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
586 cp[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
587 count[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
588 index[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
589 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
590
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
591 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
592 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
593 long iperm = sorted_seq[i]; // iperm allows us to traverse the data in sorted order.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
594 if (mark[i]!=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
595 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
596 /* Count the number of items in each bin of
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
597 dimension j, BUT we are going to reset at the end
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
598 of each "part". Thus, the total effect is to do
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
599 a sort by bin on the jth dimension for each group
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
600 of data that has been identical for the
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
601 dimensions processed up to this point. This is
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
602 the standard radix sort procedure, but doing it
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
603 this way saves us having to allocate buckets to
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
604 hold the data in each group of "identical-so-far"
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
605 elements. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
606 count[position[iperm][j]]++; //count[position[i][j]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
607 length++; // This is the total length of the part, irrespective of the value of the jth component
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
608 // (actually, less one, since we don't increment for the final element below)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
609 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
610 if (mark[i]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
611 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
612 //length++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
613 count[position[iperm][j]]++;//count[position[i][j]]++; //the current point must be counted in
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
614 start=i-length; //this part starts from start to i: [start,i]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
615 /* Now we sort on the jth radix, just like we did for the 0th above, but we restrict it to just the current part.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
616 This would be a lot more clear if we broke this bit of code out into a separate function and processed recursively,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
617 plus we could multi-thread over the parts. (Hmmm...)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
618 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
619 index[0] = start; // Let index give the offset within the whole ordered list.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
620 for (t=0;t<num_bin-1;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
621 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
622 index[t+1]=index[t]+count[t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
623
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
624 if ((index[t+1]<=file_Len) && (index[t+1]>0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
625 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
626 mark[index[t+1]-1]=1; // update the part boundaries to include the differences in the current radix.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
627 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
628
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
629 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
630 mark[i]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
631
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
632 /* Update the permutation vector for the current part (i.e., from start to i). By the time we finish the loop over i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
633 the PV will be completely updated for the partial ordering up to the current radix. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
634 for (t=start;t<=i;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
635 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
636 loc=position[sorted_seq[t]][j];//loc=position[t][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
637 temp=index[loc]+cp[loc];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
638 if ((temp<file_Len) && (temp>=0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
639 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
640 // seq is a temporary because we have to maintain the old PV until we have finished this step.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
641 seq[temp]=sorted_seq[t]; //sorted_seq[i]=temp is also another way to sort
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
642 cp[loc]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
643 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
644 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
645 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
646 printf("out of myboundary for seq at temp.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
647 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
648 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
649
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
650 for (t=start;t<=i;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
651 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
652 // copy the temporary back into sorted_seq. sorted_seq is now updated for radix j up through
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
653 // entry i in the ordered list.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
654 if ((t>=0) && (t<file_Len))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
655 sorted_seq[t]=seq[t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
656 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
657 printf("out of myboundary for seq and sorted_seq at t.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
658 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
659 //reset count, index, seq, length, and cp
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
660 length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
661 loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
662 temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
663 for (p=0;p<num_bin;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
664 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
665 cp[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
666 count[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
667 index[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
668 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
669 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
670 }//end for i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
671 }//end else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
672 }//end for j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
673
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
674 /* sorted_seq[] now contains the ordered list for all radices. mark[] gives the boundaries between groups of elements that are
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
675 identical over all radices (= dimensions in the original data) (although it appears we aren't going to make use of this fact) */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
676
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
677 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
678 grid_ID[i]=0; //in case the initial value hasn't been assigned
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
679 *num_nonempty=1; //starting from 1!
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
680
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
681 /* assign the "grid" identifiers for all of the data. A grid will be what we were calling a "part" above. We will number them
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
682 serially and tag the *unordered* data with the grid IDs. We will also count the number of populated grids (in general there will
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
683 be many possible combinations of bin values that simply never occur) */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
684
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
685 for (i=1;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
686 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
687 equal=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
688 prev_ID=sorted_seq[i-1];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
689 curr_ID=sorted_seq[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
690 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
691 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
692 if (position[prev_ID][j]!=position[curr_ID][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
693 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
694 equal=0; //not equal
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
695 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
696 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
697 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
698
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
699 if (equal)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
700 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
701 grid_ID[curr_ID]=grid_ID[prev_ID];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
702 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
703 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
704 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
705 *num_nonempty=*num_nonempty+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
706 grid_ID[curr_ID]=grid_ID[prev_ID]+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
707 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
708 //all_grid_vol[grid_ID[curr_ID]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
709 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
710
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
711 //free memory
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
712 free(count);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
713 free(index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
714 free(cp);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
715 free(seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
716 free(mark);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
717
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
718 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
719
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
720 /********************************************** Compute Position of Events ************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
721 void compute_position(double **data_in, long file_Len, long num_dm, long num_bin, long **position, double *interval)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
722 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
723 /* What we are really doing here is binning the data, with the bins
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
724 spanning the range of the data and number of bins = num_bin */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
725 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
726 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
727
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
728 double *small; //small[j] is the smallest value within dimension j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
729 double *big; //big[j] is the biggest value within dimension j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
730
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
731 small=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
732 memset(small,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
733
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
734 big=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
735 memset(big,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
736
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
737
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
738 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
739 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
740 big[j]=MAX_VALUE*(-1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
741 small[j]=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
742 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
743 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
744 if (data_in[i][j]>big[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
745 big[j]=data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
746
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
747 if (data_in[i][j]<small[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
748 small[j]=data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
749 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
750
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
751 interval[j]=(big[j]-small[j])/(double)num_bin; //interval is computed using the biggest value and smallest value instead of the channel limit
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
752 /* XXX: I'm pretty sure the denominator of the fraction above should be num_bin-1. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
753 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
754
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
755 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
756 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
757 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
758 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
759 if (data_in[i][j]>=big[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
760 position[i][j]=num_bin-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
761 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
762 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
763 position[i][j]=(long)((data_in[i][j]-small[j])/interval[j]); //position[i][j]=t means point i is at the t grid of dimensional j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
764 if ((position[i][j]>=num_bin) || (position[i][j]<0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
765 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
766 //printf("position mis-computed in density analysis!\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
767 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
768 fprintf(stderr,"Incorrect input file format or input parameters (number of bins overflows)!\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
769 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
770 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
771 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
772 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
773 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
774
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
775
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
776 free(small);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
777 free(big);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
778 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
779
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
780 /********************************************** select_bin to select the number of bins **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
781 //num_bin=select_bin(normalized_data, file_Len, num_dm, MIN_GRID, MAX_GRID, position, sorted_seq, all_grid_ID, &num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
782 /* Determine the number of bins to use in each dimension. Additionally sort the data elements according to the binned
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
783 * values, and partition the data into "grids" with identical (binned) values. We try progressively more bins until we
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
784 * maximize a merit function, then return the results obtained using the optimal number of bins.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
785 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
786 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
787 * position -- binned data values
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
788 * sorted_seq -- permutation vector mapping the ordered list to the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
789 * all_grid_ID -- grid to which each data element was assigned.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
790 * num_nonempty -- number of distinct values assumed by all_grid_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
791 * interval -- bin width for each data dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
792 * return value -- the number of bins selected.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
793 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
794
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
795 long select_bin(double **normalized_data, long file_Len, long num_dm, long min_grid, long max_grid, long **position, long *sorted_seq,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
796 long *all_grid_ID, long *num_nonempty, double *interval, long user_num_bin)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
797 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
798
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
799 long num_bin=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
800 long select_num_bin=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
801 long m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
802 long n=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
803
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
804 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
805 long bin_scope=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
806 long temp_num_nonempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
807
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
808 long *temp_grid_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
809 long *temp_sorted_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
810 long **temp_position;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
811
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
812 //sorted_seq[i]=j means the event j ranks i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
813
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
814 double temp_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
815 double *bin_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
816 double *temp_interval;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
817
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
818
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
819 temp_grid_ID=(long *)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
820 memset(temp_grid_ID,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
821
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
822 temp_sorted_seq=(long *)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
823 memset(temp_sorted_seq,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
824
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
825 temp_position=(long **)malloc(sizeof(long*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
826 memset(temp_position,0,sizeof(long*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
827 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
828 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
829 temp_position[m]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
830 memset(temp_position[m],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
831 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
832
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
833 temp_interval=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
834 memset(temp_interval,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
835
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
836 bin_scope=max_grid-min_grid+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
837 bin_index=(double *)malloc(sizeof(double)*bin_scope);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
838 memset(bin_index,0,sizeof(double)*bin_scope);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
839
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
840 i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
841
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
842 for (num_bin=min_grid;num_bin<=max_grid;num_bin++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
843 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
844
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
845 temp_num_nonempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
846 /* compute_position bins the data into num_bin bins. Each
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
847 dimension is binned independently.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
848
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
849 Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
850 temp_position[i][j] -- bin for the jth component of data element i.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
851 temp_interval[j] -- bin-width for the jth component
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
852 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
853 compute_position(normalized_data, file_Len, num_dm, num_bin, temp_position, temp_interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
854 radixsort_flock(temp_position,file_Len,num_dm,num_bin,temp_sorted_seq,&temp_num_nonempty,temp_grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
855
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
856 /* our figure of merit is the number of non-empty grids divided by number of bins per dimension.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
857 We declare victory when we have found a local maximum */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
858 bin_index[i]=((double)temp_num_nonempty)/((double)num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
859 if ((double)(temp_num_nonempty)>=(double)(file_Len)*0.95)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
860 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
861 if ((bin_index[i]<temp_index) && (user_num_bin==0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
862 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
863 if ((user_num_bin==num_bin-1) && (user_num_bin!=0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
864 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
865
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
866 /* Since we have accepted this trial bin, copy all the temporary results into
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
867 the output buffers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
868 memcpy(all_grid_ID,temp_grid_ID,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
869 memcpy(sorted_seq,temp_sorted_seq,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
870 memcpy(interval,temp_interval,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
871
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
872 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
873 for (n=0;n<num_dm;n++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
874 position[m][n]=temp_position[m][n];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
875
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
876 temp_index=bin_index[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
877 select_num_bin=num_bin;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
878 num_nonempty[0]=temp_num_nonempty;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
879 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
880 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
881
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
882 if ((select_num_bin<min_grid) || (select_num_bin>max_grid))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
883 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
884 fprintf(stderr,"Number of events collected is too few in terms of number of markers used. The file should not be processed!\n"); //modified on Nov 04, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
885 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
886 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
887
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
888 if (temp_index==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
889 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
890 fprintf(stderr,"Too many dimensions with too few events in the input file, or a too large number of bins used.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
891 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
892 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
893
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
894 free(temp_grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
895 free(temp_sorted_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
896 free(bin_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
897 free(temp_interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
898
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
899 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
900 free(temp_position[m]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
901 free(temp_position);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
902
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
903 return select_num_bin;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
904 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
905
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
906 /************************************* Select dense grids **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
907 // compute num_dense_grids, num_dense_events, dense_grid_reverse, and all_grid_vol
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
908 // den_cutoff=select_dense(file_Len, all_grid_ID, num_nonempty, &num_dense_grids, &num_dense_events, dense_grid_reverse);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
909 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
910 * Prune away grids that are insufficiently "dense" (i.e., contain too few data items)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
911 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
912 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
913 * num_dense_grids -- number of dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
914 * num_dense_events -- total number of data items in all dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
915 * dense_grid_reverse -- mapping from list of all grids to list of dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
916 * return value -- density cutoff for separating dense from non-dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
917 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
918
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
919 int select_dense(long file_Len, long *all_grid_ID, long num_nonempty, long *num_dense_grids, long *num_dense_events, long *dense_grid_reverse, int den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
920 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
921
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
922
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
923 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
924 long vol_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
925 long biggest_size=0; //biggest grid_size, to define grid_size_index
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
926 long biggest_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
927 //long actual_threshold=0; //the actual threshold on grid_size, e.g., 1 implies 1 event in the grid
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
928 //long num_remain=0; //number of remaining grids with different density thresholds
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
929 long temp_num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
930 long temp_num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
931
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
932 long *grid_size_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
933 long *all_grid_vol;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
934 long *grid_density_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
935
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
936 //double den_average=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
937 // double avr_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
938
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
939
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
940 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
941 // Compute all_grid_vol
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
942 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
943 all_grid_vol=(long *)malloc(sizeof(long)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
944 memset(all_grid_vol,0,sizeof(long)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
945
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
946 /* Grid "volume" is just the number of data contained in the grid. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
947 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
948 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
949 vol_ID=all_grid_ID[i]; //vol_ID=all_grid_ID[sorted_seq[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
950 all_grid_vol[vol_ID]++; //all_grid_vol[i]=j means grid i has j points
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
951 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
952
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
953
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
954
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
955 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
956 // Compute grid_size_index (histogram of grid sizes)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
957 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
958
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
959 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
960 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
961 if (biggest_size<all_grid_vol[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
962 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
963 biggest_size=all_grid_vol[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
964 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
965 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
966
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
967 if (biggest_size<3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
968 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
969 fprintf(stderr,"Too many dimensions with too few events in the input file, or a too large number of bins used.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
970 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
971 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
972
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
973
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
974 grid_size_index=(long*)malloc(sizeof(long)*biggest_size);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
975 memset(grid_size_index,0,sizeof(long)*biggest_size);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
976
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
977 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
978 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
979 grid_size_index[all_grid_vol[i]-1]++; //grid_size_index[0]=5 means there are 5 grids having size 1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
980 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
981
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
982
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
983
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
984 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
985 // Compute den_cutoff
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
986 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
987
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
988 grid_density_index=(long *)malloc(sizeof(long)*(biggest_size-2));//from event 2 to biggest_size-1, i.e., from 0 to biggest_size-3
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
989 memset(grid_density_index,0,sizeof(long)*(biggest_size-2));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
990
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
991 if (den_t_event==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
992 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
993 biggest_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
994
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
995 for (i=2;i<biggest_size-1;i++) //the grid with 1 event will be skipped, i.e., grid_density_index[0] won't be defined
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
996 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
997 grid_density_index[i-1]=(grid_size_index[i-1]+grid_size_index[i+1]-2*grid_size_index[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
998 if (biggest_index<grid_density_index[i-1])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
999 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1000 biggest_index=grid_density_index[i-1];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1001 den_t_event=i+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1002 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1003 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1004 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1005
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1006 if (den_t_event==0) //if biggest_size==3
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1007 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1008 fprintf(stderr,"the densest hyperregion has only 3 events, smaller than the user-specified value. Therefore the density threshold is automatically changed to 3.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1009 den_t_event=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1010 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1011
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1012 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1013 if (all_grid_vol[i]>=den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1014 temp_num_dense_grids++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1015
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1016 if (temp_num_dense_grids<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1017 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1018 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1019 //printf("a too high density threshold is set! Please decrease your density threshold.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1020 fprintf(stderr,"a too high density threshold! Please decrease your density threshold.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1021 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1022 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1023
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1024 if (temp_num_dense_grids>=100000)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1025 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1026 //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1027 //printf("a too low density threshold is set! Please increase your density threshold.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1028 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1029 fprintf(stderr,"a too low density threshold! Please increase your density threshold.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1030 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1031 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1032
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1033 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1034 // Compute dense_grid_reverse
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1035 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1036 temp_num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1037 temp_num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1038 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1039 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1040 dense_grid_reverse[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1041
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1042 if (all_grid_vol[i]>=den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1043 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1044 dense_grid_reverse[i]=temp_num_dense_grids; //dense_grid_reverse provides a mapping from all nonempty grids to dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1045 temp_num_dense_grids++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1046 temp_num_dense_events+=all_grid_vol[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1047 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1048 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1049
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1050 num_dense_grids[0]=temp_num_dense_grids;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1051 num_dense_events[0]=temp_num_dense_events;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1052
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1053 free(grid_size_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1054 free(all_grid_vol);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1055
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1056 return den_t_event;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1057 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1058
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1059 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1060 // Compute densegridID_To_gridevent and eventID_To_denseventID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1061 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1062 // grid_To_event(file_Len, dense_grid_reverse, all_grid_ID, eventID_To_denseventID, densegridID_To_gridevent);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1063 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1064 * Filter the data so that only the data belonging to dense grids is left
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1065 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1066 * Output:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1067 * eventID_To_denseeventID -- mapping from original event ID to ID in list containing only events contained in dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1068 * densegridID_To_gridevent -- mapping from dense grids to prototype members of the grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1069 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1070 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1071
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1072 void grid_To_event(long file_Len, long *dense_grid_reverse, long *all_grid_ID, long *eventID_To_denseventID, long *densegridID_To_gridevent)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1073 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1074 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1075 long dense_grid_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1076 long dense_event_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1077
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1078 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1079 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1080 dense_grid_ID=dense_grid_reverse[all_grid_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1081 eventID_To_denseventID[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1082 if (dense_grid_ID!=-1) //for point (i) belonging to dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1083 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1084 eventID_To_denseventID[i]=dense_event_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1085 dense_event_ID++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1086
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1087 if (densegridID_To_gridevent[dense_grid_ID]==-1) //for point i that hasn't been selected
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1088 densegridID_To_gridevent[dense_grid_ID]=i; //densegridID_To_gridevent maps dense_grid_ID to its representative point
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1089 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1090 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1091
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1092
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1093 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1094
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1095 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1096 // Compute dense_grid_seq
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1097 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1098 // generate_grid_seq(file_Len, num_dm, sorted_seq, num_dense_grids, densegridID_To_gridevent, position, dense_grid_rank, dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1099 /* Construct a table of binned data values for each dense grid.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1100 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1101 * Output:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1102 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1103 * dense_grid_seq -- table of binned data values for each dense grid (recall that all members of a grid have identical binned data values)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1104 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1105
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1106 void generate_grid_seq(long num_dm, long num_dense_grids, long *densegridID_To_gridevent, long **position, long **dense_grid_seq)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1107 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1108
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1109 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1110 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1111 long ReventID=0; //representative event ID of the dense grid
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1112
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1113 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1114 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1115 ReventID = densegridID_To_gridevent[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1116
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1117 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1118 dense_grid_seq[i][j]=position[ReventID][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1119 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1120 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1121
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1122 //compare two vectors
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1123 long compare_value(long num_dm, long *search_value, long *seq_value)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1124 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1125 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1126
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1127 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1128 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1129 if (search_value[i]<seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1130 return 1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1131 if (search_value[i]>seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1132 return -1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1133 if (search_value[i]==seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1134 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1135 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1136 return 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1137 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1138
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1139 //binary search the dense_grid_seq to return the dense grid ID if it exists
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1140 long binary_search(long num_dense_grids, long num_dm, long *search_value, long **dense_grid_seq)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1141 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1142
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1143 long low=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1144 long high=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1145 long mid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1146 long comp_result=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1147 long match=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1148 //long found=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1149
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1150 low = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1151 high = num_dense_grids-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1152
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1153 while (low <= high)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1154 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1155 mid = (long)((low + high)/2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1156
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1157 comp_result=compare_value(num_dm, search_value,dense_grid_seq[mid]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1158
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1159
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1160 switch(comp_result)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1161 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1162 case 1:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1163 high=mid-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1164 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1165 case -1:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1166 low=mid+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1167 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1168 case 0:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1169 match=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1170 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1171 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1172 if (match==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1173 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1174 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1175
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1176
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1177
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1178 if (match==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1179 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1180 return mid;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1181 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1182 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1183 return -1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1184 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1185
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1186
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1187 /********************************************** Computing Centers Using IDs **********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1188
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1189 void ID2Center(double **data_in, long file_Len, long num_dm, long *eventID_To_denseventID, long num_clust, long *cluster_ID, double **population_center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1190 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1191 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1192 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1193 long ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1194 long eventID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1195 long *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1196
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1197
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1198
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1199 size_c=(long *)malloc(sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1200 memset(size_c,0,sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1201
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1202 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1203 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1204 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1205
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1206 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1207 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1208 eventID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1209
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1210 if (eventID!=-1) //only events in dense grids count
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1211 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1212 ID=cluster_ID[eventID];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1213
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1214 if (ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1215 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1216 //printf("ID==-1! in ID2Center\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1217 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1218 fprintf(stderr,"Incorrect file format or input parameters (no dense regions found!)\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1219 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1220 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1221
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1222 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1223 population_center[ID][j]=population_center[ID][j]+data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1224
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1225 size_c[ID]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1226 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1227 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1228
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1229
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1230 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1231 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1232 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1233 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1234 population_center[i][j]=(population_center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1235 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1236 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1237 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1238 printf("size_c[%ld]=0 in ID2center\n",i);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1239 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1240 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1241
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1242 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1243
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1244 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1245
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1246 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1247 // Compute Population Center with all events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1248 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1249 void ID2Center_all(double **data_in, long file_Len, long num_dm, long num_clust, long *cluster_ID, double **population_center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1250 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1251 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1252 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1253 long ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1254 long *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1255
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1256
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1257
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1258 size_c=(long *)malloc(sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1259 memset(size_c,0,sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1260
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1261 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1262 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1263 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1264
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1265
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1266 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1267 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1268 ID=cluster_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1269
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1270 if (ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1271 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1272 //printf("ID==-1! in ID2Center_all\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1273 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1274 fprintf(stderr,"Incorrect file format or input parameters (resulting in incorrect population IDs)\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1275 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1276 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1277
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1278 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1279 population_center[ID][j]=population_center[ID][j]+data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1280
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1281 size_c[ID]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1282 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1283
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1284
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1285 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1286 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1287 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1288 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1289 population_center[i][j]=(population_center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1290 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1291 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1292 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1293 printf("size_c[%ld]=0 in ID2center_all\n",i);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1294 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1295 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1296
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1297
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1298 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1299
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1300 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1301
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1302 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1303 // Merge neighboring grids to clusters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1304 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1305
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1306 long merge_grids(double **normalized_data, double *interval, long file_Len, long num_dm, long num_bin, long **position, long num_dense_grids,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1307 long *dense_grid_reverse, long **dense_grid_seq, long *eventID_To_denseventID, long *densegridID_To_gridevent, long *all_grid_ID,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1308 long *cluster_ID, long *grid_ID, long *grid_clusterID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1309 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1310
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1311
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1312 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1313 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1314 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1315 long p=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1316 long num_clust=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1317 long ReventID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1318 long denseID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1319 long neighbor_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1320 //long temp_grid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1321
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1322 long *grid_value;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1323 long *search_value;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1324
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1325 long **graph_of_grid; //the graph constructed for dense grids: each dense grid is a graph node
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1326
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1327 double real_dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1328 double **norm_grid_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1329
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1330 norm_grid_center=(double **)malloc(sizeof(double*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1331 memset(norm_grid_center,0,sizeof(double*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1332
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1333 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1334 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1335 norm_grid_center[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1336 memset(norm_grid_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1337 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1338
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1339 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1340 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1341 denseID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1342 if (denseID!=-1) //only dense events can enter
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1343 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1344 grid_ID[denseID]=dense_grid_reverse[all_grid_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1345
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1346 if (grid_ID[denseID]==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1347 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1348 fprintf(stderr,"Incorrect input file format or input parameters (no dense region found)!\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1349 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1350 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1351 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1352 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1353
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1354
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1355 /* Find centroid (in the normalized data) for each dense grid */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1356 ID2Center(normalized_data,file_Len,num_dm,eventID_To_denseventID,num_dense_grids,grid_ID,norm_grid_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1357
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1358 //printf("pass the grid ID2 center\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1359
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1360
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1361 graph_of_grid=(long **)malloc(sizeof(long*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1362 memset(graph_of_grid,0,sizeof(long*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1363 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1364 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1365 graph_of_grid[i]=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1366 memset(graph_of_grid[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1367
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1368
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1369 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1370 graph_of_grid[i][j]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1371 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1372
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1373 grid_value=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1374 memset(grid_value,0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1375
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1376 search_value=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1377 memset(search_value,0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1378
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1379
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1380 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1381 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1382 ReventID=densegridID_To_gridevent[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1383
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1384 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1385 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1386 grid_value[j]=position[ReventID][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1387
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1388 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1389
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1390
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1391 /* For each dimension, find the neighbor, if any, that is equal in all other dimensions and 1 greater in
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1392 the chosen dimension. If the neighbor's centroid is not too far away, add it to this grid's neighbor
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1393 list. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1394 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1395 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1396 for (p=0;p<num_dm;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1397 search_value[p]=grid_value[p];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1398
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1399 if (grid_value[t]==num_bin-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1400 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1401
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1402 search_value[t]=grid_value[t]+1; //we only consider the neighbor at the bigger side
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1403
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1404 neighbor_ID=binary_search(num_dense_grids, num_dm, search_value, dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1405
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1406 if (neighbor_ID!=-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1407 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1408 real_dist=norm_grid_center[i][t]-norm_grid_center[neighbor_ID][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1409
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1410 if (real_dist<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1411 real_dist=-real_dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1412
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1413 if (real_dist<2*interval[t]) //by using 2*interval, this switch is void
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1414 graph_of_grid[i][t]=neighbor_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1415 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1416 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1417 grid_clusterID[i]=i; //initialize grid_clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1418 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1419
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1420
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1421 //graph constructed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1422 //DFS-based search begins
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1423
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1424 /* Use a depth-first search to construct a list of connected subgraphs (= "clusters").
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1425 Note our graph as we have constructed it is a DAG, so we can use that to our advantage
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1426 in our search. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1427 // num_clust=dfs(graph_of_grid,num_dense_grids,num_dm,grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1428 num_clust=find_connected(graph_of_grid, num_dense_grids, num_dm, grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1429
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1430
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1431 //computes grid_ID and cluster_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1432 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1433 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1434 denseID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1435 if (denseID!=-1) //only dense events can enter
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1436 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1437 cluster_ID[denseID]=grid_clusterID[grid_ID[denseID]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1438 //if (cluster_ID[denseID]==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1439 // printf("catch you!\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1440 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1441 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1442
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1443 free(search_value);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1444 free(grid_value);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1445
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1446 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1447 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1448 free(graph_of_grid[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1449 free(norm_grid_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1450 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1451 free(graph_of_grid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1452 free(norm_grid_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1453
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1454 return num_clust;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1455 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1456
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1457 /********************************************* Merge Clusters to Populations *******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1458
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1459 long merge_clusters(long num_clust, long num_dm, double *interval, double **cluster_center, long *cluster_populationID, long max_num_pop)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1460 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1461 long num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1462 long temp_num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1463
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1464 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1465 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1466 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1467 long merge=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1468 long smid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1469 long bgid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1470 double merge_dist=1.1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1471
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1472 long *map_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1473
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1474 double diff=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1475
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1476 map_ID=(long*)malloc(sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1477 memset(map_ID,0,sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1478
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1479 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1480 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1481 cluster_populationID[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1482 map_ID[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1483 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1484
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1485
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1486 while (((num_population>max_num_pop) && (merge_dist<5.0)) || ((num_population<=1) && (merge_dist>0.1)))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1487 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1488
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1489 if (num_population<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1490 merge_dist=merge_dist-0.1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1491
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1492 if (num_population>max_num_pop)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1493 merge_dist=merge_dist+0.1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1494
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1495
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1496 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1497 cluster_populationID[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1498
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1499 for (i=0;i<num_clust-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1500 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1501 for (j=i+1;j<num_clust;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1502 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1503 merge=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1504
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1505 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1506 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1507 diff=cluster_center[i][t]-cluster_center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1508
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1509 if (diff<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1510 diff=-diff;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1511 if (diff>(merge_dist*interval[t]))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1512 merge=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1513 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1514
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1515 if ((merge) && (cluster_populationID[i]!=cluster_populationID[j]))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1516 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1517 if (cluster_populationID[i]<cluster_populationID[j]) //they could not be equal
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1518 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1519 smid = cluster_populationID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1520 bgid = cluster_populationID[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1521 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1522 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1523 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1524 smid = cluster_populationID[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1525 bgid = cluster_populationID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1526 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1527 for (t=0;t<num_clust;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1528 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1529 if (cluster_populationID[t]==bgid)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1530 cluster_populationID[t]=smid;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1531 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1532 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1533 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1534 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1535
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1536
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1537
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1538 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1539 map_ID[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1540
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1541 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1542 map_ID[cluster_populationID[i]]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1543
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1544 num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1545 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1546 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1547 if (map_ID[i]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1548 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1549 map_ID[i]=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1550 num_population++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1551 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1552 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1553
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1554 if ((temp_num_population>max_num_pop) && (num_population==1))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1555 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1556 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1557 temp_num_population=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1558
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1559 if (num_clust<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1560 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1561 } //end while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1562
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1563 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1564 cluster_populationID[i]=map_ID[cluster_populationID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1565
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1566 free(map_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1567
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1568 return num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1569 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1570
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1571 ///////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1572 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1573 double kmeans(double **Matrix, long k, double kmean_term, long file_Len, long num_dm, long *shortest_id, double **center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1574 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1575
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1576 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1577 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1578 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1579 long random=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1580 long random1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1581 long random2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1582 long times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1583 long dist_used=0; //0 is Euclidean and 1 is Pearson
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1584 long random_init=0; //0: not use random seeds;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1585 long real_Len=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1586 long skipped=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1587
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1588 long *num; //num[i]=t means the ith cluster has t points
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1589
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1590 double vvv=1.0; // the biggest variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1591 double distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1592 double xv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1593 double variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1594
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1595 double mean_dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1596 double mean_dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1597 double sum_var=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1598 double dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1599 double dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1600 double sd_x=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1601 double sd_y=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1602 double diff=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1603 double distortion=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1604 double sd=0; //standard deviation
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1605 double shortest_distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1606
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1607 double *temp_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1608
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1609 double **sum;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1610
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1611 temp_center = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1612 memset(temp_center,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1613
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1614 if (random_init)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1615 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1616 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1617 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1618 random1=rand()*rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1619 random2=abs((random1%5)+1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1620 for (t=0;t<random2;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1621 random2=random2*rand()+rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1622
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1623 random=abs(random2%file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1624 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1625 center[i][j]=Matrix[random][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1626 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1627 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1628
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1629
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1630 num = (long *)malloc(sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1631 memset(num,0,sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1632
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1633 sum = (double **)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1634 memset(sum,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1635 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1636 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1637 sum[i] = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1638 memset(sum[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1639 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1640
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1641
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1642 times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1643 real_Len=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1644
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1645 while (((vvv>kmean_term) && (kmean_term<1)) || ((times<kmean_term) && (kmean_term>=1)))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1646 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1647 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1648 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1649 num[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1650 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1651 sum[i][j]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1652 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1653
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1654 for (i=0;i<file_Len;i++) //for each data point i, we compute the distance between Matrix[i] and center[j]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1655 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1656 skipped = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1657 shortest_distance=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1658 for (j=0;j<k;j++) //for each center j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1659 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1660 distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1661
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1662 if (dist_used==0) //Euclidean distance
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1663 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1664 for (t=0;t<num_dm;t++) //for each dimension here num_dm is always 1 as we consider individual dimensions
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1665 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1666
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1667 diff=center[j][t]-Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1668
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1669 diff=diff*diff;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1670 //this K-means is only used for dimension selection, and therefore for 1-dimensional data only, no need to use cube distance
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1671
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1672 distance = distance+diff; //here we have a weight for each dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1673 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1674 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1675 else //pearson correlation
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1676 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1677 mean_dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1678 mean_dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1679 sum_var=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1680 dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1681 dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1682 sd_x=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1683 sd_y=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1684 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1685 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1686 mean_dx+=center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1687 mean_dy+=Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1688 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1689 mean_dx=mean_dx/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1690 mean_dy=mean_dy/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1691
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1692 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1693 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1694 dx=center[j][t]-mean_dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1695 dy=Matrix[i][t]-mean_dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1696 sum_var+=dx*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1697 sd_x+=dx*dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1698 sd_y+=dy*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1699 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1700 if (sqrt(sd_x*sd_y)==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1701 distance = 1.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1702 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1703 distance = 1.0 - (sum_var/(sqrt(sd_x*sd_y))); // distance ranges from 0 to 2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1704 //printf("distance=%f\n",distance);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1705 } //pearson correlation ends
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1706
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1707 if ((distance<shortest_distance) && (skipped == 0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1708 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1709 shortest_distance=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1710 shortest_id[i]=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1711 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1712 }//end for j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1713 real_Len++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1714 num[shortest_id[i]]=num[shortest_id[i]]+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1715 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1716 sum[shortest_id[i]][t]=sum[shortest_id[i]][t]+Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1717 }//end for i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1718 /* recompute the centers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1719 //compute_mean(group);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1720 vvv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1721 for (j=0;j<k;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1722 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1723 memcpy(temp_center,center[j],sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1724 variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1725 if (num[j]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1726 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1727 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1728 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1729 center[j][t]=sum[j][t]/(double)num[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1730 xv=(temp_center[t]-center[j][t]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1731 variation=variation+xv*xv;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1732 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1733 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1734
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1735 if (variation>vvv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1736 vvv=variation; //vvv is the biggest variation among the k clusters;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1737 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1738 //compute_variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1739 times++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1740 } //end for while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1741
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1742
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1743
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1744
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1745 free(num);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1746
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1747 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1748 free(sum[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1749 free(sum);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1750 free(temp_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1751
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1752
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1753 return distortion;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1754
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1755 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1756
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1757 //////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1758 /*************************** Show *****************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1759 void show(double **Matrix, long *cluster_id, long file_Len, long k, long num_dm, char *name_string)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1760 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1761 int situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1762 int situ2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1763
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1764 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1765 long id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1766 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1767 long info_id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1768 long nearest_id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1769 long insert=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1770 long temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1771 long m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1772 long n=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1773 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1774
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1775 long *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1776
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1777
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1778
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1779 long **size_mybound_1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1780 long **size_mybound_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1781 long **size_mybound_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1782 long **size_mybound_0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1783
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1784 double interval=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1785
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1786 double *big;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1787 double *small;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1788
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1789
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1790 double **center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1791 double **mybound;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1792
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1793 long **prof; //prof[i][j]=1 means population i is + at parameter j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1794
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1795 FILE *fpcnt_id; //proportion id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1796 //FILE *fcent_id; //center_id, i.e., centers of clusters within the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1797 FILE *fprof_id; //profile_id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1798
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1799 big=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1800 memset(big,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1801
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1802 small=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1803 memset(small,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1804
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1805 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1806 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1807 big[i]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1808 small[i]=(double)MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1809 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1810
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1811
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1812 size_c=(long *)malloc(sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1813 memset(size_c,0,sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1814
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1815 center=(double**)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1816 memset(center,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1817 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1818 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1819 center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1820 memset(center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1821 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1822
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1823 mybound=(double**)malloc(sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1824 memset(mybound,0,sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1825 for (i=0;i<num_dm;i++) //there are 3 mybounds for 4 categories
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1826 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1827 mybound[i]=(double*)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1828 memset(mybound[i],0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1829 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1830
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1831 prof=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1832 memset(prof,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1833 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1834 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1835 prof[i]=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1836 memset(prof[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1837 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1838
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1839
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1840 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1841 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1842 id=cluster_id[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1843 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1844 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1845 center[id][j]=center[id][j]+Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1846 if (big[j]<Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1847 big[j]=Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1848 if (small[j]>Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1849 small[j]=Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1850 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1851
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1852 size_c[id]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1853 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1854
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1855 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1856 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1857 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1858 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1859 center[i][j]=(center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1860 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1861 center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1862 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1863
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1864 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1865 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1866 interval=((big[j]-small[j])/4.0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1867 //printf("interval[%d] is %f\n",j,interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1868 for (i=0;i<3;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1869 mybound[j][i]=small[j]+((double)(i+1)*interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1870 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1871
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1872
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1873 size_mybound_0=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1874 memset(size_mybound_0,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1875
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1876 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1877 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1878 size_mybound_0[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1879 memset(size_mybound_0[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1880 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1881
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1882 size_mybound_1=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1883 memset(size_mybound_1,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1884
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1885 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1886 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1887 size_mybound_1[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1888 memset(size_mybound_1[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1889 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1890
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1891 size_mybound_2=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1892 memset(size_mybound_2,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1893
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1894 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1895 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1896 size_mybound_2[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1897 memset(size_mybound_2[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1898 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1899
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1900 size_mybound_3=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1901 memset(size_mybound_3,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1902
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1903 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1904 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1905 size_mybound_3[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1906 memset(size_mybound_3[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1907 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1908
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1909 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1910 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1911 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1912 if (Matrix[i][j]<mybound[j][0])// && ((Matrix[i][j]-small[j])>0)) //the smallest values excluded
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1913 size_mybound_0[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1914 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1915 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1916 if (Matrix[i][j]<mybound[j][1])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1917 size_mybound_1[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1918 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1919 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1920 if (Matrix[i][j]<mybound[j][2])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1921 size_mybound_2[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1922 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1923 //if (Matrix[i][j]!=big[j]) //the biggest values excluded
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1924 size_mybound_3[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1925 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1926
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1927 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1928 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1929
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1930 fprof_id=fopen("profile.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1931 fprintf(fprof_id,"Population_ID\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1932 fprintf(fprof_id,"%s\n",name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1933
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1934 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1935 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1936 fprintf(fprof_id,"%ld\t",i+1); //i changed to i+1 to start from 1 instead of 0: April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1937 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1938 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1939
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1940 if (size_mybound_0[i][j]>size_mybound_1[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1941 situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1942 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1943 situ1=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1944 if (size_mybound_2[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1945 situ2=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1946 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1947 situ2=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1948
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1949 if ((situ1==0) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1950 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1951 if (size_mybound_0[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1952 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1953 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1954 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1955 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1956 if ((situ1==0) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1957 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1958 if (size_mybound_0[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1959 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1960 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1961 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1962 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1963 if ((situ1==1) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1964 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1965 if (size_mybound_1[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1966 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1967 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1968 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1969 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1970 if ((situ1==1) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1971 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1972 if (size_mybound_1[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1973 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1974 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1975 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1976 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1977
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1978 //begin to output profile
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1979 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1980 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1981 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1982 fprintf(fprof_id,"1\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1983 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1984 fprintf(fprof_id,"2\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1985 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1986 fprintf(fprof_id,"3\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1987 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1988 fprintf(fprof_id,"4\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1989 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1990 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1991 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1992 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1993 fprintf(fprof_id,"1\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1994 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1995 fprintf(fprof_id,"2\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1996 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1997 fprintf(fprof_id,"3\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1998 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1999 fprintf(fprof_id,"4\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2000 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2001 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2002 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2003 fclose(fprof_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2004
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2005 ///////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2006
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2007
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2008 fpcnt_id=fopen("percentage.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2009 fprintf(fpcnt_id,"Population_ID\tPercentage\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2011 for (t=0;t<k;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2012 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2013 fprintf(fpcnt_id,"%ld\t%.2f\n",t+1,(double)size_c[t]*100.0/(double)file_Len); //t changed to t+1 to start from 1 instead of 0: April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2014 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2015 fclose(fpcnt_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2016
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2017 free(big);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2018 free(small);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2019 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2020
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2021 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2022 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2023 free(center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2024 free(prof[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2025 free(size_mybound_0[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2026 free(size_mybound_1[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2027 free(size_mybound_2[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2028 free(size_mybound_3[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2029 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2030 free(center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2031 free(prof);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2032 free(size_mybound_0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2033 free(size_mybound_1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2034 free(size_mybound_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2035 free(size_mybound_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2036
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2037 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2038 free(mybound[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2039 free(mybound);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2040
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2041 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2042 ///////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2043
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2044 double get_avg_dist(double *population_center, long smaller_pop_ID, long larger_pop_ID, long *population_ID, long num_population, long file_Len,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2045 long num_dm, double **normalized_data, long d1, long d2, long d3, long *size_c)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2046 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2047 long k=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2048 long temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2049 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2050 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2051 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2052 long current_biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2053
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2054
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2055 double total_dist=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2056 double distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2057 double dist=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2058 double biggest_distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2059
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2060 long *nearest_neighbors;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2061 double *nearest_distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2062
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2063 k=min(size_c[smaller_pop_ID],size_c[larger_pop_ID]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2064
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2065 if (k>0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2066 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2067 k=(long)sqrt((double)k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2068 if (num_dm<=3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2069 k=(long)(2.7183*(double)k); //e*k for low-dimensional space, added Nov. 4, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2070 //printf("the k-nearest k is %d\n",k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2071 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2072 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2073 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2074 printf("error in k\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2075 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2076 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2077
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2078 nearest_neighbors=(long *)malloc(sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2079 memset(nearest_neighbors,0,sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2080
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2081 nearest_distance=(double *)malloc(sizeof(double)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2082 memset(nearest_distance,0,sizeof(double)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2083
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2084 temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2085
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2086 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2087 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2088 if ((population_ID[i]==smaller_pop_ID) || (population_ID[i]==larger_pop_ID)) //the event belongs to the pair of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2089 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2090 distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2091 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2092 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2093 if ((t!=d1) && (t!=d2) && (t!=d3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2094 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2095 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2096 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2097 dist=population_center[t]-normalized_data[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2098 distance=distance+dist*dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2099 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2100 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2101
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2102 if (temp<k)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2103 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2104 nearest_neighbors[temp]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2105 nearest_distance[temp]=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2106 temp++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2107 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2108 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2109 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2110 biggest_distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2111 for (j=0;j<k;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2112 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2113 if (nearest_distance[j]>biggest_distance)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2114 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2115 biggest_distance=nearest_distance[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2116 current_biggest=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2117 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2118 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2119
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2120 if (biggest_distance>distance)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2121 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2122 nearest_neighbors[current_biggest]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2123 nearest_distance[current_biggest]=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2124 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2125 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2126 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2127 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2128
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2129 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2130 total_dist=total_dist+nearest_distance[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2131
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2132 total_dist=total_dist/(double)k;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2133
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2134 free(nearest_distance);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2135 free(nearest_neighbors);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2136
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2137 return total_dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2138 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2139
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2140
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2141 /******************************************************** Main Function **************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2142 //for normalized data, there are five variables:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2143 //cluster_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2144 //population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2145 //grid_clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2146 //grid_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2147 //grid_Center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2148
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2149 //the same five variables exist for the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2150 //however, the three IDs (cluster_ID, grid_ID, grid_clusterID) don't change in either normalized or original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2151 //also, data + cluster_ID -> population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2152 //data + grid_ID -> grid_Center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2153
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2154 /* what is the final output */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2155 //the final things we want are grid_Center in the original data and grid_clusterID //or population_center in the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2156 //Sparse grids will not be considered when computing the two centroids (centroids of grids and centroids of clusters)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2157
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2158 /* what information should select_bin output */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2159 //the size of all IDs are unknown to function main because we only consider the events in dense grids, and also the number of dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2160 //is unknown, therefore I must use a prescreening to output
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2161 //how many bins I should use
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2162 //the number of dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2163 //total number of events in the dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2164
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2165 /* basic procedure of main function */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2166 //1. read raw file and normalize the raw file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2167 //2. select_bin
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2168 //3. allocate memory for eventID_To_denseventID, grid_clusterID, grid_ID, cluster_ID.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2169 //4. call select_dense and merge_grids with grid_clusterID, grid_ID, cluster_ID.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2170 //5. release normalized data; allocate memory for grid_Center and population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2171 //6. output grid_Center and population_center using ID2Center together with grid_clusterID //from IDs to centers
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2172
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2173 int main (int argc, char **argv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2174 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2175 //inputs
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2176 FILE *f_src; //source file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2177
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2178 FILE *f_out; //coordinates
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2179 FILE *f_cid; //population-ID of events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2180 FILE *f_ctr; //centroids of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2181 FILE *f_results; //coordinates file event and population column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2182 FILE *f_mfi; //added April 16, 2009 for mean fluorescence intensity
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2183 FILE *f_parameters; //number of bins and density calculated by
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2184 //the algorithm. Used to update the database
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2185 FILE *f_properties; //Properties file used by Image generation software
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2186
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2187 //char tmpbuf[128];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2188
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2189 char para_name_string[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2190
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2191 long time_ID=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2192 long num_bin=0; //the bins I will use on each dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2193 long num_pop=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2194 long file_Len=0; //number of events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2195 long num_dm=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2196 long num_clust=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2197 long num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2198 long num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2199 long num_nonempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2200 long num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2201 long num_real_pop=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2202 long keep_merge=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2203 long num_checked_range=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2204
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2205 //below are read from configuration file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2206 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2207 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2208 long p=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2209 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2210 long q=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2211 long temp_i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2212 long temp_j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2213 long first_i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2214 long first_j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2215 long d1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2216 long d2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2217 long d3=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2218 long d_d=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2219 long max_num_pop=0; //upperbound of the number of populations that is equal to 2^d
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2220 long index_id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2221 long num_computed_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2222 long tot_size=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2223
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2224 int den_t_event=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2225
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2226 double distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2227 double dist=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2228 double current_smallest_dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2229 double max_d_dist=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2230 double Ei=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2231 double Ej=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2232 double E1=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2233 double E2=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2234 double E3=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2235 double Ep=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2236 double temp=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2237 double tmp=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2238
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2239 long *grid_clusterID; //(dense)gridID to clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2240 long *grid_ID; //(dense)eventID to gridID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2241 long *cluster_ID; //(dense)eventID to clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2242 long *eventID_To_denseventID; //eventID_To_denseventID[i]=k means event i is in a dense grid and its ID within dense events is k
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2243 long *all_grid_ID; //gridID for all events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2244 long *densegridID_To_gridevent;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2245 long *sorted_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2246 long *dense_grid_reverse;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2247 long *population_ID; //denseeventID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2248 long *cluster_populationID; //clusterID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2249 long *grid_populationID; //gridID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2250 long *all_population_ID; //populationID of event
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2251 long *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2252 long *size_p_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2253 long *partit;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2254 long *size_p;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2255 long *temp_size_j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2256 long *all_computed_pop_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2257
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2258
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2259 long **position;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2260 long **dense_grid_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2261 long **temp_population_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2262
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2263 double *interval;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2264 double *center_1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2265 double *center_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2266 double *center_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2267 double *aver;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2268 double *std;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2269 double *center_p_1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2270 double *center_p_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2271 double *center_p_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2272
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2273 double **population_center; //population centroids in the raw/original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2274 double **cluster_center; //cluster centroids in the raw/original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2275 double **new_population_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2276 double **temp_population_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2277 double **min_pop;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2278 double **max_pop;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2279
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2280 double **input_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2281 double **normalized_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2282
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2283 double **real_pop_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2284 double **distance_pop;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2285
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2286 double ***ind_pop;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2287 double ***ind_pop_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2288
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2289 int min = 999999;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2290 int max = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2291
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2292 printf( "Starting time:\t\t\t\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2293 fflush(stdout);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2294 system("/bin/date");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2295
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2296 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2297 * Windows Version
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2298 _strtime( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2299 printf( "Starting time:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2300 _strdate( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2301 printf( "Starting date:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2302 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2303
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2304 /////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2305
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2306 if ((argc!=2) && (argc!=3) && (argc!=4) && (argc!=5) && (argc!=6))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2307 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2308 fprintf(stderr,"Incorrect number of input parameters!\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2309 fprintf(stderr,"usage:\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2310 fprintf(stderr,"basic mode: flock data_file\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2311 fprintf(stderr,"advanced mode 0 (specify maximum # of pops): flock data_file max_num_pop\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2312 fprintf(stderr,"advanced mode 1 (without # of pops): flock data_file num_bin density_index\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2313 fprintf(stderr,"advanced mode 2 (specify # of pops): flock data_file num_bin density_index number_of_pop\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2314 fprintf(stderr,"advanced mode 3 (specify both # of pops): flock data_file num_bin density_index number_of_pop max_num_pop\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2315 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2316 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2317
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2318 f_src=fopen(argv[1],"r");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2319
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2320 if (argc==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2321 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2322 max_num_pop=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2323 printf("num of maximum pop is %ld\n",max_num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2324 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2325
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2326 if (argc==4)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2327 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2328 num_bin=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2329 printf("num_bin is %ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2330
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2331 den_t_event=atoi(argv[3]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2332 printf("density_index is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2333 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2334
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2335 if (argc==5)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2336 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2337 num_bin=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2338 printf("num_bin is %ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2339
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2340 den_t_event=atoi(argv[3]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2341 printf("density_index is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2342
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2343 num_pop=atoi(argv[4]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2344 printf("num of pop is %ld\n",num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2345 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2346
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2347 if (argc==6)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2348 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2349 num_bin=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2350 printf("num_bin is %ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2351
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2352 den_t_event=atoi(argv[3]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2353 printf("density_index is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2354
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2355 num_pop=atoi(argv[4]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2356 printf("num of pop is %ld\n",num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2357
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2358 max_num_pop=atoi(argv[5]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2359 printf("num of pop is %ld\n",max_num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2360 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2361
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2362
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2363 if (num_pop==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2364 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2365 printf("it is not allowed to specify only one population\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2366 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2367 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2368
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2369
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2370
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2371 getfileinfo(f_src, &file_Len, &num_dm, para_name_string, &time_ID); //get the filelength, number of dimensions, and num/name of parameters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2372
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2373
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2374 if (max_num_pop==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2375 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2376 max_num_pop=(long)pow(2,num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2377 if (max_num_pop>MAX_POP_NUM)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2378 max_num_pop=MAX_POP_NUM;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2379 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2380
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2381 /************************************************* Read the data *****************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2382
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2383 rewind(f_src); //reset the file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2384
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2385 input_data = (double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2386 memset(input_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2387 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2388 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2389 input_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2390 memset(input_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2391 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2392
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2393 readsource(f_src, file_Len, num_dm, input_data, time_ID); //read the data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2394
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2395 fclose(f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2396
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2397 normalized_data=(double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2398 memset(normalized_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2399 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2400 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2401 normalized_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2402 memset(normalized_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2403 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2404
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2405 tran(input_data, file_Len, num_dm, NORM_METHOD, normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2406
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2407
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2408 position=(long **)malloc(sizeof(long*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2409 memset(position,0,sizeof(long*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2410 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2411 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2412 position[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2413 memset(position[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2414 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2415
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2416 all_grid_ID=(long *)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2417 memset(all_grid_ID,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2418
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2419 sorted_seq=(long*)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2420 memset(sorted_seq,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2421
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2422 interval=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2423 memset(interval,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2424
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2425 /************************************************* select_bin *************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2426
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2427 if (num_bin>=1) //num_bin has been selected by user
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2428 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2429 select_bin(normalized_data, file_Len, num_dm, MIN_GRID, MAX_GRID, position, sorted_seq, all_grid_ID, &num_nonempty, interval,num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2430 printf("user set bin number is %ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2431 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2432 else //num_bin has not been selected by user
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2433 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2434 num_bin=select_bin(normalized_data, file_Len, num_dm, MIN_GRID, MAX_GRID, position, sorted_seq, all_grid_ID, &num_nonempty, interval,num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2435 printf("selected bin number is %ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2436 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2437 printf("number of non-empty grids is %ld\n",num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2438
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2439
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2440
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2441 /* Although we return sorted_seq from select_bin(), we don't use it for anything, except possibly diagnostics */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2442 free(sorted_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2443
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2444
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2445 dense_grid_reverse=(long*)malloc(sizeof(long)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2446 memset(dense_grid_reverse,0,sizeof(long)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2447
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2448 /************************************************* select_dense **********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2449
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2450 if (den_t_event>=1) //den_t_event must be larger or equal to 2 if the user wants to set it
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2451 den_t_event=select_dense(file_Len, all_grid_ID, num_nonempty, &num_dense_grids, &num_dense_events, dense_grid_reverse, den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2452 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2453 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2454 den_t_event=select_dense(file_Len, all_grid_ID, num_nonempty, &num_dense_grids, &num_dense_events, dense_grid_reverse, den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2455 printf("automated selected density threshold is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2456 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2457
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2458 printf("Number of dense grids is %ld\n",num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2459
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2460 densegridID_To_gridevent = (long *)malloc(sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2461 memset(densegridID_To_gridevent,0,sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2462
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2463 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2464 densegridID_To_gridevent[i]=-1; //initialize all densegridID_To_gridevent values to -1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2465
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2466
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2467 eventID_To_denseventID=(long *)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2468 memset(eventID_To_denseventID,0,sizeof(long)*file_Len); //eventID_To_denseventID[i]=k means event i is in a dense grid and its ID within dense events is k
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2469
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2470
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2471 grid_To_event(file_Len, dense_grid_reverse, all_grid_ID, eventID_To_denseventID, densegridID_To_gridevent);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2472
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2473
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2474 dense_grid_seq=(long **)malloc(sizeof(long*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2475 memset(dense_grid_seq,0,sizeof(long*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2476 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2477 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2478 dense_grid_seq[i]=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2479 memset(dense_grid_seq[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2480 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2481
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2482
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2483 /* Look up the binned data values for each dense grid */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2484 generate_grid_seq(num_dm, num_dense_grids, densegridID_To_gridevent, position, dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2485
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2486
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2487 /************************************************* allocate memory *********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2488
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2489 grid_clusterID=(long *)malloc(sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2490 memset(grid_clusterID,0,sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2491
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2492 grid_ID=(long *)malloc(sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2493 memset(grid_ID,0,sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2494
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2495 cluster_ID=(long *)malloc(sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2496 memset(cluster_ID,0,sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2497
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2498
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2499 /*********************************************** merge_grids ***********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2500 //long merge_grids(long file_Len, long num_dm, long num_bin, long **position, long num_dense_grids, long *dense_grid_rank, long *dense_grid_reverse,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2501 // long **dense_grid_seq, long *eventID_To_denseventID, long *densegridID_To_gridevent, long *all_grid_ID,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2502 // long *cluster_ID, long *grid_ID, long *grid_clusterID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2503
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2504 num_clust = merge_grids(normalized_data, interval, file_Len, num_dm, num_bin, position, num_dense_grids, dense_grid_reverse, dense_grid_seq, eventID_To_denseventID, densegridID_To_gridevent, all_grid_ID, cluster_ID, grid_ID, grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2505
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2506 printf("computed number of groups is %ld\n",num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2507
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2508
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2509 /************************************** release unnecessary memory and allocate memory and compute centers **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2510
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2511
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2512 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2513 free(position[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2514 free(position);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2515
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2516 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2517 free(dense_grid_seq[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2518 free(dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2519
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2520 free(dense_grid_reverse);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2521
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2522 free(densegridID_To_gridevent);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2523 free(all_grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2524
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2525 // cluster_center ////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2526
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2527 cluster_center=(double **)malloc(sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2528 memset(cluster_center,0,sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2529 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2530 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2531 cluster_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2532 memset(cluster_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2533 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2534
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2535 ID2Center(normalized_data,file_Len,num_dm,eventID_To_denseventID,num_clust,cluster_ID,cluster_center); //produce the centers with normalized data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2536
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2537 //printf("pass the first ID2center\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2538
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2539 /*** population_ID and grid_populationID **/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2540
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2541 cluster_populationID=(long*)malloc(sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2542 memset(cluster_populationID,0,sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2543
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2544 grid_populationID=(long*)malloc(sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2545 memset(grid_populationID,0,sizeof(long)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2546
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2547 population_ID=(long*)malloc(sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2548 memset(population_ID,0,sizeof(long)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2549
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2550 num_population = merge_clusters(num_clust, num_dm, interval, cluster_center, cluster_populationID, max_num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2551
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2552
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2553
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2554 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2555 free(cluster_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2556 free(cluster_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2557
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2558 free(interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2559
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2560 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2561 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2562 grid_populationID[i]=cluster_populationID[grid_clusterID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2563 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2564
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2565 for (i=0;i<num_dense_events;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2566 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2567 population_ID[i]=cluster_populationID[cluster_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2568 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2569
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2570 printf("Number of merged groups before second-run partitioning is %ld\n",num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2571
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2572
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2573
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2574 // population_center /////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2575
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2576
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2577 population_center=(double **)malloc(sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2578 memset(population_center,0,sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2579 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2580 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2581 population_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2582 memset(population_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2583 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2584
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2585
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2586
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2587 ID2Center(normalized_data,file_Len,num_dm,eventID_To_denseventID,num_population,population_ID,population_center); //produce population centers with normalized data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2588
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2589
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2590 // show ////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2591 all_population_ID=(long*)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2592 memset(all_population_ID,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2593
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2594 kmeans(normalized_data, num_population, KMEANS_TERM, file_Len, num_dm, all_population_ID, population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2595
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2596 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2597 free(population_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2598
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2599 free(population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2600
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2601 ////// there needs to be another step to further partition the data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2602
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2603
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2604 center_p_1=(double *)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2605 memset(center_p_1,0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2606
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2607 center_p_2=(double *)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2608 memset(center_p_2,0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2609
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2610 center_p_3=(double *)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2611 memset(center_p_3,0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2612
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2613 size_p=(long *)malloc(sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2614 memset(size_p,0,sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2615
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2616 temp_size_j=(long *)malloc(sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2617 memset(temp_size_j,0,sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2618
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2619 all_computed_pop_ID=(long *)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2620 memset(all_computed_pop_ID,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2621
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2622 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2623 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2624 size_p[all_population_ID[i]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2625 all_computed_pop_ID[i]=all_population_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2626 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2627
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2628 ind_pop=(double***)malloc(sizeof(double**)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2629 memset(ind_pop,0,sizeof(double**)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2630
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2631 ind_pop_3=(double***)malloc(sizeof(double**)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2632 memset(ind_pop_3,0,sizeof(double**)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2633
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2634 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2635 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2636 ind_pop[i]=(double**)malloc(sizeof(double*)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2637 memset(ind_pop[i],0,sizeof(double*)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2638
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2639 ind_pop_3[i]=(double**)malloc(sizeof(double*)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2640 memset(ind_pop_3[i],0,sizeof(double*)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2641
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2642 for (j=0;j<size_p[i];j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2643 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2644 ind_pop[i][j]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2645 memset(ind_pop[i][j],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2646
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2647 ind_pop_3[i][j]=(double*)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2648 memset(ind_pop_3[i][j],0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2649 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2650 temp_size_j[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2651 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2652
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2653 for (i=0;i<file_Len;i++) //to generate ind_pop[i]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2654 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2655 index_id=all_population_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2656
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2657 j=temp_size_j[index_id];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2658
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2659 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2660 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2661 ind_pop[index_id][j][t]=input_data[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2662 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2663 temp_size_j[index_id]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2664 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2665
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2666 aver=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2667 memset(aver,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2668
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2669 std=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2670 memset(std,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2671
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2672 temp_population_center=(double**)malloc(sizeof(double*)*2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2673 memset(temp_population_center,0,sizeof(double*)*2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2674 for (i=0;i<2;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2675 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2676 temp_population_center[i]=(double*)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2677 memset(temp_population_center[i],0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2678 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2679
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2680 size_p_2=(long*)malloc(sizeof(long)*2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2681 memset(size_p_2,0,sizeof(long)*2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2682
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2683 partit=(long*)malloc(sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2684 memset(partit,0,sizeof(long)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2685
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2686 num_computed_population=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2687
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2688 temp_population_ID=(long**)malloc(sizeof(long*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2689 memset(temp_population_ID,0,sizeof(long*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2690
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2691 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2692 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2693 temp_population_ID[i]=(long*)malloc(sizeof(long)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2694 memset(temp_population_ID[i],0,sizeof(long)*size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2695 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2696
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2697 //printf("num_population=%d\n",num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2698
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2699 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2700 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2701 partit[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2702 tran(ind_pop[i], size_p[i], num_dm, 1, ind_pop[i]);//0-1 normalize ind_pop[i]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2703
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2704 //find the 3 dimensions with the largest std for ind_pop[i]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2705 d1=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2706 d2=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2707 d3=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2708
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2709 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2710 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2711 aver[t]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2712 for (j=0;j<size_p[i];j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2713 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2714 aver[t]=aver[t]+ind_pop[i][j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2715 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2716 aver[t]=aver[t]/(double)size_p[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2717
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2718 std[t]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2719 for (j=0;j<size_p[i];j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2720 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2721 std[t]=std[t]+((ind_pop[i][j][t]-aver[t])*(ind_pop[i][j][t]-aver[t]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2722 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2723 std[t]=sqrt(std[t]/(double)size_p[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2724 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2725
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2726
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2727
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2728 for (j=0;j<3;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2729 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2730 max_d_dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2731 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2732 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2733 if ((t!=d1) && (t!=d2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2734 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2735 dist=std[t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2736 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2737 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2738 dist=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2739
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2740 if (dist>max_d_dist)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2741 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2742 max_d_dist=dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2743 d_d=t;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2744 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2745 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2746
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2747 if (j==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2748 d1=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2749 if (j==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2750 d2=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2751 if (j==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2752 d3=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2753 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2754
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2755
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2756 for (t=0;t<size_p[i];t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2757 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2758 ind_pop_3[i][t][0]=ind_pop[i][t][d1];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2759 ind_pop_3[i][t][1]=ind_pop[i][t][d2];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2760 ind_pop_3[i][t][2]=ind_pop[i][t][d3];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2761 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2762
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2763
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2764 temp_population_center[0][0]=(aver[d1])/2.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2765
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2766 temp_population_center[0][1]=aver[d2];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2767 temp_population_center[0][2]=aver[d3];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2768
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2769 temp_population_center[1][0]=(aver[d1]+1.0)/2.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2770
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2771 temp_population_center[1][1]=aver[d2];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2772 temp_population_center[1][2]=aver[d3];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2773
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2774
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2775
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2776 //run K-means with K=2
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2777 kmeans(ind_pop_3[i], 2, KMEANS_TERM, size_p[i], 3, temp_population_ID[i], temp_population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2778
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2779 for (j=0;j<2;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2780 size_p_2[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2781
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2782 for (j=0;j<size_p[i];j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2783 size_p_2[temp_population_ID[i][j]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2784
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2785 for (j=0;j<2;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2786 if (size_p_2[j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2787 printf("size_p_2=0 at i=%ld\n",i);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2788
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2789
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2790 //check whether the 2 parts should be merged
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2791
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2792
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2793 Ei=get_avg_dist(temp_population_center[0], 0,1, temp_population_ID[i], 2,size_p[i], 3, ind_pop_3[i], 0,1,2,size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2794 Ej=get_avg_dist(temp_population_center[1], 0,1, temp_population_ID[i], 2,size_p[i], 3, ind_pop_3[i], 0,1,2,size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2795
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2796 for (t=0;t<3;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2797 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2798 center_p_1[t]=temp_population_center[0][t]*0.25+temp_population_center[1][t]*0.75;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2799 center_p_2[t]=temp_population_center[0][t]*0.5+temp_population_center[1][t]*0.5;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2800 center_p_3[t]=temp_population_center[0][t]*0.75+temp_population_center[1][t]*0.25;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2801 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2802
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2803 E1=get_avg_dist(center_p_1, 0,1, temp_population_ID[i], 2,size_p[i], 3, ind_pop_3[i], 0,1,2,size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2804
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2805 E2=get_avg_dist(center_p_2, 0,1, temp_population_ID[i], 2,size_p[i], 3, ind_pop_3[i], 0,1,2,size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2806
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2807 E3=get_avg_dist(center_p_3, 0,1, temp_population_ID[i], 2,size_p[i], 3, ind_pop_3[i], 0,1,2,size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2808
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2809 //printf("i=%d;E1=%f;E2=%f;E3=%f\n",i,E1,E2,E3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2810
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2811 if (E1<E2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2812 Ep=E2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2813 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2814 Ep=E1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2815
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2816 Ep=max(Ep,E3); //Ep is the most sparse area
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2817
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2818
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2819
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2820 if ((Ep>Ei) && (Ep>Ej)) //the two parts should be partitioned
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2821 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2822 partit[i]=num_computed_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2823 num_computed_population++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2824 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2825
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2826 } //end for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2827
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2828
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2829 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2830 temp_size_j[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2831
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2832 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2833 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2834 index_id=all_population_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2835 if (partit[index_id]>0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2836 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2837 j=temp_size_j[index_id];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2838 if (temp_population_ID[index_id][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2839 all_computed_pop_ID[i]=partit[index_id];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2840
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2841 temp_size_j[index_id]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2842 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2843 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2844
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2845 printf("Number of groups after partitioning is %ld\n", num_computed_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2846
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2847
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2848 free(size_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2849
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2850 free(aver);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2851 free(std);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2852 free(partit);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2853
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2854 free(center_p_1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2855 free(center_p_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2856 free(center_p_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2857
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2858
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2859
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2860 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2861 free(temp_population_ID[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2862 free(temp_population_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2863
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2864 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2865 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2866 for (j=0;j<size_p[i];j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2867 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2868 free(ind_pop[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2869 free(ind_pop_3[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2870 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2871 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2872
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2873
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2874
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2875 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2876 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2877 free(ind_pop[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2878 free(ind_pop_3[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2879 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2880 free(ind_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2881 free(ind_pop_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2882
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2883
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2884
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2885
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2886 for (i=0;i<2;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2887 free(temp_population_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2888
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2889 free(temp_population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2890
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2891 free(temp_size_j);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2892 free(size_p);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2893
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2894
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2895
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2896 //update the IDs, Centers, and # of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2897 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2898 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2899 all_population_ID[i]=all_computed_pop_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2900 if (all_population_ID[i]>=num_computed_population)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2901 printf("all_population_ID[%ld]=%ld\n",i,all_population_ID[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2902 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2903
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2904 num_population=num_computed_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2905
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2906 free(all_computed_pop_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2907 //end partitioning
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2908 // since the num_population has changed, population_center needs to be redefined as below
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2909
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2910 population_center=(double **)malloc(sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2911 memset(population_center,0,sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2912 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2913 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2914 population_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2915 memset(population_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2916 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2917
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2918
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2919 ID2Center_all(normalized_data,file_Len,num_dm,num_population,all_population_ID,population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2920
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2921
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2922
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2923 ////// end of further partitioning
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2924
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2925 ////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2926 // to further merge populations to avoid overpartitioning
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2927 // Added June 20, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2928 ////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2929
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2930
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2931
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2932 num_real_pop=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2933 keep_merge=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2934
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2935 if (num_pop>num_real_pop)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2936 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2937 fprintf(stderr,"number of populations specified too large\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2938 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2939 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2940
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2941 center_1=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2942 memset(center_1,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2943
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2944 center_2=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2945 memset(center_2,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2946
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2947 center_3=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2948 memset(center_3,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2949
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2950
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2951
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2952 while (((num_real_pop>num_pop) && (num_pop!=0)) || ((keep_merge==1) && (num_pop==0) && (num_real_pop>2)))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2953 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2954
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2955 keep_merge=0; //so, if no entering merge function, while will exit when num_pop==0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2956
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2957 real_pop_center=(double **)malloc(sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2958 memset(real_pop_center,0,sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2959 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2960 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2961 real_pop_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2962 memset(real_pop_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2963 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2964
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2965 min_pop=(double **)malloc(sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2966 memset(min_pop,0,sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2967 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2968 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2969 min_pop[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2970 memset(min_pop[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2971 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2972
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2973 max_pop=(double **)malloc(sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2974 memset(max_pop,0,sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2975 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2976 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2977 max_pop[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2978 memset(max_pop[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2979 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2980
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2981 if (num_real_pop==num_population)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2982 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2983 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2984 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2985 real_pop_center[i][j]=population_center[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2986 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2987 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2988 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2989 ID2Center_all(normalized_data,file_Len,num_dm,num_real_pop,all_population_ID,real_pop_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2990 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2991
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2992 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2993 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2994 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2995 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2996 min_pop[i][j]=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2997
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2998 max_pop[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2999 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3000 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3001
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3002 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3003 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3004 index_id=all_population_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3005 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3006 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3007 if (normalized_data[i][j]<min_pop[index_id][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3008 min_pop[index_id][j]=normalized_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3010 if (normalized_data[i][j]>max_pop[index_id][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3011 max_pop[index_id][j]=normalized_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3012 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3013 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3014
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3015 /* for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3016 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3017 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3018 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3019 printf("min_pop is %f\t",min_pop[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3020 //printf("max_pop is %f\n",max_pop[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3021 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3022 printf("\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3023 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3024 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3025
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3026
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3027 distance_pop=(double **)malloc(sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3028 memset(distance_pop,0,sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3029 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3030 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3031 distance_pop[i]=(double*)malloc(sizeof(double)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3032 memset(distance_pop[i],0,sizeof(double)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3033 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3034
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3035 for (i=0;i<num_real_pop-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3036 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3037 for (j=i+1;j<num_real_pop;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3038 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3039 distance=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3040
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3041 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3042 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3043 dist=real_pop_center[i][t]-real_pop_center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3044 distance=distance+dist*dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3045 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3046 distance_pop[i][j]=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3047 distance_pop[j][i]=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3048 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3049 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3050
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3051 if (num_real_pop>2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3052 num_checked_range=(long)((double)num_real_pop*(num_real_pop-1)/2.0); //to find the mergeable pair among the num_checked_range pairs of smallest distances
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3053 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3054 num_checked_range=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3055
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3056 size_c=(long *)malloc(sizeof(long)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3057 memset(size_c,0,sizeof(long)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3058
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3059 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3060 size_c[all_population_ID[i]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3061
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3062 for (p=0;p<num_checked_range;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3063 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3064 current_smallest_dist=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3065
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3066 for (i=0;i<num_real_pop-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3067 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3068 for (j=i+1;j<num_real_pop;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3069 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3070 if (distance_pop[i][j]<current_smallest_dist)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3071 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3072 current_smallest_dist=distance_pop[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3073
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3074 temp_i=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3075 temp_j=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3076 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3077 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3078 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3079
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3080 if (p==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3081 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3082 first_i=temp_i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3083 first_j=temp_j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3084 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3085
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3086 distance_pop[temp_i][temp_j]=MAX_VALUE+1; //make sure this pair won't be selected next time
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3087
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3088 //normalize and calculate std for temp_i and temp_j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3089
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3090 //pick the largest 3 dimensions on standard deviation
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3091 d1=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3092 d2=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3093 d3=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3094
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3095 for (i=0;i<3;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3096 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3097 max_d_dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3098 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3099 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3100 if ((j!=d1) && (j!=d2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3101 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3102 temp=min(min_pop[temp_i][j],min_pop[temp_j][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3103 tmp=max(max_pop[temp_i][j],max_pop[temp_j][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3104 if (tmp<=temp)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3105 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3106 //printf("min_pop[%d][%d]=%f \t min_pop[%d][%d]=%f \t max_pop[%d][%d]=%f \t max_pop[%d][%d]=%f\n",temp_i,j,min_pop[temp_i][j],temp_j, j, min_pop[temp_j][j],temp_i,j,max_pop[temp_i][j],temp_j,j,max_pop[temp_j][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3107 dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3108 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3109 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3110 dist=(real_pop_center[temp_i][j]-real_pop_center[temp_j][j])/(tmp-temp);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3111 if (dist<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3112 dist=-dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3113 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3114 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3115 dist=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3116
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3117 if (dist>max_d_dist)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3118 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3119 max_d_dist=dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3120 d_d=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3121 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3122 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3123
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3124 if (i==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3125 d1=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3126 if (i==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3127 d2=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3128 if (i==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3129 d3=d_d;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3130 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3131
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3132 //printf("d1=%d;d2=%d;d3=%d\n",d1,d2,d3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3133
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3134 Ei=get_avg_dist(real_pop_center[temp_i], temp_i,temp_j, all_population_ID, num_real_pop,file_Len, num_dm, normalized_data, d1,d2,d3,size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3135 Ej=get_avg_dist(real_pop_center[temp_j], temp_i,temp_j, all_population_ID, num_real_pop,file_Len, num_dm, normalized_data, d1,d2,d3,size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3136
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3137 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3138 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3139 center_1[t]=real_pop_center[temp_i][t]*0.25+real_pop_center[temp_j][t]*0.75;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3140 center_2[t]=real_pop_center[temp_i][t]*0.5+real_pop_center[temp_j][t]*0.5;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3141 center_3[t]=real_pop_center[temp_i][t]*0.75+real_pop_center[temp_j][t]*0.25;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3142 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3143
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3144 E1=get_avg_dist(center_1, temp_i,temp_j, all_population_ID, num_real_pop,file_Len, num_dm, normalized_data, d1,d2,d3,size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3145
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3146 E2=get_avg_dist(center_2, temp_i,temp_j, all_population_ID, num_real_pop,file_Len, num_dm, normalized_data, d1,d2,d3,size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3147
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3148 E3=get_avg_dist(center_3, temp_i,temp_j, all_population_ID, num_real_pop,file_Len, num_dm, normalized_data, d1,d2,d3,size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3149
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3150 if (E1<E2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3151 Ep=E2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3152 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3153 Ep=E1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3154
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3155 Ep=max(Ep,E3); //Ep is the most sparse area
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3156
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3157 Ep=Ep*E_T;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3158 //printf("Ep=%f;Ei=%f;Ej=%f\n",Ep,Ei,Ej);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3159
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3160 if ((Ep<=Ei) || (Ep<=Ej))//if the most sparse area between i and j are still denser than one of them, temp_i and temp_j should be merged
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3161 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3162 keep_merge=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3163 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3164 }//end if (Ep)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3165 } //end for p
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3166
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3167 //printf("keep_merge=%d\n",keep_merge);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3168
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3169 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3170 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3171 free(real_pop_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3172 free(distance_pop[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3173 free(min_pop[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3174 free(max_pop[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3175 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3176 free(real_pop_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3177 free(min_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3178 free(max_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3179 free(distance_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3180 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3181
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3182 //printf("temp_i=%d;temp_j=%d\n",temp_i,temp_j);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3183
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3184 if (keep_merge) //found one within p loop
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3185 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3186 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3187 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3188 if (all_population_ID[i]>temp_j)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3189 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3190 all_population_ID[i]=all_population_ID[i]-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3191 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3192 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3193 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3194 if (all_population_ID[i]==temp_j)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3195 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3196 all_population_ID[i]=temp_i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3197 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3198 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3199 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3200
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3201 num_real_pop--;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3202 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3203 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3204 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3205 if ((num_pop!=0) && (num_pop<num_real_pop)) //not reach the specified num of pop
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3206 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3207 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3208 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3209 if (all_population_ID[i]>first_j)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3210 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3211 all_population_ID[i]=all_population_ID[i]-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3212 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3213 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3214 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3215 if (all_population_ID[i]==first_j)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3216 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3217 all_population_ID[i]=first_i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3218 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3219 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3220 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3221
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3222 num_real_pop--;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3223
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3224
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3225
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3226 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3227 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3228 //printf("num_real_pop is %d\n",num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3229
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3230 } //end of while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3231
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3232
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3233
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3234 free(center_1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3235 free(center_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3236 free(center_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3237 printf("Final number of populations is %ld\n",num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3238
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3239 new_population_center=(double **)malloc(sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3240 memset(new_population_center,0,sizeof(double*)*num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3241 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3242 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3243 new_population_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3244 memset(new_population_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3245 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3246
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3247
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3248 ///////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3249 //End of population mapping
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3250 ///////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3251
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3252 show(input_data, all_population_ID, file_Len, num_real_pop, num_dm, para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3253
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3254 ID2Center_all(input_data,file_Len,num_dm,num_real_pop,all_population_ID,new_population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3255
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3256
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3257 f_cid=fopen("population_id.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3258 f_ctr=fopen("population_center.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3259 f_out=fopen("coordinates.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3260 f_results=fopen("flock_results.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3261
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3262 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3263 f_parameters=fopen("parameters.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3264 fprintf(f_parameters,"Number_of_Bins\t%d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3265 fprintf(f_parameters,"Density\t%f\n",aver_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3266 fclose(f_parameters);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3267 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3268
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3269 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3270 fprintf(f_cid,"%ld\n",all_population_ID[i]+1); //all_population_ID[i] changed to all_population_ID[i]+1 to start from 1 instead of 0: April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3271
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3272 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3273 * New to check for min/max to add to parameters.txt
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3274 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3275 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3276
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3277 fprintf(f_out,"%s\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3278 //fprintf(f_results,"%s\tEvent\tPopulation\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3279 fprintf(f_results,"%s\tPopulation\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3280 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3281 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3282 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3283 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3284 if (input_data[i][j] < min) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3285 min = (int)input_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3286 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3287 if (input_data[i][j] > max) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3288 max = (int)input_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3289 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3290 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3291 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3292 fprintf(f_out,"%d\n",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3293 fprintf(f_results,"%d\t",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3294 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3295 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3296 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3297 fprintf(f_out,"%d\t",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3298 fprintf(f_results,"%d\t",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3299 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3300 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3301 //fprintf(f_results,"%ld\t",i + 1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3302 fprintf(f_results,"%ld\n",all_population_ID[i]+1); //all_population_ID[i] changed to all_population_ID[i]+1 to start from 1 instead of 0: April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3303 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3304
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3305 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3306 f_parameters=fopen("parameters.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3307 fprintf(f_parameters,"Number_of_Bins\t%ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3308 fprintf(f_parameters,"Density\t%d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3309 fprintf(f_parameters,"Min\t%d\n",min);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3310 fprintf(f_parameters,"Max\t%d\n",max);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3311 fclose(f_parameters);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3312 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3313
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3314 f_properties=fopen("fcs.properties","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3315 fprintf(f_properties,"Bins=%ld\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3316 fprintf(f_properties,"Density=%d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3317 fprintf(f_properties,"Min=%d\n",min);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3318 fprintf(f_properties,"Max=%d\n",max);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3319 fprintf(f_properties,"Populations=%ld\n",num_real_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3320 fprintf(f_properties,"Events=%ld\n",file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3321 fprintf(f_properties,"Markers=%ld\n",num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3322 fclose(f_properties);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3323
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3324 for (i=0;i<num_real_pop;i++) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3325 /* Add if we want to include population id in the output
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3326 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3327 fprintf(f_ctr,"%ld\t",i+1); //i changed to i+1 to start from 1 instead of 0: April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3328
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3329 for (j=0;j<num_dm;j++) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3330 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3331 fprintf(f_ctr,"%.0f\n",new_population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3332 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3333 fprintf(f_ctr,"%.0f\t",new_population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3334 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3335 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3336
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3337 //added April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3338 f_mfi=fopen("MFI.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3339
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3340 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3341 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3342 fprintf(f_mfi,"%ld\t",i+1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3343
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3344 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3345 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3346 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3347 fprintf(f_mfi,"%.0f\n",new_population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3348 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3349 fprintf(f_mfi,"%.0f\t",new_population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3350 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3351 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3352 fclose(f_mfi);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3353
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3354 //ended April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3355
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3356 fclose(f_cid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3357 fclose(f_ctr);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3358 fclose(f_out);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3359 fclose(f_results);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3360
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3361
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3362 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3363 free(population_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3364
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3365 free(population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3367 for (i=0;i<num_real_pop;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3368 free(new_population_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3369
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3370 free(new_population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3371
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3372 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3373 free(normalized_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3374 free(normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3375
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3376 free(grid_populationID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3377
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3378 free(cluster_populationID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3379 free(grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3380 free(cluster_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3381
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3382 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3383 free(input_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3384 free(input_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3385
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3386 free(grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3387 free(population_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3388 free(all_population_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3389 free(eventID_To_denseventID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3390
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3391 ///////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3392 printf("Ending time:\t\t\t\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3393 fflush(stdout);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3394 system("/bin/date");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3395
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3396 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3397 * Windows version
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3398 _strtime( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3399 printf( "Ending time:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3400 _strdate( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3401 printf( "Ending date:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3402 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3403
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3404 return 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3405 }