68 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
69 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
70 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
92 if (!IsEnabled() || IsCreated())
return kTRUE;
94 Log() << kINFO <<
"Preparing the Principle Component (PCA) transformation..." <<
Endl;
96 UInt_t inputSize = fGet.size();
98 SetNVariables(inputSize);
101 if (inputSize <= 1) {
102 Log() << kFATAL <<
"Cannot perform PCA transformation for " << inputSize <<
" variable only" <<
Endl;
106 if (inputSize > 200) {
107 Log() << kINFO <<
"----------------------------------------------------------------------------"
110 <<
": More than 200 variables, will not calculate PCA!" <<
Endl;
111 Log() << kINFO <<
"----------------------------------------------------------------------------"
116 CalculatePrincipalComponents( events );
128 if (!IsCreated())
return 0;
139 if (cls < 0 || cls >= (
int) fMeanValues.size()) cls = fMeanValues.size()-1;
144 if (fTransformedEvent==0 ) {
145 fTransformedEvent =
new Event();
148 std::vector<Float_t>
input;
149 std::vector<Char_t>
mask;
150 std::vector<Float_t> principalComponents;
154 if( hasMaskedEntries ){
157 if( numMasked>0 && numOK>0 ){
158 Log() << kFATAL <<
"You mixed variables and targets in the decorrelation transformation. This is not possible." <<
Endl;
160 SetOutput( fTransformedEvent,
input,
mask, ev );
161 return fTransformedEvent;
164 X2P( principalComponents,
input, cls );
165 SetOutput( fTransformedEvent, principalComponents,
mask, ev );
167 return fTransformedEvent;
177 if (!IsCreated())
return 0;
179 const UInt_t nCls = GetNClasses();
185 if (cls < 0 ||
UInt_t(cls) > nCls) cls = (fMeanValues.size()==1?0:2);
188 if (fBackTransformedEvent==0 ) fBackTransformedEvent =
new Event();
190 std::vector<Float_t> principalComponents;
191 std::vector<Char_t>
mask;
192 std::vector<Float_t> output;
194 GetInput( ev, principalComponents,
mask,
kTRUE );
195 P2X( output, principalComponents, cls );
196 SetOutput( fBackTransformedEvent, output,
mask, ev,
kTRUE );
198 return fBackTransformedEvent;
207 UInt_t nvars = 0, ntgts = 0, nspcts = 0;
208 CountVariableTypes( nvars, ntgts, nspcts );
209 if( nvars>0 && ntgts>0 )
210 Log() << kFATAL <<
"Variables and targets cannot be mixed in PCA transformation." <<
Endl;
212 const Int_t inputSize = fGet.size();
215 const UInt_t nCls = GetNClasses();
216 const UInt_t maxPCA = (nCls<=1) ? nCls : nCls+1;
219 std::vector<TPrincipal*> pca(maxPCA);
225 Long64_t ievt, entries = events.size();
228 std::vector<Float_t>
input;
229 std::vector<Char_t>
mask;
230 for (ievt=0; ievt<entries; ievt++) {
231 const Event* ev = events[ievt];
235 if (hasMaskedEntries){
236 Log() << kWARNING <<
"Print event which triggers an error" <<
Endl;
237 std::ostringstream oss;
240 Log() << kFATAL <<
"Masked entries found in event read in when calculating the principal components for the PCA transformation." <<
Endl;
244 for( std::vector<Float_t>::iterator itInp =
input.begin(), itInpEnd =
input.end(); itInp != itInpEnd; ++itInp )
251 pca.at(cls)->AddRow( dvec );
252 if (nCls > 1) pca.at(maxPCA-1)->AddRow( dvec );
256 for (
UInt_t i=0; i<fMeanValues.size(); i++)
if (fMeanValues[i] != 0)
delete fMeanValues[i];
257 for (
UInt_t i=0; i<fEigenVectors.size(); i++)
if (fEigenVectors[i] != 0)
delete fEigenVectors[i];
258 fMeanValues.resize(maxPCA,0);
259 fEigenVectors.resize(maxPCA,0);
261 for (
UInt_t i=0; i<maxPCA; i++ ) {
262 pca.at(i)->MakePrincipals();
265 fMeanValues[i] =
new TVectorD( *(pca.at(i)->GetMeanValues()) );
266 fEigenVectors[i] =
new TMatrixD( *(pca.at(i)->GetEigenVectors()) );
269 for (
UInt_t i=0; i<maxPCA; i++)
delete pca.at(i);
281 const Int_t nInput =
x.size();
284 for (
Int_t i = 0; i < nInput; i++) {
286 for (
Int_t j = 0; j < nInput; j++)
287 pv += (((
Double_t)
x.at(j)) - (*fMeanValues.at(cls))(j)) * (*fEigenVectors.at(cls))(j,i);
300 const Int_t nInput = pc.size();
303 for (
Int_t i = 0; i < nInput; i++) {
305 for (
Int_t j = 0; j < nInput; j++)
306 xv += (((
Double_t)pc.at(j)) * (*fEigenVectors.at(cls))(i,j) ) + (*fMeanValues.at(cls))(j);
316 for (
Int_t sbType=0; sbType<2; sbType++) {
317 o <<
"# PCA mean values " << std::endl;
318 const TVectorD* means = fMeanValues[sbType];
319 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << means->
GetNrows() << std::endl;
321 o << std::setprecision(12) << std::setw(20) << (*means)[row];
325 o <<
"##" << std::endl;
328 for (
Int_t sbType=0; sbType<2; sbType++) {
329 o <<
"# PCA eigenvectors " << std::endl;
330 const TMatrixD* mat = fEigenVectors[sbType];
331 o << (sbType==0 ?
"Signal" :
"Background") <<
" " << mat->
GetNrows() <<
" x " << mat->
GetNcols() << std::endl;
334 o << std::setprecision(12) << std::setw(20) << (*mat)[row][col] <<
" ";
339 o <<
"##" << std::endl;
352 for (
UInt_t sbType=0; sbType<fMeanValues.size(); sbType++) {
354 const TVectorD* means = fMeanValues[sbType];
355 gTools().
AddAttr( meanxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined")) );
365 for (
UInt_t sbType=0; sbType<fEigenVectors.size(); sbType++) {
367 const TMatrixD* mat = fEigenVectors[sbType];
368 gTools().
AddAttr( evxml,
"Class", (sbType==0 ?
"Signal" :(sbType==1 ?
"Background":
"Combined") ) );
392 void* inpnode = NULL;
408 if (nodeName ==
"Statistics") {
415 if (fMeanValues.size()<=clsIdx) fMeanValues.resize(clsIdx+1,0);
416 if (fMeanValues[clsIdx]==0) fMeanValues[clsIdx] =
new TVectorD( nrows );
417 fMeanValues[clsIdx]->ResizeTo( nrows );
420 std::stringstream s(
gTools().GetContent(ch));
421 for (
Int_t row = 0; row<nrows; row++) s >> (*fMeanValues[clsIdx])(row);
423 else if ( nodeName ==
"Eigenvectors" ) {
430 if (fEigenVectors.size()<=clsIdx) fEigenVectors.resize(clsIdx+1,0);
431 if (fEigenVectors[clsIdx]==0) fEigenVectors[clsIdx] =
new TMatrixD( nrows, ncols );
432 fEigenVectors[clsIdx]->ResizeTo( nrows, ncols );
435 std::stringstream s(
gTools().GetContent(ch));
436 for (
Int_t row = 0; row<nrows; row++)
437 for (
Int_t col = 0; col<ncols; col++)
438 s >> (*fEigenVectors[clsIdx])[row][col];
452 istr.getline(buf,512);
454 Int_t nrows(0), ncols(0);
455 UInt_t classIdx=(classname==
"signal"?0:1);
457 for (
UInt_t i=0; i<fMeanValues.size(); i++) {
458 if (fMeanValues.at(i) != 0)
delete fMeanValues.at(i);
459 if (fEigenVectors.at(i) != 0)
delete fEigenVectors.at(i);
461 fMeanValues.resize(3);
462 fEigenVectors.resize(3);
464 Log() << kINFO <<
"VariablePCATransform::ReadTransformationFromStream(): " <<
Endl;
466 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
468 while (*
p==
' ' || *
p==
'\t')
p++;
469 if (*
p==
'#' || *
p==
'\0') {
470 istr.getline(buf,512);
473 std::stringstream sstr(buf);
475 if (strvar==
"signal" || strvar==
"background") {
478 Int_t sbType = (strvar==
"signal" ? 0 : 1);
480 if (fMeanValues[sbType] == 0) fMeanValues[sbType] =
new TVectorD( nrows );
481 else fMeanValues[sbType]->ResizeTo( nrows );
484 for (
Int_t row = 0; row<nrows; row++) istr >> (*fMeanValues[sbType])(row);
488 istr.getline(buf,512);
492 istr.getline(buf,512);
493 while (!(buf[0]==
'#'&& buf[1]==
'#')) {
495 while(*
p==
' ' || *
p==
'\t')
p++;
496 if (*
p==
'#' || *
p==
'\0') {
497 istr.getline(buf,512);
500 std::stringstream sstr(buf);
502 if (strvar==
"signal" || strvar==
"background") {
505 sstr >> nrows >> dummy >> ncols;
506 Int_t sbType = (strvar==
"signal" ? 0 : 1);
508 if (fEigenVectors[sbType] == 0) fEigenVectors[sbType] =
new TMatrixD( nrows, ncols );
509 else fEigenVectors[sbType]->ResizeTo( nrows, ncols );
512 for (
Int_t row = 0; row<fEigenVectors[sbType]->GetNrows(); row++) {
513 for (
Int_t col = 0; col<fEigenVectors[sbType]->GetNcols(); col++) {
514 istr >> (*fEigenVectors[sbType])[row][col];
519 istr.getline(buf,512);
521 fMeanValues[2] =
new TVectorD( *fMeanValues[classIdx] );
522 fEigenVectors[2] =
new TMatrixD( *fEigenVectors[classIdx] );
533 UInt_t nvar = fEigenVectors[0]->GetNrows();
536 UInt_t numC = fMeanValues.size();
539 fout <<
" void X2P_"<<trCounter<<
"( const double*, double*, int ) const;" << std::endl;
540 fout <<
" double fMeanValues_"<<trCounter<<
"["<<numC<<
"]["
541 << fMeanValues[0]->GetNrows() <<
"];" << std::endl;
542 fout <<
" double fEigenVectors_"<<trCounter<<
"["<<numC<<
"]["
543 << fEigenVectors[0]->GetNrows() <<
"]["
544 << fEigenVectors[0]->GetNcols() <<
"];" << std::endl;
550 if (fMeanValues[0]->GetNrows() != fMeanValues[1]->GetNrows() ||
551 fEigenVectors[0]->GetNrows() != fEigenVectors[1]->GetNrows() ||
552 fEigenVectors[0]->GetNcols() != fEigenVectors[1]->GetNcols()) {
553 Log() << kFATAL <<
"<MakeFunction> Mismatch in vector/matrix dimensions" <<
Endl;
560 fout <<
"//_______________________________________________________________________" << std::endl;
561 fout <<
"inline void " << fcncName <<
"::X2P_"<<trCounter<<
"( const double* x, double* p, int index ) const" << std::endl;
562 fout <<
"{" << std::endl;
563 fout <<
" // Calculate the principal components from the original data vector" << std::endl;
564 fout <<
" // x, and return it in p (function extracted from TPrincipal::X2P)" << std::endl;
565 fout <<
" // It's the users responsibility to make sure that both x and p are" << std::endl;
566 fout <<
" // of the right size (i.e., memory must be allocated for p)." << std::endl;
567 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
569 fout <<
" for (int i = 0; i < nVar; i++) {" << std::endl;
570 fout <<
" p[i] = 0;" << std::endl;
571 fout <<
" for (int j = 0; j < nVar; j++) p[i] += (x[j] - fMeanValues_"<<trCounter<<
"[index][j]) * fEigenVectors_"<<trCounter<<
"[index][j][i];" << std::endl;
572 fout <<
" }" << std::endl;
573 fout <<
"}" << std::endl;
575 fout <<
"//_______________________________________________________________________" << std::endl;
576 fout <<
"inline void " << fcncName <<
"::InitTransform_"<<trCounter<<
"()" << std::endl;
577 fout <<
"{" << std::endl;
578 fout <<
" // PCA transformation, initialisation" << std::endl;
581 fout <<
" // initialise vector of mean values" << std::endl;
582 std::streamsize dp = fout.precision();
584 for (
int i=0; i<fMeanValues[
index]->GetNrows(); i++) {
585 fout <<
" fMeanValues_"<<trCounter<<
"["<<
index<<
"]["<<i<<
"] = " << std::setprecision(12)
586 << (*fMeanValues[
index])(i) <<
";" << std::endl;
592 fout <<
" // initialise matrix of eigenvectors" << std::endl;
594 for (
int i=0; i<fEigenVectors[
index]->GetNrows(); i++) {
595 for (
int j=0; j<fEigenVectors[
index]->GetNcols(); j++) {
596 fout <<
" fEigenVectors_"<<trCounter<<
"["<<
index<<
"]["<<i<<
"]["<<j<<
"] = " << std::setprecision(12)
597 << (*fEigenVectors[
index])(i,j) <<
";" << std::endl;
601 fout << std::setprecision(dp);
602 fout <<
"}" << std::endl;
604 fout <<
"//_______________________________________________________________________" << std::endl;
605 fout <<
"inline void " << fcncName <<
"::Transform_"<<trCounter<<
"( std::vector<double>& iv, int cls ) const" << std::endl;
606 fout <<
"{" << std::endl;
607 fout <<
" // PCA transformation" << std::endl;
608 fout <<
" const int nVar = " << nvar <<
";" << std::endl;
609 fout <<
" double *dv = new double[nVar];" << std::endl;
610 fout <<
" double *rv = new double[nVar];" << std::endl;
611 fout <<
" if (cls < 0 || cls > "<<GetNClasses()<<
") {"<< std::endl;
612 fout <<
" if ("<<GetNClasses()<<
" > 1 ) cls = "<<GetNClasses()<<
";"<< std::endl;
613 fout <<
" else cls = "<<(numC==1?0:2)<<
";"<< std::endl;
614 fout <<
" }"<< std::endl;
618 fout <<
" for (int ivar=0; ivar<nVar; ivar++) dv[ivar] = iv[indicesGet.at(ivar)];" << std::endl;
621 fout <<
" // Perform PCA and put it into PCAed events tree" << std::endl;
622 fout <<
" this->X2P_"<<trCounter<<
"( dv, rv, cls );" << std::endl;
623 fout <<
" for (int ivar=0; ivar<nVar; ivar++) iv[indicesPut.at(ivar)] = rv[ivar];" << std::endl;
626 fout <<
" delete [] dv;" << std::endl;
627 fout <<
" delete [] rv;" << std::endl;
628 fout <<
"}" << std::endl;
char Char_t
Character 1 byte (char)
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
float Float_t
Float 4 bytes (float)
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t mask
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
TMatrixT< Double_t > TMatrixD
TVectorT< Double_t > TVectorD
Class that contains all the data information.
void Print(std::ostream &o) const
print method
Singleton class for Global types used by TMVA.
Principal Components Analysis (PCA)
MsgLogger & Endl(MsgLogger &ml)