00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 #define PROGRAM_NAME    "3dANOVA3"                   
00051 #define PROGRAM_AUTHOR  "B. Douglas Ward"                  
00052 #define PROGRAM_INITIAL "29 Jan 1997"     
00053 #define PROGRAM_LATEST  "19 Jul 2004"     
00054 
00055 
00056 
00057 #define SUFFIX ".3danova3"                
00058 
00059 #include "3dANOVA.h"
00060 #include "3dANOVA.lib"
00061 
00062 
00063 
00064 
00065 
00066 
00067 
00068 void display_help_menu()
00069 {
00070   printf 
00071     (
00072      "This program performs three-factor ANOVA on 3D data sets.           \n\n"
00073      "Usage: \n"
00074      "3dANOVA3 \n"
00075      "-type  k          type of ANOVA model to be used:                     \n"
00076      "                         k = 1   A,B,C fixed;          AxBxC          \n"
00077      "                         k = 2   A,B,C random;         AxBxC          \n"
00078      "                         k = 3   A fixed; B,C random;  AxBxC          \n"
00079      "                         k = 4   A,B fixed; C random;  AxBxC          \n"
00080      "                         k = 5   A,B fixed; C random;  AxB,BxC,C(A)   \n"
00081      "                                                                      \n"
00082      "-alevels a                     a = number of levels of factor A       \n"
00083      "-blevels b                     b = number of levels of factor B       \n"
00084      "-clevels c                     c = number of levels of factor C       \n"
00085      "-dset 1 1 1 filename           data set for level 1 of factor A       \n"
00086      "                                        and level 1 of factor B       \n"
00087      "                                        and level 1 of factor C       \n"
00088      " . . .                           . . .                                \n"
00089      "                                                                      \n"
00090      "-dset i j k filename           data set for level i of factor A       \n"
00091      "                                        and level j of factor B       \n"
00092      "                                        and level k of factor C       \n"
00093      " . . .                           . . .                                \n"
00094      "                                                                      \n"
00095      "-dset a b c filename           data set for level a of factor A       \n"
00096      "                                        and level b of factor B       \n"
00097      "                                        and level c of factor C       \n"
00098      "                                                                      \n"
00099      "[-voxel num]                   screen output for voxel # num          \n"
00100      "[-diskspace]                   print out disk space required for      \n"
00101      "                                  program execution                   \n"
00102      "                                                                      \n"
00103      "                                                                      \n"
00104      "The following commands generate individual AFNI 2 sub-brick datasets: \n"
00105      "  (In each case, output is written to the file with the specified     \n"
00106      "   prefix file name.)                                                 \n"
00107      "                                                                      \n"
00108      "[-fa prefix]                F-statistic for factor A effect           \n"
00109      "[-fb prefix]                F-statistic for factor B effect           \n"
00110      "[-fc prefix]                F-statistic for factor C effect           \n"
00111      "[-fab prefix]               F-statistic for A*B interaction           \n"
00112      "[-fac prefix]               F-statistic for A*C interaction           \n"
00113      "[-fbc prefix]               F-statistic for B*C interaction           \n"
00114      "[-fabc prefix]              F-statistic for A*B*C interaction         \n"
00115      "                                                                      \n"
00116      "[-amean i prefix]           estimate of factor A level i mean         \n"
00117      "[-bmean i prefix]           estimate of factor B level i mean         \n"
00118      "[-cmean i prefix]           estimate of factor C level i mean         \n"
00119      "[-xmean i j k prefix]       estimate mean of cell at factor A level i,\n"
00120      "                               factor B level j, factor C level k     \n"
00121      "                                                                      \n"
00122      "[-adiff i j prefix]         difference between factor A levels i and j\n"
00123      "[-bdiff i j prefix]         difference between factor B levels i and j\n"
00124      "[-cdiff i j prefix]         difference between factor C levels i and j\n"
00125      "[-xdiff i j k l m n prefix] difference between cell mean at A=i,B=j,  \n"
00126      "                               C=k, and cell mean at A=l,B=m,C=n      \n"
00127      "                                                                      \n"
00128      "[-acontr c1...ca prefix]    contrast in factor A levels               \n"
00129      "[-bcontr c1...cb prefix]    contrast in factor B levels               \n"
00130      "[-ccontr c1...cc prefix]    contrast in factor C levels               \n"
00131      "                                                                      \n"
00132      "                                                                      \n"
00133      "The following command generates one AFNI 'bucket' type dataset:       \n"
00134      "                                                                      \n"
00135      "[-bucket prefix]         create one AFNI 'bucket' dataset whose       \n"
00136      "                           sub-bricks are obtained by concatenating   \n"
00137      "                           the above output files; the output 'bucket'\n"
00138      "                           is written to file with prefix file name   \n"
00139      "\n");
00140 
00141   printf
00142     (
00143      "\n"
00144      "N.B.: For this program, the user must specify 1 and only 1 sub-brick  \n"
00145      "      with each -dset command. That is, if an input dataset contains  \n"
00146      "      more than 1 sub-brick, a sub-brick selector must be used, e.g.: \n"
00147      "      -dset 2 4 5 'fred+orig[3]'                                      \n"
00148      );
00149           
00150   printf("\n" MASTER_SHORTHELP_STRING ) ;
00151   
00152   exit(0);
00153 }
00154 
00155 
00156 #define N_INDEX(i,j,k) n[(k) + MAX_LEVELS * ((j) + MAX_LEVELS * (i))]
00157 
00158 
00159 
00160 
00161 
00162 
00163 void get_options (int argc, char ** argv, anova_options * option_data)
00164 {
00165   int nopt = 1;                  
00166   int ival, jval, kval;          
00167   int i, j, k;                            
00168   int nijk;                           
00169   float fval;                    
00170   THD_3dim_dataset * dset=NULL;             
00171   char message[MAX_NAME_LENGTH];            
00172   
00173   int * n;                       
00174 
00175     
00176   
00177   if (argc < 2 || strncmp(argv[1], "-help", 5) == 0)  display_help_menu();  
00178   
00179   
00180   AFNI_logger (PROGRAM_NAME,argc,argv); 
00181 
00182   
00183   initialize_options (option_data);
00184   
00185   
00186   n = (int *)calloc(MAX_LEVELS*MAX_LEVELS*MAX_LEVELS, sizeof(int));
00187   if ( !n )
00188   {
00189     sprintf(message, "failed to allocate %u bytes for file counters\n",
00190             (unsigned int)(MAX_LEVELS*MAX_LEVELS*MAX_LEVELS * sizeof(int)) );
00191     ANOVA_error(message);
00192   }
00193 
00194 #if 0  
00195   for (i = 0;  i < MAX_LEVELS;  i++)
00196     for (j = 0;  j < MAX_LEVELS;  j++)
00197       for (k = 0;  k < MAX_LEVELS;  k++)
00198         n[i][j][k] = 0;
00199 #endif
00200   
00201 
00202   
00203   while (nopt < argc )
00204     {
00205 
00206       
00207       if ((option_data->xname == NULL) && (option_data->a > 0) &&
00208           (option_data->b > 0) && (option_data->c > 0))
00209         {
00210           option_data->xname = 
00211             (char *****) malloc (sizeof(char ****) * option_data->a);
00212           for (i = 0;  i < option_data->a;  i++)
00213             {
00214               option_data->xname[i] =
00215                 (char ****) malloc (sizeof(char ***) * option_data->b);
00216               for (j = 0;  j < option_data->b;  j++)
00217                 {
00218                   option_data->xname[i][j] =
00219                     (char ***) malloc (sizeof(char **) * option_data->c);
00220                   for (k = 0;  k < option_data->c;  k++)
00221                     {
00222                       option_data->xname[i][j][k] =
00223                         (char **) malloc (sizeof(char *) * MAX_OBSERVATIONS);
00224                     }
00225                 }
00226             }
00227         }
00228           
00229 
00230       
00231       if( strncmp(argv[nopt],"-diskspace",5) == 0 )
00232         {
00233           option_data->diskspace = 1;
00234           nopt++ ; continue ;  
00235         }
00236 
00237       
00238       
00239       if( strncmp(argv[nopt],"-datum",5) == 0 ){
00240         if( ++nopt >= argc ) ANOVA_error("need an argument after -datum!") ;
00241         
00242         if( strcmp(argv[nopt],"short") == 0 ){
00243           option_data->datum = MRI_short ;
00244         } else if( strcmp(argv[nopt],"float") == 0 ){
00245           option_data->datum = MRI_float ;
00246         } else {
00247           char buf[256] ;
00248           sprintf(buf,"-datum of type '%s' is not supported in 3dANOVA3!",
00249                   argv[nopt] ) ;
00250           ANOVA_error(buf) ;
00251         }
00252         nopt++ ; continue ;  
00253       }
00254       
00255       
00256       
00257       if( strncmp(argv[nopt],"-session",5) == 0 ){
00258         nopt++ ;
00259         if( nopt >= argc ) ANOVA_error("need argument after -session!") ;
00260         strcpy(option_data->session , argv[nopt++]) ;
00261         continue ;
00262       }
00263       
00264 
00265       
00266       if (strncmp(argv[nopt], "-voxel", 6) == 0)
00267         {
00268           nopt++;
00269           if (nopt >= argc)  ANOVA_error ("need argument after -voxel ");
00270           sscanf (argv[nopt], "%d", &ival);
00271           if (ival <= 0)
00272             ANOVA_error ("illegal argument after -voxel ");
00273           option_data->nvoxel = ival;
00274           nopt++;
00275           continue;
00276         }
00277 
00278 
00279       
00280       if (strncmp(argv[nopt], "-type", 5) == 0)
00281       {
00282          nopt++;
00283          if (nopt >= argc)  ANOVA_error ("need argument after -type ");
00284          sscanf (argv[nopt], "%d", &ival);
00285          if ((ival < 1) || (ival > 5))
00286             ANOVA_error ("illegal argument after -type ");
00287          option_data->model = ival;
00288          nopt++;
00289          continue;
00290       }
00291       
00292       
00293       
00294       if (strncmp(argv[nopt], "-alevels", 5) == 0)
00295         {
00296           nopt++;
00297           if (nopt >= argc)  ANOVA_error ("need argument after -alevels ");
00298           sscanf (argv[nopt], "%d", &ival);
00299           if ((ival <= 0) || (ival > MAX_LEVELS))
00300             ANOVA_error ("illegal argument after -alevels ");
00301           option_data->a = ival;
00302           nopt++;
00303           continue;
00304         }
00305 
00306       
00307       
00308       if (strncmp(argv[nopt], "-blevels", 5) == 0)
00309         {
00310           nopt++;
00311           if (nopt >= argc)  ANOVA_error ("need argument after -blevels ");
00312           sscanf (argv[nopt], "%d", &ival);
00313           if ((ival <= 0) || (ival > MAX_LEVELS))
00314             ANOVA_error ("illegal argument after -blevels ");
00315           option_data->b = ival;
00316           nopt++;
00317           continue;
00318         }
00319       
00320       
00321       
00322       if (strncmp(argv[nopt], "-clevels", 5) == 0)
00323         {
00324           nopt++;
00325           if (nopt >= argc)  ANOVA_error ("need argument after -clevels ");
00326           sscanf (argv[nopt], "%d", &ival);
00327           if ((ival <= 0) || (ival > MAX_LEVELS))
00328             ANOVA_error ("illegal argument after -clevels ");
00329           option_data->c = ival;
00330           nopt++;
00331           continue;
00332         }
00333       
00334       
00335       
00336       if (strncmp(argv[nopt], "-dset", 5) == 0)
00337         {
00338           nopt++;
00339           if (nopt+3 >= argc)  ANOVA_error ("need 4 arguments after -dset ");
00340           sscanf (argv[nopt], "%d", &ival);
00341           if ((ival <= 0) || (ival > option_data->a))
00342             ANOVA_error ("illegal argument after -dset ");
00343           
00344           nopt++;
00345           sscanf (argv[nopt], "%d", &jval);
00346           if ((jval <= 0) || (jval > option_data->b))
00347             ANOVA_error ("illegal argument after -dset ");
00348           
00349           nopt++;
00350           sscanf (argv[nopt], "%d", &kval);
00351           if ((kval <= 0) || (kval > option_data->c))
00352             ANOVA_error ("illegal argument after -dset ");
00353 
00354           N_INDEX(ival-1, jval-1, kval-1) += 1;
00355           nijk = N_INDEX(ival-1, jval-1, kval-1);
00356           if (nijk > MAX_OBSERVATIONS)
00357             ANOVA_error ("too many data files");
00358           
00359           
00360           nopt++;
00361           dset = THD_open_dataset( argv[nopt] ) ;
00362           if( ! ISVALID_3DIM_DATASET(dset) )
00363             {
00364               sprintf(message,"Unable to open dataset file %s\n", 
00365                       argv[nopt]);
00366               ANOVA_error (message);
00367             }
00368 
00369           
00370           if (DSET_NVALS(dset) != 1)
00371             {
00372              sprintf(message,"Must specify exactly 1 sub-brick for file %s\n",
00373                      argv[nopt]);
00374              ANOVA_error (message);
00375             }
00376 
00377           THD_delete_3dim_dataset( dset , False ) ; dset = NULL ;
00378           
00379           option_data->xname[ival-1][jval-1][kval-1][nijk-1] 
00380             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00381           strcpy (option_data->xname[ival-1][jval-1][kval-1][nijk-1], 
00382                   argv[nopt]);
00383           nopt++;
00384           continue;
00385         }
00386       
00387       
00388       
00389       if (strncmp(argv[nopt], "-fa", 5) == 0)
00390         {
00391           nopt++;
00392           if (nopt >= argc)  ANOVA_error ("need argument after -fa ");
00393           option_data->nfa = 1;
00394           option_data->faname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00395           strcpy (option_data->faname, argv[nopt]);
00396           nopt++;
00397           continue;
00398         }
00399 
00400 
00401       
00402       if (strncmp(argv[nopt], "-fb", 5) == 0)
00403         {
00404           nopt++;
00405           if (nopt >= argc)  ANOVA_error ("need argument after -fb ");
00406           option_data->nfb = 1;
00407           option_data->fbname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00408           strcpy (option_data->fbname, argv[nopt]);
00409           nopt++;
00410           continue;
00411         }
00412       
00413       
00414       
00415       if (strncmp(argv[nopt], "-fc", 5) == 0)
00416         {
00417           nopt++;
00418           if (nopt >= argc)  ANOVA_error ("need argument after -fc ");
00419           option_data->nfc = 1;
00420           option_data->fcname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00421           strcpy (option_data->fcname, argv[nopt]);
00422           nopt++;
00423           continue;
00424         }
00425       
00426 
00427       
00428       if (strncmp(argv[nopt], "-fab", 5) == 0)
00429         {
00430           nopt++;
00431           if (nopt >= argc)  ANOVA_error ("need argument after -fab ");
00432           option_data->nfab = 1;
00433           option_data->fabname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00434           strcpy (option_data->fabname, argv[nopt]);
00435           nopt++;
00436          continue;
00437         }
00438 
00439 
00440       
00441       if (strncmp(argv[nopt], "-fac", 5) == 0)
00442         {
00443           nopt++;
00444           if (nopt >= argc)  ANOVA_error ("need argument after -fac ");
00445           option_data->nfac = 1;
00446           option_data->facname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00447           strcpy (option_data->facname, argv[nopt]);
00448           nopt++;
00449          continue;
00450         }
00451 
00452       
00453       
00454       if (strncmp(argv[nopt], "-fbc", 5) == 0)
00455         {
00456           nopt++;
00457           if (nopt >= argc)  ANOVA_error ("need argument after -fbc ");
00458           option_data->nfbc = 1;
00459           option_data->fbcname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00460           strcpy (option_data->fbcname, argv[nopt]);
00461           nopt++;
00462           continue;
00463         }
00464 
00465 
00466       
00467       if (strncmp(argv[nopt], "-fabc", 5) == 0)
00468         {
00469           nopt++;
00470           if (nopt >= argc)  ANOVA_error ("need argument after -fabc ");
00471           option_data->nfabc = 1;
00472           option_data->fabcname = malloc (sizeof(char) * MAX_NAME_LENGTH);
00473           strcpy (option_data->fabcname, argv[nopt]);
00474           nopt++;
00475           continue;
00476         }
00477 
00478 
00479       
00480       if (strncmp(argv[nopt], "-amean", 5) == 0)
00481         {
00482           nopt++;
00483           if (nopt+1 >= argc)  ANOVA_error ("need 2 arguments after -amean ");
00484           
00485           option_data->num_ameans++;
00486           if (option_data->num_ameans > MAX_MEANS)
00487             ANOVA_error ("too many factor A level mean estimates");
00488           
00489           sscanf (argv[nopt], "%d", &ival);
00490           if ((ival <= 0) || (ival > option_data->a))
00491             ANOVA_error ("illegal argument after -amean ");
00492           option_data->ameans[option_data->num_ameans-1] = ival - 1;
00493           nopt++;
00494           
00495           option_data->amname[option_data->num_ameans-1] 
00496             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00497           strcpy (option_data->amname[option_data->num_ameans-1], argv[nopt]);
00498           nopt++;
00499           continue;
00500         }
00501 
00502 
00503       
00504       if (strncmp(argv[nopt], "-bmean", 5) == 0)
00505         {
00506           nopt++;
00507           if (nopt+1 >= argc)  ANOVA_error ("need 2 arguments after -bmean ");
00508           
00509           option_data->num_bmeans++;
00510           if (option_data->num_bmeans > MAX_MEANS)
00511             ANOVA_error ("too many factor B level mean estimates");
00512           
00513           sscanf (argv[nopt], "%d", &ival);
00514           if ((ival <= 0) || (ival > option_data->b))
00515             ANOVA_error ("illegal argument after -bmean ");
00516           option_data->bmeans[option_data->num_bmeans-1] = ival - 1;
00517           nopt++;
00518           
00519           option_data->bmname[option_data->num_bmeans-1] 
00520             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00521           strcpy (option_data->bmname[option_data->num_bmeans-1], argv[nopt]);
00522           nopt++;
00523           continue;
00524         }
00525 
00526 
00527       
00528       if (strncmp(argv[nopt], "-cmean", 5) == 0)
00529         {
00530           nopt++;
00531           if (nopt+1 >= argc)  ANOVA_error ("need 2 arguments after -cmean ");
00532           
00533           option_data->num_cmeans++;
00534           if (option_data->num_cmeans > MAX_MEANS)
00535             ANOVA_error ("too many factor C level mean estimates");
00536           
00537           sscanf (argv[nopt], "%d", &ival);
00538           if ((ival <= 0) || (ival > option_data->c))
00539             ANOVA_error ("illegal argument after -cmean ");
00540           option_data->cmeans[option_data->num_cmeans-1] = ival - 1;
00541           nopt++;
00542           
00543           option_data->cmname[option_data->num_cmeans-1] 
00544             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00545           strcpy (option_data->cmname[option_data->num_cmeans-1], argv[nopt]);
00546           nopt++;
00547           continue;
00548         }
00549 
00550 
00551       
00552       if (strncmp(argv[nopt], "-xmean", 5) == 0)
00553         {
00554           nopt++;
00555           if (nopt+3 >= argc)  ANOVA_error ("need 4 arguments after -xmean ");
00556           
00557           option_data->num_xmeans++;
00558           if (option_data->num_xmeans > MAX_MEANS)
00559             ANOVA_error ("too many cell mean estimates");
00560           
00561           sscanf (argv[nopt], "%d", &ival);
00562           if ((ival <= 0) || (ival > option_data->a))
00563             ANOVA_error ("illegal argument after -xmean ");
00564           option_data->xmeans[option_data->num_xmeans-1][0] = ival - 1;
00565           nopt++;
00566           
00567           sscanf (argv[nopt], "%d", &ival);
00568           if ((ival <= 0) || (ival > option_data->b))
00569             ANOVA_error ("illegal argument after -xmean ");
00570           option_data->xmeans[option_data->num_xmeans-1][1] = ival - 1;
00571           nopt++;
00572           
00573           sscanf (argv[nopt], "%d", &ival);
00574           if ((ival <= 0) || (ival > option_data->c))
00575             ANOVA_error ("illegal argument after -xmean ");
00576           option_data->xmeans[option_data->num_xmeans-1][2] = ival - 1;
00577           nopt++;
00578           
00579           option_data->xmname[option_data->num_xmeans-1] 
00580             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00581           strcpy (option_data->xmname[option_data->num_xmeans-1], argv[nopt]);
00582           nopt++;
00583           continue;
00584         }
00585 
00586 
00587       
00588       if (strncmp(argv[nopt], "-adiff", 5) == 0)
00589         {
00590           nopt++;
00591           if (nopt+2 >= argc)  ANOVA_error ("need 3 arguments after -adiff ");
00592           
00593           option_data->num_adiffs++;
00594           if (option_data->num_adiffs > MAX_DIFFS)
00595             ANOVA_error ("too many factor A level differences");
00596           
00597           sscanf (argv[nopt], "%d", &ival);
00598           if ((ival <= 0) || (ival > option_data->a))
00599             ANOVA_error ("illegal argument after -adiff ");
00600           option_data->adiffs[option_data->num_adiffs-1][0] = ival - 1;
00601           nopt++;
00602           
00603           sscanf (argv[nopt], "%d", &ival);
00604           if ((ival <= 0) || (ival > option_data->a))
00605             ANOVA_error ("illegal argument after -adiff ");
00606           option_data->adiffs[option_data->num_adiffs-1][1] = ival - 1;
00607           nopt++;
00608           
00609           option_data->adname[option_data->num_adiffs-1] 
00610             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00611           strcpy (option_data->adname[option_data->num_adiffs-1], argv[nopt]);
00612           nopt++;
00613           continue;
00614         }
00615       
00616 
00617       
00618       if (strncmp(argv[nopt], "-bdiff", 5) == 0)
00619         {
00620           nopt++;
00621           if (nopt+2 >= argc)  ANOVA_error ("need 3 arguments after -bdiff ");
00622           
00623           option_data->num_bdiffs++;
00624           if (option_data->num_bdiffs > MAX_DIFFS)
00625             ANOVA_error ("too many factor B level differences");
00626 
00627           sscanf (argv[nopt], "%d", &ival);
00628           if ((ival <= 0) || (ival > option_data->b))
00629             ANOVA_error ("illegal argument after -bdiff ");
00630           option_data->bdiffs[option_data->num_bdiffs-1][0] = ival - 1;
00631           nopt++;
00632    
00633           sscanf (argv[nopt], "%d", &ival);
00634           if ((ival <= 0) || (ival > option_data->b))
00635             ANOVA_error ("illegal argument after -bdiff ");
00636           option_data->bdiffs[option_data->num_bdiffs-1][1] = ival - 1;
00637           nopt++;
00638 
00639           option_data->bdname[option_data->num_bdiffs-1] 
00640             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00641           strcpy (option_data->bdname[option_data->num_bdiffs-1], argv[nopt]);
00642           nopt++;
00643           continue;
00644         }
00645       
00646       
00647       
00648       if (strncmp(argv[nopt], "-cdiff", 5) == 0)
00649         {
00650           nopt++;
00651           if (nopt+2 >= argc)  ANOVA_error ("need 3 arguments after -cdiff ");
00652           
00653           option_data->num_cdiffs++;
00654           if (option_data->num_cdiffs > MAX_DIFFS)
00655             ANOVA_error ("too many factor C level differences");
00656 
00657           sscanf (argv[nopt], "%d", &ival);
00658           if ((ival <= 0) || (ival > option_data->c))
00659             ANOVA_error ("illegal argument after -cdiff ");
00660           option_data->cdiffs[option_data->num_cdiffs-1][0] = ival - 1;
00661           nopt++;
00662    
00663           sscanf (argv[nopt], "%d", &ival);
00664           if ((ival <= 0) || (ival > option_data->c))
00665             ANOVA_error ("illegal argument after -cdiff ");
00666           option_data->cdiffs[option_data->num_cdiffs-1][1] = ival - 1;
00667           nopt++;
00668 
00669           option_data->cdname[option_data->num_cdiffs-1] 
00670             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00671           strcpy (option_data->cdname[option_data->num_cdiffs-1], argv[nopt]);
00672           nopt++;
00673           continue;
00674         }
00675       
00676       
00677       
00678       if (strncmp(argv[nopt], "-xdiff", 5) == 0)
00679         {
00680           nopt++;
00681           if (nopt+6 >= argc)  ANOVA_error ("need 7 arguments after -xdiff ");
00682           
00683           option_data->num_xdiffs++;
00684           if (option_data->num_xdiffs > MAX_DIFFS)
00685             ANOVA_error ("too many cell means differences");
00686 
00687           sscanf (argv[nopt], "%d", &ival);
00688           if ((ival <= 0) || (ival > option_data->a))
00689             ANOVA_error ("illegal argument after -xdiff ");
00690           option_data->xdiffs[option_data->num_xdiffs-1][0][0] = ival - 1;
00691           nopt++;
00692    
00693           sscanf (argv[nopt], "%d", &ival);
00694           if ((ival <= 0) || (ival > option_data->b))
00695             ANOVA_error ("illegal argument after -xdiff ");
00696           option_data->xdiffs[option_data->num_xdiffs-1][0][1] = ival - 1;
00697           nopt++;
00698    
00699           sscanf (argv[nopt], "%d", &ival);
00700           if ((ival <= 0) || (ival > option_data->c))
00701             ANOVA_error ("illegal argument after -xdiff ");
00702           option_data->xdiffs[option_data->num_xdiffs-1][0][2] = ival - 1;
00703           nopt++;
00704    
00705           sscanf (argv[nopt], "%d", &ival);
00706           if ((ival <= 0) || (ival > option_data->a))
00707             ANOVA_error ("illegal argument after -xdiff ");
00708           option_data->xdiffs[option_data->num_xdiffs-1][1][0] = ival - 1;
00709           nopt++;
00710 
00711           sscanf (argv[nopt], "%d", &ival);
00712           if ((ival <= 0) || (ival > option_data->b))
00713             ANOVA_error ("illegal argument after -xdiff ");
00714           option_data->xdiffs[option_data->num_xdiffs-1][1][1] = ival - 1;
00715           nopt++;
00716 
00717           sscanf (argv[nopt], "%d", &ival);
00718           if ((ival <= 0) || (ival > option_data->c))
00719             ANOVA_error ("illegal argument after -xdiff ");
00720           option_data->xdiffs[option_data->num_xdiffs-1][1][2] = ival - 1;
00721           nopt++;
00722 
00723           option_data->xdname[option_data->num_xdiffs-1] 
00724             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00725           strcpy (option_data->xdname[option_data->num_xdiffs-1], argv[nopt]);
00726           nopt++;
00727           continue;
00728         }
00729       
00730       
00731       
00732       if (strncmp(argv[nopt], "-acontr", 5) == 0)
00733         {
00734           nopt++;
00735           if (nopt + option_data->a >= argc)  
00736             ANOVA_error ("need a+1 arguments after -acontr ");
00737           
00738           option_data->num_acontr++;
00739           if (option_data->num_acontr > MAX_CONTR)
00740             ANOVA_error ("too many factor A level contrasts");
00741           
00742           for (i = 0;  i < option_data->a;  i++)
00743             {
00744               sscanf (argv[nopt], "%f", &fval); 
00745               option_data->acontr[option_data->num_acontr - 1][i] = fval ;
00746               nopt++;
00747             }
00748           
00749           option_data->acname[option_data->num_acontr-1] 
00750             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00751           strcpy (option_data->acname[option_data->num_acontr-1], argv[nopt]);
00752           nopt++;
00753           continue;
00754         }
00755       
00756       
00757       
00758       if (strncmp(argv[nopt], "-bcontr", 5) == 0)
00759         {
00760           nopt++;
00761           if (nopt + option_data->b >= argc)  
00762             ANOVA_error ("need b+1 arguments after -bcontr ");
00763           
00764           option_data->num_bcontr++;
00765           if (option_data->num_bcontr > MAX_CONTR)
00766             ANOVA_error ("too many factor B level contrasts");
00767                   
00768           for (i = 0;  i < option_data->b;  i++)
00769             {
00770               sscanf (argv[nopt], "%f", &fval); 
00771               option_data->bcontr[option_data->num_bcontr - 1][i] = fval ;
00772               nopt++;
00773             }
00774           
00775           option_data->bcname[option_data->num_bcontr-1] 
00776             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00777           strcpy (option_data->bcname[option_data->num_bcontr-1], argv[nopt]);
00778           nopt++;
00779           continue;
00780         }
00781       
00782       
00783       
00784       if (strncmp(argv[nopt], "-ccontr", 5) == 0)
00785         {
00786           nopt++;
00787           if (nopt + option_data->c >= argc)  
00788             ANOVA_error ("need c+1 arguments after -ccontr ");
00789           
00790           option_data->num_ccontr++;
00791           if (option_data->num_ccontr > MAX_CONTR)
00792             ANOVA_error ("too many factor C level contrasts");
00793           
00794           
00795           for (i = 0;  i < option_data->c;  i++)
00796             {
00797               sscanf (argv[nopt], "%f", &fval); 
00798               option_data->ccontr[option_data->num_ccontr - 1][i] = fval ;
00799               nopt++;
00800             }
00801           
00802           option_data->ccname[option_data->num_ccontr-1] 
00803             =  malloc (sizeof(char) * MAX_NAME_LENGTH);
00804           strcpy (option_data->ccname[option_data->num_ccontr-1], argv[nopt]);
00805           nopt++;
00806           continue;
00807         }
00808       
00809       
00810       
00811       if (strncmp(argv[nopt], "-bucket", 4) == 0)
00812         {
00813           nopt++;
00814           if (nopt >= argc)  ANOVA_error ("need argument after -bucket ");
00815           option_data->bucket_filename = malloc (sizeof(char)*MAX_NAME_LENGTH);
00816           strcpy (option_data->bucket_filename, argv[nopt]);
00817           nopt++;
00818           continue;
00819         }
00820       
00821       
00822       
00823       sprintf (message,"Unrecognized command line option: %s\n", argv[nopt]);
00824       ANOVA_error (message);
00825     }
00826 
00827   
00828   
00829   option_data->n = N_INDEX(0, 0, 0);
00830   for (i = 0;  i < option_data->a;  i++)
00831     for (j = 0;  j < option_data->b;  j++)
00832       for (k = 0;  k < option_data->c;  k++)
00833         if (N_INDEX(i, j, k) != option_data->n)
00834           ANOVA_error ("must have equal sample sizes for 3dANOVA3");
00835 
00836   free(n);
00837 }
00838 
00839 
00840 
00841 
00842 
00843 
00844 
00845 void check_temporary_files (anova_options * option_data)
00846 {
00847   
00848   check_one_temporary_file ("ss0");
00849   check_one_temporary_file ("ssi");
00850   check_one_temporary_file ("ssj");
00851   check_one_temporary_file ("ssk");
00852   check_one_temporary_file ("ssij");
00853   check_one_temporary_file ("ssik");
00854   check_one_temporary_file ("ssjk");
00855   check_one_temporary_file ("ssijk");
00856   check_one_temporary_file ("ssijkm");
00857 
00858   check_one_temporary_file ("ssto");
00859   check_one_temporary_file ("sse");
00860   check_one_temporary_file ("ssa");
00861   check_one_temporary_file ("ssb");
00862   check_one_temporary_file ("ssc");
00863   check_one_temporary_file ("ssab");
00864   check_one_temporary_file ("ssac");
00865   check_one_temporary_file ("ssbc");
00866   check_one_temporary_file ("ssabc");
00867   check_one_temporary_file ("ssca");
00868   check_one_temporary_file ("ssbca");
00869 
00870 }
00871 
00872 
00873 
00874 
00875 
00876 
00877 
00878 void check_output_files (anova_options * option_data)
00879 {
00880   int i;       
00881   
00882   if (option_data->nfa > 0)   
00883     check_one_output_file (option_data, option_data->faname);
00884   
00885   if (option_data->nfb > 0)   
00886     check_one_output_file (option_data, option_data->fbname);
00887   
00888   if (option_data->nfc > 0)   
00889     check_one_output_file (option_data, option_data->fcname);
00890   
00891   if (option_data->nfab > 0)   
00892     check_one_output_file (option_data, option_data->fabname);
00893   
00894   if (option_data->nfac > 0)   
00895     check_one_output_file (option_data, option_data->facname);
00896   
00897  if (option_data->nfbc > 0)   
00898     check_one_output_file (option_data, option_data->fbcname);
00899   
00900  if (option_data->nfabc > 0)   
00901     check_one_output_file (option_data, option_data->fabcname);
00902   
00903   if (option_data->num_ameans > 0)
00904     for (i = 0;  i < option_data->num_ameans;  i++)
00905       check_one_output_file (option_data, option_data->amname[i]);
00906   
00907   if (option_data->num_bmeans > 0)
00908     for (i = 0;  i < option_data->num_bmeans;  i++)
00909       check_one_output_file (option_data, option_data->bmname[i]);
00910   
00911   if (option_data->num_cmeans > 0)
00912     for (i = 0;  i < option_data->num_cmeans;  i++)
00913       check_one_output_file (option_data, option_data->cmname[i]);
00914   
00915   if (option_data->num_xmeans > 0)
00916     for (i = 0;  i < option_data->num_xmeans;  i++)
00917       check_one_output_file (option_data, option_data->xmname[i]);
00918   
00919   if (option_data->num_adiffs > 0)
00920     for (i = 0;  i < option_data->num_adiffs;  i++)
00921       check_one_output_file (option_data, option_data->adname[i]);
00922   
00923   if (option_data->num_bdiffs > 0)
00924     for (i = 0;  i < option_data->num_bdiffs;  i++)
00925       check_one_output_file (option_data, option_data->bdname[i]);
00926   
00927   if (option_data->num_cdiffs > 0)
00928     for (i = 0;  i < option_data->num_cdiffs;  i++)
00929       check_one_output_file (option_data, option_data->cdname[i]);
00930   
00931   if (option_data->num_xdiffs > 0)
00932     for (i = 0;  i < option_data->num_xdiffs;  i++)
00933       check_one_output_file (option_data, option_data->xdname[i]);
00934   
00935   if (option_data->num_acontr > 0)
00936     for (i = 0;  i < option_data->num_acontr;  i++)
00937       check_one_output_file (option_data, option_data->acname[i]);
00938   
00939   if (option_data->num_bcontr > 0)
00940     for (i = 0;  i < option_data->num_bcontr;  i++)
00941       check_one_output_file (option_data, option_data->bcname[i]);
00942 
00943   if (option_data->num_ccontr > 0)
00944     for (i = 0;  i < option_data->num_ccontr;  i++)
00945       check_one_output_file (option_data, option_data->ccname[i]);
00946 
00947   if (option_data->bucket_filename != NULL)
00948     check_one_output_file (option_data, option_data->bucket_filename);
00949 
00950 }
00951 
00952 
00953 
00954 
00955 
00956 
00957 
00958 void check_for_valid_inputs (anova_options * option_data)
00959 {
00960   int n;
00961 
00962   
00963   if (option_data->a < 2)  
00964     ANOVA_error ("must specify number of factor A levels (a>1) ");
00965   if (option_data->b < 2) 
00966     ANOVA_error ("must specify number of factor B levels (b>1) ");
00967   if (option_data->c < 2)  
00968     ANOVA_error ("must specify number of factor C levels (c>1) ");
00969   if (option_data->n < 1)  ANOVA_error ("sample size is too small");
00970   
00971   n = option_data->n;
00972   
00973   switch (option_data->model)
00974     {
00975     case 1:
00976       if (n == 1)  ANOVA_error ("sample size is too small for Model 1");  
00977       break;
00978     case 2:
00979       if (option_data->nfa > 0) 
00980         ANOVA_error ("cannot calculate F(A) for Model 2");
00981       if (option_data->nfb > 0)
00982         ANOVA_error ("cannot calculate F(B) for Model 2"); 
00983       if (option_data->nfc > 0)  
00984         ANOVA_error ("cannot calculate F(C) for Model 2");
00985       if ((option_data->nfabc > 0) && (n == 1))
00986         ANOVA_error ("sample size is too small for calculating F(ABC)");
00987       if (option_data->num_ameans > 0)
00988         ANOVA_error ("-amean not allowed for Model 2");
00989       if (option_data->num_bmeans > 0)
00990         ANOVA_error ("-bmean not allowed for Model 2");
00991       if (option_data->num_cmeans > 0)  
00992         ANOVA_error ("-cmean not allowed for Model 2");
00993       if (option_data->num_xmeans > 0)  
00994         ANOVA_error ("-xmean not allowed for Model 2");
00995       if (option_data->num_adiffs > 0)
00996         ANOVA_error ("-adiff not allowed for Model 2");
00997       if (option_data->num_bdiffs > 0)
00998         ANOVA_error ("-bdiff not allowed for Model 2");
00999       if (option_data->num_cdiffs > 0)   
01000         ANOVA_error ("-cdiff not allowed for Model 2");
01001       if (option_data->num_xdiffs > 0)   
01002         ANOVA_error ("-xdiff not allowed for Model 2");
01003       if (option_data->num_acontr > 0) 
01004         ANOVA_error ("-acontr not allowed for Model 2");
01005       if (option_data->num_bcontr > 0) 
01006         ANOVA_error ("-bcontr not allowed for Model 2");
01007       if (option_data->num_ccontr > 0)   
01008         ANOVA_error ("-ccontr not allowed for Model 2");
01009       break;
01010     case 3:
01011       if (option_data->nfa > 0)   
01012         ANOVA_error ("cannot calculate F(A) for Model 3");
01013       if ((option_data->nfbc > 0) && (n == 1))
01014         ANOVA_error ("sample size is too small for calculating F(BC)");
01015       if ((option_data->nfabc > 0) && (n == 1))
01016         ANOVA_error ("sample size is too small for calculating F(ABC)");
01017       if (option_data->num_ameans > 0)
01018         ANOVA_error ("-amean not allowed for Model 3");
01019       if (option_data->num_bmeans > 0)
01020         ANOVA_error ("-bmean not allowed for Model 3");
01021       if (option_data->num_cmeans > 0)  
01022         ANOVA_error ("-cmean not allowed for Model 3");
01023       if (option_data->num_xmeans > 0)  
01024         ANOVA_error ("-xmean not allowed for Model 3");
01025       if (option_data->num_adiffs > 0)
01026         ANOVA_error ("-adiff not allowed for Model 3");
01027       if (option_data->num_bdiffs > 0)
01028         ANOVA_error ("-bdiff not allowed for Model 3");
01029       if (option_data->num_cdiffs > 0)   
01030         ANOVA_error ("-cdiff not allowed for Model 3");
01031       if (option_data->num_xdiffs > 0)   
01032         ANOVA_error ("-xdiff not allowed for Model 3");
01033       if (option_data->num_acontr > 0) 
01034         ANOVA_error ("-acontr not allowed for Model 3");
01035       if (option_data->num_bcontr > 0) 
01036         ANOVA_error ("-bcontr not allowed for Model 3");
01037       if (option_data->num_ccontr > 0)  
01038         ANOVA_error ("-ccontr not allowed for Model 3");
01039       break;
01040     case 4:
01041       if ((option_data->nfc > 0) && (n == 1))
01042         ANOVA_error ("sample size is too small for calculating F(C)");
01043       if ((option_data->nfac > 0) && (n == 1))
01044         ANOVA_error ("sample size is too small for calculating F(AC)");
01045       if ((option_data->nfbc > 0) && (n == 1))
01046         ANOVA_error ("sample size is too small for calculating F(BC)");
01047       if ((option_data->nfabc > 0) && (n == 1))
01048         ANOVA_error ("sample size is too small for calculating F(ABC)");
01049       if (option_data->num_cmeans > 0)  
01050         ANOVA_error ("-cmean not allowed for Model 4");
01051       if (option_data->num_xmeans > 0)  
01052         ANOVA_error ("-xmean not allowed for Model 4");
01053       if (option_data->num_cdiffs > 0)   
01054         ANOVA_error ("-cdiff not allowed for Model 4");
01055       if (option_data->num_xdiffs > 0)   
01056         ANOVA_error ("-xdiff not allowed for Model 4");
01057       if (option_data->num_ccontr > 0)  
01058         ANOVA_error ("-ccontr not allowed for Model 4");
01059       break;
01060     case 5:
01061       if ((option_data->nfc > 0) && (n == 1))
01062         ANOVA_error ("sample size is too small for calculating F(C(A))");
01063       if ((option_data->nfbc > 0) && (n == 1))
01064         ANOVA_error ("sample size is too small for calculating F(BC(A))");
01065       if (option_data->nfac > 0)
01066         ANOVA_error ("F(AC) is meaningless for Model 5");
01067       if (option_data->nfabc > 0)
01068         ANOVA_error ("F(ABC) is meaningless for Model 5");
01069       if (option_data->num_cmeans > 0)  
01070         ANOVA_error ("-cmean not allowed for Model 5");
01071       if (option_data->num_xmeans > 0)  
01072         ANOVA_error ("-xmean not allowed for Model 5");
01073       if (option_data->num_cdiffs > 0)   
01074         ANOVA_error ("-cdiff not allowed for Model 5");
01075       if (option_data->num_xdiffs > 0)   
01076         ANOVA_error ("-xdiff not allowed for Model 5");
01077       if (option_data->num_ccontr > 0)  
01078         ANOVA_error ("-ccontr not allowed for Model 5");
01079       break;
01080     }
01081   
01082 }
01083 
01084 
01085 
01086 
01087 
01088 
01089 
01090 int required_data_files (anova_options * option_data)
01091 {
01092   int now;                         
01093   int nout;                        
01094   int nmax;                        
01095 
01096 
01097   
01098   if (option_data->model != 5)
01099     {
01100       nmax = 10;
01101       now = 8;
01102     }
01103   else
01104     {
01105       nmax = 8;
01106       now = 6;
01107     }
01108 
01109   if (option_data->n == 1)
01110     {
01111       nmax -= 1;
01112       now -= 1;
01113     }
01114 
01115 
01116   
01117   nout = option_data->nfa + option_data->nfb + option_data->nfc 
01118     + option_data->nfab + option_data->nfac + option_data->nfbc 
01119     + option_data->nfabc 
01120     + option_data->num_ameans + option_data->num_bmeans 
01121     + option_data->num_cmeans + option_data->num_xmeans 
01122     + option_data->num_adiffs + option_data->num_bdiffs 
01123     + option_data->num_cdiffs + option_data->num_xdiffs
01124     + option_data->num_acontr + option_data->num_bcontr
01125     + option_data->num_ccontr;
01126 
01127   now = now + nout;
01128 
01129   nmax = max (now, nmax);
01130 
01131   if (option_data->bucket_filename != NULL)
01132     nmax = max (nmax, 2*nout);
01133   
01134   return (nmax);
01135 }
01136 
01137 
01138 
01139 
01140 
01141 
01142 
01143 void initialize (int argc,  char ** argv,  anova_options ** option_data)
01144 {
01145   int i, j, k;                         
01146   int a, b, c;                         
01147   int n;                               
01148   int nxyz;                            
01149   char message[MAX_NAME_LENGTH];       
01150   
01151 
01152   
01153   commandline = tross_commandline( PROGRAM_NAME , argc,argv ) ;
01154 
01155   
01156      
01157   *option_data = (anova_options *) malloc(sizeof(anova_options));
01158   if (*option_data == NULL)
01159     ANOVA_error ("memory allocation error");
01160 
01161   
01162   get_options(argc, argv, *option_data);
01163   
01164   
01165   (*option_data)->first_dataset = (*option_data)->xname[0][0][0][0];
01166   get_dimensions (*option_data);
01167   printf ("Data set dimensions:  nx = %d  ny = %d  nz = %d  nxyz = %d \n",
01168           (*option_data)->nx, (*option_data)->ny,
01169           (*option_data)->nz, (*option_data)->nxyz);
01170   if ((*option_data)->nvoxel > (*option_data)->nxyz)
01171     ANOVA_error ("argument of -voxel is too large");
01172   
01173   
01174   a = (*option_data)->a;
01175   b = (*option_data)->b;
01176   c = (*option_data)->c;
01177   n = (*option_data)->n;
01178   
01179   
01180   (*option_data)->nt = n * a * b * c;
01181   printf ("Number of input datasets = %d \n", (*option_data)->nt);
01182   
01183   
01184   check_for_valid_inputs (*option_data);
01185   
01186   
01187   check_temporary_files (*option_data);
01188   
01189   
01190   check_output_files (*option_data);
01191 
01192   
01193   if ((*option_data)->diskspace)  check_disk_space (*option_data);
01194 }
01195 
01196 
01197 
01198 
01199 
01200 
01201 
01202 
01203 void calculate_sum (anova_options * option_data,
01204                     int ii, int jj, int kk, float * ysum)
01205 {
01206   float * y = NULL;                
01207   int i, itop, ibot;               
01208   int j, jtop, jbot;               
01209   int k, ktop, kbot;               
01210   int m;                           
01211   int a;                           
01212   int b;                           
01213   int c;                           
01214   int n;                           
01215   int ixyz, nxyz;                  
01216   int nvoxel;                      
01217   char sum_label[MAX_NAME_LENGTH]; 
01218   char str[MAX_NAME_LENGTH];       
01219   
01220   
01221   
01222   a = option_data->a;
01223   b = option_data->b;
01224   c = option_data->c;
01225   n = option_data->n;
01226   nxyz = option_data->nxyz;
01227   nvoxel = option_data->nvoxel;
01228   
01229   
01230   y = (float *) malloc(sizeof(float)*nxyz);
01231   if (y == NULL)  ANOVA_error ("unable to allocate sufficient memory");
01232 
01233 
01234   
01235   if (ii < 0)
01236     { ibot = 0;   itop = a; }
01237   else
01238     { ibot = ii;  itop = ii+1; }
01239 
01240   if (jj < 0)
01241     { jbot = 0;   jtop = b; }
01242   else
01243     { jbot = jj;  jtop = jj+1; }
01244 
01245   if (kk < 0)
01246     { kbot = 0;   ktop = c; }
01247   else
01248     { kbot = kk;  ktop = kk+1; }
01249 
01250 
01251   volume_zero (ysum, nxyz);
01252 
01253   
01254   for (i = ibot;  i < itop;  i++)
01255     {
01256       
01257       for (j = jbot;  j < jtop;  j++)
01258         {
01259           
01260           for (k = kbot;  k < ktop;  k++)
01261             {
01262                     
01263               for (m = 0;  m < n;  m++)
01264                 {  
01265                   read_afni_data (option_data, 
01266                                   option_data->xname[i][j][k][m], y);
01267                   if (nvoxel > 0)
01268                     printf ("y[%d][%d][%d][%d] = %f \n", 
01269                             i+1, j+1, k+1, m+1, y[nvoxel-1]);
01270                   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01271                     ysum[ixyz] += y[ixyz];
01272                 } 
01273             }  
01274         }   
01275     }  
01276 
01277 
01278   
01279   if (nvoxel > 0)
01280     {
01281       strcpy (sum_label, "y");
01282       if (ii < 0)
01283         strcat (sum_label, "[.]");
01284       else
01285         {
01286           sprintf (str, "[%d]", ii+1);
01287           strcat (sum_label, str);
01288         }
01289       if (jj < 0)
01290         strcat (sum_label, "[.]");
01291       else
01292         {
01293           sprintf (str, "[%d]", jj+1);
01294           strcat (sum_label, str);
01295         }
01296       if (kk < 0)
01297         strcat (sum_label, "[.]");
01298       else
01299         {
01300           sprintf (str, "[%d]", kk+1);
01301           strcat (sum_label, str);
01302         }
01303       printf ("%s[.] = %f \n", sum_label, ysum[nvoxel-1]);
01304     }
01305  
01306   
01307   free (y);     y = NULL;
01308   
01309 }
01310   
01311 
01312 
01313 
01314 
01315 
01316 
01317 
01318 void calculate_ss0 (anova_options * option_data)
01319 {
01320   float * ss0 = NULL;              
01321   float * ysum = NULL;             
01322   int a;                           
01323   int b;                           
01324   int c;                           
01325   int n;                           
01326   int ixyz, nxyz;                  
01327   int nvoxel;                      
01328   int nval;                        
01329   char filename[MAX_NAME_LENGTH];  
01330   
01331   
01332   
01333   a = option_data->a;
01334   b = option_data->b;
01335   c = option_data->c;
01336   n = option_data->n;
01337   nxyz = option_data->nxyz;
01338   nvoxel = option_data->nvoxel;
01339   nval = a * b * c * n;
01340   
01341   
01342   ss0 = (float *) malloc(sizeof(float)*nxyz);
01343   ysum = (float *) malloc(sizeof(float)*nxyz);
01344   if ((ss0 == NULL) || (ysum == NULL))
01345     ANOVA_error ("unable to allocate sufficient memory");
01346   
01347   
01348   calculate_sum (option_data, -1, -1, -1, ysum);
01349 
01350   
01351   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01352     ss0[ixyz] = ysum[ixyz] * ysum[ixyz] / nval;
01353   
01354 
01355   
01356   if (nvoxel > 0)
01357     printf ("SS0 = %f \n", ss0[nvoxel-1]); 
01358   strcpy (filename, "ss0");
01359   volume_write (filename, ss0, nxyz);
01360   
01361 
01362   
01363   free (ysum);    ysum = NULL;
01364   free (ss0);     ss0 = NULL;
01365   
01366 }
01367 
01368   
01369 
01370 
01371 
01372 
01373 
01374 
01375 void calculate_ssi (anova_options * option_data)
01376 {
01377   float * ssi = NULL;              
01378   float * ysum = NULL;             
01379   int a;                           
01380   int b;                           
01381   int c;                           
01382   int n;                           
01383   int i;                           
01384   int ixyz, nxyz;                  
01385   int nvoxel;                      
01386   int nval;                        
01387   char filename[MAX_NAME_LENGTH];  
01388   
01389   
01390   
01391   a = option_data->a;
01392   b = option_data->b;
01393   c = option_data->c;
01394   n = option_data->n;
01395   nxyz = option_data->nxyz;
01396   nvoxel = option_data->nvoxel;
01397   nval = b * c * n;
01398   
01399   
01400   ssi = (float *) malloc(sizeof(float)*nxyz);
01401   ysum = (float *) malloc(sizeof(float)*nxyz);
01402   if ((ssi == NULL) || (ysum == NULL))
01403     ANOVA_error ("unable to allocate sufficient memory");
01404   
01405   volume_zero (ssi, nxyz);
01406 
01407   
01408   for (i = 0;  i < a;  i++)
01409     {
01410       
01411       calculate_sum (option_data, i, -1, -1, ysum);
01412 
01413       
01414       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01415         ssi[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01416     }
01417 
01418   
01419   if (nvoxel > 0)
01420     printf ("SSI = %f \n", ssi[nvoxel-1]); 
01421   strcpy (filename, "ssi");
01422   volume_write (filename, ssi, nxyz);
01423   
01424 
01425   
01426   free (ysum);    ysum = NULL;
01427   free (ssi);     ssi = NULL;
01428   
01429 }
01430 
01431   
01432 
01433 
01434 
01435 
01436 
01437 
01438 void calculate_ssj (anova_options * option_data)
01439 {
01440   float * ssj = NULL;              
01441   float * ysum = NULL;             
01442   int a;                           
01443   int b;                           
01444   int c;                           
01445   int n;                           
01446   int j;                           
01447   int ixyz, nxyz;                  
01448   int nvoxel;                      
01449   int nval;                        
01450   char filename[MAX_NAME_LENGTH];  
01451   
01452   
01453   
01454   a = option_data->a;
01455   b = option_data->b;
01456   c = option_data->c;
01457   n = option_data->n;
01458   nxyz = option_data->nxyz;
01459   nvoxel = option_data->nvoxel;
01460   nval = a * c * n;
01461   
01462   
01463   ssj = (float *) malloc(sizeof(float)*nxyz);
01464   ysum = (float *) malloc(sizeof(float)*nxyz);
01465   if ((ssj == NULL) || (ysum == NULL))
01466     ANOVA_error ("unable to allocate sufficient memory");
01467   
01468   volume_zero (ssj, nxyz);
01469 
01470   
01471   for (j = 0;  j < b;  j++)
01472     {
01473       
01474       calculate_sum (option_data, -1, j, -1, ysum);
01475 
01476       
01477       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01478         ssj[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01479     }
01480 
01481   
01482   if (nvoxel > 0)
01483     printf ("SSJ = %f \n", ssj[nvoxel-1]); 
01484   strcpy (filename, "ssj");
01485   volume_write (filename, ssj, nxyz);
01486   
01487 
01488   
01489   free (ysum);    ysum = NULL;
01490   free (ssj);     ssj = NULL;
01491   
01492 }
01493 
01494   
01495 
01496 
01497 
01498 
01499 
01500 
01501 void calculate_ssk (anova_options * option_data)
01502 {
01503   float * ssk = NULL;              
01504   float * ysum = NULL;             
01505   int a;                           
01506   int b;                           
01507   int c;                           
01508   int n;                           
01509   int k;                           
01510   int ixyz, nxyz;                  
01511   int nvoxel;                      
01512   int nval;                        
01513   char filename[MAX_NAME_LENGTH];  
01514   
01515   
01516   
01517   a = option_data->a;
01518   b = option_data->b;
01519   c = option_data->c;
01520   n = option_data->n;
01521   nxyz = option_data->nxyz;
01522   nvoxel = option_data->nvoxel;
01523   nval = a * b * n;
01524   
01525   
01526   ssk = (float *) malloc(sizeof(float)*nxyz);
01527   ysum = (float *) malloc(sizeof(float)*nxyz);
01528   if ((ssk == NULL) || (ysum == NULL))
01529     ANOVA_error ("unable to allocate sufficient memory");
01530   
01531   volume_zero (ssk, nxyz);
01532 
01533   
01534   for (k = 0;  k < c;  k++)
01535     {
01536       
01537       calculate_sum (option_data, -1, -1, k, ysum);
01538 
01539       
01540       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01541         ssk[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01542     }
01543 
01544   
01545   if (nvoxel > 0)
01546     printf ("SSK = %f \n", ssk[nvoxel-1]); 
01547   strcpy (filename, "ssk");
01548   volume_write (filename, ssk, nxyz);
01549   
01550 
01551   
01552   free (ysum);    ysum = NULL;
01553   free (ssk);     ssk = NULL;
01554   
01555 }
01556 
01557   
01558 
01559 
01560 
01561 
01562 
01563 
01564 void calculate_ssij (anova_options * option_data)
01565 {
01566   float * ssij = NULL;             
01567   float * ysum = NULL;             
01568   int a;                           
01569   int b;                           
01570   int c;                           
01571   int n;                           
01572   int i, j;                        
01573   int ixyz, nxyz;                  
01574   int nvoxel;                      
01575   int nval;                        
01576   char filename[MAX_NAME_LENGTH];  
01577   
01578   
01579   
01580   a = option_data->a;
01581   b = option_data->b;
01582   c = option_data->c;
01583   n = option_data->n;
01584   nxyz = option_data->nxyz;
01585   nvoxel = option_data->nvoxel;
01586   nval = c * n;
01587   
01588   
01589   ssij = (float *) malloc(sizeof(float)*nxyz);
01590   ysum = (float *) malloc(sizeof(float)*nxyz);
01591   if ((ssij == NULL) || (ysum == NULL))
01592     ANOVA_error ("unable to allocate sufficient memory");
01593   
01594   volume_zero (ssij, nxyz);
01595 
01596   
01597   for (i = 0;  i < a;  i++)
01598     {
01599       
01600       for (j = 0;  j < b;  j++)
01601         {
01602           
01603           calculate_sum (option_data, i, j, -1, ysum);
01604           
01605           
01606           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01607             ssij[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01608         }
01609     }
01610 
01611   
01612   if (nvoxel > 0)
01613     printf ("SSIJ = %f \n", ssij[nvoxel-1]); 
01614   strcpy (filename, "ssij");
01615   volume_write (filename, ssij, nxyz);
01616   
01617 
01618   
01619   free (ysum);    ysum = NULL;
01620   free (ssij);    ssij = NULL;
01621   
01622 }
01623 
01624   
01625 
01626 
01627 
01628 
01629 
01630 
01631 void calculate_ssik (anova_options * option_data)
01632 {
01633   float * ssik = NULL;             
01634   float * ysum = NULL;             
01635   int a;                           
01636   int b;                           
01637   int c;                           
01638   int n;                           
01639   int i, k;                        
01640   int ixyz, nxyz;                  
01641   int nvoxel;                      
01642   int nval;                        
01643   char filename[MAX_NAME_LENGTH];  
01644   
01645   
01646   
01647   a = option_data->a;
01648   b = option_data->b;
01649   c = option_data->c;
01650   n = option_data->n;
01651   nxyz = option_data->nxyz;
01652   nvoxel = option_data->nvoxel;
01653   nval = b * n;
01654   
01655   
01656   ssik = (float *) malloc(sizeof(float)*nxyz);
01657   ysum = (float *) malloc(sizeof(float)*nxyz);
01658   if ((ssik == NULL) || (ysum == NULL))
01659     ANOVA_error ("unable to allocate sufficient memory");
01660   
01661   volume_zero (ssik, nxyz);
01662 
01663   
01664   for (i = 0;  i < a;  i++)
01665     {
01666       
01667       for (k = 0;  k < c;  k++)
01668         {
01669           
01670           calculate_sum (option_data, i, -1, k, ysum);
01671           
01672           
01673           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01674             ssik[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01675         }
01676     }
01677 
01678   
01679   if (nvoxel > 0)
01680     printf ("SSIK = %f \n", ssik[nvoxel-1]); 
01681   strcpy (filename, "ssik");
01682   volume_write (filename, ssik, nxyz);
01683   
01684 
01685   
01686   free (ysum);    ysum = NULL;
01687   free (ssik);    ssik = NULL;
01688   
01689 }
01690 
01691   
01692 
01693 
01694 
01695 
01696 
01697 
01698 void calculate_ssjk (anova_options * option_data)
01699 {
01700   float * ssjk = NULL;             
01701   float * ysum = NULL;             
01702   int a;                           
01703   int b;                           
01704   int c;                           
01705   int n;                           
01706   int j, k;                        
01707   int ixyz, nxyz;                  
01708   int nvoxel;                      
01709   int nval;                        
01710   char filename[MAX_NAME_LENGTH];  
01711   
01712   
01713   
01714   a = option_data->a;
01715   b = option_data->b;
01716   c = option_data->c;
01717   n = option_data->n;
01718   nxyz = option_data->nxyz;
01719   nvoxel = option_data->nvoxel;
01720   nval = a * n;
01721   
01722   
01723   ssjk = (float *) malloc(sizeof(float)*nxyz);
01724   ysum = (float *) malloc(sizeof(float)*nxyz);
01725   if ((ssjk == NULL) || (ysum == NULL))
01726     ANOVA_error ("unable to allocate sufficient memory");
01727   
01728   volume_zero (ssjk, nxyz);
01729 
01730   
01731   for (j = 0;  j < b;  j++)
01732     {
01733       
01734       for (k = 0;  k < c;  k++)
01735         {
01736           
01737           calculate_sum (option_data, -1, j, k, ysum);
01738           
01739           
01740           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01741             ssjk[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01742         }
01743     }
01744 
01745   
01746   if (nvoxel > 0)
01747     printf ("SSJK = %f \n", ssjk[nvoxel-1]); 
01748   strcpy (filename, "ssjk");
01749   volume_write (filename, ssjk, nxyz);
01750   
01751 
01752   
01753   free (ysum);    ysum = NULL;
01754   free (ssjk);    ssjk = NULL;
01755   
01756 }
01757 
01758   
01759 
01760 
01761 
01762 
01763 
01764 
01765 void calculate_ssijk (anova_options * option_data)
01766 {
01767   float * ssijk = NULL;            
01768   float * ysum = NULL;             
01769   int a;                           
01770   int b;                           
01771   int c;                           
01772   int n;                           
01773   int i, j, k;                     
01774   int ixyz, nxyz;                  
01775   int nvoxel;                      
01776   int nval;                        
01777   char filename[MAX_NAME_LENGTH];  
01778   
01779   
01780   
01781   a = option_data->a;
01782   b = option_data->b;
01783   c = option_data->c;
01784   n = option_data->n;
01785   nxyz = option_data->nxyz;
01786   nvoxel = option_data->nvoxel;
01787   nval = n;
01788   
01789   
01790   ssijk = (float *) malloc(sizeof(float)*nxyz);
01791   ysum = (float *) malloc(sizeof(float)*nxyz);
01792   if ((ssijk == NULL) || (ysum == NULL))
01793     ANOVA_error ("unable to allocate sufficient memory");
01794   
01795   volume_zero (ssijk, nxyz);
01796 
01797   
01798   for (i = 0;  i < a;  i++)
01799     {
01800       
01801       for (j = 0;  j < b;  j++)
01802         {
01803           
01804           for (k = 0;  k < c;  k++)
01805             {
01806               
01807               if (n != 1)
01808                 calculate_sum (option_data, i, j, k, ysum);
01809               else
01810                 {
01811                   read_afni_data (option_data, 
01812                                   option_data->xname[i][j][k][0], ysum);
01813                   if (nvoxel > 0)
01814                     printf ("y[%d][%d][%d][.] = %f \n", 
01815                             i+1, j+1, k+1, ysum[nvoxel-1]);
01816                 }
01817               
01818               
01819               for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01820                 ssijk[ixyz] += ysum[ixyz] * ysum[ixyz] / nval;
01821             }
01822         }
01823     }
01824 
01825   
01826   if (nvoxel > 0)
01827     printf ("SSIJK = %f \n", ssijk[nvoxel-1]); 
01828   strcpy (filename, "ssijk");
01829   volume_write (filename, ssijk, nxyz);
01830   
01831   
01832   
01833   free (ysum);    ysum = NULL;
01834   free (ssijk);   ssijk = NULL;
01835   
01836 }
01837 
01838   
01839 
01840 
01841 
01842 
01843 
01844 
01845 void calculate_ssijkm (anova_options * option_data)
01846 {
01847   float * ssijkm = NULL;            
01848   float * y = NULL;                 
01849   int i;                            
01850   int j;                            
01851   int k;                            
01852   int m;                            
01853   int a;                            
01854   int b;                            
01855   int c;                            
01856   int n;                            
01857   int ixyz, nxyz;                   
01858   int nvoxel;                       
01859   float yval;                       
01860   
01861   
01862   
01863   a = option_data->a;
01864   b = option_data->b;
01865   c = option_data->c;
01866   n = option_data->n;
01867   nxyz = option_data->nxyz;
01868   nvoxel = option_data->nvoxel;
01869   
01870   
01871   ssijkm = (float *) malloc(sizeof(float)*nxyz);
01872   y = (float *) malloc(sizeof(float)*nxyz);
01873   if ((ssijkm == NULL) || (y == NULL))
01874     ANOVA_error ("unable to allocate sufficient memory");
01875 
01876 
01877   volume_zero (ssijkm, nxyz);
01878   
01879   for (i = 0;  i < a;  i++)
01880     {
01881       for (j = 0;  j < b;  j++)
01882         {
01883           for (k = 0;  k < c;  k++)
01884             {
01885               for (m = 0;  m < n;  m++)
01886                 {
01887                   read_afni_data (option_data, 
01888                                   option_data->xname[i][j][k][m], y);
01889                   if (nvoxel > 0)
01890                     printf ("y[%d][%d][%d][%d] = %f \n", 
01891                             i+1, j+1, k+1, m+1, y[nvoxel-1]);
01892           
01893                   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01894                       ssijkm[ixyz] += y[ixyz] * y[ixyz]; 
01895                 }
01896             }
01897         }
01898     }
01899   
01900 
01901     
01902   if (nvoxel > 0)
01903     printf ("SSIJKM = %f \n", ssijkm[nvoxel-1]); 
01904   volume_write ("ssijkm", ssijkm, nxyz);
01905   
01906   
01907   free (y);       y = NULL;
01908   free (ssijkm);  ssijkm = NULL;
01909   
01910 }
01911 
01912 
01913 
01914 
01915 
01916 
01917 
01918 
01919 void calculate_ssto (anova_options * option_data)
01920 {
01921   float * y = NULL;                   
01922   float * ssto = NULL;                
01923   int ixyz, nxyz;                     
01924   int nvoxel;                         
01925   
01926   
01927   
01928   nxyz = option_data->nxyz;
01929   nvoxel = option_data->nvoxel;
01930   
01931   
01932   ssto = (float *) malloc (sizeof(float)*nxyz);
01933   y = (float *) malloc (sizeof(float)*nxyz);
01934   if ((y == NULL) || (ssto == NULL))
01935     ANOVA_error ("unable to allocate sufficient memory");
01936 
01937  
01938   
01939   if (option_data->n != 1)
01940     volume_read ("ssijkm", ssto, nxyz);
01941   else
01942     volume_read ("ssijk", ssto, nxyz);
01943 
01944   volume_read ("ss0", y, nxyz);
01945   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01946     ssto[ixyz] -= y[ixyz];
01947     
01948   
01949   
01950   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01951     if (ssto[ixyz] < 0.0)  ssto[ixyz] = 0.0; 
01952   
01953   
01954   if (nvoxel > 0)
01955     printf ("SSTO = %f \n", ssto[nvoxel-1]);
01956   volume_write ("ssto", ssto, nxyz);
01957   
01958   
01959   free (y);      y = NULL;
01960   free (ssto);    ssto = NULL;
01961   
01962 }
01963 
01964 
01965 
01966 
01967 
01968 
01969 
01970 
01971 void calculate_sse (anova_options * option_data)
01972 {
01973   float * y = NULL;                   
01974   float * sse = NULL;                 
01975   int ixyz, nxyz;                     
01976   int nvoxel;                         
01977   
01978   
01979   
01980   nxyz = option_data->nxyz;
01981   nvoxel = option_data->nvoxel;
01982   
01983   
01984   sse = (float *) malloc (sizeof(float)*nxyz);
01985   y = (float *) malloc (sizeof(float)*nxyz);
01986   if ((y == NULL) || (sse == NULL))
01987     ANOVA_error ("unable to allocate sufficient memory");
01988 
01989  
01990   
01991   volume_read ("ssto", sse, nxyz);
01992 
01993   volume_read ("ssijk", y, nxyz);
01994   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01995     sse[ixyz] -= y[ixyz]; 
01996   
01997   volume_read ("ss0", y, nxyz);
01998   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
01999     sse[ixyz] += y[ixyz];
02000 
02001   
02002   
02003   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02004     if (sse[ixyz] < 0.0)  sse[ixyz] = 0.0; 
02005   
02006   
02007   if (nvoxel > 0)
02008     printf ("SSE = %f \n", sse[nvoxel-1]);
02009   volume_write ("sse", sse, nxyz);
02010   
02011   
02012   free (y);      y = NULL;
02013   free (sse);    sse = NULL;
02014   
02015 }
02016 
02017 
02018 
02019 
02020 
02021 
02022 
02023 
02024 void calculate_ssa (anova_options * option_data)
02025 {
02026   float * y = NULL;                   
02027   float * ssa = NULL;                 
02028   int ixyz, nxyz;                     
02029   int nvoxel;                         
02030 
02031 
02032   
02033   nxyz = option_data->nxyz;
02034   nvoxel = option_data->nvoxel;
02035   
02036   
02037   ssa = (float *) malloc (sizeof(float)*nxyz);
02038   y = (float *) malloc (sizeof(float)*nxyz);
02039   if ((y == NULL) || (ssa == NULL))
02040     ANOVA_error ("unable to allocate sufficient memory");
02041 
02042  
02043   
02044   volume_read ("ssi", ssa, nxyz);
02045 
02046   volume_read ("ss0", y, nxyz);
02047   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02048     ssa[ixyz] -= y[ixyz]; 
02049   
02050   
02051   
02052   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02053     if (ssa[ixyz] < 0.0)  ssa[ixyz] = 0.0; 
02054   
02055   
02056   if (nvoxel > 0)
02057     printf ("SSA = %f \n", ssa[nvoxel-1]);
02058   volume_write ("ssa", ssa, nxyz);
02059   
02060   
02061   free (y);     y = NULL;
02062   free (ssa);   ssa = NULL;
02063   
02064 }
02065 
02066 
02067 
02068 
02069 
02070 
02071 
02072 
02073 void calculate_ssb (anova_options * option_data)
02074 {
02075   float * y = NULL;                   
02076   float * ssb = NULL;                 
02077   int ixyz, nxyz;                     
02078   int nvoxel;                         
02079 
02080 
02081   
02082   nxyz = option_data->nxyz;
02083   nvoxel = option_data->nvoxel;
02084   
02085   
02086   ssb = (float *) malloc (sizeof(float)*nxyz);
02087   y = (float *) malloc (sizeof(float)*nxyz);
02088   if ((y == NULL) || (ssb == NULL))
02089     ANOVA_error ("unable to allocate sufficient memory");
02090 
02091  
02092   
02093   volume_read ("ssj", ssb, nxyz);
02094 
02095   volume_read ("ss0", y, nxyz);
02096   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02097     ssb[ixyz] -= y[ixyz]; 
02098   
02099   
02100   
02101   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02102     if (ssb[ixyz] < 0.0)  ssb[ixyz] = 0.0; 
02103   
02104   
02105   if (nvoxel > 0)
02106     printf ("SSB = %f \n", ssb[nvoxel-1]);
02107   volume_write ("ssb", ssb, nxyz);
02108   
02109   
02110   free (y);     y = NULL;
02111   free (ssb);   ssb = NULL;
02112   
02113 }
02114 
02115 
02116 
02117 
02118 
02119 
02120 
02121 
02122 void calculate_ssc (anova_options * option_data)
02123 {
02124   float * y = NULL;                   
02125   float * ssc = NULL;                 
02126   int ixyz, nxyz;                     
02127   int nvoxel;                         
02128 
02129 
02130   
02131   nxyz = option_data->nxyz;
02132   nvoxel = option_data->nvoxel;
02133   
02134   
02135   ssc = (float *) malloc (sizeof(float)*nxyz);
02136   y = (float *) malloc (sizeof(float)*nxyz);
02137   if ((y == NULL) || (ssc == NULL))
02138     ANOVA_error ("unable to allocate sufficient memory");
02139 
02140  
02141   
02142   volume_read ("ssk", ssc, nxyz);
02143 
02144   volume_read ("ss0", y, nxyz);
02145   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02146     ssc[ixyz] -= y[ixyz]; 
02147   
02148   
02149   
02150   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02151     if (ssc[ixyz] < 0.0)  ssc[ixyz] = 0.0; 
02152   
02153   
02154   if (nvoxel > 0)
02155     printf ("SSC = %f \n", ssc[nvoxel-1]);
02156   volume_write ("ssc", ssc, nxyz);
02157   
02158   
02159   free (y);     y = NULL;
02160   free (ssc);   ssc = NULL;
02161   
02162 }
02163 
02164 
02165 
02166 
02167 
02168 
02169 
02170 
02171 void calculate_ssab (anova_options * option_data)
02172 {
02173   float * y = NULL;                   
02174   float * ssab = NULL;                
02175   int ixyz, nxyz;                     
02176   int nvoxel;                         
02177 
02178 
02179   
02180   nxyz = option_data->nxyz;
02181   nvoxel = option_data->nvoxel;
02182   
02183   
02184   ssab = (float *) malloc (sizeof(float)*nxyz);
02185   y = (float *) malloc (sizeof(float)*nxyz);
02186   if ((y == NULL) || (ssab == NULL))
02187     ANOVA_error ("unable to allocate sufficient memory");
02188 
02189  
02190   
02191   volume_read ("ssij", ssab, nxyz);
02192 
02193   volume_read ("ssa", y, nxyz);
02194   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02195     ssab[ixyz] -= y[ixyz];
02196 
02197   volume_read ("ssb", y, nxyz);
02198   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02199     ssab[ixyz] -= y[ixyz];
02200 
02201   volume_read ("ss0", y, nxyz);
02202   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02203     ssab[ixyz] -= y[ixyz]; 
02204   
02205   
02206   
02207   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02208     if (ssab[ixyz] < 0.0)  ssab[ixyz] = 0.0; 
02209   
02210   
02211   if (nvoxel > 0)
02212     printf ("SSAB = %f \n", ssab[nvoxel-1]);
02213   volume_write ("ssab", ssab, nxyz);
02214   
02215   
02216   free (y);      y = NULL;
02217   free (ssab);   ssab = NULL;
02218   
02219 }
02220 
02221 
02222 
02223 
02224 
02225 
02226 
02227 
02228 void calculate_ssac (anova_options * option_data)
02229 {
02230   float * y = NULL;                   
02231   float * ssac = NULL;                
02232   int ixyz, nxyz;                     
02233   int nvoxel;                         
02234 
02235 
02236   
02237   nxyz = option_data->nxyz;
02238   nvoxel = option_data->nvoxel;
02239   
02240   
02241   ssac = (float *) malloc (sizeof(float)*nxyz);
02242   y = (float *) malloc (sizeof(float)*nxyz);
02243   if ((y == NULL) || (ssac == NULL))
02244     ANOVA_error ("unable to allocate sufficient memory");
02245 
02246  
02247   
02248   volume_read ("ssik", ssac, nxyz);
02249 
02250   volume_read ("ssa", y, nxyz);
02251   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02252     ssac[ixyz] -= y[ixyz];
02253 
02254   volume_read ("ssc", y, nxyz);
02255   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02256     ssac[ixyz] -= y[ixyz];
02257 
02258   volume_read ("ss0", y, nxyz);
02259   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02260     ssac[ixyz] -= y[ixyz]; 
02261   
02262   
02263   
02264   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02265     if (ssac[ixyz] < 0.0)  ssac[ixyz] = 0.0; 
02266   
02267   
02268   if (nvoxel > 0)
02269     printf ("SSAC = %f \n", ssac[nvoxel-1]);
02270   volume_write ("ssac", ssac, nxyz);
02271   
02272   
02273   free (y);      y = NULL;
02274   free (ssac);   ssac = NULL;
02275   
02276 }
02277 
02278 
02279 
02280 
02281 
02282 
02283 
02284 
02285 void calculate_ssca (anova_options * option_data)
02286 {
02287   float * y = NULL;                   
02288   float * ssca = NULL;                
02289   int ixyz, nxyz;                     
02290   int nvoxel;                         
02291 
02292 
02293   
02294   nxyz = option_data->nxyz;
02295   nvoxel = option_data->nvoxel;
02296   
02297   
02298   ssca = (float *) malloc (sizeof(float)*nxyz);
02299   y = (float *) malloc (sizeof(float)*nxyz);
02300   if ((y == NULL) || (ssca == NULL))
02301     ANOVA_error ("unable to allocate sufficient memory");
02302 
02303  
02304   
02305   volume_read ("ssik", ssca, nxyz);
02306 
02307   volume_read ("ssa", y, nxyz);
02308   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02309     ssca[ixyz] -= y[ixyz];
02310 
02311   volume_read ("ss0", y, nxyz);
02312   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02313     ssca[ixyz] -= y[ixyz]; 
02314   
02315   
02316   
02317   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02318     if (ssca[ixyz] < 0.0)  ssca[ixyz] = 0.0; 
02319   
02320   
02321   if (nvoxel > 0)
02322     printf ("SSC(A) = %f \n", ssca[nvoxel-1]);
02323   volume_write ("ssca", ssca, nxyz);
02324   
02325   
02326   free (y);      y = NULL;
02327   free (ssca);   ssca = NULL;
02328   
02329 }
02330 
02331 
02332 
02333 
02334 
02335 
02336 
02337 
02338 void calculate_ssbc (anova_options * option_data)
02339 {
02340   float * y = NULL;                   
02341   float * ssbc = NULL;                
02342   int ixyz, nxyz;                     
02343   int nvoxel;                         
02344 
02345 
02346   
02347   nxyz = option_data->nxyz;
02348   nvoxel = option_data->nvoxel;
02349   
02350   
02351   ssbc = (float *) malloc (sizeof(float)*nxyz);
02352   y = (float *) malloc (sizeof(float)*nxyz);
02353   if ((y == NULL) || (ssbc == NULL))
02354     ANOVA_error ("unable to allocate sufficient memory");
02355 
02356  
02357   
02358   volume_read ("ssjk", ssbc, nxyz);
02359 
02360   volume_read ("ssb", y, nxyz);
02361   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02362     ssbc[ixyz] -= y[ixyz];
02363 
02364   volume_read ("ssc", y, nxyz);
02365   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02366     ssbc[ixyz] -= y[ixyz];
02367 
02368   volume_read ("ss0", y, nxyz);
02369   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02370     ssbc[ixyz] -= y[ixyz]; 
02371   
02372   
02373   
02374   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02375     if (ssbc[ixyz] < 0.0)  ssbc[ixyz] = 0.0; 
02376   
02377   
02378   if (nvoxel > 0)
02379     printf ("SSBC = %f \n", ssbc[nvoxel-1]);
02380   volume_write ("ssbc", ssbc, nxyz);
02381   
02382   
02383   free (y);      y = NULL;
02384   free (ssbc);   ssbc = NULL;
02385   
02386 }
02387 
02388 
02389 
02390 
02391 
02392 
02393 
02394 
02395 void calculate_ssabc (anova_options * option_data)
02396 {
02397   float * y = NULL;                   
02398   float * ssabc = NULL;               
02399   int ixyz, nxyz;                     
02400   int nvoxel;                         
02401   
02402   
02403   
02404   nxyz = option_data->nxyz;
02405   nvoxel = option_data->nvoxel;
02406   
02407   
02408   ssabc = (float *) malloc (sizeof(float)*nxyz);
02409   y = (float *) malloc (sizeof(float)*nxyz);
02410   if ((y == NULL) || (ssabc == NULL))
02411     ANOVA_error ("unable to allocate sufficient memory");
02412 
02413 
02414   
02415   volume_read ("ssto", ssabc, nxyz);
02416 
02417   if (option_data->n > 1)
02418     {
02419       volume_read ("sse", y, nxyz);
02420       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02421         ssabc[ixyz] -= y[ixyz];  
02422     }
02423 
02424   volume_read ("ssa", y, nxyz);
02425   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02426     ssabc[ixyz] -= y[ixyz];  
02427 
02428   volume_read ("ssb", y, nxyz);
02429   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02430     ssabc[ixyz] -= y[ixyz];  
02431 
02432   volume_read ("ssc", y, nxyz);
02433   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02434     ssabc[ixyz] -= y[ixyz];  
02435 
02436   volume_read ("ssab", y, nxyz);
02437   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02438     ssabc[ixyz] -= y[ixyz];  
02439 
02440   volume_read ("ssac", y, nxyz);
02441   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02442     ssabc[ixyz] -= y[ixyz];  
02443 
02444   volume_read ("ssbc", y, nxyz);
02445   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02446     ssabc[ixyz] -= y[ixyz];  
02447   
02448 
02449   
02450   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02451     if (ssabc[ixyz] < 0.0)  ssabc[ixyz] = 0.0; 
02452   
02453   
02454   if (nvoxel > 0)
02455     printf ("SSABC = %f \n", ssabc[nvoxel-1]);
02456   volume_write ("ssabc", ssabc, nxyz);
02457   
02458   
02459   free (y);     y = NULL;
02460   free (ssabc);   ssabc = NULL;
02461   
02462 }
02463 
02464 
02465 
02466 
02467 
02468 
02469 
02470 
02471 void calculate_ssbca (anova_options * option_data)
02472 {
02473   float * y = NULL;                   
02474   float * ssbca = NULL;               
02475   int ixyz, nxyz;                     
02476   int nvoxel;                         
02477   
02478   
02479   
02480   nxyz = option_data->nxyz;
02481   nvoxel = option_data->nvoxel;
02482   
02483   
02484   ssbca = (float *) malloc (sizeof(float)*nxyz);
02485   y = (float *) malloc (sizeof(float)*nxyz);
02486   if ((y == NULL) || (ssbca == NULL))
02487     ANOVA_error ("unable to allocate sufficient memory");
02488 
02489 
02490   
02491   volume_read ("ssto", ssbca, nxyz);
02492 
02493   if (option_data->n > 1)
02494     {
02495       volume_read ("sse", y, nxyz);
02496       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02497         ssbca[ixyz] -= y[ixyz];  
02498     }
02499 
02500   volume_read ("ssa", y, nxyz);
02501   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02502     ssbca[ixyz] -= y[ixyz];  
02503 
02504   volume_read ("ssb", y, nxyz);
02505   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02506     ssbca[ixyz] -= y[ixyz];  
02507 
02508   volume_read ("ssca", y, nxyz);
02509   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02510     ssbca[ixyz] -= y[ixyz];  
02511 
02512   volume_read ("ssab", y, nxyz);
02513   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02514     ssbca[ixyz] -= y[ixyz];  
02515   
02516 
02517   
02518   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02519     if (ssbca[ixyz] < 0.0)  ssbca[ixyz] = 0.0; 
02520   
02521   
02522   if (nvoxel > 0)
02523     printf ("SSBC(A) = %f \n", ssbca[nvoxel-1]);
02524   volume_write ("ssbca", ssbca, nxyz);
02525   
02526   
02527   free (y);     y = NULL;
02528   free (ssbca);   ssbca = NULL;
02529   
02530 }
02531 
02532 
02533 
02534 
02535 
02536 
02537 
02538 
02539 
02540 
02541 
02542 
02543 
02544 
02545 
02546 
02547 
02548 
02549 
02550 
02551 
02552 void calculate_fa (anova_options * option_data)
02553 {
02554   const float  EPSILON = 1.0e-10;      
02555   float * msa = NULL;                  
02556   float * fa = NULL;                   
02557   int a;                               
02558   int b;                               
02559   int c;                               
02560   int n;                               
02561   int ixyz, nxyz;                      
02562   int nvoxel;                          
02563   int numdf;                           
02564   int dendf;                           
02565   float fval;                          
02566   
02567   
02568   
02569   a = option_data->a;
02570   b = option_data->b;
02571   c = option_data->c;
02572   n = option_data->n;
02573   nxyz = option_data->nxyz;
02574   nvoxel = option_data->nvoxel;
02575   
02576   
02577   fa = (float *) malloc(sizeof(float)*nxyz);
02578   msa = (float *) malloc(sizeof(float)*nxyz);
02579   if ((fa == NULL) || (msa == NULL))
02580     ANOVA_error ("unable to allocate sufficient memory");
02581   
02582   
02583   volume_read ("ssa", msa, nxyz); 
02584   numdf = a - 1; 
02585   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02586     msa[ixyz] = msa[ixyz] / numdf;             
02587   if (nvoxel > 0)
02588     printf ("MSA = %f \n", msa[nvoxel-1]);
02589   
02590   
02591   switch (option_data->model)
02592     {
02593     case 1:
02594       volume_read ("sse", fa, nxyz); 
02595       dendf = a * b * c * (n-1);
02596       break;
02597     case 4:
02598       volume_read ("ssac", fa, nxyz); 
02599       dendf = (a-1) * (c-1);
02600       break;
02601     case 5:
02602       volume_read ("ssca", fa, nxyz); 
02603       dendf = a * (c-1);
02604       break;
02605     }
02606 
02607   
02608   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02609     {
02610       fval = fa[ixyz] / dendf;         
02611       if (fval < EPSILON)
02612         fa[ixyz] = 0.0;
02613       else
02614         fa[ixyz] = msa[ixyz] / fval;            
02615     }
02616   
02617   if (nvoxel > 0)
02618     printf ("F(A) = %f \n", fa[nvoxel-1]);
02619 
02620   
02621   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02622     msa[ixyz] = sqrt(msa[ixyz]);        
02623   write_afni_data (option_data, option_data->faname, 
02624                    msa, fa, numdf, dendf);
02625 
02626   
02627   free (msa);   msa = NULL;
02628   free (fa);    fa = NULL;
02629   
02630 }
02631 
02632 
02633 
02634 
02635 
02636 
02637 
02638 
02639 
02640 
02641 
02642 
02643 
02644 
02645 
02646 
02647 
02648 
02649 
02650 
02651 
02652 
02653 void calculate_fb (anova_options * option_data)
02654 {
02655   const float  EPSILON = 1.0e-10;      
02656   float * msb = NULL;                  
02657   float * fb = NULL;                   
02658   int a;                               
02659   int b;                               
02660   int c;                               
02661   int n;                               
02662   int ixyz, nxyz;                      
02663   int nvoxel;                          
02664   int numdf;                           
02665   int dendf;                           
02666   float fval;                          
02667  
02668 
02669   
02670   a = option_data->a;
02671   b = option_data->b;
02672   c = option_data->c;
02673   n = option_data->n;
02674   nxyz = option_data->nxyz;
02675   nvoxel = option_data->nvoxel;
02676    
02677   
02678   fb = (float *) malloc(sizeof(float)*nxyz);
02679   msb = (float *) malloc(sizeof(float)*nxyz);
02680   if ((fb == NULL) || (msb == NULL))
02681     ANOVA_error ("unable to allocate sufficient memory");
02682   
02683   
02684   volume_read ("ssb", msb, nxyz); 
02685   numdf = b - 1; 
02686   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02687     msb[ixyz] = msb[ixyz] / numdf;             
02688   if (nvoxel > 0)
02689     printf ("MSB = %f \n", msb[nvoxel-1]);
02690   
02691   
02692   switch (option_data->model)
02693     {
02694     case 1:
02695       volume_read ("sse", fb, nxyz); 
02696       dendf = a * b * c * (n-1);
02697       break;
02698     case 3:
02699     case 4:
02700       volume_read ("ssbc", fb, nxyz); 
02701       dendf = (b-1) * (c-1);
02702       break;
02703     case 5:
02704       volume_read ("ssbca", fb, nxyz); 
02705       dendf = a * (b-1) * (c-1);
02706       break;
02707     }
02708 
02709   
02710   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02711     {
02712       fval = fb[ixyz] / dendf;   
02713       if (fval < EPSILON) 
02714         fb[ixyz] = 0.0;
02715       else
02716         fb[ixyz] = msb[ixyz] / fval;
02717     }
02718   
02719   if (nvoxel > 0)
02720     printf ("F(B) = %f \n", fb[nvoxel-1]);
02721   
02722   
02723   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02724     msb[ixyz] = sqrt(msb[ixyz]);        
02725   write_afni_data (option_data, option_data->fbname, 
02726                    msb, fb, numdf, dendf);
02727   
02728   
02729   free (msb);    msb = NULL;
02730   free (fb);     fb  = NULL;
02731   
02732 }
02733 
02734 
02735 
02736 
02737 
02738 
02739 
02740 
02741 
02742 
02743 
02744 
02745 
02746 
02747 
02748 
02749 
02750 
02751 
02752 
02753 
02754 
02755 void calculate_fc (anova_options * option_data)
02756 {
02757   const float  EPSILON = 1.0e-10;      
02758   float * msc = NULL;                  
02759   float * fc = NULL;                   
02760   int a;                               
02761   int b;                               
02762   int c;                               
02763   int n;                               
02764   int ixyz, nxyz;                      
02765   int nvoxel;                          
02766   int numdf;                           
02767   int dendf;                           
02768   float fval;                          
02769  
02770 
02771   
02772   a = option_data->a;
02773   b = option_data->b;
02774   c = option_data->c;
02775   n = option_data->n;
02776   nxyz = option_data->nxyz;
02777   nvoxel = option_data->nvoxel;
02778    
02779   
02780   fc = (float *) malloc(sizeof(float)*nxyz);
02781   msc = (float *) malloc(sizeof(float)*nxyz);
02782   if ((fc == NULL) || (msc == NULL))
02783     ANOVA_error ("unable to allocate sufficient memory");
02784   
02785   
02786   switch (option_data->model)
02787     {
02788     case 1:
02789     case 3:
02790     case 4:
02791       volume_read ("ssc", msc, nxyz); 
02792       numdf = c - 1;
02793       break;
02794     case 5:
02795       volume_read ("ssca", msc, nxyz); 
02796       numdf = a * (c-1);
02797       break;
02798     }
02799 
02800   
02801   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02802     msc[ixyz] = msc[ixyz] / numdf;       
02803   if (nvoxel > 0)
02804     if (option_data->model != 5)
02805       printf ("MSC = %f \n", msc[nvoxel-1]);
02806     else
02807       printf ("MSC(A) = %f \n", msc[nvoxel-1]);
02808       
02809 
02810   
02811   switch (option_data->model)
02812     {
02813     case 1:
02814     case 4:
02815     case 5:
02816       volume_read ("sse", fc, nxyz); 
02817       dendf = a * b * c * (n-1);
02818       break;
02819     case 3:
02820       volume_read ("ssbc", fc, nxyz); 
02821       dendf = (b-1) * (c-1);
02822       break;
02823     }
02824   
02825   
02826   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02827     {
02828       fval = fc[ixyz] / dendf;               
02829       if (fval < EPSILON)
02830         fc[ixyz] = 0.0;
02831       else
02832         fc[ixyz] = msc[ixyz] / fval;          
02833     }
02834   
02835   if (nvoxel > 0)
02836     if (option_data->model != 5)
02837       printf ("F(C) = %f \n", fc[nvoxel-1]);
02838     else
02839       printf ("F(C(A)) = %f \n", fc[nvoxel-1]);
02840      
02841   
02842   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02843     msc[ixyz] = sqrt(msc[ixyz]);        
02844   write_afni_data (option_data, option_data->fcname, 
02845                    msc, fc, numdf, dendf);
02846   
02847   
02848   free (msc);    msc = NULL;
02849   free (fc);     fc  = NULL;
02850   
02851 }
02852 
02853 
02854 
02855 
02856 
02857 
02858 
02859 
02860 
02861 
02862 
02863 
02864 
02865 
02866 
02867 
02868 
02869 
02870 
02871 
02872 
02873 
02874 
02875 void calculate_fab (anova_options * option_data)
02876 {
02877   const float  EPSILON = 1.0e-10;      
02878   float * msab = NULL;                 
02879   float * fab = NULL;                  
02880   int a;                               
02881   int b;                               
02882   int c;                               
02883   int n;                               
02884   int ixyz, nxyz;                      
02885   int nvoxel;                          
02886   int numdf;                           
02887   int dendf;                           
02888   float fval;                          
02889   
02890   
02891   
02892   a = option_data->a;
02893   b = option_data->b;
02894   c = option_data->c;
02895   n = option_data->n;
02896   nxyz = option_data->nxyz;
02897   nvoxel = option_data->nvoxel;
02898    
02899   
02900   fab = (float *) malloc(sizeof(float)*nxyz);
02901   msab = (float *) malloc(sizeof(float)*nxyz);
02902   if ((fab == NULL) || (msab == NULL))
02903     ANOVA_error ("unable to allocate sufficient memory");
02904   
02905   
02906   volume_read ("ssab", msab, nxyz); 
02907   numdf = (a-1) * (b-1); 
02908   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02909     msab[ixyz] = msab[ixyz] / numdf;      
02910   if (nvoxel > 0)
02911     printf ("MSAB = %f \n", msab[nvoxel-1]);
02912 
02913   
02914   switch (option_data->model)
02915     {
02916     case 1:
02917       volume_read ("sse", fab, nxyz); 
02918       dendf = a * b * c * (n-1);
02919       break;
02920     case 2:
02921     case 3:
02922     case 4:
02923       volume_read ("ssabc", fab, nxyz); 
02924       dendf = (a-1) * (b-1) * (c-1);
02925       break;
02926     case 5:
02927       volume_read ("ssbca", fab, nxyz); 
02928       dendf = a * (b-1) * (c-1);
02929       break;
02930     }
02931 
02932   
02933   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02934     {
02935       fval = fab[ixyz] / dendf;  
02936       if (fval < EPSILON)
02937         fab[ixyz] = 0.0;
02938       else
02939         fab[ixyz] = msab[ixyz] / fval;       
02940     }
02941   if (nvoxel > 0)
02942     printf ("F(AB) = %f \n", fab[nvoxel-1]);
02943   
02944   
02945   
02946   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
02947     msab[ixyz] = sqrt(msab[ixyz]);      
02948   write_afni_data (option_data, option_data->fabname, 
02949                    msab, fab, numdf, dendf);
02950   
02951   
02952   
02953   free (msab);   msab = NULL;
02954   free (fab);    fab  = NULL;
02955   
02956 }
02957 
02958 
02959 
02960 
02961 
02962 
02963 
02964 
02965 
02966 
02967 
02968 
02969 
02970 
02971 
02972 
02973 
02974 
02975 
02976 
02977 
02978 
02979 
02980 void calculate_fbc (anova_options * option_data)
02981 {
02982   const float  EPSILON = 1.0e-10;      
02983   float * msbc = NULL;                 
02984   float * fbc = NULL;                  
02985   int a;                               
02986   int b;                               
02987   int c;                               
02988   int n;                               
02989   int ixyz, nxyz;                      
02990   int nvoxel;                          
02991   int numdf;                           
02992   int dendf;                           
02993   float fval;                          
02994   
02995   
02996   
02997   a = option_data->a;
02998   b = option_data->b;
02999   c = option_data->c;
03000   n = option_data->n;
03001   nxyz = option_data->nxyz;
03002   nvoxel = option_data->nvoxel;
03003    
03004   
03005   fbc = (float *) malloc(sizeof(float)*nxyz);
03006   msbc = (float *) malloc(sizeof(float)*nxyz);
03007   if ((fbc == NULL) || (msbc == NULL))
03008     ANOVA_error ("unable to allocate sufficient memory");
03009 
03010   
03011   switch (option_data->model)
03012     {
03013     case 1:
03014     case 2:
03015     case 3:
03016     case 4:
03017       volume_read ("ssbc", msbc, nxyz); 
03018       numdf = (b-1) * (c-1);
03019       break;
03020     case 5:
03021       volume_read ("ssbca", msbc, nxyz); 
03022       numdf = a * (b-1) * (c-1);
03023       break;
03024     }
03025   
03026   
03027   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03028     msbc[ixyz] = msbc[ixyz] / numdf; 
03029   if (nvoxel > 0)
03030     if (option_data->model != 5)
03031       printf ("MSBC = %f \n", msbc[nvoxel-1]);
03032     else
03033       printf ("MSBC(A) = %f \n", msbc[nvoxel-1]);
03034  
03035  
03036   switch (option_data->model)
03037     {
03038     case 1:
03039     case 3:
03040     case 4:
03041     case 5:
03042       volume_read ("sse", fbc, nxyz); 
03043       dendf = a * b * c * (n-1);
03044       break;
03045     case 2:
03046       volume_read ("ssabc", fbc, nxyz); 
03047       dendf = (a-1) * (b-1) * (c-1);
03048       break;
03049     }
03050  
03051   
03052   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03053     {
03054       fval = fbc[ixyz] / dendf;              
03055       if (fval < EPSILON)
03056         fbc[ixyz] = 0.0;
03057       else
03058         fbc[ixyz] = msbc[ixyz] / fval;
03059     }
03060   if (nvoxel > 0)
03061     if (option_data->model != 5)
03062       printf ("F(BC) = %f \n", fbc[nvoxel-1]);
03063     else
03064       printf ("F(BC(A)) = %f \n", fbc[nvoxel-1]);
03065   
03066   
03067   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03068     msbc[ixyz] = sqrt(msbc[ixyz]);      
03069   write_afni_data (option_data, option_data->fbcname, 
03070                    msbc, fbc, numdf, dendf);
03071   
03072   
03073   free (msbc);   msbc = NULL;
03074   free (fbc);    fbc  = NULL;
03075   
03076 }
03077 
03078 
03079 
03080 
03081 
03082 
03083 
03084 
03085 
03086 
03087 
03088 
03089 
03090 
03091 
03092 
03093 
03094 
03095 
03096 
03097 void calculate_fac (anova_options * option_data)
03098 {
03099   const float  EPSILON = 1.0e-10;      
03100   float * msac = NULL;                 
03101   float * fac = NULL;                  
03102   int a;                               
03103   int b;                               
03104   int c;                               
03105   int n;                               
03106   int ixyz, nxyz;                      
03107   int nvoxel;                          
03108   int numdf;                           
03109   int dendf;                           
03110   float fval;                          
03111   
03112   
03113   
03114   a = option_data->a;
03115   b = option_data->b;
03116   c = option_data->c;
03117   n = option_data->n;
03118   nxyz = option_data->nxyz;
03119   nvoxel = option_data->nvoxel;
03120    
03121   
03122   fac = (float *) malloc(sizeof(float)*nxyz);
03123   msac = (float *) malloc(sizeof(float)*nxyz);
03124   if ((fac == NULL) || (msac == NULL))
03125     ANOVA_error ("unable to allocate sufficient memory");
03126   
03127   
03128   volume_read ("ssac", msac, nxyz); 
03129   numdf = (a-1) * (c-1);
03130   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03131     msac[ixyz] = msac[ixyz] / numdf; 
03132   if (nvoxel > 0)
03133     printf ("MSAC = %f \n", msac[nvoxel-1]);
03134  
03135  
03136   switch (option_data->model)
03137     {
03138     case 1:
03139     case 4:
03140       volume_read ("sse", fac, nxyz); 
03141       dendf = a * b * c * (n-1);
03142       break;
03143     case 2:
03144     case 3:
03145       volume_read ("ssabc", fac, nxyz); 
03146       dendf = (a-1) * (b-1) * (c-1);
03147       break;
03148     }
03149  
03150   
03151   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03152     {
03153       fval = fac[ixyz] / dendf;              
03154       if (fval < EPSILON)
03155         fac[ixyz] = 0.0;
03156       else
03157         fac[ixyz] = msac[ixyz] / fval;
03158     }
03159   if (nvoxel > 0)
03160     printf ("F(AC) = %f \n", fac[nvoxel-1]);
03161   
03162   
03163   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03164     msac[ixyz] = sqrt(msac[ixyz]);      
03165   write_afni_data (option_data, option_data->facname, 
03166                    msac, fac, numdf, dendf);
03167   
03168   
03169   free (msac);   msac = NULL;
03170   free (fac);    fac  = NULL;
03171   
03172 }
03173 
03174 
03175 
03176 
03177 
03178 
03179 
03180 
03181 
03182 
03183 
03184 
03185 
03186 
03187 
03188 
03189 
03190 
03191 
03192 void calculate_fabc (anova_options * option_data)
03193 {
03194   const float  EPSILON = 1.0e-10;      
03195   float * msabc = NULL;                
03196   float * fabc = NULL;                 
03197   int a;                               
03198   int b;                               
03199   int c;                               
03200   int n;                               
03201   int ixyz, nxyz;                      
03202   int nvoxel;                          
03203   int numdf;                           
03204   int dendf;                           
03205   float fval;                          
03206   
03207   
03208   
03209   a = option_data->a;
03210   b = option_data->b;
03211   c = option_data->c;
03212   n = option_data->n;
03213   nxyz = option_data->nxyz;
03214   nvoxel = option_data->nvoxel;
03215    
03216   
03217   fabc = (float *) malloc(sizeof(float)*nxyz);
03218   msabc = (float *) malloc(sizeof(float)*nxyz);
03219   if ((fabc == NULL) || (msabc == NULL))
03220     ANOVA_error ("unable to allocate sufficient memory");
03221   
03222   
03223   volume_read ("ssabc", msabc, nxyz); 
03224   numdf = (a-1) * (b-1) * (c-1);
03225   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03226     msabc[ixyz] = msabc[ixyz] / numdf; 
03227   if (nvoxel > 0)
03228     printf ("MSABC = %f \n", msabc[nvoxel-1]);
03229  
03230  
03231   switch (option_data->model)
03232     {
03233     case 1:
03234     case 2:
03235     case 3:
03236     case 4:
03237       volume_read ("sse", fabc, nxyz); 
03238       dendf = a * b * c * (n-1);
03239       break;
03240     }
03241  
03242   
03243   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03244     {
03245       fval = fabc[ixyz] / dendf;              
03246       if (fval < EPSILON)
03247         fabc[ixyz] = 0.0;
03248       else
03249         fabc[ixyz] = msabc[ixyz] / fval;
03250     }
03251   if (nvoxel > 0)
03252     printf ("F(ABC) = %f \n", fabc[nvoxel-1]);
03253   
03254   
03255   for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03256     msabc[ixyz] = sqrt(msabc[ixyz]);      
03257   write_afni_data (option_data, option_data->fabcname, 
03258                    msabc, fabc, numdf, dendf);
03259   
03260   
03261   free (msabc);   msabc = NULL;
03262   free (fabc);    fabc  = NULL;
03263   
03264 }
03265 
03266 
03267 
03268 
03269 
03270 
03271 
03272 
03273 
03274 
03275 void calculate_ameans (anova_options * option_data)
03276 {
03277   const float  EPSILON = 1.0e-10;    
03278   float * mean = NULL;               
03279   float * tmean = NULL;              
03280   int a;                             
03281   int b;                             
03282   int c;                             
03283   int n;                             
03284   int ixyz, nxyz;                    
03285   int nvoxel;                        
03286   int num_means;                     
03287   int imean;                         
03288   int level;                         
03289   int df;                            
03290   float fval;                        
03291   float stddev;                      
03292   char filename[MAX_NAME_LENGTH];    
03293  
03294 
03295   
03296   a = option_data->a;
03297   b = option_data->b;
03298   c = option_data->c;
03299   n = option_data->n;
03300   num_means = option_data->num_ameans;
03301   nxyz = option_data->nxyz;
03302   nvoxel = option_data->nvoxel;
03303 
03304   
03305   mean = (float *) malloc(sizeof(float)*nxyz);
03306   tmean = (float *) malloc(sizeof(float)*nxyz);
03307   if ((mean == NULL) || (tmean == NULL))  
03308     ANOVA_error ("unable to allocate sufficient memory");
03309    
03310    
03311   for (imean = 0;  imean < num_means;  imean++)
03312     {
03313       level = option_data->ameans[imean];
03314       
03315       
03316       calculate_sum (option_data, level, -1, -1, mean);
03317       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03318         mean[ixyz] = mean[ixyz] / (b*c*n);
03319       if (nvoxel > 0)
03320         printf ("Mean of factor A level %d = %f \n", level+1, mean[nvoxel-1]);
03321       
03322       
03323       switch (option_data->model)
03324         {
03325         case 1:
03326           volume_read ("sse", tmean, nxyz); 
03327           df = a * b * c * (n-1);
03328           break;
03329         case 4:
03330           volume_read ("ssac", tmean, nxyz); 
03331           df = (a-1) * (c-1);
03332           break;
03333         case 5:
03334           volume_read ("ssca", tmean, nxyz); 
03335           df = a * (c-1);
03336           break;
03337         }
03338 
03339       
03340       fval = (1.0 / df) * (1.0 / (b*c*n));
03341       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03342         {
03343           stddev =  sqrt(tmean[ixyz] * fval);
03344           if (stddev < EPSILON)
03345             tmean[ixyz] = 0.0;
03346           else
03347             tmean[ixyz] = mean[ixyz] / stddev;
03348         }
03349       
03350       if (nvoxel > 0)
03351         printf ("t for mean of factor A level %d = %f \n", 
03352                 level+1, tmean[nvoxel-1]);
03353       
03354       
03355       write_afni_data (option_data, option_data->amname[imean], 
03356                        mean, tmean, df, 0);
03357       
03358     }
03359   
03360   
03361   free (tmean);   tmean = NULL;
03362   free (mean);    mean = NULL;
03363 }
03364 
03365 
03366 
03367 
03368 
03369 
03370 
03371 
03372 
03373 void calculate_bmeans (anova_options * option_data)
03374 {
03375   const float  EPSILON = 1.0e-10;    
03376   float * mean = NULL;               
03377   float * tmean = NULL;              
03378   int a;                             
03379   int b;                             
03380   int c;                             
03381   int n;                             
03382   int ixyz, nxyz;                    
03383   int nvoxel;                        
03384   int nt;                            
03385   int num_means;                     
03386   int imean;                         
03387   int level;                         
03388   int df;                            
03389   float fval;                        
03390   float stddev;                      
03391   char filename[MAX_NAME_LENGTH];    
03392  
03393 
03394   
03395   a = option_data->a;
03396   b = option_data->b;
03397   c = option_data->c;
03398   n = option_data->n;
03399   nt = option_data->nt;
03400   num_means = option_data->num_bmeans;
03401   nxyz = option_data->nxyz;
03402   nvoxel = option_data->nvoxel;
03403 
03404   
03405   mean = (float *) malloc(sizeof(float)*nxyz);
03406   tmean = (float *) malloc(sizeof(float)*nxyz);
03407   if ((mean == NULL) || (tmean == NULL))  
03408     ANOVA_error ("unable to allocate sufficient memory");
03409    
03410    
03411   for (imean = 0;  imean < num_means;  imean++)
03412     {
03413       level = option_data->bmeans[imean];
03414       
03415       
03416       calculate_sum (option_data, -1, level, -1, mean);
03417       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03418         mean[ixyz] = mean[ixyz] / (a*c*n);
03419       if (nvoxel > 0)
03420         printf ("Mean of factor B level %d = %f \n", level+1, mean[nvoxel-1]);
03421       
03422       
03423       switch (option_data->model)
03424         {
03425         case 1:
03426           volume_read ("sse", tmean, nxyz); 
03427           df = a * b * c * (n-1);
03428           break;
03429         case 4:
03430           volume_read ("ssbc", tmean, nxyz); 
03431           df = (b-1) * (c-1);
03432           break;
03433         case 5:
03434           volume_read ("ssbca", tmean, nxyz); 
03435           df = a * (b-1) * (c-1);
03436           break;
03437         }
03438 
03439       
03440       fval = (1.0 / df) * (1.0 / (a*c*n));
03441       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03442         {
03443           stddev =  sqrt(tmean[ixyz] * fval);
03444           if (stddev < EPSILON)
03445             tmean[ixyz] = 0.0;
03446           else
03447             tmean[ixyz] = mean[ixyz] / stddev;
03448         }
03449       if (nvoxel > 0)
03450         printf ("t for mean of factor B level %d = %f \n", 
03451                 level+1, tmean[nvoxel-1]);
03452       
03453       
03454       write_afni_data (option_data, option_data->bmname[imean], 
03455                        mean, tmean, df, 0);
03456       
03457     }
03458   
03459   
03460   free (tmean);   tmean = NULL;
03461   free (mean);    mean = NULL;
03462 }
03463 
03464 
03465 
03466 
03467 
03468 
03469 
03470 
03471 
03472 
03473 void calculate_cmeans (anova_options * option_data)
03474 {
03475   const float  EPSILON = 1.0e-10;    
03476   float * mean = NULL;               
03477   float * tmean = NULL;              
03478   int a;                             
03479   int b;                             
03480   int c;                             
03481   int n;                             
03482   int ixyz, nxyz;                    
03483   int nvoxel;                        
03484   int nt;                            
03485   int num_means;                     
03486   int imean;                         
03487   int level;                         
03488   int df;                            
03489   float fval;                        
03490   float stddev;                      
03491   char filename[MAX_NAME_LENGTH];    
03492  
03493 
03494   
03495   a = option_data->a;
03496   b = option_data->b;
03497   c = option_data->c;
03498   n = option_data->n;
03499   df = a * b * c * (n-1);
03500   nt = option_data->nt;
03501   num_means = option_data->num_cmeans;
03502   nxyz = option_data->nxyz;
03503   nvoxel = option_data->nvoxel;
03504 
03505   
03506   mean = (float *) malloc(sizeof(float)*nxyz);
03507   tmean = (float *) malloc(sizeof(float)*nxyz);
03508   if ((mean == NULL) || (tmean == NULL))  
03509     ANOVA_error ("unable to allocate sufficient memory");
03510    
03511    
03512   for (imean = 0;  imean < num_means;  imean++)
03513     {
03514       level = option_data->cmeans[imean];
03515       
03516       
03517       calculate_sum (option_data, -1, -1, level, mean);
03518       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03519         mean[ixyz] = mean[ixyz] / (a*b*n);
03520       if (nvoxel > 0)
03521         printf ("Mean of factor C level %d = %f \n", level+1, mean[nvoxel-1]);
03522       
03523       
03524       volume_read ("sse", tmean, nxyz); 
03525       fval = (1.0 / df) * (1.0 / (a*b*n));
03526       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03527         {
03528           stddev =  sqrt(tmean[ixyz] * fval);
03529           if (stddev < EPSILON)
03530             tmean[ixyz] = 0.0;
03531           else
03532             tmean[ixyz] = mean[ixyz] / stddev;
03533         }
03534       if (nvoxel > 0)
03535         printf ("t for mean of factor C level %d = %f \n", 
03536                 level+1, tmean[nvoxel-1]);
03537       
03538       
03539       write_afni_data (option_data, option_data->cmname[imean], 
03540                        mean, tmean, df, 0);
03541       
03542     }
03543   
03544   
03545   free (tmean);   tmean = NULL;
03546   free (mean);    mean = NULL;
03547 }
03548 
03549 
03550 
03551 
03552 
03553 
03554 
03555 
03556 
03557 
03558 void calculate_xmeans (anova_options * option_data)
03559 {
03560   const float  EPSILON = 1.0e-10;    
03561   float * mean = NULL;               
03562   float * tmean = NULL;              
03563   int a;                             
03564   int b;                             
03565   int c;                             
03566   int n;                             
03567   int ixyz, nxyz;                    
03568   int nvoxel;                        
03569   int nt;                            
03570   int num_means;                     
03571   int imean;                         
03572   int alevel, blevel, clevel;        
03573   int df;                            
03574   float fval;                        
03575   float stddev;                      
03576   char filename[MAX_NAME_LENGTH];    
03577  
03578 
03579   
03580   a = option_data->a;
03581   b = option_data->b;
03582   c = option_data->c;
03583   n = option_data->n;
03584   df = a * b * c * (n-1);
03585   nt = option_data->nt;
03586   num_means = option_data->num_xmeans;
03587   nxyz = option_data->nxyz;
03588   nvoxel = option_data->nvoxel;
03589 
03590   
03591   mean = (float *) malloc(sizeof(float)*nxyz);
03592   tmean = (float *) malloc(sizeof(float)*nxyz);
03593   if ((mean == NULL) || (tmean == NULL))  
03594     ANOVA_error ("unable to allocate sufficient memory");
03595    
03596    
03597   for (imean = 0;  imean < num_means;  imean++)
03598     {
03599       alevel = option_data->xmeans[imean][0];
03600       blevel = option_data->xmeans[imean][1];
03601       clevel = option_data->xmeans[imean][2];
03602       
03603       
03604       calculate_sum (option_data, alevel, blevel, clevel, mean);
03605       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03606         mean[ixyz] = mean[ixyz] / n;
03607       if (nvoxel > 0)
03608         printf ("Mean of Cell[%d][%d][%d] = %f \n", 
03609                 alevel+1, blevel+1, clevel+1, mean[nvoxel-1]);
03610       
03611       
03612       volume_read ("sse", tmean, nxyz); 
03613       fval = (1.0 / df) * (1.0 / n);
03614       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03615         {
03616           stddev =  sqrt(tmean[ixyz] * fval);
03617           if (stddev < EPSILON)
03618             tmean[ixyz] = 0.0;
03619           else
03620             tmean[ixyz] = mean[ixyz] / stddev;
03621         }
03622       if (nvoxel > 0)
03623         printf ("t-stat for Mean of Cell[%d][%d][%d] = %f \n", 
03624                 alevel+1, blevel+1, clevel+1, tmean[nvoxel-1]);
03625       
03626       
03627       write_afni_data (option_data, option_data->xmname[imean], 
03628                        mean, tmean, df, 0);
03629       
03630     }
03631   
03632   
03633   free (tmean);   tmean = NULL;
03634   free (mean);    mean = NULL;
03635 }
03636 
03637 
03638 
03639 
03640 
03641 
03642 
03643 
03644 
03645 
03646 void calculate_adifferences (anova_options * option_data)
03647 {
03648   const float  EPSILON = 1.0e-10;     
03649   float * diff = NULL;                
03650   float * tdiff = NULL;               
03651   int a;                              
03652   int b;                              
03653   int c;                              
03654   int n;                              
03655   int ixyz, nxyz;                     
03656   int nvoxel;                         
03657   int num_diffs;                      
03658   int idiff;                          
03659   int i, j;                           
03660   int df;                             
03661   float fval;                         
03662   float stddev;                       
03663   char filename[MAX_NAME_LENGTH];     
03664   
03665   
03666   
03667   a = option_data->a;
03668   b = option_data->b;
03669   c = option_data->c;
03670   n = option_data->n;
03671   num_diffs = option_data->num_adiffs;
03672   nxyz = option_data->nxyz;
03673   nvoxel = option_data->nvoxel;
03674   
03675   
03676   diff = (float *) malloc(sizeof(float)*nxyz);
03677   tdiff = (float *) malloc(sizeof(float)*nxyz);
03678   if ((diff == NULL) || (tdiff == NULL))
03679     ANOVA_error ("unable to allocate sufficient memory");
03680   
03681   
03682   for (idiff = 0;  idiff < num_diffs;  idiff++)
03683     {
03684 
03685       
03686       i = option_data->adiffs[idiff][0];
03687       calculate_sum (option_data, i, -1, -1, diff);
03688       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03689         diff[ixyz] = diff[ixyz] / (b*c*n);
03690       
03691       
03692       j = option_data->adiffs[idiff][1];
03693       calculate_sum (option_data, j, -1, -1, tdiff);
03694       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03695         diff[ixyz] -= tdiff[ixyz] / (b*c*n);
03696       if (nvoxel > 0)
03697         printf ("Difference of factor A level %d - level %d = %f \n", 
03698                 i+1, j+1, diff[nvoxel-1]);
03699 
03700       
03701       switch (option_data->model)
03702         {
03703         case 1:
03704           volume_read ("sse", tdiff, nxyz); 
03705           df = a * b * c * (n-1);
03706           break;
03707         case 4:
03708           volume_read ("ssac", tdiff, nxyz); 
03709           df = (a-1) * (c-1);
03710           break;
03711         case 5:
03712           volume_read ("ssca", tdiff, nxyz); 
03713           df = a * (c-1);
03714           break;
03715         }
03716       
03717         
03718       fval = (1.0 / df) * (2.0 / (b*c*n));
03719       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03720         {
03721           stddev = sqrt (tdiff[ixyz] * fval);
03722           if (stddev < EPSILON)
03723             tdiff[ixyz] = 0.0;
03724           else
03725             tdiff[ixyz] = diff[ixyz] / stddev;
03726         } 
03727       
03728       if (nvoxel > 0)
03729         printf ("t for difference of factor A level %d - level %d = %f \n", 
03730                 i+1, j+1, tdiff[nvoxel-1]);
03731       
03732       
03733       write_afni_data (option_data, option_data->adname[idiff], 
03734                        diff, tdiff, df, 0);
03735       
03736     }
03737   
03738   
03739   free (tdiff);   tdiff = NULL;
03740   free (diff);    diff = NULL;
03741   
03742 }
03743 
03744 
03745 
03746 
03747 
03748 
03749 
03750 
03751 
03752 
03753 void calculate_bdifferences (anova_options * option_data)
03754 {
03755   const float  EPSILON = 1.0e-10;     
03756   float * diff = NULL;                
03757   float * tdiff = NULL;               
03758   int a;                              
03759   int b;                              
03760   int c;                              
03761   int n;                              
03762   int ixyz, nxyz;                     
03763   int nvoxel;                         
03764   int num_diffs;                      
03765   int idiff;                          
03766   int i, j;                           
03767   int df;                             
03768   float fval;                         
03769   float stddev;                       
03770   char filename[MAX_NAME_LENGTH];     
03771   
03772   
03773   
03774   a = option_data->a;
03775   b = option_data->b;
03776   c = option_data->c;
03777   n = option_data->n;
03778   num_diffs = option_data->num_bdiffs;
03779   nxyz = option_data->nxyz;
03780   nvoxel = option_data->nvoxel;
03781   
03782   
03783   diff = (float *) malloc(sizeof(float)*nxyz);
03784   tdiff = (float *) malloc(sizeof(float)*nxyz);
03785   if ((diff == NULL) || (tdiff == NULL))
03786     ANOVA_error ("unable to allocate sufficient memory");
03787   
03788   
03789   for (idiff = 0;  idiff < num_diffs;  idiff++)
03790     {
03791 
03792       
03793       i = option_data->bdiffs[idiff][0];
03794       calculate_sum (option_data, -1, i, -1, diff);
03795       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03796         diff[ixyz] = diff[ixyz] / (a*c*n);
03797       
03798       
03799       j = option_data->bdiffs[idiff][1];
03800       calculate_sum (option_data, -1, j, -1, tdiff);
03801       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03802         diff[ixyz] -= tdiff[ixyz] / (a*c*n);
03803       if (nvoxel > 0)
03804         printf ("Difference of factor B level %d - level %d = %f \n", 
03805                 i+1, j+1, diff[nvoxel-1]);
03806 
03807       
03808       switch (option_data->model)
03809         {
03810         case 1:
03811           volume_read ("sse", tdiff, nxyz); 
03812           df = a * b * c * (n-1);
03813           break;
03814         case 4:
03815           volume_read ("ssbc", tdiff, nxyz); 
03816           df = (b-1) * (c-1);
03817           break;
03818         case 5:
03819           volume_read ("ssbca", tdiff, nxyz); 
03820           df = a * (b-1) * (c-1);
03821           break;
03822         }
03823 
03824       
03825       fval = (1.0 / df) * (2.0 / (a*c*n));
03826       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03827         {
03828           stddev = sqrt (tdiff[ixyz] * fval);
03829           if (stddev < EPSILON)
03830             tdiff[ixyz] = 0.0;
03831           else
03832             tdiff[ixyz] = diff[ixyz] / stddev;
03833         } 
03834       
03835       if (nvoxel > 0)
03836         printf ("t for difference of factor B level %d - level %d = %f \n", 
03837                 i+1, j+1, tdiff[nvoxel-1]);
03838       
03839       
03840       write_afni_data (option_data, option_data->bdname[idiff], 
03841                        diff, tdiff, df, 0);
03842       
03843     }
03844   
03845   
03846   free (tdiff);   tdiff = NULL;
03847   free (diff);    diff = NULL;
03848   
03849 }
03850 
03851 
03852 
03853 
03854 
03855 
03856 
03857 
03858 
03859 
03860 void calculate_cdifferences (anova_options * option_data)
03861 {
03862   const float  EPSILON = 1.0e-10;     
03863   float * diff = NULL;                
03864   float * tdiff = NULL;               
03865   int a;                              
03866   int b;                              
03867   int c;                              
03868   int n;                              
03869   int ixyz, nxyz;                     
03870   int nvoxel;                         
03871   int num_diffs;                      
03872   int idiff;                          
03873   int i, j;                           
03874   int df;                             
03875   float fval;                         
03876   float stddev;                       
03877   char filename[MAX_NAME_LENGTH];     
03878   
03879   
03880   
03881   a = option_data->a;
03882   b = option_data->b;
03883   c = option_data->c;
03884   n = option_data->n;
03885   num_diffs = option_data->num_cdiffs;
03886   nxyz = option_data->nxyz;
03887   nvoxel = option_data->nvoxel;
03888   
03889   
03890   diff = (float *) malloc(sizeof(float)*nxyz);
03891   tdiff = (float *) malloc(sizeof(float)*nxyz);
03892   if ((diff == NULL) || (tdiff == NULL))
03893     ANOVA_error ("unable to allocate sufficient memory");
03894   
03895   
03896   for (idiff = 0;  idiff < num_diffs;  idiff++)
03897     {
03898 
03899       
03900       i = option_data->cdiffs[idiff][0];
03901       calculate_sum (option_data, -1, -1, i, diff);
03902       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03903         diff[ixyz] = diff[ixyz] / (a*b*n);
03904       
03905       
03906       j = option_data->cdiffs[idiff][1];
03907       calculate_sum (option_data, -1, -1, j, tdiff);
03908       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03909         diff[ixyz] -= tdiff[ixyz] / (a*b*n);
03910       if (nvoxel > 0)
03911         printf ("Difference of factor C level %d - level %d = %f \n", 
03912                 i+1, j+1, diff[nvoxel-1]);
03913 
03914       
03915       volume_read ("sse", tdiff, nxyz); 
03916       df = a * b * c * (n-1);
03917       
03918       
03919       fval = (1.0 / df) * (2.0 / (a*b*n));
03920       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
03921         {
03922           stddev = sqrt (tdiff[ixyz] * fval);
03923           if (stddev < EPSILON)
03924             tdiff[ixyz] = 0.0;
03925           else
03926             tdiff[ixyz] = diff[ixyz] / stddev;
03927         } 
03928       
03929       if (nvoxel > 0)
03930         printf ("t for difference of factor C level %d - level %d = %f \n", 
03931                 i+1, j+1, tdiff[nvoxel-1]);
03932       
03933       
03934       write_afni_data (option_data, option_data->cdname[idiff], 
03935                        diff, tdiff, df, 0);
03936       
03937     }
03938   
03939   
03940   free (tdiff);   tdiff = NULL;
03941   free (diff);    diff = NULL;
03942   
03943 }
03944 
03945 
03946 
03947 
03948 
03949 
03950 
03951 
03952 
03953 
03954 void calculate_xdifferences (anova_options * option_data)
03955 {
03956   const float  EPSILON = 1.0e-10;     
03957   float * diff = NULL;                
03958   float * tdiff = NULL;               
03959   int a;                              
03960   int b;                              
03961   int c;                              
03962   int n;                              
03963   int ixyz, nxyz;                     
03964   int nvoxel;                         
03965   int num_diffs;                      
03966   int idiff;                          
03967   int ia, ib, ic, ja, jb, jc;         
03968   int df;                             
03969   float fval;                         
03970   float stddev;                       
03971   char filename[MAX_NAME_LENGTH];     
03972   
03973   
03974   
03975   a = option_data->a;
03976   b = option_data->b;
03977   c = option_data->c;
03978   n = option_data->n;
03979   df = a * b * c * (n-1);
03980   num_diffs = option_data->num_xdiffs;
03981   nxyz = option_data->nxyz;
03982   nvoxel = option_data->nvoxel;
03983   
03984   
03985   diff = (float *) malloc(sizeof(float)*nxyz);
03986   tdiff = (float *) malloc(sizeof(float)*nxyz);
03987   if ((diff == NULL) || (tdiff == NULL))
03988     ANOVA_error ("unable to allocate sufficient memory");
03989   
03990   
03991   for (idiff = 0;  idiff < num_diffs;  idiff++)
03992     {
03993 
03994       
03995       ia = option_data->xdiffs[idiff][0][0];
03996       ib = option_data->xdiffs[idiff][0][1];
03997       ic = option_data->xdiffs[idiff][0][2];
03998       calculate_sum (option_data, ia, ib, ic, diff);
03999       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04000         diff[ixyz] = diff[ixyz] / n;
04001       
04002       
04003       ja = option_data->xdiffs[idiff][1][0];
04004       jb = option_data->xdiffs[idiff][1][1];
04005       jc = option_data->xdiffs[idiff][1][2];
04006       calculate_sum (option_data, ja, jb, jc, tdiff);
04007       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04008         diff[ixyz] -= tdiff[ixyz] / n;
04009       if (nvoxel > 0)
04010         printf ("Difference Cell[%d][%d][%d] - Cell[%d][%d][%d] = %f \n", 
04011                 ia+1, ib+1, ic+1, ja+1, jb+1, jc+1, diff[nvoxel-1]);
04012 
04013       
04014       volume_read ("sse", tdiff, nxyz); 
04015       fval = (1.0 / df) * (2.0 / n);
04016       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04017         {
04018           stddev = sqrt (tdiff[ixyz] * fval);
04019           if (stddev < EPSILON)
04020             tdiff[ixyz] = 0.0;
04021           else
04022             tdiff[ixyz] = diff[ixyz] / stddev;
04023         } 
04024       
04025       if (nvoxel > 0)
04026         printf ("t-stat for Cell[%d][%d][%d] - Cell[%d][%d][%d] = %f \n", 
04027                 ia+1, ib+1, ic+1, ja+1, jb+1, jc+1, tdiff[nvoxel-1]);
04028       
04029       
04030       write_afni_data (option_data, option_data->xdname[idiff], 
04031                        diff, tdiff, df, 0);
04032       
04033     }
04034   
04035   
04036   free (tdiff);   tdiff = NULL;
04037   free (diff);    diff = NULL;
04038   
04039 }
04040 
04041 
04042 
04043 
04044 
04045 
04046 
04047 
04048 
04049 
04050 void calculate_acontrasts (anova_options * option_data)
04051 {
04052   const float  EPSILON = 1.0e-10;     
04053   float * contr = NULL;               
04054   float * tcontr = NULL;              
04055   int a;                              
04056   int b;                              
04057   int c;                              
04058   int n;                              
04059   int ixyz, nxyz;                     
04060   int nvoxel;                         
04061   int num_contr;                      
04062   int icontr;                         
04063   int level;                          
04064   int df;                             
04065   float fval;                         
04066   float coef;                         
04067   float stddev;                       
04068   char filename[MAX_NAME_LENGTH];     
04069   
04070   
04071   
04072   a = option_data->a;
04073   b = option_data->b;
04074   c = option_data->c;
04075   n = option_data->n;
04076   num_contr = option_data->num_acontr;
04077   nxyz = option_data->nxyz;
04078   nvoxel = option_data->nvoxel;
04079   
04080   
04081   contr  = (float *) malloc(sizeof(float)*nxyz);
04082   tcontr = (float *) malloc(sizeof(float)*nxyz);
04083   if ((contr == NULL) || (tcontr == NULL))
04084     ANOVA_error ("unable to allocate sufficient memory");
04085   
04086   
04087   
04088   for (icontr = 0;  icontr < num_contr;  icontr++)
04089     {
04090       volume_zero (contr, nxyz);
04091       fval = 0.0;
04092       
04093       for (level = 0;  level < a;  level++)
04094         {
04095           coef = option_data->acontr[icontr][level]; 
04096           if (coef == 0.0) continue; 
04097           
04098           
04099           calculate_sum (option_data, level, -1, -1, tcontr);
04100           fval += coef * coef / (b*c*n);
04101           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04102             contr[ixyz] += coef * tcontr[ixyz] / (b*c*n);
04103         }
04104 
04105       if (nvoxel > 0)
04106         printf ("No.%d contrast for factor A = %f \n", 
04107                 icontr+1, contr[nvoxel-1]);
04108       
04109       
04110       switch (option_data->model)
04111         {
04112         case 1:
04113           volume_read ("sse", tcontr, nxyz); 
04114           df = a * b * c * (n-1);
04115           break;
04116         case 4:
04117           volume_read ("ssac", tcontr, nxyz); 
04118           df = (a-1) * (c-1);
04119           break;
04120         case 5:
04121           volume_read ("ssca", tcontr, nxyz); 
04122           df = a * (c-1);
04123           break;
04124         }
04125 
04126       
04127       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04128         {
04129           stddev = sqrt ((tcontr[ixyz] / df) * fval);
04130           if (stddev < EPSILON)
04131             tcontr[ixyz] = 0.0;
04132           else
04133             tcontr[ixyz] = contr[ixyz] / stddev;
04134         }   
04135       
04136       if (nvoxel > 0)
04137         printf ("t of No.%d contrast for factor A = %f \n", 
04138                 icontr+1, tcontr[nvoxel-1]);
04139       
04140       
04141       write_afni_data (option_data, option_data->acname[icontr], 
04142                        contr, tcontr, df, 0);
04143       
04144     }
04145   
04146   
04147   free (tcontr);   tcontr = NULL;
04148   free (contr);    contr = NULL;
04149   
04150 }
04151 
04152 
04153 
04154 
04155 
04156 
04157 
04158 
04159 
04160 void calculate_bcontrasts (anova_options * option_data)
04161 {
04162   const float  EPSILON = 1.0e-10;     
04163   float * contr = NULL;               
04164   float * tcontr = NULL;              
04165   int a;                              
04166   int b;                              
04167   int c;                              
04168   int n;                              
04169   int ixyz, nxyz;                     
04170   int nvoxel;                         
04171   int num_contr;                      
04172   int icontr;                         
04173   int level;                          
04174   int df;                             
04175   float fval;                         
04176   float coef;                         
04177   float stddev;                       
04178   char filename[MAX_NAME_LENGTH];     
04179   
04180   
04181   
04182   a = option_data->a;
04183   b = option_data->b;
04184   c = option_data->c;
04185   n = option_data->n;
04186   num_contr = option_data->num_bcontr;
04187   nxyz = option_data->nxyz;
04188   nvoxel = option_data->nvoxel;
04189   
04190   
04191   contr  = (float *) malloc(sizeof(float)*nxyz);
04192   tcontr = (float *) malloc(sizeof(float)*nxyz);
04193   if ((contr == NULL) || (tcontr == NULL))
04194     ANOVA_error ("unable to allocate sufficient memory");
04195   
04196   
04197   
04198   for (icontr = 0;  icontr < num_contr;  icontr++)
04199     {
04200       volume_zero (contr, nxyz);
04201       fval = 0.0;
04202       
04203       for (level = 0;  level < b;  level++)
04204         {
04205           coef = option_data->bcontr[icontr][level]; 
04206           if (coef == 0.0) continue; 
04207           
04208           
04209           calculate_sum (option_data, -1, level, -1, tcontr);
04210           fval += coef * coef / (a*c*n);
04211           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04212             contr[ixyz] += coef * tcontr[ixyz] / (a*c*n);
04213         }
04214 
04215       if (nvoxel > 0)
04216         printf ("No.%d contrast for factor B = %f \n", 
04217                 icontr+1, contr[nvoxel-1]);
04218       
04219       
04220       switch (option_data->model)
04221         {
04222         case 1:
04223           volume_read ("sse", tcontr, nxyz); 
04224           df = a * b * c * (n-1);
04225           break;
04226         case 4:
04227           volume_read ("ssbc", tcontr, nxyz); 
04228           df = (b-1) * (c-1);
04229           break;
04230         case 5:
04231           volume_read ("ssbca", tcontr, nxyz); 
04232           df = a * (b-1) * (c-1);
04233           break;
04234         }
04235 
04236       
04237       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04238         {
04239           stddev = sqrt ((tcontr[ixyz] / df) * fval);
04240           if (stddev < EPSILON)
04241             tcontr[ixyz] = 0.0;
04242           else
04243             tcontr[ixyz] = contr[ixyz] / stddev;
04244         }   
04245       
04246       if (nvoxel > 0)
04247         printf ("t of No.%d contrast for factor B = %f \n", 
04248                 icontr+1, tcontr[nvoxel-1]);
04249       
04250       
04251       write_afni_data (option_data, option_data->bcname[icontr], 
04252                        contr, tcontr, df, 0);
04253       
04254     }
04255   
04256   
04257   free (tcontr);   tcontr = NULL;
04258   free (contr);    contr = NULL;
04259   
04260 }
04261 
04262 
04263 
04264 
04265 
04266 
04267 
04268 
04269 
04270 void calculate_ccontrasts (anova_options * option_data)
04271 {
04272   const float  EPSILON = 1.0e-10;     
04273   float * contr = NULL;               
04274   float * tcontr = NULL;              
04275   int a;                              
04276   int b;                              
04277   int c;                              
04278   int n;                              
04279   int ixyz, nxyz;                     
04280   int nvoxel;                         
04281   int num_contr;                      
04282   int icontr;                         
04283   int level;                          
04284   int df;                             
04285   float fval;                         
04286   float coef;                         
04287   float stddev;                       
04288   char filename[MAX_NAME_LENGTH];     
04289   
04290   
04291   
04292   a = option_data->a;
04293   b = option_data->b;
04294   c = option_data->c;
04295   n = option_data->n;
04296   num_contr = option_data->num_ccontr;
04297   nxyz = option_data->nxyz;
04298   nvoxel = option_data->nvoxel;
04299   
04300   
04301   contr  = (float *) malloc(sizeof(float)*nxyz);
04302   tcontr = (float *) malloc(sizeof(float)*nxyz);
04303   if ((contr == NULL) || (tcontr == NULL))
04304     ANOVA_error ("unable to allocate sufficient memory");
04305   
04306   
04307   
04308   for (icontr = 0;  icontr < num_contr;  icontr++)
04309     {
04310       volume_zero (contr, nxyz);
04311       fval = 0.0;
04312       
04313       for (level = 0;  level < c;  level++)
04314         {
04315           coef = option_data->ccontr[icontr][level]; 
04316           if (coef == 0.0) continue; 
04317           
04318           
04319           calculate_sum (option_data, -1, -1, level, tcontr);
04320           fval += coef * coef / (a*b*n);
04321           for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04322             contr[ixyz] += coef * tcontr[ixyz] / (a*b*n);
04323         }
04324 
04325       if (nvoxel > 0)
04326         printf ("No.%d contrast for factor C = %f \n", 
04327                 icontr+1, contr[nvoxel-1]);
04328       
04329       
04330       volume_read ("sse", tcontr, nxyz); 
04331       df = a * b * c * (n-1);
04332 
04333       
04334       for (ixyz = 0;  ixyz < nxyz;  ixyz++)
04335         {
04336           stddev = sqrt ((tcontr[ixyz] / df) * fval);
04337           if (stddev < EPSILON)
04338             tcontr[ixyz] = 0.0;
04339           else
04340             tcontr[ixyz] = contr[ixyz] / stddev;
04341         }   
04342       
04343       if (nvoxel > 0)
04344         printf ("t of No.%d contrast for factor C = %f \n", 
04345                 icontr+1, tcontr[nvoxel-1]);
04346       
04347       
04348       write_afni_data (option_data, option_data->ccname[icontr], 
04349                        contr, tcontr, df, 0);
04350       
04351     }
04352   
04353   
04354   free (tcontr);   tcontr = NULL;
04355   free (contr);    contr = NULL;
04356   
04357 }
04358 
04359 
04360 
04361 
04362 
04363 
04364 void calculate_anova (anova_options * option_data)
04365 {
04366 
04367   
04368   calculate_ss0 (option_data);
04369   calculate_ssi (option_data);
04370   calculate_ssj (option_data);
04371   if (option_data->model != 5)  calculate_ssk (option_data);
04372   calculate_ssij (option_data);
04373   calculate_ssik (option_data);
04374   if (option_data->model != 5)  calculate_ssjk (option_data);
04375   calculate_ssijk (option_data);
04376   if (option_data->n != 1)  calculate_ssijkm (option_data);
04377 
04378 
04379   
04380   calculate_ssto (option_data);
04381   if (option_data->n != 1)  volume_delete ("ssijkm");
04382   
04383   
04384   if (option_data->n != 1)  calculate_sse (option_data);
04385   volume_delete ("ssijk");
04386   
04387   
04388   calculate_ssa (option_data);
04389   volume_delete ("ssi");
04390   
04391   
04392   calculate_ssb (option_data);
04393   volume_delete ("ssj");
04394   
04395   if (option_data->model != 5)
04396     {
04397       
04398       calculate_ssc (option_data);
04399       volume_delete ("ssk");
04400     }
04401   
04402   
04403   calculate_ssab (option_data);
04404   volume_delete ("ssij");
04405   
04406   if (option_data->model != 5)
04407     
04408     calculate_ssac (option_data);
04409   else
04410     
04411     calculate_ssca (option_data);
04412   volume_delete ("ssik");
04413   
04414   if (option_data->model != 5)
04415     {
04416       
04417       calculate_ssbc (option_data);
04418       volume_delete ("ssjk");
04419     }
04420   
04421   volume_delete ("ss0");
04422   
04423   if (option_data->model != 5)
04424     
04425     calculate_ssabc (option_data);
04426   else
04427     
04428     calculate_ssbca (option_data);
04429   volume_delete ("ssto");
04430 }
04431 
04432 
04433 
04434 
04435 
04436 
04437 
04438 void analyze_results (anova_options * option_data)
04439 {
04440 
04441    
04442    if (option_data->nfa)  calculate_fa (option_data);
04443 
04444    
04445    if (option_data->nfb)  calculate_fb (option_data);
04446 
04447    
04448    if (option_data->nfc)  calculate_fc (option_data);
04449 
04450    
04451    if (option_data->nfab)  calculate_fab (option_data);
04452 
04453    
04454    if (option_data->nfac)  calculate_fac (option_data);
04455 
04456    
04457    if (option_data->nfbc)  calculate_fbc (option_data);
04458 
04459    
04460    if (option_data->nfabc)  calculate_fabc (option_data);
04461 
04462    
04463    if (option_data->num_ameans)  calculate_ameans (option_data);
04464 
04465    
04466    if (option_data->num_bmeans)  calculate_bmeans (option_data);
04467 
04468    
04469    if (option_data->num_cmeans)  calculate_cmeans (option_data);
04470 
04471    
04472    if (option_data->num_xmeans)  calculate_xmeans (option_data);
04473 
04474    
04475    if (option_data->num_adiffs)  calculate_adifferences (option_data);
04476 
04477    
04478    if (option_data->num_bdiffs)  calculate_bdifferences (option_data);
04479 
04480    
04481    if (option_data->num_cdiffs)  calculate_cdifferences (option_data);
04482 
04483    
04484    if (option_data->num_xdiffs)  calculate_xdifferences (option_data);
04485 
04486    
04487    if (option_data->num_acontr)  calculate_acontrasts (option_data);
04488 
04489    
04490    if (option_data->num_bcontr)  calculate_bcontrasts (option_data);
04491 
04492    
04493    if (option_data->num_ccontr)  calculate_ccontrasts (option_data);
04494 
04495 }
04496 
04497 
04498 
04499 
04500 
04501 
04502 
04503 void create_bucket (anova_options * option_data)
04504 
04505 {
04506   char bucket_str[10000];             
04507   char refit_str[10000];              
04508   THD_3dim_dataset * dset=NULL;       
04509   THD_3dim_dataset * new_dset=NULL;   
04510   int i;                              
04511   int ibrick;                         
04512   char str[100];                      
04513 
04514 
04515   
04516   dset = THD_open_dataset (option_data->first_dataset) ;
04517   if( ! ISVALID_3DIM_DATASET(dset) ){
04518     fprintf(stderr,"*** Unable to open dataset file %s\n",
04519             option_data->first_dataset);
04520     exit(1) ;
04521   }
04522 
04523        if( DSET_IS_1D(dset) ) USE_1D_filenames(1) ; 
04524   else if( DSET_IS_3D(dset) ) USE_1D_filenames(3) ; 
04525   
04526 
04527   
04528   new_dset = EDIT_empty_copy( dset ) ;
04529   THD_delete_3dim_dataset (dset , False);   dset = NULL;
04530   EDIT_dset_items (new_dset, 
04531                    ADN_directory_name, option_data->session,
04532                    ADN_none);
04533 
04534   
04535   
04536   strcpy (bucket_str, "3dbucket");
04537   strcat (bucket_str, " -prefix ");
04538   strcat (bucket_str, option_data->bucket_filename);
04539 
04540 
04541   
04542   strcpy (refit_str, "3drefit ");
04543   ibrick = -1;
04544 
04545  
04546   
04547   if (option_data->nfa != 0)
04548     {
04549       add_file_name (new_dset, option_data->faname, bucket_str);
04550 
04551       ibrick++;
04552       sprintf (str, " -sublabel %d %s:Inten ",
04553                ibrick, option_data->faname);
04554       strcat (refit_str, str);
04555 
04556       ibrick++;
04557       sprintf (str, " -sublabel %d %s:F-stat ", 
04558                ibrick, option_data->faname);
04559       strcat (refit_str, str);
04560     }
04561   
04562   
04563   
04564   if (option_data->nfb != 0)
04565     {
04566       add_file_name (new_dset, option_data->fbname, bucket_str);
04567 
04568       ibrick++;
04569       sprintf (str, " -sublabel %d %s:Inten ",
04570                ibrick, option_data->fbname);
04571       strcat (refit_str, str);
04572 
04573       ibrick++;
04574       sprintf (str, " -sublabel %d %s:F-stat ", 
04575                ibrick, option_data->fbname);
04576       strcat (refit_str, str);
04577     }
04578   
04579   
04580   
04581   if (option_data->nfc != 0)
04582     {
04583       add_file_name (new_dset, option_data->fcname, bucket_str);
04584 
04585       ibrick++;
04586       sprintf (str, " -sublabel %d %s:Inten ",
04587                ibrick, option_data->fcname);
04588       strcat (refit_str, str);
04589 
04590       ibrick++;
04591       sprintf (str, " -sublabel %d %s:F-stat ", 
04592                ibrick, option_data->fcname);
04593       strcat (refit_str, str);
04594     }
04595   
04596   
04597   
04598   if (option_data->nfab != 0)
04599     {
04600       add_file_name (new_dset, option_data->fabname, bucket_str);
04601 
04602       ibrick++;
04603       sprintf (str, " -sublabel %d %s:Inten ",
04604                ibrick, option_data->fabname);
04605       strcat (refit_str, str);
04606 
04607       ibrick++;
04608       sprintf (str, " -sublabel %d %s:F-stat ", 
04609                ibrick, option_data->fabname);
04610       strcat (refit_str, str);
04611     }
04612   
04613   
04614   
04615   if (option_data->nfac != 0)
04616     {
04617       add_file_name (new_dset, option_data->facname, bucket_str);
04618 
04619       ibrick++;
04620       sprintf (str, " -sublabel %d %s:Inten ",
04621                ibrick, option_data->facname);
04622       strcat (refit_str, str);
04623 
04624       ibrick++;
04625       sprintf (str, " -sublabel %d %s:F-stat ", 
04626                ibrick, option_data->facname);
04627       strcat (refit_str, str);
04628     }
04629   
04630   
04631   
04632   if (option_data->nfbc != 0)
04633     {
04634       add_file_name (new_dset, option_data->fbcname, bucket_str);
04635 
04636       ibrick++;
04637       sprintf (str, " -sublabel %d %s:Inten ",
04638                ibrick, option_data->fbcname);
04639       strcat (refit_str, str);
04640 
04641       ibrick++;
04642       sprintf (str, " -sublabel %d %s:F-stat ", 
04643                ibrick, option_data->fbcname);
04644       strcat (refit_str, str);
04645     }
04646   
04647   
04648   
04649   if (option_data->nfabc != 0)
04650     {
04651       add_file_name (new_dset, option_data->fabcname, bucket_str);
04652 
04653       ibrick++;
04654       sprintf (str, " -sublabel %d %s:Inten ",
04655                ibrick, option_data->fabcname);
04656       strcat (refit_str, str);
04657 
04658       ibrick++;
04659       sprintf (str, " -sublabel %d %s:F-stat ", 
04660                ibrick, option_data->fabcname);
04661       strcat (refit_str, str);
04662     }
04663   
04664   
04665   
04666   if (option_data->num_ameans > 0)
04667     for (i = 0; i < option_data->num_ameans; i++)
04668       {
04669         add_file_name (new_dset, option_data->amname[i], bucket_str);
04670 
04671         ibrick++;
04672         sprintf (str, " -sublabel %d %s:Mean ", 
04673                  ibrick, option_data->amname[i]);
04674         strcat (refit_str, str);
04675 
04676         ibrick++;
04677         sprintf (str, " -sublabel %d %s:t-stat ", 
04678                  ibrick, option_data->amname[i]);
04679         strcat (refit_str, str);
04680       }
04681   
04682 
04683   
04684   if (option_data->num_bmeans > 0)
04685     for (i = 0; i < option_data->num_bmeans; i++)
04686       {
04687         add_file_name (new_dset, option_data->bmname[i], bucket_str);
04688 
04689         ibrick++;
04690         sprintf (str, " -sublabel %d %s:Mean ", 
04691                  ibrick, option_data->bmname[i]);
04692         strcat (refit_str, str);
04693 
04694         ibrick++;
04695         sprintf (str, " -sublabel %d %s:t-stat ", 
04696                  ibrick, option_data->bmname[i]);
04697         strcat (refit_str, str);
04698       }
04699   
04700 
04701   
04702   if (option_data->num_cmeans > 0)
04703     for (i = 0; i < option_data->num_cmeans; i++)
04704       {
04705         add_file_name (new_dset, option_data->cmname[i], bucket_str);
04706 
04707         ibrick++;
04708         sprintf (str, " -sublabel %d %s:Mean ", 
04709                  ibrick, option_data->cmname[i]);
04710         strcat (refit_str, str);
04711 
04712         ibrick++;
04713         sprintf (str, " -sublabel %d %s:t-stat ", 
04714                  ibrick, option_data->cmname[i]);
04715         strcat (refit_str, str);
04716       }
04717   
04718 
04719   
04720   if (option_data->num_xmeans > 0)
04721     for (i = 0; i < option_data->num_xmeans; i++)
04722       {
04723         add_file_name (new_dset, option_data->xmname[i], bucket_str);
04724 
04725         ibrick++;
04726         sprintf (str, " -sublabel %d %s:Mean ", 
04727                  ibrick, option_data->xmname[i]);
04728         strcat (refit_str, str);
04729 
04730         ibrick++;
04731         sprintf (str, " -sublabel %d %s:t-stat ", 
04732                  ibrick, option_data->xmname[i]);
04733         strcat (refit_str, str);
04734       }
04735   
04736 
04737   
04738   if (option_data->num_adiffs > 0)
04739     for (i = 0; i < option_data->num_adiffs; i++)
04740       {
04741         add_file_name (new_dset, option_data->adname[i], bucket_str);
04742 
04743         ibrick++;
04744         sprintf (str, " -sublabel %d %s:Diff ", 
04745                  ibrick, option_data->adname[i]);
04746         strcat (refit_str, str);
04747 
04748         ibrick++;
04749         sprintf (str, " -sublabel %d %s:t-stat ", 
04750                  ibrick, option_data->adname[i]);
04751         strcat (refit_str, str);
04752       }
04753   
04754 
04755   
04756   if (option_data->num_bdiffs > 0)
04757     for (i = 0; i < option_data->num_bdiffs; i++)
04758       {
04759         add_file_name (new_dset, option_data->bdname[i], bucket_str);
04760 
04761         ibrick++;
04762         sprintf (str, " -sublabel %d %s:Diff ", 
04763                  ibrick, option_data->bdname[i]);
04764         strcat (refit_str, str);
04765 
04766         ibrick++;
04767         sprintf (str, " -sublabel %d %s:t-stat ", 
04768                  ibrick, option_data->bdname[i]);
04769         strcat (refit_str, str);
04770       }
04771   
04772 
04773   
04774   if (option_data->num_cdiffs > 0)
04775     for (i = 0; i < option_data->num_cdiffs; i++)
04776       {
04777         add_file_name (new_dset, option_data->cdname[i], bucket_str);
04778 
04779         ibrick++;
04780         sprintf (str, " -sublabel %d %s:Diff ", 
04781                  ibrick, option_data->cdname[i]);
04782         strcat (refit_str, str);
04783 
04784         ibrick++;
04785         sprintf (str, " -sublabel %d %s:t-stat ", 
04786                  ibrick, option_data->cdname[i]);
04787         strcat (refit_str, str);
04788       }
04789   
04790 
04791   
04792   if (option_data->num_xdiffs > 0)
04793     for (i = 0; i < option_data->num_xdiffs; i++)
04794       {
04795         add_file_name (new_dset, option_data->xdname[i], bucket_str);
04796 
04797         ibrick++;
04798         sprintf (str, " -sublabel %d %s:Diff ", 
04799                  ibrick, option_data->xdname[i]);
04800         strcat (refit_str, str);
04801 
04802         ibrick++;
04803         sprintf (str, " -sublabel %d %s:t-stat ", 
04804                  ibrick, option_data->xdname[i]);
04805         strcat (refit_str, str);
04806       }
04807   
04808 
04809   
04810   if (option_data->num_acontr > 0)
04811     for (i = 0; i < option_data->num_acontr; i++)
04812       {
04813         add_file_name (new_dset, option_data->acname[i], bucket_str);
04814 
04815         ibrick++;
04816         sprintf (str, " -sublabel %d %s:Contr ", 
04817                  ibrick, option_data->acname[i]);
04818         strcat (refit_str, str);
04819 
04820         ibrick++;
04821         sprintf (str, " -sublabel %d %s:t-stat ", 
04822                  ibrick, option_data->acname[i]);
04823         strcat (refit_str, str);
04824       }
04825 
04826 
04827   
04828   if (option_data->num_bcontr > 0)
04829     for (i = 0; i < option_data->num_bcontr; i++)
04830       {
04831         add_file_name (new_dset, option_data->bcname[i], bucket_str);
04832 
04833         ibrick++;
04834         sprintf (str, " -sublabel %d %s:Contr ", 
04835                  ibrick, option_data->bcname[i]);
04836         strcat (refit_str, str);
04837 
04838         ibrick++;
04839         sprintf (str, " -sublabel %d %s:t-stat ", 
04840                  ibrick, option_data->bcname[i]);
04841         strcat (refit_str, str);
04842       }
04843 
04844 
04845   
04846   if (option_data->num_ccontr > 0)
04847     for (i = 0; i < option_data->num_ccontr; i++)
04848       {
04849         add_file_name (new_dset, option_data->ccname[i], bucket_str);
04850 
04851         ibrick++;
04852         sprintf (str, " -sublabel %d %s:Contr ", 
04853                  ibrick, option_data->ccname[i]);
04854         strcat (refit_str, str);
04855 
04856         ibrick++;
04857         sprintf (str, " -sublabel %d %s:t-stat ", 
04858                  ibrick, option_data->ccname[i]);
04859         strcat (refit_str, str);
04860       }
04861 
04862 
04863   
04864 
04865   printf("Writing `bucket' dataset ");
04866   printf("into %s\n", option_data->bucket_filename);
04867   fprintf(stderr,"RUNNING COMMAND: %s\n",bucket_str) ;
04868   system (bucket_str);
04869 
04870 
04871   
04872   add_file_name (new_dset, option_data->bucket_filename, refit_str);
04873   fprintf(stderr,"RUNNING COMMAND: %s\n",refit_str) ;
04874   system (refit_str);
04875 
04876 
04877   
04878   THD_delete_3dim_dataset (new_dset , False);   new_dset = NULL;
04879 
04880 }
04881 
04882 
04883 
04884 
04885 
04886 
04887 
04888 
04889 
04890 void terminate (anova_options * option_data)
04891 {
04892   int i;
04893   THD_3dim_dataset * dset=NULL;       
04894   THD_3dim_dataset * new_dset=NULL;   
04895 
04896 
04897   
04898   if (option_data->n != 1)  volume_delete ("sse");
04899   volume_delete ("ssa");
04900   volume_delete ("ssb");
04901   volume_delete ("ssab");
04902   if (option_data->model != 5) 
04903     {
04904       volume_delete ("ssc");
04905       volume_delete ("ssac");
04906       volume_delete ("ssbc");
04907       volume_delete ("ssabc");
04908     }
04909   else
04910     {
04911       volume_delete ("ssca");
04912       volume_delete ("ssbca");
04913     }
04914 
04915 
04916   
04917   if (option_data->bucket_filename != NULL)
04918     create_bucket (option_data);
04919   
04920 
04921   
04922 
04923   if (option_data->bucket_filename != NULL)
04924     {
04925 
04926       
04927       dset = THD_open_dataset (option_data->first_dataset) ;
04928       if( ! ISVALID_3DIM_DATASET(dset) ){
04929         fprintf(stderr,"*** Unable to open dataset file %s\n",
04930                 option_data->first_dataset);
04931         exit(1) ;
04932       }
04933       
04934       
04935       new_dset = EDIT_empty_copy (dset);
04936       THD_delete_3dim_dataset (dset , False);   dset = NULL;
04937       EDIT_dset_items (new_dset, 
04938                        ADN_directory_name, option_data->session,
04939                        ADN_none);
04940       
04941       
04942       if (option_data->nfa != 0)
04943         remove_dataset (new_dset, option_data->faname);
04944       
04945       
04946       if (option_data->nfb != 0)
04947         remove_dataset (new_dset, option_data->fbname);
04948       
04949       
04950       if (option_data->nfc != 0)
04951         remove_dataset (new_dset, option_data->fcname);
04952       
04953       
04954       if (option_data->nfab != 0)
04955         remove_dataset (new_dset, option_data->fabname);
04956       
04957       
04958       if (option_data->nfac != 0)
04959         remove_dataset (new_dset, option_data->facname);
04960       
04961       
04962       if (option_data->nfbc != 0)
04963         remove_dataset (new_dset, option_data->fbcname);
04964       
04965       
04966       if (option_data->nfabc != 0)
04967         remove_dataset (new_dset, option_data->fabcname);
04968       
04969      
04970       if (option_data->num_ameans > 0)
04971         for (i = 0; i < option_data->num_ameans; i++)
04972           remove_dataset (new_dset, option_data->amname[i]);
04973       
04974       
04975       if (option_data->num_bmeans > 0)
04976         for (i = 0; i < option_data->num_bmeans; i++)
04977           remove_dataset (new_dset, option_data->bmname[i]);
04978       
04979       
04980       if (option_data->num_cmeans > 0)
04981         for (i = 0; i < option_data->num_cmeans; i++)
04982           remove_dataset (new_dset, option_data->cmname[i]);
04983       
04984       
04985       if (option_data->num_xmeans > 0)
04986         for (i = 0; i < option_data->num_xmeans; i++)
04987           remove_dataset (new_dset, option_data->xmname[i]);
04988       
04989       
04990       if (option_data->num_adiffs > 0)
04991         for (i = 0; i < option_data->num_adiffs; i++)
04992           remove_dataset (new_dset, option_data->adname[i]);
04993       
04994       
04995       if (option_data->num_bdiffs > 0)
04996         for (i = 0; i < option_data->num_bdiffs; i++)
04997           remove_dataset (new_dset, option_data->bdname[i]);
04998       
04999       
05000       if (option_data->num_cdiffs > 0)
05001         for (i = 0; i < option_data->num_cdiffs; i++)
05002           remove_dataset (new_dset, option_data->cdname[i]);
05003       
05004       
05005       if (option_data->num_xdiffs > 0)
05006         for (i = 0; i < option_data->num_xdiffs; i++)
05007           remove_dataset (new_dset, option_data->xdname[i]);
05008       
05009       
05010       if (option_data->num_acontr > 0)
05011         for (i = 0; i < option_data->num_acontr; i++)
05012           remove_dataset (new_dset, option_data->acname[i]);
05013       
05014       
05015       if (option_data->num_bcontr > 0)
05016         for (i = 0; i < option_data->num_bcontr; i++)
05017           remove_dataset (new_dset, option_data->bcname[i]);
05018       
05019       
05020       if (option_data->num_ccontr > 0)
05021         for (i = 0; i < option_data->num_ccontr; i++)
05022           remove_dataset (new_dset, option_data->ccname[i]);
05023       
05024       THD_delete_3dim_dataset (new_dset , False);   new_dset = NULL;
05025     }
05026 
05027 
05028   
05029   destroy_anova_options (option_data);
05030 
05031 }
05032   
05033 
05034 
05035 
05036 
05037 
05038  
05039 int main (int argc, char ** argv)
05040 {
05041   anova_options * option_data = NULL;
05042   
05043    
05044   
05045   printf ("\n\n");
05046   printf ("Program:          %s \n", PROGRAM_NAME);
05047   printf ("Author:           %s \n", PROGRAM_AUTHOR); 
05048   printf ("Initial Release:  %s \n", PROGRAM_INITIAL);
05049   printf ("Latest Revision:  %s \n", PROGRAM_LATEST);
05050   printf ("\n");
05051     
05052   
05053   if (argc < 2 || strncmp(argv[1], "-help", 5) == 0)  display_help_menu();  
05054   
05055   
05056 
05057   mainENTRY("3dANOVA3 main") ; machdep() ;
05058    { int new_argc ; char ** new_argv ;
05059      addto_args( argc , argv , &new_argc , &new_argv ) ;
05060      if( new_argv != NULL ){ argc = new_argc ; argv = new_argv ; }
05061    }
05062 
05063    
05064    initialize (argc, argv, &option_data);
05065 
05066    
05067    calculate_anova (option_data);
05068 
05069    
05070    analyze_results (option_data);
05071 
05072    
05073    terminate (option_data);
05074    free (option_data);   option_data = NULL;
05075 
05076    exit(0);
05077 }
05078 
05079 
05080 
05081 
05082 
05083 
05084 
05085 
05086 
05087 
05088 
05089 
05090 
05091 
05092 
05093 
05094