-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_add.c
More file actions
643 lines (582 loc) · 19.3 KB
/
Copy pathfloat_add.c
File metadata and controls
643 lines (582 loc) · 19.3 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/* add two binary "float" files together - report on resulting stats -James Holton 3-29-24
example:
gcc -O -O -o float_add float_add.c -lm
./float_add file1.map file2.map output.map
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#ifndef NAN
#define NAN strtod("NAN",NULL)
#endif
char *infile1name = "";
char *infile2name = "";
FILE *infile1 = NULL;
FILE *infile2 = NULL;
char *outfilename = "output.bin\0";
FILE *outfile = NULL;
float fmedian(unsigned int n, float arr[]);
float fmedian_with_rejection(unsigned int n, float arr[],float sigma,float *mad,int *final_n);
float fmedian_absolute_deviation(unsigned int n, float arr[], float median_value);
float fmean_with_rejection(unsigned int starting_points, float arr[], float sigma_cutoff, double *final_rmsd, int *final_n);
int main(int argc, char** argv)
{
int n,i,j,k,pixels;
float *outimage;
float *inimage1;
float *inimage2;
unsigned short int *invalid_pixel;
float *medbuffer;
float median,mad;
double sum_arej,sumsq_arej,sumd_arej,sumdsq_arej,avg_arej,rms_arej,rmsd_arej;
double sumd3_arej,sumd4_arej,skewness_arej,kurtosis_arej,max_arej,min_arej;
int valid_pixels,n_arej;
float scale1=1.0,scale2=1.0,outscale=1.0,offset=0.0,outoffset=0.0;
double float1, float2, float3, float4;
double result,outdouble;
float outfloat;
int header=0;
int normalize=0;
int overflows=0;
int underflows=0;
int ignore_values=0;
float ignore_value[70000];
int reject_outliers=0;
float outlier_sigma=6.0;
int histogramize=0;
unsigned long int *histogram;
float binsize=1.0,epsilon=0.0;
int bin,bins;
double diff,deviate;
double sum,sumsq,sumd,sumdsq,avg,rms,rmsd,max,min;
double sumd3,sumd4,skewness,kurtosis;
double sumx=0,sumy=0,sumxy=0,sumxx=0,sumyy=0;
double avgx,avgy,avgxy,avgxx,avgyy,CC;
double denom,lsq_scale,lsq_offset;
/* check argument list */
for(i=1; i<argc; ++i)
{
if(strlen(argv[i]) > 4)
{
if(strstr(argv[i]+strlen(argv[i])-4,".bin") || strstr(argv[i]+strlen(argv[i])-4,".map"))
{
printf("filename: %s ",argv[i]);
if(infile1 == NULL){
infile1 = fopen(argv[i],"r");
if(infile1 == NULL){
perror(argv[i]);
}else{
infile1name = argv[i];
printf(" input1");
}
}
else
{
if(infile2 == NULL){
infile2 = fopen(argv[i],"r");
if(infile2 == NULL){
perror(argv[i]);
}else{
infile2name = argv[i];
printf(" input2");
}
}
else
{
outfilename = argv[i];
printf(" output");
}
}
printf("\n");
}
}
if(argv[i][0] == '-')
{
/* option specified */
if(strstr(argv[i], "-scale1") && (argc >= (i+1)))
{
scale1 = atof(argv[i+1]);
}
if(strstr(argv[i], "-scale2") && (argc >= (i+1)))
{
scale2 = atof(argv[i+1]);
}
if(strstr(argv[i], "-outscale") && (argc >= (i+1)))
{
outscale = atof(argv[i+1]);
}
if(strstr(argv[i], "-outfile") && (argc >= (i+1)))
{
outfilename = argv[i+1];
++i;
continue;
}
if(strstr(argv[i], "-output") && (argc >= (i+1)))
{
outfilename = argv[i+1];
++i;
continue;
}
if(strstr(argv[i], "-normalize"))
{
normalize = 1;
}
if(strstr(argv[i], "-histogram"))
{
histogramize = 1;
}
if(strstr(argv[i], "-bin") && (argc >= (i+1)))
{
binsize = atof(argv[i+1]);
}
if(strstr(argv[i], "-reject"))
{
reject_outliers = 1;
}
if(strstr(argv[i], "-offset") && (argc >= (i+1)))
{
offset = atof(argv[i+1]);
outoffset = atof(argv[i+1]);
}
if(strstr(argv[i], "-ignore") && (argc >= (i+1)))
{
++ignore_values;
ignore_value[ignore_values] = atof(argv[i+1]);
}
if(strstr(argv[i], "-epsilon") && (argc >= (i+1)))
{
epsilon = atof(argv[i+1]);
}
if(strstr(argv[i], "-outoffset") && (argc >= (i+1)))
{
outoffset = atof(argv[i+1]);
}
if(strstr(argv[i], "-header") && (argc >= (i+1)))
{
header = atof(argv[i+1]);
}
}
}
if(infile1 == NULL){
printf("usage: float_add file1.bin file2.bin [outfile.bin] -scale1 1.0 -scale2 1.0 -offset 40 -header 512\n");
printf("options:\n");\
printf("\t-scale1 \tscale factor for first file\n");
printf("\t-scale2 \tscale factor for second file\n");
printf("\t-offset \tinteger offset to be subtracted from each file before applying scale\n");
printf("\t-outoffset\tinteger offset to be added to the output file after all scaling\n");
printf("\t-header \tnumber of bytes to ignore in header of each file\n");
printf("\t-normalize \toutput relative difference instead of difference\n");
printf("\t-histogram \tprint out a histogram of output values\n");
printf("\t-binsize \tsize of the bins to use for the histogram\n");
printf("\t-epsilon \tmaximum difference between values to be considered equal\n");
printf("\t-ignore \tflag particular values as bad pixels, kept in output\n");
printf("\t-reject \treject outliers from printed statistics\n");
exit(9);
}
/* load first float-image */
fseek(infile1,0,SEEK_END);
n = ftell(infile1);
fseek(infile1,0,SEEK_SET);
inimage1 = calloc(n,1);
inimage2 = calloc(n,1);
outimage = calloc(n,1);
invalid_pixel = calloc(n,1);
fread(inimage1,n,1,infile1);
fclose(infile1);
if(infile2 != NULL)
{
fseek(infile2,0,SEEK_SET);
fread(inimage2,n,1,infile2);
fclose(infile2);
}
pixels = (n-header)/sizeof(float);
medbuffer = calloc(2*pixels+2,sizeof(float));
outfile = fopen(outfilename,"wb");
if(outfile == NULL)
{
printf("ERROR: unable to open %s\n", outfilename);
exit(9);
}
printf("header = %d bytes pixel zero offset = %g (%g in output)\n",header,offset,outoffset);
for(k=1;k<=ignore_values;++k)
{
printf("ignoring value %g in both files\n",ignore_value[k]);
}
/* copy the header of the first file */
for(i=0;i<=header/sizeof(float);++i)
{
outimage[i] = inimage1[i];
}
sum = sumsq = sumd = sumdsq = 0.0;
min = NAN;
max = NAN;
valid_pixels = 0;
for(j=0;j<pixels;++j)
{
i=j+header/sizeof(float);
float1 = (double) inimage1[i];
float2 = (double) inimage2[i];
if( normalize )
{
/* report relative difference */
avg = ( scale1*(float1-offset) + scale2*(float2-offset) ) / 2.0 ;
diff = scale1*(float1-offset) - scale2*(float2-offset) ;
result = outscale * (diff / avg);
}
else
{
result = outscale * (scale1*(float1-offset) + scale2*(float2-offset));
}
/* convert back to float */
outfloat = outdouble = result+outoffset;
// i = fpclassify(outfloat);
if(isnan(outfloat))
{
++invalid_pixel[i];
}
#ifdef isnormal
if(! isnormal(outfloat))
{
#endif
if(isinf(outfloat))
{
++overflows;
}
#ifdef isnormal
if(! isfinite(outfloat))
{
++underflows;
}
#endif
#ifdef isnormal
}
#endif
/* assign to output array */
outimage[i] = outfloat;
/* skip any invalid values, propagate to output */
for(k=1;k<=ignore_values;++k)
{
if(inimage1[i]==ignore_value[k]){
outimage[i] = ignore_value[k];
++invalid_pixel[i];
/* no need to check others */
break;
}
if(inimage2[i]==ignore_value[k] && infile2 != NULL){
outimage[i] = ignore_value[k];
++invalid_pixel[i];
/* no need to check others */
break;
}
}
/* skip stats for this one */
if(invalid_pixel[i]) continue;
/* only use valid pixels in statistics */
++valid_pixels;
/* don't do median stuff if we don't have to */
medbuffer[valid_pixels]=result;
/* basic stats */
sum += result;
sumsq += result*result;
if(result>max || isnan(max)) max=result;
if(result<min || isnan(min)) min=result;
/* set up for CC calculation */
sumx+=float1;
sumy+=float2;
sumxy+=float1*float2;
sumxx+=float1*float1;
sumyy+=float2*float2;
}
avg = sum/valid_pixels;
rms = sqrt(sumsq/valid_pixels);
denom = (sumxx*valid_pixels)-sumx*sumx;
if( denom == 0 || valid_pixels < 2 )
{
lsq_scale = 0.0;
lsq_offset = 0.0;
}
else
{
lsq_scale = (sumxy*valid_pixels - sumx*sumy)/denom;
lsq_offset = (sumxx*sumy - sumx*sumxy)/denom;
}
printf("xy lsq scale= %lg offset= %lg\n",lsq_scale,lsq_offset);
denom = (sumyy*valid_pixels)-sumy*sumy;
if( denom == 0 || valid_pixels < 2 )
{
lsq_scale = 0.0;
lsq_offset = 0.0;
}
else
{
lsq_scale = (sumxy*valid_pixels - sumx*sumy)/denom;
lsq_offset = (sumyy*sumx - sumy*sumxy)/denom;
}
printf("yx lsq scale= %lg offset= %lg\n",lsq_scale,lsq_offset);
sumd=sumdsq=sumd3=sumd4=0.0;
for(j=0;j<pixels;++j)
{
i=j+header/sizeof(float);
if(invalid_pixel[i]) continue;
diff = outimage[i]-outoffset - avg;
sumd += diff;
sumdsq += diff*diff;
sumd3 += diff*diff*diff;
sumd4 += diff*diff*diff*diff;
}
rmsd = sqrt(sumdsq/valid_pixels);
skewness = sumd3/valid_pixels/(rmsd*rmsd*rmsd);
kurtosis = sumd4/valid_pixels/(rmsd*rmsd*rmsd*rmsd);
if(histogramize)
{
/* default to reasonable bin size */
if(binsize <= 0)
{
binsize = rmsd/3.0;
}
/* data are all same value? */
if(binsize <= 0) binsize = 1.0;
/* calculate number of bins needed for histogram, hope its not ridiculous */
bins = ( ceil( (max - min + 2) / binsize ) );
if(bins > 100000) bins = 100000;
histogram = calloc(bins+1,sizeof(unsigned long int));
if(histogram == NULL)
{
perror("histogram");
}
else
{
for(i=1;i<=valid_pixels;++i)
{
bin = ( ceil( (medbuffer[i] - min) / binsize ) - 0.5 );
++histogram[bin];
}
printf("histogram:\n");
for(bin=0;bin<=bins;++bin)
{
if(histogram[bin]>0)
{
printf("%g %ld\n",binsize*bin+min,histogram[bin]);
}
}
}
}
avgx = sumx/valid_pixels;
avgy = sumy/valid_pixels;
avgxy = sumxy/valid_pixels;
avgxx = sumxx/valid_pixels;
avgyy = sumyy/valid_pixels;
if(avgyy<avgy*avgy)avgyy=avgy*avgy;
if(avgxx<avgx*avgx){
CC=0.0;
}
else
{
CC=(avgxy-avgx*avgy)/(sqrt(avgxx-avgx*avgx)*sqrt(avgyy-avgy*avgy));
}
if(ignore_values || reject_outliers) printf("%d invalid of ",pixels-valid_pixels);
printf("%d pixels ",pixels);
if(ignore_values || reject_outliers) printf("( %d left)",valid_pixels);
printf("\n");
printf("%d overflows, %d underflows\n",overflows,underflows);
printf("max = %g min = %g\n",max,min);
printf("mean = %g rms = %g rmsd = %g skewness = %g kurtosis = %g\n",avg,rms,rmsd,skewness,kurtosis);
if(reject_outliers)
{
median = fmedian_with_rejection(valid_pixels+1,medbuffer,outlier_sigma,&mad,&n_arej);
printf("median = %g mad = %g\n",median,mad);
for(j=1;j<=n_arej;++j)
{
// deviate = sqrt(medbuffer[j]*medbuffer[j]);
// if(deviate>4*mad) printf("GOTHERE: medbuffer[%d]=%g >%g\n",j,medbuffer[j],4*mad);
outfloat = medbuffer[j]-outoffset;
if(j==1) max_arej=min_arej=outfloat;
if(outfloat>max_arej)max_arej=outfloat;
if(outfloat<min_arej)min_arej=outfloat;
}
printf("%d pixels rejected after median-mad filter, new max= %g min= %g\n",valid_pixels-n_arej,max_arej,min_arej);
avg_arej = fmean_with_rejection(n_arej,medbuffer,outlier_sigma,&rmsd_arej,&n_arej);
sumsq_arej=sumd_arej=sumdsq_arej=sumd3_arej=sumd4_arej=0.0;
for(j=1;j<=n_arej;++j)
{
outfloat = medbuffer[j]-outoffset;
if(j==1) max_arej=min_arej=outfloat;
if(outfloat>max_arej)max_arej=outfloat;
if(outfloat<min_arej)min_arej=outfloat;
sumsq_arej += outfloat*outfloat;
diff = outfloat - avg_arej;
sumd_arej += diff;
sumdsq_arej += diff*diff;
sumd3_arej += diff*diff*diff;
sumd4_arej += diff*diff*diff*diff;
}
rms_arej=sqrt(sumsq_arej/n_arej);
rmsd_arej=sqrt(sumdsq_arej/n_arej);
skewness_arej = sumd3_arej/n_arej/(rmsd_arej*rmsd_arej*rmsd_arej);
kurtosis_arej = sumd4_arej/n_arej/(rmsd_arej*rmsd_arej*rmsd_arej*rmsd_arej);
printf("%d pixels rejected after mean-rmsd filter, new max= %g min= %g\n",valid_pixels-n_arej,max_arej,min_arej);
printf("after outlier rejection: mean= %g rms= %g rmsd= %g skewness= %g kurtosis= %g ( %d points)\n",avg_arej,rms_arej,rmsd_arej,skewness_arej,kurtosis_arej,n_arej);
}
printf("CC = %.12g\n",CC);
if(outfile != NULL)
{
printf("writing %s as a %d-byte header with %d %ld-byte floats\n",outfilename,header,pixels,sizeof(float));
fwrite(outimage,n,1,outfile);
fclose(outfile);
}
return 0;
}
/* find the median value of an array of data - scrambles the array */
#define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
float fmedian(unsigned int n, float arr[])
{
unsigned int i,j,k,l,ir,mid;
float a,temp;
l=1;
ir=n;
k=(n+1)/2;
//printf("n=%d; k=%d\n",n,k);
//for(i=1;i<=n;++i) printf("arr[%d]=%f\n",i,arr[i]);
for(;;)
{
if(ir <= l+1)
{
if(ir == l+1 && arr[ir] < arr[l])
{
SWAP(arr[l],arr[ir]);
}
//for(i=1;i<=n;++i) printf("arr[%d]=%f\n",i,arr[i]);
return arr[k];
} else {
mid=(l+ir) >> 1;
SWAP(arr[mid],arr[l+1]);
if(arr[l+1] > arr[ir])
{
SWAP(arr[l+1],arr[ir]);
}
if(arr[l] > arr[ir])
{
SWAP(arr[l],arr[ir]);
}
if(arr[l+1] > arr[l])
{
SWAP(arr[l+1],arr[l]);
}
i=l+1; // initialize pointers for partitioning
j=ir;
a=arr[l]; // partitioning element
for(;;) // innermost loop
{
do i++; while(arr[i]<a); // scan up to find element > a
do j--; while(arr[j]>a); // scan down to find element < a
if( j < i ) break; // pointers crossed, median is in between
SWAP(arr[i],arr[j]);
}
arr[l]=arr[j]; // insert partitioning element
arr[j]=a;
if( j >= k ) ir=j-1; // Keep partition that contains the median active
if( j <= k ) l=i;
}
}
}
float fmedian_with_rejection(unsigned int n, float arr[],float sigma_cutoff, float *final_mad, int *final_n)
{
float median_value;
int i,orig_n,done;
float min_frac,deviate,mad;
orig_n = n;
min_frac = 0.7;
done = 0;
while(! done)
{
/* compute the median (centroid) value */
median_value = fmedian(n,arr);
/* now figure out what the mean absolute deviation from this value is */
mad = fmedian_absolute_deviation(n,arr,median_value);
//if(flag) printf("mad = %f\n",mad);
done = 1;
/* reject all outliers */
for(i=1;i<=n;++i)
{
/* reject positive and negative outliers */
deviate = fabs(arr[i]-median_value);
if(deviate > sigma_cutoff*mad)
{
/* needs to go */
/* move value at the end of the array to this "reject" and then shorten the array */
//if(flag) printf("rejecting arr[%d] = %f (%f)\n",i,arr[i],deviate);
//arr[worst]+=10000;
if(i != n)
{
//temp=arr[worst];
arr[i] = arr[n];
//arr[n]=temp;
}
--n;
done = 0;
}
}
}
/* basically two return values */
*final_mad = mad;
*final_n = n;
return median_value;
}
/* note: there must be 2*n elements in this array! */
float fmedian_absolute_deviation(unsigned int n, float arr[], float median_value)
{
int i;
for(i=1;i<=n;++i)
{
arr[i+n] = fabs(arr[i]-median_value);
}
return fmedian(n,arr+n);
}
/* this function keeps track of outliers by swapping them to the end of the array */
/* counting starts at 0 and "points" is the number of points */
float fmean_with_rejection(unsigned int starting_points, float arr[], float sigma_cutoff, double *final_rmsd, int *final_n)
{
int points,i;
int rejection,worst;
float temp,sum,avg,sumd,rmsd,deviate,worst_deviate;
points=starting_points;
rejection = 1;
while ( rejection && points>starting_points/2.0 )
{
/* find the mean and rms deivation */
sum = sumd = 0.0;
for(i=0;i<points;++i)
{
sum+=arr[i];
}
avg=sum/points;
worst=-1;
worst_deviate=-1.0;
for(i=0;i<points;++i)
{
deviate=fabs(arr[i]-avg);
if(deviate > worst_deviate)
{
worst=i;
worst_deviate=deviate;
}
sumd+=deviate*deviate;
}
rmsd=sqrt(sumd/points);
rejection=0;
if(worst_deviate>sigma_cutoff*rmsd)
{
/* we have a reject! */
rejection=1;
//printf("GOTHERE: rejecting arr[%d] = %f = %f > %f * %f\n",worst,arr[worst],worst_deviate,sigma_cutoff,rmsd);
/* move it to end of the array and forget about it */
SWAP(arr[worst],arr[points]);
--points;
}
}
*final_rmsd = rmsd;
*final_n = points;
return avg;
}