-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstats.cpp
More file actions
349 lines (314 loc) · 9.43 KB
/
stats.cpp
File metadata and controls
349 lines (314 loc) · 9.43 KB
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
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Purpose: Compute statistics on a column of a given datafile
Changes log:
- Apr/7-JZ: Checked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
#include <dynamo.h>
int main(int argc,char *argv[])
{
//////////////////////////////////////////////////////////////
//PROGRAM VARIBALES
//////////////////////////////////////////////////////////////
char datafile[FSIZE]="",statsfile[FSIZE]="",typebin='l';
int numcols=0,col=0,colw=0;
int numbin=10,npb=0;
int numlines;
int i;
//////////////////////////////////////////////////////////////
//INITIALIZE
//////////////////////////////////////////////////////////////
TITLE(stdout,'*',"STATISTICS FOR DATA FILE");
//////////////////////////////////////////////////////////////
//SET OPTIONS AND USAGE
//////////////////////////////////////////////////////////////
SET_OPTIONS(":hvVf:s:n:c:b:t:N:w:");
SET_USAGE(
"=======================================================================================\n"
"Usage:\n\n"
"\t./program -f <datafile> [-s <statsfile>] -n <numcols> -c <col> [-w <col_weight>]\n"
"\t -b <numbins> [-t <type_binning> -N <min_number_per_bin>]\n"
"\n"
"Compute basic statistics on column <col> in <datafile> and generate an histogram.\n"
"Optionally a column <col_weight> with weights used to make weight statistics, could\n"
"be probided (example mass of particles)\n"
"\n"
"Basic statistics are stored in the header of the <statsfile>.\n"
"The type of bining <type_binning> is selected between (l)inear, lo(g)arithmic,\n"
"(a)daptative (with <min_number_per_bin> threshold\n"
"=======================================================================================\n"
);
//////////////////////////////////////////////////////////////
//READ OPTIONS
//////////////////////////////////////////////////////////////
while(ITEROPTIONS){
switch(OPTION){
case 'f':
strcpy(datafile,optarg);
break;
case 's':
strcpy(statsfile,optarg);
break;
case 'n':
numcols=atoi(optarg);
break;
case 'c':
col=atoi(optarg);
break;
case 'w':
colw=atoi(optarg);
break;
//-------------------
//OPTIONAL
//-------------------
case 'b':
numbin=atoi(optarg);
break;
case 't':
typebin=optarg[0];
break;
case 'N':
npb=atoi(optarg);
break;
//========================================
//COMMON
//========================================
case 'v':
VERBOSITY=1;
break;
case 'V':
VERBOSITY=2;
break;
//DETECT ERRORS
OPTION_ERRORS;
}
}
//////////////////////////////////////////////////////////////
//VALIDATE OPTIONS
//////////////////////////////////////////////////////////////
if(isBlank(datafile)){
fprintf(stderr,"Error: No datafile was provided\n");
PRINT_USAGE;
EXIT;
}
if(!fileExists(datafile)){
fprintf(stderr,"Error: Datafile '%s' does not exist\n",datafile);
PRINT_USAGE;
EXIT;
}
if((numlines=countLines(datafile))==0){
fprintf(stderr,"Error: Datafile '%s' seems empty\n",datafile);
PRINT_USAGE;
EXIT;
}
if(numcols<1){
fprintf(stderr,"Error: The number of columns should be different from 0\n");
PRINT_USAGE;
EXIT;
}
if(isBlank(statsfile)){
sprintf(statsfile,"%s.his",datafile);
}
if(col==0){
col=1;
}else if(col>numcols){
fprintf(stderr,"Error: Column %d out of range (%d)\n",col,numcols);
PRINT_USAGE;
EXIT;
}
if(colw>numcols){
fprintf(stderr,"Error: Column %d out of range (%d)\n",col,numcols);
PRINT_USAGE;
EXIT;
}
if(npb==0){
npb=numlines/numbin;
}
//////////////////////////////////////////////////////////////
//REPORT INPUT INFORMATION
//////////////////////////////////////////////////////////////
if(VERBOSE(1)){
BAR(stdout,'O');
fprintf(stdout,"Datafile: %s\n",datafile);
fprintf(stdout,"Number of datapoints: %d\n",numlines);
fprintf(stdout,"Statistics file: %s\n",statsfile);
fprintf(stdout,"Number of columns: %d\n",numcols);
fprintf(stdout,"Selected column: %d\n",col);
if(colw)
fprintf(stdout,"Weight column: %d\n",colw);
else
fprintf(stdout,"Constant weight");
fprintf(stdout,"Number of bins: %d\n",numbin);
fprintf(stdout,"Type of binning: %c\n",typebin);
fprintf(stdout,"Minimum per bin: %d\n",npb);
BAR(stdout,'O');
}
//////////////////////////////////////////////////////////////
//PROGRAM
//////////////////////////////////////////////////////////////
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//READING DATA
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
STPRINTF("Reading data from datafile '%s'...\n",datafile);
real2 wntot;
real2 *weights;
real2 *values=readColumn(datafile,numlines,numcols,col);
if(colw){
//==================================================
//WEIGHTED AVERAGE
//==================================================
weights=readColumn(datafile,numlines,numcols,colw);
wntot=0;
for(i=0;i<numlines;i++) wntot+=weights[i];
}else{
//==================================================
//NORMAL AVERAGE
//==================================================
weights=(real2*)calloc(numlines,sizeof(real2));
for(i=0;i<numlines;i++) weights[i]=1;
wntot=numlines;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//STATISTICS
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int ic;
real2 min=MAXREAL,max=MINREAL,mean=0,median=0,rms=0,sd=0;
real2 q25,q50,q75,mode=MINREAL,nmode=MINREAL;
STPRINTF("Computing basic statistics...\n",numlines);
//Sort
gsl_sort(values,1,numlines);
//Basic statistics
//COMPUTE THIS STATISTICS BUT IN THE WEIGHTED CASE
mean=gsl_stats_mean(values,1,numlines);
sd=gsl_stats_sd(values,1,numlines);
rms=sqrt((numlines-1)/numlines*sd*sd+mean*mean);
gsl_stats_minmax(&min,&max,values,1,numlines);
q25=gsl_stats_quantile_from_sorted_data(values,1,numlines,0.25);
median=q50=gsl_stats_quantile_from_sorted_data(values,1,numlines,0.50);
q75=gsl_stats_quantile_from_sorted_data(values,1,numlines,0.75);
//Binning
real2* bins=(real2*)calloc(numbin+1,sizeof(real2));
real2* nhis=(real2*)calloc(numbin,sizeof(real2));
real2* nhisw=(real2*)calloc(numbin,sizeof(real2));
real2 xini,x,dx,xmed,xend;
switch(typebin){
case 'l':
xini=min;
dx=(max-min)/numbin;
break;
case 'g':
if(gsl_isinf(log10(min))!=0 || gsl_isinf(log10(min))!=0){
fprintf(stderr,"Error: A logarithmic binning cannot be used for this data (min:%g,max:%g).\n\tTry with linear or adaptative binning\n",min,max);
PRINT_USAGE;
EXIT;
}
xini=log10(min);
dx=(log10(max)-log10(min))/numbin;
break;
default:
bins[0]=xini=min;
dx=0;
break;
}
if(typebin!='a'){
for(ic=0;ic<=numbin;ic++){
bins[ic]=xini+ic*dx;
}
xend=bins[1];
}else{
bins[0]=min;
}
//Compute the histogram
STPRINTF("Computing histogram...\n",numlines);
ic=0;
for(i=0;i<numlines;i++){
//Value of x
if(typebin=='g') x=log10(values[i]);
else x=values[i];
//Test change of bin
if(typebin=='a'){
if(nhis[ic]>npb){
ic++;
bins[ic]=x;
}
if(ic>numbin){
fprintf(stderr,"Error: The number of bins is not enough for the amount of data\n");
PRINT_USAGE;
EXIT;
}
}else if(gsl_fcmp(x,xend,EPSREAL)>0){
ic++;
xend=bins[ic+1];
}
//Increment
nhis[ic]++;
nhisw[ic]+=weights[i];
}
if(typebin=='a'){
numbin=ic+1;
bins[numbin]=max;
STPRINTF("Number of detected bins: %d\n",numbin);
}
//Compute derivative quantities
for(ic=0;ic<numbin;ic++)
if(nhis[ic]>nmode){
mode=(bins[ic]+bins[ic+1])/2;
nmode=nhis[ic];
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//STORING HISTOGRAM
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file fs=fileOpen(statsfile,"w");
//Header
fprintf(fs,"#Data file: %s, Column: %d\n",datafile,col);
fprintf(fs,"#Typebin: %c\n",typebin);
fprintf(fs,"#Numbins: %d\n",numbin);
fprintf(fs,"#NPB: %d\n",npb);
fprintf(fs,"#Tot.stats(Ntot,Min,Max): %d,%+14.7e %+14.7e\n",numlines,min,max);
fprintf(fs,"#Stats(mean,median,mode,rms,disp): %+14.7e %+14.7e %+14.7e %+14.7e %+14.7e\n",
mean,median,mode,rms,sd);
fprintf(fs,"#Quartiles(25,50,75):%+14.7e,%+14.7e,%+14.7e\n",q25,q50,q75);
fprintf(fs,"%-14s %-14s %-14s %-14s %-14s %-14s ",
"#1:xini","2:xmed","3:xend","4:n","5:h","6:f");
fprintf(fs,"%-14s %-14s %-14s ","7:dn","8:dh","9:df");
fprintf(fs,"%-14s %-14s ","10:F","11:P");
fprintf(fs,"\n");
//Histogram
real2 nh,F,dn,hhis,dh,fhis,df,P;
F=0;
P=0;
for(ic=0;ic<numbin;ic++){
if(typebin=='g'){
xini=pow10(bins[ic]);
xend=pow10(bins[ic+1]);
}else{
xini=bins[ic];
xend=bins[ic+1];
}
xmed=(xini+xend)/2;
dx=(xend-xini);
if(colw)
nh=nhisw[ic];
else
nh=nhis[ic];
hhis=nh/wntot;
fhis=hhis/dx;
dn=sqrt(nh);
dh=dn/wntot;
df=dh/dx;
F+=nh;
P+=hhis;
//xini xend nhis hhis fhis
fprintf(fs,"%+14.7e %+14.7e %+14.7e %+14.7e %+14.7e %+14.7e ",
xini,xmed,xend,nh,hhis,fhis);
//Errors
fprintf(fs,"%+14.7e %+14.7e %+14.7e ",dn,dh,df);
//Cummulative
fprintf(fs,"%+14.7e %+14.7e ",F,P);
fprintf(fs,"\n");
}
fclose(fs);
STPRINTF("Histogram file '%s' saved...\n",statsfile);
return 0;
}