/* Copyright (C) 2004 University of Oxford */ /* Part of FSL - FMRIB's Software Library http://www.fmrib.ox.ac.uk/fsl fsl@fmrib.ox.ac.uk Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance Imaging of the Brain), Department of Clinical Neurology, Oxford University, Oxford, UK LICENCE FMRIB Software Library, Release 5.0 (c) 2012, The University of Oxford (the "Software") The Software remains the property of the University of Oxford ("the University"). The Software is distributed "AS IS" under this Licence solely for non-commercial use in the hope that it will be useful, but in order that the University as a charitable foundation protects its assets for the benefit of its educational and research purposes, the University makes clear that no condition is made or to be implied, nor is any warranty given or to be implied, as to the accuracy of the Software, or that it will be suitable for any particular purpose or for use under any specific conditions. Furthermore, the University disclaims all responsibility for the use which is made of the Software. It further disclaims any liability for the outcomes arising from using the Software. The Licensee agrees to indemnify the University and hold the University harmless from and against any and all claims, damages and liabilities asserted by third parties (including claims for negligence) which arise directly or indirectly from the use of the Software or the sale of any products based on the Software. No part of the Software may be reproduced, modified, transmitted or transferred in any form or by any means, electronic or mechanical, without the express permission of the University. The permission of the University is not required if the said reproduction, modification, transmission or transference is done without financial return, the conditions of this Licence are imposed upon the receiver of the product, and all original and amended source code is included in any transmitted product. You may be held legally responsible for any copyright infringement that is caused or encouraged by your failure to abide by these terms and conditions. You are not permitted under this Licence to use this Software commercially. Use for which any financial return is received shall be defined as commercial use, and includes (1) integration of all or part of the source code or the Software into a product for sale or license by or on behalf of Licensee to third parties or (2) use of the Software or any derivative of it for research with the final aim of developing software products for sale or license to a third party or (3) use of the Software or any derivative of it for research with the final aim of developing non-software products for sale or license to a third party, or (4) use of the Software to provide any service to an external organisation for which payment is received. If you are interested in using the Software commercially, please contact Isis Innovation Limited ("Isis"), the technology transfer company of the University, to negotiate a licence. Contact details are: innovation@isis.ox.ac.uk quoting reference DE/9564. */ #ifndef EXPOSE_TREACHEROUS #define EXPOSE_TREACHEROUS #endif #include #include #include "miscmaths/miscmaths.h" #include "newmat.h" #include "dtifitOptions.h" #include "newimage/newimageall.h" using namespace std; using namespace NEWMAT; using namespace MISCMATHS; using namespace DTIFIT; using namespace NEWIMAGE; const float maxfloat=1e10; const float minfloat=1e-10; const float maxlogfloat=23; const float minlogfloat=-23; const int maxint=1000000000; inline float PI() { return 3.14159265358979;} inline float min(float a,float b){ return ab ? a:b;} inline Matrix Anis() { Matrix A(3,3); A << 1 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0; return A; } inline Matrix Is() { Matrix I(3,3); I << 1 << 0 << 0 << 0 << 1 << 0 << 0 << 0 << 1; return I; } inline ColumnVector Cross(const ColumnVector& A,const ColumnVector& B) { ColumnVector res(3); res << A(2)*B(3)-A(3)*B(2) << A(3)*B(1)-A(1)*B(3) << A(1)*B(2)-B(1)*A(2); return res; } inline Matrix Cross(const Matrix& A,const Matrix& B) { Matrix res(3,1); res << A(2,1)*B(3,1)-A(3,1)*B(2,1) << A(3,1)*B(1,1)-A(1,1)*B(3,1) << A(1,1)*B(2,1)-B(1,1)*A(2,1); return res; } float mod(float a, float b){ while(a>b){a=a-b;} while(a<0){a=a+b;} return a; } Matrix form_Amat(const Matrix& r,const Matrix& b) { Matrix A(r.Ncols(),7); Matrix tmpvec(3,1), tmpmat; for( int i = 1; i <= r.Ncols(); i++){ tmpvec << r(1,i) << r(2,i) << r(3,i); tmpmat = tmpvec*tmpvec.t()*b(1,i); A(i,1) = tmpmat(1,1); A(i,2) = 2*tmpmat(1,2); A(i,3) = 2*tmpmat(1,3); A(i,4) = tmpmat(2,2); A(i,5) = 2*tmpmat(2,3); A(i,6) = tmpmat(3,3); A(i,7) = 1; } return A; } Matrix form_Amat(const Matrix& r,const Matrix& b, const Matrix & cni ) { //cni are confound regressors of no interest Matrix A(r.Ncols(),7 + cni.Ncols()); Matrix A_noconf(r.Ncols(),7); Matrix tmpvec(3,1), tmpmat; for( int i = 1; i <= r.Ncols(); i++){ tmpvec << r(1,i) << r(2,i) << r(3,i); tmpmat = tmpvec*tmpvec.t()*b(1,i); //this is the b-Matrix for direction i A(i,1) = tmpmat(1,1); A(i,2) = 2*tmpmat(1,2); A(i,3) = 2*tmpmat(1,3); A(i,4) = tmpmat(2,2); A(i,5) = 2*tmpmat(2,3); A(i,6) = tmpmat(3,3); A(i,7) = 1; A_noconf(i,1) = tmpmat(1,1); A_noconf(i,2) = 2*tmpmat(1,2); A_noconf(i,3) = 2*tmpmat(1,3); A_noconf(i,4) = tmpmat(2,2); A_noconf(i,5) = 2*tmpmat(2,3); A_noconf(i,6) = tmpmat(3,3); A_noconf(i,7) = 1; for( int col=1;col<=cni.Ncols();col++){ A(i,col+7)=cni(i,col); } } Matrix tmp1=(A_noconf.t()*A_noconf).i(); Matrix tmp2=(A.t()*A).i(); cout<<"Efficiency loss due to confounds: xx xy xz yy yz zz"<0 ? S(i)*S(i):1); //Weights according to (Salvador, HBM 2005) pinvA=(((Amat.t()*W)*Amat).i()*Amat.t())*W; //WLS pseudoinverse of Amat pinvA.Release(); return pinvA; } //Performs fitting of the tensor using a precalculated pseudoinverse of the design matrix (Amat_pinv) //Depending on Amat_pinv, the function performs an OLS or WLS fiting of the DTI model. void tensorfit(DiagonalMatrix& Dd,ColumnVector& evec1,ColumnVector& evec2,ColumnVector& evec3,float& f,float& s0,float& mode,ColumnVector& Dvec, float& sse, const Matrix& Amat, const Matrix& Amat_pinv, const ColumnVector& S) { ColumnVector logS(S.Nrows()); SymmetricMatrix tens; //Basser's Diffusion Tensor; Matrix Vd; //eigenvectors DiagonalMatrix Ddsorted(3); float mDd, fsquared; for (int i=1; i<=S.Nrows(); i++) { if(S(i)>0) logS(i)=log(S(i)); else logS(i)=0; } Dvec=-Amat_pinv*logS; //Estimate the model parameters if(Dvec(7)>-maxlogfloat ) s0=exp(-Dvec(7)); else s0=S.MaximumAbsoluteValue(); for ( int i = 1; i <= S.Nrows(); i++) { if(s00.01 ? log(S(i)):log(0.01*s0); } Dvec = -Amat_pinv*logS; sse=(Amat*Dvec+logS).SumSquare(); //sse = (W*(Amat*Dvec+logS)).SumSquare(); //In case of WLS, the weighted SSE will be evaluated, otherwise W=I, so OLS SSE is computed s0=exp(-Dvec(7)); if(s0 Dd(2) ? 1:2; //finding max,mid and min eigenvalues maxind = Dd(maxind) > Dd(3) ? maxind:3; int midind; if( (Dd(1)>=Dd(2) && Dd(2)>=Dd(3)) || (Dd(1)<=Dd(2) && Dd(2)<=Dd(3)) ){midind=2;} else if( (Dd(2)>=Dd(1) && Dd(1)>=Dd(3)) || (Dd(2)<=Dd(1) && Dd(1)<=Dd(3)) ){midind=1;} else {midind=3;} int minind = Dd(1) < Dd(2) ? 1:2; //finding minimum eigenvalue minind = Dd(minind) < Dd(3) ? minind:3; Ddsorted << Dd(maxind) << Dd(midind) << Dd(minind); Dd=Ddsorted; evec1 << Vd(1,maxind) << Vd(2,maxind) << Vd(3,maxind); evec2 << Vd(1,midind) << Vd(2,midind) << Vd(3,midind); evec3 << Vd(1,minind) << Vd(2,minind) << Vd(3,minind); float e1=Dd(maxind)-mDd, e2=Dd(midind)-mDd, e3=Dd(minind)-mDd; float n = (e1 + e2 - 2*e3)*(2*e1 - e2 - e3)*(e1 - 2*e2 + e3); float d = (e1*e1 + e2*e2 + e3*e3 - e1*e2 - e2*e3 - e1*e3); d = sqrt(MAX(0, d)); d = 2*d*d*d; mode = MIN(MAX(d ? n/d : 0.0, -1),1); //Compute the FA float numer=1.5*((Dd(1)-mDd)*(Dd(1)-mDd)+(Dd(2)-mDd)*(Dd(2)-mDd)+(Dd(3)-mDd)*(Dd(3)-mDd)); float denom=(Dd(1)*Dd(1)+Dd(2)*Dd(2)+Dd(3)*Dd(3)); if(denom>0) fsquared=numer/denom; else fsquared=0; if(fsquared>0){f=sqrt(fsquared);} else{f=0;} } //Correct bvals/bvecs accounting for Gradient Nonlinearities //ColumnVector grad_nonlin has 9 entries, corresponding to the 3 components of each of the x,y and z gradient deviation void correct_bvals_bvecs(const Matrix& bvals,const Matrix& bvecs, const ColumnVector& grad_nonlin, const Matrix& Qform, const Matrix& Qform_inv, Matrix& bvals_c, Matrix& bvecs_c){ bvals_c=bvals; bvecs_c=bvecs; Matrix L(3,3); //gradient coil tensor float mag; L(1,1)=grad_nonlin(1); L(1,2)=grad_nonlin(4); L(1,3)=grad_nonlin(7); L(2,1)=grad_nonlin(2); L(2,2)=grad_nonlin(5); L(2,3)=grad_nonlin(8); L(3,1)=grad_nonlin(3); L(3,2)=grad_nonlin(6); L(3,3)=grad_nonlin(9); IdentityMatrix Id(3); for (int l=1; l<=bvals.Ncols(); l++){ if (bvals(1,l)>0){ //do not correct b0s //Rotate bvecs to scanner's coordinate system ColumnVector bvec_tmp(3); bvec_tmp=Qform*bvecs.Column(l); bvec_tmp(1)=-bvec_tmp(1); //Sign-flip X coordinate //Correct for grad-nonlin in scanner's coordinate system bvecs_c.Column(l)=(Id+L)*bvec_tmp;//bvecs.Column(l); mag=sqrt(bvecs_c(1,l)*bvecs_c(1,l)+bvecs_c(2,l)*bvecs_c(2,l)+bvecs_c(3,l)*bvecs_c(3,l)); if (mag!=0) bvecs_c.Column(l)=bvecs_c.Column(l)/mag; bvals_c(1,l)=mag*mag*bvals(1,l); bvec_tmp=bvecs_c.Column(l); //Rotate corrected bvecs back to voxel coordinate system bvec_tmp(1)=-bvec_tmp(1); //Sign-flip X coordinate bvecs_c.Column(l)=Qform_inv*bvec_tmp; } } } //Get the scale-free Qform matrix to rotate bvecs back to scanner's coordinate system //After applying this matrix, make sure to sign flip the x coordinate of the resulted bvecs! //If you apply the inverse of the matrix, sign flip the x coordinate of the input bvecs void Return_Qform(const Matrix& qform_mat, Matrix& QMat, const float xdim, const float ydim, const float zdim){ Matrix QMat_tmp; DiagonalMatrix Scale(3); QMat_tmp=qform_mat.SubMatrix(1,3,1,3); Scale(1)=xdim; Scale(2)=ydim; Scale(3)=zdim; QMat_tmp=Scale.i()*QMat_tmp; QMat=QMat_tmp; } int main(int argc, char** argv) { //parse command line dtifitOptions& opts = dtifitOptions::getInstance(); int success=opts.parse_command_line(argc,argv); if(!success) return 1; if(opts.verbose.value()){ cout<<"data file "<3) r=r.t(); for(int i=1;i<=r.Ncols();i++){ float tmpsum=sqrt(r(1,i)*r(1,i)+r(2,i)*r(2,i)+r(3,i)*r(3,i)); if(tmpsum!=0){ r(1,i)=r(1,i)/tmpsum; r(2,i)=r(2,i)/tmpsum; r(3,i)=r(3,i)/tmpsum; } } Matrix b = read_ascii_matrix(opts.bvalsfile.value()); if(b.Nrows()>1) b=b.t(); volume4D data; volume mask; if(opts.verbose.value()) cout<<"reading data"< grad, bvalmap; Matrix Qform, Qform_inv; if (opts.grad_file.set()){ read_volume4D(grad,opts.grad_file.value()); //Get the scale-free Qform matrix to rotate bvecs back to scanner's coordinate system Return_Qform(data.qform_mat(), Qform, data.xdim(), data.ydim(), data.zdim()); Qform_inv=Qform.i(); } int minx=opts.littlebit.value() ? opts.x_min.value():0; int maxx=opts.littlebit.value() ? opts.x_max.value():mask.xsize(); int miny=opts.littlebit.value() ? opts.y_min.value():0; int maxy=opts.littlebit.value() ? opts.y_max.value():mask.ysize(); int minz=opts.littlebit.value() ? opts.z_min.value():0; int maxz=opts.littlebit.value() ? opts.z_max.value():mask.zsize(); cout< l1(maxx-minx,maxy-miny,maxz-minz); volume l2(maxx-minx,maxy-miny,maxz-minz); volume l3(maxx-minx,maxy-miny,maxz-minz); volume MD(maxx-minx,maxy-miny,maxz-minz); volume FA(maxx-minx,maxy-miny,maxz-minz); volume S0(maxx-minx,maxy-miny,maxz-minz); volume MODE(maxx-minx,maxy-miny,maxz-minz); volume4D V1(maxx-minx,maxy-miny,maxz-minz,3); volume4D V2(maxx-minx,maxy-miny,maxz-minz,3); volume4D V3(maxx-minx,maxy-miny,maxz-minz,3); volume4D Delements(maxx-minx,maxy-miny,maxz-minz,6); if (opts.save_bvals.value()) bvalmap.reinitialize(maxx-minx,maxy-miny,maxz-minz,data.tsize()); volume4D cni_cope; volume sse; if(opts.verbose.value()) cout<<"copying input properties to output volumes"<0){ for(int t=0;t < data.tsize();t++){ S(t+1)=data(i,j,k,t); } if (!opts.grad_file.set()){ //Check whether Gradient-Nonlinearities are considered. If not proceed as normal if (opts.wls.value()) pinv_Amat=WLS_pinv(Amat,S); } else{ //If they are, correct the bvals and bvecs and get a new Amat for each voxel Matrix bvals_c, bvecs_c; ColumnVector gradm(9); for (int t=0; t<9; t++) gradm(t+1)=grad(i,j,k,t); correct_bvals_bvecs(b,r, gradm,Qform,Qform_inv,bvals_c,bvecs_c); if (opts.save_bvals.value()){ for (int t=0; tdyad_D(2)){ // if(dyad_D(1)>dyad_D(3)) maxeig=1; // else maxeig=3; // } // else{ // if(dyad_D(2)>dyad_D(3)) maxeig=2; // else maxeig=3; // } // dyadic_vecs(i-minx,j-miny,k-minz,0)=dyad_V(1,maxeig); // dyadic_vecs(i-minx,j-miny,k-minz,1)=dyad_V(2,maxeig); // dyadic_vecs(i-minx,j-miny,k-minz,2)=dyad_V(3,maxeig); } } } } string fafile=opts.ofile.value()+"_FA"; string s0file=opts.ofile.value()+"_S0"; string l1file=opts.ofile.value()+"_L1"; string l2file=opts.ofile.value()+"_L2"; string l3file=opts.ofile.value()+"_L3"; string v1file=opts.ofile.value()+"_V1"; string v2file=opts.ofile.value()+"_V2"; string v3file=opts.ofile.value()+"_V3"; string MDfile=opts.ofile.value()+"_MD"; string MOfile=opts.ofile.value()+"_MO"; string tensfile=opts.ofile.value()+"_tensor"; if(opts.littlebit.value()){ fafile+="littlebit"; s0file+="littlebit"; l1file+="littlebit"; l2file+="littlebit"; l3file+="littlebit"; v1file+="littlebit"; v2file+="littlebit"; v3file+="littlebit"; MDfile+="littlebit"; MOfile+="littlebit"; tensfile+="littlebit"; } FA.setDisplayMaximumMinimum(1,0); save_volume(FA,fafile); S0.setDisplayMaximumMinimum(S0.max(),0); save_volume(S0,s0file); MODE.setDisplayMaximumMinimum(1,-1); save_volume(MODE,MOfile); V1.setDisplayMaximumMinimum(1,-1); save_volume4D(V1,v1file); V2.setDisplayMaximumMinimum(1,-1); save_volume4D(V2,v2file); V3.setDisplayMaximumMinimum(1,-1); save_volume4D(V3,v3file); l1.setDisplayMaximumMinimum(l1.max(),0); save_volume(l1,l1file); l2.setDisplayMaximumMinimum(l1.max(),0); save_volume(l2,l2file); l3.setDisplayMaximumMinimum(l1.max(),0); save_volume(l3,l3file); MD.setDisplayMaximumMinimum(l1.max(),0); save_volume(MD,MDfile); if(opts.savetensor.value()) { Delements.setDisplayMaximumMinimum(l1.max(),0); save_volume4D(Delements,tensfile); } if(opts.save_bvals.value()) { bvalmap.setDisplayMaximumMinimum(bvalmap.max(),0); string tmpfile=opts.ofile.value()+"_bvals"; save_volume4D(bvalmap,tmpfile); } if(opts.cni.value()!=""){ string cnifile=opts.ofile.value()+"_cnicope"; if(opts.littlebit.value()){ cnifile+="littlebit"; } cni_cope.setDisplayMaximumMinimum(cni_cope.max(),0); save_volume4D(cni_cope,cnifile); } if(opts.sse.value()){ string ssefile=opts.ofile.value()+"_sse"; if(opts.littlebit.value()){ ssefile+="littlebit"; } sse.setDisplayMaximumMinimum(sse.max(),0); save_volume(sse,ssefile); } return 0; }