File: gbhisto.c

package info (click to toggle)
gbutils 5.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,300 kB
  • sloc: ansic: 21,535; sh: 4,482; makefile: 138
file content (531 lines) | stat: -rw-r--r-- 14,381 bytes parent folder | download
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
  gbhisto (ver. 5.6.1) -- Produce histogram from data
  Copyright (C) 1998-2018 Giulio Bottazzi

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  (version 2) as published by the Free Software Foundation;
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "tools.h"


void printhist(double const *data,int const size,
	       int const steps,int const o_output,
	       int const o_bintype,double ratio,
	       double const refpoint,double xmin, double xmax) {

  int i=0;
  double stepsize;
  int *histo;
  double *midpoint,*width;
  int index=0;

  histo = (int *) malloc(steps*sizeof(int));
  midpoint = (double *) malloc(steps*sizeof(double));
  width = (double *) malloc(steps*sizeof(double));

  for(i=0;i<steps;i++){
    histo[i]=0;
  }

  if(o_bintype == 0){		/* linear binning */    
    stepsize=(xmax-xmin)/steps;
    
    for(i=0;i<steps;i++)
      midpoint[i] = xmin+stepsize*(i+refpoint);

    for(i=0;i<steps;i++)
      width[i] = stepsize;
    
    for(i=0;i<size;i++){
      const double val = data[i];
      if(val<xmax){
	index = (int) floor((val-xmin)/stepsize);
	index = (index<0?0:index);
	histo[index]++;
      }
      else if(val==xmax){
	histo[steps-1]++;
      }
    }
  }
  else if(o_bintype==1){	/* log binning */
    int ignored=0;
    double lxmin;

    if(xmax <=0) {
      fprintf(stderr,"ERROR (%s): no positive values! Exiting...\n",GB_PROGNAME);
      exit(-1);
    }
    else if(xmin <=0){      
      /* determine the smallest positive value */
      xmin=xmax;
      for(i=0;i<size;i++){
	if( data[i]>0 && xmin > data[i]) xmin = data[i]; 
      }
    }

    lxmin=log(xmin);

    stepsize=(log(xmax)-log(xmin))/steps;
    
    for(i=0;i<steps;i++)
      midpoint[i] = xmin*exp(stepsize*(i+refpoint));

    for(i=0;i<steps;i++)
      width[i] = xmin*( exp(stepsize*(i+1))-exp(stepsize*i) );
    
    for(i=0;i<size;i++){
      const double val = data[i];
      if(val>=xmin && val<xmax){
	index = (int) floor((log(val)-lxmin)/stepsize);
	index = (index<0?0:index);
	histo[index]++;
      }
      else if(val==xmax){
	histo[steps-1]++;
      }
      else if (val<xmin){
	ignored++;
      }
    }

    if(ignored>0)
      fprintf(stderr,"WARNING (%s): %d non positive values ignored\n",GB_PROGNAME,ignored);

  }
  else if(o_bintype==2){	/* geometric binning */

    double lratio = log(ratio);

    stepsize=(xmax-xmin)/(exp(lratio*steps)-1.);

    for(i=0;i<steps;i++){
      midpoint[i] = xmin+(exp(lratio*(refpoint+(double) i))-1.)*stepsize;
      width[i] = exp(lratio*i)*(ratio-1.)*stepsize;
    }
    
    for(i=0;i<size;i++){
      const double val = data[i];
      if(val<xmax){
	index = (int) floor( log(1.+(val-xmin)/stepsize)/lratio );
	index = (index<0?0:index);
	histo[index]++;
      }
      else if(val==xmax){
	histo[steps-1]++;
      }
    }
  }
  
  if(o_output==2){
    for(i=0;i<steps;i++){
      printf(FLOAT_SEP,midpoint[i]);
      printf(FLOAT_NL,(double) histo[i]/(size*width[i]));
    }
  }
  else if(o_output==1){
    for(i=0;i<steps;i++){
      printf(FLOAT_SEP,midpoint[i]);
      printf(FLOAT_NL,(double) histo[i]/size);
    }
  }
  else if(o_output==5){
    double cumprob=0;
    for(i=0;i<steps;i++){
      cumprob += (double) histo[i]/size;
      printf(FLOAT_SEP,midpoint[i]);
      printf(FLOAT_NL,cumprob);
    }
  }
  else if(o_output==6){
    double cumprob=0;
    for(i=0;i<steps;i++){
      cumprob += (double) histo[i]/size;
      printf(FLOAT_SEP,midpoint[i]);
      printf(FLOAT_NL,1-cumprob);
    }
  }
  else {
    for(i=0;i<steps;i++){
      printf(FLOAT_SEP,midpoint[i]);
      printf(INT_NL,histo[i]);
    }
  }
  
}


