示例#1
0
int THD_dataset_mismatch( THD_3dim_dataset *ds1 , THD_3dim_dataset *ds2 )
{
   THD_dataxes * dax1 , * dax2 ;
   THD_fvec3 fv1 , fv2 , dv ;
   int code ;
   float cd,c1,c2 ;
   double angle;

ENTRY("THD_dataset_mismatch") ;

   if( !ISVALID_DSET(ds1) || !ISVALID_DSET(ds2) ) RETURN(-1) ;

   dax1 = ds1->daxes ;
   dax2 = ds2->daxes ;
   code = 0 ;           /* will be return value */

   /* check if the number of voxels in each direction is the same */

   if( dax1->nxx != dax2->nxx ||
       dax1->nyy != dax2->nyy ||
       dax1->nzz != dax2->nzz   ) code |= MISMATCH_DIMEN ;

   /* check if the grid spacings are the same */

   if( fabs(dax1->xxdel-dax2->xxdel) > 0.01*fabs(dax1->xxdel) ||
       fabs(dax1->yydel-dax2->yydel) > 0.01*fabs(dax1->yydel) ||
       fabs(dax1->zzdel-dax2->zzdel) > 0.01*fabs(dax1->zzdel)   ) code |= MISMATCH_DELTA ;

   /* check if the orientations are the same */

   if( dax1->xxorient != dax2->xxorient ||
       dax1->yyorient != dax2->yyorient ||
       dax1->zzorient != dax2->zzorient   ) code |= MISMATCH_ORIENT ;

   /* check if they have the same centers */

   fv1 = THD_dataset_center( ds1 ) ;
   fv2 = THD_dataset_center( ds2 ) ;
   dv  = SUB_FVEC3(fv1,fv2) ; cd = SIZE_FVEC3(dv) ;

   LOAD_FVEC3(fv1,dax1->xxdel,dax1->yydel,dax1->zzdel) ; c1 = SIZE_FVEC3(fv1) ;
   LOAD_FVEC3(fv2,dax2->xxdel,dax2->yydel,dax2->zzdel) ; c2 = SIZE_FVEC3(fv1) ;

   if( cd > 0.1*(c1+c2) ) code |= MISMATCH_CENTER ;

   /* check if the obliquity is the same */
   angle = dset_obliquity_angle_diff(ds1, ds2, -1.0);
   if (angle > 0.0) code |= MISMATCH_OBLIQ ;
   
   RETURN(code) ;
}
示例#2
0
MRI_IMAGE * THD_median_brick( THD_3dim_dataset *dset )
{
   int nvox , nvals , ii ;
   MRI_IMAGE *tsim , *medim ;
   float *medar ;
   float *tsar ;  /* 05 Nov 2001 */

ENTRY("THD_median_brick") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;
   DSET_load(dset) ;
   if( !DSET_LOADED(dset) ) RETURN(NULL) ;

   nvals = DSET_NVALS(dset) ;
   tsim  = DSET_BRICK(dset,0) ;

   if( nvals == 1 ){
     medim = mri_scale_to_float( DSET_BRICK_FACTOR(dset,0), tsim ) ;
     RETURN(medim) ;
   }

   medim = mri_new_conforming( tsim , MRI_float ) ;
   medar = MRI_FLOAT_PTR(medim) ;
   nvox  = DSET_NVOX(dset) ;

   tsar = (float *) calloc( sizeof(float),nvals+1 ) ; /* 05 Nov 2001 */
   for( ii=0 ; ii < nvox ; ii++ ){
     THD_extract_array( ii , dset , 0 , tsar ) ;     /* 05 Nov 2001 */
     medar[ii] = qmed_float( nvals , tsar ) ;
   }

   free(tsar) ; RETURN(medim) ;
}
示例#3
0
void THD_vectim_to_dset_indexed( MRI_vectim *mrv ,
                                 THD_3dim_dataset *dset , int *tlist )
{
   int nvals , nvec ,  jj,kk , tmax=0 ;
   float *tar , *var ;

ENTRY("THD_vectim_to_dset_indexed") ;

   if( mrv == NULL || !ISVALID_DSET(dset) || tlist == NULL ) EXRETURN ;

   nvec  = mrv->nvec ;
   nvals = mrv->nvals ;

   for( kk=0 ; kk < nvals ; kk++ ){
     if( tlist[kk] < 0    ) EXRETURN ;
     if( tlist[kk] > tmax ) tmax = tlist[kk] ;
   }
   tmax++ ; if( DSET_NVALS(dset) < tmax ) EXRETURN ;

   tar = (float *)malloc(sizeof(float)*tmax) ;

   for( kk=0 ; kk < nvec ; kk++ ){
     var = VECTIM_PTR(mrv,kk) ;
     for( jj=0 ; jj < tmax  ; jj++ ) tar[jj]        = 0.0f    ;
     for( jj=0 ; jj < nvals ; jj++ ) tar[tlist[jj]] = var[jj] ;
     THD_insert_series( mrv->ivec[kk] , dset ,
                        tmax , MRI_float , tar , 0 ) ;
   }

   free(tar) ; EXRETURN ;
}
示例#4
0
void THD_vectim_indexed_to_dset( MRI_vectim *mrv, int nlist, int *ilist,
                                 THD_3dim_dataset *dset )
{
   int nvals , nvec ,  jj,kk ;
   float *tar , *var ;

ENTRY("THD_vectim_indexed_to_dset") ;

   if( mrv   == NULL || !ISVALID_DSET(dset) ||
       nlist <= 0    || ilist == NULL       || nlist > DSET_NVALS(dset)  ){
     ERROR_message("THD_vectim_indexed_to_dset: illegal inputs (nlist=%d)",nlist) ;
     EXRETURN ;
   }

   nvec  = mrv->nvec ;
   nvals = mrv->nvals ;

   for( kk=0 ; kk < nlist ; kk++ ){
     if( ilist[kk] < 0 || ilist[kk] >= nvals ){
       ERROR_message("THD_vectim_indexed_to_dset: illegal ilist[%d]=%d",kk,ilist[kk]) ;
       EXRETURN ;
     }
   }

   tar = (float *)malloc(sizeof(float)*nlist) ;

   for( kk=0 ; kk < nvec ; kk++ ){
     var = VECTIM_PTR(mrv,kk) ;
     for( jj=0 ; jj < nlist ; jj++ ) tar[jj] = var[ilist[jj]] ;
     THD_insert_series( mrv->ivec[kk] , dset ,
                        nlist , MRI_float , tar , 0 ) ;
   }

   free(tar) ; EXRETURN ;
}
示例#5
0
void THD_vectim_to_dset( MRI_vectim *mrv , THD_3dim_dataset *dset )
{
   int nvals , nvec ,  kk , ign ;

ENTRY("THD_vectim_to_dset") ;

   if( mrv == NULL || !ISVALID_DSET(dset)           ) EXRETURN ;
   if( mrv->nvals + mrv->ignore != DSET_NVALS(dset) ) EXRETURN ;

   nvec  = mrv->nvec ;
   nvals = mrv->nvals ;
   ign   = mrv->ignore ;

   if( ign == 0 ){
     for( kk=0 ; kk < nvec ; kk++ )
       THD_insert_series( mrv->ivec[kk] , dset ,
                          nvals , MRI_float , VECTIM_PTR(mrv,kk) , 0 ) ;
   } else {
     float *var ;
#pragma omp critical (MALLOC)
     var = (float *)malloc(sizeof(float)*(nvals+ign)) ;
     for( kk=0 ; kk < nvec ; kk++ ){
       (void)THD_extract_array( mrv->ivec[kk] , dset , 0 , var ) ;
       AAmemcpy( var+ign , VECTIM_PTR(mrv,kk) , sizeof(float)*nvals ) ;
       THD_insert_series( mrv->ivec[kk] , dset ,
                          nvals , MRI_float , var , 0 ) ;
     }
   }

   EXRETURN ;
}
示例#6
0
int main( int argc , char *argv[] )
{
   THD_3dim_dataset *dset ; int aa,ll ; char *cpt ; float val ;

   if( argc < 2 ){
     printf("Usage: 3dSatCheck dataset [...]\n"
            "\n"
            "Prints the 'raw' initial transient (saturation) check\n"
            "value for each dataset on the command line.  Round this\n"
            "number to the nearest integer to get an estimate of\n"
            "how many non-saturated time points start a dataset.\n"
           ) ;
     exit(0) ;
   }
   for( aa=1 ; aa < argc ; aa++ ){
     dset = THD_open_dataset( argv[aa] ) ; if( !ISVALID_DSET(dset) ) continue ;
     if( DSET_NVALS(dset) < 9 ) continue ;
     DSET_load(dset) ; if( !DSET_LOADED(dset) ) continue ;
     val = THD_saturation_check( dset , NULL , 0,0 ) ;
     ll = strlen(argv[aa]) ;
     cpt = (ll <= 50) ? argv[aa] : argv[aa]+(ll-50) ;
     INFO_message("%-50.50s = %.3f",cpt,val) ;
     DSET_delete(dset) ;
   }
   exit(0) ;
}
示例#7
0
MRI_IMAGE * THD_rms_brick( THD_3dim_dataset *dset )
{
   int nvox , nvals , ii , jj ;
   MRI_IMAGE *tsim , *medim ;
   float *medar , sum,fac ;
   float *tsar ;

ENTRY("THD_rms_brick") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;
   DSET_load(dset) ;
   if( !DSET_LOADED(dset) ) RETURN(NULL) ;

   nvals = DSET_NVALS(dset)   ; fac = 1.0 / nvals ;
   tsim  = DSET_BRICK(dset,0) ;

   if( nvals == 1 ){
     medim = mri_scale_to_float( DSET_BRICK_FACTOR(dset,0), tsim ) ;
     RETURN(medim) ;
   }

   medim = mri_new_conforming( tsim , MRI_float ) ;
   medar = MRI_FLOAT_PTR(medim) ;
   nvox  = DSET_NVOX(dset) ;

   tsar = (float *) calloc( sizeof(float),nvals+1 ) ;
   for( ii=0 ; ii < nvox ; ii++ ){
     THD_extract_array( ii , dset , 0 , tsar ) ;
     for( sum=0.0,jj=0 ; jj < nvals ; jj++ ) sum += tsar[jj]*tsar[jj] ;
     medar[ii] = sqrtf(fac * sum) ;
   }

   free(tsar) ; RETURN(medim) ;
}
示例#8
0
MRI_IMARR * THD_medmad_bricks( THD_3dim_dataset *dset )
{
   int nvox , nvals , ii ;
   MRI_IMAGE *tsim , *madim, *medim ;
   float             *madar, *medar ;
   MRI_IMARR *imar ;
   float *tsar ;

ENTRY("THD_medmad_bricks") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;

   nvals = DSET_NVALS(dset) ; if( nvals == 1 ) RETURN(NULL) ;

   DSET_load(dset) ;  if( !DSET_LOADED(dset) ) RETURN(NULL) ;
   tsim  = DSET_BRICK(dset,0) ;

   madim = mri_new_conforming( tsim , MRI_float ) ;
   madar = MRI_FLOAT_PTR(madim) ;
   medim = mri_new_conforming( tsim , MRI_float ) ;
   medar = MRI_FLOAT_PTR(medim) ;
   nvox  = DSET_NVOX(dset) ;

   tsar = (float *) calloc( sizeof(float),nvals+1 ) ;
   for( ii=0 ; ii < nvox ; ii++ ){
     THD_extract_array( ii , dset , 0 , tsar ) ;
     qmedmad_float( nvals , tsar , medar+ii , madar+ii ) ;
   }

   free(tsar) ;
   INIT_IMARR(imar) ; ADDTO_IMARR(imar,medim) ; ADDTO_IMARR(imar,madim) ;
   RETURN(imar) ;
}
示例#9
0
MRI_vectim * THD_dset_list_censored_to_vectim( int nds, THD_3dim_dataset **ds,
                                               byte *mask , int nkeep , int *keep )
{
   MRI_vectim *vout , **vim ;
   int kk , jj ;

   if( nds < 1 || ds == NULL ) return NULL ;

   if( nds == 1 )   /* trivial case */
     return THD_dset_censored_to_vectim( ds[0],mask,nkeep,keep );

   for( kk=0 ; kk < nds ; kk++ ){
     if( !ISVALID_DSET(ds[kk]) ) return NULL ;
     if( DSET_NVALS(ds[kk]) != DSET_NVALS(ds[0]) ) return NULL ;
   }

#pragma omp critical (MALLOC)
   vim = (MRI_vectim **)malloc(sizeof(MRI_vectim *)*nds) ;
   for( kk=0 ; kk < nds ; kk++ ){
     vim[kk] = THD_dset_censored_to_vectim( ds[kk] , mask , nkeep,keep ) ;
     /** DSET_unload( ds[kk] ) ; **/
     if( vim[kk] == NULL ){
       for( jj=0 ; jj < kk ; jj++ ) VECTIM_destroy(vim[jj]) ;
       free(vim) ; return NULL ;
     }
   }

   vout = THD_tcat_vectims( nds , vim ) ;
   for( jj=0 ; jj < nds ; jj++ ) VECTIM_destroy(vim[jj]) ;
   free(vim) ; return vout ;
}
示例#10
0
double ENTROPY_dataset( THD_3dim_dataset *dset )
{
   if( !ISVALID_DSET(dset) ) return(0.0) ;
   DSET_load(dset) ;
   if( !DSET_LOADED(dset) ) return(0.0) ;
   return ENTROPY_datablock( dset->dblk ) ;
}
示例#11
0
THD_3dim_dataset *Seg_load_dset_eng( char *set_name, char *view ) 
{
   static char FuncName[]={"Seg_load_dset_eng"};
   THD_3dim_dataset *dset=NULL, *sdset=NULL;
   int i=0;
   byte make_cp=0;
   int verb=0;
   char sprefix[THD_MAX_PREFIX+10], *stmp=NULL;
   
   SUMA_ENTRY;
   
   dset = THD_open_dataset( set_name );
   if( !ISVALID_DSET(dset) ){
     fprintf(stderr,"**ERROR: can't open dataset %s\n",set_name) ;
     SUMA_RETURN(NULL);
   }
   
   DSET_mallocize(dset)   ; DSET_load(dset);
   
   for (i=0; i<DSET_NVALS(dset); ++i) {
      if (DSET_BRICK_TYPE(dset,i) != MRI_short) {
         if (verb) INFO_message("Sub-brick %d in %s not of type short.\n"
                       "Creating new short copy of dset ", 
                       i, DSET_PREFIX(dset));
         make_cp=1; break;
      }
   }
   
   if (make_cp) {
      if (!SUMA_ShortizeDset(&dset, -1.0)) {
         SUMA_S_Err("**ERROR: Failed to shortize");
         SUMA_RETURN(NULL);
      }
   }
   
   if (DSET_IS_MASTERED(dset)) {
      if (verb) INFO_message("Dset is mastered, making copy...");
      stmp = SUMA_ModifyName(set_name, "append", ".cp", NULL);
      sdset = dset;
      dset = EDIT_full_copy(sdset, stmp);
      free(stmp); DSET_delete(sdset); sdset = NULL;  
   }
      
   
   if (view) {
      if (view) {
               if (!strstr(view,"orig")) 
            EDIT_dset_items(dset,ADN_view_type, VIEW_ORIGINAL_TYPE, ADN_none); 
         else  if (!strstr(view,"acpc")) 
            EDIT_dset_items(dset,ADN_view_type, VIEW_ACPCALIGNED_TYPE, ADN_none);
         else  if (!strstr(view,"tlrc")) 
            EDIT_dset_items(dset ,ADN_view_type, VIEW_TALAIRACH_TYPE, ADN_none);
         else SUMA_S_Errv("View of %s is rubbish", view);
      }
   }
   
   SUMA_RETURN(dset);
}
示例#12
0
int is_integral_dset ( THD_3dim_dataset *dset, int check_values)
{
   int i=0;

   if(   !ISVALID_DSET(dset)  ) return(0);
   for (i=0; i<DSET_NVALS(dset); ++i) {
      if (!is_integral_sub_brick(dset, i, check_values)) return(0);
   }
   return(1);
}
示例#13
0
float THD_get_float_value( int ind , int ival , THD_3dim_dataset *dset )
{
   MRI_TYPE typ ; float val=0.0f ;

   if( ind < 0 || ival < 0 || !ISVALID_DSET(dset) ||
       ival >= DSET_NVALS(dset) || ind >= DSET_NVOX(dset) ) return val ;

   typ = DSET_BRICK_TYPE(dset,ival) ;  /* raw data type */

   switch( typ ){

      default:           /* don't know what to do --> return nada */
         return(-1);
      break ;

      case MRI_byte:{
         byte *bar ;
         bar = (byte *) DSET_ARRAY(dset,ival) ;
         if( bar != NULL ) val = (float)bar[ind] ;
      }
      break ;

      case MRI_short:{
         short *bar ;
         bar = (short *) DSET_ARRAY(dset,ival) ;
         if( bar != NULL ) val = (float)bar[ind] ;
      }
      break ;

      case MRI_float:{
         float *bar ;
         bar = (float *) DSET_ARRAY(dset,ival) ;
         if( bar != NULL ) val = bar[ind] ;
      }
      break ;

      case MRI_complex:{
         complex *bar ;
         bar = (complex *) DSET_ARRAY(dset,ival) ;
         if( bar != NULL ) val = CABS(bar[ind]) ;
      }
      break ;

   }

   if( DSET_BRICK_FACTOR(dset,ival) > 0.0f )
     val *= DSET_BRICK_FACTOR(dset,ival) ;

   return val ;
}
示例#14
0
int THD_voxel_is_constant( int ind , THD_3dim_dataset *dset )
{
   float *far ; int ii,nvox,nvals ;

   if( !ISVALID_DSET(dset) ) return 1 ;
   if( ind < 0 || ind >= DSET_NVOX(dset) ) return 1 ;

   nvals = DSET_NVALS(dset) ; if( nvals == 1 ) return 1 ;
   far = (float *)malloc(sizeof(float)*nvals) ; NULL_CHECK(far) ;
   ii = THD_extract_array( ind , dset , 0 , far ) ;
   if( ii < 0 ){ free(far); return 1; }
   for( ii=1 ; ii < nvals && far[ii]==far[0]; ii++ ) ; /*nada*/
   free(far) ; return (ii==nvals) ;
}
示例#15
0
int64_t THD_vectim_size( THD_3dim_dataset *dset , byte *mask )
{
   int nvals , nvox , nmask ;
   int64_t sz ;

   ENTRY("THD_vectim_size") ;

   if( !ISVALID_DSET(dset) ) RETURN(0) ;

   nvals = DSET_NVALS(dset) ;
   nvox  = DSET_NVOX(dset) ;
   if( mask != NULL ) nmask = THD_countmask( nvox , mask ) ;
   else               nmask = DSET_NVOX(dset) ;

   sz = ((int64_t)nmask) * ( ((int64_t)nvals) * sizeof(float) + sizeof(int) ) ;
   RETURN(sz) ;
}
示例#16
0
MRI_vectim * THD_dset_to_vectim_byslice( THD_3dim_dataset *dset, byte *mask ,
                                         int ignore , int kzbot , int kztop  )
{
   byte *mmm ;
   MRI_vectim *mrv=NULL ;
   int kk,iv , nvals , nvox , nmask , nxy , nz ;

ENTRY("THD_dset_to_vectim_byslice") ;

                     if( !ISVALID_DSET(dset) ) RETURN(NULL) ;
   DSET_load(dset) ; if( !DSET_LOADED(dset)  ) RETURN(NULL) ;

   nvals = DSET_NVALS(dset) ; if( nvals <= 0 ) RETURN(NULL) ;
   nvox  = DSET_NVOX(dset) ;

   nxy = DSET_NX(dset) * DSET_NY(dset) ; nz = DSET_NZ(dset) ;

   if( kzbot <  0  ) kzbot = 0 ;
   if( kztop >= nz ) kztop = nz-1 ;
   if( kztop < kzbot ) RETURN(NULL) ;
   if( kzbot == 0 && kztop == nz-1 ){
     mrv = THD_dset_to_vectim( dset , mask, ignore ) ; RETURN(mrv) ;
   }

   /* make a mask that includes cutting out un-desirable slices */

   { int ibot , itop , ii ;
#pragma omp critical (MALLOC)
     mmm = (byte *)malloc(sizeof(byte)*nvox) ;
     if( mask == NULL ) AAmemset( mmm ,    1 , sizeof(byte)*nvox ) ;
     else               AAmemcpy( mmm , mask , sizeof(byte)*nvox ) ;
     if( kzbot > 0 )
       AAmemset( mmm               , 0 , sizeof(byte)*kzbot       *nxy ) ;
     if( kztop < nz-1 )
       AAmemset( mmm+(kztop+1)*nxy , 0 , sizeof(byte)*(nz-1-kztop)*nxy ) ;
   }

   /* and make the vectim using the standard function */

   mrv = THD_dset_to_vectim( dset , mmm , ignore ) ;
   free(mmm) ;
   RETURN(mrv) ;
}
示例#17
0
int is_integral_sub_brick ( THD_3dim_dataset *dset, int isb, int check_values)
{
   float mfac = 0.0;
   void *vv=NULL;

   if(   !ISVALID_DSET(dset)    ||
            isb < 0                     ||
            isb >= DSET_NVALS(dset)  ) {

      fprintf(stderr,"** Bad dset or sub-brick index.\n");
      return (0) ;

   }
   if( !DSET_LOADED(dset) ) DSET_load(dset);

   switch( DSET_BRICK_TYPE(dset,isb) ){
      case MRI_short:
      case MRI_byte:
         if (check_values) {
            mfac = DSET_BRICK_FACTOR(dset,isb) ;
            if (mfac != 0.0f && mfac != 1.0f) return(0);
         }
         break;
      case MRI_double:
      case MRI_complex:
      case MRI_float:
         vv = (void *)DSET_ARRAY(dset,isb);
         mfac = DSET_BRICK_FACTOR(dset,isb) ;
         if (mfac != 0.0f && mfac != 1.0f) return(0);
         if (!vv) {
            fprintf(stderr,"** NULL array!\n");
            return(0);
         }
         return(is_integral_data(DSET_NVOX(dset),
                                 DSET_BRICK_TYPE(dset,isb),
                                 DSET_ARRAY(dset,isb) ) );
         break;
      default:
         return(0);
   }

   return(1);
}
示例#18
0
void THD_load_tcat( THD_datablock *dblk )
{
   int ivout , dd , iv ;
   THD_3dim_dataset *dset_in , *dset_out ;
   NI_str_array *sar ;

ENTRY("THD_load_tcat") ;

   if( !ISVALID_DBLK(dblk) ) EXRETURN ;
   dset_out = (THD_3dim_dataset *)dblk->parent ;
   if( !ISVALID_DSET(dset_out) ) EXRETURN ;
   sar = NI_decode_string_list( dset_out->tcat_list , "~" ) ;
   if( sar == NULL ) EXRETURN ;
   if( sar->num != dset_out->tcat_num ){ NI_delete_str_array(sar); EXRETURN; }

   ivout = 0 ;
   for( dd=0 ; dd < sar->num ; dd++ ){
     dset_in = THD_open_dataset( sar->str[dd] ) ;
     if( dset_in == NULL ){
       NI_delete_str_array(sar) ; DSET_unload(dset_out) ;
       EXRETURN ;
     }
     DSET_mallocize(dset_in) ; DSET_load(dset_in) ;
     if( !DSET_LOADED(dset_in) ){
       NI_delete_str_array(sar) ; DSET_unload(dset_out) ; DSET_delete(dset_in) ;
       EXRETURN ;
     }

     for( iv=0 ; iv < DSET_NVALS(dset_in) ; iv++ ){
       EDIT_substitute_brick( dset_out , ivout ,
                              DSET_BRICK_TYPE(dset_in,iv), DSET_ARRAY(dset_in,iv) );
       mri_fix_data_pointer( NULL , DSET_BRICK(dset_in,iv) ) ;
       EDIT_BRICK_FACTOR( dset_out , ivout , DSET_BRICK_FACTOR(dset_in,iv) ) ;
       EDIT_BRICK_LABEL(dset_out, ivout, 
                        DSET_BRICK_LABEL(dset_in, iv)); /* ZSS Aug. 27 2012 */
       ivout++ ;
     }
     DSET_delete(dset_in) ;
   }

   NI_delete_str_array(sar) ; EXRETURN ;
}
示例#19
0
MRI_IMAGE * THD_dset_to_1Dmri( THD_3dim_dataset *dset )
{
   MRI_IMAGE *im ; float *far ;
   int nx , ny , ii ;

ENTRY("THD_dset_to_1D") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;
   DSET_load(dset) ;
   if( !DSET_LOADED(dset) ) RETURN(NULL) ;

   nx = DSET_NVALS(dset) ;
   ny = DSET_NVOX(dset) ;
   im = mri_new( nx , ny , MRI_float ) ; far = MRI_FLOAT_PTR(im) ;

   for( ii=0 ; ii < ny ; ii++ )
     THD_extract_array( ii , dset , 0 , far + ii*nx ) ;

   RETURN(im) ;
}
示例#20
0
void THD_extract_detrended_array( THD_3dim_dataset *dset ,
                                  int nref, float **ref, MRI_IMARR *imar,
                                  int ii, int scl, float *far )
{
   int tt , nval , qq ;
   float val , **fitar , *var ;
   MRI_IMAGE *qim ;

ENTRY("THD_extract_detrended_array") ;

   if( !ISVALID_DSET(dset) ||
       nref < 1            || ref == NULL                ||
       imar == NULL        || IMARR_COUNT(imar) < nref+1 ||
       ii < 0              || ii >= DSET_NVOX(dset)      || far == NULL ) EXRETURN ;

   qq = THD_extract_array( ii , dset , 0 , far ) ;  /* get data */
   if( qq < 0 ) EXRETURN ;
   nval = DSET_NVALS(dset) ;

   fitar = (float **)malloc(sizeof(float *)*nref) ;
   for( qq=0 ; qq < nref ; qq++ ){
     qim = IMARR_SUBIM(imar,qq) ; fitar[qq] = MRI_FLOAT_PTR(qim) ;
   }
   qim = IMARR_SUBIM(imar,nref) ; var = MRI_FLOAT_PTR(qim) ;

   for( tt=0 ; tt < nval ; tt++ ){  /* get residuals */
     val = far[tt] ;
     for( qq=0 ; qq < nref ; qq++ ) val -= ref[qq][tt] * fitar[qq][ii] ;
     far[tt] = val ;
   }

   if( scl && var[ii] > 0.0f ){
     val = 1.0f / var[ii] ;
     for( tt=0 ; tt < nval ; tt++ ) far[tt] *= val ;
   }
   
   /* ZSS: Need to free fitar */
   free(fitar); fitar=NULL;
   EXRETURN ;
}
示例#21
0
Boolean THD_write_nimlatr( THD_datablock *blk )  /* 01 Jun 2005 */
{
   NI_stream ns ;
   NI_group  *ngr ;
   char sname[2048] ;
   THD_3dim_dataset *dset ;

ENTRY("THD_write_nimlatr") ;

   /* get dataset that contains this datablock */

   dset = (THD_3dim_dataset *)blk->parent ;
   if( !ISVALID_DSET(dset) ){
     STATUS("parent is not valid dataset!"); RETURN(False);      /* bad */
   }

   /* convert dataset struct AFNI attributes into a NIML element */

   ngr = THD_nimlize_dsetatr( dset ) ;
   if( ngr == NULL ){
     STATUS("can't create NIML header element!"); RETURN(False); /* bad */
   }
   NI_set_attribute( ngr , "self_prefix" , blk->diskptr->prefix ) ;

   /* open output NIML stream (to file) */

   sprintf(sname,"file:%s",blk->diskptr->header_name) ;
   ns = NI_stream_open( sname , "w" ) ;
   if( ns == (NI_stream)NULL ){
     STATUS("can't open output NIML stream!"); RETURN(False);    /* bad */
   }

   /* write XML header and then the AFNI header element */

   STATUS("writing NIML header") ;
   NI_stream_writestring( ns , "<?xml version='1.0' ?>\n" ) ;
   NI_write_element( ns , ngr , NI_TEXT_MODE ) ;
   NI_stream_close( ns ) ;
   RETURN(True) ;
}
示例#22
0
MRI_IMAGE * THD_extract_series( int ind , THD_3dim_dataset *dset , int raw )
{
   int nv , typ , ii ;
   MRI_IMAGE *im ;
   void *imar ;

ENTRY("THD_extract_series") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;

   nv  = dset->dblk->nvals ;
   if( raw ) typ = DSET_BRICK_TYPE(dset,0) ;  /* type of output array */
   else      typ = MRI_float ;

   im   = mri_new( nv , 1 , typ ) ;           /* output image */
   imar = mri_data_pointer(im) ;

   ii = THD_extract_array( ind , dset , raw , imar ) ; /* get data */

   if( ii != 0 ){ mri_free(im) ; RETURN(NULL) ; }      /* bad */

   if( dset->taxis != NULL ){  /* 21 Oct 1996 */
      float zz , tt ;
      int kz = ind / ( dset->daxes->nxx * dset->daxes->nyy ) ;

      zz = dset->daxes->zzorg + kz * dset->daxes->zzdel ;
      tt = THD_timeof( 0 , zz , dset->taxis ) ;

      im->xo = tt ; im->dx = dset->taxis->ttdel ;   /* origin and delta */

      if( dset->taxis->units_type == UNITS_MSEC_TYPE ){ /* convert to sec */
         im->xo *= 0.001 ; im->dx *= 0.001 ;
      }
   } else {
      im->xo = 0.0 ; im->dx = 1.0 ;  /* 08 Nov 1996 */
   }

   RETURN(im) ;
}
示例#23
0
THD_3dim_dataset * THD_detrend_dataset( THD_3dim_dataset *dset ,
                                        int nref , float **ref ,
                                        int meth , int scl ,
                                        byte *mask , MRI_IMARR **imar )
{
   MRI_IMARR *qmar ;
   int ii,jj,kk , nvals,nvox , iv ;
   float *var ;
   THD_3dim_dataset *newset ;

ENTRY("THD_detrend_dataset") ;

   if( !ISVALID_DSET(dset) ) RETURN(NULL) ;
   nvals = DSET_NVALS(dset) ; nvox = DSET_NVOX(dset) ;

   qmar = THD_time_fit_dataset( dset , nref,ref , meth , mask ) ;
   if( qmar == NULL ) RETURN(NULL) ;

   newset = EDIT_empty_copy(dset) ;
   for( iv=0 ; iv < nvals ; iv++ ){
     EDIT_substitute_brick( newset , iv , MRI_float , NULL ) ;
     EDIT_BRICK_FACTOR( newset , iv , 0.0f ) ;  /* 04 Jun 2007 */
   }

   var = (float *)malloc(sizeof(float)*nvals) ;
   for( ii=0 ; ii < nvox ; ii++ ){
     if( mask == NULL || mask[ii] )
       THD_extract_detrended_array( dset , nref,ref , qmar , ii,scl , var ) ;
     else
       memset(var,0,sizeof(float)*nvals) ;
     THD_insert_series( ii , newset , nvals , MRI_float , var , 0 ) ;
   }

   free(var) ;
   if( imar != NULL ) *imar = qmar ;
   else               DESTROY_IMARR(qmar) ;

   RETURN(newset) ;
}
void RT_test_callback(void *junk)
{
   RT_status *rts = GLOBAL_library.realtime_status ;
   int cc , nval,nbr ;

   if( rts == NULL ){ ERROR_message("bad call to RT_test_callback"); return; }

   INFO_message("RT_test_callback: numchan=%d status=%d numdset=%d",
                rts->numchan , rts->status , rts->numdset ) ;

   for( cc=0 ; cc < rts->numdset ; cc++ ){     /* print out some dataset info */
     if( !ISVALID_DSET(rts->dset[cc]) ){
       ININFO_message(" dset[%d] invalid!",cc) ;       /* should never happen */
     } else {
       nval = DSET_NVALS(rts->dset[cc]) ;                 /* number of bricks */
       nbr  = THD_count_databricks(rts->dset[cc]->dblk) ; /* number with data */
       ININFO_message(" dset[%d] '%s': nvals=%d  nbr=%d",
                      cc , DSET_HEADNAME(rts->dset[cc]) , nval,nbr ) ;
     }
   }
   return ;
}
示例#25
0
THD_3dim_dataset * THD_despike9_dataset( THD_3dim_dataset *inset , byte *mask )
{
   THD_3dim_dataset *outset ;
   MRI_vectim *mrv ;
   int ii ;

ENTRY("THD_despike9_dataset") ;

   if( !ISVALID_DSET(inset) || DSET_NVALS(inset) < 9 ) RETURN(NULL) ;

   mrv = THD_dset_to_vectim(inset,mask,0) ;  DSET_unload(inset) ;
   if( mrv == NULL ) RETURN(NULL) ;

   (void)THD_vectim_despike9(mrv) ;

   outset = EDIT_empty_copy(inset) ;
   for( ii=0 ; ii < DSET_NVALS(outset) ; ii++ )
     EDIT_substitute_brick(outset,ii,MRI_float,NULL) ;

   THD_vectim_to_dset(mrv,outset) ; VECTIM_destroy(mrv) ;
   RETURN(outset) ;
}
示例#26
0
int THD_retrend_dataset( THD_3dim_dataset *dset ,
                         int nref , float **ref ,
                         int scl , byte *mask , MRI_IMARR *imar )
{
   int ii,qq,tt , nvals,nvox ;
   MRI_IMAGE *qim ; float **fitar , *far , fac , *var , val ;

ENTRY("THD_retrend_dataset") ;

   if( !ISVALID_DSET(dset) ||
       nref < 1            || ref == NULL ||
       imar == NULL        || IMARR_COUNT(imar) <= nref ) RETURN(0) ;

   nvals = DSET_NVALS(dset) ; nvox = DSET_NVOX(dset) ;

   fitar = (float **)malloc(sizeof(float *)*nref) ;
   for( qq=0 ; qq < nref ; qq++ ){
     qim = IMARR_SUBIM(imar,qq) ; fitar[qq] = MRI_FLOAT_PTR(qim) ;
   }
   qim = IMARR_SUBIM(imar,nref) ; var = MRI_FLOAT_PTR(qim) ;

   far  = (float *)malloc(sizeof(float)*nvals) ;
   for( ii=0 ; ii < nvox ; ii++ ){
     if( mask != NULL && !mask[ii] ) continue ;
     qq = THD_extract_array( ii , dset , 0 , far ) ;  /* get data */
     if( qq < 0 ) continue ;
     fac = (scl) ? var[ii] : 1.0f ;
     for( tt=0 ; tt < nvals ; tt++ ){  /* add fit back in */
       val = far[tt] * fac ;
       for( qq=0 ; qq < nref ; qq++ ) val += ref[qq][tt] * fitar[qq][ii] ;
       far[tt] = val ;
     }
     THD_insert_series( ii , dset , nvals , MRI_float , far , 0 ) ;
   }

   free(far) ; free(fitar) ; RETURN(1) ;
}
THD_session * THD_init_session( char *sessname )
{
    THD_session            *sess ;
    XtPointer_array        *dblk_arrarr ;
    THD_datablock_array    *dblk_arr ;
    THD_3dim_dataset       *dset=NULL , *temp_dset;
    THD_3dim_dataset_array *dset_arr ;

    int ibar , idset , iview  , nds , qview=-666 ;

    ENTRY("THD_init_session") ;

    /*-- sanity check --*/

    if( sessname == NULL || strlen(sessname) == 0 || !THD_is_directory(sessname) )
        RETURN( NULL ) ;

    /*-- initialize session --*/

    sess         = myXtNew( THD_session ) ;
    sess->type   = SESSION_TYPE ;
    sess->parent = NULL ;
#ifdef DEBUG_SESSIONS
    fprintf(stderr, "blanking session\n");
#endif
    BLANK_SESSION(sess) ;  /* null out all entries */

    /* save directory name, with a trailing slash */

    MCW_strncpy( sess->sessname , sessname , THD_MAX_NAME ) ;
    iview = strlen(sess->sessname) ;
    if( sess->sessname[iview-1] != '/' ) { /* tack trailing / onto sessname */
        sess->sessname[iview]   = '/' ;
        sess->sessname[iview+1] = '\0' ;
    } else {
        iview-- ;  /* iview now points to last non-NUL character in string */
    }

    /* save last name from sessname */
#if 1
    {   char *env = my_getenv( "AFNI_SESSTRAIL" ) ;
        int tt = 0 ;
        if( env != NULL ) tt = strtol(env,NULL,10) ;
        env = THD_trailname(sess->sessname,tt) ;
        tt = 1+strlen(env) - THD_MAX_NAME ;
        if( tt < 0 ) tt = 0 ;
        strcpy( sess->lastname , env+tt ) ;
    }
#else
    for( iview-- ; iview >= 0 ; iview-- )
        if( sess->sessname[iview] == '/' ) break ;
    MCW_strncpy( sess->lastname, &(sess->sessname[iview+1]), THD_MAX_NAME ) ;
#endif

    /*-- override dataset 'view'??  [30 May 2013] --*/

    {   char *env = my_getenv("AFNI_OVERRIDE_VIEW") ;
        if( env != NULL ) {
            if( toupper(*env) == 'T' ) qview = VIEW_TALAIRACH_TYPE ;
            else if( toupper(*env) == 'O' ) qview = VIEW_ORIGINAL_TYPE  ;
            else
                WARNING_message("AFNI_OVERRIDE_VIEW=%s is not understood",env) ;
        }
    }

    /*-- read all datablocks --*/

    dblk_arrarr = THD_init_alldir_datablocks( sess->sessname ) ;

    /*-- for each datablock array ... --*/

    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ) {

        /*-- get the current array of datablocks --*/

        dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
        if( dblk_arr == NULL || dblk_arr->num <= 0 ) continue ;    /* huh? */

        /*-- convert it into an array of datasets --*/

        dset_arr = THD_array_3dim_from_block( dblk_arr ) ;
        if( dset_arr == NULL || dset_arr->num <= 0 ) continue ;

        /*-- place it into the next row of the THD_session [28 Jul 2003] --*/

        nds = sess->num_dsset ;

        if( nds >= THD_MAX_SESSION_SIZE ) {  /* bad! */
            fprintf(stderr,
                    "\n*** Session %s table overflow with dataset %s ***\n",
                    sessname , dset_arr->ar[0]->self_name) ;
            for( idset=0 ; idset < dset_arr->num ; idset++ )
                THD_delete_3dim_dataset( dset_arr->ar[idset] , False ) ;
            FREE_3DARR(dset_arr) ;
            continue ;  /* skip to next dblk_arr (ibar loop) */
        }

        /*-- put each dataset into this row in its view place --*/

        for( idset=0 ; idset < dset_arr->num ; idset++ ) {
            dset  = dset_arr->ar[idset] ;
            iview = dset->view_type ;
            if( qview >= 0 ) iview = qview ;  /* 30 May 2013 */

            if( GET_SESSION_DSET(sess,nds,iview) != NULL ) { /* should never happen */
                fprintf(stderr,
                        "\n*** Session %s has duplicate dataset views of %s ***\n",
                        sessname , dset->self_name) ;
                THD_delete_3dim_dataset( dset , False ) ;
            } else {
#ifdef DEBUG_SESSIONS
                fprintf(stderr,"\nputting datasets into initial view \n");
#endif
                SET_SESSION_DSET(dset, sess, nds, iview);  /* should always happen */
                /*        sess->dsset_xform_table[nds][iview] = dset ; */  /* should always happen */
            }
        }

        sess->num_dsset ++ ;  /* one more row */

        FREE_3DARR(dset_arr) ;

    } /* end of loop over each datablock array (ibar) */

    /*-- throw away the datablock arrays at this point --*/

    STATUS("trashing dblk_arrarr") ;

    for( ibar=0 ; ibar < dblk_arrarr->num ; ibar++ ) {
        dblk_arr = (THD_datablock_array *) dblk_arrarr->ar[ibar] ;
        FREE_DBARR( dblk_arr ) ;
    }
    FREE_XTARR( dblk_arrarr ) ;

    /*-- 29 Oct 2001: try to read .mnc "datasets" --*/

    if( !AFNI_noenv("AFNI_MINC_DATASETS") ) {
        char ename[THD_MAX_NAME] , **fn_minc , *eee ;
        int num_minc , ii ;

        STATUS("looking for MINC files") ;

        strcpy(ename,sess->sessname) ;
        strcat(ename,"*.mnc") ;
        eee = ename ;
        MCW_file_expand( 1,&eee , &num_minc,&fn_minc ) ;  /* find files */

        if( num_minc > 0 ) {                              /* got some! */
            STATUS("opening MINC files") ;
            for( ii=0 ; ii < num_minc ; ii++ ) {            /* loop over files */
                dset = THD_open_minc( fn_minc[ii] ) ;         /* try it on */
                if( !ISVALID_DSET(dset) ) continue ;          /* doesn't fit? */
                nds = sess->num_dsset ;
                if( nds >= THD_MAX_SESSION_SIZE ) {
                    fprintf(stderr,
                            "\n*** Session %s table overflow with MINC dataset %s ***\n",
                            sessname , fn_minc[ii] ) ;
                    THD_delete_3dim_dataset( dset , False ) ;
                    break ; /* out of for loop */
                }
                iview = dset->view_type ;
                SET_SESSION_DSET(dset, sess, nds, iview);
                /*         sess->dsset_xform_table[nds][iview] = dset ;*/
                sess->num_dsset ++ ;
            } /* end of loop over files */
            MCW_free_expand( num_minc , fn_minc ) ;
        } /* end of if we found MINC files */
    }

    /*-- 06 Apr 2005: try to read NIfTI-1 files [KRH and RWC] --*/

    if( !AFNI_noenv("AFNI_NIFTI_DATASETS") ) {
        char *ename[2] , **fn_nifti ;
        int num_nifti , ii ;

        STATUS("looking for NIFTI files") ;

        ename[0] = AFMALL(char, THD_MAX_NAME) ;
        ename[1] = AFMALL(char, THD_MAX_NAME) ;
        strcpy(ename[0],sess->sessname) ;
        strcat(ename[0],"*.nii") ;
        strcpy(ename[1],sess->sessname) ;
        strcat(ename[1],"*.nii.gz") ;
        MCW_file_expand( 2,ename , &num_nifti,&fn_nifti ) ;  /* find files */
        free(ename[0]) ;
        free(ename[1]) ;

        if( num_nifti > 0 ) {                              /* got some! */
            STATUS("opening NIFTI files") ;
            for( ii=0 ; ii < num_nifti ; ii++ ) {            /* loop over files */
                dset = THD_open_nifti( fn_nifti[ii] ) ;        /* try it on */
                if( !ISVALID_DSET(dset) ) continue ;           /* doesn't fit? */
                nds = sess->num_dsset ;
                if( nds >= THD_MAX_SESSION_SIZE ) {
                    fprintf(stderr,
                            "\n*** Session %s table overflow with NIfTI dataset %s ***\n",
                            sessname , fn_nifti[ii] ) ;
                    THD_delete_3dim_dataset( dset , False ) ;
                    break ; /* out of for loop */
                }
                iview = dset->view_type ;
                SET_SESSION_DSET(dset, sess, nds, iview);
                /*         sess->dsset_xform_table[nds][iview] = dset ;*/
                sess->num_dsset ++ ;
            } /* end of loop over files */
            MCW_free_expand( num_nifti , fn_nifti ) ;
        } /* end of if we found NIFTI files */
    }
