/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs,n; MRIS *SurfReg[100]; MRI *SrcVal, *TrgVal; char *base; nargs = handle_version_option (argc, argv, vcid, "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; cmdline = argv2cmdline(argc,argv); uname(&uts); getcwd(cwd,2000); Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); if (checkoptsonly) return(0); dump_options(stdout); printf("Loading %s\n",SrcValFile); SrcVal = MRIread(SrcValFile); if(SrcVal==NULL) exit(1); if(DoSynthRand) { if (SynthSeed < 0) SynthSeed = PDFtodSeed(); printf("INFO: synthesizing, seed = %d\n",SynthSeed); srand48(SynthSeed); MRIrandn(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes,0, 1, SrcVal); } if(DoSynthOnes != 0) { printf("INFO: filling input with all 1s\n"); MRIconst(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes, 1, SrcVal); } for(n=0; n<nsurfs; n++) { printf("%d Loading %s\n",n+1,SurfRegFile[n]); base = fio_basename(SurfRegFile[n],".tri"); if(strcmp(base,"ic7")==0) { // Have to do it this way to rescale. Need to find a better more robust way. printf(" reading as ico 7, rescaling radius to 100\n"); SurfReg[n] = ReadIcoByOrder(7, 100); } else SurfReg[n] = MRISread(SurfRegFile[n]); free(base); if(SurfReg[n]==NULL) exit(1); } TrgVal = MRISapplyReg(SrcVal, SurfReg, nsurfs,ReverseMapFlag,DoJac,UseHash); if(TrgVal == NULL) exit(1); printf("Writing %s\n",TrgValFile); MRIwrite(TrgVal,TrgValFile); printf("mris_apply_reg done\n"); return 0; }
/*---------------------------------------------------------------*/ int main(int argc, char **argv) { SDCMFILEINFO **sdfi_list; SDCMFILEINFO *sdfi=NULL; int nlist; int NRuns; int nthfile; char *fname, *psname, *protoname; int PrevRunNo; Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); cmdline = argv2cmdline(argc,argv); parse_commandline(argc, argv); check_options(); uname(&uts); getcwd(cwd,2000); fprintf(stdout,"%s\n",vcid); fprintf(stdout,"cwd %s\n",cwd); fprintf(stdout,"cmdline %s\n",cmdline); fprintf(stdout,"sysname %s\n",uts.sysname); fprintf(stdout,"hostname %s\n",uts.nodename); fprintf(stdout,"machine %s\n",uts.machine); fprintf(stdout,"user %s\n",VERuser()); strip_leading_slashes(sdicomdir); /* open the output file, or set output stream to stdout */ if (outfile != NULL) { outstream = fopen(outfile,"w"); if (outstream == NULL) { fprintf(stderr,"ERROR: could not open %s for writing\n",outfile); exit(1); } } else outstream = stdout; /* Get all the Siemens DICOM files from this directory */ fprintf(stderr,"INFO: scanning path to Siemens DICOM DIR:\n %s\n", sdicomdir); sdfi_list = ScanSiemensDCMDir(sdicomdir, &nlist); if (sdfi_list == NULL) { fprintf(stderr,"ERROR: scanning directory %s\n",sdicomdir); exit(1); } printf("INFO: found %d Siemens files\n",nlist); printf("%s\n",sdfi_list[0]->NumarisVer); tmpstring = sdcmExtractNumarisVer(sdfi_list[0]->NumarisVer, &Maj, &Min, &MinMin); if (tmpstring == NULL) free(tmpstring); else { if ((Min == 1 && MinMin <= 6) && Maj < 4) { // This should only be run for pretty old data. I've lost // track as to which versions should do this. With Maj<4, // I'm pretty sure that this section of code will never // be run. It might need to be run with version 4VA16 // and earlier. Note: this same code is in DICOMRead.c printf("Computing TR with number of slices\n"); TRSlice = 1; } } /* Sort the files by Series, Slice Position, and Image Number */ printf("Sorting\n"); fflush(stdout); fflush(stderr); SortSDCMFileInfo(sdfi_list,nlist); /* Assign run numbers to each file (count number of runs)*/ if (sortbyrun) { fprintf(stderr,"Assigning Run Numbers\n"); fflush(stderr); NRuns = sdfiAssignRunNo2(sdfi_list, nlist); if (NRuns == 0) { fprintf(stderr,"ERROR: sorting runs\n"); fflush(stderr); exit(1); } } PrevRunNo = -1; for (nthfile = 0; nthfile < nlist; nthfile++) { sdfi = sdfi_list[nthfile]; if (summarize && PrevRunNo == sdfi->RunNo) continue; PrevRunNo = sdfi->RunNo; sdfi->RepetitionTime /= 1000.0; // Old version of Siemens software had reptime as time between // slices. New has reptime as time between volumes. // Code (above) looks at version and does the right thing. if (sdfi->IsMosaic && TRSlice) sdfi->RepetitionTime *= sdfi->VolDim[2]; fname = fio_basename(sdfi->FileName,NULL); psname = strip_white_space(sdfi->PulseSequence); protoname = strip_white_space(sdfi->ProtocolName); fprintf(outstream, "%4d %s %3d %d %4d %d %3d %3d %3d %3d %8.4f %8.4f %s\n", nthfile+1, fname, sdfi->SeriesNo, sdfi->ErrorFlag, sdfi->ImageNo, sdfi->IsMosaic, sdfi->VolDim[0], sdfi->VolDim[1], sdfi->VolDim[2], sdfi->NFrames, sdfi->RepetitionTime, sdfi->EchoTime, protoname); fflush(outstream); free(fname); free(psname); free(protoname); } while (nlist--) { // free strings free(sdfi_list[nlist]->FileName); free(sdfi_list[nlist]->StudyDate); free(sdfi_list[nlist]->StudyTime); free(sdfi_list[nlist]->PatientName); free(sdfi_list[nlist]->SeriesTime); free(sdfi_list[nlist]->AcquisitionTime); free(sdfi_list[nlist]->ScannerModel); free(sdfi_list[nlist]->NumarisVer); free(sdfi_list[nlist]->PulseSequence); free(sdfi_list[nlist]->ProtocolName); free(sdfi_list[nlist]->PhEncDir); // free(sdfi_list[nlist]); } free(sdfi_list); if (outfile != NULL) fclose(outstream); fflush(stdout); fflush(stderr); exit(0); return(0); }
/*--------------------------------------------------*/ int main(int argc, char **argv) { int nargs, nthin, nframestot=0, nr=0,nc=0,ns=0, fout; int r,c,s,f,outf,nframes,err,nthrep; double v, v1, v2, vavg, vsum; int inputDatatype=MRI_UCHAR; MATRIX *Upca=NULL,*Spca=NULL; MRI *Vpca=NULL; char *stem; /* rkt: check for and handle version tag */ nargs = handle_version_option (argc, argv, vcid, "$Name: stable5 $"); if (nargs && argc - nargs == 1) { exit (0); } argc -= nargs; Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) { usage_exit(); } parse_commandline(argc, argv); check_options(); dump_options(stdout); if(maskfile) { printf("Loading mask %s\n",maskfile); mask = MRIread(maskfile); if(mask == NULL) { exit(1); } } printf("ninputs = %d\n",ninputs); if(DoCheck) { printf("Checking inputs\n"); for(nthin = 0; nthin < ninputs; nthin++) { if(Gdiag_no > 0 || debug) { printf("Checking %2d %s\n",nthin,inlist[nthin]); fflush(stdout); } mritmp = MRIreadHeader(inlist[nthin],MRI_VOLUME_TYPE_UNKNOWN); if (mritmp == NULL) { printf("ERROR: reading %s\n",inlist[nthin]); exit(1); } if (nthin == 0) { nc = mritmp->width; nr = mritmp->height; ns = mritmp->depth; } if (mritmp->width != nc || mritmp->height != nr || mritmp->depth != ns) { printf("ERROR: dimension mismatch between %s and %s\n", inlist[0],inlist[nthin]); exit(1); } nframestot += mritmp->nframes; inputDatatype = mritmp->type; // used by DoKeepDatatype option MRIfree(&mritmp); } } else { printf("NOT Checking inputs, assuming nframestot = ninputs\n"); nframestot = ninputs; mritmp = MRIreadHeader(inlist[0],MRI_VOLUME_TYPE_UNKNOWN); if (mritmp == NULL) { printf("ERROR: reading %s\n",inlist[0]); exit(1); } nc = mritmp->width; nr = mritmp->height; ns = mritmp->depth; MRIfree(&mritmp); } printf("nframestot = %d\n",nframestot); if (DoRMS) { if (ninputs != 1) { printf("ERROR: --rms supports only single input w/ multiple frames\n"); exit (1); } if (nframestot == 1) { printf("ERROR: --rms input must have multiple frames\n"); exit (1); } } if(ngroups != 0) { printf("Creating grouped mean matrix ngroups=%d, nper=%d\n", ngroups,nframestot/ngroups); M = GroupedMeanMatrix(ngroups,nframestot); if(M==NULL) { exit(1); } if(debug) { MatrixPrint(stdout,M); } } if(M != NULL) { if(nframestot != M->cols) { printf("ERROR: dimension mismatch between inputs (%d) and matrix (%d)\n", nframestot,M->rows); exit(1); } } if (DoPaired) { if (remainder(nframestot,2) != 0) { printf("ERROR: --paired-xxx specified but there are an " "odd number of frames\n"); exit(1); } } printf("Allocing output\n"); fflush(stdout); int datatype=MRI_FLOAT; if (DoKeepDatatype) { datatype = inputDatatype; } if (DoRMS) { // RMS always has single frame output mriout = MRIallocSequence(nc,nr,ns,datatype,1); } else { mriout = MRIallocSequence(nc,nr,ns,datatype,nframestot); } if (mriout == NULL) { exit(1); } printf("Done allocing\n"); fout = 0; for (nthin = 0; nthin < ninputs; nthin++) { if (DoRMS) break; // MRIrms reads the input frames if(Gdiag_no > 0 || debug) { printf("Loading %dth input %s\n", nthin+1,fio_basename(inlist[nthin],NULL)); fflush(stdout); } mritmp = MRIread(inlist[nthin]); if(mritmp == NULL) { printf("ERROR: loading %s\n",inlist[nthin]); exit(1); } if(nthin == 0) { MRIcopyHeader(mritmp, mriout); //mriout->nframes = nframestot; } if(DoAbs) { if(Gdiag_no > 0 || debug) { printf("Removing sign from input\n"); } MRIabs(mritmp,mritmp); } if(DoPos) { if(Gdiag_no > 0 || debug) { printf("Setting input negatives to 0.\n"); } MRIpos(mritmp,mritmp); } if(DoNeg) { if(Gdiag_no > 0 || debug) { printf("Setting input positives to 0.\n"); } MRIneg(mritmp,mritmp); } for(f=0; f < mritmp->nframes; f++) { for(c=0; c < nc; c++) { for(r=0; r < nr; r++) { for(s=0; s < ns; s++) { v = MRIgetVoxVal(mritmp,c,r,s,f); MRIsetVoxVal(mriout,c,r,s,fout,v); } } } fout++; } MRIfree(&mritmp); } if(DoCombine) { // Average frames from non-zero voxels int nhits; mritmp = MRIallocSequence(nc,nr,ns,MRI_FLOAT,1); MRIcopyHeader(mritmp,mriout); for(c=0; c < nc; c++) { for(r=0; r < nr; r++) { for(s=0; s < ns; s++) { nhits = 0; vsum = 0; for(f=0; f < mriout->nframes; f++) { v = MRIgetVoxVal(mriout,c,r,s,f); if (v > 0) { vsum += v; nhits ++; } } if(nhits > 0 ) { MRIsetVoxVal(mritmp,c,r,s,0,vsum/nhits); } } // for s }// for r } // for c MRIfree(&mriout); mriout = mritmp; } // do combine if(DoPrune) { // This computes the prune mask, applied below printf("Computing prune mask \n"); PruneMask = MRIframeBinarize(mriout,FLT_MIN,NULL); printf("Found %d voxels in prune mask\n",MRInMask(PruneMask)); } if(DoNormMean) { printf("Normalizing by mean across frames\n"); MRInormalizeFramesMean(mriout); } if(DoNorm1) { printf("Normalizing by first across frames\n"); MRInormalizeFramesFirst(mriout); } if(DoASL) { printf("Computing ASL matrix matrix\n"); M = ASLinterpMatrix(mriout->nframes); } if(M != NULL) { printf("Multiplying by matrix\n"); mritmp = fMRImatrixMultiply(mriout, M, NULL); if(mritmp == NULL) { exit(1); } MRIfree(&mriout); mriout = mritmp; } if(DoPaired) { printf("Combining pairs\n"); mritmp = MRIcloneBySpace(mriout,-1,mriout->nframes/2); for (c=0; c < nc; c++) { for (r=0; r < nr; r++) { for (s=0; s < ns; s++) { fout = 0; for (f=0; f < mriout->nframes; f+=2) { v1 = MRIgetVoxVal(mriout,c,r,s,f); v2 = MRIgetVoxVal(mriout,c,r,s,f+1); v = 0; if(DoPairedAvg) { v = (v1+v2)/2.0; } if(DoPairedSum) { v = (v1+v2); } if(DoPairedDiff) { v = v1-v2; // difference } if(DoPairedDiffNorm) { v = v1-v2; // difference vavg = (v1+v2)/2.0; if (vavg != 0.0) { v = v/vavg; } } if(DoPairedDiffNorm1) { v = v1-v2; // difference if (v1 != 0.0) { v = v/v1; } else { v = 0; } } if(DoPairedDiffNorm2) { v = v1-v2; // difference if (v2 != 0.0) { v = v/v2; } else { v = 0; } } MRIsetVoxVal(mritmp,c,r,s,fout,v); fout++; } } } } MRIfree(&mriout); mriout = mritmp; } nframes = mriout->nframes; printf("nframes = %d\n",nframes); if(DoBonfCor) { DoAdd = 1; AddVal = -log10(mriout->nframes); } if(DoMean) { printf("Computing mean across frames\n"); mritmp = MRIframeMean(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoMedian) { printf("Computing median across frames\n"); mritmp = MRIframeMedian(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoMeanDivN) { printf("Computing mean2 = sum/(nframes^2)\n"); mritmp = MRIframeSum(mriout,NULL); MRIfree(&mriout); mriout = mritmp; MRImultiplyConst(mriout, 1.0/(nframes*nframes), mriout); } if(DoSum) { printf("Computing sum across frames\n"); mritmp = MRIframeSum(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoTAR1) { printf("Computing temoral AR1 %d\n",mriout->nframes-TAR1DOFAdjust); mritmp = fMRItemporalAR1(mriout,TAR1DOFAdjust,NULL,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoStd || DoVar) { printf("Computing std/var across frames\n"); if(mriout->nframes < 2) { printf("ERROR: cannot compute std from one frame\n"); exit(1); } //mritmp = fMRIvariance(mriout, -1, 1, NULL); mritmp = fMRIcovariance(mriout, 0, -1, NULL, NULL); if(DoStd) { MRIsqrt(mritmp, mritmp); } MRIfree(&mriout); mriout = mritmp; } if(DoMax) { printf("Computing max across all frames \n"); mritmp = MRIvolMax(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoMaxIndex) { printf("Computing max index across all frames \n"); mritmp = MRIvolMaxIndex(mriout,1,NULL,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoConjunction) { printf("Computing conjunction across all frames \n"); mritmp = MRIconjunct(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoMin) { printf("Computing min across all frames \n"); mritmp = MRIvolMin(mriout,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoSort) { printf("Sorting \n"); mritmp = MRIsort(mriout,mask,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoVote) { printf("Voting \n"); mritmp = MRIvote(mriout,mask,NULL); MRIfree(&mriout); mriout = mritmp; } if(DoMultiply) { printf("Multiplying by %lf\n",MultiplyVal); MRImultiplyConst(mriout, MultiplyVal, mriout); } if(DoAdd) { printf("Adding %lf\n",AddVal); MRIaddConst(mriout, AddVal, mriout); } if(DoSCM) { printf("Computing spatial correlation matrix (%d)\n",mriout->nframes); mritmp = fMRIspatialCorMatrix(mriout); if(mritmp == NULL) { exit(1); } MRIfree(&mriout); mriout = mritmp; } if(DoPCA) { // Saves only non-zero components printf("Computing PCA\n"); if(PCAMaskFile) { printf(" PCA Mask %s\n",PCAMaskFile); PCAMask = MRIread(PCAMaskFile); if(PCAMask == NULL) { exit(1); } } err=MRIpca(mriout, &Upca, &Spca, &Vpca, PCAMask); if(err) { exit(1); } stem = IDstemFromName(out); sprintf(tmpstr,"%s.u.mtx",stem); MatrixWriteTxt(tmpstr, Upca); sprintf(tmpstr,"%s.stats.dat",stem); WritePCAStats(tmpstr,Spca); MRIfree(&mriout); mriout = Vpca; } if(NReplications > 0) { printf("NReplications %d\n",NReplications); mritmp = MRIallocSequence(mriout->width, mriout->height, mriout->depth, mriout->type, mriout->nframes*NReplications); if(mritmp == NULL) { exit(1); } printf("Done allocing\n"); MRIcopyHeader(mriout,mritmp); for(c=0; c < mriout->width; c++) { for(r=0; r < mriout->height; r++) { for(s=0; s < mriout->depth; s++) { outf = 0; for(nthrep = 0; nthrep < NReplications; nthrep++) { for(f=0; f < mriout->nframes; f++) { v = MRIgetVoxVal(mriout,c,r,s,f); MRIsetVoxVal(mritmp,c,r,s,outf,v); outf ++; } } } } } MRIfree(&mriout); mriout = mritmp; } if(DoPrune) { // Apply prune mask that was computed above printf("Applying prune mask \n"); MRImask(mriout, PruneMask, mriout, 0, 0); } if(DoRMS) { printf("Computing RMS across input frames\n"); mritmp = MRIread(inlist[0]); MRIcopyHeader(mritmp, mriout); MRIrms(mritmp,mriout); } printf("Writing to %s\n",out); err = MRIwrite(mriout,out); if(err) { exit(err); } return(0); }
/*---------------------------------------------------------------*/ int main(int argc, char *argv[]) { int nargs,n,err; MRIS *SurfReg[100]; MRI *SrcVal, *TrgVal; char *base; COLOR_TABLE *ctab=NULL; nargs = handle_version_option (argc, argv, vcid, "$Name: $"); if (nargs && argc - nargs == 1) exit (0); argc -= nargs; cmdline = argv2cmdline(argc,argv); uname(&uts); getcwd(cwd,2000); Progname = argv[0] ; argc --; argv++; ErrorInit(NULL, NULL, NULL) ; DiagInit(NULL, NULL, NULL) ; if (argc == 0) usage_exit(); parse_commandline(argc, argv); check_options(); if (checkoptsonly) return(0); dump_options(stdout); // Load in surface registrations for(n=0; n<nsurfs;n++){ printf("%d Loading %s\n",n+1,SurfRegFile[n]); base = fio_basename(SurfRegFile[n],".tri"); if(strcmp(base,"ic7")==0){ // Have to do it this way to rescale. Need to find a better more robust way. printf(" reading as ico 7, rescaling radius to 100\n"); SurfReg[n] = ReadIcoByOrder(7, 100); } else SurfReg[n] = MRISread(SurfRegFile[n]); free(base); if(SurfReg[n]==NULL) exit(1); } // Load in source data SrcVal = NULL; if(DoSynthRand) { if (SynthSeed < 0) SynthSeed = PDFtodSeed(); printf("INFO: synthesizing, seed = %d\n",SynthSeed); srand48(SynthSeed); MRIrandn(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes,0, 1, SrcVal); } else if(DoSynthOnes != 0) { printf("INFO: filling input with all 1s\n"); MRIconst(SrcVal->width, SrcVal->height, SrcVal->depth, SrcVal->nframes, 1, SrcVal); } else if(AnnotFile) { printf("Loading annotation %s\n",AnnotFile); err = MRISreadAnnotation(SurfReg[0], AnnotFile); if(err) exit(1); SrcVal = MRISannotIndex2Seg(SurfReg[0]); ctab = CTABdeepCopy(SurfReg[0]->ct); } else if(LabelFile) { LABEL *srclabel; printf("Loading label %s\n",LabelFile); srclabel = LabelRead(NULL, LabelFile); if(srclabel == NULL) exit(1); SrcVal = MRISlabel2Mask(SurfReg[0],srclabel,NULL); printf(" %d points in input label\n",srclabel->n_points); LabelFree(&srclabel); } else { printf("Loading %s\n",SrcValFile); SrcVal = MRIread(SrcValFile); if(SrcVal==NULL) exit(1); } // Apply registration to source TrgVal = MRISapplyReg(SrcVal, SurfReg, nsurfs, ReverseMapFlag, DoJac, UseHash); if(TrgVal == NULL) exit(1); // Save output if(AnnotFile){ printf("Converting to target annot\n"); err = MRISseg2annot(SurfReg[nsurfs-1],TrgVal,ctab); if(err) exit(1); printf("Writing %s\n",TrgValFile); MRISwriteAnnotation(SurfReg[nsurfs-1], TrgValFile); } else if(LabelFile){ LABEL *label; label = MRISmask2Label(SurfReg[nsurfs-1], TrgVal, 0, 10e-5); printf(" %d points in output label\n",label->n_points); err = LabelWrite(label,TrgValFile); if(err){ printf("ERROR: writing label file %s\n",TrgValFile); exit(1); } LabelFree(&label); } else{ printf("Writing %s\n",TrgValFile); MRIwrite(TrgVal,TrgValFile); } printf("mris_apply_reg done\n"); return 0; }