forked from sandywang/classify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voldiff.c
1630 lines (1224 loc) · 47.4 KB
/
voldiff.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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------- MNI Header -----------------------------------
@NAME : voldiff
@INPUT : argc, argv -command line arguments
@OUTPUT :
@RETURNS :
@DESCRIPTION: compares bunch o volumes and produces a confusion matrix
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : June 8, 1995 (Vasco KOLLOKIAN)
@MODIFIED : $Log: voldiff.c,v $
@MODIFIED : Revision 1.1 2011-01-06 19:18:58 jason
@MODIFIED : added voldiff
@MODIFIED :
@MODIFIED : Revision 1.2 2001/07/08 21:03:39 crisco
@MODIFIED : bug fix: under linux (egcs), the mask value test wasn't working if -O was used...; lowered the user_mask_value
@MODIFIED :
@MODIFIED : Revision 1.1 2001/07/08 20:58:19 crisco
@MODIFIED : original (vasco\'s) version...
@MODIFIED :
* Revision 1.16 1996/03/24 03:37:59 vasco
* Fixed the bug with confusion matrix alignment, to include 9 digit numbers.
* reversed -ignore_outvoxel to -outvoxels_die, thus making ignore_outvoxel default.
* Added set_default_max_bytes_in_cache(0), to enable cache of 1 block.
*
* Revision 1.15 1996/01/24 08:04:13 vasco
* removed -exclude from the options list
*
* Revision 1.14 1996/01/24 07:48:57 vasco
* Fixed a bug in calculating kappa_{numer,denom} by casting into (Real).
*
* Revision 1.13 1996/01/24 06:46:18 vasco
* Removed sub_kappa and nozero_kappa, and all exclude related items.
* Added modified version of per class kappa based on Bishop 1975.
* Modified perviously displayed kappa to be renamed Ckappa for collapsed.
* Added Total_Kappa, Brain_Kappa, Paren_Kappa as standard statistics.
* Ultimately Brain_Kappa will be the most significant statistic to denote
* similarity in healthy tissue classification.
*
* Revision 1.12 1995/12/04 18:41:16 vasco
* added Total_kappa, and Xzero_kappa to report to help parsing.
*
* Revision 1.11 1995/09/24 22:22:23 vasco
* added -mask option, to limit comparison to a given masked volume
*
* Revision 1.10 1995/09/24 18:18:59 vasco
* I forgot to check it, thus the modifications I made
*
* Revision 1.9 1995/08/12 22:35:15 vasco
* changed switchs, -matrix_size -> view_class_size
* changed switch , -uppler_class -> c_matrix_size
* separated compare_volume(i) to produce_confusion_matrix(i), and
* calculate_similarity_measures()
* fixed the alignment of vertical labels
* removed -nozeroless, added -exclude <class>
* changed total_voxels to c_matrix[class_total][class_total]
* fixed excluded reporting scheme.
* added kappa, ICC, AZms, and changed hit and miss into specificity
* accuract, sensitivity, and error
* started to work on weighted kappa
*
* Revision 1.8 1995/07/23 19:46:58 vasco
* Also reports total hit and miss ratio without including class 0 in the
* calculation. And provided a -noexcluded switch to turn off
* reporting of excluded calculation.
*
* Revision 1.7 1995/07/17 01:26:54 vasco
* fixed a bug in calculating the percent hit ratio / class
* total_voxels per class, should have been that of target and not source.
* Also added %hit and %miss ratio of the whole volume.
*
* Revision 1.6 1995/06/15 06:33:14 vasco
* give %hit and miss ratio per class, and combine stdout and file routines
*
* Revision 1.5 1995/06/07 05:20:25 vasco
* a complete rewrite with parseargv,
* loading of gold volume once, option of stdout and file
* option of user specifying upper_class
*
---------------------------------------------------------------------------- */
#include <ParseArgv.h>
#include <volume_io.h>
/* number of stats to report */
#define STATNUM 7
/* index of each stat */
#define CKAPPA 0 /* collapsed kappa */
#define AZSM 1
#define SENSITIVITY 2
#define ERROR 3
#define SPECIFICITY 4
#define ACCURACY 5
#define PCLASS_KAPPA 6 /* kappa per class */
#define DEFCMATSIZE 4 /* default confusion matrix size */
#define DEFVIEWSIZE 4 /* default confusion matrix view size */
/* Function declarations and prototyping */
void parse_arguments(int argc, char *argv[]);
void load_first_input_volume(void);
void load_input_volume(int volume_index);
void produce_confusion_matrix(int volume_index);
void produce_confusion_matrix_in_world_coor(int volume_index);
void set_confusion_matrix(void);
void calculate_similarity_measures(void);
void calculate_overall_measures(void);
void write_report(int volume_index);
void load_mask_volume(char *mask_file);
int voxel_is_in_volume( int index, Real vox1, Real vox2, Real vox3);
void create_voxel_to_voxel_transform(Volume volume1, Volume volume2,
General_transform *v1_to_v2 );
/* Global variables */
Status status; /* status of loading and saving */
char *pname; /* the name of the invoked program */
int verbose = FALSE; /* inform user as things are processed */
int clobber = FALSE; /* overwrite existing report file */
int exclude_class = 0; /* exclude class (X) in total hit and miss ratios */
int debug = FALSE; /* print debugging information */
int stest = 0; /* test stats on internal data */
Volume *in_volume; /* pointer to array of volumes */
Volume mask_volume; /* volume data to store the mask */
int **in_volume_sizes; /* 2D array to hold sizes */
int num_volumes; /* the number of volumes to compare */
char **input_filename; /* 1D char array to hold volume filenames*/
char *output_filename; /* confusion report filename */
int view_class_size=DEFVIEWSIZE; /* the size of the window to on CM */
Real total_hit_ratio,
excluded_total_hit_ratio, /* ratio without class zero */
excluded_total_miss_ratio,
total_miss_ratio, /* total hit and miss ratio */
total_kappa, /* a measure of similarity ala Cohen */
brain_kappa, /* same measure for brain only */
paren_kappa, /* same measure for parenchyma (gry & wht) */
sig_pclass_kappa, /* same measure as above - Bishop */
total_azsm; /* a measure of similarity ala Zijdenbos */
int c_matrix_size = DEFCMATSIZE; /* the size of the confusion matrix */
int class_total; /* more discriptive index for marginals totals */
/* this is the same as c_matrix_size, init. later */
long **c_matrix; /* confusion matrix */
int r,c; /* row and column counters */
Real **c_table; /* confusion table to report hit-miss/ class */
long total_voxels_per_class,
total_hits,
missed_voxels_per_class;
FILE *matrep; /* FILE variable of output_filename */
char *mask_filename; /* filename of the mask volume */
Real user_mask_value = 0.99; /* default mask value to consider */
int world_coor = FALSE; /* flag for world coordinate traversal */
int outvoxels_die = FALSE; /* die if voxels fall outside volume 2 */
int cache_set = FALSE; /* set the caching option to false */
int block_sizes[3] = {1,-1,-1}; /* default block size, (1 slice) */
ArgvInfo argTable[] = {
{NULL, ARGV_HELP, NULL, NULL,
" "},
{"-debug", ARGV_CONSTANT, (char *) TRUE, (char *) &debug,
"show debugging information."},
{"-stest", ARGV_INT, (char *) NULL, (char *) &stest,
"test stats on different internal data sets."},
{"-clobber", ARGV_CONSTANT, (char *) TRUE, (char *) &clobber,
"Overwrite confusion report file."},
{"-verbose", ARGV_CONSTANT, (char *) TRUE, (char *) &verbose,
"Show progress"},
{"-view_class_size", ARGV_INT, (char *) NULL, (char *) &view_class_size,
"Specify the viewing size of the confusion matrix."},
{"-report", ARGV_STRING, (char *) NULL, (char *) &output_filename,
"Write statistics to specified file"},
{"-c_matrix_size", ARGV_INT, (char *) NULL, (char *) &c_matrix_size,
"Specify the actual size of the confusion matrix."},
{"-mask", ARGV_STRING, (char *) NULL, (char *) &mask_filename,
"Specify volume which will be used as a mask"},
{"-user_mask_value", ARGV_FLOAT, (char *) NULL, (char *) &user_mask_value,
"Specify the mask value, in the mask volume to be used"},
{"-world", ARGV_CONSTANT, (char *) TRUE, (char *) &world_coor,
"Traverse the first volume in world coordinate space."},
{"-outvoxels_die", ARGV_CONSTANT, (char *) TRUE, (char *) &outvoxels_die,
"If voxels in first volume fall outside the second volume - abort."},
{NULL, ARGV_HELP, NULL, NULL,
"\nSpecify volume caching options.\n" },
{"-cached", ARGV_CONSTANT, (char *) TRUE, (char *) &cache_set,
"Specify that volume caching be used."},
{"-block_sizes", ARGV_INT, (char *) 3, (char *) &block_sizes,
"Set the size of the cache block for the volumes."},
{NULL, ARGV_END, NULL, NULL, NULL}
};
/* Main program starts here */
int main(int argc, char *argv[])
{
int vol_idx;
parse_arguments(argc, argv);
/* if you are testing measture of silumarity on dummy internal data */
if ( stest ) {
set_confusion_matrix();
calculate_similarity_measures();
calculate_overall_measures();
write_report(vol_idx);
exit(EXIT_SUCCESS);
}
/* set specific caching options */
if ( cache_set ) {
/* enable volume caching by setting threshold to 0 */
set_n_bytes_cache_threshold(0);
/* set max cache size to 0 to include only 1 block*/
set_default_max_bytes_in_cache(0);
/* randomize caching to access for 1 slice at a time - user selected */
set_default_cache_block_sizes(block_sizes);
}
else {
/* disable caching all together*/
set_n_bytes_cache_threshold(-1);
}
load_first_input_volume();
/* if a mask has been specified, load it in - invalid for world coordinates */
if ( mask_filename && !world_coor)
load_mask_volume( mask_filename);
for_less ( vol_idx, 1, num_volumes) {
load_input_volume(vol_idx);
/* if world coordinates have been specified, traverse in world */
if ( world_coor)
produce_confusion_matrix_in_world_coor(vol_idx);
else
produce_confusion_matrix(vol_idx);
calculate_similarity_measures();
calculate_overall_measures();
write_report(vol_idx);
delete_volume(in_volume[vol_idx]);
}
exit(EXIT_SUCCESS);
} /* main */
/* ----------------------------- MNI Header -----------------------------------
@NAME : parse_arguments
@INPUT : argc, argv - command line arguments
@OUTPUT :
@RETURNS :
@DESCRIPTION: parses command line arguments
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : February 6, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void parse_arguments(int argc, char *argv[])
{
int i;
pname = argv[0];
/* Call ParseArgv - at least two volume has to be specified */
if (ParseArgv(&argc, argv, argTable, 0) || ( argc < 2 ) ) {
if ( !stest && ( argc < 3 ) ) {
(void) fprintf(stderr,
"\nUsage: %s <options> <infile1> <infile2> [infile2] ...\n", pname);
(void) fprintf(stderr,
" %s [-help]\n\n", pname);
exit(EXIT_FAILURE);
}
}
if ( stest ) {
}
else {
num_volumes = argc - 1; /* count out progname */
ALLOC( input_filename, num_volumes);
/* make sure that the typed filenames exist */
for_less(i, 0, num_volumes ) {
if (!file_exists(argv[i+1])) {
(void) fprintf(stderr, "filename `%s' not found. \n", argv[i+1]);
exit(EXIT_FAILURE);
}
/* get the filename of the volume */
input_filename[i] = argv[i+1];
if (debug)
(void) fprintf(stderr, "input filename = `%s' \n", input_filename[i]);
}
/* if a report file is specified and exists, warn about clobbering */
if (!clobber && output_filename && file_exists(output_filename)) {
(void) fprintf(stderr,"File `%s' exists ! \n", output_filename);
(void) fprintf(stderr,"Use -clobber to overwrite file.\n");
exit(EXIT_FAILURE);
}
/* make sure view_class size is <= c_matrix_size */
if ( view_class_size > c_matrix_size ) {
fprintf(stderr, "view_class_size should be <= %d\n", c_matrix_size);
exit(EXIT_FAILURE);
}
/* make sure excluded class label is < c_matrix_size */
if ( exclude_class >= c_matrix_size ) {
fprintf(stderr, "excluded class label should be < %d\n", c_matrix_size);
exit(EXIT_FAILURE);
}
/* open the report file */
if ( output_filename) {
matrep = fopen(output_filename, "w");
if ( matrep == NULL) {
fprintf(stderr, "Cannot open report file %s\n", output_filename);
exit(EXIT_FAILURE);
}
}
/* prevent use of mask while travering in world coordinate space,
this is a temp measure not to deal with issues of world coor masks */
if (mask_filename && world_coor ) {
fprintf(stderr, "-mask and -world are mutually exclusive \n");
exit(EXIT_FAILURE);
}
/* allocate area for filenames and derivatives */
ALLOC2D( in_volume_sizes, num_volumes, MAX_DIMENSIONS);
ALLOC( in_volume, num_volumes);
} /* if (test) */
/* allocate area for confusion matrix */
ALLOC2D( c_matrix, c_matrix_size+1, c_matrix_size+1); /* 1 extra for totals */
/* confusion table has STATNUM cols to report the following statistics,
kappa, azsm(Alex's), sensit., specif., accur. error, pclass_kappa */
ALLOC2D( c_table, c_matrix_size, STATNUM);
/* to refer to class totals in the confusion matrix, it is more informative to
refer to row-wise class total as c_matrix[<class>][class_total], than as
c_matrix[<class>][c_matrix_size], and for columns as well, functionally
they are the same */
class_total = c_matrix_size ;
} /* parse_arguments */
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_input_volume
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION:
@METHOD :
@GLOBALS : in_volume[j] - the number of input volumes
in_volume_sizes[MAX_VOLUMES][MAX_DIMESIONS] - sizes of volumes
@CALLS :
@CREATED :
@MODIFIED :
---------------------------------------------------------------------------- */
void load_input_volume(int volume_index)
{
if (verbose)
fprintf (stderr, "Loading volume %s\n", input_filename[volume_index]);
/* load the volume */
status = input_volume(input_filename[volume_index],
3,
NULL,
NC_BYTE,
FALSE,
0.0, 0.0,
TRUE,
&in_volume[volume_index],
(minc_input_options *) NULL ) ;
if ( status != OK )
exit(EXIT_FAILURE);
/* get the volume sizes */
get_volume_sizes(in_volume[volume_index], in_volume_sizes[volume_index]);
/* check to see if volumes are of same size in each dim, if not in world*/
if ( !world_coor ) {
if (in_volume_sizes[volume_index][X] != in_volume_sizes[0][X]) {
(void) fprintf(stderr,"Error - Volume size mismatch in X dimension\n");
exit(EXIT_FAILURE);
}
if (in_volume_sizes[volume_index][Y] != in_volume_sizes[0][Y]) {
(void) fprintf(stderr,"Error - Volume size mismatch in Y dimension\n");
exit(EXIT_FAILURE);
}
if (in_volume_sizes[volume_index][Z] != in_volume_sizes[0][Z]) {
(void) fprintf(stderr,"Error - Volume size mismatch in Z dimension\n");
exit(EXIT_FAILURE);
}
} /* if ( !world_coor ) */
} /* load_input_volume */
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_first_input_volume
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION:
@METHOD :
@GLOBALS : load the first volume - the gold standard
@CALLS :
@CREATED : June 6, 1995
@MODIFIED :
---------------------------------------------------------------------------- */
void load_first_input_volume(void)
{
if (verbose)
fprintf (stderr, "Loading first volume %s\n", input_filename[0]);
if ( debug && cache_set )
fprintf (stderr, "Caching has been specified\n");
/* load the volume */
status = input_volume(input_filename[0],
3,
NULL,
NC_BYTE,
FALSE,
0.0, 0.0,
TRUE,
&in_volume[0],
(minc_input_options *) NULL ) ;
if ( status != OK )
exit(EXIT_FAILURE);
/* get the volume sizes */
get_volume_sizes(in_volume[0], in_volume_sizes[0]);
} /* load_first_input_volume */
/* ----------------------------- MNI Header -----------------------------------
@NAME : produce_confusion_matrix(int volume_index)
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: produces a confusion matrix for a give volume (volume_index)
with the first volume.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : Aug. 6, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void produce_confusion_matrix(int volume_index)
{
int v1, v2, v3; /* voxel indeces */
Real image_value1, image_value2; /* values to be compared */
Real mask_value; /* value of the mask voxel */
/* in the next two loops, and then in subsequent ones in this programm,
for_less (r, 0, c_matrix_size ) === for ( r = 0; r < c_matrix_size; r++)
ie. if c_matrix_size = 8, r ranges in [0..7]
don't get confused that matrix totals are being included also */
/* initialize the confusion matrix, including marginal totals (+1) */
for_less(r, 0, c_matrix_size+1)
for_less(c, 0, c_matrix_size+1)
c_matrix[r][c] = 0;
/* initialize the confusion table */
for_less(r, 0, c_matrix_size)
for_less(c, 0, STATNUM)
c_table[r][c] = 0.0;
/* start traversing each of volume1, volume2 */
if (verbose)
fprintf(stderr, "Traversing volumes %s %s \n",
input_filename[0], input_filename[volume_index]);
for ( v1 = 0; v1 < in_volume_sizes[volume_index][0]; v1++) {
for ( v2 = 0; v2 < in_volume_sizes[volume_index][1]; v2++) {
for ( v3 = 0; v3 < in_volume_sizes[volume_index][2]; v3++) {
/* if a mask is chosen, ( by having a non-NULL mask_filename )
and mask value is > user_mask_value, consider only that
voxel, in the confusion matrix. */
if ( mask_filename ) {
/* make sure the map is previously loaded, and has the same size */
GET_VALUE_3D( mask_value, mask_volume, v1, v2, v3);
if ( mask_value < user_mask_value ) {
if( 0 && debug)
fprintf( stdout, "mask_value= %f < %f\n", mask_value, user_mask_value);
goto NEXT_VOXEL;
}
/* this might be a future consideration spot for
applying mask to only volume1, or volume2, separatly
if ( mask_value < user_mask_value ) {
switch ( option ) {
case 1:
goto NEXT_VOXEL;
case 2:
do something
break;
case 3:
do something
break;
}
}
*/
} /* if (mask_filename) */
GET_VALUE_3D(image_value1, in_volume[0], v1, v2, v3);
GET_VALUE_3D(image_value2, in_volume[volume_index], v1, v2, v3);
if (image_value1 >= c_matrix_size) {
fprintf(stderr, "%s may not be a classified volume\n", input_filename[0]);
fprintf(stderr, "Label value = %d, increase c_matrix_size\n",
(int)ROUND(image_value1) );
exit(EXIT_FAILURE);
}
if (image_value2 >= c_matrix_size) {
fprintf(stderr, "%s may not be a classified volume\n",
input_filename[volume_index]);
fprintf(stderr, "Label value = %d, increase c_matrix_size\n",
(int)ROUND(image_value2) );
exit(EXIT_FAILURE);
}
c_matrix[ROUND(image_value1)][ROUND(image_value2)]++;
NEXT_VOXEL: ;
}
}
}
if (verbose)
fprintf(stderr, "Producing confusion Matrix...\n\n");
/* calclate the total number of voxels for each class row-wise */
for_less(r, 0, c_matrix_size)
for_less(c, 0, c_matrix_size)
c_matrix[r][class_total] += c_matrix[r][c];
/* calclate the total number of voxels for each class column-wise */
for_less(c, 0, c_matrix_size)
for_less(r, 0, c_matrix_size)
c_matrix[class_total][c] += c_matrix[r][c];
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : produce_confusion_matrix_in_world(int volume_index)
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: produces a confusion matrix for a give volume (volume_index)
with the first volume, while the first volume is being traversed
in world coordinate space. This is good to compare a high-res
(0.5mm) phantom, with lower res phantoms, to assess the effect
of partial volme.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : Mar 16, 1996 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void produce_confusion_matrix_in_world_coor(int volume_index)
{
int v1, v2, v3; /* voxel indeces */
Real image_value1, image_value2; /* values to be compared */
Real wx, wy, wz; /* world coordinates */
Real invol_v1, invol_v2, invol_v3; /* voxel coordinates of in_volume[idx] */
General_transform trans; /* trans from vol1 voxel to vol2 voxel */
/* in the next two loops, and then in subsequent ones in this programm,
for_less (r, 0, c_matrix_size ) === for ( r = 0; r < c_matrix_size; r++)
ie. if c_matrix_size = 8, r ranges in [0..7]
don't get confused that matrix totals are being included also */
/* initialize the confusion matrix, including marginal totals (+1) */
for_less(r, 0, c_matrix_size+1)
for_less(c, 0, c_matrix_size+1)
c_matrix[r][c] = 0;
/* initialize the confusion table */
for_less(r, 0, c_matrix_size)
for_less(c, 0, STATNUM)
c_table[r][c] = 0.0;
/* start traversing each of volume1, volume2 */
if (verbose)
fprintf(stderr, "Traversing volumes %s (in voxel) %s (in world)\n",
input_filename[0], input_filename[volume_index]);
/* compose the voxel to voxel transform */
create_voxel_to_voxel_transform(in_volume[0], in_volume[volume_index], &trans);
for ( v1 = 0; v1 < in_volume_sizes[0][0]; v1++) {
for ( v2 = 0; v2 < in_volume_sizes[0][1]; v2++) {
for ( v3 = 0; v3 < in_volume_sizes[0][2]; v3++) {
GET_VALUE_3D(image_value1, in_volume[0], v1, v2, v3);
/* voxel coor of in_volume[0] is checked against the world,
coordinate of the in_volume[voxel_idx] at the very same location */
/* the following method is a slow way of doing things, a better faster
method was suggested by David MacDonald, that concatanates transforms.
This is left here just for comparative purposed
convert_3D_voxel_to_world(in_volume[0],
(Real)v1,
(Real)v2,
(Real)v3,
&wx,
&wy,
&wz);
convert_3D_world_to_voxel(in_volume[volume_index],
wx,
wy,
wz,
&invol_v1,
&invol_v2,
&invol_v3);
*/
general_transform_point( &trans, (Real) v1, (Real) v2, (Real) v3,
&invol_v1, &invol_v2, &invol_v3 );
if ( voxel_is_in_volume( volume_index, invol_v1, invol_v2, invol_v3) ) {
image_value2 = get_volume_real_value(in_volume[volume_index],
ROUND(invol_v1),
ROUND(invol_v2),
ROUND(invol_v3),
0, 0);
}
else if ( outvoxels_die ) {
fprintf( stderr, "Voxels fall outside volume. Aborted... \n");
exit(EXIT_FAILURE);
}
else {
goto NEXT_VOXEL;
}
if (image_value1 >= c_matrix_size) {
fprintf(stderr, "%s may not be a classified volume\n", input_filename[0]);
fprintf(stderr, "Label value = %d, increase c_matrix_size\n",
(int)ROUND(image_value1) );
exit(EXIT_FAILURE);
}
if (image_value2 >= c_matrix_size) {
fprintf(stderr, "%s may not be a classified volume\n",
input_filename[volume_index]);
fprintf(stderr, "Label value = %d, increase c_matrix_size\n",
(int)ROUND(image_value2) );
exit(EXIT_FAILURE);
}
c_matrix[ROUND(image_value1)][ROUND(image_value2)]++;
NEXT_VOXEL: ;
}
}
}
if (verbose)
fprintf(stderr, "Producing confusion Matrix...\n\n");
/* calclate the total number of voxels for each class row-wise */
for_less(r, 0, c_matrix_size)
for_less(c, 0, c_matrix_size)
c_matrix[r][class_total] += c_matrix[r][c];
/* calclate the total number of voxels for each class column-wise */
for_less(c, 0, c_matrix_size)
for_less(r, 0, c_matrix_size)
c_matrix[class_total][c] += c_matrix[r][c];
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : set_confusion_matrix(void)
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: produces a dummy confusion matrix to test stats
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : Aug. 15, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void set_confusion_matrix(void)
{
/* in the next two loops, and then in subsequent ones in this programm,
for_less (r, 0, c_matrix_size ) === for ( r = 0; r < c_matrix_size; r++)
ie. if c_matrix_size = 8, r ranges in [0..7]
don't get confused that matrix totals are being included also */
/* initialize the confusion matrix, including marginal totals (+1) */
for_less(r, 0, c_matrix_size+1)
for_less(c, 0, c_matrix_size+1)
c_matrix[r][c] = 0;
/* initialize the confusion table */
for_less(r, 0, c_matrix_size)
for_less(c, 0, STATNUM)
c_table[r][c] = 0.0;
/* start setting custom c_matrix values here */
switch (stest) {
case 1 :
/* set 1 - perfect agreement */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 100;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 2:
/* set 2 */
c_matrix[0][0] = 5970;
c_matrix[0][1] = 30;
c_matrix[1][1] = 100;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 3:
/* set 3 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 100;
c_matrix[2][1] = 30;
c_matrix[2][2] = 170;
c_matrix[3][3] = 300;
break;
case 4:
/* set 4 */
c_matrix[0][0] = 6000;
c_matrix[1][0] = 30;
c_matrix[1][1] = 70;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 5:
/* set 5 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 70;
c_matrix[1][2] = 30;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 6:
/* set 6 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 70;
c_matrix[1][3] = 30;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 7:
/* set 7 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 70;
c_matrix[1][2] = 15;
c_matrix[1][3] = 15;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 8:
/* set 8 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 70;
c_matrix[1][2] = 15;
c_matrix[2][1] = 15;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 9:
/* set 9 */
c_matrix[0][0] = 6000;
c_matrix[1][0] = 15;
c_matrix[1][1] = 70;
c_matrix[2][1] = 15;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 10:
/* set 10 */
c_matrix[0][0] = 6000;
c_matrix[0][1] = 15;
c_matrix[1][0] = 15;
c_matrix[1][1] = 70;
c_matrix[2][2] = 200;
c_matrix[3][3] = 300;
break;
case 11:
/* set 11 */
c_matrix[0][0] = 6000;
c_matrix[1][2] = 30;
c_matrix[1][1] = 70;
c_matrix[2][2] = 170;
c_matrix[2][1] = 30;
c_matrix[3][1] = 30;
c_matrix[3][3] = 270;
break;
case 12:
/* set 12 */
c_matrix[0][0] = 6000;
c_matrix[1][1] = 70;
c_matrix[1][2] = 15;
c_matrix[1][3] = 15;
c_matrix[2][1] = 15;
c_matrix[2][2] = 170;
c_matrix[2][3] = 15;
c_matrix[3][1] = 15;
c_matrix[3][2] = 15;
c_matrix[3][3] = 270;
break;
case 13:
/* set 13 */
c_matrix[0][0] = 6000;
c_matrix[0][1] = 0;
c_matrix[0][2] = 0;
c_matrix[0][3] = 0;
c_matrix[1][0] = 30;
c_matrix[1][1] = 70;
c_matrix[2][0] = 30;
c_matrix[2][2] = 170;
c_matrix[3][0] = 30;
c_matrix[3][3] = 270;
break;
case 14:
/* set 14 */
c_matrix[0][0] = 5870;
c_matrix[0][1] = 55;
c_matrix[0][2] = 35;
c_matrix[0][3] = 40;
c_matrix[1][0] = 30;
c_matrix[1][1] = 70;
c_matrix[2][0] = 30;
c_matrix[2][2] = 170;
c_matrix[3][0] = 30;
c_matrix[3][3] = 270;
break;
case 15:
/* set 15 */
c_matrix[0][0] = 6000;
c_matrix[0][1] = 0;
c_matrix[0][2] = 0;
c_matrix[0][3] = 0;
c_matrix[1][0] = 0;
c_matrix[1][1] = 100;
c_matrix[2][0] = 0;