void process_1ddata () { float * ffim = NULL; /* input list of p-values */ /*----- Set pointer to input array of p-values -----*/ ffim = FDR_input1D_data; /*----- Calculate FDR z-scores for all input p-values -----*/ process_volume (ffim, -1, NULL); if( FDR_output_prefix != NULL && ffim != NULL ){ MRI_IMAGE *im = mri_new_vol_empty( FDR_nxyz,1,1 , MRI_float ) ; mri_fix_data_pointer( ffim , im ) ; mri_write_1D( FDR_output_prefix , im ) ; mri_fix_data_pointer( NULL , im ) ; mri_free( im ) ; } /*----- Deallocate memory -----*/ if (ffim != NULL) { free (ffim); ffim = NULL; } }
MRI_IMAGE * mri_new_fvectim( int nx , int ny , int nz , int vdim ) { MRI_IMAGE *im ; im = mri_new_vol_empty( nx , ny , nz , MRI_fvect ) ; mri_adjust_fvectim( im , vdim ) ; return(im) ; }
/*-------------------------------------------------------*/ MRI_IMARR * dset_to_mri(THD_3dim_dataset * dset) /*--------------------------------------------------------*/ { int ii, kk, ntime, datum; int nvox, nx, ny, nz; int use_fac; MRI_IMARR * ims_in; MRI_IMAGE * im, *temp_im; byte ** bptr = NULL ; /* one of these will be the array of */ short ** sptr = NULL ; /* pointers to input dataset sub-bricks */ float ** fptr = NULL ; /* (depending on input datum type) */ float * fac = NULL ; /* array of brick scaling factors */ float * fout; ntime = DSET_NUM_TIMES(dset) ; nx = dset->daxes->nxx; ny = dset->daxes->nyy; nz = dset->daxes->nzz; nvox = dset->daxes->nxx * dset->daxes->nyy * dset->daxes->nzz ; datum = DSET_BRICK_TYPE( dset , 0 ) ; /* get dataset datum type */ switch( datum ){ /* pointer type depends on input datum type */ default: return NULL ; /** create array of pointers into old dataset sub-bricks **/ /*--------- input is bytes ----------*/ /* voxel #i at time #k is bptr[k][i] */ /* for i=0..nvox-1 and k=0..ntime-1. */ case MRI_byte: bptr = (byte **) malloc( sizeof(byte *) * ntime ) ; if( bptr == NULL ) return NULL ; for( kk=0 ; kk < ntime ; kk++ ) bptr[kk] = (byte *) DSET_ARRAY(dset,kk) ; break ; /*--------- input is shorts ---------*/ /* voxel #i at time #k is sptr[k][i] */ /* for i=0..nvox-1 and k=0..ntime-1. */ case MRI_short: sptr = (short **) malloc( sizeof(short *) * ntime ) ; if( sptr == NULL ) return NULL ; for( kk=0 ; kk < ntime; kk++ ) sptr[kk] = (short *) DSET_ARRAY(dset,kk) ; break ; /*--------- input is floats ---------*/ /* voxel #i at time #k is fptr[k][i] */ /* for i=0..nvox-1 and k=0..ntime-1. */ case MRI_float: fptr = (float **) malloc( sizeof(float *) * ntime) ; if( fptr == NULL ) return NULL ; for( kk=0 ; kk < ntime; kk++ ) fptr[kk] = (float *) DSET_ARRAY(dset,kk) ; break ; } /* end of switch on input type */ INIT_IMARR(ims_in) ; for( kk=0 ; kk < ntime ; kk++ ){ im = mri_new_vol_empty( nx , ny , nz , datum ) ; ADDTO_IMARR(ims_in,im) ; } for( kk=0 ; kk < ntime ; kk++ ){ im = IMARR_SUBIMAGE(ims_in,kk) ; switch( datum ){ case MRI_byte: mri_fix_data_pointer( bptr[kk], im ) ; break ; case MRI_short: mri_fix_data_pointer( sptr[kk], im ) ; break ; case MRI_float: mri_fix_data_pointer( fptr[kk], im ) ; break ; } } return(ims_in); }
int main( int argc , char * argv[] ) { /* --- variable declarations --- */ THD_3dim_dataset * dset ; THD_diskptr * dskptr; int nx, ny, nz, nv, itim; Boolean verbose, nsize; int ok; MRI_IMAGE * im, * im2d, * tim2d; MRI_TYPE kind; int ibr, iz, count, izz,izsub ; int zfirst, zlast, tfirst, tlast; char input_filename[THD_MAX_NAME], prefix_filename[THD_MAX_NAME], output_filename[THD_MAX_NAME], str[THD_MAX_NAME]; /* --- get user command line inputs --- */ F3D_initialize_user_data (argc, argv, &verbose, &nsize, &zfirst, &zlast, &tfirst, &tlast, input_filename, prefix_filename ); /* --- open 3d data set --- */ dset = THD_open_one_dataset( input_filename ) ; if( dset == NULL ) FatalError ("Unable to open input file") ; if ( verbose ) printf("EPsim: 3d Dataset File = %s\n" , input_filename ) ; /* --- load data block --- */ ok = THD_load_datablock( dset->dblk ); if ( !ok ) FatalError ("Unable to load data block") ; /* --- get data dimensions --- */ dskptr = dset->dblk->diskptr; nx = dskptr->dimsizes[0]; ny = dskptr->dimsizes[1]; nz = dskptr->dimsizes[2]; nv = dskptr->nvals; if ( verbose ) printf ("EPsim: nx=%d ny=%d nz=%d nv=%d\n", nx, ny, nz, nv); /* --- check for valid user inputs --- */ if (zfirst < 1) zfirst = 1; if (zlast > nz) zlast = nz; if (tfirst < 1) tfirst = 1; if (tlast > nv) tlast = nv; if (zfirst > nz) FatalError ("No data selected -- zfirst too large."); if (zlast < 1) FatalError ("No data selected -- zlast too small."); if (tfirst > nv) FatalError ("No data selected -- tfirst too large."); if (tlast < 1) FatalError ("No data selected -- tlast too small."); /* --- get data type --- */ kind = IMAGE_IN_IMARR ( dset->dblk->brick, 0 ) -> kind; if ( verbose ) printf ("EPsim: datum = %s \n", MRI_TYPE_name[kind]); /* --- create 2d data pointer --- */ im2d = mri_new_vol_empty ( nx, ny, 1, kind ); /*** open channel to AFNI ***/ sprintf( buf , "tcp:%s:%d" , host , get_port_named("CONTROL_PORT") ) ; ioc = iochan_init( buf , "create" ) ; if( ioc == NULL ) FatalError("Cannot open control channel to AFNI") ; if( verbose ) printf("EPsim: waiting for AFNI") ; fflush(stdout) ; while(1){ iz = iochan_goodcheck( ioc , 1000 ) ; if( iz < 0 ) FatalError("control channel failed") ; if( iz > 0 ) break ; if( verbose ){ printf(".") ; fflush(stdout) ; } } if( verbose ){ printf("!\n") ; fflush(stdout) ; } if( use_shm ) strcpy( buf , SHM_NAME ) ; else sprintf(buf , "tcp:%s:%d" , host , get_port_named("AFNI_PLUGOUT_TCP_BASE") ) ; if( use_child ){ jj = strlen(buf) ; sprintf(buf+jj,"\ncat epsim.out") ; } else if( use_3T ){ jj = strlen(buf) ; sprintf(buf+jj,"\n3T_toafni -dummy < %s" , fname_3T ) ; } if( verbose ) printf("sending control data: %s\n",buf) ; jj = iochan_sendall( ioc , buf , strlen(buf)+1 ) ; if( jj < 0 ) FatalError("send control data failed") ; iochan_sleep(LONG_DELAY) ; /* wait a bit */ while( ! iochan_clearcheck(ioc,LONG_DELAY) ) /* loop until cleared */ iochan_sleep(LONG_DELAY) ; if( verbose ) printf("EPsim: closing control channel\n") ; IOCHAN_CLOSE(ioc) ; /*** now open data channel ***/ ioc = iochan_init( buf , "create" ) ; if( ioc == NULL ) FatalError("Cannot open data channel to AFNI") ; if( verbose ) printf("EPsim: waiting for AFNI") ; fflush(stdout) ; while(1){ iz = iochan_goodcheck( ioc , 1000 ) ; if( iz < 0 ) FatalError("data channel failed") ; if( iz > 0 ) break ; if( verbose ){ printf(".") ; fflush(stdout) ; } } if( verbose ){ printf("!\n") ; fflush(stdout) ; } if( use_child ){ FILE * fp = fopen( "epsim.out" , "w" ) ; if( fp == NULL ){fprintf(stderr,"Can't open epsim.out!\n");IOCHAN_CLOSE(ioc);exit(1);} fprintf( fp , "ZNUM %d\n" "ZDELTA %g\n" "XYFOV %g %g\n" "ZFIRST %g%c\n" "ZORDER seq\n" "XYZAXES %s %s %s\n" "ACQUISITION_TYPE %s\n" , nz , fabs(dset->daxes->zzdel) , fabs(dset->daxes->xxdel)*nx , fabs(dset->daxes->yydel)*ny , fabs(dset->daxes->zzorg) , ORIENT_first[dset->daxes->zzorient] , ORIENT_shortstr[dset->daxes->xxorient] , ORIENT_shortstr[dset->daxes->yyorient] , ORIENT_shortstr[dset->daxes->zzorient] , ( ((zlast-zfirst)>0) ? "2D+zt" : "2D+z" ) ) ; fclose(fp) ; if( verbose ) printf("EPsim: wrote epsim.out file\n") ; sprintf( buf , "XYMATRIX %d %d\n" "DATUM %s\n" , nx , ny , MRI_TYPE_name[kind] ) ; } else if( use_3T ){ sprintf( buf , "XYMATRIX %d %d\n" "DATUM %s\n" , nx , ny , MRI_TYPE_name[kind] ) ; } else { sprintf( buf , "ZNUM %d\n" "ZDELTA %g\n" "XYFOV %g %g\n" "ZFIRST %g%c\n" "ZORDER seq\n" "XYZAXES %s %s %s\n" "ACQUISITION_TYPE %s\n" "XYMATRIX %d %d\n" "DATUM %s\n" , nz , fabs(dset->daxes->zzdel) , fabs(dset->daxes->xxdel)*nx , fabs(dset->daxes->yydel)*ny , fabs(dset->daxes->zzorg) , ORIENT_first[dset->daxes->zzorient] , ORIENT_shortstr[dset->daxes->xxorient] , ORIENT_shortstr[dset->daxes->yyorient] , ORIENT_shortstr[dset->daxes->zzorient] , ( ((zlast-zfirst)>0) ? "2D+zt" : "2D+z" ) , nx , ny , MRI_TYPE_name[kind] ) ; } nbytes = im2d->nvox * im2d->pixel_size ; for( itim=0 ; itim < ntimes ; itim++ ){ count = 0; if( use_3T ) izsub = (nz%2 == 0) ? (nz-1) : (nz) ; for ( ibr = tfirst-1 ; ibr < tlast ; ibr++ ) { for ( iz = zfirst-1 ; iz < zlast ; iz++ ) { /* --- set 2d data pointer into 3d data set --- */ im = IMAGE_IN_IMARR ( dset->dblk->brick, ibr ); if( use_3T ){ izz = 2*iz ; if( izz >= nz ) izz -= izsub ; /* alt ordering */ } else { izz = iz ; /* seq ordering */ } switch ( kind ) { case MRI_byte : mri_set_data_pointer(im2d, MRI_BYTE_PTR(im) + iz*nx*ny) ; break; case MRI_short : mri_set_data_pointer(im2d, MRI_SHORT_PTR(im) + iz*nx*ny) ; break; case MRI_int : mri_set_data_pointer(im2d, MRI_INT_PTR(im) + iz*nx*ny) ; break; case MRI_float : mri_set_data_pointer(im2d, MRI_FLOAT_PTR(im) + iz*nx*ny) ; break; case MRI_double : mri_set_data_pointer(im2d, MRI_DOUBLE_PTR(im) + iz*nx*ny) ; break; case MRI_complex : mri_set_data_pointer(im2d, MRI_COMPLEX_PTR(im) + iz*nx*ny) ; break; case MRI_rgb : mri_set_data_pointer(im2d, MRI_RGB_PTR(im) + 3*iz*nx*ny) ; break; default : FatalError ("Illegal data type encountered."); } #if 0 /* --- create 2d data file name --- */ strcpy ( output_filename, prefix_filename ); if ( nv > 1 ) sprintf ( str, "%02d.%04d", izz+1, ibr+1 ); else if ( nz > 999 ) sprintf ( str, ".%04d", izz+1 ); else sprintf ( str, ".%03d", izz+1 ); strcat ( output_filename, str ); #endif if( first ){ if( verbose ) printf("EPsim: sending this data as header info in image channel:\n%s\n",buf) ; jj = iochan_sendall( ioc , buf , strlen(buf)+1 ) ; if( jj < 0 ) FatalError("send header info failed") ; first = 0 ; } if ( verbose ) printf ( "EPsim: sending 2D image izz=%d ibr=%d\n", izz,ibr ); jj = iochan_sendall( ioc , mri_data_pointer(im2d) , nbytes ) ; if( jj < 0 ) FatalError("send image failed") ; iochan_sleep( delay ) ; #if 0 if ( !nsize ) ok = mri_write ( output_filename, im2d ); else { tim2d = mri_nsize (im2d); ok = mri_write ( output_filename, tim2d); mri_free (tim2d); } #endif count ++ ; } /* --- iz --- */ } /* --- ibr --- */ sleep(20) ; } /* -- itim --*/ if ( verbose ) printf ("Sent %d 2D images. \n", count); if( verbose ){ printf("Waiting for AFNI") ; fflush(stdout) ; } while(1){ jj = iochan_clearcheck(ioc,1000) ; if( jj ) break ; if( verbose ){ printf(".") ; fflush(stdout) ; } } if( verbose ) printf("!\n") ; iochan_sleep(100) ; IOCHAN_CLOSE(ioc) ; exit(0) ; }
void EDIT_add_bricklist( THD_3dim_dataset *dset , int nbr, int *tbr, float *fbr , void *sbr[] ) { int ibr , typ , nx,ny,nz , nvals,new_nvals ; THD_datablock *dblk ; MRI_IMAGE *qim ; char str[32] ; ENTRY("EDIT_add_bricklist") ; /**-- Sanity Checks --**/ if( ! ISVALID_3DIM_DATASET(dset) || nbr <= 0 ) EXRETURN; /* error! */ if( dset->dblk->brick == NULL ) EXRETURN; /* error! */ if( dset->dblk->malloc_type != DATABLOCK_MEM_MALLOC )EXRETURN; /* error! */ dblk = dset->dblk ; nvals = dblk->nvals ; nx = dblk->diskptr->dimsizes[0] ; ny = dblk->diskptr->dimsizes[1] ; nz = dblk->diskptr->dimsizes[2] ; /**-- reallocate the brick control information --**/ new_nvals = nvals + nbr ; dblk->brick_bytes = (int64_t *) XtRealloc( (char *) dblk->brick_bytes , sizeof(int64_t) * new_nvals ) ; dblk->brick_fac = (float *) XtRealloc( (char *) dblk->brick_fac , sizeof(float) * new_nvals ) ; dblk->nvals = dblk->diskptr->nvals = new_nvals ; /** allocate new sub-brick images **/ for( ibr=0 ; ibr < nbr ; ibr++ ){ typ = (tbr != NULL ) ? tbr[ibr] : MRI_short ; qim = mri_new_vol_empty( nx,ny,nz , typ ) ; /* image with no data */ if( sbr != NULL && sbr[ibr] != NULL ) /* attach data to image */ mri_fix_data_pointer( sbr[ibr] , qim ) ; ADDTO_IMARR( dblk->brick , qim ) ; /* attach image to dset */ dblk->brick_fac[nvals+ibr] = (fbr != NULL) ? fbr[ibr] : 0.0 ; dblk->brick_bytes[nvals+ibr] = (int64_t)qim->pixel_size * (int64_t)qim->nvox ; dblk->total_bytes += dblk->brick_bytes[ibr] ; } /** allocate new sub-brick auxiliary data: labels **/ if( dblk->brick_lab == NULL ) THD_init_datablock_labels( dblk ) ; else dblk->brick_lab = (char **) XtRealloc( (char *) dblk->brick_lab , sizeof(char *) * new_nvals ) ; for( ibr=0 ; ibr < nbr ; ibr++ ){ sprintf( str , "#%d" , nvals+ibr ) ; dblk->brick_lab[nvals+ibr] = NULL ; THD_store_datablock_label( dblk , nvals+ibr , str ) ; } /** keywords **/ if( dblk->brick_keywords == NULL ) THD_init_datablock_keywords( dblk ) ; else dblk->brick_keywords = (char **) XtRealloc( (char *) dblk->brick_keywords , sizeof(char *) * new_nvals ) ; for( ibr=0 ; ibr < nbr ; ibr++ ){ dblk->brick_keywords[nvals+ibr] = NULL ; THD_store_datablock_keywords( dblk , nvals+ibr , NULL ) ; } /** stataux **/ if( dblk->brick_statcode != NULL ){ dblk->brick_statcode = (int *) XtRealloc( (char *) dblk->brick_statcode , sizeof(int) * new_nvals ) ; dblk->brick_stataux = (float **) XtRealloc( (char *) dblk->brick_stataux , sizeof(float *) * new_nvals ) ; for( ibr=0 ; ibr < nbr ; ibr++ ){ dblk->brick_statcode[nvals+ibr] = 0 ; dblk->brick_stataux[nvals+ibr] = NULL ; } } /** fdrcurve **/ if( dblk->brick_fdrcurve != NULL ){ dblk->brick_fdrcurve = (floatvec **) realloc( (void *)dblk->brick_fdrcurve , sizeof(floatvec *) * new_nvals ) ; for( ibr=0 ; ibr < nbr ; ibr++ ) dblk->brick_fdrcurve[nvals+ibr] = NULL ; } if( dblk->brick_mdfcurve != NULL ){ /* 22 Oct 2008 */ dblk->brick_mdfcurve = (floatvec **) realloc( (void *)dblk->brick_mdfcurve , sizeof(floatvec *) * new_nvals ) ; for( ibr=0 ; ibr < nbr ; ibr++ ) dblk->brick_mdfcurve[nvals+ibr] = NULL ; } EXRETURN; }
char * MASKAVE_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; THD_3dim_dataset * input_dset , * mask_dset ; int iv , mcount , nvox , ii , sigmait , nvals=0 , doall , ivbot,ivtop ; float mask_bot=666.0 , mask_top=-666.0 ; double sum=0.0 , sigma=0.0 ; float * sumar=NULL , * sigmar=NULL ; char * tag , * str , buf[64] , abuf[32],sbuf[32] ; byte * mmm ; char * cname=NULL ; /* 06 Aug 1998 */ int cdisk=0 ; /* 22 Aug 2000 */ int miv=0 ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "*************************\n" "MASKAVE_main: NULL input\n" "*************************" ; /*-- read 1st line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; input_dset = PLUTO_find_dset(idc) ; if( input_dset == NULL ) return "********************************\n" "MASKAVE_main: bad input dataset\n" "********************************" ; iv = (int) PLUTO_get_number(plint) ; if( iv >= DSET_NVALS(input_dset) ) return "**********************************\n" "MASKAVE_main: bad input sub-brick\n" "**********************************" ; doall = (iv < 0) ; if( doall ){ nvals = DSET_NVALS(input_dset) ; ivbot = 0 ; ivtop = nvals-1 ; } else { ivbot = ivtop = iv ; } DSET_load(input_dset) ; if( DSET_ARRAY(input_dset,ivbot) == NULL ) return "*********************************\n" "MASKAVE_main: can't load dataset\n" "*********************************" ; nvox = DSET_NVOX(input_dset) ; /*-- read 2nd line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; mask_dset = PLUTO_find_dset(idc) ; if( mask_dset == NULL ) return "*******************************\n" "MASKAVE_main: bad mask dataset\n" "*******************************" ; if( DSET_NVOX(mask_dset) != nvox ) return "*************************************************************\n" "MASKAVE_main: mask input dataset doesn't match source dataset\n" "*************************************************************" ; miv = (int) PLUTO_get_number(plint) ; /* 06 Aug 1998 */ if( miv >= DSET_NVALS(mask_dset) ) return "*****************************************************\n" "MASKAVE_main: mask dataset sub-brick index is too big\n" "*****************************************************" ; DSET_load(mask_dset) ; if( DSET_ARRAY(mask_dset,0) == NULL ) return "**************************************\n" "MASKAVE_main: can't load mask dataset\n" "**************************************" ; /*-- read optional lines --*/ while( (tag=PLUTO_get_optiontag(plint)) != NULL ){ if( strcmp(tag,"Range") == 0 ){ mask_bot = PLUTO_get_number(plint) ; mask_top = PLUTO_get_number(plint) ; continue ; } if( strcmp(tag,"1D Save") == 0 ){ char * yn ; cname = PLUTO_get_string(plint) ; yn = PLUTO_get_string(plint) ; cdisk = (strcmp(yn,yesno_list[0]) == 0) ; continue ; } } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ /*-- build a byte mask array --*/ mmm = (byte *) malloc( sizeof(byte) * nvox ) ; if( mmm == NULL ) return "*** Can't malloc workspace! ***" ; /* separate code for each input data type */ switch( DSET_BRICK_TYPE(mask_dset,miv) ){ default: free(mmm) ; return "*** Can't use mask dataset -- illegal data type! ***" ; case MRI_short:{ short mbot , mtop ; short * mar = (short *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = SHORTIZE(mask_bot/mfac) ; mtop = SHORTIZE(mask_top/mfac) ; } else { mbot = (short) -MRI_TYPE_maxval[MRI_short] ; mtop = (short) MRI_TYPE_maxval[MRI_short] ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; case MRI_byte:{ byte mbot , mtop ; byte * mar = (byte *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = BYTEIZE(mask_bot/mfac) ; mtop = BYTEIZE(mask_top/mfac) ; if( mtop == 0 ){ free(mmm) ; return "*** Illegal mask range for mask dataset of bytes. ***" ; } } else { mbot = 0 ; mtop = (byte) MRI_TYPE_maxval[MRI_short] ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; case MRI_float:{ float mbot , mtop ; float * mar = (float *) DSET_ARRAY(mask_dset,miv) ; float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ; if( mfac == 0.0 ) mfac = 1.0 ; if( mask_bot <= mask_top ){ mbot = (float) (mask_bot/mfac) ; mtop = (float) (mask_top/mfac) ; } else { mbot = -WAY_BIG ; mtop = WAY_BIG ; } for( mcount=0,ii=0 ; ii < nvox ; ii++ ) if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ){ mmm[ii] = 1 ; mcount++ ; } else { mmm[ii] = 0 ; } } break ; } if( mcount == 0 ){ free(mmm) ; return "*** No voxels survive the masking operations! ***" ; } sigmait = (mcount > 1) ; /*-- compute statistics --*/ if( doall ){ sumar = (float *) malloc( sizeof(float) * nvals ) ; sigmar = (float *) malloc( sizeof(float) * nvals ) ; } for( iv=ivbot ; iv <= ivtop ; iv++ ){ sum = sigma = 0.0 ; /* 13 Dec 1999 */ switch( DSET_BRICK_TYPE(input_dset,iv) ){ default: free(mmm) ; if( doall ){ free(sumar) ; free(sigmar) ; } return "*** Can't use source dataset -- illegal data type! ***" ; case MRI_short:{ short * bar = (short *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; case MRI_byte:{ byte * bar = (byte *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; case MRI_float:{ float * bar = (float *) DSET_ARRAY(input_dset,iv) ; float mfac = DSET_BRICK_FACTOR(input_dset,iv) ; if( mfac == 0.0 ) mfac = 1.0 ; for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sum += bar[ii] ; sum = sum / mcount ; if( sigmait ){ for( ii=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) sigma += SQR(bar[ii]-sum) ; sigma = mfac * sqrt( sigma/(mcount-1) ) ; } sum = mfac * sum ; } break ; } if( doall ){ sumar[iv] = sum ; sigmar[iv] = sigma ; } } free(mmm) ; /*-- send report --*/ if( doall ){ str = (char *) malloc( 1024 + 64*nvals ) ; sprintf(str," ****** ROI statistics ****** \n" " Source = %s [all sub-bricks] \n" " Mask = %s [%s]" , DSET_FILECODE(input_dset) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) ) ; if( mask_bot <= mask_top ){ sprintf(buf," [range %g .. %g]" , mask_bot , mask_top ) ; strcat(str,buf) ; } strcat(str," \n") ; sprintf(buf," Count = %d voxels\n",mcount) ; strcat(str,buf) ; for( iv=0 ; iv < nvals ; iv++ ){ AV_fval_to_char( sumar[iv] , abuf ) ; AV_fval_to_char( sigmar[iv] , sbuf ) ; sprintf(buf," Average = %9.9s Sigma = %9.9s [%s] \n", abuf,sbuf , DSET_BRICK_LABEL(input_dset,iv) ) ; strcat(str,buf) ; } PLUTO_popup_textwin( plint , str ) ; /* 06 Aug 1998 */ if( cname != NULL && cname[0] != '\0' ){ MRI_IMAGE * qim = mri_new_vol_empty( nvals,1,1 , MRI_float ) ; mri_fix_data_pointer( sumar , qim ) ; PLUTO_register_timeseries( cname , qim ) ; if( cdisk ){ /* 22 Aug 2000 */ if( PLUTO_prefix_ok(cname) ){ char * cn ; if( strstr(cname,".1D") == NULL ){ cn = malloc(strlen(cname)+8) ; strcpy(cn,cname) ; strcat(cn,".1D") ; } else { cn = cname ; } mri_write_1D( cn , qim ) ; if( cn != cname ) free(cn) ; } else { PLUTO_popup_transient(plint," \n" "** Illegal filename **\n" "** in 'To Disk?' !! **\n" ) ; } } mri_fix_data_pointer( NULL , qim ) ; mri_free(qim) ; } free(str) ; free(sumar) ; free(sigmar) ; } else if( mask_bot <= mask_top ){ str = (char *) malloc( 1024 ) ; sprintf( str , " *** ROI Statistics *** \n" " Source = %s [%s] \n" " Mask = %s [%s] [range %g .. %g] \n" " Count = %d voxels \n" " Average = %g \n" " Sigma = %g " , DSET_FILECODE(input_dset) , DSET_BRICK_LABEL(input_dset,ivbot) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) , mask_bot , mask_top , mcount , sum , sigma ) ; PLUTO_popup_message(plint,str) ; free(str) ; } else { str = (char *) malloc( 1024 ) ; sprintf( str , " *** ROI Statistics *** \n" " Source = %s [%s] \n" " Mask = %s [%s] \n" " Count = %d voxels \n" " Average = %g \n" " Sigma = %g " , DSET_FILECODE(input_dset) , DSET_BRICK_LABEL(input_dset,ivbot) , DSET_FILECODE(mask_dset) , DSET_BRICK_LABEL(mask_dset,miv) , mcount , sum , sigma ) ; PLUTO_popup_message(plint,str) ; free(str) ; } return NULL ; }
void process_volume (float * ffim, int statcode, float * stataux) { int ixyz; /* voxel index */ int icount; /* count of sorted p-values */ float fval; /* voxel input statistical value */ float pval; /* voxel input stat. p-value */ float qval; /* voxel FDR q-value */ float zval; /* voxel FDR z-score */ float qval_min; /* smallest previous q-value */ voxel * head_voxel = NULL; /* linked list of voxels */ voxel * voxel_ptr = NULL; /* pointer to current voxel */ int ibin; /* p-value bin */ int * iarray = NULL; /* output array of voxel indices */ float * parray = NULL; /* output array of voxel p-values */ float * qarray = NULL; /* output array of voxel FDR q-values */ float * zarray = NULL; /* output array of voxel FDR z-scores */ float numer ; /*------------ 18 Jan 2008: use the 'new' method? ------------*/ if( FDR_old < 1 ){ MRI_IMAGE *qim ; int flags=0 ; qim = mri_new_vol_empty( FDR_nxyz,1,1 , MRI_float ) ; mri_fix_data_pointer( ffim , qim ) ; if( FDR_mask != NULL ){ float zz = (FUNC_IS_STAT(statcode)) ? 0.0f : 1.0f ; for( ixyz=0 ; ixyz < FDR_nxyz ; ixyz++ ) if( !FDR_mask[ixyz] ) ffim[ixyz] = zz ; } if( FDR_curve ){ /* hidden option: produce t-z curve */ floatvec *fv = mri_fdr_curve( qim , statcode , stataux ) ; if( fv == NULL ) ERROR_message("mri_fdr_curve fails!") ; else { printf("# FDR thresh-z curve\n") ; for( ixyz=0 ; ixyz < fv->nar ; ixyz++ ) printf("%g %g\n", fv->x0+ixyz*fv->dx , fv->ar[ixyz] ) ; } exit(0) ; } else { /* normal operation: convert to z(q) or q */ if( FDR_pmask == 0 ) flags |= 1 ; /* compatibility mode */ if( FDR_cn > 1.0f ) flags |= 2 ; /* dependency flag */ if( FDR_qval ) flags |= 4 ; /* qval flag */ (void)mri_fdrize( qim , statcode,stataux , flags ) ; } mri_clear_data_pointer(qim); mri_free(qim); return ; } /*---------------- back to the 'old' method ------------------*/ /*----- Allocate memory for screen output arrays -----*/ if (FDR_list) { iarray = (int *) malloc (sizeof(int) * FDR_nthr); MTEST(iarray); parray = (float *) malloc (sizeof(float) * FDR_nthr); MTEST(parray); qarray = (float *) malloc (sizeof(float) * FDR_nthr); MTEST(qarray); zarray = (float *) malloc (sizeof(float) * FDR_nthr); MTEST(zarray); } /*----- Loop over all voxels; sort p-values -----*/ icount = FDR_nthr; for (ixyz = 0; ixyz < FDR_nxyz; ixyz++) { /*----- First, check if voxel is inside the mask -----*/ if( FDR_mask != NULL && !FDR_mask[ixyz] ) continue; /*----- Convert stats to p-values -----*/ fval = fabs(ffim[ixyz]); if (statcode <= 0) pval = fval; else pval = THD_stat_to_pval (fval, statcode, stataux); if (pval >= 1.0) { /*----- Count but don't sort voxels with p-value = 1 -----*/ icount--; if (FDR_list) { iarray[icount] = ixyz; parray[icount] = 1.0; qarray[icount] = 1.0; zarray[icount] = 0.0; } } else { /*----- Place voxel in p-value bin -----*/ ibin = (int) (pval * (FDR_MAX_LL)); if (ibin < 0) ibin = 0; if (ibin > FDR_MAX_LL-1) ibin = FDR_MAX_LL-1; head_voxel = new_voxel (ixyz, pval, FDR_head_voxel[ibin]); FDR_head_voxel[ibin] = head_voxel; } } /*----- Calculate FDR q-values -----*/ qval_min = 1.0; ibin = FDR_MAX_LL-1; numer = (FDR_pmask) ? icount : FDR_nthr ; /* 18 Jan 2008 */ while (ibin >= 0) { voxel_ptr = FDR_head_voxel[ibin]; while (voxel_ptr != NULL) { /*----- Convert sorted p-values to FDR q-values -----*/ pval = voxel_ptr->pvalue; qval = FDR_cn * (pval*numer) / icount; if (qval > qval_min) qval = qval_min; else qval_min = qval; /*----- Convert FDR q-value to FDR z-score -----*/ if( !FDR_qval ){ if (qval < 1.0e-20) zval = 10.0; else zval = normal_p2t(qval); } else { zval = qval ; } icount--; /*----- Save calculated values -----*/ if (FDR_list) { iarray[icount] = voxel_ptr->ixyz; parray[icount] = pval; qarray[icount] = qval; zarray[icount] = zval; } voxel_ptr->pvalue = zval; voxel_ptr = voxel_ptr->next_voxel; } ibin--; } /*----- Write out the calculated values -----*/ if (FDR_list) { printf ("%12s %12s %12s %12s \n", "Index", "p-value", "q-value", "z-score"); for (icount = 0; icount < FDR_nthr; icount++) { if (FDR_input1D_filename != NULL) ixyz = iarray[icount] + 1; else ixyz = iarray[icount]; printf ("%12d %12.6f %12.6f %12.6f \n", ixyz, parray[icount], qarray[icount], zarray[icount]); } /*----- Deallocate memory for output arrays -----*/ free (iarray); free (parray); free (qarray); free (zarray); } /*----- Place FDR z-scores into float array -----*/ save_all_voxels (ffim); /*----- Deallocate linked-list memory -----*/ delete_all_voxels(); }
static char * BFIT_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; THD_3dim_dataset * input_dset , * mask_dset = NULL ; BFIT_data * bfd ; BFIT_result * bfr ; int nvals,ival , nran,nvox , nbin , miv=0 , sqr,sqt ; float abot,atop,bbot,btop,pcut , eps,eps1 , hlast ; float *bval , *cval ; double aa,bb,xc ; double chq,ccc,cdf ; int ihqbot,ihqtop ; int mcount,mgood , ii , jj , ibot,itop ; float mask_bot=666.0 , mask_top=-666.0 , hbot,htop,dbin ; char buf[THD_MAX_NAME+128] , tbuf[THD_MAX_NAME+128] , * tag ; int * hbin , * jbin,*kbin=NULL , *jist[2] ; MRI_IMAGE * flim ; double aext=-1.0,bext=-1.0 ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "************************\n" "BFIT_main: NULL input\n" "************************" ; /*-- read 1st line --*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; input_dset = PLUTO_find_dset(idc) ; if( input_dset == NULL ) return "****************************\n" "BFIT_main: bad input dataset\n" "****************************" ; nvox = DSET_NVOX(input_dset) ; nvals = DSET_NVALS(input_dset) ; ival = (int) PLUTO_get_number(plint) ; if( ival < 0 || ival >= nvals ) return "**************************\n" "BFIT_main: bad Brick index\n" "**************************" ; DSET_load(input_dset) ; if( DSET_ARRAY(input_dset,0) == NULL ) return "*****************************\n" "BFIT_main: can't load dataset\n" "*****************************" ; tag = PLUTO_get_string(plint) ; sqr = PLUTO_string_index(tag,NYESNO,YESNO_strings) ; /*-- read 2nd line --*/ PLUTO_next_option(plint) ; abot = PLUTO_get_number(plint) ; atop = PLUTO_get_number(plint) ; if( atop <= abot ) return "*** atop <= abot! ***" ; PLUTO_next_option(plint) ; bbot = PLUTO_get_number(plint) ; btop = PLUTO_get_number(plint) ; if( atop <= abot ) return "*** btop <= bbot! ***" ; hlast = PLUTO_get_number(plint) ; PLUTO_next_option(plint) ; nran = (int) PLUTO_get_number(plint) ; pcut = PLUTO_get_number(plint) ; tag = PLUTO_get_string(plint) ; sqt = PLUTO_string_index(tag,NYESNO,YESNO_strings) ; /*-- read optional lines --*/ while( (tag=PLUTO_get_optiontag(plint)) != NULL ){ /*-- Mask itself --*/ if( strcmp(tag,"Mask") == 0 ){ idc = PLUTO_get_idcode(plint) ; mask_dset = PLUTO_find_dset(idc) ; if( mask_dset == NULL ){ return "******************************\n" "BFIT_main: bad mask dataset\n" "******************************" ; } if( DSET_NVOX(mask_dset) != nvox ){ return "************************************************************\n" "BFIT_main: mask input dataset doesn't match source dataset\n" "************************************************************" ; } miv = (int) PLUTO_get_number(plint) ; if( miv >= DSET_NVALS(mask_dset) || miv < 0 ){ return "****************************************************\n" "BFIT_main: mask dataset sub-brick index is illegal\n" "****************************************************" ; } DSET_load(mask_dset) ; if( DSET_ARRAY(mask_dset,miv) == NULL ){ return "*************************************\n" "BFIT_main: can't load mask dataset\n" "*************************************" ; } continue ; } /*-- Mask range of values --*/ if( strcmp(tag,"Range") == 0 ){ if( mask_dset == NULL ){ return "******************************************\n" "BFIT_main: Can't use Range without Mask\n" "******************************************" ; } mask_bot = PLUTO_get_number(plint) ; mask_top = PLUTO_get_number(plint) ; continue ; } /*-- Extra plot --*/ if( strcmp(tag,"Extra") == 0 ){ aext = PLUTO_get_number(plint) ; bext = PLUTO_get_number(plint) ; continue ; } } /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ bfd = BFIT_prepare_dataset( input_dset , ival , sqr , mask_dset , miv , mask_bot , mask_top ) ; if( bfd == NULL ) return "*** BFIT_prepare_dataset fails ***" ; bfr = BFIT_compute( bfd , pcut , abot,atop , bbot,btop , nran,200 ) ; if( bfr == NULL ){ BFIT_free_data( bfd ) ; return "*** BFIT_compute fails! ***" ; } itop = bfr->itop ; mgood = bfr->mgood ; ibot = bfd->ibot ; bval = bfd->bval ; cval = bfd->cval ; mcount = bfd->mcount ; xc = bfr->xcut ; aa = bfr->a ; bb = bfr->b ; eps = bfr->eps ; eps1 = 1.0 - eps ; if( eps1 > 1.0 ) eps1 = 1.0 ; eps1 = (mcount-ibot) * eps1 ; /*-- compute and plot histogram --*/ /* original data was already squared (e.g., R**2 values) */ if( !sqr ){ hbot = 0.0 ; htop = 1.0 ; nbin = 200 ; if( bval[mcount-1] < 1.0 ) htop = bval[mcount-1] ; dbin = (htop-hbot)/nbin ; hbin = (int *) calloc((nbin+1),sizeof(int)) ; /* actual histogram */ jbin = (int *) calloc((nbin+1),sizeof(int)) ; /* theoretical fit */ for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ jbin[ii] = (int)( eps1 * ( beta_t2p(hbot+ii*dbin,aa,bb) -beta_t2p(hbot+ii*dbin+dbin,aa,bb) ) ) ; } jist[0] = jbin ; flim = mri_new_vol_empty( mcount-ibot,1,1 , MRI_float ) ; mri_fix_data_pointer( bval+ibot , flim ) ; mri_histogram( flim , hbot,htop , TRUE , nbin,hbin ) ; /* "extra" histogram (nominal values?) */ if( aext > 0.0 ){ kbin = (int *) calloc((nbin+1),sizeof(int)) ; jist[1] = kbin ; for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ kbin[ii] = (int)( eps1 * ( beta_t2p(hbot+ii*dbin,aext,bext) -beta_t2p(hbot+ii*dbin+dbin,aext,bext) ) ) ; } } } else { /* original data was not squared (e.g., correlations) */ double hb,ht ; htop = 1.0 ; nbin = 200 ; if( bval[mcount-1] < 1.0 ) htop = sqrt(bval[mcount-1]) ; hbot = -htop ; dbin = (htop-hbot)/nbin ; hbin = (int *) calloc((nbin+1),sizeof(int)) ; /* actual histogram */ jbin = (int *) calloc((nbin+1),sizeof(int)) ; /* theoretical fit */ for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ hb = hbot+ii*dbin ; ht = hb+dbin ; hb = hb*hb ; ht = ht*ht ; if( hb > ht ){ double qq=hb ; hb=ht ; ht=qq ; } jbin[ii] = (int)( 0.5*eps1 * ( beta_t2p(hb,aa,bb) -beta_t2p(ht,aa,bb) ) ) ; } jist[0] = jbin ; flim = mri_new_vol_empty( mcount-ibot,1,1 , MRI_float ) ; mri_fix_data_pointer( cval+ibot , flim ) ; mri_histogram( flim , hbot,htop , TRUE , nbin,hbin ) ; /* nominal fit */ if( aext > 0.0 ){ kbin = (int *) calloc((nbin+1),sizeof(int)) ; jist[1] = kbin ; for( ii=0 ; ii < nbin ; ii++ ){ /* beta fit */ hb = hbot+ii*dbin ; ht = hb+dbin ; hb = hb*hb ; ht = ht*ht ; if( hb > ht ){ double qq=hb ; hb=ht ; ht=qq ; } kbin[ii] = (int)( 0.5*eps1 * ( beta_t2p(hb,aext,bext) -beta_t2p(ht,aext,bext) ) ) ; } } } sprintf(buf,"%s[%d] a=%.2f b=%.2f \\epsilon=%.2f %%=%.0f", DSET_FILECODE(input_dset),ival,aa,bb,eps,pcut ) ; ccc = bfr->q_chisq ; /* blow up histogram details by sqrt-ing, if ordered */ if( sqt ){ for( ii=0 ; ii < nbin ; ii++ ){ hbin[ii] = (int) sqrt( (double)(100*hbin[ii]+0.5) ) ; jbin[ii] = (int) sqrt( (double)(100*jbin[ii]+0.5) ) ; if( kbin!=NULL ) kbin[ii] = (int) sqrt( (double)(100*kbin[ii]+0.5) ) ; } } /* and plot */ sprintf(tbuf,"\\beta fit: cutoff=%.2f nvox=%d q(\\chi^2)=%8.2e", (sqr)?sqrt(xc):xc , mgood , ccc ) ; if( sqt ){ ii = strlen(tbuf) ; sprintf( tbuf+ii , " \\surd ogram" ) ; } if( hlast > 0.0 ){ hbin[nbin-1] = jbin[nbin-1] = hlast ; if( kbin != NULL ) kbin[nbin-1] = hlast ; } PLUTO_histoplot( nbin,hbot,htop,hbin , tbuf,NULL,buf , (kbin==NULL)?1:2 , jist ) ; /* cleanup */ mri_clear_data_pointer(flim) ; mri_free(flim) ; free(hbin) ; free(jbin) ; if( kbin != NULL ) free(kbin); BFIT_free_data(bfd) ; BFIT_free_result(bfr) ; return NULL ; }
char * IMREG_main( PLUGIN_interface * plint ) { MCW_idcode * idc ; /* input dataset idcode */ THD_3dim_dataset * old_dset , * new_dset ; /* input and output datasets */ char * new_prefix , * str ; /* strings from user */ int base , ntime , datum , nx,ny,nz , ii,kk , npix ; float dx,dy,dz ; MRI_IMARR * ims_in , * ims_out ; MRI_IMAGE * im , * imbase ; byte ** bptr = NULL , ** bout = NULL ; short ** sptr = NULL , ** sout = NULL ; float ** fptr = NULL , ** fout = NULL ; float * dxar = NULL , * dyar = NULL , * phiar = NULL ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ /*--------- go to first input line ---------*/ PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; /* get dataset item */ old_dset = PLUTO_find_dset(idc) ; /* get ptr to dataset */ if( old_dset == NULL ) return "*************************\n" "Cannot find Input Dataset\n" "*************************" ; ntime = DSET_NUM_TIMES(old_dset) ; if( ntime < 2 ) return "*****************************\n" "Dataset has only 1 time point\n" "*****************************" ; ii = DSET_NVALS_PER_TIME(old_dset) ; if( ii > 1 ) return "************************************\n" "Dataset has > 1 value per time point\n" "************************************" ; nx = old_dset->daxes->nxx ; dx = old_dset->daxes->xxdel ; ny = old_dset->daxes->nyy ; dy = old_dset->daxes->yydel ; npix = nx*ny ; nz = old_dset->daxes->nzz ; dz = old_dset->daxes->zzdel ; if( nx != ny || fabs(dx) != fabs(dy) ) { #ifdef IMREG_DEBUG fprintf(stderr,"\nIMREG: nx=%d ny=%d nz=%d dx=%f dy=%f dz=%f\n", nx,ny,nz,dx,dy,dz ) ; #endif return "***********************************\n" "Dataset does not have square slices\n" "***********************************" ; } new_prefix = PLUTO_get_string(plint) ; /* get string item (the output prefix) */ if( ! PLUTO_prefix_ok(new_prefix) ) /* check if it is OK */ return "************************\n" "Output Prefix is illegal\n" "************************" ; /*--------- go to next input line ---------*/ PLUTO_next_option(plint) ; base = PLUTO_get_number(plint) ; if( base >= ntime ) return "********************\n" "Base value too large\n" "********************" ; /*--------- see if the 3rd option line is present --------*/ str = PLUTO_get_optiontag( plint ) ; if( str != NULL ) { float fsig , fdxy , fdph ; fsig = PLUTO_get_number(plint) * 0.42466090 ; fdxy = PLUTO_get_number(plint) ; fdph = PLUTO_get_number(plint) ; mri_align_params( 0 , 0.0,0.0,0.0 , fsig,fdxy,fdph ) ; /* fprintf(stderr,"Set fine params = %f %f %f\n",fsig,fdxy,fdph) ; */ } /*------------- ready to compute new dataset -----------*/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: loading dataset\n") ; #endif DSET_load( old_dset ) ; /*** 1) Copy the dataset in toto ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: Copying dataset\n") ; #endif new_dset = PLUTO_copy_dset( old_dset , new_prefix ) ; if( new_dset == NULL ) return "****************************\n" "Failed to copy input dataset\n" "****************************" ; /*** 2) Make an array of empty images ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: making empty images\n") ; #endif datum = DSET_BRICK_TYPE(new_dset,0) ; INIT_IMARR(ims_in) ; for( ii=0 ; ii < ntime ; ii++ ) { im = mri_new_vol_empty( nx , ny , 1 , datum ) ; ADDTO_IMARR(ims_in,im) ; } imbase = mri_new_vol_empty( nx , ny , 1 , datum ) ; dxar = (float *) malloc( sizeof(float) * ntime ) ; dyar = (float *) malloc( sizeof(float) * ntime ) ; phiar = (float *) malloc( sizeof(float) * ntime ) ; /*** 3) Get pointers to sub-bricks in old and new datasets ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: getting input brick pointers\n") ; #endif switch( datum ) { /* pointer type depends on input datum type */ case MRI_byte: bptr = (byte **) malloc( sizeof(byte *) * ntime ) ; bout = (byte **) malloc( sizeof(byte *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { bptr[ii] = (byte *) DSET_ARRAY(old_dset,ii) ; bout[ii] = (byte *) DSET_ARRAY(new_dset,ii) ; } break ; case MRI_short: sptr = (short **) malloc( sizeof(short *) * ntime ) ; sout = (short **) malloc( sizeof(short *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { sptr[ii] = (short *) DSET_ARRAY(old_dset,ii) ; sout[ii] = (short *) DSET_ARRAY(new_dset,ii) ; } #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: sptr[0] = %p sout[0] = %p\n",sptr[0],sout[0]) ; #endif break ; case MRI_float: fptr = (float **) malloc( sizeof(float *) * ntime ) ; fout = (float **) malloc( sizeof(float *) * ntime ) ; for( ii=0 ; ii < ntime ; ii++ ) { fptr[ii] = (float *) DSET_ARRAY(old_dset,ii) ; fout[ii] = (float *) DSET_ARRAY(new_dset,ii) ; } break ; } /*** 4) Loop over slices ***/ PLUTO_popup_meter(plint) ; for( kk=0 ; kk < nz ; kk++ ) { /*** 4a) Setup ims_in images to point to input slices ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- setup input images\n",kk) ; #endif for( ii=0 ; ii < ntime ; ii++ ) { im = IMARR_SUBIMAGE(ims_in,ii) ; switch( datum ) { case MRI_byte: mri_fix_data_pointer( bptr[ii] + kk*npix, im ) ; break ; case MRI_short: mri_fix_data_pointer( sptr[ii] + kk*npix, im ) ; break ; case MRI_float: mri_fix_data_pointer( fptr[ii] + kk*npix, im ) ; break ; } } /*** 4b) Setup im to point to base image ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- setup base image\n",kk) ; #endif switch( datum ) { case MRI_byte: mri_fix_data_pointer( bptr[base] + kk*npix, imbase ) ; break ; case MRI_short: mri_fix_data_pointer( sptr[base] + kk*npix, imbase ) ; break ; case MRI_float: mri_fix_data_pointer( fptr[base] + kk*npix, imbase ) ; break ; } /*** 4c) Register this slice at all times ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- register\n",kk) ; #endif ims_out = mri_align_dfspace( imbase , NULL , ims_in , ALIGN_REGISTER_CODE , dxar,dyar,phiar ) ; if( ims_out == NULL ) fprintf(stderr,"IMREG: mri_align_dfspace return NULL\n") ; /*** 4d) Put the output back in on top of the input; note that the output is always in MRI_float format ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: slice %d -- put output back into dataset\n",kk) ; #endif for( ii=0 ; ii < ntime ; ii++ ) { switch( datum ) { case MRI_byte: im = mri_to_mri( MRI_byte , IMARR_SUBIMAGE(ims_out,ii) ) ; memcpy( bout[ii] + kk*npix , MRI_BYTE_PTR(im) , sizeof(byte)*npix ) ; mri_free(im) ; break ; case MRI_short: #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: conversion to short at ii=%d\n",ii) ; #endif im = mri_to_mri( MRI_short , IMARR_SUBIMAGE(ims_out,ii) ) ; #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: copying to %p from %p\n",sout[ii] + kk*npix,MRI_SHORT_PTR(im)) ; #endif memcpy( sout[ii] + kk*npix , MRI_SHORT_PTR(im) , sizeof(short)*npix ) ; #ifdef IMREG_DEBUG if( ii==0 )fprintf(stderr,"IMREG: freeing\n") ; #endif mri_free(im) ; break ; case MRI_float: im = IMARR_SUBIMAGE(ims_out,ii) ; memcpy( fout[ii] + kk*npix , MRI_FLOAT_PTR(im) , sizeof(float)*npix ) ; break ; } } PLUTO_set_meter(plint, (100*(kk+1))/nz ) ; /*** 4e) Destroy the output images ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: destroying aligned output\n") ; #endif DESTROY_IMARR( ims_out ) ; } /*** 5) Destroy the empty images and other workspaces ***/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: destroy workspaces\n") ; #endif mri_clear_data_pointer(imbase) ; mri_free(imbase) ; for( ii=0 ; ii < ntime ; ii++ ) { im = IMARR_SUBIMAGE(ims_in,ii) ; mri_clear_data_pointer(im) ; } DESTROY_IMARR(ims_in) ; FREE_WORKSPACE ; /*------------- let AFNI know about the new dataset ------------*/ #ifdef IMREG_DEBUG fprintf(stderr,"IMREG: send result to AFNI\n") ; #endif PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ; return NULL ; /* null string returned means all was OK */ }