/* ccops.cc Tim Behrens, Saad Jbabdi, FMRIB Image Analysis Group Copyright (C) 1999-2010 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. */ #include #include #include #include "newimage/newimageall.h" #include "ccopsOptions.h" #include #include #include "csv.h" #include "miscmaths/SpMat.h" using namespace std; using namespace NEWIMAGE; using namespace NEWMAT; using namespace MISCMATHS; using namespace CCOPS; /////// RE-ORDERING ////////////////////// void spect_reord(SymmetricMatrix& A,ColumnVector& r,ColumnVector& y){ SymmetricMatrix Q=-A; DiagonalMatrix t(Q.Nrows()); t=0; DiagonalMatrix D; Matrix V; for(int i=1;i<=Q.Nrows();i++){ float rowsum1=0, rowsum2=0; for(int j=1;j<=Q.Ncols();j++){ if(i!=j) rowsum1+=Q(i,j); rowsum2+=A(i,j); } Q(i,i)=-rowsum1; t(i)=1/sqrt(rowsum2); } Q << t*Q*t; EigenValues(Q,D,V); vector > myvec; vector > myvec2; for(int i=1;i<=D.Nrows();i++){ pair mypair; mypair.first=D(i); mypair.second=i; myvec.push_back(mypair); } sort(myvec.begin(),myvec.end()); int ind=myvec[1].second; // index for second eigenval ColumnVector v2scale(V.Nrows()); for(int i=1;i<=V.Nrows();i++){ v2scale(i)=V(i,ind); //second eigvec } v2scale=t*v2scale; //scale it for(int i=1;i<=D.Nrows();i++){ pair mypair; mypair.first=v2scale(i); mypair.second=i; myvec2.push_back(mypair); } //myvec2 contains scaled second eigenvector and index for sorting. sort(myvec2.begin(),myvec2.end()); r.ReSize(D.Nrows()); y.ReSize(D.Nrows()); for(int i=1;i<=D.Nrows();i++){ y(i)=myvec2[i-1].first; r(i)=myvec2[i-1].second; } } bool compare(const pair &r1,const pair &r2){ return (r1.first >& r){ for(unsigned int i=1;i<=r.size();i++){ pair p(rand()/float(RAND_MAX),i); r[i-1]=p; } sort(r.begin(),r.end(),compare); } void do_kmeans(const Matrix& data,ColumnVector& y,const int k){ int numiter=50; // hard-coded number of iterations in kmeans if(data.Nrows() != (int)y.Nrows()){ y.ReSize(data.Nrows()); } int n = data.Nrows(); int d = data.Ncols(); Matrix means(d,k),newmeans(d,k); ColumnVector nmeans(k); means=0; nmeans=0; // initialise with far-away trick // start with a random class centre. then each new class centre is // as far as possible from the cog of the previous classes means.Column(1) = data.Row(round(rand()/float(RAND_MAX)*float(n-1))+1).t(); ColumnVector cog(d); for(int cl=2;cl<=k;cl++){ cog = sum(means.SubMatrix(1,d,1,cl-1),2); int maxi=1;float dist=0,maxdist=0; for(int i=1;i<=n;i++){ float cdist=0,mindist=-1;int minc=1; for(int prevcl=cl-1;prevcl>=1;prevcl--){ cdist = (means.Column(prevcl)-data.Row(i).t()).SumSquare(); if(mindist==-1 || cdist=maxdist){maxdist=dist;maxi=i;} } means.Column(cl)=data.Row(maxi).t(); } // iterate for(int iter=0;iter > myvec2; for(int i=1;i<=A.Nrows();i++){ pair mypair; mypair.first=(int)y(i); mypair.second=i; myvec2.push_back(mypair); } sort(myvec2.begin(),myvec2.end()); r.ReSize(A.Nrows()); y.ReSize(A.Nrows()); for(int i=1;i<=A.Nrows();i++){ y(i)=myvec2[i-1].first; r(i)=myvec2[i-1].second; } } void do_fuzzy(const Matrix& data,Matrix& u,const int k){ int numiter = 50; // hard-coded #iterations float fuzziness = 2; // hard-coded fuzziness factor int n = data.Nrows(); int d = data.Ncols(); Matrix means(d,k),newmeans(d,k); ColumnVector nmeans(k); means=0; nmeans=0; // initialise with far-away trick // start with a random class centre. then each new class centre is // as far as possible from the cog of the previous classes means.Column(1) = data.Row(round(rand()/float(RAND_MAX)*float(n-1))+1).t(); ColumnVector cog(d); for(int cl=2;cl<=k;cl++){ cog = sum(means.SubMatrix(1,d,1,cl-1),2); int maxi=1;float dist=0,maxdist=0; for(int i=1;i<=n;i++){ float cdist=0,mindist=-1;int minc=1; for(int prevcl=cl-1;prevcl>=1;prevcl--){ cdist = (means.Column(prevcl)-data.Row(i).t()).SumSquare(); if(mindist==-1 || cdist=maxdist){maxdist=dist;maxi=i;} } means.Column(cl)=data.Row(maxi).t(); } // iterate for(int iter=0;iter > myvec2; for(int i=1;i<=A.Nrows();i++){ pair mypair; mypair.first=(int)y(i); mypair.second=i; myvec2.push_back(mypair); } sort(myvec2.begin(),myvec2.end()); r.ReSize(A.Nrows()); y.ReSize(A.Nrows()); for(int i=1;i<=A.Nrows();i++){ y(i)=myvec2[i-1].first; r(i)=myvec2[i-1].second; } } ///////////// PRE-PROCESS MATRIX ///////////// void rem_zrowcol(const Matrix& myOM3Col,vector& excl_cols, const Matrix& coordmat,const Matrix& tractcoordmat, const bool coordbool,const bool tractcoordbool, Matrix& newOM3Col,Matrix& newcoordmat, Matrix& newtractcoordmat) { // first pass to determine zero rows/columns to keep int nrows = (int)myOM3Col(myOM3Col.Nrows(),1); int ncols = (int)myOM3Col(myOM3Col.Nrows(),2); vector keep_cols(ncols,0),keep_rows(nrows,0); for(int i=1;i lu_r(nrows,0); for(unsigned int i=0;i lu_c(ncols,0); for(unsigned int i=0;i _r,_c,_old_r,_old_c; vector _v; int r,c; double v; for(int i=1;i is,rows; for(int i=1;i=2){ if(M(i,2)>M(i-1,2)){//we are still in the same column for(unsigned int ii=0;ii=0){ for(int i=1;i excl_cols; if(opts.excl_mask.value()!=""){ volume lookup_tract; volume excl; string exname=opts.excl_mask.value(); read_volume(lookup_tract,opts.ptxdir.value()+"/lookup_tractspace_"+ip); make_basename(exname); read_volume(excl,exname); if(!samesize(excl,lookup_tract)){ cerr<<"Whoops - your exlusion mask does not appear to be " <<"in the same space as your original low resolution mask - sorry"< outCCvol (newnrows,newnrows,1); Matrix outcoords (newcoordmat.Nrows(),coordmat.Ncols()); if(opts.verbose.value()) cout<<"Computing correlation"< opaths; read_volume(opaths,opts.ptxdir.value()+"/lookup_tractspace_"+ip); opaths = 0.0; // firstly, determine sum of matrix2 over different classes Matrix sumOverSeeds((int)y1.Maximum(),newtractcoordmat.Nrows()); sumOverSeeds = 0.0; for(int i=1;i refvol; read_volume(refvol,opts.ptxdir.value()+"/fdt_paths"); CSV mask(refvol); mask.load_rois(opts.mask.value()); mask.reset_values(); string type;int roiind,roiloc; for(int i=1;i<=outcoords.Nrows();i++){ roiind=outcoords(i,4); roiloc=outcoords(i,5); if(mask.isVol(roiind)) type="volume"; else type="surface"; mask.set_value(type,roiind,roiloc,(int)y1(i+1)); } if(mask.Nrois()>1){ for(int i=0;i umask; umask.reinitialize(mask.xsize(),mask.ysize(),mask.zsize()); //OUT(U); for(int cl=1;cl<=opts.nclusters.value();cl++){ umask=0; for(int i=0;i outvol(newOMmat.Nrows(),newOMmat.Ncols(),1); volume outtractcoords(newtractcoordmat.Nrows(),3,1); if(opts.verbose.value()) cout<<"Permuting Matrix"<