/* Calculates the average value for each voxel in dset across all timepoints. PRE: dset is a valid 3D+T dataset with at least 1 timepoint; ignore is the number of timepoints to ignore at the beginning of dset; 0 <= ignore < number of timepoints in dset; POST: return value is NULL on error, else, return value is an array of floats with the same dimensions as the subbricks of dset, containing the voxel value averages; Note: The caller is responsible for free()ing the returned block of memory when done. Note2: The complex datatype is not supported, and any such bricks will result in an error (NULL return value). */ double * RIC_CalcVoxelMeans(const THD_3dim_dataset * dset, int ignore) { double * avg; /* The voxel averages to be returned */ float scalefactor; /* Current dset brick scaling factor */ int ival, nvals; /* Current, number of dset timepoints */ int ivox, nvoxs; /* Current, number of dset brick voxels */ /* Quick check of arguments */ if (!ISVALID_3DIM_DATASET(dset) || DSET_NVALS(dset) < 1 || ignore < 0 || ignore >= DSET_NVALS(dset)) { return NULL; } /* Initialize */ DSET_load(dset); nvals = DSET_NVALS(dset); nvoxs = dset->daxes->nxx * dset->daxes->nyy * dset->daxes->nzz; avg = malloc(sizeof(double) * nvoxs); if (avg == NULL) { return NULL; } /* Calculate the voxel averages; treat matrices as 1-D arrays */ /* Zero the voxel sums */ for (ivox = 0; ivox < nvoxs; ivox += 1) { avg[ivox] = 0.0; } /* Sum each voxel across time (and hope there are not too many points) */ for (ival = ignore; ival < nvals; ival += 1) { scalefactor = DSET_BRICK_FACTOR(dset, ival); switch (DSET_BRICK_TYPE(dset, ival)) { case MRI_short: RIC_CALCVOXELMEANS__DO_VOXSUM(short); break; case MRI_byte: RIC_CALCVOXELMEANS__DO_VOXSUM(byte); break; case MRI_float: RIC_CALCVOXELMEANS__DO_VOXSUM(float); break; default: /* Unsupported datatype */ free(avg); return NULL; } } /* Divide by number of timepoints to get average */ nvals -= ignore; /* We do not average over the ignored timepoints */ for (ivox = 0; ivox < nvoxs; ivox += 1) { avg[ivox] /= nvals; } return avg; }
void get_options ( int argc, /* number of input arguments */ char ** argv /* array of input arguments */ ) { int nopt = 1; /* input option argument counter */ int ival, index; /* integer input */ float fval; /* float input */ char message[MAX_STRING_LENGTH]; /* error message */ /*----- does user request help menu? -----*/ if (argc < 2 || strncmp(argv[1], "-help", 5) == 0) display_help_menu(); /*----- main loop over input options -----*/ while (nopt < argc ) { /*----- -anat filename -----*/ if (strncmp(argv[nopt], "-anat", 5) == 0) { nopt++; if (nopt >= argc) estPDF_error ("need argument after -anat "); anat_filename = malloc (sizeof(char) * MAX_STRING_LENGTH); MTEST (anat_filename); strcpy (anat_filename, argv[nopt]); anat = THD_open_one_dataset (anat_filename); if (!ISVALID_3DIM_DATASET (anat)) { sprintf (message, "Can't open dataset: %s\n", anat_filename); estPDF_error (message); } DSET_load(anat); CHECK_LOAD_ERROR(anat); nopt++; continue; } /*----- unknown command -----*/ sprintf(message,"Unrecognized command line option: %s\n", argv[nopt]); estPDF_error (message); } }
/* * check count against limit * - clear small values * - if not count, set large values to 1 */ int limit_to_frac(THD_3dim_dataset * cset, int limit, int count, int verb) { short * dptr; int index, nsub, nsuper; ENTRY("limit_to_frac"); if( ! ISVALID_3DIM_DATASET(cset) ) { ERROR_message("invalid count dataset"); RETURN(1); } else if( DSET_BRICK_TYPE(cset, 0) != MRI_short ) { ERROR_message("count dataset not of type short"); RETURN(1); } if(verb > 1) INFO_message("limiting to %d (count = %d)\n",limit,count); /* note how many voxels are affected, just for kicks */ dptr = DBLK_ARRAY(cset->dblk, 0); nsub = nsuper = 0; for(index = 0; index < DSET_NVOX(cset); index++, dptr++) { if( ! *dptr ) continue; /* 0, so skip */ else if( *dptr < limit ) { /* small, so clear */ *dptr = 0; nsub++; } else { /* big enough */ if ( ! count ) *dptr = 1; nsuper++; } } /* entertain the user */ if( verb ) INFO_message("voxel limits: %d clipped, %d survived, %d were zero\n", nsub, nsuper, DSET_NVOX(cset)-nsub-nsuper); RETURN(0); }
char * RENAME_main( PLUGIN_interface * plint ) { char * new_prefix ; MCW_idcode * idc ; THD_3dim_dataset * dset ; char * old_header_name , * old_brick_name ; THD_slist_find find ; THD_session * ss ; int iss , id , ivv , ierr , mm ; /*--------------------------------------------------------------------*/ /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/ if( plint == NULL ) return "***********************\n" "RENAME_main: NULL input\n" "***********************" ; PLUTO_next_option(plint) ; idc = PLUTO_get_idcode(plint) ; dset = PLUTO_find_dset(idc) ; if( dset == NULL ) return "******************************\n" "RENAME_main:bad input dataset\n" "******************************" ; PLUTO_next_option(plint) ; new_prefix = PLUTO_get_string(plint) ; if( ! PLUTO_prefix_ok(new_prefix) ) return "***********************\n" "RENAME_main:bad prefix\n" "***********************" ; /*------------------------------------------------------*/ /*---------- At this point, the inputs are OK ----------*/ /*-- find this dataset in the AFNI library --*/ find = THD_dset_in_sessionlist( FIND_IDCODE, idc, GLOBAL_library.sslist, -1 ) ; iss = find.sess_index ; ss = GLOBAL_library.sslist->ssar[iss] ; id = find.dset_index ; /*-- for each element of this row, change its internal names and, if needed, filenames on disk --*/ ierr = 0 ; for( ivv=FIRST_VIEW_TYPE ; ivv <= LAST_VIEW_TYPE ; ivv++ ){ dset = GET_SESSION_DSET(ss, id, ivv); if( ! ISVALID_3DIM_DATASET(dset) ) continue ; /* skip this one */ /*-- copy the old filenames --*/ old_header_name = XtNewString( dset->dblk->diskptr->header_name ) ; old_brick_name = XtNewString( dset->dblk->diskptr->brick_name ) ; /*-- initialize the new filenames inside the dataset --*/ EDIT_dset_items( dset , ADN_prefix , new_prefix , ADN_none ) ; /*-- rename the old files to the new files, if they exist on disk --*/ if( THD_is_file(old_header_name) ) ierr += rename( old_header_name , dset->dblk->diskptr->header_name ) ; /* May 1998: fix .BRIK rename to allow for compression */ #if 0 if( THD_is_file(old_brick_name) ) ierr += rename( old_brick_name , dset->dblk->diskptr->brick_name ) ; #else mm = COMPRESS_filecode(old_brick_name) ; if( mm != COMPRESS_NOFILE ){ char * old_name = COMPRESS_add_suffix(old_brick_name,mm) ; char * new_name = COMPRESS_add_suffix(dset->dblk->diskptr->brick_name,mm) ; ierr += rename( old_name , new_name ) ; free(old_name) ; free(new_name) ; } #endif XtFree(old_header_name) ; XtFree(old_brick_name) ; } /*-- clean up AFNI --*/ PLUTO_fixup_names() ; /*-- done --*/ if( ierr ) return "***********************************************\n" "RENAME_main: some file rename operations failed\n" "***********************************************" ; return NULL ; }
void THD_set_dataset_attributes( THD_3dim_dataset *dset ) { THD_datablock *blk ; THD_dataxes *daxes ; THD_diskptr *dkptr ; int itemp[IFILL_DIM] , ii ; float ftemp[FFILL_DIM] ; int id , nx , ny , nz , nv , nxy , nxyz , ibr , nb ; int atrank[ATRSIZE_DATASET_RANK] , atdims[ATRSIZE_DATASET_DIMENSIONS] ; MRI_IMAGE *im ; int save_order ; THD_dmat33 tmat ; THD_dfvec3 tvec ; mat44 Tc, Tr; float angle; char name[666] ; floatvec *fv ; ENTRY("THD_set_dataset_attributes") ; /*-- sanity checks --*/ if( ! ISVALID_3DIM_DATASET(dset) || ! ISVALID_DATABLOCK(dset->dblk) || ! ISVALID_DISKPTR(dset->dblk->diskptr) ) EXRETURN ; blk = dset->dblk ; daxes = dset->daxes ; /* always used fixed daxes */ dkptr = blk->diskptr ; /******/ /****** These attributes used to be set in THD_write_3dim_dataset() *****/ /******/ /*----- write TYPESTRING attribute -----*/ THD_set_string_atr( blk , ATRNAME_TYPESTRING , DATASET_typestr[dset->type] ) ; /*----- write IDCODE attributes -----*/ THD_set_string_atr( blk , ATRNAME_IDSTRING , dset->idcode.str ) ; THD_set_string_atr( blk , ATRNAME_IDDATE , dset->idcode.date ) ; if( ! ISZERO_IDCODE(dset->anat_parent_idcode) ) THD_set_string_atr( blk, ATRNAME_IDANATPAR, dset->anat_parent_idcode.str ); else THD_erase_one_atr ( blk, ATRNAME_IDANATPAR ) ; if( ! ISZERO_IDCODE(dset->warp_parent_idcode) ) THD_set_string_atr( blk, ATRNAME_IDWARPPAR, dset->warp_parent_idcode.str ); else THD_erase_one_atr ( blk, ATRNAME_IDWARPPAR ) ; /*----- write SCENE_TYPE attribute -----*/ itemp[0] = dset->view_type ; itemp[1] = dset->func_type ; itemp[2] = dset->type ; ITFILL(3,ATRSIZE_SCENE_TYPE) ; THD_set_int_atr( blk , ATRNAME_SCENE_TYPE , ATRSIZE_SCENE_TYPE , itemp ) ; /*----- write data labels -----*/ if( strlen(dset->self_name) == 0 ) DSET_FIX_NAMES(dset) ; THD_set_string_atr( blk , ATRNAME_LABEL1 , dset->label1 ) ; THD_set_string_atr( blk , ATRNAME_LABEL2 , dset->label2 ) ; THD_set_string_atr( blk , ATRNAME_DATANAME , dset->self_name ) ; if( dset->keywords != NULL ) THD_set_string_atr( blk , ATRNAME_KEYWORDS , dset->keywords ) ; else THD_erase_one_atr ( blk , ATRNAME_KEYWORDS ) ; /*----- write parent names, if they exist -----*/ if( strlen(dset->warp_parent_name) > 0 ) THD_set_string_atr( blk , ATRNAME_WARP_PARENT , dset->warp_parent_name ) ; else THD_erase_one_atr ( blk , ATRNAME_WARP_PARENT ) ; if( strlen(dset->anat_parent_name) > 0 ) THD_set_string_atr( blk , ATRNAME_ANATOMY_PARENT , dset->anat_parent_name ) ; else THD_erase_one_atr ( blk , ATRNAME_ANATOMY_PARENT ) ; /*----- write axes orientation -----*/ itemp[0] = daxes->xxorient ; itemp[1] = daxes->yyorient ; itemp[2] = daxes->zzorient ; ITFILL(3,ATRSIZE_ORIENT_SPECIFIC) ; THD_set_int_atr( blk , ATRNAME_ORIENT_SPECIFIC , ATRSIZE_ORIENT_SPECIFIC , itemp ) ; /*----- write axes origin -----*/ ftemp[0] = daxes->xxorg ; ftemp[1] = daxes->yyorg ; ftemp[2] = daxes->zzorg ; FTFILL(3,ATRSIZE_ORIGIN) ; THD_set_float_atr( blk , ATRNAME_ORIGIN , ATRSIZE_ORIGIN , ftemp ) ; /*----- write axes spacings -----*/ ftemp[0] = daxes->xxdel ; ftemp[1] = daxes->yydel ; ftemp[2] = daxes->zzdel ; FTFILL(3,ATRSIZE_DELTA) ; THD_set_float_atr( blk , ATRNAME_DELTA , ATRSIZE_DELTA , ftemp ) ; /*-- write matrix for (i,j,k) to DICOM (x,y,z) conversion [15 Dec 2005] --*/ if( !ISVALID_MAT44(daxes->ijk_to_dicom) ) THD_daxes_to_mat44( daxes ) ; if( ISVALID_MAT44(daxes->ijk_to_dicom) ){ UNLOAD_MAT44(daxes->ijk_to_dicom, ftemp[0],ftemp[1],ftemp[2],ftemp[3], ftemp[4],ftemp[5],ftemp[6],ftemp[7], ftemp[8],ftemp[9],ftemp[10],ftemp[11] ); THD_set_float_atr( blk , "IJK_TO_DICOM" , 12 , ftemp ) ; } /*-- write matrix for (i,j,k) to DICOM real (x,y,z) conversion [18 May 2007] --*/ /* to store obliquity information */ if(!THD_update_obliquity_status()){ /* maybe update the obliquity unless refitting */ THD_check_oblique_field(dset); if (ISVALID_MAT44(dset->daxes->ijk_to_dicom_real)){ /* if not oblique already,compute Tc (Cardinal transformation matrix) */ angle = THD_compute_oblique_angle(daxes->ijk_to_dicom_real, 0); if(angle==0.0){ THD_dicom_card_xform(dset, &tmat, &tvec); LOAD_MAT44(Tc, tmat.mat[0][0], tmat.mat[0][1], tmat.mat[0][2], tvec.xyz[0], tmat.mat[1][0], tmat.mat[1][1], tmat.mat[1][2], tvec.xyz[1], tmat.mat[2][0], tmat.mat[2][1], tmat.mat[2][2], tvec.xyz[2]); daxes->ijk_to_dicom_real = Tc; } } } if( ISVALID_MAT44(daxes->ijk_to_dicom_real) ){ UNLOAD_MAT44(daxes->ijk_to_dicom_real, ftemp[0],ftemp[1],ftemp[2],ftemp[3], ftemp[4],ftemp[5],ftemp[6],ftemp[7], ftemp[8],ftemp[9],ftemp[10],ftemp[11] ); THD_set_float_atr( blk , "IJK_TO_DICOM_REAL" , 12 , ftemp ) ; } /*----- write markers, if present -----*/ if( dset->markers != NULL ){ for( ii=0 ; ii < MARKS_MAXNUM ; ii++ ){ /* put bad data in */ if( !dset->markers->valid[ii] ) /* invalid markers */ dset->markers->xyz[ii][0] = dset->markers->xyz[ii][1] = dset->markers->xyz[ii][2] = FFILL ; } THD_set_float_atr( blk , ATRNAME_MARKSXYZ , ATRSIZE_MARKSXYZ , &(dset->markers->xyz[0][0]) ) ; THD_set_char_atr( blk , ATRNAME_MARKSLAB , ATRSIZE_MARKSLAB , &(dset->markers->label[0][0]) ) ; THD_set_char_atr( blk , ATRNAME_MARKSHELP , ATRSIZE_MARKSHELP , &(dset->markers->help[0][0]) ) ; THD_set_int_atr( blk , ATRNAME_MARKSFLAG , ATRSIZE_MARKSFLAG , &(dset->markers->aflags[0]) ) ; } else { THD_erase_one_atr( blk , ATRNAME_MARKSXYZ ) ; THD_erase_one_atr( blk , ATRNAME_MARKSLAB ) ; THD_erase_one_atr( blk , ATRNAME_MARKSHELP ) ; THD_erase_one_atr( blk , ATRNAME_MARKSFLAG ) ; } /*----- write warp, if present -----*/ if( dset->warp != NULL ){ int wdata_size = 0 ; switch( dset->warp->type ){ case WARP_AFFINE_TYPE:{ THD_affine_warp *ww = (THD_affine_warp *) dset->warp ; itemp[0] = WARP_AFFINE_TYPE ; itemp[1] = ww->resam_type ; wdata_size = MAPPING_LINEAR_FSIZE ; COPY_FROM_STRUCT( ww->warp , MAPPING_LINEAR_FSTART , float , ftemp , MAPPING_LINEAR_FSIZE ) ; } break ; case WARP_TALAIRACH_12_TYPE:{ THD_talairach_12_warp *ww = (THD_talairach_12_warp *) dset->warp ; int iw , ioff ; itemp[0] = WARP_TALAIRACH_12_TYPE ; itemp[1] = ww->resam_type ; wdata_size = WARP_TALAIRACH_12_SIZE ; for( iw=0 ; iw < 12 ; iw++ ){ ioff = iw * MAPPING_LINEAR_FSIZE ; COPY_FROM_STRUCT( ww->warp[iw] , MAPPING_LINEAR_FSTART , float , &(ftemp[ioff]) , MAPPING_LINEAR_FSIZE ) ; } } break ; } /* end of switch on warp type */ ITFILL(2,ATRSIZE_WARP_TYPE) ; THD_set_int_atr( blk , ATRNAME_WARP_TYPE , ATRSIZE_WARP_TYPE , itemp ) ; THD_set_float_atr( blk , ATRNAME_WARP_DATA , wdata_size , ftemp ) ; } else { /* no warp exists */
char * THD_dataset_info( THD_3dim_dataset *dset , int verbose ) { THD_dataxes *daxes ; THD_fvec3 fv1 , fv2 , fv3 ; int ival , ntimes , nval_per , n1,n2,n3 , kv,npar ; float tf, angle=0.0; long long tb ; static char *RR="[R]" , *LL="[L]" , *PP="[P]" , *AA="[A]" , *SS="[S]" , *II="[I]" , *ZZ=" " ; char *xlbot , *xltop , *ylbot , *yltop , *zlbot , *zltop , *cpt ; char str[1024], soblq[1024] ; int nstr , obliquity; char *outbuf = NULL ; /* output buffer */ ENTRY("THD_dataset_info") ; if( ! ISVALID_3DIM_DATASET(dset) ) RETURN(NULL) ; daxes = dset->daxes ; if( DSET_IS_BRIK(dset) ) outbuf = THD_zzprintf(outbuf,"Dataset File: %s\n" , DSET_FILECODE(dset) ) ; else outbuf = THD_zzprintf(outbuf,"Dataset File: %s\n" , DSET_BRIKNAME(dset) ) ; outbuf = THD_zzprintf(outbuf,"Identifier Code: %s Creation Date: %s\n" , dset->idcode.str , dset->idcode.date ) ; outbuf = THD_zzprintf(outbuf, "Template Space: %s\n", dset->atlas_space); if( ISANAT(dset) ){ outbuf = THD_zzprintf(outbuf,"Dataset Type: %s (-%s)\n", ANAT_typestr[dset->func_type] , ANAT_prefixstr[dset->func_type] ) ; } else { outbuf = THD_zzprintf(outbuf,"Dataset Type: %s (-%s)\n", FUNC_typestr[dset->func_type] , FUNC_prefixstr[dset->func_type] ) ; } /* 25 April 1998: do byte order stuff */ switch( DSET_BYTEORDER(dset) ){ case LSB_FIRST: outbuf = THD_zzprintf(outbuf,"Byte Order: %s" , LSB_FIRST_STRING) ; break ; case MSB_FIRST: outbuf = THD_zzprintf(outbuf,"Byte Order: %s" , MSB_FIRST_STRING) ; break ; } if( THD_find_string_atr(dset->dblk,ATRNAME_BYTEORDER) == NULL ) /* 19 Sep 1999 */ outbuf = THD_zzprintf(outbuf," {assumed}") ; kv = mri_short_order() ; switch( kv ){ case LSB_FIRST: outbuf = THD_zzprintf(outbuf," [this CPU native = %s]\n" , LSB_FIRST_STRING) ; break ; case MSB_FIRST: outbuf = THD_zzprintf(outbuf," [this CPU native = %s]\n" , MSB_FIRST_STRING) ; break ; } /*-- 21 Jun 2002: print storage mode --*/ if( dset->dblk->diskptr != NULL ){ outbuf = THD_zzprintf(outbuf,"Storage Mode: %s\n", storage_mode_str(dset->dblk->diskptr->storage_mode)); } tb = dset->dblk->total_bytes ; if( tb > 0 ) outbuf = THD_zzprintf(outbuf,"Storage Space: %s (%s) bytes\n", commaized_integer_string(dset->dblk->total_bytes) , approximate_number_string(dset->dblk->total_bytes) ) ; /*-- keywords --*/ if( verbose >= 0 ){ cpt = DSET_KEYWORDS(dset) ; if( cpt != NULL && cpt[0] != '\0' ){ int j = strlen(cpt) ; if( j < 99 ){ outbuf = THD_zzprintf(outbuf,"Keywords: %s\n" , cpt ) ; } else { int k ; outbuf = THD_zzprintf(outbuf,"\n----- KEYWORDS -----\n") ; for( k=0 ; k < j ; k += ZMAX ) outbuf = THD_zzprintf(outbuf,SZMAX,cpt+k) ; outbuf = THD_zzprintf(outbuf,"\n") ; } } } /*-- idcodes --*/ if( verbose >= 0 ){ if( ! ISZERO_IDCODE(dset->anat_parent_idcode) ) outbuf = THD_zzprintf(outbuf,"Anatomy Parent: %s [%s]\n" , dset->anat_parent_name , dset->anat_parent_idcode.str ) ; else if( strlen(dset->anat_parent_name) > 0 ) outbuf = THD_zzprintf(outbuf,"Anatomy Parent: %s\n" , dset->anat_parent_name ) ; if( ! ISZERO_IDCODE(dset->warp_parent_idcode) ) outbuf = THD_zzprintf(outbuf,"Warp Parent: %s [%s]\n" , dset->warp_parent_name , dset->warp_parent_idcode.str) ; else if( strlen(dset->warp_parent_name) > 0 ) outbuf = THD_zzprintf(outbuf,"Warp Parent: %s\n" , dset->warp_parent_name ) ; } /*-- tagset --*/ if( verbose > 0 && dset->tagset != NULL && dset->tagset->num > 0 ){ int ii , ns=0 ; for( ii=0 ; ii < dset->tagset->num ; ii++ ) if( dset->tagset->tag[ii].set ) ns++ ; outbuf = THD_zzprintf(outbuf,"Tagset: %d set [out of %d total]\n", ns , dset->tagset->num ) ; } /* are we oblique ? */ if((obliquity = dset_obliquity(dset, &angle)) >= 0) { if(angle>0.0) { sprintf (soblq, "Data Axes Tilt: Oblique (%.3f deg. from plumb)\n" "Data Axes Approximate Orientation:", angle); } else { sprintf (soblq, "Data Axes Tilt: Plumb\n" "Data Axes Orientation:"); } { char *gstr = EDIT_get_geometry_string(dset) ; if( gstr != NULL && *gstr != '\0' ) outbuf = THD_zzprintf(outbuf,"Geometry String: \"%s\"\n",gstr) ; } } else { sprintf (soblq, "Data Axes Tilt: Unspecified, assumed plumb\n" "Data Axes Orientation:"); } outbuf = THD_zzprintf(outbuf, "%s\n" " first (x) = %s\n" " second (y) = %s\n" " third (z) = %s [-orient %c%c%c]\n" , soblq, ORIENT_typestr[daxes->xxorient] , ORIENT_typestr[daxes->yyorient] , ORIENT_typestr[daxes->zzorient] , ORIENT_typestr[daxes->xxorient][0] , ORIENT_typestr[daxes->yyorient][0] , ORIENT_typestr[daxes->zzorient][0] ) ; LOAD_FVEC3(fv1 , daxes->xxorg , daxes->yyorg , daxes->zzorg) ; fv1 = THD_3dmm_to_dicomm( dset , fv1 ) ; LOAD_FVEC3(fv2 , daxes->xxorg + (daxes->nxx-1)*daxes->xxdel , daxes->yyorg + (daxes->nyy-1)*daxes->yydel , daxes->zzorg + (daxes->nzz-1)*daxes->zzdel ) ; fv2 = THD_3dmm_to_dicomm( dset , fv2 ) ; if( fv1.xyz[0] > fv2.xyz[0] ) FSWAP( fv1.xyz[0] , fv2.xyz[0] ) ; if( fv1.xyz[1] > fv2.xyz[1] ) FSWAP( fv1.xyz[1] , fv2.xyz[1] ) ; if( fv1.xyz[2] > fv2.xyz[2] ) FSWAP( fv1.xyz[2] , fv2.xyz[2] ) ; LOAD_FVEC3(fv3 , daxes->xxdel , daxes->yydel , daxes->zzdel) ; fv3 = THD_3dmm_to_dicomm( dset , fv3 ) ; XLAB(xlbot,fv1.xyz[0]) ; YLAB(ylbot,fv1.xyz[1]) ; ZLAB(zlbot,fv1.xyz[2]) ; XLAB(xltop,fv2.xyz[0]) ; YLAB(yltop,fv2.xyz[1]) ; ZLAB(zltop,fv2.xyz[2]) ; n1 = DAXES_NUM(daxes,ORI_R2L_TYPE) ; n2 = DAXES_NUM(daxes,ORI_A2P_TYPE) ; n3 = DAXES_NUM(daxes,ORI_I2S_TYPE) ; outbuf = THD_zzprintf(outbuf, "R-to-L extent: %9.3f %s -to- %9.3f %s -step- %9.3f mm [%3d voxels]\n" "A-to-P extent: %9.3f %s -to- %9.3f %s -step- %9.3f mm [%3d voxels]\n" "I-to-S extent: %9.3f %s -to- %9.3f %s -step- %9.3f mm [%3d voxels]\n" , fv1.xyz[0],xlbot , fv2.xyz[0],xltop , fabs(fv3.xyz[0]) , n1 , fv1.xyz[1],ylbot , fv2.xyz[1],yltop , fabs(fv3.xyz[1]) , n2 , fv1.xyz[2],zlbot , fv2.xyz[2],zltop , fabs(fv3.xyz[2]) , n3 ) ; /*-- 01 Feb 2001: print the center of the dataset as well --*/ if( verbose > 0 ){ fv1.xyz[0] = 0.5*(fv1.xyz[0]+fv2.xyz[0]) ; XLAB(xlbot,fv1.xyz[0]) ; fv1.xyz[1] = 0.5*(fv1.xyz[1]+fv2.xyz[1]) ; YLAB(ylbot,fv1.xyz[1]) ; fv1.xyz[2] = 0.5*(fv1.xyz[2]+fv2.xyz[2]) ; ZLAB(zlbot,fv1.xyz[2]) ; outbuf = THD_zzprintf(outbuf, "R-to-L center: %9.3f %s\n" "A-to-P center: %9.3f %s\n" "I-to-S center: %9.3f %s\n" , fv1.xyz[0],xlbot , fv1.xyz[1],ylbot , fv1.xyz[2],zlbot ) ; } ntimes = DSET_NUM_TIMES(dset) ; nval_per = DSET_NVALS_PER_TIME(dset) ; if( ntimes > 1 ){ outbuf = THD_zzprintf(outbuf, "Number of time steps = %d" , ntimes ) ; STATUS("timestep") ; outbuf = THD_zzprintf(outbuf, " Time step = %.5f%s Origin = %.5f%s" , dset->taxis->ttdel , UNITS_TYPE_LABEL(dset->taxis->units_type) , dset->taxis->ttorg , UNITS_TYPE_LABEL(dset->taxis->units_type) ) ; if( dset->taxis->nsl > 0 ) outbuf = THD_zzprintf(outbuf," Number time-offset slices = %d Thickness = %.3f", dset->taxis->nsl , fabs(dset->taxis->dz_sl) ) ; outbuf = THD_zzprintf(outbuf,"\n") ; STATUS("nsl done") ; if( verbose > 0 && dset->taxis->nsl > 0 && dset->taxis->toff_sl != NULL ){ outbuf = THD_zzprintf(outbuf,"Time-offsets per slice:") ; for( ival=0 ; ival < dset->taxis->nsl ; ival++ ) outbuf = THD_zzprintf(outbuf, " %.3f" , dset->taxis->toff_sl[ival] ) ; outbuf = THD_zzprintf(outbuf,"\n") ; } } else { outbuf = THD_zzprintf(outbuf, "Number of values stored at each pixel = %d\n" , nval_per ) ; } #if 0 if( verbose > 0 && ntimes > 1 ) nval_per = dset->dblk->nvals ; else nval_per = 1 ; /* 12 Feb 2002 */ #else nval_per = dset->dblk->nvals ; if( verbose < 0 && nval_per > 5 ) nval_per = 3 ; #endif /* print out stuff for each sub-brick */ for( ival=0 ; ival < nval_per ; ival++ ){ STATUS("ival a") ; sprintf( str , " -- At sub-brick #%d '%s' datum type is %s" , ival , DSET_BRICK_LAB(dset,ival) , MRI_TYPE_name[DSET_BRICK_TYPE(dset,ival)] ) ; nstr = strlen(str) ; tf = DSET_BRICK_FACTOR(dset,ival) ; if( ISVALID_STATISTIC(dset->stats) ){ if( tf != 0.0 ){ sprintf( str+nstr , ":%13.6g to %13.6g [internal]\n" "%*s[*%13.6g] %13.6g to %13.6g [scaled]\n" , dset->stats->bstat[ival].min/tf , dset->stats->bstat[ival].max/tf , nstr-16," " , tf , dset->stats->bstat[ival].min , dset->stats->bstat[ival].max ) ; } else { sprintf( str+nstr , ":%13.6g to %13.6g\n" , dset->stats->bstat[ival].min , dset->stats->bstat[ival].max ) ; } } else if( tf != 0.0 ){ sprintf( str+nstr , " [*%g]\n",tf) ; } else { sprintf( str+nstr , "\n") ; } STATUS("ival b") ; outbuf = THD_zzprintf(outbuf,"%s",str) ; /** 30 Nov 1997: print sub-brick stat params **/ kv = DSET_BRICK_STATCODE(dset,ival) ; if( FUNC_IS_STAT(kv) ){ STATUS("ival c") ; outbuf = THD_zzprintf(outbuf," statcode = %s",FUNC_prefixstr[kv] ) ; npar = FUNC_need_stat_aux[kv] ; if( npar > 0 ){ outbuf = THD_zzprintf(outbuf,"; statpar =") ; for( kv=0 ; kv < npar ; kv++ ) outbuf = THD_zzprintf(outbuf," %g",DSET_BRICK_STATPAR(dset,ival,kv)) ; } outbuf = THD_zzprintf(outbuf,"\n") ; STATUS("ival d") ; } cpt = DSET_BRICK_KEYWORDS(dset,ival) ; if( cpt != NULL && cpt[0] != '\0' ){ outbuf = THD_zzprintf(outbuf," keywords = %.66s\n",cpt) ; } STATUS("ival z") ; } if( verbose < 0 && nval_per < dset->dblk->nvals ) /* 21 Sep 2007 */ outbuf = THD_zzprintf(outbuf, "** For info on all %d sub-bricks, use '3dinfo -verb' **\n", dset->dblk->nvals) ; /** print out dataset global statistical parameters **/ if( ISFUNC(dset) && FUNC_need_stat_aux[dset->func_type] > 0 ){ outbuf = THD_zzprintf(outbuf,"Auxiliary functional statistical parameters:\n %s\n", FUNC_label_stat_aux[dset->func_type] ) ; for( ival=0 ; ival < FUNC_need_stat_aux[dset->func_type] ; ival++ ) outbuf = THD_zzprintf(outbuf," %g",dset->stat_aux[ival]) ; outbuf = THD_zzprintf(outbuf,"\n") ; } /** If present, print out History **/ { char *chn ; int j,k ; chn = tross_Get_History(dset) ; if( chn != NULL ){ j = strlen(chn) ; outbuf = THD_zzprintf(outbuf,"\n----- HISTORY -----\n") ; for( k=0 ; k < j ; k += ZMAX ) outbuf = THD_zzprintf(outbuf,SZMAX,chn+k) ; free(chn) ; outbuf = THD_zzprintf(outbuf,"\n") ; } } /** If present, print out Notes **/ if( verbose >= 0 ){ ATR_int *notecount; int num_notes, i, j, mmm ; char *chn , *chd ; notecount = THD_find_int_atr(dset->dblk, "NOTES_COUNT"); if( notecount != NULL ){ num_notes = notecount->in[0] ; if( verbose == 0 && num_notes > 5 ) num_notes = 5 ; mmm = (verbose > 0) ? ZMAX : 1200 ; /* 400 it was! Come on Bob, have a heart! -ZSS */ for (i=1; i<= num_notes; i++) { chn = tross_Get_Note( dset , i ) ; if( chn != NULL ){ j = strlen(chn) ; if( j > mmm ) chn[mmm] = '\0' ; chd = tross_Get_Notedate(dset,i) ; if( chd == NULL ){ chd = AFMALL(char,16) ; strcpy(chd,"no date") ; } outbuf = THD_zzprintf(outbuf,"\n----- NOTE %d [%s] -----\n%s\n",i,chd,chn) ; free(chn) ; free(chd) ; }
static int * PLUTO_4D_to_nothing (THD_3dim_dataset * old_dset , int ignore , int detrend , generic_func * user_func, void * user_data ) { 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) */ complex ** cptr = NULL ; float * fxar = NULL ; /* array loaded from input dataset */ float * fac = NULL ; /* array of brick scaling factors */ float * dtr = NULL ; /* will be array of detrending coeff */ float val , d0fac , d1fac , x0,x1; double tzero=0.0 , tdelta , ts_mean , ts_slope ; int ii , old_datum , nuse , use_fac , iz,izold, nxy,nvox ; static int retval; register int kk ; /*----------------------------------------------------------*/ /*----- Check inputs to see if they are reasonable-ish -----*/ if( ! ISVALID_3DIM_DATASET(old_dset) ) return NULL ; if( user_func == NULL ) return NULL ; if( ignore < 0 ) ignore = 0 ; /*--------- set up pointers to each sub-brick in the input dataset ---------*/ old_datum = DSET_BRICK_TYPE( old_dset , 0 ) ; /* get old dataset datum */ nuse = DSET_NUM_TIMES(old_dset) - ignore ; /* # of points on time axis */ if( nuse < 2 ) return NULL ; DSET_load( old_dset ) ; /* must be in memory before we get pointers to it */ kk = THD_count_databricks( old_dset->dblk ) ; /* check if it was */ if( kk < DSET_NVALS(old_dset) ){ /* loaded correctly */ DSET_unload( old_dset ) ; return NULL ; } switch( old_datum ){ /* pointer type depends on input datum type */ default: /** don't know what to do **/ DSET_unload( old_dset ) ; 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..nuse-1. */ case MRI_byte: bptr = (byte **) malloc( sizeof(byte *) * nuse ) ; if( bptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) bptr[kk] = (byte *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is shorts ---------*/ /* voxel #i at time #k is sptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_short: sptr = (short **) malloc( sizeof(short *) * nuse ) ; if( sptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) sptr[kk] = (short *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is floats ---------*/ /* voxel #i at time #k is fptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_float: fptr = (float **) malloc( sizeof(float *) * nuse ) ; if( fptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) fptr[kk] = (float *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is complex ---------*/ /* voxel #i at time #k is cptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_complex: cptr = (complex **) malloc( sizeof(complex *) * nuse ) ; if( cptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) cptr[kk] = (complex *) DSET_ARRAY(old_dset,kk+ignore) ; break ; } /* end of switch on input type */ nvox = old_dset->daxes->nxx * old_dset->daxes->nyy * old_dset->daxes->nzz ; /*---- allocate space for 1 voxel timeseries ----*/ fxar = (float *) malloc( sizeof(float) * nuse ) ; /* voxel timeseries */ if( fxar == NULL ){ ZFREE_WORKSPACE ; return NULL ; } /*--- get scaling factors for sub-bricks ---*/ fac = (float *) malloc( sizeof(float) * nuse ) ; /* factors */ if( fac == NULL ){ ZFREE_WORKSPACE ; return NULL ; } use_fac = 0 ; for( kk=0 ; kk < nuse ; kk++ ){ fac[kk] = DSET_BRICK_FACTOR(old_dset,kk+ignore) ; if( fac[kk] != 0.0 ) use_fac++ ; else fac[kk] = 1.0 ; } if( !use_fac ) ZFREEUP(fac) ; /*--- setup for detrending ---*/ dtr = (float *) malloc( sizeof(float) * nuse ) ; if( dtr == NULL ){ ZFREE_WORKSPACE ; return NULL ; } d0fac = 1.0 / nuse ; d1fac = 12.0 / nuse / (nuse*nuse - 1.0) ; for( kk=0 ; kk < nuse ; kk++ ) dtr[kk] = kk - 0.5 * (nuse-1) ; /* linear trend, orthogonal to 1 */ /*----- set up to find time at each voxel -----*/ tdelta = old_dset->taxis->ttdel ; if( DSET_TIMEUNITS(old_dset) == UNITS_MSEC_TYPE ) tdelta *= 0.001 ; if( tdelta == 0.0 ) tdelta = 1.0 ; izold = -666 ; nxy = old_dset->daxes->nxx * old_dset->daxes->nyy ; /*----------------------------------------------------*/ /*----- Setup has ended. Now do some real work. -----*/ /* start notification */ #if 0 user_func( 0.0 , 0.0 , nvox , NULL,0.0,0.0 , user_data ) ; #else { void (*uf)(double,double,int,float *,double,double,void *) = (void (*)(double,double,int,float *,double,double,void *))(user_func) ; uf( 0.0l,0.0l , nvox , NULL , 0.0l,0.0l , user_data ) ; } #endif /***** loop over voxels *****/ for( ii=0 ; ii < nvox ; ii++ ){ /* 1 time series at a time */ /*** load data from input dataset, depending on type ***/ switch( old_datum ){ /*** input = bytes ***/ case MRI_byte: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = bptr[kk][ii] ; break ; /*** input = shorts ***/ case MRI_short: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = sptr[kk][ii] ; break ; /*** input = floats ***/ case MRI_float: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = fptr[kk][ii] ; break ; /*** input = complex (note we use absolute value) ***/ case MRI_complex: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = CABS(cptr[kk][ii]) ; break ; } /* end of switch over input type */ /*** scale? ***/ if( use_fac ) for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] *= fac[kk] ; /** compute mean and slope **/ x0 = x1 = 0.0 ; for( kk=0 ; kk < nuse ; kk++ ){ x0 += fxar[kk] ; x1 += fxar[kk] * dtr[kk] ; } x0 *= d0fac ; x1 *= d1fac ; /* factors to remove mean and trend */ ts_mean = x0 ; ts_slope = x1 / tdelta ; /** detrend? **/ if( detrend ) for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] -= (x0 + x1 * dtr[kk]) ; /** compute start time of this timeseries **/ /* The info computed here is not being used in this version*/ iz = ii / nxy ; /* which slice am I in? */ if( iz != izold ){ /* in a new slice? */ tzero = THD_timeof( ignore , old_dset->daxes->zzorg + iz*old_dset->daxes->zzdel , old_dset->taxis ) ; izold = iz ; if( DSET_TIMEUNITS(old_dset) == UNITS_MSEC_TYPE ) tzero *= 0.001 ; } /*** Send data to user function ***/ #if 0 user_func( tzero,tdelta , nuse,fxar,ts_mean,ts_slope , user_data) ; #else { void (*uf)(double,double,int,float *,double,double,void *) = (void (*)(double,double,int,float *,double,double,void *))(user_func) ; uf( tzero,tdelta , nuse,fxar,ts_mean,ts_slope , user_data) ; } #endif } /* end of outer loop over 1 voxels at a time */ DSET_unload( old_dset ) ; /* end notification */ #if 0 user_func( 0.0 , 0.0 , 0 , NULL,0.0,0.0 , user_data ) ; #else { void (*uf)(double,double,int,float *,double,double,void *) = (void (*)(double,double,int,float *,double,double,void *))(user_func) ; uf( 0.0l,0.0l, 0 , NULL,0.0l,0.0l, user_data ) ; } #endif /*-------------- Cleanup and go home ----------------*/ ZFREE_WORKSPACE ; retval = 0; return &retval; /* this value is not used for now .... */ }
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; }
int main( int argc , char *argv[] ) { int nx,ny,nz , nxyz , ii,kk , num1,num2 , num_tt=0 , iv , piece , fim_offset; float dx,dy,dz , dxyz , num1_inv=0.0 , num2_inv , num1m1_inv=0.0 , num2m1_inv , dof , dd,tt,q1,q2 , f1,f2 , tt_max=0.0 ; THD_3dim_dataset *dset=NULL , *new_dset=NULL ; THD_3dim_dataset * base_dset; float *av1 , *av2 , *sd1 , *sd2 , *ffim , *gfim ; float *base_ary=NULL; void *vsp ; void *vdif ; /* output mean difference */ char cbuf[THD_MAX_NAME] ; float fbuf[MAX_STAT_AUX] , fimfac ; int output_datum ; float npiece , memuse ; float *dofbrik=NULL , *dofar=NULL ; THD_3dim_dataset *dof_dset=NULL ; /*-- read command line arguments --*/ if( argc < 2 || strncmp(argv[1],"-help",5) == 0 ) TT_syntax(NULL) ; /*-- 20 Apr 2001: addto the arglist, if user wants to [RWCox] --*/ mainENTRY("3dttest main"); machdep() ; PRINT_VERSION("3dttest") ; INFO_message("For most purposes, 3dttest++ should be used instead of 3dttest!") ; { int new_argc ; char ** new_argv ; addto_args( argc , argv , &new_argc , &new_argv ) ; if( new_argv != NULL ){ argc = new_argc ; argv = new_argv ; } } AFNI_logger("3dttest",argc,argv) ; TT_read_opts( argc , argv ) ; if( ! TT_be_quiet ) printf("3dttest: t-tests of 3D datasets, by RW Cox\n") ; /*-- read first dataset in set2 to get dimensions, etc. --*/ dset = THD_open_dataset( TT_set2->ar[0] ) ; /* 20 Dec 1999 BDW */ if( ! ISVALID_3DIM_DATASET(dset) ) ERROR_exit("Unable to open dataset file %s",TT_set2->ar[0]); nx = dset->daxes->nxx ; ny = dset->daxes->nyy ; nz = dset->daxes->nzz ; nxyz = nx * ny * nz ; dx = fabs(dset->daxes->xxdel) ; dy = fabs(dset->daxes->yydel) ; dz = fabs(dset->daxes->zzdel) ; dxyz = dx * dy * dz ; #ifdef TTDEBUG printf("*** nx=%d ny=%d nz=%d\n",nx,ny,nz) ; #endif /*-- make an empty copy of this dataset, for eventual output --*/ #ifdef TTDEBUG printf("*** making empty dataset\n") ; #endif new_dset = EDIT_empty_copy( dset ) ; tross_Make_History( "3dttest" , argc,argv , new_dset ) ; strcpy( cbuf , dset->self_name ) ; strcat( cbuf , "+TT" ) ; iv = DSET_PRINCIPAL_VALUE(dset) ; if( TT_datum >= 0 ){ output_datum = TT_datum ; } else { output_datum = DSET_BRICK_TYPE(dset,iv) ; if( output_datum == MRI_byte ) output_datum = MRI_short ; } #ifdef TTDEBUG printf(" ** datum = %s\n",MRI_TYPE_name[output_datum]) ; #endif iv = EDIT_dset_items( new_dset , ADN_prefix , TT_prefix , ADN_label1 , TT_prefix , ADN_directory_name , TT_session , ADN_self_name , cbuf , ADN_type , ISHEAD(dset) ? HEAD_FUNC_TYPE : GEN_FUNC_TYPE , ADN_func_type , FUNC_TT_TYPE , ADN_nvals , FUNC_nvals[FUNC_TT_TYPE] , ADN_ntt , 0 , /* 07 Jun 2007 */ ADN_datum_all , output_datum , ADN_none ) ; if( iv > 0 ) ERROR_exit("%d errors in attempting to create output dataset!",iv ) ; if( THD_deathcon() && THD_is_file(new_dset->dblk->diskptr->header_name) ) ERROR_exit( "Output dataset file %s already exists--cannot continue!\a", new_dset->dblk->diskptr->header_name ) ; #ifdef TTDEBUG printf("*** deleting exemplar dataset\n") ; #endif THD_delete_3dim_dataset( dset , False ) ; dset = NULL ; /** macro to test a malloc-ed pointer for validity **/ #define MTEST(ptr) \ if((ptr)==NULL) \ ( fprintf(stderr,"*** Cannot allocate memory for statistics!\n"), exit(0) ) /*-- make space for the t-test computations --*/ /* (allocate entire volumes) 13 Dec 2005 [rickr] */ npiece = 3.0 ; /* need at least this many */ if( TT_paired ) npiece += 1.0 ; else if( TT_set1 != NULL ) npiece += 2.0 ; npiece += mri_datum_size(output_datum) / (float) sizeof(float) ; npiece += mri_datum_size(output_datum) / (float) sizeof(float) ; #if 0 piece_size = TT_workmem * MEGA / ( npiece * sizeof(float) ) ; if( piece_size > nxyz ) piece_size = nxyz ; #ifdef TTDEBUG printf("*** malloc-ing space for statistics: %g float arrays of length %d\n", npiece,piece_size) ; #endif #endif av2 = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(av2) ; sd2 = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(sd2) ; ffim = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(ffim) ; num2 = TT_set2->num ; if( TT_paired ){ av1 = sd1 = NULL ; gfim = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(gfim) ; num1 = num2 ; } else if( TT_set1 != NULL ){ av1 = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(av1) ; sd1 = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(sd1) ; gfim = NULL ; num1 = TT_set1->num ; } else { av1 = sd1 = NULL ; gfim = NULL ; num1 = 0 ; } vdif = (void *) malloc( mri_datum_size(output_datum) * nxyz ) ; MTEST(vdif) ; vsp = (void *) malloc( mri_datum_size(output_datum) * nxyz ) ; MTEST(vsp) ; /* 27 Dec 2002: make DOF dataset (if prefix is given, and unpooled is on) */ if( TT_pooled == 0 && TT_dof_prefix[0] != '\0' ){ dofbrik = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(dofbrik) ; dof_dset = EDIT_empty_copy( new_dset ) ; tross_Make_History( "3dttest" , argc,argv , dof_dset ) ; EDIT_dset_items( dof_dset , ADN_prefix , TT_dof_prefix , ADN_directory_name , TT_session , ADN_type , ISHEAD(dset) ? HEAD_FUNC_TYPE : GEN_FUNC_TYPE, ADN_func_type , FUNC_BUCK_TYPE , ADN_nvals , 1 , ADN_datum_all , MRI_float , ADN_none ) ; if( THD_is_file(dof_dset->dblk->diskptr->header_name) ) ERROR_exit( "-dof_prefix dataset file %s already exists--cannot continue!\a", dof_dset->dblk->diskptr->header_name ) ; EDIT_substitute_brick( dof_dset , 0 , MRI_float , dofbrik ) ; } /* print out memory usage to edify the user */ if( ! TT_be_quiet ){ memuse = sizeof(float) * nxyz * npiece + ( mri_datum_size(output_datum) + sizeof(short) ) * nxyz ; if( dofbrik != NULL ) memuse += sizeof(float) * nxyz ; /* 27 Dec 2002 */ printf("--- allocated %d Megabytes memory for internal use (%d volumes)\n", (int)(memuse/MEGA), (int)npiece) ; } mri_fix_data_pointer( vdif , DSET_BRICK(new_dset,0) ) ; /* attach bricks */ mri_fix_data_pointer( vsp , DSET_BRICK(new_dset,1) ) ; /* to new dataset */ /** only short and float are allowed for output **/ if( output_datum != MRI_short && output_datum != MRI_float ) ERROR_exit("Illegal output data type %d = %s", output_datum , MRI_TYPE_name[output_datum] ) ; num2_inv = 1.0 / num2 ; num2m1_inv = 1.0 / (num2-1) ; if( num1 > 0 ){ num1_inv = 1.0 / num1 ; num1m1_inv = 1.0 / (num1-1) ; } /*----- loop over pieces to process the input datasets with -----*/ /** macro to open a dataset and make it ready for processing **/ #define DOPEN(ds,name) \ do{ int pv ; (ds) = THD_open_dataset((name)) ; /* 16 Sep 1999 */ \ if( !ISVALID_3DIM_DATASET((ds)) ) \ ERROR_exit("Can't open dataset: %s",(name)) ; \ if( (ds)->daxes->nxx!=nx || (ds)->daxes->nyy!=ny || (ds)->daxes->nzz!=nz ) \ ERROR_exit("Axes size mismatch: %s",(name)) ; \ if( !EQUIV_GRIDS((ds),new_dset) ) \ WARNING_message("Grid mismatch: %s",(name)) ; \ if( DSET_NUM_TIMES((ds)) > 1 ) \ ERROR_exit("Can't use time-dependent data: %s",(name)) ; \ if( TT_use_editor ) EDIT_one_dataset( (ds), &TT_edopt ) ; \ else DSET_load((ds)) ; \ pv = DSET_PRINCIPAL_VALUE((ds)) ; \ if( DSET_ARRAY((ds),pv) == NULL ) \ ERROR_exit("Can't access data: %s",(name)) ; \ if( DSET_BRICK_TYPE((ds),pv) == MRI_complex ) \ ERROR_exit("Can't use complex data: %s",(name)) ; \ break ; } while (0) #if 0 /* can do it directly now (without offsets) 13 Dec 2005 [rickr] */ /** macro to return pointer to correct location in brick for current processing **/ #define SUB_POINTER(ds,vv,ind,ptr) \ do{ switch( DSET_BRICK_TYPE((ds),(vv)) ){ \ default: ERROR_exit("Illegal datum! ***"); \ case MRI_short:{ short * fim = (short *) DSET_ARRAY((ds),(vv)) ; \ (ptr) = (void *)( fim + (ind) ) ; \ } break ; \ case MRI_byte:{ byte * fim = (byte *) DSET_ARRAY((ds),(vv)) ; \ (ptr) = (void *)( fim + (ind) ) ; \ } break ; \ case MRI_float:{ float * fim = (float *) DSET_ARRAY((ds),(vv)) ; \ (ptr) = (void *)( fim + (ind) ) ; \ } break ; } break ; } while(0) #endif /** number of pieces to process **/ /* num_piece = (nxyz + piece_size - 1) / nxyz ; */ #if 0 nice(2) ; /** lower priority a little **/ #endif /* possibly open TT_base_dset now, and convert to floats */ if( TT_base_dname ) { DOPEN(base_dset, TT_base_dname) ; base_ary = (float *) malloc( sizeof(float) * nxyz ) ; MTEST(base_ary) ; EDIT_coerce_scale_type(nxyz , DSET_BRICK_FACTOR(base_dset,0) , DSET_BRICK_TYPE(base_dset,0),DSET_ARRAY(base_dset,0), /* input */ MRI_float ,base_ary ) ; /* output */ THD_delete_3dim_dataset( base_dset , False ) ; base_dset = NULL ; } /* only 1 'piece' now 13 Dec 2005 [rickr] */ for( piece=0 ; piece < 1 ; piece++ ){ fim_offset = 0 ; #ifdef TTDEBUG printf("*** start of piece %d: length=%d offset=%d\n",piece,nxyz,fim_offset) ; #else if( ! TT_be_quiet ){ printf("--- starting piece %d/%d (%d voxels) ",piece+1,1,nxyz) ; fflush(stdout) ; } #endif /** process set2 (and set1, if paired) **/ for( ii=0 ; ii < nxyz ; ii++ ) av2[ii] = 0.0 ; for( ii=0 ; ii < nxyz ; ii++ ) sd2[ii] = 0.0 ; for( kk=0 ; kk < num2 ; kk++ ){ /** read in the data **/ DOPEN(dset,TT_set2->ar[kk]) ; iv = DSET_PRINCIPAL_VALUE(dset) ; #ifndef TTDEBUG if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; } /* progress */ #else printf(" ** opened dataset file %s\n",TT_set2->ar[kk]); #endif #if 0 /* fimfac will be compute when the results are ready */ if( piece == 0 && kk == 0 ){ fimfac = DSET_BRICK_FACTOR(dset,iv) ; if( fimfac == 0.0 ) fimfac = 1.0 ; fimfacinv = 1.0 / fimfac ; #ifdef TTDEBUG printf(" ** set fimfac = %g\n",fimfac) ; #endif } #endif /** convert it to floats (in ffim) **/ EDIT_coerce_scale_type(nxyz , DSET_BRICK_FACTOR(dset,iv) , DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */ MRI_float ,ffim ) ; /* output */ THD_delete_3dim_dataset( dset , False ) ; dset = NULL ; /** get the paired dataset, if present **/ if( TT_paired ){ DOPEN(dset,TT_set1->ar[kk]) ; iv = DSET_PRINCIPAL_VALUE(dset) ; #ifndef TTDEBUG if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; } /* progress */ #else printf(" ** opened dataset file %s\n",TT_set1->ar[kk]); #endif EDIT_coerce_scale_type( nxyz , DSET_BRICK_FACTOR(dset,iv) , DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */ MRI_float ,gfim ) ; /* output */ THD_delete_3dim_dataset( dset , False ) ; dset = NULL ; if( TT_voxel >= 0 ) fprintf(stderr,"-- paired values #%02d: %f, %f\n", kk,ffim[TT_voxel],gfim[TT_voxel]) ; for( ii=0 ; ii < nxyz ; ii++ ) ffim[ii] -= gfim[ii] ; } else if( TT_voxel >= 0 ) fprintf(stderr,"-- set2 value #%02d: %f\n",kk,ffim[TT_voxel]); #ifdef TTDEBUG printf(" * adding into av2 and sd2\n") ; #endif /* accumulate into av2 and sd2 */ for( ii=0 ; ii < nxyz ; ii++ ){ dd = ffim[ii] ; av2[ii] += dd ; sd2[ii] += dd * dd ; } } /* end of loop over set2 datasets */ /** form the mean and stdev of set2 **/ #ifdef TTDEBUG printf(" ** forming mean and sigma of set2\n") ; #endif for( ii=0 ; ii < nxyz ; ii++ ){ av2[ii] *= num2_inv ; dd = (sd2[ii] - num2*av2[ii]*av2[ii]) ; sd2[ii] = (dd > 0.0) ? sqrt( num2m1_inv * dd ) : 0.0 ; } if( TT_voxel >= 0 ) fprintf(stderr,"-- s2 mean = %g, sd = %g\n", av2[TT_voxel],sd2[TT_voxel]) ; /** if set1 exists but is not paired with set2, process it now **/ if( ! TT_paired && TT_set1 != NULL ){ for( ii=0 ; ii < nxyz ; ii++ ) av1[ii] = 0.0 ; for( ii=0 ; ii < nxyz ; ii++ ) sd1[ii] = 0.0 ; for( kk=0 ; kk < num1 ; kk++ ){ DOPEN(dset,TT_set1->ar[kk]) ; iv = DSET_PRINCIPAL_VALUE(dset) ; #ifndef TTDEBUG if( ! TT_be_quiet ){ printf(".") ; fflush(stdout) ; } /* progress */ #else printf(" ** opened dataset file %s\n",TT_set1->ar[kk]); #endif EDIT_coerce_scale_type( nxyz , DSET_BRICK_FACTOR(dset,iv) , DSET_BRICK_TYPE(dset,iv),DSET_ARRAY(dset,iv), /* input */ MRI_float ,ffim ) ; /* output */ THD_delete_3dim_dataset( dset , False ) ; dset = NULL ; #ifdef TTDEBUG printf(" * adding into av1 and sd1\n") ; #endif for( ii=0 ; ii < nxyz ; ii++ ){ dd = ffim[ii] ; av1[ii] += dd ; sd1[ii] += dd * dd ; } if( TT_voxel >= 0 ) fprintf(stderr,"-- set1 value #%02d: %g\n",kk,ffim[TT_voxel]) ; } /* end of loop over set1 datasets */ /** form the mean and stdev of set1 **/ #ifdef TTDEBUG printf(" ** forming mean and sigma of set1\n") ; #endif for( ii=0 ; ii < nxyz ; ii++ ){ av1[ii] *= num1_inv ; dd = (sd1[ii] - num1*av1[ii]*av1[ii]) ; sd1[ii] = (dd > 0.0) ? sqrt( num1m1_inv * dd ) : 0.0 ; } if( TT_voxel >= 0 ) fprintf(stderr,"-- s1 mean = %g, sd = %g\n", av1[TT_voxel], sd1[TT_voxel]) ; } /* end of processing set1 by itself */ /***** now form difference and t-statistic *****/ #ifndef TTDEBUG if( ! TT_be_quiet ){ printf("+") ; fflush(stdout) ; } /* progress */ #else printf(" ** computing t-tests next\n") ; #endif #if 0 /* will do at end using EDIT_convert_dtype 13 Dec 2005 [rickr] */ /** macro to assign difference value to correct type of array **/ #define DIFASS switch( output_datum ){ \ case MRI_short: sdar[ii] = (short) (fimfacinv*dd) ; break ; \ case MRI_float: fdar[ii] = (float) dd ; break ; } #define TOP_SS 32700 #define TOP_TT (32700.0/FUNC_TT_SCALE_SHORT) #endif if( TT_paired || TT_use_bval == 1 ){ /** case 1: paired estimate or 1-sample **/ if( TT_paired || TT_n1 == 0 ){ /* the olde waye: 1 sample test */ f2 = 1.0 / sqrt( (double) num2 ) ; for( ii=0 ; ii < nxyz ; ii++ ){ av2[ii] -= (base_ary ? base_ary[ii] : TT_bval) ; /* final mean */ if( sd2[ii] > 0.0 ){ num_tt++ ; tt = av2[ii] / (f2 * sd2[ii]) ; sd2[ii] = tt; /* final t-stat */ tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ; } else { sd2[ii] = 0.0; } } if( TT_voxel >= 0 ) fprintf(stderr,"-- paired/bval mean = %g, t = %g\n", av2[TT_voxel], sd2[TT_voxel]) ; } else { /* 10 Oct 2007: -sdn1 was used with -base1: 'two' sample test */ f1 = (TT_n1-1.0) * (1.0/TT_n1 + 1.0/num2) / (TT_n1+num2-2.0) ; f2 = (num2 -1.0) * (1.0/TT_n1 + 1.0/num2) / (TT_n1+num2-2.0) ; for( ii=0 ; ii < nxyz ; ii++ ){ av2[ii] -= (base_ary ? base_ary[ii] : TT_bval) ; /* final mean */ q1 = f1 * TT_sd1*TT_sd1 + f2 * sd2[ii]*sd2[ii] ; if( q1 > 0.0 ){ num_tt++ ; tt = av2[ii] / sqrt(q1) ; sd2[ii] = tt ; /* final t-stat */ tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ; } else { sd2[ii] = 0.0 ; } } } /* end of -sdn1 special case */ #ifdef TTDEBUG printf(" ** paired or bval test: num_tt = %d\n",num_tt) ; #endif } else if( TT_pooled ){ /** case 2: unpaired 2-sample, pooled variance **/ f1 = (num1-1.0) * (1.0/num1 + 1.0/num2) / (num1+num2-2.0) ; f2 = (num2-1.0) * (1.0/num1 + 1.0/num2) / (num1+num2-2.0) ; for( ii=0 ; ii < nxyz ; ii++ ){ av2[ii] -= av1[ii] ; /* final mean */ q1 = f1 * sd1[ii]*sd1[ii] + f2 * sd2[ii]*sd2[ii] ; if( q1 > 0.0 ){ num_tt++ ; tt = av2[ii] / sqrt(q1) ; sd2[ii] = tt ; /* final t-stat */ tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ; } else { sd2[ii] = 0.0 ; } } if( TT_voxel >= 0 ) fprintf(stderr,"-- unpaired, pooled mean = %g, t = %g\n", av2[TT_voxel], sd2[TT_voxel]) ; #ifdef TTDEBUG printf(" ** pooled test: num_tt = %d\n",num_tt) ; #endif } else { /** case 3: unpaired 2-sample, unpooled variance **/ /** 27 Dec 2002: modified to save DOF into dofar **/ if( dofbrik != NULL ) dofar = dofbrik + fim_offset ; /* 27 Dec 2002 */ for( ii=0 ; ii < nxyz ; ii++ ){ av2[ii] -= av1[ii] ; q1 = num1_inv * sd1[ii]*sd1[ii] ; q2 = num2_inv * sd2[ii]*sd2[ii] ; if( q1>0.0 && q2>0.0 ){ /* have positive variances? */ num_tt++ ; tt = av2[ii] / sqrt(q1+q2) ; sd2[ii] = tt ; /* final t-stat */ tt = fabs(tt) ; if( tt > tt_max ) tt_max = tt ; if( dofar != NULL ) /* 27 Dec 2002 */ dofar[ii] = (q1+q2)*(q1+q2) / (num1m1_inv*q1*q1 + num2m1_inv*q2*q2) ; } else { sd2[ii] = 0.0 ; if( dofar != NULL ) dofar[ii] = 1.0 ; /* 27 Dec 2002 */ } } if( TT_voxel >= 0 ) fprintf(stderr,"-- unpaired, unpooled mean = %g, t = %g\n", av2[TT_voxel], sd2[TT_voxel]) ; #ifdef TTDEBUG printf(" ** unpooled test: num_tt = %d\n",num_tt) ; #endif } #ifndef TTDEBUG if( ! TT_be_quiet ){ printf("\n") ; fflush(stdout) ; } #endif } /* end of loop over pieces of the input */ if( TT_paired ){ printf("--- Number of degrees of freedom = %d (paired test)\n",num2-1) ; dof = num2 - 1 ; } else if( TT_use_bval == 1 ){ if( TT_n1 == 0 ){ printf("--- Number of degrees of freedom = %d (1-sample test)\n",num2-1) ; dof = num2 - 1 ; } else { dof = TT_n1+num2-2 ; printf("--- Number of degrees of freedom = %d (-sdn1 2-sample test)\n",(int)dof) ; } } else { printf("--- Number of degrees of freedom = %d (2-sample test)\n",num1+num2-2) ; dof = num1+num2-2 ; if( ! TT_pooled ) printf(" (For unpooled variance estimate, this is only approximate!)\n") ; } printf("--- Number of t-tests performed = %d out of %d voxels\n",num_tt,nxyz) ; printf("--- Largest |t| value found = %g\n",tt_max) ; kk = sizeof(ptable) / sizeof(float) ; for( ii=0 ; ii < kk ; ii++ ){ tt = student_p2t( ptable[ii] , dof ) ; printf("--- Double sided tail p = %8f at t = %8f\n" , ptable[ii] , tt ) ; } /**----------------------------------------------------------------------**/ /** now convert data to output format 13 Dec 2005 [rickr] **/ /* first set mean */ fimfac = EDIT_convert_dtype(nxyz , MRI_float,av2 , output_datum,vdif , 0.0) ; DSET_BRICK_FACTOR(new_dset, 0) = (fimfac != 0.0) ? 1.0/fimfac : 0.0 ; dd = fimfac; /* save for debug output */ /* if output is of type short, limit t-stat magnitude to 32.7 */ if( output_datum == MRI_short ){ for( ii=0 ; ii < nxyz ; ii++ ){ if ( sd2[ii] > 32.7 ) sd2[ii] = 32.7 ; else if( sd2[ii] < -32.7 ) sd2[ii] = -32.7 ; } } fimfac = EDIT_convert_dtype(nxyz , MRI_float,sd2 , output_datum,vsp , 0.0) ; DSET_BRICK_FACTOR(new_dset, 1) = (fimfac != 0.0) ? 1.0/fimfac : 0.0 ; #ifdef TTDEBUG printf(" ** fimfac for mean, t-stat = %g, %g\n",dd, fimfac) ; #endif /**----------------------------------------------------------------------**/ INFO_message("Writing combined dataset into %s\n", DSET_BRIKNAME(new_dset) ) ; fbuf[0] = dof ; for( ii=1 ; ii < MAX_STAT_AUX ; ii++ ) fbuf[ii] = 0.0 ; (void) EDIT_dset_items( new_dset , ADN_stat_aux , fbuf , ADN_none ) ; #if 0 /* factors already set */ fbuf[0] = (output_datum == MRI_short && fimfac != 1.0 ) ? fimfac : 0.0 ; fbuf[1] = (output_datum == MRI_short ) ? 1.0 / FUNC_TT_SCALE_SHORT : 0.0 ; (void) EDIT_dset_items( new_dset , ADN_brick_fac , fbuf , ADN_none ) ; #endif if( !AFNI_noenv("AFNI_AUTOMATIC_FDR") ) ii = THD_create_all_fdrcurves(new_dset) ; else ii = 0 ; THD_load_statistics( new_dset ) ; THD_write_3dim_dataset( NULL,NULL , new_dset , True ) ; if( ii > 0 ) ININFO_message("created %d FDR curves in header",ii) ; if( dof_dset != NULL ){ /* 27 Dec 2002 */ DSET_write( dof_dset ) ; WROTE_DSET( dof_dset ) ; } exit(0) ; }
THD_3dim_dataset * MAKER_4D_to_typed_fim( THD_3dim_dataset * old_dset , char * new_prefix , int new_datum , int ignore , int detrend , generic_func * user_func , void * user_data ) { THD_3dim_dataset * new_dset ; /* output dataset */ 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) */ complex ** cptr = NULL ; float * fxar = NULL ; /* array loaded from input dataset */ float * fac = NULL ; /* array of brick scaling factors */ float * fout = NULL ; /* will be array of output floats */ float * dtr = NULL ; /* will be array of detrending coeff */ float val , d0fac , d1fac , x0,x1; double tzero=0 , tdelta , ts_mean , ts_slope ; int ii , old_datum , nuse , use_fac , iz,izold, nxy,nvox , nbad ; register int kk ; void (*ufunc)(double,double,int,float *,double,double,void *,float *) = (void (*)(double,double,int,float *,double,double,void *,float *)) user_func ; /*----------------------------------------------------------*/ /*----- Check inputs to see if they are reasonable-ish -----*/ if( ! ISVALID_3DIM_DATASET(old_dset) ) return NULL ; if( new_datum >= 0 && new_datum != MRI_byte && new_datum != MRI_short && new_datum != MRI_float ) return NULL ; if( user_func == NULL ) return NULL ; if( ignore < 0 ) ignore = 0 ; /*--------- set up pointers to each sub-brick in the input dataset ---------*/ old_datum = DSET_BRICK_TYPE( old_dset , 0 ) ; /* get old dataset datum */ nuse = DSET_NUM_TIMES(old_dset) - ignore ; /* # of points on time axis */ if( nuse < 2 ) return NULL ; if( new_datum < 0 ) new_datum = old_datum ; /* output datum = input */ if( new_datum == MRI_complex ) return NULL ; /* but complex = bad news */ DSET_load( old_dset ) ; /* must be in memory before we get pointers to it */ kk = THD_count_databricks( old_dset->dblk ) ; /* check if it was */ if( kk < DSET_NVALS(old_dset) ){ /* loaded correctly */ DSET_unload( old_dset ) ; return NULL ; } switch( old_datum ){ /* pointer type depends on input datum type */ default: /** don't know what to do **/ DSET_unload( old_dset ) ; 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..nuse-1. */ case MRI_byte: bptr = (byte **) malloc( sizeof(byte *) * nuse ) ; if( bptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) bptr[kk] = (byte *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is shorts ---------*/ /* voxel #i at time #k is sptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_short: sptr = (short **) malloc( sizeof(short *) * nuse ) ; if( sptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) sptr[kk] = (short *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is floats ---------*/ /* voxel #i at time #k is fptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_float: fptr = (float **) malloc( sizeof(float *) * nuse ) ; if( fptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) fptr[kk] = (float *) DSET_ARRAY(old_dset,kk+ignore) ; break ; /*--------- input is complex ---------*/ /* voxel #i at time #k is cptr[k][i] */ /* for i=0..nvox-1 and k=0..nuse-1. */ case MRI_complex: cptr = (complex **) malloc( sizeof(complex *) * nuse ) ; if( cptr == NULL ) return NULL ; for( kk=0 ; kk < nuse ; kk++ ) cptr[kk] = (complex *) DSET_ARRAY(old_dset,kk+ignore) ; break ; } /* end of switch on input type */ /*---- allocate space for 1 voxel timeseries ----*/ fxar = (float *) malloc( sizeof(float) * nuse ) ; /* voxel timeseries */ if( fxar == NULL ){ FREE_WORKSPACE ; return NULL ; } /*--- get scaling factors for sub-bricks ---*/ fac = (float *) malloc( sizeof(float) * nuse ) ; /* factors */ if( fac == NULL ){ FREE_WORKSPACE ; return NULL ; } use_fac = 0 ; for( kk=0 ; kk < nuse ; kk++ ){ fac[kk] = DSET_BRICK_FACTOR(old_dset,kk+ignore) ; if( fac[kk] != 0.0 ) use_fac++ ; else fac[kk] = 1.0 ; } if( !use_fac ) FREEUP(fac) ; /*--- setup for detrending ---*/ dtr = (float *) malloc( sizeof(float) * nuse ) ; if( dtr == NULL ){ FREE_WORKSPACE ; return NULL ; } d0fac = 1.0 / nuse ; d1fac = 12.0 / nuse / (nuse*nuse - 1.0) ; for( kk=0 ; kk < nuse ; kk++ ) dtr[kk] = kk - 0.5 * (nuse-1) ; /* linear trend, orthogonal to 1 */ /*---------------------- make a new dataset ----------------------*/ new_dset = EDIT_empty_copy( old_dset ) ; /* start with copy of old one */ /*-- edit some of its internal parameters --*/ ii = EDIT_dset_items( new_dset , ADN_prefix , new_prefix , /* filename prefix */ ADN_malloc_type , DATABLOCK_MEM_MALLOC , /* store in memory */ ADN_datum_all , new_datum , /* atomic datum */ ADN_nvals , 1 , /* # sub-bricks */ ADN_ntt , 0 , /* # time points */ ADN_type , ISHEAD(old_dset) /* dataset type */ ? HEAD_FUNC_TYPE : GEN_FUNC_TYPE , ADN_func_type , FUNC_FIM_TYPE , /* function type */ ADN_none ) ; if( ii != 0 ){ ERROR_message("Error creating dataset '%s'",new_prefix) ; THD_delete_3dim_dataset( new_dset , False ) ; /* some error above */ FREE_WORKSPACE ; return NULL ; } /*------ make floating point output brick (only at the end will scale to byte or shorts) ------*/ nvox = old_dset->daxes->nxx * old_dset->daxes->nyy * old_dset->daxes->nzz ; fout = (float *) malloc( sizeof(float) * nvox ) ; /* ptr to brick */ if( fout == NULL ){ THD_delete_3dim_dataset( new_dset , False ) ; FREE_WORKSPACE ; return NULL ; } /*----- set up to find time at each voxel -----*/ tdelta = old_dset->taxis->ttdel ; if( DSET_TIMEUNITS(old_dset) == UNITS_MSEC_TYPE ) tdelta *= 0.001 ; if( tdelta == 0.0 ) tdelta = 1.0 ; izold = -666 ; nxy = old_dset->daxes->nxx * old_dset->daxes->nyy ; /*----------------------------------------------------*/ /*----- Setup has ended. Now do some real work. -----*/ /* start notification */ #if 0 user_func( 0.0 , 0.0 , nvox , NULL,0.0,0.0 , user_data , NULL ) ; #else ufunc( 0.0 , 0.0 , nvox , NULL,0.0,0.0 , user_data , NULL ) ; #endif /***** loop over voxels *****/ for( ii=0 ; ii < nvox ; ii++ ){ /* 1 time series at a time */ /*** load data from input dataset, depending on type ***/ switch( old_datum ){ /*** input = bytes ***/ case MRI_byte: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = bptr[kk][ii] ; break ; /*** input = shorts ***/ case MRI_short: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = sptr[kk][ii] ; break ; /*** input = floats ***/ case MRI_float: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = fptr[kk][ii] ; break ; /*** input = complex (note we use absolute value) ***/ case MRI_complex: for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] = CABS(cptr[kk][ii]) ; break ; } /* end of switch over input type */ /*** scale? ***/ if( use_fac ) for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] *= fac[kk] ; /** compute mean and slope **/ x0 = x1 = 0.0 ; for( kk=0 ; kk < nuse ; kk++ ){ x0 += fxar[kk] ; x1 += fxar[kk] * dtr[kk] ; } x0 *= d0fac ; x1 *= d1fac ; /* factors to remove mean and trend */ ts_mean = x0 ; ts_slope = x1 / tdelta ; /** detrend? **/ if( detrend ) for( kk=0 ; kk < nuse ; kk++ ) fxar[kk] -= (x0 + x1 * dtr[kk]) ; /** compute start time of this timeseries **/ iz = ii / nxy ; /* which slice am I in? */ if( iz != izold ){ /* in a new slice? */ tzero = THD_timeof( ignore , old_dset->daxes->zzorg + iz*old_dset->daxes->zzdel , old_dset->taxis ) ; izold = iz ; if( DSET_TIMEUNITS(old_dset) == UNITS_MSEC_TYPE ) tzero *= 0.001 ; } /*** compute output ***/ #if 0 user_func( tzero,tdelta , nuse,fxar,ts_mean,ts_slope , user_data , fout+ii ) ; #else ufunc( tzero,tdelta , nuse,fxar,ts_mean,ts_slope , user_data , fout+ii ) ; #endif } /* end of outer loop over 1 voxels at a time */ DSET_unload( old_dset ) ; /* don't need this no more */ /* end notification */ #if 0 user_func( 0.0 , 0.0 , 0 , NULL,0.0,0.0 , user_data , NULL ) ; #else ufunc( 0.0 , 0.0 , 0 , NULL,0.0,0.0 , user_data , NULL ) ; #endif nbad = thd_floatscan( nvox , fout ) ; /* 08 Aug 2000 */ if( nbad > 0 ) fprintf(stderr, "++ Warning: %d bad floats computed in MAKER_4D_to_typed_fim\n\a", nbad ) ; /*------------------------------------------------------------*/ /*------- The output is now in fout[ii], ii=0..nvox-1. We must now put this into the output dataset -------*/ switch( new_datum ){ /*** output is floats is the simplest: we just have to attach the fout brick to the dataset ***/ case MRI_float: EDIT_substitute_brick( new_dset , 0 , MRI_float , fout ) ; fout = NULL ; /* so it won't be freed later */ break ; /*** output is shorts: we have to create a scaled sub-brick from fout ***/ case MRI_short:{ short * bout ; float sfac ; /*-- get output sub-brick --*/ bout = (short *) malloc( sizeof(short) * nvox ) ; if( bout == NULL ){ fprintf(stderr, "\nFinal malloc error in MAKER_4D_to_fim - is memory exhausted?\n\a"); EXIT(1) ; } /*-- find scaling and then scale --*/ sfac = MCW_vol_amax( nvox,1,1 , MRI_float , fout ) ; if( sfac > 0.0 ){ sfac = 32767.0 / sfac ; EDIT_coerce_scale_type( nvox,sfac , MRI_float,fout , MRI_short,bout ) ; sfac = 1.0 / sfac ; } /*-- put output brick into dataset, and store scale factor --*/ EDIT_substitute_brick( new_dset , 0 , MRI_short , bout ) ; EDIT_dset_items( new_dset , ADN_brick_fac , &sfac , ADN_none ) ; } break ; /*** output is bytes (byte = unsigned char) we have to create a scaled sub-brick from fout ***/ case MRI_byte:{ byte * bout ; float sfac ; /*-- get output sub-brick --*/ bout = (byte *) malloc( sizeof(byte) * nvox ) ; if( bout == NULL ){ fprintf(stderr, "\nFinal malloc error in MAKER_4D_to_fim - is memory exhausted?\n\a"); EXIT(1) ; } /*-- find scaling and then scale --*/ sfac = MCW_vol_amax( nvox,1,1 , MRI_float , fout ) ; if( sfac > 0.0 ){ sfac = 255.0 / sfac ; EDIT_coerce_scale_type( nvox,sfac , MRI_float,fout , MRI_byte,bout ) ; sfac = 1.0 / sfac ; } /*-- put output brick into dataset, and store scale factor --*/ EDIT_substitute_brick( new_dset , 0 , MRI_byte , bout ) ; EDIT_dset_items( new_dset , ADN_brick_fac , &sfac , ADN_none ) ; } break ; } /* end of switch on output data type */ /*-------------- Cleanup and go home ----------------*/ FREE_WORKSPACE ; return new_dset ; }
int RIC_CorrectDataset(THD_3dim_dataset * dset, MRI_IMAGE * phase, const double * a, const double * b, int M, int ignore) { float scalefactor; /* Scaling factor for current dset brick */ int ival, nvals; /* Current, number of dset timepoints */ int ivox, nvoxs; /* Current, number of voxels in dset subbrick */ float * pdata; /* Phase data */ int m; /* Current order number (1, M) */ double mp, cmp, smp; /* m * phase; cos(mp); sin(mp) */ double sampd; /* Intersample time in ms for phase */ double slicetime; /* Time of current slice in milliseconds */ int slicestart; /* Voxel index of 1st voxel in current slice */ int sliceend; /* End voxel index of current slice */ int icoeff, ncoeffs; /* Current, number of a,b coefficients */ int islice, nslices; /* Current, number of slices in a subbrick */ int nvoxpers; /* The number of voxels per slice */ /* Quick check of arguments */ if (!ISVALID_3DIM_DATASET(dset) || DSET_NVALS(dset) < 1 || !ISVALID_TIMEAXIS(dset->taxis) || phase == NULL || phase->nx < 1 || phase->ny != 1 || a == NULL || b == NULL || M < 1 || ignore < 0 || ignore >= DSET_NVALS(dset)) { return -1; } /* Initialize */ DSET_load(dset); nvals = DSET_NVALS(dset); nvoxpers = dset->daxes->nxx * dset->daxes->nyy; nslices = dset->daxes->nzz; nvoxs = nvoxpers * nslices; ncoeffs = nvoxs * M; sampd = dset->taxis->ttdel * nvals / phase->nx; switch (dset->taxis->units_type) { case UNITS_MSEC_TYPE: break; case UNITS_SEC_TYPE: sampd *= 1000; break; case UNITS_HZ_TYPE: sampd = 1000 / sampd; break; default: return -1; } pdata = MRI_FLOAT_PTR(phase); /* Correct each subbrick in dset */ /* Iterate over dset timepoints (TRs) => BRIK read from disk once */ for (ival = ignore; ival < nvals; ival += 1) { scalefactor = DSET_BRICK_FACTOR(dset, ival); /* Apply each order of correction to each voxel of the subbrick */ switch (DSET_BRICK_TYPE(dset, ival)) { case MRI_short: RIC_CORRECTDATASET__DO_CORRECT(short); break; case MRI_byte: RIC_CORRECTDATASET__DO_CORRECT(byte); break; case MRI_float: RIC_CORRECTDATASET__DO_CORRECT(float); break; default: /* Unsupported datatype */ return -1; } } return 0; }
int RIC_CalcCoeffAB(THD_3dim_dataset * dset, MRI_IMAGE * phase, const double * avg, double ** a, double ** b, int M, int ignore) { float scalefactor; /* Scaling factor for current dset brick */ int ival, nvals; /* Current, number of dset timepoints */ int ivox, nvoxs; /* Current, number of voxels in dset subbrick */ double * newa, * newb; /* Coefficients a and b, to be calculated */ float * pdata; /* Phase data */ int m; /* Current order number (1, M) */ double mp, cmp, smp; /* m * current phase; cos(mp); sin(mp) */ double sampd; /* Intersample time in ms for phase */ double slicetime; /* Time of current slice in milliseconds */ int slicestart; /* Voxel index of 1st voxel in current slice */ int sliceend; /* End voxel index of current slice */ double * denoma, * denomb; /* Coeff denominators for each m and slice */ int idenom, ndenoms; /* Current, number of coeff denominators */ int inumer, nnumers; /* Current, number of coeff numerators */ int islice, nslices; /* Current, number of slices in a subbrick */ int nvoxpers; /* The number of voxels per slice */ /* Quick check of arguments */ if (!ISVALID_3DIM_DATASET(dset) || DSET_NVALS(dset) < 1 || !ISVALID_TIMEAXIS(dset->taxis) || phase == NULL || phase->nx < 1 || phase->ny != 1 || avg == NULL || a == NULL || b == NULL || M < 1 || ignore < 0 || ignore >= DSET_NVALS(dset)) { return -1; } /* Initialize */ DSET_load(dset); nvals = DSET_NVALS(dset); nvoxpers = dset->daxes->nxx * dset->daxes->nyy; nslices = dset->daxes->nzz; nvoxs = nvoxpers * nslices; ndenoms = nslices * M; nnumers = nvoxs * M; sampd = dset->taxis->ttdel * nvals / phase->nx; switch (dset->taxis->units_type) { case UNITS_MSEC_TYPE: break; case UNITS_SEC_TYPE: sampd *= 1000; break; case UNITS_HZ_TYPE: sampd = 1000 / sampd; break; default: return -1; } pdata = MRI_FLOAT_PTR(phase); newa = malloc(sizeof(double) * nnumers); if (newa == NULL) { return -1; } newb = malloc(sizeof(double) * nnumers); if (newb == NULL) { free(newa); return -1; } for (inumer = 0; inumer < nnumers; inumer += 1) { newa[inumer] = 0.0; newb[inumer] = 0.0; } denoma = malloc(sizeof(double) * ndenoms); if (denoma == NULL) { free(newa); free(newb); return -1; } denomb = malloc(sizeof(double) * ndenoms); if (denomb == NULL) { free(newa); free(newb); free(denoma); return -1; } for (idenom = 0; idenom < ndenoms; idenom += 1) { denoma[idenom] = 0.0; denomb[idenom] = 0.0; } /* Calculate the coeff numerators and denominators */ /* Iterate over dset timepoints (TRs) => BRIK read from disk once */ for (ival = ignore; ival < nvals; ival += 1) { scalefactor = DSET_BRICK_FACTOR(dset, ival); /* Calculate coeff numerators and denominators over all data */ switch (DSET_BRICK_TYPE(dset, ival)) { case MRI_short: RIC_CALCCOEFFAB__DO_CALCNUMERDENOM(short); break; case MRI_byte: RIC_CALCCOEFFAB__DO_CALCNUMERDENOM(byte); break; case MRI_float: RIC_CALCCOEFFAB__DO_CALCNUMERDENOM(float); break; default: /* Unsupported datatype */ free(newa); free(newb); free(denoma); free(denomb); return -1; } } /* Divide the coeff numerators by their denominators */ idenom = 0; inumer = 0; /* m loop outside voxel loop <- coeff arrays bigger than one volume */ for (m = 1; m <= M; m += 1) { slicestart = 0; /* Iterate over slices */ for (islice = 0; islice < nslices; islice += 1) { sliceend = slicestart + nvoxpers; /* Divide numerator by denominator */ for (ivox = slicestart; ivox < sliceend; ivox += 1) { newa[inumer] /= denoma[idenom]; newb[inumer] /= denomb[idenom]; inumer += 1; } slicestart = sliceend; idenom += 1; } } *a = newa; *b = newb; free(denoma); free(denomb); return 0; }
THD_3dim_dataset * EDIT_full_copy( THD_3dim_dataset *dset , char *new_prefix ) { THD_3dim_dataset *new_dset ; int ival , ityp , nbytes , nvals ; void *new_brick , *old_brick ; ENTRY("EDIT_full_copy") ; /*-- sanity check --*/ if( ! ISVALID_3DIM_DATASET(dset) ) RETURN(NULL) ; /*-- make the empty copy --*/ new_dset = EDIT_empty_copy( dset ) ; /* copy is set to MALLOC memory */ /*-- change its name? --*/ if( new_prefix != NULL ) EDIT_dset_items( new_dset , ADN_prefix , new_prefix , ADN_label1 , new_prefix , ADN_none ) ; /*-- make brick(s) for this dataset --*/ if( !DSET_LOADED(dset) ) DSET_load(dset) ; /* make sure is in memory */ nvals = DSET_NVALS(dset) ; for( ival=0 ; ival < nvals ; ival++ ){ ityp = DSET_BRICK_TYPE(new_dset,ival) ; /* type of data */ nbytes = DSET_BRICK_BYTES(new_dset,ival) ; /* how much data */ new_brick = malloc( nbytes ) ; /* make room */ if( new_brick == NULL ){ THD_delete_3dim_dataset( new_dset , False ) ; RETURN(NULL) ; } EDIT_substitute_brick( new_dset , ival , ityp , new_brick ) ; /*-- copy data from old brick to new brick --*/ old_brick = DSET_BRICK_ARRAY(dset,ival) ; if( old_brick == NULL ){ THD_delete_3dim_dataset( new_dset , False ) ; RETURN(NULL) ; } memcpy( new_brick , old_brick , nbytes ) ; } if (0) { /* For DG to activate */ THD_copy_labeltable_atr( new_dset->dblk, dset->dblk); } RETURN( new_dset ); }
void UC_read_opts( int argc , char * argv[] ) { int nopt = 1 ; float val ; int kk, nxyz, mm,nn ; float * vv , * bb ; while( nopt < argc && argv[nopt][0] == '-' ){ /**** -verbose ****/ if( strncmp(argv[nopt],"-verbose",5) == 0 ){ UC_be_quiet = 0 ; nopt++ ; continue ; } /**** -ref file.1D ****/ if( strncmp(argv[nopt],"-ref",4) == 0 ){ MRI_IMAGE * im ; nopt++ ; if( nopt >= argc ) UC_syntax("-ref needs an argument!") ; im = mri_read( argv[nopt] ) ; if( im == NULL ) UC_syntax("Can't read -ref file!") ; if( im->kind == MRI_float ){ UC_ref = im ; } else { UC_ref = mri_to_float(im) ; mri_free(im) ; } im = mri_transpose(UC_ref) ; mri_free(UC_ref) ; UC_ref = im ; nopt++ ; continue ; } /**** -prefix prefix ****/ if( strncmp(argv[nopt],"-prefix",6) == 0 ){ nopt++ ; if( nopt >= argc ) UC_syntax("-prefix needs an argument!") ; MCW_strncpy( UC_prefix , argv[nopt++] , THD_MAX_PREFIX ) ; continue ; } /**** -mask mset ****/ if( strncmp(argv[nopt],"-mask",5) == 0 ){ THD_3dim_dataset * mset ; int ii ; nopt++ ; if( nopt >= argc ) UC_syntax("need arguments after -mask!") ; mset = THD_open_dataset( argv[nopt] ) ; if( mset == NULL ) UC_syntax("can't open -mask dataset!") ; UC_mask = THD_makemask( mset , 0 , 1.0,0.0 ) ; UC_mask_nvox = DSET_NVOX(mset) ; DSET_delete(mset) ; if( UC_mask == NULL ) UC_syntax("can't use -mask dataset!") ; UC_mask_hits = THD_countmask( UC_mask_nvox , UC_mask ) ; if( UC_mask_hits == 0 ) UC_syntax("mask is all zeros!") ; if( !UC_be_quiet ) printf("--- %d voxels in mask\n",UC_mask_hits) ; nopt++ ; continue ; } /**** unknown switch ****/ fprintf(stderr,"\n*** unrecognized option %s\n",argv[nopt]) ; exit(1) ; } /* end of loop over options */ /*--- a simple consistency check ---*/ /*--- last input is dataset name ---*/ if( nopt >= argc ) UC_syntax("no input dataset name?") ; UC_dset = THD_open_dataset( argv[nopt] ) ; if( !ISVALID_3DIM_DATASET(UC_dset) ){ fprintf(stderr,"\n*** can't open dataset file %s\n",argv[nopt]) ; exit(1) ; } nxyz = DSET_NVOX(UC_dset) ; if( UC_mask != NULL && nxyz != UC_mask_nvox ) UC_syntax("mask and input dataset size mismatch!") ; /*--- load vectors ---*/ UC_nvec = (UC_mask_hits > 0) ? UC_mask_hits : nxyz ; UC_vdim = DSET_NVALS(UC_dset) ; if( UC_vdim < 4 ) UC_syntax("input dataset needs at least 4 sub-bricks!") ; if( UC_ref == NULL || UC_ref->nx < UC_vdim ) UC_syntax("input ref not long enough for input dataset!") ; vv = (float *) malloc( sizeof(float) * UC_nvec * UC_vdim ) ; UC_vec = (float **) malloc( sizeof(float *) * UC_nvec ) ; for( kk=0 ; kk < UC_nvec ; kk++ ) UC_vec[kk] = vv + (kk*UC_vdim) ; if( !UC_be_quiet ) printf("--- reading dataset\n") ; DSET_load(UC_dset) ; CHECK_LOAD_ERROR(UC_dset) ; /* copy brick data into float storage */ if( !UC_be_quiet ) printf("--- loading vectors\n") ; bb = (float *) malloc( sizeof(float) * nxyz ) ; for( mm=0 ; mm < UC_vdim ; mm++ ){ EDIT_coerce_type( nxyz , DSET_BRICK_TYPE(UC_dset,mm) , DSET_ARRAY(UC_dset,mm) , MRI_float , bb ) ; DSET_unload_one( UC_dset , mm ) ; if( UC_mask == NULL ){ for( kk=0 ; kk < nxyz ; kk++ ) UC_vec[kk][mm] = bb[kk] ; } else { for( nn=kk=0 ; kk < nxyz ; kk++ ) if( UC_mask[kk] ) UC_vec[nn++][mm] = bb[kk] ; } } free(bb) ; DSET_unload( UC_dset ) ; /* detrend and normalize vectors */ if( !UC_be_quiet ) printf("--- normalizing vectors\n") ; for( kk=0 ; kk < UC_nvec ; kk++ ) normalize( UC_vdim , UC_vec[kk] ) ; for( kk=0 ; kk < UC_ref->ny ; kk++ ) normalize( UC_vdim , MRI_FLOAT_PTR(UC_ref) + kk*UC_ref->nx ) ; return ; }
int main(int argc, char * argv[]) { char cphase1d[256] = "\0", rphase1d[256] = "\0"; char new_prefix[THD_MAX_PREFIX] = TRIC_O_DEF_NEWPREFIX; MCW_idcode * idc ; THD_3dim_dataset * dset , * new_dset; double * avg = NULL; double * ca , * cb, * ra, * rb; MRI_IMAGE * card = NULL, * resp = NULL; MRI_IMAGE * cardphase = NULL, * respphase = NULL; float threshold = TRIC_C_DEF_THRESHOLD; int ignore = TRIC_I_DEF_IGNORE; int M = TRIC_M_DEF_ORDER; int winsize = TRIC_R_DEF_WINSIZE; float tr; int ival, nvals; FILE * fp; float * cpdata, * rpdata; int argi = 1; /*-------------------------------------------------------------*/ /*----- Check arguments to see if they are reasonable-ish -----*/ if (argc < 2 || strcmp(argv[1], "-help") == 0) { TRIC_printhelp(); exit(-1); } mainENTRY("3dretroicor main"); PRINT_VERSION("3dretroicor") ; machdep(); AFNI_logger("3dretroicor", argc, argv); /* Iterate over commandline args */ while (argi < argc && argv[argi][0] == '-') { /*-- ignore --*/ if (strcmp(argv[argi], "-ignore") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -ignore needs an argument!\n"); exit(1); } argi += 1; ignore = atoi(argv[argi]); if (ignore < 0) { fprintf(stderr, "*** %i is not a valid number to ignore!\n", ignore); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- prefix --*/ if (strcmp(argv[argi], "-prefix") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -prefix needs an argument!\n"); exit(1); } argi += 1; MCW_strncpy(new_prefix, argv[argi], THD_MAX_PREFIX); if (! THD_filename_ok(new_prefix)) { fprintf(stderr, "*** %s is not a valid prefix!\n", new_prefix); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- card --*/ if (strcmp(argv[argi], "-card") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -card needs an argument!\n"); exit(1); } argi += 1; card = mri_read_1D(argv[argi]); if (card == NULL) { fprintf(stderr, "*** Can't read -card %s\n", argv[argi]); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- cardphase --*/ if (strcmp(argv[argi], "-cardphase") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -cardphase needs an argument!\n"); exit(1); } argi += 1; MCW_strncpy(cphase1d, argv[argi], 256); if (! THD_filename_ok(cphase1d)) { fprintf(stderr, "*** Bad name argument for -cardphase\n"); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- threshold --*/ if (strcmp(argv[argi], "-threshold") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -threshold needs an argument!\n"); exit(1); } argi += 1; threshold = atof(argv[argi]); argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- resp --*/ if (strcmp(argv[argi], "-resp") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -resp needs an argument!\n"); exit(1); } argi += 1; resp = mri_read_1D(argv[argi]); if (resp == NULL) { fprintf(stderr, "*** Can't read -resp %s\n", argv[argi]); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- respphase --*/ if (strcmp(argv[argi], "-respphase") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -respphase needs an argument!\n"); exit(1); } argi += 1; MCW_strncpy(rphase1d, argv[argi], 256); if (! THD_filename_ok(rphase1d)) { fprintf(stderr, "*** Bad name argument for -respphase\n"); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } /*-- window --*/ /*-- removed winsize ui --*/ #if 0 if (strcmp(argv[argi], "-window") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -window needs an argument!\n"); exit(1); } argi += 1; winsize = atoi(argv[argi]); argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } #endif /*-- removed winsize ui --*/ /*-- order --*/ if (strcmp(argv[argi], "-order") == 0) { if (argi + 1 >= argc) { fprintf(stderr, "*** -order needs an argument!\n"); exit(1); } argi += 1; M = atoi(argv[argi]); if (M < 1) { fprintf(stderr, "*** %i is not a valid order number\n", M); exit(1); } argi += 1; continue; /* Skip the rest of the loop and go to the next arg */ } } /* End iterating over commandline args */ /* Check that at least one of Cardiac and Resp were selected */ if (card == NULL && resp == NULL) { fprintf(stderr, "*** Need at least one correction (-card, -resp)\n"); exit(1); } /*-- Open and check input dataset --*/ if (argi >= argc) { fprintf(stderr, "*** No input dataset!?\n"); exit(1); } else if (argi < argc - 1) { fprintf(stderr, "*** Too many input datasets?!\n"); exit(1); } dset = THD_open_dataset(argv[argi]); if (! ISVALID_3DIM_DATASET(dset)) { fprintf(stderr, "*** Can't open dataset %s\n", argv[argi]); exit(1); } if (DSET_NUM_TIMES(dset) < 2) { fprintf(stderr, "*** Input dataset is not 3D+time!\n"); exit(1); } /*----------------------------------------------------------*/ /*---------- At this point, the inputs are OK-ish ----------*/ /*-- copy the image data for editing in place --*/ new_dset = EDIT_full_copy( dset , new_prefix ); if( new_dset == NULL ) { fprintf(stderr, "*** Error copying dataset\n"); exit(1); } tross_Copy_History(dset, new_dset); /* Copy and add to new_dset history */ tross_Make_History("3dretroicor", argc, argv, new_dset); DSET_unload( dset ) ; /* We won't need the old dataset anymore */ /*-- calculate cardiac correction coefficients if requested --*/ if (card != NULL) { /*-- convert cardiac waveform to phase --*/ cardphase = RIC_ToCardiacPhase(card, threshold) ; if (cardphase == NULL) { THD_delete_3dim_dataset( new_dset , False ) ; fprintf(stderr, "*** Error transforming cardiac data\n"); exit(2); } /*-- calculate dataset voxel means --*/ avg = RIC_CalcVoxelMeans(new_dset, ignore); if (avg == NULL) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(cardphase); fprintf(stderr, "*** Error calculating dataset voxel means\n"); exit(2); } /*-- calculate coefficients for each voxel --*/ if (RIC_CalcCoeffAB(new_dset, cardphase, avg, &ca, &cb, M, ignore) != 0) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(cardphase); fprintf(stderr, "*** Error calculating cardiac a b coefficients\n"); exit(2); } } /*-- calculate respiratory correction coefficients if requested --*/ if (resp != NULL) { /*-- Set winsize to 1/2 sampling rate of resp in Hz --*/ tr = new_dset->taxis->ttdel; switch (new_dset->taxis->units_type) { case UNITS_MSEC_TYPE: tr /= 1000; break; case UNITS_SEC_TYPE: break; case UNITS_HZ_TYPE: tr = 1 / tr; break; default: THD_delete_3dim_dataset( new_dset , False ) ; fprintf(stderr, "*** Bad time units type in dataset\n"); exit(2); } winsize = ceil(resp->nx / (tr * DSET_NVALS(new_dset)) / 2.0); /*-- convert respiratory waveform to phase --*/ respphase = RIC_ToRespPhase(resp, winsize) ; if (respphase == NULL) { THD_delete_3dim_dataset( new_dset , False ) ; fprintf(stderr, "*** Error transforming resp data\n"); exit(2); } /*-- calculate dataset voxel means if not already done --*/ if (avg == NULL) { avg = RIC_CalcVoxelMeans(new_dset, ignore); if (avg == NULL) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(respphase); fprintf(stderr, "*** Error calculating dataset voxel means2\n"); exit(2); } } /*-- calculate coefficients for each voxel --*/ if (RIC_CalcCoeffAB(new_dset, respphase, avg, &ra, &rb, M, ignore) != 0) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(respphase); fprintf(stderr, "*** Error calculating resp a, b coefficients\n"); exit(2); } } /*-- do cardiac correction if requested --*/ if (card != NULL) { /*-- correct the image data --*/ if (RIC_CorrectDataset(new_dset, cardphase, ca, cb, M, ignore) != 0) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(cardphase); free(ca); free(cb); fprintf(stderr, "*** Error applying cardiac correction\n"); exit(3); } /*-- if requested, write phase data to file and pass to AFNI --*/ if ( THD_filename_ok(cphase1d) ) { /* Write the file */ fp = fopen(cphase1d, "w"); nvals = cardphase->nx; cpdata = MRI_FLOAT_PTR(cardphase); for (ival = 0; ival < nvals; ival += 1) { fprintf(fp, "%f\n", cpdata[ival]); } fclose(fp); } mri_free(cardphase); free(ca); free(cb); free(avg); avg = NULL; } /*-- do resp correction if requested --*/ if (resp != NULL) { /*-- correct the image data --*/ if (RIC_CorrectDataset(new_dset, respphase, ra, rb, M, ignore) != 0) { THD_delete_3dim_dataset( new_dset , False ) ; mri_free(respphase); free(ra); free(rb); fprintf(stderr, "*** Error applying resp correction\n"); exit(3); } /*-- if requested, write phase data to file and pass to AFNI --*/ if ( THD_filename_ok(rphase1d) ) { /* Write the file */ fp = fopen(rphase1d, "w"); nvals = respphase->nx; rpdata = MRI_FLOAT_PTR(respphase); for (ival = 0; ival < nvals; ival += 1) { fprintf(fp, "%f\n", rpdata[ival]); } fclose(fp); } mri_free(respphase); free(ra); free(rb); if (avg != NULL) free(avg); } /*-- write out new dataset --*/ if (DSET_write(new_dset) != False) { fprintf(stderr,"++ output dataset: %s\n",DSET_BRIKNAME(new_dset)) ; exit(0) ; } else { fprintf(stderr, "** 3dretroicor: Failed to write output!\n" ) ; exit(1) ; } /*-- done successfully!!! --*/ exit(0); }
int EDIT_dset_items( THD_3dim_dataset *dset , ... ) { va_list vararg_ptr ; int flag_arg , errnum = 0 ; int redo_bricks , redo_daxes , redo_taxis , ii ; void *dummy ; int iarg ; static int ncall=0 ; /* 20 Dec 2005 */ /**----- variables to flag and store presence of arguments -----**/ int new_prefix = 0; char * prefix = NULL ; int new_directory_name= 0; char * directory_name = NULL ; int new_brick_fac = 0; float * brick_fac = NULL ; int new_malloc_type = 0; int malloc_type = ILLEGAL_TYPE; int new_datum_all = 0; int datum_all = ILLEGAL_TYPE; int new_datum_array = 0; int * datum_array = NULL ; int new_nvals = 0; int nvals = 0 ; int new_nxyz = 0; THD_ivec3 nxyz ; int new_xyzdel = 0; THD_fvec3 xyzdel ; int new_xyzorg = 0; THD_fvec3 xyzorg ; int new_xyzorient = 0; THD_ivec3 xyzorient ; int new_to_dicomm = 0; THD_mat33 to_dicomm ; int new_ntt = 0; int ntt = 0 ; int new_ttorg = 0; float ttorg = 0.0 ; int new_ttdel = 0; float ttdel = 0.0 ; int new_ttdur = 0; float ttdur = 0.0 ; int new_nsl = 0; int nsl = 0 ; int new_zorg_sl = 0; float zorg_sl = 0.0 ; int new_dz_sl = 0; float dz_sl = 0.0 ; int new_toff_sl = 0; float * toff_sl = NULL ; int new_type = 0; int type = ILLEGAL_TYPE; int new_view_type = 0; int view_type = ILLEGAL_TYPE; int new_func_type = 0; int func_type = ILLEGAL_TYPE; int new_label1 = 0; char * label1 = NULL ; int new_label2 = 0; char * label2 = NULL ; int new_self_name = 0; char * self_name = NULL ; int new_warp_parent = 0; THD_3dim_dataset *warp_parent = NULL ; int new_anat_parent = 0; THD_3dim_dataset *anat_parent = NULL ; int new_stat_aux = 0; float * stat_aux = NULL ; int new_warp = 0; THD_warp * warp = NULL ; int new_tunits = 0; int tunits = ILLEGAL_TYPE; int new_keywords = 0; char * keywords = NULL ; /* 30 Nov 1997 */ int new_brick_label_one = 0 ; char * brick_label_one = NULL ; int brick_label_one_iv = -1 ; int new_brick_fac_one = 0 ; float brick_fac_one = 0.0 ; int brick_fac_one_iv = -1 ; int new_brick_stataux_one = 0 ; float * brick_stataux_one = NULL ; int brick_stataux_one_iv = -1 ; int new_brick_keywords_one = 0 ; char * brick_keywords_one = NULL ; int brick_keywords_one_iv = -1 ; /* 19 Dec 2005 */ int new_ijk_to_dicom = 0; mat44 ijk_to_dicom ; /* 14 July 2006 */ int cmode = COMPRESS_NOFILE; /* check compression mode for NIFTI separately */ int new_smode=STORAGE_UNDEFINED; /* ZSS Feb 2012 */ /****---------------------- Sanity Check ----------------------****/ ENTRY("EDIT_dset_items") ; ncall++ ; /* 20 Dec 2005: to keep track of number of calls for EDERR */ if( ! ISVALID_3DIM_DATASET(dset) ){ /* bad data */ EDERR("invalid input dataset"); RETURN(errnum); } /****----------- Scan input argument list; - Load data into locals (va_arg); - Check for legal values; - Flag its presence (the new_ variables); - Carry out simple processing that doesn't depend on the presence of other arguments. ---------****/ va_start( vararg_ptr , dset ) ; /** Initialize arg reading **/ iarg = 1 ; memset(&ijk_to_dicom, 0, sizeof(mat44)); memset(&xyzorient, 0, sizeof(THD_ivec3)); memset(&xyzorg, 0, sizeof(THD_fvec3)); memset(&xyzdel, 0, sizeof(THD_fvec3)); memset(&nxyz, 0, sizeof(THD_ivec3)); do{ flag_arg = va_arg( vararg_ptr , int ) ; /** Get next arg **/ if( flag_arg == ADN_none ) break ; /** No more args! **/ #if 0 fprintf(stderr,"EDIT_dset_items: iarg=%d flag_arg=%d\n",iarg,flag_arg) ; #endif iarg++ ; switch( flag_arg ){ default:{ int iv ; char str[128] ; /** 30 Nov 1997: check for special cases **/ iv = flag_arg - ADN_brick_label_one ; if( iv >= 0 && iv < ADN_ONE_STEP ){ brick_label_one_iv = iv ; brick_label_one = va_arg( vararg_ptr , char * ) ; if( brick_label_one != NULL ) new_brick_label_one = 1 ; break ; /* exit switch */ } iv = flag_arg - ADN_brick_fac_one ; if( iv >= 0 && iv < ADN_ONE_STEP ){ brick_fac_one_iv = iv ; brick_fac_one = va_arg( vararg_ptr , double ) ; new_brick_fac_one = 1 ; break ; /* exit switch */ } iv = flag_arg - ADN_brick_stataux_one ; if( iv >= 0 && iv < ADN_ONE_STEP ){ brick_stataux_one_iv = iv ; brick_stataux_one = va_arg( vararg_ptr , float * ) ; if( brick_stataux_one != NULL ) new_brick_stataux_one = 1 ; break ; /* exit switch */ } iv = flag_arg - ADN_brick_keywords_replace_one ; if( iv >= 0 && iv < ADN_ONE_STEP ){ brick_keywords_one_iv = iv ; brick_keywords_one = va_arg( vararg_ptr , char * ) ; new_brick_keywords_one = 1 ; break ; /* exit switch */ }