示例#28
0
int main( int argc , char * argv[] )
{
   int nopt=1 , ia , HasSb=0;
   THD_3dim_dataset * dset ;
   char * aname =NULL;
   ATR_any * atr ;
   THD_fvec3 fv;
   char *ssep=NULL, *sprep = NULL, *cpt=NULL;
   char ssep_def[] = {"~"};
   char quote = '\0';
   
   if( argc < 3 || strcmp(argv[1],"-help") == 0 ){
      printf(
"Usage: 3dAttribute [options] aname dset\n"
 "Prints (to stdout) the value of the attribute 'aname' from\n"
 "the header of dataset 'dset'.  If the attribute doesn't exist,\n"
 "prints nothing and sets the exit status to 1.\n"
 "\n"
 "Options:\n"
 "  -name = Include attribute name in printout\n"
 "  -all  = Print all attributes [don't put aname on command line]\n"
 "          Also implies '-name'.  Attributes print in whatever order\n"
 "          they are in the .HEAD file, one per line.  You may want\n"
 "          to do '3dAttribute -all elvis+orig | sort' to get them\n"
 "          in alphabetical order.\n"
 "  -center = Center of volume in RAI coordinates.\n"
 "            Note that center is not itself an attribute in the \n"
 "           .HEAD file. It is calculated from other attributes.\n"
 "  Special options for string attributes:\n"
 "    -ssep SSEP    Use string SSEP as a separator between strings for\n"
 "                  multiple sub-bricks. The default is '~', which is what\n"
 "                  is used internally in AFNI's .HEAD file. For tcsh,\n"
 "                  I recommend ' ' which makes parsing easy, assuming each\n"
 "                  individual string contains no spaces to begin with.\n" 
 "                  Try -ssep 'NUM'\n"
 "    -sprep SPREP  Use string SPREP to replace blank space in string \n"
 "                  attributes.\n"
 "    -quote        Use single quote around each string.\n"
 "    Examples:\n"
 "       3dAttribute -quote -ssep ' '  BRICK_LABS SomeStatDset+tlrc.BRIK\n"
 "       3dAttribute -quote -ssep 'NUM' -sprep '+' BRICK_LABS SomeStatDset+tlrc.BRIK\n"
) ;
      PRINT_COMPILE_DATE ; exit(0) ;
   }

   while( nopt < argc && argv[nopt][0] == '-' ){

      if( strcmp(argv[nopt],"-all") == 0 ){
         do_all = do_name = 1 ;
         nopt++ ; continue ;
      }
      
      if( strcmp(argv[nopt],"-center") == 0 ){
         do_center = 1 ;
         nopt++ ; continue ;
      }
      
      if( strcmp(argv[nopt],"-quote") == 0 ){
         quote = '\'' ;
         nopt++ ; continue ;
      }
      
      if( strcmp(argv[nopt],"-ssep") == 0 ){
         nopt++ ;
         if (nopt >= argc) {
            fprintf(stderr,"*** Need string after -ssep\n");
            exit(1) ;
         }
         ssep = argv[nopt] ;
         nopt++ ; continue ;
      }
      
      if( strcmp(argv[nopt],"-sprep") == 0 ){
         nopt++ ;
         if (nopt >= argc) {
            fprintf(stderr,"*** Need string after -sprep\n");
            exit(1) ;
         }
         sprep = argv[nopt] ;
         nopt++ ; continue ;
      }
      
      if( strcmp(argv[nopt],"-name") == 0 ){
         do_name = 1 ;
         nopt++ ; continue ;
      }

      fprintf(stderr,"*** Illegal option: %s\n",argv[nopt]) ; exit(1) ;
   }

   if (!ssep) ssep = ssep_def;
   if( !do_all && !do_center) aname = argv[nopt++] ;

   /* Check to see if you have sub-brick selection */
   cpt = strstr(argv[nopt],"[") ;
   if( cpt == NULL ){
      HasSb = 0 ;
   } else if( cpt == argv[nopt] ){
      fprintf(stderr,"illegal dataset specifier: %s\n",argv[nopt]) ;
      exit(1) ;
   } else {
      HasSb = 1 ;
   }
   
   if (HasSb) {
      dset  = THD_open_dataset( argv[nopt] ) ;/* changed from open_one_ to allow 
                                                for getting sub-brick specific
                                                attributes ZSS-April 08 */
      if (ISVALID_DSET(dset)) {
         /* push hearder structure back to header attributes */
         /* NOTICE SOME ATTRIBUTES NOT LOADED INTO STRUCTS WILL VANISH */
         THD_set_dataset_attributes(dset);
      }
   } else {
      dset  = THD_open_one_dataset( argv[nopt] ) ;

      /* in either case (i.e. handle non-AFNI dsets)   16 Mar 2010 [rickr] */
      THD_set_dataset_attributes(dset);
   }
   if( !ISVALID_DSET(dset) ){
      fprintf(stderr,"*** Can't open dataset %s\n",argv[nopt]); exit(1);
   }
   
   if( !do_all && aname){
      atr = THD_find_atr( dset->dblk , aname ) ;
      if( atr == NULL ) {
         if (HasSb) {
            *cpt = '\0';
            /* see if attribute is present in parent dset but
            not preserved with sub-brick selection */
            dset = THD_open_one_dataset( argv[nopt] ) ; /* memory leak, 
                                                         but OK here */
            if (  ISVALID_DSET(dset) &&
                  THD_find_atr( dset->dblk , aname ) ) {
               fprintf(stderr,
                  "Failed to find attribute %s with sub-brick selection.\n"
                  "Since %s exists in %s then it is an attribute that \n"
                  "is not preserved with sub-brick selection.\n",
                  aname, aname, argv[nopt]);
            }
         }
         exit(1) ;                  /* failure */
      }
      atr_print( atr, ssep, sprep, quote, do_name ) ;
      exit(0) ;
   }

   if (do_all) {
      for( ia=0 ; ia < dset->dblk->natr ; ia++ ){
         atr = &(dset->dblk->atr[ia]) ;
         atr_print(atr, ssep, sprep, quote, do_name) ;
      }
   }

   if (do_center) {
      fv = THD_dataset_center( dset ) ;
      if (do_name) fprintf(stdout, "center = ");
      fprintf(stdout, "%f %f %f\n", fv.xyz[0], fv.xyz[1], fv.xyz[2]);
   }
   exit(0) ;
}
示例#29
0
/*! Replace a voxel's value by the value's rank in the entire set of input datasets */
int main( int argc , char * argv[] )
{
   THD_3dim_dataset ** dsets_in = NULL, *dset=NULL; /*input and output datasets*/
   int nopt=0, nbriks=0, nsubbriks=0, ib=0, isb=0;
   byte *cmask=NULL;
   int *all_uniques=NULL, **uniques=NULL, *final_unq=NULL, *N_uniques=NULL;
   int N_final_unq=0, iun=0, total_unq=0;
   INT_HASH_DATUM *rmap=NULL, *hd=NULL;
   int imax=0, iunq=0, ii=0, id = 0;
   long int off=0;
   char *prefix=NULL;
   char stmp[THD_MAX_PREFIX+1]={""}; 
   FILE *fout=NULL;

   /*----- Read command line -----*/
   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
      Rank_help ();
      exit(0) ;
   }

   mainENTRY("3dRank main"); machdep(); AFNI_logger("3dRank",argc,argv);
   nopt = 1 ;
   
   while( nopt < argc && argv[nopt][0] == '-' ){
      if( strcmp(argv[nopt],"-ver") == 0 ){
         PRINT_VERSION("3dRank"); AUTHOR("Ziad Saad");
         nopt++; continue;
      }

      if( strcmp(argv[nopt],"-help") == 0 ){
         Rank_help();
         exit(0) ;
      }

      if( strcmp(argv[nopt],"-prefix") == 0 ){
         ++nopt;
         if (nopt>=argc) {
            fprintf(stderr,"**ERROR: Need string after -prefix\n");
            exit(1);
         }
         prefix = argv[nopt] ;
         ++nopt; continue;
      }
      if( strcmp(argv[nopt],"-input") == 0 ){
         dsets_in = (THD_3dim_dataset**)
                        calloc(argc-nopt+1, sizeof(THD_3dim_dataset*));
         ++nopt; nbriks=0;
         while (nopt < argc ) {
            dsets_in[nbriks] = THD_open_dataset( argv[nopt] );
            if( !ISVALID_DSET(dsets_in[nbriks]) ){
              fprintf(stderr,"**ERROR: can't open dataset %s\n",argv[nopt]) ;
              exit(1);
            }
            ++nopt; ++nbriks; 
         }
         continue;
      }
      
      ERROR_exit( " Error - unknown option %s", argv[nopt]);
   } 
   if (nopt < argc) {
      ERROR_exit( " Error unexplained trailing option: %s\n", argv[nopt]);
   }
   if (!nbriks) {
      ERROR_exit( " Error no volumes entered on command line?");
   }
   
   /* some checks and inits*/
   nsubbriks = 0;
   for (ib = 0; ib<nbriks; ++ib) {
      if (!is_integral_dset(dsets_in[ib], 0)) {
         ERROR_exit("Dset %s is not of an integral data type.", 
                        DSET_PREFIX(dsets_in[ib]));
      }
      nsubbriks += DSET_NVALS(dsets_in[ib]);
   }
   
   /* Now get unique arrays */
   uniques = (int **)calloc(nsubbriks, sizeof(int*));
   N_uniques = (int *)calloc(nsubbriks, sizeof(int));
   total_unq = 0;
   iun = 0;
   for (ib = 0; ib<nbriks; ++ib) {
      DSET_mallocize(dsets_in[ib]); DSET_load(dsets_in[ib]);
      for (isb=0; isb<DSET_NVALS(dsets_in[ib]); ++isb) {
         uniques[iun] = THD_unique_vals(dsets_in[ib], isb,
                                        &(N_uniques[iun]), cmask);
         total_unq += N_uniques[iun]; 
         ++iun;
      }
   }
   
   /* put all the arrays together and get the unique of the uniques */
   all_uniques = (int *)calloc(total_unq, sizeof(int));
   off=0;
   for (iun=0; iun<nsubbriks; ++iun) {
      memcpy(all_uniques+off, uniques[iun], N_uniques[iun]*sizeof(int));
      off += N_uniques[iun];
   }
   
   /* free intermediate unique arrays */
   for (iun=0; iun<nsubbriks; ++iun) {
      free(uniques[iun]);
   }
   free(uniques); uniques=NULL;
   free(N_uniques); N_uniques=NULL;
   
   /* get unique of catenated array */
   if (!(final_unq = UniqueInt (all_uniques, total_unq, &N_final_unq, 0 ))) {
      ERROR_exit( " Failed to get unique list (%d, %d, %d) ", 
                  total_unq, N_final_unq, nsubbriks);
   }
   free(all_uniques); all_uniques=NULL;
  
   if (prefix) {
      snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
               "%s.rankmap.1D", prefix);
   } else {
      if (nbriks == 1) {
        snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
                  "%s.rankmap.1D", DSET_PREFIX(dsets_in[0]));
      } else { 
         snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
                  "%s+.rankmap.1D", DSET_PREFIX(dsets_in[0]));
      }
   }
      
   if (stmp[0]) {
      if ((fout = fopen(stmp,"w"))) {
         fprintf(fout, "#Rank Map (%d unique values)\n", N_final_unq);
         fprintf(fout, "#Col. 0: Rank\n");
         fprintf(fout, "#Col. 1: Input Dset Value\n");
      }
   } 

   
   /* get the maximum integer in the unique array */
   imax = 0;
   for (iunq=0; iunq<N_final_unq; ++iunq) {
      if (final_unq[iunq] > imax) imax = final_unq[iunq]; 
      if (fout) fprintf(fout, "%d   %d\n", iunq, final_unq[iunq]);
      hd = (INT_HASH_DATUM*)calloc(1,sizeof(INT_HASH_DATUM));
      hd->id = final_unq[iunq];
      hd->index = iunq;
      HASH_ADD_INT(rmap, id, hd); 
   }
   
   fclose(fout); fout=NULL;

   /* now cycle over all dsets and replace their voxel values with rank */
   for (ib = 0; ib<nbriks; ++ib) {
      for (isb=0; isb<DSET_NVALS(dsets_in[ib]); ++isb) {
         EDIT_BRICK_LABEL(  dsets_in[ib],isb, "rank" ) ;
         EDIT_BRICK_TO_NOSTAT(  dsets_in[ib],isb ) ;
         EDIT_BRICK_FACTOR( dsets_in[ib],isb, 0.0);/* no factors for rank*/
         switch (DSET_BRICK_TYPE(dsets_in[ib],isb) ){
            default:
               fprintf(stderr,
                        "** Bad dset type for unique operation.\n"
                        "Only Byte, Short, and float dsets are allowed.\n");
               break ; /* this should not happen here, 
                        so don't bother returning*/
            case MRI_short:{
               short *mar = (short *) DSET_ARRAY(dsets_in[ib],isb) ;
               if (imax >  MRI_TYPE_maxval[MRI_short]) {
                  WARNING_message("Maximum rank value of %d is\n"
                                  "than maximum value for dset datatype of %d\n",
                                  imax, MRI_TYPE_maxval[MRI_short]);
               }
               for( ii=0 ; ii < DSET_NVOX(dsets_in[ib]) ; ii++ )
                  if (!cmask || cmask[ii]) {
                     id = (int)mar[ii];
                     HASH_FIND_INT(rmap,&id ,hd);
                     if (hd) mar[ii] = (short)(hd->index); 
                     else 
                       ERROR_exit("** Failed to find key %d in hash table\n",id);
                  } else mar[ii] = 0;
            }
            break ;
            case MRI_byte:{
               byte *mar ;
               if (imax >  MRI_TYPE_maxval[MRI_short]) {
                  WARNING_message("Maximum rank value of %d is\n"
                                  "than maximum value for dset datatype of %d\n",
                                  imax, MRI_TYPE_maxval[MRI_byte]);
               }
               mar = (byte *) DSET_ARRAY(dsets_in[ib],isb) ;
               for( ii=0 ; ii < DSET_NVOX(dsets_in[ib]) ; ii++ )
                  if (!cmask || cmask[ii]) {
                     id = (int)mar[ii];
                     HASH_FIND_INT(rmap,&id ,hd);
                     if (hd) mar[ii] = (byte)(hd->index); 
                     else 
                       ERROR_exit("** Failed to find key %d in hash table\n",id);
                  } else mar[ii] = 0;
            }
            break ;
            case MRI_float:{
               float *mar = (float *) DSET_ARRAY(dsets_in[ib],isb) ;
               for( ii=0 ; ii < DSET_NVOX(dsets_in[ib]) ; ii++ )
                  if (!cmask || cmask[ii]) {
                     id = (int)mar[ii]; /* Assuming float is integral valued */
                     HASH_FIND_INT(rmap,&id ,hd);
                     if (hd) mar[ii] = (float)(hd->index); 
                     else 
                       ERROR_exit("** Failed to find key %d in hash table\n",id);
                  } else mar[ii] = 0;
            }
            break ;

         }
      }

      /* update range, etc. */
      THD_load_statistics(dsets_in[ib]);
      
      /* Now write the bricks */
      if (prefix) {
         if (nbriks == 1) { 
            snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
                     "%s", prefix);
         } else {
            snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
                     "r%02d.%s", ib, prefix);
         }
      } else {
         snprintf(stmp, sizeof(char)*THD_MAX_PREFIX, 
                  "rank.%s", DSET_PREFIX(dsets_in[ib]));
      }
      EDIT_dset_items( dsets_in[ib] ,
                       ADN_prefix   , stmp ,
                       ADN_none ) ;
      
      /* change storage mode, this way prefix will determine
         format of output dset */
      dsets_in[ib]->dblk->diskptr->storage_mode = STORAGE_BY_BRICK;
      
      tross_Make_History( "3dRank" , argc, argv , dsets_in[ib] ) ;
      if (DSET_IS_MASTERED(dsets_in[ib])) {
         /*  THD_write_3dim_dataset won't write a mastered dude */
         dset = EDIT_full_copy(dsets_in[ib],stmp); 
      } else {
         dset = dsets_in[ib];
      }
      
      /* New ID */
      ZERO_IDCODE(dset->idcode);
      dset->idcode = MCW_new_idcode() ;
      
      if (!THD_write_3dim_dataset( NULL, stmp, dset,True )) {
         ERROR_message("Failed to write %s", stmp);
         exit(1);  
      } else {
         WROTE_DSET(dsets_in[ib]); 
         if (dset != dsets_in[ib]) DSET_deletepp(dset);
         DSET_deletepp(dsets_in[ib]);
         
      }
   }
   
   /* destroy hash */
   while (rmap) {
      hd = rmap;
      HASH_DEL(rmap,hd);
      free(hd);
   }

   free(final_unq);  final_unq=NULL;
   
   exit(0);
}
示例#30
0
int main( int argc , char *argv[] )
{
   int vstep=0 , ii,nvox , ntin , ntout , do_one=0 , nup=-1 ;
   THD_3dim_dataset *inset=NULL , *outset ;
   char *prefix="Upsam", *dsetname=NULL ;
   int verb=0 , iarg=1, datum = MRI_float;
   float *ivec , *ovec , trin , trout, *fac=NULL, *ofac=NULL, 
         top=0.0, maxtop=0.0;

   /*------- help the pitifully ignorant user? -------*/

   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
     printf(
      "Usage: 3dUpsample [options] n dataset\n"
      "\n"
      "* Upsamples a 3D+time dataset, in the time direction,\n"
      "   by a factor of 'n'.\n"
      "* The value of 'n' must be between 2 and 320 (inclusive).\n"
      "* The output dataset is in float format by default.\n"
      "\n"
      "Options:\n"
      "--------\n"
      " -1 or -one = Use linear interpolation. Otherwise,\n"
      " or -linear   7th order polynomial interpolation is used.\n"
      "\n"
      " -prefix pp = Define the prefix name of the output dataset.\n"
      "              [default prefix is 'Upsam']\n"
      "\n"
      " -verb      = Be eloquently and mellifluosly verbose.\n"
      "\n"
      " -n n       = An alternate way to specify n\n"
      " -input dataset = An alternate way to specify dataset\n"
      "\n"
      " -datum ddd = Use datatype ddd at output. Choose from\n"
      "              float (default), short, byte.\n"
      "Example:\n"
      "--------\n"
      " 3dUpsample -prefix LongFred 5 Fred+orig\n"
      "\n"
      "Nota Bene:\n"
      "----------\n"
      "* You should not use this for files that were 3dTcat-ed across\n"
      "   imaging run boundaries, since that will result in interpolating\n"
      "   between non-contiguous time samples!\n"
      "* If the input has M time points, the output will have n*M time\n"
      "   points.  The last n-1 of them will be past the end of the original\n"
      "   time series.\n"
      "* This program gobbles up memory and diskspace as a function of n.\n"
      "  You can reduce output file size with -datum option.\n"
      "\n"
      "--- RW Cox - April 2008\n"
     ) ;
     PRINT_COMPILE_DATE ; exit(0) ;
   }

   mainENTRY("3dUpsample"); machdep();
   PRINT_VERSION("3dUpsample"); AUTHOR("RWCox") ;
   AFNI_logger("3dUpsample",argc,argv);

   /*------- read command line args -------*/

   datum = MRI_float;
   iarg = 1 ;
   while( iarg < argc && argv[iarg][0] == '-' ){

     if( strncasecmp(argv[iarg],"-prefix",5) == 0 ){
       if( ++iarg >= argc )
         ERROR_exit("Need argument after '%s'",argv[iarg-1]);
       prefix = argv[iarg] ;
       if( !THD_filename_ok(prefix) )
         ERROR_exit("Illegal string after -prefix: '%s'",prefix) ;
       iarg++ ; continue ;
     }

     if( strncasecmp(argv[iarg],"-one",4) == 0 ||
         strcmp     (argv[iarg],"-1"    ) == 0 ||
         strncasecmp(argv[iarg],"-lin",4) == 0   ){
       do_one = 1 ; iarg++ ; continue ;
     }

     if( strncasecmp(argv[iarg],"-verb",3) == 0 ){
       verb = 1 ; iarg++ ; continue ;
     }

     if( strcasecmp(argv[iarg],"-n") == 0 ){
      if( ++iarg >= argc )
         ERROR_exit("Need argument after '%s'",argv[iarg-1]);
      nup = (int)strtod(argv[iarg],NULL) ;
      if( nup < 2 || nup > 320 )
        ERROR_exit("3dUpsample rate '%d' is outside range 2..320",nup) ;
      iarg++ ; continue ;
     }

     if( strcasecmp(argv[iarg],"-input") == 0 ){
      if( ++iarg >= argc )
         ERROR_exit("Need argument after '%s'",argv[iarg-1]);
      dsetname = argv[iarg];
      iarg++ ; continue ;
     }
     
     if( strcasecmp(argv[iarg],"-datum") == 0 ){
      if( ++iarg >= argc )
         ERROR_exit("Need argument after '%s'",argv[iarg-1]);
      
         if( strcmp(argv[iarg],"short") == 0 ){
            datum = MRI_short ;
         } else if( strcmp(argv[iarg],"float") == 0 ){
            datum = MRI_float ;
         } else if( strcmp(argv[iarg],"byte") == 0 ){
            datum = MRI_byte ;
         } else {
            ERROR_message("-datum of type '%s' not supported in 3dUpsample!\n",
                    argv[iarg] ) ;
            exit(1) ;
         }
         
      iarg++ ; continue ;
     }
     
     ERROR_message("Unknown argument on command line: '%s'",argv[iarg]) ;
     suggest_best_prog_option(argv[0], argv[iarg]);
     exit (1);
   }

   /*------- check options for completeness and consistency -----*/
   
   if (nup == -1) {
      if( iarg+1 >= argc )
        ERROR_exit("need 'n' and 'dataset' on command line!") ;

      nup = (int)strtod(argv[iarg++],NULL) ;
      if( nup < 2 || nup > 320 )
        ERROR_exit("3dUpsample rate '%d' is outside range 2..320",nup) ;
   } 
   if (!dsetname) {
      if( iarg >= argc )
        ERROR_exit("need 'dataset' on command line!") ;
      dsetname = argv[iarg];
   }
   
   inset = THD_open_dataset(dsetname) ;
   if( !ISVALID_DSET(inset) )
     ERROR_exit("3dUpsample can't open dataset '%s'", dsetname) ;
   ntin = DSET_NVALS(inset) ; trin = DSET_TR(inset) ;
   if( ntin < 2 )
     ERROR_exit("dataset '%s' has only 1 value per voxel?!",dsetname) ;

   nvox = DSET_NVOX(inset) ;

   if( verb ) INFO_message("loading input dataset into memory") ;

   DSET_load(inset) ; CHECK_LOAD_ERROR(inset) ;


   /*------ create output dataset ------*/

   ntout = ntin * nup ; trout = trin / nup ;

   /* scaling factor for output */
   fac = NULL; maxtop = 0.0;
   if (MRI_IS_INT_TYPE(datum)) {
      fac = (float *)calloc(DSET_NVALS(inset), sizeof(float));
      ofac = (float *)calloc(ntout, sizeof(float));
      for (ii=0; ii<DSET_NVALS(inset); ++ii) {
         top = MCW_vol_amax( DSET_NVOX(inset),1,1 , 
                             DSET_BRICK_TYPE(inset,ii), 
                             DSET_BRICK_ARRAY(inset,ii) ) ;
         if (DSET_BRICK_FACTOR(inset, ii)) 
            top = top * DSET_BRICK_FACTOR(inset,ii);
         fac[ii] = (top > MRI_TYPE_maxval[datum]) ? 
                        top/MRI_TYPE_maxval[datum] : 0.0 ;
         if (top > maxtop) maxtop = top;
      }
      if (storage_mode_from_filename(prefix) != STORAGE_BY_BRICK) {
         fac[0] = (maxtop > MRI_TYPE_maxval[datum]) ? 
                        maxtop/MRI_TYPE_maxval[datum] : 0.0 ;
         for (ii=0; ii<ntout; ++ii) 
            ofac[ii] = fac[0];
         if (verb) INFO_message("Forcing global scaling, Max = %f, fac = %f\n", 
                        maxtop, fac[0]);
      } else {
         if (verb) INFO_message("Reusing scaling factors of input dset\n");
         upsample_1( nup, DSET_NVALS(inset), fac, ofac);
      }
   }
   free(fac); fac = NULL;
   outset = EDIT_empty_copy(inset) ;
   EDIT_dset_items( outset ,
                        ADN_nvals     , ntout          ,
                        ADN_ntt       , DSET_NUM_TIMES(inset) > 1 ? ntout : 0 ,
                        ADN_datum_all , datum      ,
                        ADN_brick_fac , ofac           ,
                        ADN_prefix    , prefix         ,
                      ADN_none ) ;
   tross_Copy_History( inset , outset ) ;
   tross_Make_History( "3dUpsample" , argc,argv , outset ) ;
   free(ofac); ofac = NULL;
   
   if( outset->taxis != NULL ){
     outset->taxis->ttdel /= nup ;
     outset->taxis->ttdur /= nup ;
     if( outset->taxis->toff_sl != NULL ){
       for( ii=0 ; ii < outset->taxis->nsl ; ii++ )
         outset->taxis->toff_sl[ii] /= nup ;
     }
   }

   for( ii=0 ; ii < ntout ; ii++ ){ /* create empty bricks to be filled below */
     EDIT_substitute_brick( outset , ii , datum , NULL ) ;
   }

   /*------- loop over voxels and process them one at a time ---------*/

   if( verb )
     INFO_message("Upsampling time series from %d to %d: %s interpolation",
                  ntin , ntout , (do_one) ? "linear" : "heptic" ) ;

   if( verb && nvox > 499 ) vstep = nvox / 50 ;
   if( vstep > 0 ) fprintf(stderr,"++ voxel loop: ") ;

   ivec = (float *)malloc(sizeof(float)*ntin) ;
   ovec = (float *)malloc(sizeof(float)*ntout) ;

   for( ii=0 ; ii < nvox ; ii++ ){

     if( vstep > 0 && ii%vstep==vstep-1 ) vstep_print() ;

     THD_extract_array( ii , inset , 0 , ivec ) ;

     if( do_one ) upsample_1( nup , ntin , ivec , ovec ) ;
     else         upsample_7( nup , ntin , ivec , ovec ) ;

     THD_insert_series( ii , outset , ntout , MRI_float , ovec , 
                        datum==MRI_float ? 1:0 ) ;
   } /* end of loop over voxels */

   if( vstep > 0 ) fprintf(stderr," Done!\n") ;

   /*----- clean up and go away -----*/

   DSET_write(outset) ;
   if( verb ) WROTE_DSET(outset) ;
   if( verb ) INFO_message("Total CPU time = %.1f s",COX_cpu_time()) ;
   exit(0);
}