forked from sandywang/classify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classify.cc
1930 lines (1428 loc) · 55 KB
/
classify.cc
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
/*--------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1997, Vasken Kollokian,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
----------------------------------------------------------------------------
$RCSfile: classify.cc,v $
$Revision: 1.8 $
$Author: claude $
$Date: 2011-05-27 20:47:01 $
$State: Exp $
--------------------------------------------------------------------------*/
/* ----------------------------- MNI Header -----------------------------------
@NAME : classify
@INPUT : argc, argv -command line arguments
@OUTPUT :
@RETURNS : error status
@DESCRIPTION: calls a classifier routine, feeds input data, stores result
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 8, 1995 (Vasco KOLLOKIAN)
@MODIFIED : $Log: classify.cc,v $
@MODIFIED : Revision 1.8 2011-05-27 20:47:01 claude
@MODIFIED : mostly fixes for memory bugs
@MODIFIED :
@MODIFIED : Revision 1.7 2006-09-26 18:42:31 claude
@MODIFIED : switched initialization of fuzzy volume after cache_set stuff, otherwise minc will not write fuzzy volume - strange
@MODIFIED :
@MODIFIED : Revision 1.6 2006/09/14 19:47:25 claude
@MODIFIED : initialize fuzzy volume to zero
@MODIFIED :
@MODIFIED : Revision 1.4 2006/09/14 19:04:23 claude
@MODIFIED : initialize fuzzy volume to zero
@MODIFIED :
@MODIFIED : Revision 1.3 2005/02/11 20:16:15 bert
@MODIFIED : Minor changes, primarily for compilation issues
@MODIFIED :
@MODIFIED : Revision 1.2 2002/03/20 22:25:06 jason
@MODIFIED : added a sadly forgotten configure.ac and took out one debugging line
@MODIFIED :
@MODIFIED : Revision 1.1.1.1 2002/03/20 22:16:34 jason
@MODIFIED : first autoconfiscated version that compiles under linux gcc 3
@MODIFIED :
@MODIFIED : Revision 1.9 1999/01/14 19:32:50 alex
@MODIFIED : Added a few printf statements
@MODIFIED :
@MODIFIED : Revision 1.8 1998/02/26 19:52:12 alex
@MODIFIED : C++-ified things
@MODIFIED :
@MODIFIED : Revision 1.7 1997/12/16 18:33:27 alex
@MODIFIED : Added -output_range option
@MODIFIED :
@MODIFIED : Revision 1.6 1997/12/14 16:02:15 alex
@MODIFIED : Fixed small bug in '-fuzzy all' option
@MODIFIED :
@MODIFIED : Revision 1.5 1997/12/13 20:26:04 alex
@MODIFIED : Added '-fuzzy all' option
@MODIFIED :
@MODIFIED : Revision 1.4 1997/09/11 14:26:05 alex
@MODIFIED : Various changes directed at g++ compatibility
@MODIFIED :
@MODIFIED : Revision 1.3 1997/02/14 21:07:22 alex
@MODIFIED : Modified ann training, added -clobber switch
@MODIFIED :
@MODIFIED : Revision 1.2 1997/02/11 05:48:31 alex
@MODIFIED : Changed history from overwriting to appending to the history of the first input file
@MODIFIED :
@MODIFIED : Revision 1.1.1.1 1997/02/11 00:06:41 alex
@MODIFIED : Sources for classify, copied from Vasken Kollokian
@MODIFIED :
* Revision 1.26 1996/08/25 05:20:49 vasco
* Added support for Bayes classification.
* Added support for apriori probabilities to be fed to any classifier
* Fixed bug, so that fuzzy volume clobbering is checked before classifications starts
* Made cache the default behavior by adding -nocache switch
*
* Revision 1.25 1996/03/14 06:30:02 vasco
* eliminated some unused variables.
*
* Revision 1.24 1996/03/14 05:45:50 vasco
* fixed a bug in cleanup_memory, and re-commented it out.
*
* Revision 1.23 1996/03/06 00:25:41 vasco
* The is another major revamp. I added Fuzzy C Means, and I modularized fcm min and hcm
*
* Revision 1.22 1996/01/25 06:10:03 vasco
* fixed max_class_index bug in -load_tr option. and commented out
* cleanup_memory()
*
* Revision 1.21 1995/12/07 15:48:19 vasco
* Added Hard C Means classification, by supplying load_tag_volume
* function, also added supervised flag to distinguish between the two
* type of classifiers, supervised and unsupervised.
* Replaced volume and image min and max by max_class_label.
*
* Revision 1.20 1995/11/17 07:50:54 vasco
* fixed a bug in -dump, and added -fpath, a fuzzy pathname for fuzzvols.
*
* Revision 1.19 1995/11/06 06:39:41 vasco
* Added memory cleanup routines, and chaged some volio calls.
*
* Revision 1.18 1995/10/27 05:34:39 vasco
* added volume caching options, and set class_names to NULL
*
* Revision 1.17 1995/10/26 06:28:36 vasco
* added -fuzzy "01001" string to select which class is expected
* to write a fuzzy classification. This gives a finer control
* over what class results oin fuzzy classification. This is
* quite useful in MS since they require the fuzziness of only
* one 'lesion' class.
*
* Revision 1.16 1995/10/23 02:03:46 vasco
* Prints "*" while classifying each slice.
*
* Revision 1.15 1995/10/09 00:45:11 vasco
* added an option to user specified voxel_min and voxel_man, image_min, image_max
* fuzzy_voxel_min, fuzzy_voxel_max, fuzzy_image_min, fuzzy_image_max.
* fixed the -help crashing. added some newlines for more readability, and added punctuations.
*
* Revision 1.14 1995/10/03 03:03:05 vasco
* added -dump_features flag, and changed some stderr to stdout.
*
* Revision 1.13 1995/09/24 17:31:26 vasco
* Fixed a bug in counting the correct number of remaining tags. Once
* the tags that fell outside the volume were ignored. Also fixed a bug
* (rather volio modification) of accessing volume size by volume->size.
*
* Revision 1.12 1995/09/20 06:07:11 vasco
* I added class_names array that houses the class names, in the order they
* appear in the tag file.
*
* Revision 1.11 1995/09/15 20:46:51 vasco
* Added -mask -user_mask_class -user_mask_value, so that masks could be supported
*
* Revision 1.10 1995/09/15 08:16:34 vasco
* added -fuzzy option with all of its intricacies and dependencies.
* these include, writing of different data types in the fuzzy volumes
* availability of fuzziness of implemented classifiers
* creation, initialization and writing of fuzzy volumes.
* Also, isolated in set_classifier_functions(), all function initializations
* moved data allocation just before load_training to avoid duplication of code.
*
* Revision 1.9 1995/09/14 03:24:37 vasco
* removed test_classifier option, and converted include files to <> form.
*
* Revision 1.8 1995/08/28 05:11:48 vasco
* first version of including c4.5
*
* Revision 1.7 1995/08/28 03:25:51 vasco
* converted to g++, and added class_names
*
* Revision 1.6 1995/07/12 05:12:23 vasco
* added -parameter switch to load custom classifier parameters
*
* Revision 1.5 1995/07/12 03:49:36 vasco
* added command history saving feature.
*
* Revision 1.4 1995/07/12 03:21:32 vasco
* added training tag inside volume checks
*
* Revision 1.3 1995/06/10 17:59:50 vasco
* Radical changes
*
* Revision 1.2 1995/05/31 01:29:22 vasco
* added some comments and potential location of initialization routine.
*
* Revision 1.1 1995/05/31 00:58:46 vasco
* Initial revision
*
---------------------------------------------------------------------------- */
#include "config.h"
#include <iostream> // (bert) added to force -lCio?
using namespace std; // (bert) added
extern "C" {
#include <volume_io.h>
#include "time_stamp.h"
#include <ParseArgv.h> // (bert) moved inside extern "C"
}
#include <unistd.h>
#include "class_protos.h" // (bert) use quotes
#include "classify.h" // (bert) use quotes
#include "mindist/mindist.h" // (bert) use quotes
#include "knn/knn.h" // (bert) use quotes
#include "ann/ann.h" // (bert) use quotes
//extern "C" {
//#include <c4_5.h>
//}
#include "hcm/hcm.h" // (bert) use quotes
#include "fcm/fcm.h" // (bert) use quotes
#include "bayes/bayes.h" // (bert) use quotes
/* MAIN */
int main(int argc, char *argv[])
{
parse_arguments(argc, argv);
allocate_memory();
/* set the generic classifier functions */
set_classifier_functions( classifier );
/* the three following verification are to be called after
set_classifier_functions() that is why they do not reside in
parse_argv */
if ( apriori && !use_apriori ) {
(void) fprintf(stderr,"Chosen classifier doesn't support -apriori.\n");
exit(EXIT_FAILURE);
}
if ( supervised && trainvol_filename && !apriori) {
(void) fprintf(stderr,"Supervised classifiers don't accept tag volumes without -apriori switch.\n");
exit(EXIT_FAILURE);
}
if ( !supervised && tagfile_filename ) {
(void) fprintf(stderr,"Unsupervised classifiers don't accept tag files.\n");
exit(EXIT_FAILURE);
}
/* check for fuzziness, ie if a classifier supports a fuzzy option */
if ( fuzzy && !fuzzy_available) {
fprintf(stderr, "The specified classifier does not support a fuzzy option\n");
exit(EXIT_FAILURE);
}
/* set specific caching options */
if ( cache_set && supervised && !apriori ) {
/* enable volume caching by setting threshold to 0 */
set_n_bytes_cache_threshold(0);
/* set max cache size, originally 0 to include only 1 block*/
set_default_max_bytes_in_cache(max_cache_size);
/* randomize caching to access for 1 voxel at a time which is good
for reading tag files throughout the volumes - no user choice */
block_sizes[0] = 1;
block_sizes[1] = 1;
block_sizes[2] = 1;
set_default_cache_block_sizes(block_sizes);
}
else if ( cache_set && ( apriori || !supervised)) {
/* if unsupervised classifier used, or apriori , enable slice caching */
/* enable volume caching by setting threshold to 0 */
set_n_bytes_cache_threshold(0);
/* set max cache size, originally 0 to include only 1 block*/
set_default_max_bytes_in_cache(max_cache_size);
block_sizes[0] = user_block_sizes[0];
block_sizes[1] = user_block_sizes[1];
block_sizes[2] = user_block_sizes[2];
/* And set slice caching for volumes to be created */
set_default_cache_block_sizes(block_sizes);
}
else {
/* disable caching all together*/
set_n_bytes_cache_threshold(-1);
}
load_input_volumes();
/* train supervised classifiers here */
if ( supervised && tagfile_filename) {
load_tag_file( tagfile_filename );
create_feature_matrix_from_tagfile();
init_training(param_filename);
/* load the apriori volumes (with the training volume mechanism)
since it is exactly as loading FCM training volumes */
if ( apriori ) {
load_train_volumes(trainvol_filename);
/* make sure number of classes is the same number of apriori volumes */
if ( num_classes != num_train_vols ) {
fprintf( stderr, "Number of classes <> # of apriori volumes\n");
exit(EXIT_FAILURE);
}
/* allocate some memory for apriori vector - for apriori classifier */
ALLOC(apriori_vector, num_classes);
}
if ( classifier != KNN )
/* knn has to train in each sample, so it is placed on it own */
train();
}
/* train unsupervised classifiers here */
if ( !supervised && trainvol_filename) {
load_train_volumes(trainvol_filename);
init_training(param_filename);
train();
}
/* load previously trained classifier here - if proper switch is supplied */
if ( load_train_filename) {
init_training(param_filename);
load_training(load_train_filename);
max_class_index = num_classes;
}
/* save trained classifier here - if proper switch is supplied */
if ( save_train_filename )
save_training(save_train_filename);
/* if not training, create, initialize, load mask, classify and write */
if ( !train_only ) {
if ( cache_set ) /* set slice caching options */
convert_features_to_slice_caching();
if ( fuzzy ) {
decide_fuzzy_volumes();
}
create_empty_classified_volume();
if ( mask_filename )
load_mask_volume( mask_filename);
classify_volume();
write_classified_volume();
}
if ( fuzzy || classifier == FCM)
write_fuzzy_volumes();
/* this is for testing memory leaks, and may not be necessary to call */
cleanup_memory();
return(EXIT_SUCCESS);
} /* main */
/* ----------------------------- MNI Header -----------------------------------
@NAME : parse_arguments
@INPUT : argc, argv - command line arguments
@OUTPUT :
@RETURNS :
@DESCRIPTION: parses command line arguments
@METHOD :
@GLOBALS : pname - program name
verbose - show progress
clobber - overwrite file
debug - show debug info
sub_command - subclassifer arguments
type - type to read
signtype - sign to read
num_features - number of input volumes
output_filename- the name of the classified volume
@CALLS :
@CREATED : February 6, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void parse_arguments(int argc, char *argv[])
{
int i, dst_idx = 0, src_idx = 0, buff_idx = 0;
/* buffer to hold fuzzy_training volume names, limit 10, kludge! */
char fuzzy_train_buffer[10][256];
pname = argv[0];
/* form the history string - before parseargv is called */
history = time_stamp(argc, argv);
/* Call ParseArgv */
if ( !ParseArgv(&argc, argv, argTable, 0) ) { /* switches supplied are OK */
if ( ( train_only || dump_features ) && (argc < 2) ) {
/* at least one volume has to be specified : output volume is optional
if the -train_only, or -dump_features switches are specified */
(void) fprintf(stderr, "-train, -dump: specify at least one input volume.\n");
(void) fprintf(stderr, " %s [-help]\n\n", pname);
exit(EXIT_FAILURE);
}
if ( !(train_only || dump_features) && (argc < 3) ) {
/* at least two volumes have to be specified - source and destination */
(void) fprintf(stderr,
"\nUsage: %s <options> <infile1> [infile2] ... <outfile>\n", pname);
(void) fprintf(stderr, " %s [-help]\n\n", pname);
exit(EXIT_FAILURE);
}
} /* ParseArgv */
/* determine the number of volumes - ie features given to classify */
if ( !( train_only || dump_features) )
num_features = argc - 2; /* count out progname and outvol */
else
num_features = argc - 1; /* -train, -dump: count out only progname - no output*/
/* Set clobber flags */
if (clobber_all)
clobber = clobber_training = clobber_fuzzy = TRUE;
/* this is a kludge, to prevent -help from core dumping I guess it
is a shortcoming of parseargv or my ignorance, rather impatient
to deal with this problem. I guess the way ParseArgv is structured */
if ( num_features == 0 )
exit(EXIT_SUCCESS);
/* reserve array for input volume filenames */
ALLOC(input_filename, num_features);
/* make sure that the typed filenames exist */
for_less(i, 0, num_features ) {
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 >= 1)
(void) fprintf(stdout, "input_filename[%d] = `%s'\n", i, input_filename[i]);
} /* for_less */
if ( !(train_only || dump_features) )
output_filename = argv[num_features + 1];
if ( debug >= 1 )
(void) fprintf(stdout, "output_filename = `%s' \n", output_filename);
if (!clobber && !train_only && !dump_features && file_exists(output_filename)) {
(void) fprintf(stderr,"File `%s' exists ! \n", output_filename);
(void) fprintf(stderr,"Use -clob_out to overwrite output classified volume.\n");
exit(EXIT_FAILURE);
}
/* if a mask is specified, make sure it exists */
if (mask_filename && !file_exists(mask_filename)) {
(void) fprintf(stderr,"File `%s' doesn't exists ! \n", mask_filename);
exit(EXIT_FAILURE);
}
if ( train_only && save_train_filename == NULL ) {
(void) fprintf(stderr,"-train_only : please specify -save_train <file>\n");
exit(EXIT_FAILURE);
}
if ( train_only && fuzzy ) {
(void) fprintf(stderr,"-train_only and -fuzzy are mutually exclusive.\n");
exit(EXIT_FAILURE);
}
if ( dump_features && fuzzy ) {
(void) fprintf(stderr,"-dump_features and -fuzzy are mutually exclusive.\n");
exit(EXIT_FAILURE);
}
/* for load_train_filename, checking perhaps should be done by the
actual routine that is doing the loading, in that case it could
take default values in case a filename is not provided */
if ( load_train_filename && !file_exists(load_train_filename) ) {
(void) fprintf(stderr,"File `%s' doesn't exist !\n ", load_train_filename);
exit(EXIT_FAILURE);
}
if (!clobber_training &&
save_train_filename &&
file_exists(save_train_filename)) {
(void) fprintf(stderr,"File `%s' exists !\n", save_train_filename);
(void) fprintf(stderr,"Use -clob_tr to overwrite output training file.\n");
exit(EXIT_FAILURE);
}
if (!tagfile_filename &&
!load_train_filename &&
!tagvolume_buffer && (argc > 2 ) ) {
/* argc > 2 is used to supress the message only if -help is used */
(void) fprintf(stderr,"Specify one of -tagfile or -load_train.\n");
exit(EXIT_FAILURE);
}
if ( apriori && !tagvolume_buffer ) {
(void) fprintf(stderr,"Specify apriori volumes with -volume\n");
exit(EXIT_FAILURE);
}
/* make sure the specified tag file exists */
if (tagfile_filename != NULL && !file_exists(tagfile_filename) ) {
(void) fprintf(stderr,"File `%s' doesn't exist !\n ", tagfile_filename);
exit(EXIT_FAILURE);
}
if (tagfile_filename != NULL &&
load_train_filename != NULL ) {
(void) fprintf(stderr,"-tagfile, -load_train are mutually exlusive.\n");
exit(EXIT_FAILURE);
}
/* parse the argument supplied by the -volume switch, which is a
comma separated collection of training volume
filenames, num_train_vols is determined by the number of training
volumes supplied, */
if ( tagvolume_buffer) {
if ( debug > 4 )
fprintf( stdout, "tagvolume_buffer = %s\n", tagvolume_buffer);
while ( 1 ) {
/* while buf is not a ',' or null, advance pointer and copy character */
while ( tagvolume_buffer[src_idx] != ',' &&
tagvolume_buffer[src_idx] != '\0') {
fuzzy_train_buffer[buff_idx][dst_idx++] = tagvolume_buffer[src_idx++];
}
/* as soon as you encounter a ',' or null, end target string */
fuzzy_train_buffer[buff_idx][dst_idx] = '\0';
/* reset target string pointer */
dst_idx = 0;
/* advance to next storage slot */
buff_idx++;
/* increment the number of training volumes */
num_train_vols++;
/* if you reach the end of the string, break and carry on */
if (tagvolume_buffer[src_idx] == '\0')
break;
else
src_idx++;
}
/* allocate space for the filename array */
ALLOC( trainvol_filename, num_train_vols);
/* allocate space, and copy buffer content */
for_less( i, 0, num_train_vols ) {
ALLOC( trainvol_filename[i], strlen(fuzzy_train_buffer[i])+1 );
strcpy( trainvol_filename[i], fuzzy_train_buffer[i] );
if ( debug > 4 )
fprintf( stdout, "%s\n", trainvol_filename[i]);
if ( !file_exists(trainvol_filename[i]) ) {
fprintf(stderr,"File `%s' doesn't exist !\n ", trainvol_filename[i]);
exit(EXIT_FAILURE);
}
}
/* allocate memory for training volume pointers */
ALLOC( train_volume, num_train_vols);
} /* if ( trainvol_buffer ) */
} /* parse_arguments */
/* ----------------------------- MNI Header -----------------------------------
@NAME : allocate_memory
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: allocates some memory for data structures
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : June 10, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void allocate_memory(void)
{
/* allocate some memory for feature vector */
ALLOC(feature_vector, num_features);
/* allocate memory for first volume sizes */
ALLOC(first_volume_sizes, MAX_DIMENSIONS);
/* allocate memory for volume pointers */
ALLOC( in_volume, num_features);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_input_volumes
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION:
@METHOD :
@GLOBALS : in_volume[j] - the number of input volumes
@CALLS :
@CREATED :
@MODIFIED : Feb 10, 1996 ( Vasco KOLLOKIAN)
---------------------------------------------------------------------------- */
void load_input_volumes(void)
{
int j;
/* read the volumes in one by one */
for_less( j, 0, num_features ) {
if (verbose)
fprintf (stdout, "Loading volume %s\n", input_filename[j]);
/* load the volume */
status = input_volume(input_filename[j],
3,
NULL, /* File_order_dimension_names, */
type,
sign,
0.0, 0.0,
TRUE,
&in_volume[j],
(minc_input_options *) NULL ) ;
if ( status != OK )
exit(EXIT_FAILURE);
/* if loading the very first volume, get its sizes, number of dims and dim
names, dim starts and dim steps */
if ( j == 0 ) {
int k; /* local counter */
get_volume_sizes(in_volume[0], first_volume_sizes);
first_volume_num_dims = get_volume_n_dimensions(in_volume[0]);
first_volume_dim_names = get_volume_dimension_names(in_volume[0]);
if ( debug > 2 ) {
fprintf(stdout, "Vol number of dims. = %d\n", first_volume_num_dims);
fprintf(stdout, "Vol dimension names = ");
for_less ( k, 0, first_volume_num_dims )
fprintf(stdout, "%s ", first_volume_dim_names[k]);
fprintf(stdout, "\n");
}
}
/* if you have more than one volume, check to see if volumes are
of same size in each dim and make sure the dimension orders are
also the same. this is done by volume_size_is_ok() function */
if ( j >= 1 && !volume_size_is_ok( in_volume[j] )){
(void) fprintf(stderr,"in volume %s\n", input_filename[j]);
exit(EXIT_FAILURE);
}
} /* for_less j */
} /* load_input_volumes */
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_mask_volume
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION: loads the mask in form of minc file
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : Sep. 15, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void load_mask_volume(char *mask_file)
{
if (verbose)
fprintf (stdout, "Loading mask volume %s\n", mask_file);
/* load the volume */
status = input_volume(mask_file,
3,
NULL, /* File_order_dimension_names, */
type,
sign,
0.0, 0.0,
TRUE,
&mask_volume,
(minc_input_options *) NULL ) ;
if ( status != OK )
exit(EXIT_FAILURE);
if ( !volume_size_is_ok( mask_volume) ) {
fprintf( stderr, "in mask volume %s\n", mask_file);
exit(EXIT_FAILURE);
}
} /* load_mask_volume */
/* ----------------------------- MNI Header -----------------------------------
@NAME : create_empty_classified_volume
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIN: cre and initialized an empty classified volume
@METHOD :
@GLOBAL : cssified_volume
@CALLS :
@CREATED : February 6, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
-------------------------------------------------------------------------- */
void create_empty_classified_volume(void)
{
Real minval = (output_range[0] == -MAXDOUBLE) ? 0 : output_range[0];
Real maxval = (output_range[1] == -MAXDOUBLE) ? max_class_index : output_range[1];
/* create the classification volume here */
if (verbose) {
write(2,"Creating output volume\n",23);
}
classified_volume = copy_volume_definition(in_volume[0],
NC_BYTE,
FALSE,
minval, maxval);
set_volume_voxel_range(classified_volume, minval, maxval);
set_volume_real_range(classified_volume, minval, maxval);
if ( cache_set ) {
set_cache_output_volume_parameters(classified_volume,
output_filename,
NC_BYTE,
FALSE,
minval, maxval,
input_filename[0],
history,
(minc_output_options *) NULL ) ;
}
} /* create_empty_classified_volume */
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_tag_file
@INPUT : name of tag file
@OUTPUT :
@RETURNS : number of tag points read
@DESCRIPTION: opens and loads a tag file
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 29, 1995 ( Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void load_tag_file ( char *tag_filename )
{
if (verbose)
(void) fprintf(stdout, "Loading tagfile %s\n", tagfile_filename);
/* tag file should be opened here */
if ( input_tag_file(tag_filename, &n_tag_volumes, &num_samples,
&tags, NULL, NULL, NULL, NULL, &labels ) != OK ) {
printf("Error reading the tag file.\n");
exit(EXIT_FAILURE);
}
if ( n_tag_volumes == 2 )
printf("Tag file contains two volumes, using the first one.\n");
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : load_train_volumes
@INPUT :
@OUTPUT :
@RETURNS :
@DESCRIPTION:
@METHOD :
@GLOBALS :
@CALLS :
@CREATED :
@MODIFIED : Feb 11, 1996 ( Vasco KOLLOKIAN )
---------------------------------------------------------------------------- */
void load_train_volumes(char **trainvol_filename)
{
int i;
/* load the training volumes */
for_less( i, 0, num_train_vols ) {
if (verbose)
fprintf (stdout, "Loading tag volume %s\n", trainvol_filename[i]);
status = input_volume(trainvol_filename[i],
3,
NULL, /* File_order_dimension_names, */
NC_BYTE,
FALSE,
0.0, 0.0,
TRUE,
&train_volume[i],
(minc_input_options *) NULL ) ;
if ( status != OK )
exit(EXIT_FAILURE);
if ( !volume_size_is_ok( train_volume[i] ) ) {
fprintf( stderr, "in training volume %s\n", trainvol_filename);
exit(EXIT_FAILURE);
}
} /* for_less( i, 0, num_train_vols ) */
} /* load_train_volume */
/* ----------------------------- MNI Header -----------------------------------
@NAME : void create_feature_matrix_from_tagfile
@INPUT :
@OUTPUT :
@RETURNS : create a feature matrix and a class vector for each point
@DESCRIPTION:
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 29, 1995 (Vasco KOLLOKIAN)
@MODIFIED :
---------------------------------------------------------------------------- */
void create_feature_matrix_from_tagfile(void)
{
int i, j; /* counters i, over samples, j over volumes */
Real wx, wy, wz; /* world x y z coordinates */
Real v1, v2, v3; /* voxel x y z coordinates */
Real value; /* voxel value to go into feature matrix */
int num_adj_samples; /* var to hold the number of adjusted samples */
Real *class_counter; /* int array to count the number of classes in samples */
/* set adjusted samples to zero, it will also index into feature_matrix
and class column.*/
num_adj_samples = 0;
if (verbose)
(void) fprintf(stdout, "Creating feature matrix from tagfile\n");
/* allocate memory for feature matrix space, as big as your samples size */
ALLOC2D( feature_matrix, num_samples, num_features);
/* allocate momory for the class column, also as big as your samples size*/
ALLOC( class_column, num_samples );
/* initialize the feature_matrix */
for_less( j, 0, num_features)
for_less( i, 0, num_samples)
feature_matrix[i][j] = 0.0;
/* start traversing the tag file */
for_less( i, 0, num_samples) {
/* get the world coordinate from the tag file */
wx = tags[i][0];
wy = tags[i][1];
wz = tags[i][2];
/* convert world into voxel coordinates using first volume */
convert_3D_world_to_voxel(in_volume[0], wx, wy, wz, &v1, &v2, &v3);
/* NOTE: check to see if the training point (voxel coordinate)
falls in the volume. Since all the volumes have the same sizes,
take volume 0, (that is why 'voxel_is_in_volume( v1, v2, v3)'. If
later a -world switch is supplied, each tag should be tested in
each feature volume. At this point, check only for volume 0 */
/* if voxel is in the volume, get the feature volumes values */
if ( voxel_is_in_volume( v1, v2, v3)) {
for_less( j, 0, num_features) {
GET_VALUE_3D(value, in_volume[j], ROUND(v1), ROUND(v2), ROUND(v3));
feature_matrix[num_adj_samples][j] = value;
}