annotate src/flock1.c @ 1:7eab80f86779 draft

add FLOCK
author immport-devteam
date Mon, 27 Feb 2017 13:26:09 -0500
parents
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3 FLOCK: FLOw cytometry Clustering without K (Named by: Jamie A. Lee and Richard H. Scheuermann)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
4
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
5 Author: (Max) Yu Qian, Ph.D.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
6
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
7 Copyright: Scheuermann Lab, Dept. of Pathology, UTSW
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
8
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
9 Development: November 2005 ~ Forever
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
10
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
11 Algorithm Status: May 2007: Release 1.0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
12
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
13 Usage: flock data_file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
14 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
15
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
16 Changes made July 23, 2010: made errors to STDERR
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
17 Changes made Nov 4, 2010: added one more error (select_num_bin<min_grid) || (select_num_bin>max_grid) to STDERR;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
18 MAX_GRID changed to 50 as larger than 50 seems not useful for any file we have got
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
19
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
20 ******************************************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
21 #include <time.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
22 #include <stdio.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
23 #include <stdlib.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
24 #include <math.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
25 #include <string.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
26 #include <sys/stat.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
27 #include <unistd.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
28 #include <assert.h>
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 #define DEBUG 0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
32 #define LINE_LEN 1024
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
33 #define FILE_NAME_LEN 128
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
34 #define PARA_NAME_LEN 64
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
35 #define MAX_VALUE 1000000000
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
36 #define MIN_GRID 6
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
37 #define MAX_GRID 50
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
38
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
39 #define NORM_METHOD 2 //2 if z-score; 0 if no normalization; 1 if min-max
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
40 #define KMEANS_TERM 100
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
41 //#define MAX_NUM_POP 30
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
42
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
43
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
44 int find_connected(int **G, int num_dense_grids, int ndim, int *grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
45
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
46 /************* Read basic info of the source file ****************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
47 void getfileinfo(FILE *f_src, int *file_Len, int *num_dm, char *name_string, int *time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
48 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
49 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
50 char current_name[64];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
51 char prv;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
52
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
53 int num_rows=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
54 int num_columns=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
55 int ch='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
56 int prev='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
57 int time_pos=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
58 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
59 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
60 int sw=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
61
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
62 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
63 fgets(src, LINE_LEN, f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
64
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
65 if ((src[0]=='F') && (src[1]=='C') && (src[2]=='S'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
66 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
67 fprintf(stderr,"the correct input format is a tab-delimited txt file, instead of FCS file.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
68 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
69 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
70
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
71 name_string[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
72 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
73 prv='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
74
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
75 // skip space and tab characters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
76 while ((src[i]==' ') || (src[i]=='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
77 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
78
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
79 // repeat until the end of line is reached
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
80 while ((src[i]!='\0') && (src[i]!='\n') && (src[i]!='\r'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
81 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
82 current_name[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
83
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
84 if ((src[i]=='\t') && (prv!='\t')) //a complete word
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
85 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
86 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
87
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
88 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
89 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
90 num_columns++; //num_columns does not inlcude the column of Time
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
91 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
92 if (sw) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
93 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
94 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
95 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
96 sw = 1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
97 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
98 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
99 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
100 *time_ID=time_pos;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
101 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
102
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
103
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
104 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
105 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
106 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
107
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
108 if ((src[i]=='\t') && (prv=='\t')) //a duplicate tab or space
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
109 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
110 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
111 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
112 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
113
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
114 if (src[i]!='\t')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
115 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
116
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
117 prv=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
118 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
119 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
120
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
121 if (prv!='\t') //the last one hasn't been retrieved
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
122 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
123 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
124
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
125 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
126 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
127 num_columns++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
128 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
129 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
130 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
131 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
132 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
133 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
134 *time_ID=time_pos;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
135 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
136
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
137
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
138 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
139 if (DEBUG==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
140 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
141 printf("time_ID is %d\n",*time_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
142 printf("name_string is %s\n",name_string);
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 //start computing # of rows
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
146
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
147 while ((ch = fgetc(f_src))!= EOF )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
148 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
149 if (ch == '\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
150 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
151 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
152 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
153 prev = ch;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
154 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
155 if (prev!='\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
156 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
157
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
158 //added on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
159 if (num_rows<50)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
160 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
161 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
162 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
163 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
164
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
165 *file_Len=num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
166 *num_dm=num_columns;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
167
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
168 printf("original file size is %d; number of dimensions is %d\n", *file_Len, *num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
169 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
170
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
171
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
172
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
173 /************************************* Read the source file into uncomp_data **************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
174 void readsource(FILE *f_src, int file_Len, int num_dm, double **uncomp_data, int time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
175 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
176 int time_pass=0; //to mark whether the time_ID has been passed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
177 int index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
178
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
179 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
180 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
181 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
182
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
183 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
184 char xc[LINE_LEN/10];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
185
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
186 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
187 fgets(src,LINE_LEN, f_src); //skip the first line about parameter names
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
188
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
189 while (!feof(f_src) && (index<file_Len)) //index = 0, 1, ..., file_Len-1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
190 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
191 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
192 fgets(src,LINE_LEN,f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
193 i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
194 time_pass=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
195
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
196 if (time_ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
197 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
198 for (t=0;t<num_dm;t++) //there is no time_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
199 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
200 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
201 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
202 while ((src[i]!='\0') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
203 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
204 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
205 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
206 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
207 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
208
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
209 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
210 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
211
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
212 uncomp_data[index][t]=atof(xc);
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 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
216 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
217 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
218 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
219 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
220 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
221 while ((src[i]!='\0') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
222 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
223 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
224 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
225 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
226 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
227
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
228 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
229 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
230
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
231 if (t==time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
232 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
233 time_pass=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
234 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
235 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
236
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
237 if (time_pass)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
238 uncomp_data[index][t-1]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
239 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
240 uncomp_data[index][t]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
241 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
242 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
243 index++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
244 //fprintf(fout_ID,"%s",src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
245 } //end of while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
246
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
247 if (DEBUG == 1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
248 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
249 printf("the last line of the source data is:\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
250 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
251 printf("%f ",uncomp_data[index-1][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
252 printf("\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
253 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
254 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
255
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
256
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
257 /**************************************** Normalization ******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
258 void tran(double **orig_data, int file_Len, int num_dm, int norm_used, double **matrix_to_cluster)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
259 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
260 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
261 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
262
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
263 double biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
264 double smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
265
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
266 double *aver; //average of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
267 double *std; //standard deviation of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
268
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
269 aver=(double*)malloc(sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
270 memset(aver,0,sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
271
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
272 std=(double*)malloc(sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
273 memset(std,0,sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
274
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
275 if (norm_used==2) //z-score normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
276 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
277 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
278 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
279 aver[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
280 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
281 aver[j]=aver[j]+orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
282 aver[j]=aver[j]/(double)file_Len;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
283
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
284 std[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
285 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
286 std[j]=std[j]+(orig_data[i][j]-aver[j])*(orig_data[i][j]-aver[j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
287 std[j]=sqrt(std[j]/(double)file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
288
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
289 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
290 matrix_to_cluster[i][j]=(orig_data[i][j]-aver[j])/std[j]; //z-score normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
291 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
292 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
293
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
294 if (norm_used==1) //0-1 min-max normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
295 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
296 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
297 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
298 biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
299 smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
300 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
301 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
302 if (orig_data[i][j]>biggest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
303 biggest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
304 if (orig_data[i][j]<smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
305 smallest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
306 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
307
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
308 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
309 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
310 if (biggest==smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
311 matrix_to_cluster[i][j]=biggest;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
312 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
313 matrix_to_cluster[i][j]=(orig_data[i][j]-smallest)/(biggest-smallest);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
314 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
315 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
316 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
317
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
318 if (norm_used==0) //no normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
319 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
320 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
321 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
322 matrix_to_cluster[i][j]=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
323 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
324
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
325 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
326
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 /********************************************** RadixSort *******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
330 /* Perform a radix sort using each dimension from the original data as a radix.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
331 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
332 * sorted_seq -- a permutation vector mapping the ordered list onto the original data.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
333 * (sorted_seq[i] -> index in the original data of the ith element of the ordered list)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
334 * grid_ID -- mapping between the original data and the "grids" (see below) found as a byproduct
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
335 * of the sorting procedure.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
336 * 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
337 * by grid_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
338 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
339
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
340 void radixsort_flock(int **position,int file_Len,int num_dm,int num_bin,int *sorted_seq,int *num_nonempty,int *grid_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
341 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
342 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
343 int length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
344 int start=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
345 int prev_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
346 int curr_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
347
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
348 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
349 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
350 int p=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
351 int loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
352 int temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
353 int equal=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
354
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
355 int *count; //count[i]=j means there are j numbers having value i at the processing digit
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
356 int *index; //index[i]=j means the starting position of grid i is j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
357 int *cp; //current position
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
358 int *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
359 int *seq; //temporary sequence
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
360
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
361 count=(int*)malloc(sizeof(int)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
362 memset(count,0,sizeof(int)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
363
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
364 cp=(int*)malloc(sizeof(int)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
365 memset(cp,0,sizeof(int)*num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
367 index=(int*)malloc(sizeof(int)*num_bin); // initialized below
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
368
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
369 seq=(int*)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
370 memset(seq,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
371
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
372 mark=(int*)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
373 memset(mark,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
374
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
375 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
376 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
377 sorted_seq[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
378 mark[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
379 seq[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
380 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
381 for (i=0;i<num_bin;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
382 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
383 index[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
384 cp[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
385 count[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
386 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
387
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
388 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
389 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
390 if (j==0) //compute the initial values of mark
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
391 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
392 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
393 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
394
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
395 index[0] = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
396 for (i=0;i<num_bin-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
397 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
398 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
399 if ((index[i+1]>0) && (index[i+1]<=file_Len))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
400 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
401 mark[index[i+1]-1]=1; // Mark the end of the segment in the ordered list
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
402 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
403 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
404 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
405 printf("out of myboundary for mark at index[i+1]-1.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
406 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
407 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
408 mark[file_Len-1]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
409
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
410 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
411 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
412 /* Build a permutation vector for the partially ordered data. Store the PV in sorted_seq */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
413 loc=position[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
414 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
415 sorted_seq[temp]=i; //sorted_seq[i]=temp is also another way to sort
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
416 cp[loc]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
417 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
418 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
419 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
420 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
421 //reset count, index, loc, temp, cp, start, and length
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
422 length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
423 loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
424 temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
425 start=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
426 for (p=0;p<num_bin;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
427 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
428 cp[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
429 count[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
430 index[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
431 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
432
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
433 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
434 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
435 int iperm = sorted_seq[i]; // iperm allows us to traverse the data in sorted order.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
436 if (mark[i]!=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
437 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
438 /* Count the number of items in each bin of
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
439 dimension j, BUT we are going to reset at the end
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
440 of each "part". Thus, the total effect is to do
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
441 a sort by bin on the jth dimension for each group
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
442 of data that has been identical for the
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
443 dimensions processed up to this point. This is
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
444 the standard radix sort procedure, but doing it
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
445 this way saves us having to allocate buckets to
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
446 hold the data in each group of "identical-so-far"
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
447 elements. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
448 count[position[iperm][j]]++; //count[position[i][j]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
449 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
450 // (actually, less one, since we don't increment for the final element below)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
451 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
452 if (mark[i]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
453 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
454 //length++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
455 count[position[iperm][j]]++;//count[position[i][j]]++; //the current point must be counted in
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
456 start=i-length; //this part starts from start to i: [start,i]
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
457 /* 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
458 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
459 plus we could multi-thread over the parts. (Hmmm...)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
460 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
461 index[0] = start; // Let index give the offset within the whole ordered list.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
462 for (t=0;t<num_bin-1;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
463 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
464 index[t+1]=index[t]+count[t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
465
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
466 if ((index[t+1]<=file_Len) && (index[t+1]>0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
467 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
468 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
469 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
470
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
471 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
472 mark[i]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
473
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
474 /* 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
475 the PV will be completely updated for the partial ordering up to the current radix. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
476 for (t=start;t<=i;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
477 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
478 loc=position[sorted_seq[t]][j];//loc=position[t][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
479 temp=index[loc]+cp[loc];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
480 if ((temp<file_Len) && (temp>=0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
481 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
482 // 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
483 seq[temp]=sorted_seq[t]; //sorted_seq[i]=temp is also another way to sort
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
484 cp[loc]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
485 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
486 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
487 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
488 printf("out of myboundary for seq at temp.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
489 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
490 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
491
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
492 for (t=start;t<=i;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
493 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
494 // 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
495 // entry i in the ordered list.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
496 if ((t>=0) && (t<file_Len))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
497 sorted_seq[t]=seq[t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
498 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
499 printf("out of myboundary for seq and sorted_seq at t.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
500 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
501 //reset count, index, seq, length, and cp
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
502 length=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
503 loc=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
504 temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
505 for (p=0;p<num_bin;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
506 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
507 cp[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
508 count[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
509 index[p]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
510 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
511 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
512 }//end for i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
513 }//end else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
514 }//end for j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
515
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
516 /* 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
517 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
518
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
519 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
520 grid_ID[i]=0; //in case the initial value hasn't been assigned
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
521 *num_nonempty=1; //starting from 1!
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
522
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
523 /* 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
524 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
525 be many possible combinations of bin values that simply never occur) */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
526
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
527 for (i=1;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
528 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
529 equal=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
530 prev_ID=sorted_seq[i-1];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
531 curr_ID=sorted_seq[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
532 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
533 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
534 if (position[prev_ID][j]!=position[curr_ID][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
535 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
536 equal=0; //not equal
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
537 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
538 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
539 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
540
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
541 if (equal)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
542 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
543 grid_ID[curr_ID]=grid_ID[prev_ID];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
544 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
545 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
546 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
547 *num_nonempty=*num_nonempty+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
548 grid_ID[curr_ID]=grid_ID[prev_ID]+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
549 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
550 //all_grid_vol[grid_ID[curr_ID]]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
551 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
552
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
553 //free memory
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
554 free(count);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
555 free(index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
556 free(cp);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
557 free(seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
558 free(mark);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
559
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
560 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
561
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
562 /********************************************** Compute Position of Events ************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
563 void compute_position(double **data_in, int file_Len, int num_dm, int num_bin, int **position, double *interval)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
564 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
565 /* What we are really doing here is binning the data, with the bins
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
566 spanning the range of the data and number of bins = num_bin */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
567 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
568 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
569
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
570 double *small; //small[j] is the smallest value within dimension j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
571 double *big; //big[j] is the biggest value within dimension j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
572
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
573 small=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
574 memset(small,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
575
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
576 big=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
577 memset(big,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
578
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
579
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
580 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
581 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
582 big[j]=MAX_VALUE*(-1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
583 small[j]=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
584 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
585 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
586 if (data_in[i][j]>big[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
587 big[j]=data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
588
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
589 if (data_in[i][j]<small[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
590 small[j]=data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
591 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
592
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
593 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
594 /* XXX: I'm pretty sure the denominator of the fraction above should be num_bin-1. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
595 /* I don't think so: num_bin is the number of bins */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
596 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
597
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
598 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
599 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
600 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
601 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
602 if (data_in[i][j]>=big[j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
603 position[i][j]=num_bin-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
604 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
605 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
606 position[i][j]=(int)((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
607 if ((position[i][j]>=num_bin) || (position[i][j]<0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
608 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
609 //printf("position mis-computed in density analysis!\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
610 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
611 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
612 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
613
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
614 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
615 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
616 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
617 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
618
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
619
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
620 free(small);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
621 free(big);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
622 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
623
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
624 /********************************************** select_bin to select the number of bins **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
625 //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
626 /* 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
627 * 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
628 * maximize a merit function, then return the results obtained using the optimal number of bins.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
629 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
630 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
631 * position -- binned data values
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
632 * sorted_seq -- permutation vector mapping the ordered list to the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
633 * all_grid_ID -- grid to which each data element was assigned.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
634 * num_nonempty -- number of distinct values assumed by all_grid_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
635 * interval -- bin width for each data dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
636 * return value -- the number of bins selected.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
637 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
638
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
639 int select_bin(double **normalized_data, int file_Len, int num_dm, int min_grid, int max_grid, int **position, int *sorted_seq,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
640 int *all_grid_ID, int *num_nonempty, double *interval, int user_num_bin)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
641 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
642
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
643 int num_bin=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
644 int select_num_bin=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
645 int m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
646 int n=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
647
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
648 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
649 int bin_scope=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
650 int temp_num_nonempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
651
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
652 int *temp_grid_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
653 int *temp_sorted_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
654 int **temp_position;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
655
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
656 //sorted_seq[i]=j means the event j ranks i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
657
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
658 double temp_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
659 double *bin_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
660 double *temp_interval;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
661
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
662
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
663
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
664 temp_grid_ID=(int *)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
665 memset(temp_grid_ID,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
666
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
667 temp_sorted_seq=(int *)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
668 memset(temp_sorted_seq,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
669
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
670 temp_position=(int **)malloc(sizeof(int*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
671 memset(temp_position,0,sizeof(int*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
672 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
673 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
674 temp_position[m]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
675 memset(temp_position[m],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
676 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
677
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
678 temp_interval=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
679 memset(temp_interval,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
680
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
681 bin_scope=max_grid-min_grid+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
682 bin_index=(double *)malloc(sizeof(double)*bin_scope);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
683 memset(bin_index,0,sizeof(double)*bin_scope);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
684
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
685 i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
686
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
687 for (num_bin=min_grid;num_bin<=max_grid;num_bin++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
688 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
689 /* compute_position bins the data into num_bin bins. Each
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
690 dimension is binned independently.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
691
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
692 Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
693 temp_position[i][j] -- bin for the jth component of data element i.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
694 temp_interval[j] -- bin-width for the jth component
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
695 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
696 compute_position(normalized_data, file_Len, num_dm, num_bin, temp_position, temp_interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
697 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
698
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
699 /* 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
700 We declare victory when we have found a local maximum */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
701 bin_index[i]=((double)temp_num_nonempty)/((double)num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
702 if ((double)(temp_num_nonempty)>=(double)(file_Len)*0.95)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
703 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
704 if ((bin_index[i]<temp_index) && (user_num_bin==0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
705 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
706 if ((user_num_bin==num_bin-1) && (user_num_bin!=0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
707 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
708
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
709 /* Since we have accepted this trial bin, copy all the temporary results into
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
710 the output buffers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
711 memcpy(all_grid_ID,temp_grid_ID,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
712 memcpy(sorted_seq,temp_sorted_seq,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
713 memcpy(interval,temp_interval,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
714
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
715 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
716 for (n=0;n<num_dm;n++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
717 position[m][n]=temp_position[m][n];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
718
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
719 temp_index=bin_index[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
720 select_num_bin=num_bin;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
721 num_nonempty[0]=temp_num_nonempty;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
722 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
723 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
724
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
725 if ((select_num_bin<min_grid) || (select_num_bin>max_grid))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
726 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
727 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 4, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
728 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
729 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
730
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
731 if (temp_index==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
732 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
733 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
734 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
735 }
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 free(temp_grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
739 free(temp_sorted_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
740 free(bin_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
741 free(temp_interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
742
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
743 for (m=0;m<file_Len;m++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
744 free(temp_position[m]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
745 free(temp_position);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
746
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
747 return select_num_bin;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
748 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
749
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
750 /************************************* Select dense grids **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
751 // compute num_dense_grids, num_dense_events, dense_grid_reverse, and all_grid_vol
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
752 // 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
753 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
754 * Prune away grids that are insufficiently "dense" (i.e., contain too few data items)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
755 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
756 * Outputs:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
757 * num_dense_grids -- number of dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
758 * num_dense_events -- total number of data items in all dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
759 * dense_grid_reverse -- mapping from list of all grids to list of dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
760 * return value -- density cutoff for separating dense from non-dense grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
761 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
762
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
763 int select_dense(int file_Len, int *all_grid_ID, int num_nonempty, int *num_dense_grids, int *num_dense_events, int *dense_grid_reverse, int den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
764 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
765
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
766
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
767 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
768 int vol_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
769 int biggest_size=0; //biggest grid_size, to define grid_size_index
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
770 int biggest_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
771 //int 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
772 //int num_remain=0; //number of remaining grids with different density thresholds
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
773 int temp_num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
774 int temp_num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
775
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
776 int *grid_size_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
777 int *all_grid_vol;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
778 int *grid_density_index;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
779
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
780 //double den_average=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
781 // double avr_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
782
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
783
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
784 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
785 // Compute all_grid_vol
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
786 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
787 all_grid_vol=(int *)malloc(sizeof(int)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
788 memset(all_grid_vol,0,sizeof(int)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
789
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
790 /* Grid "volume" is just the number of data contained in the grid. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
791 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
792 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
793 vol_ID=all_grid_ID[i]; //vol_ID=all_grid_ID[sorted_seq[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
794 all_grid_vol[vol_ID]++; //all_grid_vol[i]=j means grid i has j points
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
795 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
796
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 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
800 // Compute grid_size_index (histogram of grid sizes)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
801 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
802
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
803 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
804 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
805 if (biggest_size<all_grid_vol[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
806 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
807 biggest_size=all_grid_vol[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
808 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
809 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
810
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
811 //added on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
812 if (biggest_size<3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
813 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
814 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
815 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
816 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
817
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
818 grid_size_index=(int*)malloc(sizeof(int)*biggest_size);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
819 memset(grid_size_index,0,sizeof(int)*biggest_size);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
820
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
821 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
822 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
823 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
824 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
825
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
826
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
827
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
828 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
829 // Compute den_cutoff
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
830 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
831
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
832 grid_density_index=(int *)malloc(sizeof(int)*(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
833 memset(grid_density_index,0,sizeof(int)*(biggest_size-2));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
834
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
835 if (den_t_event==0) //user doesn't define the density threshold
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
836 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
837 biggest_index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
838
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
839 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
840 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
841 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
842 if (biggest_index<grid_density_index[i-1])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
843 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
844 biggest_index=grid_density_index[i-1];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
845 den_t_event=i+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
846 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
847 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
848 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
849
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
850 if (den_t_event==0) //if something is wrong
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
851 den_t_event=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
852
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
853 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
854 if (all_grid_vol[i]>=den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
855 temp_num_dense_grids++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
856
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
857 if (temp_num_dense_grids<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
858 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
859 //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
860 //printf("a too high density threshold is set! Please decrease your density threshold.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
861 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
862 fprintf(stderr,"a too high density threshold is set! Please decrease your density threshold.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
863 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
864 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
865
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
866 if (temp_num_dense_grids>=100000)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
867 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
868 //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
869 //printf("a too low density threshold is set! Please increase your density threshold.\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
870 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
871 fprintf(stderr,"a too low density threshold is set! Please increase your density threshold.\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
872 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
873 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
874
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
875 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
876 // Compute dense_grid_reverse
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
877 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
878 temp_num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
879 temp_num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
880 for (i=0;i<num_nonempty;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
881 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
882 dense_grid_reverse[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
883
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
884 if (all_grid_vol[i]>=den_t_event)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
885 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
886 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
887 temp_num_dense_grids++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
888 temp_num_dense_events+=all_grid_vol[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
889 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
890 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
891
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
892 num_dense_grids[0]=temp_num_dense_grids;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
893 num_dense_events[0]=temp_num_dense_events;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
894
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
895 free(grid_size_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
896 free(all_grid_vol);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
897
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
898 return den_t_event;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
899 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
900
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
901 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
902 // Compute densegridID_To_gridevent and eventID_To_denseventID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
903 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
904 // 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
905 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
906 * Filter the data so that only the data belonging to dense grids is left
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
907 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
908 * Output:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
909 * 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
910 * densegridID_To_gridevent -- mapping from dense grids to prototype members of the grids.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
911 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
912 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
913
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
914 void grid_To_event(int file_Len, int *dense_grid_reverse, int *all_grid_ID, int *eventID_To_denseventID, int *densegridID_To_gridevent)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
915 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
916 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
917 int dense_grid_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
918 int dense_event_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
919
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
920 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
921 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
922 dense_grid_ID=dense_grid_reverse[all_grid_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
923 eventID_To_denseventID[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
924 if (dense_grid_ID!=-1) //for point (i) belonging to dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
925 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
926 eventID_To_denseventID[i]=dense_event_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
927 dense_event_ID++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
928
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
929 if (densegridID_To_gridevent[dense_grid_ID]==-1) //for point i that hasn't been selected
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
930 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
931 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
932 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
933
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
934
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
935 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
936
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
937 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
938 // Compute dense_grid_seq
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
939 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
940 // 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
941 /* Construct a table of binned data values for each dense grid.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
942 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
943 * Output:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
944 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
945 * 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
946 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
947
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
948 void generate_grid_seq(int num_dm, int num_dense_grids, int *densegridID_To_gridevent, int **position, int **dense_grid_seq)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
949 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
950
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
951 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
952 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
953 int ReventID=0; //representative event ID of the dense grid
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
954
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
955 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
956 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
957 ReventID = densegridID_To_gridevent[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
958
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
959 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
960 dense_grid_seq[i][j]=position[ReventID][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
961 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
962 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
963
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
964 //compare two vectors
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
965 int compare_value(int num_dm, int *search_value, int *seq_value)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
966 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
967 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
968
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
969 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
970 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
971 if (search_value[i]<seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
972 return 1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
973 if (search_value[i]>seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
974 return -1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
975 if (search_value[i]==seq_value[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
976 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
977 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
978 return 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
979 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
980
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
981 //binary search the dense_grid_seq to return the dense grid ID if it exists
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
982 int binary_search(int num_dense_grids, int num_dm, int *search_value, int **dense_grid_seq)
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 int low=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
986 int high=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
987 int mid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
988 int comp_result=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
989 int match=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
990 //int found=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
991
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
992 low = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
993 high = num_dense_grids-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
994
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
995 while (low <= high)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
996 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
997 mid = (int)((low + high)/2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
998
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
999 comp_result=compare_value(num_dm, search_value,dense_grid_seq[mid]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1000
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1001
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1002 switch(comp_result)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1003 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1004 case 1:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1005 high=mid-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1006 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1007 case -1:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1008 low=mid+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1009 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1010 case 0:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1011 match=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1012 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1013 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1014 if (match==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1015 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1016 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1017
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1018
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1019
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1020 if (match==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1021 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1022 return mid;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1023 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1024 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1025 return -1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1026 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1027
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1028
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1029 /********************************************** Computing Centers Using IDs **********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1030
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1031 void ID2Center(double **data_in, int file_Len, int num_dm, int *eventID_To_denseventID, int num_clust, int *cluster_ID, double **population_center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1032 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1033 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1034 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1035 int ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1036 int eventID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1037 int *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1038
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1039
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1040
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1041 size_c=(int *)malloc(sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1042 memset(size_c,0,sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1043
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1044 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1045 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1046 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1047
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1048 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1049 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1050 eventID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1051
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1052 if (eventID!=-1) //only events in dense grids count
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1053 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1054 ID=cluster_ID[eventID];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1055
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1056 if (ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1057 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1058 //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1059 //printf("ID==-1! in ID2Center\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1060 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1061 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
1062 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1063 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1064
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1065 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1066 population_center[ID][j]=population_center[ID][j]+data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1067
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1068 size_c[ID]++;
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1073 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1074 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1075 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1076 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1077 population_center[i][j]=(population_center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1078 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1079 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1080 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1081
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1082 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1083
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1084 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1085
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1086 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1087 // Compute Population Center with all events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1088 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1089 void ID2Center_all(double **data_in, int file_Len, int num_dm, int num_clust, int *cluster_ID, double **population_center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1090 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1091 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1092 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1093 int ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1094 int *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1095
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1096
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1097
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1098 size_c=(int *)malloc(sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1099 memset(size_c,0,sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1100
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1101 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1102 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1103 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1104
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1105 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1106 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1107 ID=cluster_ID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1108
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1109 if (ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1110 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1111 //commented on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1112 //printf("ID==-1! in ID2Center_all\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1113 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1114 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
1115 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1116 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1117
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1118 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1119 population_center[ID][j]=population_center[ID][j]+data_in[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1120
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1121 size_c[ID]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1122 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1123
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1124
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1125 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1126 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1127 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1128 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1129 population_center[i][j]=(population_center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1130 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1131 population_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1132 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1133
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1134 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1135
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1136 }
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 // Merge neighboring grids to clusters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1140 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1141
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1142 int merge_grids(double **normalized_data, double *interval, int file_Len, int num_dm, int num_bin, int **position, int num_dense_grids,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1143 int *dense_grid_reverse, int **dense_grid_seq, int *eventID_To_denseventID, int *densegridID_To_gridevent, int *all_grid_ID,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1144 int *cluster_ID, int *grid_ID, int *grid_clusterID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1145 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1146
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1147
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1148 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1149 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1150 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1151 int p=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1152 int num_clust=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1153 int ReventID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1154 int denseID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1155 int neighbor_ID=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1156 //int temp_grid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1157
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1158 int *grid_value;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1159 int *search_value;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1160
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1161 int **graph_of_grid; //the graph constructed for dense grids: each dense grid is a graph node
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1162
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1163 double real_dist=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1164 double **norm_grid_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1165
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1166 norm_grid_center=(double **)malloc(sizeof(double*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1167 memset(norm_grid_center,0,sizeof(double*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1168
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1169 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1170 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1171 norm_grid_center[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1172 memset(norm_grid_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1173 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1174
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1175 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1176 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1177 denseID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1178 if (denseID!=-1) //only dense events can enter
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1179 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1180 grid_ID[denseID]=dense_grid_reverse[all_grid_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1181
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1182 if (grid_ID[denseID]==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1183 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1184 fprintf(stderr,"Incorrect input file format or input parameters (no dense region found)!\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1185 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1186 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1187 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1188 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1189
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1190
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1191 /* Find centroid (in the normalized data) for each dense grid */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1192 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
1193
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1194 //printf("pass the grid ID2 center\n"); //commmented on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1195
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1196
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1197 graph_of_grid=(int **)malloc(sizeof(int*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1198 memset(graph_of_grid,0,sizeof(int*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1199 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1200 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1201 graph_of_grid[i]=(int *)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1202 memset(graph_of_grid[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1203
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1204
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1205 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1206 graph_of_grid[i][j]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1207 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1208
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1209 grid_value=(int *)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1210 memset(grid_value,0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1211
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1212 search_value=(int *)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1213 memset(search_value,0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1214
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1215
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1216 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1217 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1218 ReventID=densegridID_To_gridevent[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1219
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1220 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1221 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1222 grid_value[j]=position[ReventID][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1223
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1224 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1225
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1226
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1227 /* 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
1228 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
1229 list. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1230 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1231 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1232 for (p=0;p<num_dm;p++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1233 search_value[p]=grid_value[p];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1234
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1235 if (grid_value[t]==num_bin-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1236 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1237
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1238 search_value[t]=grid_value[t]+1; //we only consider the neighbor at the bigger side
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1239
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1240 neighbor_ID=binary_search(num_dense_grids, num_dm, search_value, dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1241
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1242 if (neighbor_ID!=-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1243 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1244 real_dist=norm_grid_center[i][t]-norm_grid_center[neighbor_ID][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1245
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1246 if (real_dist<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1247 real_dist=-real_dist;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1248
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1249 if (real_dist<2*interval[t])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1250 graph_of_grid[i][t]=neighbor_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1251 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1252 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1253 grid_clusterID[i]=i; //initialize grid_clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1254 }
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 //graph constructed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1258 //DFS-based search begins
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1259
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1260 /* Use a depth-first search to construct a list of connected subgraphs (= "clusters").
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1261 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
1262 in our search. */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1263 // num_clust=dfs(graph_of_grid,num_dense_grids,num_dm,grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1264 num_clust=find_connected(graph_of_grid, num_dense_grids, num_dm, grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1265
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1266
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1267 //computes grid_ID and cluster_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1268 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1269 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1270 denseID=eventID_To_denseventID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1271 if (denseID!=-1) //only dense events can enter
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1272 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1273 cluster_ID[denseID]=grid_clusterID[grid_ID[denseID]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1274 //if (cluster_ID[denseID]==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1275 // printf("catch you!\n");
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1279 free(search_value);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1280 free(grid_value);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1281
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1282 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1283 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1284 free(graph_of_grid[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1285 free(norm_grid_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1286 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1287 free(graph_of_grid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1288 free(norm_grid_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1289
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1290 return num_clust;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1291 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1292
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1293 /********************************************* Merge Clusters to Populations *******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1294 // This is the function future work can be on because it is about how to cluster the about 500 points accurately
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1295
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1296 int merge_clusters(int num_clust, int num_dm, double *interval, double **cluster_center, int *cluster_populationID, int max_num_pop)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1297 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1298 int num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1299 int temp_num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1300
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1301 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1302 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1303 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1304 int merge=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1305 int smid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1306 int bgid=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1307 double merge_dist=1.1; //initial value of merge_dist*interval should be slightly larger than the bin width
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1308
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1309 int *map_ID;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1310
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1311 double diff=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1312
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1313 map_ID=(int*)malloc(sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1314 memset(map_ID,0,sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1315
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1316 while ((num_population>max_num_pop) || (num_population<=1))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1317 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1318
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1319 if (num_population<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1320 merge_dist=merge_dist-0.1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1321
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1322 if (num_population>max_num_pop)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1323 merge_dist=merge_dist+0.1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1324
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1325
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1326 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1327 cluster_populationID[i]=i;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1328
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1329 for (i=0;i<num_clust-1;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1330 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1331 for (j=i+1;j<num_clust;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1332 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1333 merge=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1334
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1335 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1336 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1337 diff=cluster_center[i][t]-cluster_center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1338
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1339 if (diff<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1340 diff=-diff;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1341 if (diff>(merge_dist*interval[t]))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1342 merge=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1343 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1344
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1345 if ((merge) && (cluster_populationID[i]!=cluster_populationID[j]))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1346 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1347 if (cluster_populationID[i]<cluster_populationID[j]) //they could not be equal
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1348 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1349 smid = cluster_populationID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1350 bgid = cluster_populationID[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1351 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1352 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1353 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1354 smid = cluster_populationID[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1355 bgid = cluster_populationID[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1356 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1357 for (t=0;t<num_clust;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1358 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1359 if (cluster_populationID[t]==bgid)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1360 cluster_populationID[t]=smid;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1361 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1362 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1363 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1364 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1365
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1367
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1368 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1369 map_ID[i]=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1370
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1371 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1372 map_ID[cluster_populationID[i]]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1373
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1374 num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1375 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1376 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1377 if (map_ID[i]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1378 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1379 map_ID[i]=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1380 num_population++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1381 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1382 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1383
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1384 if ((temp_num_population>max_num_pop) && (num_population==1))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1385 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1386 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1387 temp_num_population=num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1388
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1389 if (num_clust<=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1390 break;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1391 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1392
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1393 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1394 cluster_populationID[i]=map_ID[cluster_populationID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1395
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1396 free(map_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1397
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1398 return num_population;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1399 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1400
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1401 ///////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1402 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1403 double kmeans(double **Matrix, int k, double kmean_term, int file_Len, int num_dm, int *shortest_id, double **center)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1404 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1405
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1406 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1407 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1408 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1409 int random=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1410 int random1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1411 int random2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1412 int times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1413 int dist_used=0; //0 is Euclidean and 1 is Pearson
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1414 int random_init=0; //0: not use random seeds;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1415 int real_Len=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1416 int skipped=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1417
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1418 int *num; //num[i]=t means the ith cluster has t points
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1419
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1420 double vvv=1.0; // the biggest variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1421 double distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1422 double xv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1423 double variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1424
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1425 double mean_dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1426 double mean_dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1427 double sum_var=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1428 double dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1429 double dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1430 double sd_x=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1431 double sd_y=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1432 double diff=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1433 double distortion=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1434
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1435 double shortest_distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1436
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1437 double *temp_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1438
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1439 double **sum;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1440
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1441 temp_center = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1442 memset(temp_center,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1443
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1444 if (random_init)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1445 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1446 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1447 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1448 random1=rand()*rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1449 random2=abs((random1%5)+1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1450 for (t=0;t<random2;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1451 random2=random2*rand()+rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1452
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1453 random=abs(random2%file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1454 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1455 center[i][j]=Matrix[random][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1456 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1457 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1458
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1459
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1460 num = (int *)malloc(sizeof(int)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1461 memset(num,0,sizeof(int)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1462
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1463 sum = (double **)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1464 memset(sum,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1465 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1466 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1467 sum[i] = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1468 memset(sum[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1469 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1470
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1471
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1472 times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1473 real_Len=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1474
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1475 while (((vvv>kmean_term) && (kmean_term<1)) || ((times<kmean_term) && (kmean_term>=1)))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1476 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1477 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1478 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1479 num[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1480 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1481 sum[i][j]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1482 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1483
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1484 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
1485 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1486 skipped = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1487 shortest_distance=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1488 for (j=0;j<k;j++) //for each center j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1489 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1490 distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1491
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1492 if (dist_used==0) //Euclidean distance
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1493 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1494 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
1495 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1496
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1497 diff=center[j][t]-Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1498
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1499 diff=diff*diff;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1500
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1501 distance = distance+diff; //here we have a weight for each dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1502 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1503 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1504 else //pearson correlation
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1505 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1506 mean_dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1507 mean_dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1508 sum_var=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1509 dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1510 dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1511 sd_x=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1512 sd_y=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1513 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1514 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1515 mean_dx+=center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1516 mean_dy+=Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1517 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1518 mean_dx=mean_dx/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1519 mean_dy=mean_dy/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1520
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1521 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1522 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1523 dx=center[j][t]-mean_dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1524 dy=Matrix[i][t]-mean_dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1525 sum_var+=dx*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1526 sd_x+=dx*dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1527 sd_y+=dy*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1528 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1529 if (sqrt(sd_x*sd_y)==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1530 distance = 1.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1531 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1532 distance = 1.0 - (sum_var/(sqrt(sd_x*sd_y))); // distance ranges from 0 to 2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1533 //printf("distance=%f\n",distance);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1534 } //pearson correlation ends
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1535
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1536 if ((distance<shortest_distance) && (skipped == 0))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1537 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1538 shortest_distance=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1539 shortest_id[i]=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1540 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1541 }//end for j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1542 real_Len++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1543 num[shortest_id[i]]=num[shortest_id[i]]+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1544 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1545 sum[shortest_id[i]][t]=sum[shortest_id[i]][t]+Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1546 }//end for i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1547 /* recompute the centers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1548 //compute_mean(group);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1549 vvv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1550 for (j=0;j<k;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1551 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1552 memcpy(temp_center,center[j],sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1553 variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1554 if (num[j]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1555 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1556 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1557 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1558 center[j][t]=sum[j][t]/(double)num[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1559 xv=(temp_center[t]-center[j][t]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1560 variation=variation+xv*xv;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1561 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1562 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1563
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1564 if (variation>vvv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1565 vvv=variation; //vvv is the biggest variation among the k clusters;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1566 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1567 //compute_variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1568 times++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1569 } //end for while
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1574 free(num);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1575
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1576 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1577 free(sum[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1578 free(sum);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1579 free(temp_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1580
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1581
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1582 return distortion;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1583
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1584 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1585
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1586 //////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1587 /*************************** Show *****************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1588 void show(double **Matrix, int *cluster_id, int file_Len, int k, int num_dm, char *name_string)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1589 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1590 int situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1591 int situ2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1592
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1593 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1594 int id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1595 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1596 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1597
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1598 int *size_c;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1599
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1600
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1601
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1602 int **size_mybound_1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1603 int **size_mybound_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1604 int **size_mybound_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1605 int **size_mybound_0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1606
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1607 double interval=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1608
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1609 double *big;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1610 double *small;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1611
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1612
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1613 double **center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1614 double **mybound;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1615
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1616 int **prof; //prof[i][j]=1 means population i is + at parameter j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1617
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1618 FILE *fpcnt_id; //proportion id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1619 //FILE *fcent_id; //center_id, i.e., centers of clusters within the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1620 FILE *fprof_id; //profile_id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1621
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1622 big=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1623 memset(big,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1624
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1625 small=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1626 memset(small,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1627
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1628 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1629 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1630 big[i]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1631 small[i]=(double)MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1632 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1633
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1634
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1635 size_c=(int *)malloc(sizeof(int)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1636 memset(size_c,0,sizeof(int)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1637
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1638 center=(double**)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1639 memset(center,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1640 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1641 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1642 center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1643 memset(center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1644 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1645
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1646 mybound=(double**)malloc(sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1647 memset(mybound,0,sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1648 for (i=0;i<num_dm;i++) //there are 3 mybounds for 4 categories
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1649 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1650 mybound[i]=(double*)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1651 memset(mybound[i],0,sizeof(double)*3);
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 prof=(int **)malloc(sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1655 memset(prof,0,sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1656 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1657 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1658 prof[i]=(int *)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1659 memset(prof[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1660 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1661
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1662
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1663 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1664 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1665 id=cluster_id[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1666 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1667 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1668 center[id][j]=center[id][j]+Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1669 if (big[j]<Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1670 big[j]=Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1671 if (small[j]>Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1672 small[j]=Matrix[i][j];
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 size_c[id]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1676 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1677
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1678 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1679 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1680 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1681 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1682 center[i][j]=(center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1683 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1684 center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1685 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1686
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1687 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1688 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1689 interval=((big[j]-small[j])/4.0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1690 //printf("interval[%d] is %f\n",j,interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1691 for (i=0;i<3;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1692 mybound[j][i]=small[j]+((double)(i+1)*interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1693 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1694
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1695
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1696 size_mybound_0=(int **)malloc(sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1697 memset(size_mybound_0,0,sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1698
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1699 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1700 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1701 size_mybound_0[i]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1702 memset(size_mybound_0[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1703 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1704
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1705 size_mybound_1=(int **)malloc(sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1706 memset(size_mybound_1,0,sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1707
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1708 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1709 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1710 size_mybound_1[i]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1711 memset(size_mybound_1[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1712 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1713
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1714 size_mybound_2=(int **)malloc(sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1715 memset(size_mybound_2,0,sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1716
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1717 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1718 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1719 size_mybound_2[i]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1720 memset(size_mybound_2[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1721 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1722
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1723 size_mybound_3=(int **)malloc(sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1724 memset(size_mybound_3,0,sizeof(int*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1725
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1726 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1727 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1728 size_mybound_3[i]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1729 memset(size_mybound_3[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1730 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1731
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1732 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1733 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1734 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1735 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
1736 size_mybound_0[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1737 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1738 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1739 if (Matrix[i][j]<mybound[j][1])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1740 size_mybound_1[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1741 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1742 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1743 if (Matrix[i][j]<mybound[j][2])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1744 size_mybound_2[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1745 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1746 //if (Matrix[i][j]!=big[j]) //the biggest values excluded
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1747 size_mybound_3[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1748 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1749
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1750 }
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 fprof_id=fopen("profile.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1754 fprintf(fprof_id,"Population_ID\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1755 fprintf(fprof_id,"%s\n",name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1756
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1757 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1758 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1759 fprintf(fprof_id,"%d\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
1760 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1761 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1762
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1763 if (size_mybound_0[i][j]>size_mybound_1[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1764 situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1765 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1766 situ1=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1767 if (size_mybound_2[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1768 situ2=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1769 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1770 situ2=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1771
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1772 if ((situ1==0) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1773 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1774 if (size_mybound_0[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1775 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1776 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1777 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1778 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1779 if ((situ1==0) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1780 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1781 if (size_mybound_0[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1782 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1783 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1784 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1785 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1786 if ((situ1==1) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1787 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1788 if (size_mybound_1[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1789 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1790 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1791 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1792 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1793 if ((situ1==1) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1794 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1795 if (size_mybound_1[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1796 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1797 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1798 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1799 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1800
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1801 //begin to output profile
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1802 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1803 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1804 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1805 fprintf(fprof_id,"1\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1806 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1807 fprintf(fprof_id,"2\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1808 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1809 fprintf(fprof_id,"3\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1810 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1811 fprintf(fprof_id,"4\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1812 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1813 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1814 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1815 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1816 fprintf(fprof_id,"1\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1817 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1818 fprintf(fprof_id,"2\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1819 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1820 fprintf(fprof_id,"3\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1821 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1822 fprintf(fprof_id,"4\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1823 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1824 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1825 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1826 fclose(fprof_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1827
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1828 ///////////////////////////////////////////////////////////
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 fpcnt_id=fopen("percentage.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1832 fprintf(fpcnt_id,"Population_ID\tPercentage\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1833
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1834 for (t=0;t<k;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1835 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1836 fprintf(fpcnt_id,"%d\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
1837 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1838 fclose(fpcnt_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1839
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1840 free(big);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1841 free(small);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1842 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1843
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1844 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1845 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1846 free(center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1847 free(prof[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1848 free(size_mybound_0[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1849 free(size_mybound_1[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1850 free(size_mybound_2[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1851 free(size_mybound_3[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1852 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1853 free(center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1854 free(prof);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1855 free(size_mybound_0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1856 free(size_mybound_1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1857 free(size_mybound_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1858 free(size_mybound_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1859
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1860 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1861 free(mybound[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1862 free(mybound);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1863
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1864 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1865
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1866
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1867
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1868 /******************************************************** Main Function **************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1869 //for normalized data, there are five variables:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1870 //cluster_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1871 //population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1872 //grid_clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1873 //grid_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1874 //grid_Center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1875
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1876 //the same five variables exist for the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1877 //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
1878 //also, data + cluster_ID -> population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1879 //data + grid_ID -> grid_Center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1880
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1881 /* what is the final output */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1882 //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
1883 //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
1884
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1885 /* what information should select_bin output */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1886 //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
1887 //is unknown, therefore I must use a prescreening to output
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1888 //how many bins I should use
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1889 //the number of dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1890 //total number of events in the dense grids
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1891
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1892 /* basic procedure of main function */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1893 //1. read raw file and normalize the raw file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1894 //2. select_bin
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1895 //3. allocate memory for eventID_To_denseventID, grid_clusterID, grid_ID, cluster_ID.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1896 //4. call select_dense and merge_grids with grid_clusterID, grid_ID, cluster_ID.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1897 //5. release normalized data; allocate memory for grid_Center and population_center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1898 //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
1899
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1900 int main (int argc, char **argv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1901 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1902 //inputs
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1903 FILE *f_src; //source file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1904
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1905 FILE *f_out; //coordinates
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1906 FILE *f_cid; //population-ID of events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1907 FILE *f_ctr; //centroids of populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1908 FILE *f_results; //coordinates file event and population column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1909 FILE *f_mfi; //added April 16, 2009 for mean fluorescence intensity
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1910 FILE *f_parameters; //number of bins and density calculated by
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1911 //the algorithm. Used to update the database
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1912 FILE *f_properties; //Properties file used by Image generation software
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1913
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1914 char para_name_string[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1915
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1916 int time_ID=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1917 int num_bin=0; //the bins I will use on each dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1918
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1919 int file_Len=0; //number of events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1920 int num_dm=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1921 int num_clust=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1922 int num_dense_events=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1923 int num_dense_grids=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1924 int num_nonempty=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1925 int num_population=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1926 //int temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1927
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1928 //below are read from configuration file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1929 int i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1930 int j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1931 int max_num_pop=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1932
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1933 int den_t_event=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1934
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1935 int *grid_clusterID; //(dense)gridID to clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1936 int *grid_ID; //(dense)eventID to gridID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1937 int *cluster_ID; //(dense)eventID to clusterID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1938 int *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
1939 int *all_grid_ID; //gridID for all events
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1940 int *densegridID_To_gridevent;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1941 int *sorted_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1942 int *dense_grid_reverse;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1943 int *population_ID; //denseeventID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1944 int *cluster_populationID; //clusterID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1945 int *grid_populationID; //gridID to populationID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1946 int *all_population_ID; //populationID of event
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1947
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1948 int **position;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1949 int **dense_grid_seq;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1950
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1951 double *interval;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1952
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1953 double **population_center; //population centroids in the raw/original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1954 double **cluster_center; //cluster centroids in the raw/original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1955
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1956 double **input_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1957 double **normalized_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1958
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1959 int min = 999999;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1960 int max = 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1961
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1962 printf( "Starting time:\t\t\t\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1963 fflush(stdout);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1964 system("/bin/date");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1965 /////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1966
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1967 if ((argc!=2) && (argc!=4) && (argc!=5))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1968 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1969 //modified on Jul 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1970 //printf("usage:\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1971 //printf("basic mode: flock data_file\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1972 //printf("advanced mode: flock data_file num_bin density_index\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1973 //exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1974
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1975 fprintf(stderr,"Incorrect number of input parameters!\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1976 //fprintf(stderr,"basic mode: flock data_file\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1977 //fprintf(stderr,"advanced mode1: flock data_file num_bin density_index\n"); //modified on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1978 fprintf(stderr,"advanced mode: flock data_file num_bin density_index max_num_pop\n"); //added on Dec 16, 2010 for GenePattern
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1979 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1980 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1981
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1982 f_src=fopen(argv[1],"r");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1983
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1984 if (argc==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1985 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1986 max_num_pop=30; //default value, maximum 30 clusters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1987 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1988
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1989 if (argc==4)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1990 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1991 num_bin=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1992 printf("num_bin is %d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1993
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1994 den_t_event=atoi(argv[3]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1995 printf("density_index is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1996
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1997 max_num_pop=30;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1998
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1999 if (((num_bin<6) && (num_bin!=0)) || (num_bin>29))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2000 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2001 fprintf(stderr,"Incorrect input range of number of bins, which should be larger than 5 and smaller than 30\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2002 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2003 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2004
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2005 if (((den_t_event<3) && (den_t_event!=0)) || (den_t_event>99))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2006 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2007 fprintf(stderr,"Incorrect input range of density threshold, which should be larger than 2 and smaller than 100\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2008 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2009 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2010 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2011
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2012 if (argc==5)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2013 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2014 num_bin=atoi(argv[2]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2015 printf("num_bin is %d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2016
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2017 den_t_event=atoi(argv[3]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2018 printf("density_index is %d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2019
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2020 max_num_pop=atoi(argv[4]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2021 printf("max number of clusters is %d\n",max_num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2022
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2023 if (((num_bin<6) && (num_bin!=0)) || (num_bin>29))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2024 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2025 fprintf(stderr,"Incorrect input range of number of bins, which should be larger than 5 and smaller than 30\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2026 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2027 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2028
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2029 if (((den_t_event<3) && (den_t_event!=0)) || (den_t_event>99))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2030 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2031 fprintf(stderr,"Incorrect input range of density threshold, which should be larger than 2 and smaller than 100\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2032 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2033 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2034
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2035 if ((max_num_pop<5) || (max_num_pop>999))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2036 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2037 fprintf(stderr,"Incorrect input range of maximum number of populations, which should be larger than 4 and smaller than 1000\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2038 abort();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2039 }
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 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
2044
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2045 /************************************************* Read the data *****************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2046
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2047 rewind(f_src); //reset the file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2048
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2049 input_data = (double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2050 memset(input_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2051 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2052 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2053 input_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2054 memset(input_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2055 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2056
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2057 readsource(f_src, file_Len, num_dm, input_data, time_ID); //read the data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2058
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2059 fclose(f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2060
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2061 normalized_data=(double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2062 memset(normalized_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2063 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2064 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2065 normalized_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2066 memset(normalized_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2067 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2068
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2069 tran(input_data, file_Len, num_dm, NORM_METHOD, normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2070
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2071
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2072 position=(int **)malloc(sizeof(int*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2073 memset(position,0,sizeof(int*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2074 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2075 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2076 position[i]=(int*)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2077 memset(position[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2078 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2079
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2080 all_grid_ID=(int *)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2081 memset(all_grid_ID,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2082
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2083 sorted_seq=(int*)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2084 memset(sorted_seq,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2085
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2086 interval=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2087 memset(interval,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2088
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2089 /************************************************* select_bin *************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2090
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2091 if (num_bin>=1) //num_bin has been selected by user
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2092 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
2093 else //num_bin has not been selected by user
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2094 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2095 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
2096 printf("selected bin number is %d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2097 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2098 printf("number of non-empty grids is %d\n",num_nonempty);
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 /* 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
2103 free(sorted_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2104
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2105
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2106 dense_grid_reverse=(int*)malloc(sizeof(int)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2107 memset(dense_grid_reverse,0,sizeof(int)*num_nonempty);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2108
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2109 /************************************************* select_dense **********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2110
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2111 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
2112 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
2113 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2114 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2115 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
2116 printf("automated selected density threshold is %d\n",den_t_event);
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 printf("Number of dense grids is %d\n",num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2120
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2121 densegridID_To_gridevent = (int *)malloc(sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2122 memset(densegridID_To_gridevent,0,sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2123
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2124 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2125 densegridID_To_gridevent[i]=-1; //initialize all densegridID_To_gridevent values to -1
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 eventID_To_denseventID=(int *)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2129 memset(eventID_To_denseventID,0,sizeof(int)*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
2130
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2131
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2132 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
2133
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2134
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2135 dense_grid_seq=(int **)malloc(sizeof(int*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2136 memset(dense_grid_seq,0,sizeof(int*)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2137 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2138 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2139 dense_grid_seq[i]=(int *)malloc(sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2140 memset(dense_grid_seq[i],0,sizeof(int)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2141 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2142
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2143
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2144 /* Look up the binned data values for each dense grid */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2145 generate_grid_seq(num_dm, num_dense_grids, densegridID_To_gridevent, position, dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2146
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2147
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2148 /************************************************* allocate memory *********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2149
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2150 grid_clusterID=(int *)malloc(sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2151 memset(grid_clusterID,0,sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2152
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2153 grid_ID=(int *)malloc(sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2154 memset(grid_ID,0,sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2155
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2156 cluster_ID=(int *)malloc(sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2157 memset(cluster_ID,0,sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2158
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2159
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2160 /*********************************************** merge_grids ***********************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2161 //int merge_grids(int file_Len, int num_dm, int num_bin, int **position, int num_dense_grids, int *dense_grid_rank, int *dense_grid_reverse,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2162 // int **dense_grid_seq, int *eventID_To_denseventID, int *densegridID_To_gridevent, int *all_grid_ID,
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2163 // int *cluster_ID, int *grid_ID, int *grid_clusterID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2164
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2165 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
2166
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2167 printf("computed number of groups is %d\n",num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2168
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2169
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2170 /************************************** release unnecessary memory and allocate memory and compute centers **********************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2171
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2172
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2173 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2174 free(position[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2175 free(position);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2176
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2177 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2178 free(dense_grid_seq[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2179 free(dense_grid_seq);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2180
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2181 free(dense_grid_reverse);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2182
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2183 free(densegridID_To_gridevent);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2184 free(all_grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2185
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2186 // cluster_center ////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2187
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2188 cluster_center=(double **)malloc(sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2189 memset(cluster_center,0,sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2190 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2191 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2192 cluster_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2193 memset(cluster_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2194 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2195
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2196 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
2197
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2198 //printf("pass the first ID2center\n"); //commented on July 23, 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2199
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2200 /*** population_ID and grid_populationID **/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2201
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2202 cluster_populationID=(int*)malloc(sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2203 memset(cluster_populationID,0,sizeof(int)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2204
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2205 grid_populationID=(int*)malloc(sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2206 memset(grid_populationID,0,sizeof(int)*num_dense_grids);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2207
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2208 population_ID=(int*)malloc(sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2209 memset(population_ID,0,sizeof(int)*num_dense_events);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2210
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2211 num_population = merge_clusters(num_clust, num_dm, interval, cluster_center, cluster_populationID,max_num_pop);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2212
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2213
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2214
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2215 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2216 free(cluster_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2217 free(cluster_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2218
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2219 free(interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2220
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2221 for (i=0;i<num_dense_grids;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2222 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2223 grid_populationID[i]=cluster_populationID[grid_clusterID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2224 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2225
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2226 for (i=0;i<num_dense_events;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2227 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2228 population_ID[i]=cluster_populationID[cluster_ID[i]];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2229 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2230
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2231 printf("computed number of populations is %d\n",num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2232
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2233
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2234
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2235 // population_center /////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2236
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2237
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2238 population_center=(double **)malloc(sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2239 memset(population_center,0,sizeof(double*)*num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2240 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2241 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2242 population_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2243 memset(population_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2244 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2245
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2246
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2247
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2248 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
2249
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2250
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2251 // show ////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2252 all_population_ID=(int*)malloc(sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2253 memset(all_population_ID,0,sizeof(int)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2254
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2255 kmeans(normalized_data, num_population, KMEANS_TERM, file_Len, num_dm, all_population_ID, population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2256 show(input_data, all_population_ID, file_Len, num_population, num_dm, para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2257
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2258 ID2Center_all(input_data,file_Len,num_dm,num_population,all_population_ID,population_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2259
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2260
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2261 f_cid=fopen("population_id.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2262 f_ctr=fopen("population_center.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2263 f_out=fopen("coordinates.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2264 f_results=fopen("flock_results.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2265
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2266 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2267 f_parameters=fopen("parameters.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2268 fprintf(f_parameters,"Number_of_Bins\t%d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2269 fprintf(f_parameters,"Density\t%f\n",aver_index);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2270 fclose(f_parameters);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2271 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2272
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2273 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2274 fprintf(f_cid,"%d\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
2275
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2276 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2277 * New to check for min/max to add to parameters.txt
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2278 *
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2279 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2280
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2281 fprintf(f_out,"%s\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2282 //fprintf(f_results,"%s\tEvent\tPopulation\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2283 fprintf(f_results,"%s\tPopulation\n",para_name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2284 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2285 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2286 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2287 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2288 if (input_data[i][j] < min) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2289 min = (int)input_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2290 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2291 if (input_data[i][j] > max) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2292 max = (int)input_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2293 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2294 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2295 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2296 fprintf(f_out,"%d\n",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2297 fprintf(f_results,"%d\t",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2298 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2299 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2300 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2301 fprintf(f_out,"%d\t",(int)input_data[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2302 fprintf(f_results,"%d\t",(int)input_data[i][j]);
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 //fprintf(f_results,"%d\t",i + 1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2306 fprintf(f_results,"%d\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
2307 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2308
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2309 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2310 f_parameters=fopen("parameters.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2311 fprintf(f_parameters,"Number_of_Bins\t%d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2312 fprintf(f_parameters,"Density\t%d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2313 fprintf(f_parameters,"Min\t%d\n",min);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2314 fprintf(f_parameters,"Max\t%d\n",max);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2315 fclose(f_parameters);
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_properties=fopen("fcs.properties","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2319 fprintf(f_properties,"Bins=%d\n",num_bin);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2320 fprintf(f_properties,"Density=%d\n",den_t_event);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2321 fprintf(f_properties,"Min=%d\n",min);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2322 fprintf(f_properties,"Max=%d\n",max);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2323 fprintf(f_properties,"Populations=%d\n",num_population);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2324 fprintf(f_properties,"Events=%d\n",file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2325 fprintf(f_properties,"Markers=%d\n",num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2326 fclose(f_properties);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2327
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2328
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2329 for (i=0;i<num_population;i++) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2330 /* Add if we want to include population id in the output
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2331 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2332 fprintf(f_ctr,"%d\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
2333
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2334 for (j=0;j<num_dm;j++) {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2335 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2336 fprintf(f_ctr,"%.0f\n",population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2337 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2338 fprintf(f_ctr,"%.0f\t",population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2339 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2340 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2341
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2342 //added April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2343 f_mfi=fopen("MFI.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2344
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2345 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2346 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2347 fprintf(f_mfi,"%d\t",i+1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2348
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2349 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2350 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2351 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2352 fprintf(f_mfi,"%.0f\n",population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2353 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2354 fprintf(f_mfi,"%.0f\t",population_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2355 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2356 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2357 fclose(f_mfi);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2358
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2359 //ended April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2360
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2361 fclose(f_cid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2362 fclose(f_ctr);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2363 fclose(f_out);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2364 fclose(f_results);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2365
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2367 for (i=0;i<num_population;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2368 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2369 free(population_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2370 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2371 free(population_center);
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 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2375 free(normalized_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2376 free(normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2377
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2378 free(grid_populationID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2379
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2380 free(cluster_populationID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2381 free(grid_clusterID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2382 free(cluster_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2383
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2384 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2385 free(input_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2386 free(input_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2387
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2388 free(grid_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2389 free(population_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2390 free(all_population_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2391 free(eventID_To_denseventID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2392
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2393 ///////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2394 printf("Ending time:\t\t\t\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2395 fflush(stdout);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2396 system("/bin/date");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2397
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2398 return 0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2399
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
2400 }