void printhist_discrete(double *data,int size,int const o_output) {


  qsort(data,size,sizeof(double),sort_by_value);

  int i=0,count=1;

  if(o_output==3){
  
    while(i<size){
      if(i<size-1 && data[i+1]==data[i]) count++;
      else{
	if(floor(data[i])==data[i])
	  printf(INT_SEP,(int) data[i]);
	else 
	  printf(FLOAT_SEP,data[i]);
	printf(INT_NL,count);
	count=1;
      }
      i++;
    }

  }
  else if(o_output==4){

    while(i<size){
      if(i<size-1 && data[i+1]==data[i]) count++;
      else{
	if(floor(data[i])==data[i])
	  printf(INT_SEP,(int) data[i]);
	else 
	  printf(FLOAT_SEP,data[i]);
	printf(FLOAT_NL,(double) count/size);
	count=1;
      }
      i++;
    }

  }

}




int main(int argc,char* argv[]){

  unsigned steps=10;    
  int o_output=0;/*output type*/
  int o_bintype=0;/*binning type*/
  double refpoint=0.5;/* bin reference point  */
  int o_table=0;/*if input is of tabular type*/
  int o_window=0; /* if the windows to bin is provided */

  double wmin,wmax;

  double ratio=1.618; /*golden ratio by default... meaningless but nice*/
  
  int o_verbose=0;

  char *splitstring = strdup(" \t");
  
  /* COMMAND LINE PROCESSING */
  
  /* variables for reading command line options */
  /* ------------------------------------------ */
  int opt;
  /* ------------------------------------------ */
  
  /* read the command line */    
  while((opt=getopt_long(argc,argv,"n:M:tvhB:r:F:w:S:",gb_long_options, &gb_option_index))!=EOF){
    if(opt==0){
      gbutils_header(argv[0],stdout);
      exit(0);
    }
    else if(opt=='?'){
      fprintf(stderr,"ERROR (%s): option %c not recognized\n",GB_PROGNAME,optopt);
      return(-1);
    }
    else if(opt=='F'){
      /*set the fields separator string*/
      free(splitstring);
      splitstring = strdup(optarg);
    }
    else if(opt=='w'){
      /*set the binning window */

      char *stmp1=strdup (optarg);
      char *stmp2;
      char *stmp3=stmp1;

      stmp2=strsep (&stmp1,",");
      if(strlen(stmp2)>0 && stmp1 != NULL && strlen(stmp1)>0){
	wmin=atof(stmp2);	
	wmax=atof(stmp1);
      }
      else
	fprintf(stderr,"ERROR (%s): provide comma separated min and max. Try option -h.\n",GB_PROGNAME);

      free(stmp3);

      o_window=1;
    }
    else if(opt=='n'){
      /*set the number of steps*/
      steps = (unsigned) atoi(optarg);
    }
    else if(opt=='M'){
	/*set the type of output*/
      o_output = atoi(optarg);
      if(o_output<0 || o_output>6){
	fprintf(stderr,"ERROR (%s): output option '%d' not recognized. Try option -h.\n",
		GB_PROGNAME,o_output);
	exit(-1);
      } 
    }
    else if(opt=='B'){
      /*set the type of binning*/
      o_bintype = atoi(optarg);
      if(o_bintype <0 || o_bintype>2 ) {
	fprintf(stderr,"ERROR (%s): binning option '%d' not recognized. Try option -h.\n",
		GB_PROGNAME,o_bintype);
	exit(-1);
      }
    }
    else if(opt=='S'){
      /*set the type of binning*/
      refpoint = atof(optarg);
      if(refpoint <0 || refpoint >1 ) {
	fprintf(stderr,"ERROR (%s): unsupported reference point value '%f'. Try option -h.\n",
		GB_PROGNAME,refpoint);
	exit(-1);
      }
    }
    else if(opt=='r'){
	/*set the ration in the geometric binning*/
      ratio = atof(optarg);
      if(ratio <=1) {
	fprintf(stderr,"ERROR (%s): option -r requires a value larger than one. Try option -h.\n",
		GB_PROGNAME);
	exit(-1);
      }
    }
    else if(opt=='t'){
      /*set the table form*/
      o_table=1;
    }
    else if(opt=='v'){
      /*set verbose output*/
      o_verbose=1;
    }
    else if(opt=='h'){
      /*print short help*/
      fprintf(stdout,"Compute histogram counting the occurrences of each value (discrete data) or\n");
      fprintf(stdout,"binning the values on a regular grid (continuous data). Data are read from \n");
      fprintf(stdout,"standard input. For discrete data, values and their occurrences are printed;\n");
      fprintf(stdout,"For binned data, the bin reference point and the relative statistics are\n");
      fprintf(stdout,"printed. The type of variable and the statistics can be set with option -M.\n");
      fprintf(stdout,"The binning grid can be linear, logarithmic or geometric. The bin reference\n");
      fprintf(stdout,"point is the algebraic midpoint for linear binning and the geometric midpoint\n");
      fprintf(stdout,"for log or geometric binning. The reference point can be moved using option\n");
      fprintf(stdout,"-S. Set it to 0 for the lower bound or to 1 for the upper bound.\n");
      fprintf(stdout,"\nUsage: %s [options]\n\n",argv[0]);
      fprintf(stdout,"Options:\n");
      fprintf(stdout," -n  number of equispaced bins where the histogram is computed (default 10)\n");
      fprintf(stdout," -w  min,max set manually the binning window                               \n");
      fprintf(stdout," -M  set the type of output (default 0)                                    \n");
      fprintf(stdout,"      0  occurrences number (continuous binned variable)\n");
      fprintf(stdout,"      1  relative frequency (continuous binned variable)\n");
      fprintf(stdout,"      2  empirical density  (continuous binned variable)\n");
      fprintf(stdout,"      3  occurrences number (discrete variable)\n");
      fprintf(stdout,"      4  relative frequency (discrete variable)\n");
      fprintf(stdout,"      5  CDF (left cumulated probability; continuous binned variable)\n");
      fprintf(stdout,"      6  inverse CDF (1-CDF; continuous binned variable)\n");
      fprintf(stdout," -B  set the type of binning (default 0)                                   \n");
      fprintf(stdout,"      0  linear\n");
      fprintf(stdout,"      1  logarithmic\n");
      fprintf(stdout,"      2  geometric\n");
      fprintf(stdout," -S  set the bin reference point (default 0.5)                              \n");
      fprintf(stdout," -r  set the ratio for geometrical binning  (default 1.618)               \n");
      fprintf(stdout," -t  print the histogram for each imput column\n");
      fprintf(stdout," -F  specify the input fields separators (default \" \\t\")                \n");
      fprintf(stdout," -v  verbose mode\n");
      exit(0);
    }
  }
  /* END OF COMMAND LINE PROCESSING */

  /* initialize global variables */
  initialize_program(argv[0]);
  

  /* ++++++++++++++++++++++++++++ */
  if(o_verbose){
    fprintf(stdout,"#variable type:\t");
    switch(o_output){
    case 0:
    case 1:
    case 2:
      fprintf(stdout,"continuous");
      break;
    case 3:
      fprintf(stdout,"discrete");
      break;
    }
    fprintf(stdout,"\n");

    fprintf(stdout,"#output type:\t");
    switch(o_output){
    case 0:
      fprintf(stdout,"occurencies number");
      break;
    case 1:
      fprintf(stdout,"relative frequency");
      break;
    case 2:
      fprintf(stdout,"empirical density");
      break;
    case 3:
      fprintf(stdout,"occurencies number");
      break;
    case 4:
      fprintf(stdout,"relative frequency");
      break;
    case 5:
      fprintf(stdout,"left cumulated probability");
      break;
    case 6:
      fprintf(stdout,"right cumulated probability");
      break;
    }
    fprintf(stdout,"\n");

    fprintf(stdout,"#binning:\t");
    switch(o_bintype){
    case 0:
      fprintf(stdout,"linear");
    case 1:
      fprintf(stdout,"logarithmic");
    case 2:
      fprintf(stdout,"geometric with ratio %f",ratio);
      break;
    }
    fprintf(stdout,"\n");

    fprintf(stdout,"#reference point: %f\n",refpoint);

    fprintf(stdout,"#bins number:\t");
    switch(o_output){
    case 0:
    case 1:
    case 2:
      fprintf(stdout,"%d",steps);
      break;
    }
    fprintf(stdout,"\n");

    fprintf(stdout,"#tabular data:\t");
    switch(o_table){
    case 0:
      fprintf(stdout,"off");
      break;
    case 1:
      fprintf(stdout,"on");
      break;
    }
    fprintf(stdout,"\n");

    fprintf(stdout,"#data statistics:\n");
    fprintf(stdout,
	    "# mean      stdev     skewness  kurtosis  ave.dev.  min       max\n");
  }  
  /* ++++++++++++++++++++++++++++ */


  
  if(o_table){
    size_t rows=0,columns=0,i;
    double **data=NULL;
    
   loadtable(&data,&rows,&columns,0,splitstring);
    

    /* ++++++++++++++++++++++++++++ */
    if(o_verbose){
      for(i=0;i<columns;i++){
	double xmean,xvar,xsd,xskew,xkurt,xadev,xmin,xmax;
	moment(data[i],rows,&xmean,&xadev,&xsd,&xvar,&xskew,&xkurt,&xmin,&xmax);
	fprintf(stdout,"# %+.2e %+.2e %+.2e %+.2e %+.2e %+.2e %+.2e %zd\n",
		xmean,xsd,xskew,xkurt,xadev,xmin,xmax,rows);
      }
    }
    /* ++++++++++++++++++++++++++++ */

    if(o_output>=3 && o_output<=4)
      for(i=0;i<columns;i++)
	printhist_discrete(data[i],rows,o_output);
    else      
      for(i=0;i<columns;i++){
	size_t j;
	double xmin = DBL_MAX;  /*set maximum double */
	double xmax = -DBL_MAX;  /*set to minimum double */
	
	/* determination of xmax and xmin */
	if(o_window){
	  xmin=wmin;
	  xmax=wmax;
	}
	else
	  for(j=0;j<rows;j++){
	    if( xmin > data[i][j]) xmin = data[i][j]; 
	    if( xmax < data[i][j]) xmax = data[i][j];
	  }
	/* ------------------------------ */

	printhist(data[i],rows,steps,o_output,o_bintype,ratio,refpoint,xmin,xmax);
	if(i<columns-1) printf("\n\n");
      }
   
  }
  else{
    double *data=NULL;
    size_t size;
    
   load(&data,&size,0,splitstring);
    
    /* ++++++++++++++++++++++++++++ */
    if(o_verbose){
      double xmean,xvar,xsd,xskew,xkurt,xadev,xmin,xmax;
      moment(data,size,&xmean,&xadev,&xsd,&xvar,&xskew,&xkurt,&xmin,&xmax);
      fprintf(stdout,"# %+.2e %+.2e %+.2e %+.2e %+.2e %+.2e %+.2e %zd\n",
	      xmean,xsd,xskew,xkurt,xadev,xmin,xmax,size);
    }
    /* ++++++++++++++++++++++++++++ */
    
    if(o_output>=3 && o_output<=4) printhist_discrete(data,size,o_output);
    else {
      
      size_t i;
      double xmin = DBL_MAX;  /*set maximum double */
      double xmax = -DBL_MAX;  /*set to minimum double */

      
      /* determination of xmax and xmin */
      if(o_window){
	xmin=wmin;
	xmax=wmax;
      }
      else
	for(i=0;i<size;i++){
	  if( xmin > data[i]) xmin = data[i]; 
	  if( xmax < data[i]) xmax = data[i];
	}
      /* ------------------------------ */

      printhist(data,size,steps,o_output,o_bintype,ratio,refpoint,xmin,xmax);
    }

  }

  
  return(0);
}