annotate src/cent_adjust.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 // Cent_adjust version number and modification history
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
3 // ImmPort BISC project
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
4 // Author: Yu "Max" Qian
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
5 // v1.01: Oct 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
6 // Line 899 of the main function:
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
7 // Changed kmean_term=1 to kmean_term=2
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
8 //////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
9
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
10
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
11 #include <time.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
12 #include <stdio.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
13 #include <stdlib.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
14 #include <math.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
15 #include <string.h>
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
16
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
17 #define DEBUG 0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
18 #define LINE_LEN 1024
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
19 #define FILE_NAME_LEN 128
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
20 #define PARA_NAME_LEN 64
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
21 #define MAX_VALUE 1000000000
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
22 #define CUBE 0
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
23
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
24
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
25 void getctrfileinfo(FILE *f_src_ctr, long *num_clust)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
26 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
27 int ch='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
28 int prev='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
29 long num_rows=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
30
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
31 while ((ch = fgetc(f_src_ctr))!= EOF )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
32 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
33 if (ch == '\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
34 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
35 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
36 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
37 prev = ch;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
38 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
39 if (prev!='\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
40 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
41
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
42 *num_clust=num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
43 //printf("center file has %ld rows\n", *num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
44 }
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, long *file_Len, long *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 long num_rows=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
54 long 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 long time_pos=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
58 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
59 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
60
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
61
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
62
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
63 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
64 fgets(src, LINE_LEN, f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
65
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
66 name_string[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
67 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
68 prv='\n';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
69
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
70 while ((src[i]==' ') || (src[i]=='\t')) //skip space and tab characters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
71 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
72
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
73 while ((src[i]!='\r') && (src[i]!='\n')) //repeat until the end of the line
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
74 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
75 current_name[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
76
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
77 if ((src[i]=='\t') && (prv!='\t')) //a complete word
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
78 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
79 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
80
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
81 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
82 * Commented out John Campbell, June 10 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
83 * We no longer want to automatically remove Time column.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
84 * This column should have been removed by column selection
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
85 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
86 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
87 num_columns++; //num_columns does not inlcude the column of Time
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
88 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
89 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
90 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
91 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
92 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
93 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
94 *time_ID=time_pos;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
95 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
96 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
97
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
98 num_columns++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
99 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
100 strcat(name_string,"\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
101
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
102 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
103 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
104 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
105
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
106 if ((src[i]=='\t') && (prv=='\t')) //a duplicate tab or space
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
107 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
108 current_name[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
109 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
110 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
111
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
112 if (src[i]!='\t')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
113 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
114
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
115
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
116 prv=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
117 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
118 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
119
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
120 if (prv!='\t') //the last one hasn't been retrieved
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
121 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
122 current_name[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
123 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
124 * Commented out John Campbell, June 10 2010
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
125 * We no longer want to automatically remove Time column.
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
126 * This column should have been removed by column selection
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
127 if (0!=strcmp(current_name,"Time"))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
128 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
129 num_columns++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
130 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
131 time_pos++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
132 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
133 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
134 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
135 *time_ID=time_pos;
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 num_columns++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
140 strcat(name_string,current_name);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
141 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
142
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
143 if (DEBUG==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
144 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
145 printf("time_ID is %d\n",*time_ID);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
146 printf("name_string is %s\n",name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
147 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
148
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
149 // # of rows
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
150
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
151 while ((ch = fgetc(f_src))!= EOF )
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
152 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
153 if (ch == '\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
154 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
155 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
156 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
157 prev = ch;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
158 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
159 if (prev!='\n')
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
160 ++num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
161
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
162 *file_Len=num_rows;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
163 *num_dm=num_columns;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
164
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
165 //printf("original file size is %ld; number of dimensions is %ld\n", *file_Len, *num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
166 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
167
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
168
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 /************************************* Read the source file into uncomp_data **************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
172 void readsource(FILE *f_src, long file_Len, long num_dm, double **uncomp_data, int time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
173 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
174 long time_pass=0; //to mark whether the time_ID has been passed
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
175 long index=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
176
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
177 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
178 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
179 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
180
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
181 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
182 char xc[LINE_LEN/10];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
183
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
184 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
185 fgets(src,LINE_LEN, f_src); //skip the first line about parameter names
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
186
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
187 while (!feof(f_src) && (index<file_Len)) //index = 0, 1, ..., file_Len-1
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
188 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
189 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
190 fgets(src,LINE_LEN,f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
191 i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
192 time_pass=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
193
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
194 if (time_ID==-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
195 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
196 for (t=0;t<num_dm;t++) //there is no time_ID
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
197 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
198 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
199 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
200 while ((src[i]!='\r') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
201 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
202 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
203 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
204 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
205 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
206
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
207 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
208 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
209
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
210 uncomp_data[index][t]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
211 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
212 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
213 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
214 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
215 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
216 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
217 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
218 j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
219 while ((src[i]!='\r') && (src[i]!='\n') && (src[i]!=' ') && (src[i]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
220 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
221 xc[j]=src[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
222 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
223 j++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
224 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
225
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
226 xc[j]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
227 i++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
228
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
229 if (t==time_ID)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
230 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
231 time_pass=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
232 continue;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
233 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
234
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
235 if (time_pass)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
236 uncomp_data[index][t-1]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
237 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
238 uncomp_data[index][t]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
239 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
240 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
241 index++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
242 //fprintf(fout_ID,"%s",src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
243 } //end of while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
244
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
245 if (DEBUG == 1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
246 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
247 printf("the last line of the source data is:\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
248 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
249 printf("%f ",uncomp_data[index-1][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
250 printf("\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
251 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
252 }
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 void readcenter(FILE *f_src_ctr, long num_clust, long num_dm, double **cluster_center, long *IDmapping)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
256 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
257 char src[LINE_LEN];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
258 char xc[LINE_LEN/10];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
259
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
260 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
261 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
262 int m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
263 int t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
264
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
265 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
266 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
267 src[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
268 fgets(src,LINE_LEN, f_src_ctr);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
269 m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
270 for (j=0;j<num_dm+1;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
271 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
272 xc[0]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
273 t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
274 while ((src[m]!='\r') && (src[m]!='\n') && (src[m]!=' ') && (src[m]!='\t'))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
275 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
276 xc[t]=src[m];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
277 m++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
278 t++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
279 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
280 xc[t]='\0';
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
281 m++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
282 if (j==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
283 IDmapping[i]=atoi(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
284 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
285 cluster_center[i][j-1]=atof(xc);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
286 //printf("cluster_center[%d][%d]=%f\n",i,j,cluster_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
287 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
288 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
289 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
290
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
291
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
292 /**************************************** Normalization ******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
293 void tran(double **orig_data, long clean_Len, long num_dm, long norm_used, double **matrix_to_cluster)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
294 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
295 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
296 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
297
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
298 double biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
299 double smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
300
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
301 double *aver; //average of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
302 double *std; //standard deviation of each column
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
303
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
304 aver=(double*)malloc(sizeof(double)*clean_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
305 memset(aver,0,sizeof(double)*clean_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
306
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
307 std=(double*)malloc(sizeof(double)*clean_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
308 memset(std,0,sizeof(double)*clean_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
309
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
310 if (norm_used==2) //z-score normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
311 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
312 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
313 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
314 aver[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
315 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
316 aver[j]=aver[j]+orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
317 aver[j]=aver[j]/(double)clean_Len;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
318
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
319 std[j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
320 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
321 std[j]=std[j]+(orig_data[i][j]-aver[j])*(orig_data[i][j]-aver[j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
322 std[j]=sqrt(std[j]/(double)clean_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
323
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
324 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
325 matrix_to_cluster[i][j]=(orig_data[i][j]-aver[j])/std[j]; //z-score normalization
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 if (norm_used==1) //0-1 min-max normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
330 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
331 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
332 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
333 biggest=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
334 smallest=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
335 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
336 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
337 if (orig_data[i][j]>biggest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
338 biggest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
339 if (orig_data[i][j]<smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
340 smallest=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
341 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
342
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
343 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
344 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
345 if (biggest==smallest)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
346 matrix_to_cluster[i][j]=biggest;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
347 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
348 matrix_to_cluster[i][j]=(orig_data[i][j]-smallest)/(biggest-smallest);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
349 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
350 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
351 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
352
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
353 if (norm_used==0) //no normalization
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
354 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
355 for (i=0;i<clean_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
356 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
357 matrix_to_cluster[i][j]=orig_data[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
358 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
359
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
360
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
361 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
362
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
363 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
364 void assign_event(double **Matrix, long k, long dist_used, double kmean_term, long file_Len, long num_dm, long *shortest_id, double **center, int random_init)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
365 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
366
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
367 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
368
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
369 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
370 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
371 long random=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
372 long random1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
373 long random2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
374 long times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
375 long times_allowed=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
376
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
377 long *num; //num[i]=t means the ith cluster has t points
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
378
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
379 double vvv=1.0; // the biggest variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
380 double distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
381 double xv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
382 double variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
383 double EPS=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
384 double diff=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
385 double mean_dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
386 double mean_dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
387 double sum_var=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
388 double dx=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
389 double dy=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
390 double sd_x=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
391 double sd_y=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
392
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
393 double *temp_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
394 double *shortest_distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
395
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
396 double **sum;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
397
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
398 temp_center = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
399 memset(temp_center,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
400
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
401 /* Choosing Centers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
402 if (random_init)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
403 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
404 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
405 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
406 random1=rand()*rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
407 //srand( (unsigned)time( NULL ) );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
408 random2=abs((random1%5)+1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
409 for (t=0;t<random2;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
410 random2=random2*rand()+rand();
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
411
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
412 random=abs(random2%file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
413 //printf("random=%d\n",random);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
414 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
415 center[i][j]=Matrix[random][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
416
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
420 //printf("finish random selection\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
421 /* To compute the nearest center for every point */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
422
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
423 shortest_distance = (double *)malloc(sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
424 memset(shortest_distance,0,sizeof(double)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
425
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
426 num = (long *)malloc(sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
427 memset(num,0,sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
428
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
429 sum = (double **)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
430 memset(sum,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
431 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
432 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
433 sum[i] = (double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
434 memset(sum[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
435 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
436
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
437 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
438 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
439 sum[i][j]=0.0; //sum[i][j] = k means the sum of the jth dimension of all points in the ith group is k
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
440
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
441 //printf("before recursion\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
442 if (kmean_term>=1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
443 times_allowed = (long)kmean_term;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
444 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
445 EPS = kmean_term;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
446
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
447 times=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
448
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
449 while (((vvv>EPS) && (kmean_term<1)) || ((times<times_allowed) && (kmean_term>=1)))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
450 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
451 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
452 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
453 num[i]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
454 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
455 sum[i][j]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
456 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
457
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
458 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
459 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
460 shortest_distance[i]=MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
461 for (j=0;j<k;j++) //for each center j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
462 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
463
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
464 distance=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
465
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
466 if (dist_used==0) //Euclidean distance
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
467 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
468 for (t=0;t<num_dm;t++) //for each dimension
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
469 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
470 diff=center[j][t]-Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
471 if (diff<0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
472 diff=-diff;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
473
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
474 if (CUBE)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
475 distance = distance+(diff*diff*diff);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
476 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
477 distance = distance+(diff*diff);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
478 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
479 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
480 else //pearson correlation
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
481 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
482 mean_dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
483 mean_dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
484 sum_var=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
485 dx=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
486 dy=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
487 sd_x=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
488 sd_y=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
489 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
490 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
491 mean_dx+=center[j][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
492 mean_dy+=Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
493 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
494 mean_dx=mean_dx/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
495 mean_dy=mean_dy/(double)num_dm;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
496 //printf("mean_dx=%f\n",mean_dx);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
497
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
498 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
499 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
500 dx=center[j][t]-mean_dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
501 dy=Matrix[i][t]-mean_dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
502 sum_var+=dx*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
503 sd_x+=dx*dx;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
504 sd_y+=dy*dy;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
505 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
506 if (sqrt(sd_x*sd_y)==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
507 distance = 1.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
508 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
509 distance = 1.0 - (sum_var/(sqrt(sd_x*sd_y))); // distance ranges from 0 to 2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
510 //printf("distance=%f\n",distance);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
511 } //pearson correlation ends
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
512
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
513
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
514 if (distance<shortest_distance[i])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
515 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
516 shortest_distance[i]=distance;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
517 shortest_id[i]=j;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
518 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
519 }//end for j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
520 num[shortest_id[i]]=num[shortest_id[i]]+1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
521 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
522 sum[shortest_id[i]][t]=sum[shortest_id[i]][t]+Matrix[i][t];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
523 }//end for i
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
524 /* recompute the centers */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
525 //compute_mean(group);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
526 vvv=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
527 for (j=0;j<k;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
528 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
529 memcpy(temp_center,center[j],sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
530 variation=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
531 if (num[j]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
532 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
533 for (t=0;t<num_dm;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
534 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
535 center[j][t]=sum[j][t]/(double)num[j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
536 xv=(temp_center[t]-center[j][t]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
537 variation=variation+xv*xv;
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 (variation>vvv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
542 vvv=variation; //vvv is the biggest variation among the k clusters;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
543 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
544 //compute_variation;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
545 times++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
546 } //end for while
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
547
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
548
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
549
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
550 free(num);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
551 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
552 free(sum[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
553 free(sum);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
554 free(temp_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
555 free(shortest_distance);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
556
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
557 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
558
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
559 //////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
560 /*************************** Show *****************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
561 void show(double **Matrix, long *cluster_id, long file_Len, long k, long num_dm, char *name_string, long *IDmapping)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
562 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
563 int situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
564 int situ2=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
565
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
566 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
567 long id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
568 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
569 long info_id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
570 long nearest_id=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
571 long insert=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
572 long temp=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
573 long m=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
574 long n=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
575 long t=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
576
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
577 long *size_c;
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
581 long **size_mybound_1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
582 long **size_mybound_2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
583 long **size_mybound_3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
584 long **size_mybound_0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
585
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
586 double interval=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
587
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
588 double *big;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
589 double *small;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
590
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
591
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
592 double **center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
593 double **mybound;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
594
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
595 long **prof; //prof[i][j]=1 means population i is + at parameter j
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
596
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
597 FILE *fpcnt_id; //proportion id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
598 //FILE *fcent_id; //center_id, i.e., centers of clusters within the original data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
599 FILE *fprof_id; //profile_id
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
600
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
601 big=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
602 memset(big,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
603
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
604 small=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
605 memset(small,0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
606
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
607 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
608 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
609 big[i]=0.0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
610 small[i]=(double)MAX_VALUE;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
611 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
612
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
613
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
614 size_c=(long *)malloc(sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
615 memset(size_c,0,sizeof(long)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
616
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
617 center=(double**)malloc(sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
618 memset(center,0,sizeof(double*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
619 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
620 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
621 center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
622 memset(center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
623 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
624
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
625 mybound=(double**)malloc(sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
626 memset(mybound,0,sizeof(double*)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
627 for (i=0;i<num_dm;i++) //there are 3 mybounds for 4 categories
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
628 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
629 mybound[i]=(double*)malloc(sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
630 memset(mybound[i],0,sizeof(double)*3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
631 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
632
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
633 prof=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
634 memset(prof,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
635 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
636 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
637 prof[i]=(long *)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
638 memset(prof[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
639 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
640
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
641
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
642 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
643 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
644 id=cluster_id[i];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
645 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
646 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
647 center[id][j]=center[id][j]+Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
648 if (big[j]<Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
649 big[j]=Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
650 if (small[j]>Matrix[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
651 small[j]=Matrix[i][j];
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
652 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
653
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
654 size_c[id]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
655 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
656
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
657 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
658 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
659 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
660 if (size_c[i]!=0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
661 center[i][j]=(center[i][j]/(double)(size_c[i]));
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
662 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
663 center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
664 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
665
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
666 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
667 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
668 interval=((big[j]-small[j])/4.0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
669 //printf("interval[%d] is %f\n",j,interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
670 for (i=0;i<3;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
671 mybound[j][i]=small[j]+((double)(i+1)*interval);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
672 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
673
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
674
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
675 size_mybound_0=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
676 memset(size_mybound_0,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
677
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
678 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
679 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
680 size_mybound_0[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
681 memset(size_mybound_0[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
682 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
683
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
684 size_mybound_1=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
685 memset(size_mybound_1,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
686
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
687 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
688 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
689 size_mybound_1[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
690 memset(size_mybound_1[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
691 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
692
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
693 size_mybound_2=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
694 memset(size_mybound_2,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
695
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
696 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
697 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
698 size_mybound_2[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
699 memset(size_mybound_2[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
700 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
701
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
702 size_mybound_3=(long **)malloc(sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
703 memset(size_mybound_3,0,sizeof(long*)*k);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
704
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
705 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
706 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
707 size_mybound_3[i]=(long*)malloc(sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
708 memset(size_mybound_3[i],0,sizeof(long)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
709 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
710
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
711 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
712 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
713 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
714 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
715 size_mybound_0[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
716 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
717 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
718 if (Matrix[i][j]<mybound[j][1])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
719 size_mybound_1[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
720 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
721 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
722 if (Matrix[i][j]<mybound[j][2])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
723 size_mybound_2[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
724 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
725 //if (Matrix[i][j]!=big[j]) //the biggest values excluded
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
726 size_mybound_3[cluster_id[i]][j]++;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
727 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
728
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
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
732 fprof_id=fopen("profile.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
733 fprintf(fprof_id,"Population_ID\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
734 fprintf(fprof_id,"%s\n",name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
735
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
736 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
737 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
738 fprintf(fprof_id,"%ld\t",IDmapping[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
739 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
740 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
741
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
742 if (size_mybound_0[i][j]>size_mybound_1[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
743 situ1=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
744 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
745 situ1=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
746 if (size_mybound_2[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
747 situ2=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
748 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
749 situ2=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
750
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
751 if ((situ1==0) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
752 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
753 if (size_mybound_0[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
754 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
755 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
756 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
757 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
758 if ((situ1==0) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
759 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
760 if (size_mybound_0[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
761 prof[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
762 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
763 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
764 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
765 if ((situ1==1) && (situ2==2))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
766 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
767 if (size_mybound_1[i][j]>size_mybound_2[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
768 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
769 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
770 prof[i][j]=2;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
771 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
772 if ((situ1==1) && (situ2==3))
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
773 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
774 if (size_mybound_1[i][j]>size_mybound_3[i][j])
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
775 prof[i][j]=1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
776 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
777 prof[i][j]=3;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
778 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
779
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
780 //begin to output profile
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
781 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
782 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
783 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
784 fprintf(fprof_id,"1\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
785 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
786 fprintf(fprof_id,"2\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
787 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
788 fprintf(fprof_id,"3\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
789 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
790 fprintf(fprof_id,"4\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
791 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
792 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
793 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
794 if (prof[i][j]==0)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
795 fprintf(fprof_id,"1\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
796 if (prof[i][j]==1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
797 fprintf(fprof_id,"2\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
798 if (prof[i][j]==2)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
799 fprintf(fprof_id,"3\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
800 if (prof[i][j]==3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
801 fprintf(fprof_id,"4\t");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
802 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
803 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
804 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
805 fclose(fprof_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
806
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
807 ///////////////////////////////////////////////////////////
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 fpcnt_id=fopen("percentage.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
811 fprintf(fpcnt_id,"Population_ID\tPercentage\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
812
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
813 for (t=0;t<k;t++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
814 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
815 fprintf(fpcnt_id,"%ld\t%.2f\n",IDmapping[t],(double)size_c[t]*100.0/(double)file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
816 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
817 fclose(fpcnt_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
818
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
819 free(big);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
820 free(small);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
821 free(size_c);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
822
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
823 for (i=0;i<k;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
824 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
825 free(center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
826 free(prof[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
827 free(size_mybound_0[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
828 free(size_mybound_1[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
829 free(size_mybound_2[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
830 free(size_mybound_3[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
831 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
832 free(center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
833 free(prof);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
834 free(size_mybound_0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
835 free(size_mybound_1);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
836 free(size_mybound_2);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
837 free(size_mybound_3);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
838
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
839 for (i=0;i<num_dm;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
840 free(mybound[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
841 free(mybound);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
842
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
843 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
844 /******************************************************** Main Function **************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
845 int main (int argc, char **argv)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
846 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
847 //inputs
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
848 FILE *f_src; //source file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
849 FILE *f_src_ctr; //source center file
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
850 //outputs
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
851 FILE *f_cid; //cluster-id file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
852 FILE *f_mfi; //added April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
853
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
854
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
855 char name_string[LINE_LEN]; //for name use
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
856
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
857 int time_id=-1;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
858
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
859 long file_Len=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
860 long num_clust=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
861 long num_dm=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
862 long norm_used=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
863 long dist_used=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
864 long i=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
865 long j=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
866
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
867 long *cluster_id;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
868 long *IDmapping; //this is to keep the original populationID of the center.txt
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
869
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
870 double kmean_term=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
871
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
872 double **cluster_center;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
873 double **orig_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
874 double **normalized_data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
875
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
876 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
877 _strtime( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
878 printf( "Starting time:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
879 _strdate( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
880 printf( "Starting date:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
881 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
882
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
883 if (argc!=3)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
884 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
885 printf("usage: cent_adjust input_center input_data_file\n");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
886 exit(0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
887 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
888
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
889
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
890 f_src_ctr=fopen(argv[1],"r");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
891
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
892 //read source data
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
893 f_src=fopen(argv[2],"r");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
894
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
895 getfileinfo(f_src, &file_Len, &num_dm, name_string, &time_id); //get the filelength, number of dimensions, and num/name of parameters
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
896
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
897 rewind(f_src); //reset data file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
898
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
899 orig_data = (double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
900 memset(orig_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
901 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
902 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
903 orig_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
904 memset(orig_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
905 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
906
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
907 readsource(f_src, file_Len, num_dm, orig_data, time_id); //read the data;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
908
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
909 fclose(f_src);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
910 /////////////////////////////////////////////////////////////////////////////
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
911 getctrfileinfo(f_src_ctr, &num_clust); //get how many populations
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
912 norm_used=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
913 dist_used=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
914 kmean_term=2; //modified on Oct 16, 2009: changed kmean_term=1 to kmean_term=2
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
915
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
916 rewind(f_src_ctr); //reset center file pointer
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
917
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
918 //read population center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
919 cluster_center=(double **)malloc(sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
920 memset(cluster_center,0,sizeof(double*)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
921 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
922 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
923 cluster_center[i]=(double*)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
924 memset(cluster_center[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
925 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
926 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
927 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
928 cluster_center[i][j]=0;
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
929
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
930 IDmapping=(long *)malloc(sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
931 memset(IDmapping,0,sizeof(long)*num_clust);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
932
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
933 readcenter(f_src_ctr,num_clust,num_dm,cluster_center,IDmapping); //read population center
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
934 fclose(f_src_ctr);
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 normalized_data=(double **)malloc(sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
938 memset(normalized_data,0,sizeof(double*)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
939 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
940 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
941 normalized_data[i]=(double *)malloc(sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
942 memset(normalized_data[i],0,sizeof(double)*num_dm);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
943 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
944
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
945 tran(orig_data, file_Len, num_dm, norm_used, normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
946 /************************************************* Compute number of clusters *************************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
947
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
948 cluster_id=(long*)malloc(sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
949 memset(cluster_id,0,sizeof(long)*file_Len);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
950
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
951 assign_event(normalized_data,num_clust,dist_used,kmean_term,file_Len,num_dm,cluster_id,cluster_center,0);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
952
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
953
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
954 //show(orig_data,cluster_id,file_Len,num_clust,num_dm,show_data,num_disp,name_string);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
955 show(orig_data, cluster_id, file_Len, num_clust, num_dm, name_string, IDmapping);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
956
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
957 f_cid=fopen("population_id.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
958
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
959 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
960 fprintf(f_cid,"%ld\n",IDmapping[cluster_id[i]]);
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 fclose(f_cid);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
964
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
965 //added April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
966 f_mfi=fopen("MFI.txt","w");
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
967
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
968 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
969 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
970 fprintf(f_mfi,"%ld\t",IDmapping[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
971
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
972 for (j=0;j<num_dm;j++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
973 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
974 if (j==num_dm-1)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
975 fprintf(f_mfi,"%.0f\n",cluster_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
976 else
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
977 fprintf(f_mfi,"%.0f\t",cluster_center[i][j]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
978 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
979 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
980 fclose(f_mfi);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
981
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
982 //ended April 16, 2009
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
983
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
984 for (i=0;i<num_clust;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
985 free(cluster_center[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
986 free(cluster_center);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
987
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
988
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
989 /********************************************** Release memory ******************************************/
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
990
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
991 for (i=0;i<file_Len;i++)
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
992 {
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
993 free(orig_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
994 free(normalized_data[i]);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
995 }
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
996
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
997 free(orig_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
998 free(normalized_data);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
999 free(cluster_id);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1000 free(IDmapping);
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1001
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1002 /*
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1003 _strtime( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1004 printf( "Ending time:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1005 _strdate( tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1006 printf( "Ending date:\t\t\t\t%s\n", tmpbuf );
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1007 */
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1008
7eab80f86779 add FLOCK
immport-devteam
parents:
diff changeset
1009 }