/* Brian Patenaude, FMRIB Image Analysis Group */ /* Copyright (C) 2012 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 "MVdisc.h" #include "fslvtkio/fslvtkio.h" #include #include #include #include #include #include #include "meshclass/meshclass.h" #include "newimage/newimageall.h" #include "first_lib/first_newmat_vec.h" using namespace std; using namespace NEWIMAGE; using namespace mesh; using namespace fslvtkio; using namespace FIRST_LIB; namespace mvdisc { MVdisc::MVdisc(){ } MVdisc::~MVdisc(){ } void MVdisc::quickSVD(const Matrix & data, DiagonalMatrix &D,Matrix &U, Matrix &V ) const { //this is appropriate for the case where number of subjects is much less than the number of dimnesions SVD(data.t()*data, D,U,V); for (int i=0; i & nk) const { //return number of subjects per class unsigned int K=1; //count the number of classes for (int i=0;i< target.Nrows();i++) if (target.element(i)>=K) K++; unsigned int N=Data.Ncols(); //store total numbver of subjects if (N!=static_cast(target.Nrows())) cerr<<"Number of Subjects in Design does not math that in the data."< & nk, ColumnVector & targSorted) const { //return number of subjects per class short K=static_cast(nk.size()); //count the number of classes int N=Data.Ncols(); //store total numbver of subjects int p=Data.Nrows(); //number of dimensions //check for consistency if (N!=target.Nrows()) throw mvdiscException("Number of Subjects in Design does not match that in the data."); //declare matrix of means targSorted.ReSize(target.Nrows()); Matrix DataSorted(p,N);//sorted and demeaned data //sort data by class and demean int sortCount=0; for (int cl=0;cl & nK, Matrix & U, vector & vD) const { //K is number of classes unsigned int K=nK.size(); int p=DeMean.Nrows(); int N=DeMean.Ncols(); //store total numbver of subjects //check for consistency //if (N!=target.Nrows()) // throw mvdiscException("Number of Subjects in Design does not math that in the data."); DiagonalMatrix D; //Check if number of subjects is greater than number of dimensions if (N > p) { //dealing with a full-rank matrix Matrix COV(p,p); COV=0; int cumsum=0; for (unsigned int i=0 ; i & nK, vector< Matrix > & vU, vector< DiagonalMatrix > & vD) const { //K is number of classes vD.clear(); vU.clear(); int N=DeMean.Ncols(); //store total numbver of subjects // if (N!=target.Nrows()) // cerr<<"Number of Subjects in Design does not math that in the data."< nK.at(i)){ //dealing with a full-rank matrix // cout<<"well cond"< vector MVdisc::threshInvert(const vector & D, const T & p) const { vector Di; T ssq=0; for (typename vector::const_iterator i=D.begin(); i!=D.end() ;i++) { ssq+=*i; Di.push_back(0); } T thresh=sqrt(ssq*p);//fraction of total variance typename vector::iterator j=Di.begin(); for (typename vector::const_iterator i=D.begin();i!=D.end();i++,j++) if ( (*i) > thresh ) (*j) = 1/(*i); return Di; } //returns a vector of classifications vector MVdisc::applyLDA(const vector & Data, const float & eigThresh) const { if ( (LDAmu.Ncols()==0) || (LDAmu.Nrows()==0) || (LDAcov_Vecs.Nrows()==0) || (LDAcov_Vecs.Ncols()==0) || (LDAcov_Eigs.size()==0) || (LDAnsubs.size()==0) ) throw mvdiscException("The LDA parameters have not been set or are ill-defined"); vector v_classified; unsigned int Dclass=0; float decisionOld=0; //perform the discrimiant DiagonalMatrix Di; Di=first_newmat_vector::vectorToDiagonalMatrix( threshInvert(LDAcov_Eigs,eigThresh) ); //invert with threhsold for (vector::const_iterator i=Data.begin();i!=Data.end();i++) { for (unsigned int cl=0;cl(LDAnsubs.at(cl))/LDAnsubs.size()); float decision=-0.5*(centDist) + descPi; if (cl==0) { Dclass=0; decisionOld=decision; }else if (decision>decisionOld) { Dclass=cl; decisionOld=decision; } } v_classified.push_back(Dclass); } return v_classified; } //returns a vector of classifications short MVdisc::applyLDA(ColumnVector & Data, const float & eigThresh) const { if ( (LDAmu.Ncols()==0) || (LDAmu.Nrows()==0) || (LDAcov_Vecs.Nrows()==0) || (LDAcov_Vecs.Ncols()==0) || (LDAcov_Eigs.size()==0) || (LDAnsubs.size()==0) ) throw mvdiscException("The LDA parameters have not been set or are ill-defined"); short Dclass=0; float decisionOld=0; //perform the discrimiant DiagonalMatrix Di; Di=first_newmat_vector::vectorToDiagonalMatrix( threshInvert(LDAcov_Eigs,eigThresh) ); //invert with threhsold for (short cl=0;cl(LDAnsubs.size());cl++)//calculate for each class { float centDist=(Di * LDAcov_Vecs.t() * (LDAmu.Column(cl+1) - Data)).SumSquare(); float descPi=log(static_cast(LDAnsubs.at(cl))/LDAnsubs.size()); float decision=-0.5*(centDist) + descPi; if (cl==0) { Dclass=0; decisionOld=decision; }else if (decision>decisionOld) { Dclass=cl; decisionOld=decision; } } return Dclass; } void MVdisc::estimateLDAParams(const Matrix & Data, const ColumnVector & target) { //asisgns first and second required LDA parameters LDAmu=calculateClassMeans(Data, target, LDAnsubs); //assigend demenad matrix (with sorting) and sorted target //this is currently limited to 2 classes??? Matrix demeaned; //demeaned data ColumnVector targSorted; demeaned=sortAndDemeanByClass(Data, target, LDAmu, LDAnsubs, targSorted); //estiamte covariance matrix and setting thrid LDA parameters (uses SVD if number of subjects is less than number of dimensions) estimateCommonCov(demeaned, LDAnsubs, LDAcov_Vecs, LDAcov_Eigs); demeaned.Release(); } void MVdisc::estimateAndAppendLDAParams(const Matrix & Data, const ColumnVector & target) { cout<<"mean hmm2 "< LDAcov_Eigs_temp; cout<<"estimate cov"<<" "<::iterator i= LDAcov_Eigs_temp.begin();i!= LDAcov_Eigs_temp.end();i++) LDAcov_Eigs.push_back(*i); LDAmu=LDAmu & LDAmu_temp; demeaned.Release(); } void MVdisc::saveLDAParams(const string & outname) const { cout<<"saving stuff "<setDataType(static_cast(0)); //store mean cout<<"points "<setPoints(LDAmu.Column(1)); discOut->addFieldData(first_newmat_vector::wrapMatrix(LDAmu.Column(2)),"mean2","float"); discOut->addFieldData(LDAcov_Vecs,"Covariance_EigVecs","float"); discOut->addFieldData(LDAcov_Eigs,"Covariance_EigVals","float"); discOut->addFieldData(LDAnsubs,"number_of_subjects","float"); discOut->save(outname); cout<<"done saving stuff"<setDataType(static_cast(0)); //store mean cout<<"points "<setPoints(LDAmu.Column(1)); discOut->setPolygons(polygons); discOut->addFieldData(first_newmat_vector::wrapMatrix(LDAmu.Column(2)),"mean2","float"); discOut->addFieldData(LDAcov_Vecs,"Covariance_EigVecs","float"); discOut->addFieldData(LDAcov_Eigs,"Covariance_EigVals","float"); discOut->addFieldData(LDAnsubs,"number_of_subjects","float"); discOut->save(outname); cout<<"done saving stuff"<