Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

plug_stats.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006    
00007 #include "afni.h"
00008 
00009 #ifndef ALLOW_PLUGINS
00010 #  error "Plugins not properly set up -- see machdep.h"
00011 #endif
00012 
00013 /***********************************************************************
00014   Plugin to compute voxelwise mean, slope, or sigma of a 3D+time dataset.
00015 ************************************************************************/
00016 
00017 /*--------------------- string to 'help' the user --------------------*/
00018 
00019 static char helpstring[] =
00020   "Purpose: Compute mean, slope, or sigma of a 3D+time dataset.\n"
00021   "Input items are:\n"
00022   "   3d+time = 3D+time dataset to analyze\n"
00023   "   Method  = Mean, Slope, or Sigma = type of analysis to do\n"
00024   "   Ignore  = How many points to ignore at start\n"
00025   "\n"
00026   "Output: Prefix = Filename prefix for new dataset"
00027 ;
00028 
00029 /*--------------------- strings for output format --------------------*/
00030 
00031 static char * method_strings[] = { "Mean" , "Slope" , "Sigma" , "CVar" } ;
00032 
00033 #define NUM_METHOD_STRINGS (sizeof(method_strings)/sizeof(char *))
00034 
00035 #define METH_MEAN  0
00036 #define METH_SLOPE 1
00037 #define METH_SIGMA 2
00038 #define METH_CVAR  3
00039 
00040 /*----------------- prototypes for internal routines -----------------*/
00041 
00042 char * STATS_main( PLUGIN_interface * ) ;  /* the entry point */
00043 
00044 void STATS_tsfunc( double tzero , double tdelta ,
00045                    int npts , float ts[] , double ts_mean , double ts_slope ,
00046                    void * ud , float * val ) ;
00047 
00048 /*---------------------------- global data ---------------------------*/
00049 
00050 static PLUGIN_interface * global_plint = NULL ;
00051 
00052 /***********************************************************************
00053    Set up the interface to the user:
00054     1) Create a new interface using "PLUTO_new_interface";
00055 
00056     2) For each line of inputs, create the line with "PLUTO_add_option"
00057          (this line of inputs can be optional or mandatory);
00058 
00059     3) For each item on the line, create the item with
00060         "PLUTO_add_dataset" for a dataset chooser,
00061         "PLUTO_add_string"  for a string chooser,
00062         "PLUTO_add_number"  for a number chooser.
00063 ************************************************************************/
00064 
00065 
00066 DEFINE_PLUGIN_PROTOTYPE
00067 
00068 PLUGIN_interface * PLUGIN_init( int ncall )
00069 {
00070    PLUGIN_interface * plint ;     /* will be the output of this routine */
00071 
00072    if( ncall > 0 ) return NULL ;  /* only one interface */
00073 
00074    /*---------------- set titles and call point ----------------*/
00075 
00076    plint = PLUTO_new_interface( "3D+t Statistic" ,
00077                                 "Voxel Statistics of 3D+time Dataset" ,
00078                                 helpstring ,
00079                                 PLUGIN_CALL_VIA_MENU , STATS_main  ) ;
00080 
00081    PLUTO_add_hint( plint , "Voxel Statistics of 3D+time Dataset" ) ;
00082 
00083    PLUTO_set_sequence( plint , "A:newdset:statistics" ) ;
00084 
00085    global_plint = plint ;  /* make global copy */
00086 
00087    /*--------- 1st line: Input dataset ---------*/
00088 
00089    PLUTO_add_option( plint ,
00090                      "Input" ,  /* label at left of input line */
00091                      "Input" ,  /* tag to return to plugin */
00092                      TRUE       /* is this mandatory? */
00093                    ) ;
00094 
00095    PLUTO_add_dataset(  plint ,
00096                        "3D+time" ,        /* label next to button   */
00097                        ANAT_ALL_MASK ,    /* take any anat datasets */
00098                        FUNC_FIM_MASK ,    /* only allow fim funcs   */
00099                        DIMEN_4D_MASK |    /* need 3D+time datasets  */
00100                        BRICK_ALLREAL_MASK /* need real-valued datasets */
00101                     ) ;
00102 
00103    PLUTO_add_hint( plint , "Choose input dataset" ) ;
00104 
00105    /*---------- 2nd line: other inputs ----------*/
00106 
00107    PLUTO_add_option( plint ,
00108                      "Input" ,  /* label at left of input line */
00109                      "Input" ,  /* tag to return to plugin */
00110                      TRUE       /* is this mandatory? */
00111                    ) ;
00112 
00113    PLUTO_add_hint( plint , "Control parameters" ) ;
00114 
00115    PLUTO_add_string( plint ,
00116                      "Method" ,           /* label next to chooser button */
00117                      NUM_METHOD_STRINGS , /* number of strings to choose among */
00118                      method_strings ,     /* list of strings to choose among */
00119                      0                    /* index of default string */
00120                    ) ;
00121 
00122    PLUTO_add_hint( plint , "Choose statistic to compute" ) ;
00123 
00124    PLUTO_add_number( plint ,
00125                      "Ignore" ,  /* label next to chooser */
00126                      0 ,         /* smallest possible value */
00127                      20 ,        /* largest possible value */
00128                      0 ,         /* decimal shift (none in this case) */
00129                      3 ,         /* default value */
00130                      FALSE       /* allow user to edit value? */
00131                    ) ;
00132 
00133    PLUTO_add_hint( plint , "Number of points to ignore at start of time series" ) ;
00134 
00135    /*---------- 3rd line: Output dataset ----------*/
00136 
00137    PLUTO_add_option( plint ,
00138                      "Output" ,  /* label at left of input line */
00139                      "Output" ,  /* tag to return to plugin */
00140                      TRUE        /* is this mandatory? */
00141                    ) ;
00142 
00143    PLUTO_add_string( plint ,
00144                      "Prefix" ,  /* label next to textfield */
00145                      0,NULL ,    /* no fixed strings to choose among */
00146                      19          /* 19 spaces for typing in value */
00147                    ) ;
00148 
00149    PLUTO_add_hint( plint , "Name of output dataset" ) ;
00150 
00151    /*--------- done with interface setup ---------*/
00152 
00153    return plint ;
00154 }
00155 
00156 /***************************************************************************
00157   Main routine for this plugin (will be called from AFNI).
00158   If the return string is not NULL, some error transpired, and
00159   AFNI will popup the return string in a message box.
00160 ****************************************************************************/
00161 
00162 char * STATS_main( PLUGIN_interface * plint )
00163 {
00164    MCW_idcode * idc ;                          /* input dataset idcode */
00165    THD_3dim_dataset * old_dset , * new_dset ;  /* input and output datasets */
00166    char * new_prefix , * str ;                 /* strings from user */
00167    int meth , ignore ;
00168 
00169    /*--------------------------------------------------------------------*/
00170    /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/
00171 
00172    /*--------- go to first input line ---------*/
00173 
00174    PLUTO_next_option(plint) ;
00175 
00176    idc      = PLUTO_get_idcode(plint) ;   /* get dataset item */
00177    old_dset = PLUTO_find_dset(idc) ;      /* get ptr to dataset */
00178    if( old_dset == NULL )
00179       return "*************************\n"
00180              "Cannot find Input Dataset\n"
00181              "*************************"  ;
00182 
00183    /*--------- go to next input line ---------*/
00184 
00185    PLUTO_next_option(plint) ;
00186 
00187    str  = PLUTO_get_string(plint) ;      /* get string item (the method) */
00188    meth = PLUTO_string_index( str ,      /* find it in list it is from */
00189                               NUM_METHOD_STRINGS ,
00190                               method_strings ) ;
00191 
00192    ignore = PLUTO_get_number(plint) ;    /* get number item */
00193 
00194    /*--------- go to next input line ---------*/
00195 
00196    PLUTO_next_option(plint) ;
00197 
00198    new_prefix = PLUTO_get_string(plint) ;   /* get string item (the output prefix) */
00199    if( ! PLUTO_prefix_ok(new_prefix) )      /* check if it is OK */
00200       return "************************\n"
00201              "Output Prefix is illegal\n"
00202              "************************"  ;
00203 
00204    /*------------- ready to compute new dataset -----------*/
00205 
00206    new_dset = PLUTO_4D_to_fim( old_dset ,             /* input dataset */
00207                                new_prefix ,           /* output prefix */
00208                                ignore ,               /* ignore count */
00209                                1 ,                    /* detrend = ON */
00210                                STATS_tsfunc ,         /* timeseries processor */
00211                                (void *) meth          /* data for tsfunc */
00212                              ) ;
00213 
00214    PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ;
00215 
00216    return NULL ;  /* null string returned means all was OK */
00217 }
00218 
00219 /**********************************************************************
00220    Function that does the real work
00221 ***********************************************************************/
00222 
00223 void STATS_tsfunc( double tzero , double tdelta ,
00224                    int npts , float ts[] , double ts_mean , double ts_slope ,
00225                    void * ud , float * val )
00226 {
00227    int meth = (int) ud ;
00228    static int nvox , ncall ;
00229 
00230    /** is this a "notification"? **/
00231 
00232    if( val == NULL ){
00233 
00234       if( npts > 0 ){  /* the "start notification" */
00235 
00236          PLUTO_popup_meter( global_plint ) ;  /* progress meter  */
00237          nvox  = npts ;                       /* keep track of   */
00238          ncall = 0 ;                          /* number of calls */
00239 
00240       } else {  /* the "end notification" */
00241 
00242          PLUTO_set_meter( global_plint , 100 ) ; /* set meter to 100% */
00243 
00244       }
00245       return ;
00246    }
00247 
00248    /** OK, actually do some work **/
00249 
00250    switch( meth ){
00251 
00252       default:
00253       case METH_MEAN:  *val = ts_mean  ; break ;
00254 
00255       case METH_SLOPE: *val = ts_slope ; break ;
00256 
00257       case METH_CVAR:
00258       case METH_SIGMA:{
00259          register int ii ;
00260          register double sum ;
00261 
00262          sum = 0.0 ;
00263          for( ii=0 ; ii < npts ; ii++ ) sum += ts[ii] * ts[ii] ;
00264 
00265          sum = sqrt( sum/(npts-1) ) ;
00266 
00267          if( meth == METH_SIGMA )  *val = sum ;
00268          else if( ts_mean != 0.0 ) *val = sum / fabs(ts_mean) ;
00269          else                      *val = 0.0 ;
00270       }
00271    }
00272 
00273    /** set the progress meter to the % of completion **/
00274 
00275    ncall++ ;
00276    PLUTO_set_meter( global_plint , (100*ncall)/nvox ) ;
00277    return ;
00278 }
 

Powered by Plone

This site conforms to the following standards: