-
Notifications
You must be signed in to change notification settings - Fork 1
/
SextractUShapelets.c
381 lines (260 loc) · 9.43 KB
/
SextractUShapelets.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "serial_compute.h"
#define UPPER 50
#define LOWER 45
#define STEP 5
void compute_mean_stddev(int *dataset_A, int dataset_Alen, double *dist, int newsize, double *mean, double *stddev)
{
double sum = 0.0, avg = 0.0, dev = 0.0;
int i;
//for(i=0 ; i<dataset_Alen; i++) printf("%d ", dataset_A[i]);
//for(i=0 ; i<newsize; i++) printf("%f ", dist[i]);
//printf("\n");
for(i = 0; i < dataset_Alen; i++) {
sum += dist[dataset_A[i]];
}
printf("sum is %f\n", sum);
avg = sum/dataset_Alen;
for(i=0; i < dataset_Alen; i++) {
dev += pow((dist[dataset_A[i]] - avg), 2.0);
}
*mean = avg;
*stddev = sqrt(dev / dataset_Alen);
}
int clustered(int newcluster, int newsize) {
//printf("Newsize is %d Newcluster is %d\n", newsize, newcluster);
return ((newsize == newcluster) || (newcluster < 2));
}
int max_index(double *gap, int total)
{
int max = 0;
int i;
for(i=1; i< total; i++) {
//printf("gap of i is %f max is %f indx %d\n ", gap[i], gap[max], i);
if(CompareDoubles2(gap[max], gap[i]) < 0) {
max = i;
}
}
printf("\n\nmax is %d\n\n ", max);
return max;
}
void extractU_Shapelets(double **pd_Dataset, int* ds_len, int n_sample, int sLen, int app_no, char app[][100], char file[][100], char*outputname, int start_ts_id)
{
int sl;
int i;
int iter = 0;
int cluster_id[n_sample];
double *ts;
int ts_len;
int cnt;
int newsize;
int k,j,l;
int index, index2;
double mean, stddev, range;
/*Empty discriminatory ushapelets*/
double* ushapelet[n_sample];
int ushapelet_len[n_sample];
ts = pd_Dataset[start_ts_id];
ts_len = ds_len[start_ts_id];
printf("\nextractU_Shapelet\n");
memset(cluster_id, -1, n_sample *sizeof(int));
FILE *fp;
fp = fopen(outputname, "a");
if(!fp) {
perror(outputname);
return;
}
fprintf(fp, "\n\n\nNew Clustering Batch------------------------------\n");
fprintf(fp,"************************\n");
fprintf(fp, "Iteration %d\n", iter);
fprintf(fp, "Application %s\n Dataset path %s\n", app[start_ts_id], file[start_ts_id]);
while(1) {
cnt = 0;
newsize = 0;
for(sl=sLen-LOWER; sl <= sLen+UPPER && sl <= ts_len; sl+=STEP) {
cnt += (ts_len - sl+1);
// printf("sl = %d ts_len =%d cnt = %d\n", sl, ts_len, cnt);
}
double* p_subseq[cnt];
int ps_len[cnt];
double gap[cnt];
double dt[cnt];
for (k = 0, j= 0 ; k< n_sample; k++) {
if(cluster_id[k] == -1)
newsize++;
}
double* n_dataset[newsize];
int n_datalen[newsize];
int k,j;
int old_id[n_sample];
memset(old_id, 0, newsize*sizeof(int));
/*create new unclustered data set*/
for (k = 0, j= 0 ; k< n_sample; k++) {
if(cluster_id[k] == -1) {
// copy the data set and len and keep a mapping
n_dataset[j] = pd_Dataset[k];
n_datalen[j] = ds_len[k];
old_id[j] = k;
j++;
}
}
double dist[newsize];
/*index of the distance within threshold*/
int dataset_A[newsize];//--------Check: declared in both gobal and local
int dataset_Alen;//--------Check: declared in both gobal and local
memset(dataset_A, -1, newsize *sizeof(int));
/*********VERIFY*******/
if(ts_len < sLen) {
printf(" The time series is too short to classify\n");
break; // break?
}
int cluster_no = app_no;
/*****VERIFY END****/
cnt = 0;
/*For all possible subsequences for a timeseries from the new dataset*/
for(sl=sLen-LOWER; sl <= sLen+UPPER && sl <= ts_len; sl+=STEP) {
//printf("sl is %d ts_len -sl +1 is %d \n\n", sl, ts_len - sl + 1);
for(i=0; i< ts_len - sl +1; i++) {
p_subseq[cnt] = ts + i;
ps_len[cnt] = sl;
/*Compute the gap and threshold for each of the subsequence*/
gap[cnt] = computeGap(&dt[cnt], cluster_no, p_subseq[cnt], ps_len[cnt], n_dataset, newsize , n_datalen);
//printf("i=%d sl=%d ps_len=%d cnt = %d\n", i, sl, ps_len[cnt], cnt);
//printf("gap is %2.6f dt is %2.6f\n", gap[cnt], dt[cnt]);
cnt++;
}
}
/*Find the subsequence which gives the maximum gap for the dataset*/
printf("max gap is %d\n", cnt);
index = max_index(gap, cnt);
/*Add the discriminatory subsequence to the ushapelet list*/
printf("Discovered ushapelet gap is %2.6f dt is %2.6f index %d len %d\n", gap[index], dt[index], index, ps_len[index]);
fprintf(fp, "Shapelet: ");
for(k=0; k<ps_len[index]; k++)
fprintf(fp,"%2.2f ", p_subseq[index][k]);
printf("\n");
ushapelet[iter] = p_subseq[index];
ushapelet_len[iter] = ps_len[index];
fprintf(fp, "Shapelet len: %d\n",ps_len[index]);
dataset_Alen = 0;
j=0;
for(l=0; l<newsize; l++) {
/*Compute the minimum distance of the shapelet from each of the dataset */
dist[l]= computeDistance(p_subseq[index], ps_len[index], n_dataset[l], n_datalen[l]);
/*If the computed distance is less than threshold then add to Dataset A*/
if (CompareDoubles2(dist[l], dt[index]) <= 0) {
//printf("distance within threshold %2.2f %2.2f %d\n", dist[l], dt[index], l);
dataset_A[j] = l;
j++;
dataset_Alen++;
}
}
if(clustered(dataset_Alen, newsize)) break;
else {
mean = 0.0;
stddev = 0.0;
range = 0.0;
/*Compute the mean standard deviation and range of the Dataset A*/
compute_mean_stddev(dataset_A, dataset_Alen, dist, newsize, &mean, &stddev);
range = mean + stddev;
//printf("%2.2f is the range mean %2.2f stddev %2.2f \n", range, mean, stddev);
/*Exclude all the dataset within the range by marking it as clustered*/
for (k = 0, j= 0 ; k< newsize; k++) {
if(CompareDoubles2(dist[k], range) <= 0) {
//printf("Clustered dataset %d\n", old_id[k]);
fprintf(fp, "Appname: %s Filename: %s\n", app[old_id[k]], file[old_id[k]]);
cluster_id[old_id[k]] = iter;
}
}
/*Find the dataset far away from the ushapelet*/
//printf("max distance is at %d\n", newsize);
index2 = max_index(dist, newsize);
ts = n_dataset[index2];
//printf("Finding next data set is at %d\n", index2);
fprintf(fp,"************************\n");
fprintf(fp, "Iteration %d\n", iter+1);
fprintf(fp, "Application %s\n Dataset path %s\n", app[old_id[index2]], file[old_id[index2]]);
}
++iter;
}
fprintf(fp,"************************\n");
fprintf(fp, "Remaining set\n");
for( int z=0; z< n_sample; z++) {
if(cluster_id[z]==-1)
fprintf(fp, "Appname: %s Filename: %s\n", app[z], file[z]);
}
fclose(fp);
printf("\n");
}
//sine wave generation
double sin(double x)
{
double res=0, pow=x, fact=1;
for(int i=0; i<5; ++i)
{
res+=pow/fact;
pow*=x*x;
fact*=(2*(i+1))*(2*(i+1)+1);
}
return res;
}
double* generate_sinewave(double phase, double amp, double freq, int len){
double* result = (double*) malloc(sizeof(double)*len);
for (int i=0; i<len; i++) {
result[i] = amp*sin(phase+(double)i/freq);
}
return result;
}
double* generate_stepwave(int step_len, int len, double amp){
double* result = (double*) malloc(sizeof(double)*len);
for (int i=0; i<len; i++) {
result[i] = (double)(((i/step_len)%2)*2 - 1)*amp;
}
return result;
}
int main(int argc, char *argv[])
{
int set_no = 30;//even number better
double* pd_Dataset[set_no];
int ds_len[set_no];
char appname[set_no][100];
char inputfile[set_no][100];
FILE* serial_synthesis_file = fopen("serial_synthesis_input.txt", "w");
//three sets of data
//set 1 sine
for (int i=0; i<set_no/2; i++) {
ds_len[i] = rand()%5 + 100;
double phase = ((double) (rand()%10))/3.0;
double amp = ((double) (rand()%10))/3.0+1.0;
double freq = (double) (rand()%10)+1.0;
pd_Dataset[i] = generate_sinewave(phase, amp, freq, ds_len[i]);
sprintf(appname[i], "sinewave");
sprintf(inputfile[i], "sinewave_%d.txt", i);
}
//set 2 step
for (int i=set_no/2; i<set_no; i++) {
ds_len[i] = rand()%5 + 100;
int step = rand()%5+1;
double amp = ((double) (rand()%10))/3.0+1.0;
pd_Dataset[i] = generate_stepwave(step, ds_len[i],amp);
sprintf(appname[i], "stepwave");
sprintf(inputfile[i], "stepwave_%d.txt", i);
}
printf("\nInput set generated......");
fprintf(serial_synthesis_file, "Input set:\n");
for (int i=0; i<set_no; i++) {
for (int j=0; j<ds_len[i]; j++) {
fprintf(serial_synthesis_file, "%f ", pd_Dataset[i][j]);
}
fprintf(serial_synthesis_file, "\n\n");
}
fclose(serial_synthesis_file);
char* outputfile = "serial_synthesis_output.txt";
for (int i=0; i<30; i++) {
extractU_Shapelets(pd_Dataset, ds_len, set_no, 50, 3, appname, inputfile,outputfile,i);
}
for (int i = 0; i< 30; i++)
free(pd_Dataset[i]);
}