Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IPythonInteractive doc and test
  • Loading branch information
qati committed Sep 19, 2016
commit c7ce819dc5682af3b385555bbebc8d58cc96c1c4
98 changes: 98 additions & 0 deletions test/stressTMVA.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2827,6 +2827,102 @@ bool MethodUnitTestWithComplexData::create_data(const char* filename, int nmax)
dataFile->Close();
return true;
}



//Author: Attila Bagoly <[email protected]>

#ifndef UTIPythonInteractive_H
#define UTIPythonInteractive_H

#include <vector>
#include "TMVA/MethodBase.h"

/**
Unit test for TMVA::IPythonInteractive located in TMVA/MethodBase.h
*/
class utIPythonInteractive : public UnitTesting::UnitTest{
public:
utIPythonInteractive();
void run();
private:
void testInit();
void testMethods();

TMVA::IPythonInteractive * ipyi;
std::vector<TString> titles;
std::vector<Double_t> xvec;
std::vector<Double_t> y1vec, y2vec;
int N;
};

#endif


#include <random>
#include "TGraph.h"
#include "TMultiGraph.h"

///////////////////////////////////////////////////////////////////////////////////////////////////
///// Standard constructor
utIPythonInteractive::utIPythonInteractive() : UnitTesting::UnitTest("IPythonInteractive", __FILE__)
{
N = 1000;
titles.push_back("Training Error");
titles.push_back("Testing Error");
std::uniform_real_distribution<double> unif(0, 1);
std::default_random_engine re;
for(int i=0;i<N;i++){
xvec.push_back(i);
y1vec.push_back(unif(re));
y2vec.push_back(unif(re));
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////
///// Run tests
void utIPythonInteractive::run()
{
testInit();
testMethods();
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///// Testing creating IPythonInteractive object and initialization.
void utIPythonInteractive::testInit()
{
ipyi = new TMVA::IPythonInteractive();
test_(ipyi->Get() != NULL);

ipyi->Init(titles);
test_(!ipyi->NotInitialized());
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///// Adding datas to IPythonInteractive and testing if it got all of them correctly.
void utIPythonInteractive::testMethods()
{
for(int i=0;i<N;i++){
ipyi->AddPoint(xvec[i], y1vec[i], y2vec[i]);
TList * graphs = ipyi->Get()->GetListOfFunctions();
TIter next(graphs);
int j=0;
TObject *obj;
while ((obj = (TObject*)next())){
TGraph * gr = dynamic_cast<TGraph*>(obj);
test_(gr->GetN()==(i+1));
Double_t x, y;
test_(gr->GetPoint(i, x, y)!=-1);
test_(x==xvec[i]);
test_(j==0 ? y1vec[i] : y2vec[i]);
j++;
}
}
}



// including file stressTMVA.cxx
// Authors: Christoph Rosemann, Eckhard von Toerne July 2010
// TMVA unit tests
Expand Down Expand Up @@ -3053,6 +3149,8 @@ int main(int argc, char **argv)
addRegressionTests(TMVA_test, full);
addDataInputTests(TMVA_test, full);
addComplexClassificationTests(TMVA_test, full);

TMVA_test.addTest(new utIPythonInteractive);

// run all
ROOT::EnableThreadSafety();
Expand Down
14 changes: 6 additions & 8 deletions tmva/tmva/inc/TMVA/MethodBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ namespace TMVA {
class MethodBoost;
class DataSetInfo;

//////////////////////////////////////////////////////////////////////////
// //
// IPythonInteractive //
// //
// Helper class for tracking errors during the training //
// in Jupyter notebook. This class is needed by JsMVA //
// //
//////////////////////////////////////////////////////////////////////////
/** \class IPythonInteractive
This class is needed by JsMVA, and it's a helper class for tracking errors during the training in Jupyter notebook.
It’s only initialized in Jupyter notebook context. In initialization we specify some title,
and a TGraph will be created for every title. We can add new data points easily to all TGraphs.
These graphs are added to a TMultiGraph, and during an interactive training we get this TMultiGraph object and plot it with JsROOT.
*/
class IPythonInteractive {
public:
IPythonInteractive();
Expand Down
14 changes: 11 additions & 3 deletions tmva/tmva/src/MethodBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ TMVA::IPythonInteractive::~IPythonInteractive()
}

////////////////////////////////////////////////////////////////////////////////
/// creating error graphs with specific names and adding them to multigraph
/// This function gets some title and it creates a TGraph for every title.
/// It also sets up the style for every TGraph. All graphs are added to a single TMultiGrah.
/// \param[in] graphTtitles vector of titles
void TMVA::IPythonInteractive::Init(std::vector<TString>& graphTitles)
{
if (fNumGraphs!=0){
Expand All @@ -181,8 +183,12 @@ void TMVA::IPythonInteractive::Init(std::vector<TString>& graphTitles)
}
return;
}

////////////////////////////////////////////////////////////////////////////////
/// inserting points to graphs
/// This function is used only in 2 TGraph case, and it will add new data points to graphs.
/// \param[in] x the x coordinate
/// \param[in] y1 the y coordinate for the first TGraph
/// \param[in] y2 the y coordinate for the second TGraph
void TMVA::IPythonInteractive::AddPoint(Double_t x, Double_t y1, Double_t y2)
{
fGraphs[0]->Set(fIndex+1);
Expand All @@ -194,7 +200,9 @@ void TMVA::IPythonInteractive::AddPoint(Double_t x, Double_t y1, Double_t y2)
}

////////////////////////////////////////////////////////////////////////////////
/// inserting points to graphs
/// This function can add data points to as many TGraps as we have.
/// \param[in] dat vector of data points. The dat[0] contains the x coordinate,
/// dat[1] contains the y coordinate for first TGraph, dat[2] for second, ...
void TMVA::IPythonInteractive::AddPoint(std::vector<Double_t>& dat)
{
for(Int_t i=0; i<fNumGraphs;i++){
Expand Down