22 TFile* Network_GFile = 0;
24 static Int_t c_DarkBackground = TColor::GetColor(
"#6e7a85" );
27 Bool_t MovieMode = kFALSE;
29 void TMVA::draw_network(TString dataset, TFile* f, TDirectory* d,
const TString& hName,
30 Bool_t movieMode ,
const TString& epoch )
32 Bool_t __PRINT_LOGO__ = kTRUE;
35 MovieMode = movieMode;
36 if (MovieMode) c_DarkBackground = TColor::GetColor(
"#707F7F" );
39 TStyle* TMVAStyle = gROOT->GetStyle(
"TMVA");
40 Int_t canvasColor = TMVAStyle->GetCanvasColor();
41 TMVAStyle->SetCanvasColor( c_DarkBackground );
43 Int_t titleFillColor = TMVAStyle->GetTitleFillColor();
44 Int_t titleTextColor = TMVAStyle->GetTitleTextColor();
45 Int_t borderSize = TMVAStyle->GetTitleBorderSize();
47 TMVAStyle->SetTitleFillColor( c_DarkBackground );
48 TMVAStyle->SetTitleTextColor( TColor::GetColor(
"#FFFFFF" ) );
49 TMVAStyle->SetTitleBorderSize( 0 );
51 static Int_t icanvas = -1;
52 Int_t ixc = 100 + (icanvas)*40;
53 Int_t iyc = 0 + (icanvas+1)*20;
54 if (MovieMode) ixc = iyc = 0;
55 TString canvasnumber = Form(
"c%i", icanvas );
56 TString canvastitle = Form(
"Neural Network Layout for: %s", d->GetName());
57 TCanvas* c =
new TCanvas( canvasnumber, canvastitle,
58 ixc, 0 + (icanvas+1)*20, 1000, 650 );
60 TIter next = d->GetListOfKeys();
66 Double_t maxWeight = 0;
68 while ((key = (TKey*)next())) {
70 TClass *cl = gROOT->GetClass(key->GetClassName());
71 if (!cl->InheritsFrom(
"TH2F"))
76 std::cout<<key->GetClassName()<<
"----"<<cl->InheritsFrom(
"TH2F")<<
"----"<<hName<<std::endl;
79 TH2F* h = (TH2F*)key->ReadObj();
81 cout <<
"Big troubles in \"draw_network\" (1)" << endl;
84 std::cout<<h->GetName()<<
"----"<<hName<<std::endl;
85 if (TString(h->GetName()).Contains( hName )){
88 Int_t n1 = h->GetNbinsX();
89 Int_t n2 = h->GetNbinsY();
90 for (Int_t i = 0; i < n1; i++) {
91 for (Int_t j = 0; j < n2; j++) {
92 Double_t weight = TMath::Abs(h->GetBinContent(i+1, j+1));
93 if (maxWeight < weight) maxWeight = weight;
99 cout <<
"Error: could not find histograms" << endl;
108 while ((key = (TKey*)next())) {
111 TClass *cl = gROOT->GetClass(key->GetClassName());
112 if (!cl->InheritsFrom(
"TH2F"))
continue;
115 TH2F* h = (TH2F*)key->ReadObj();
118 cout <<
"Big troubles in \"draw_network\" (2)" << endl;
122 if (TString(h->GetName()).Contains( hName )) {
124 draw_layer(dataset,c, h, count++, numHists+1, maxWeight);
129 draw_layer_labels(numHists+1);
133 TText* t =
new TText();
134 t->SetTextSize( 0.04 );
135 t->SetTextColor( 0 );
136 t->SetTextAlign( 31 );
137 t->DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() - 0.033,
138 Form(
"Epoch: %s", epoch.Data() ) );
142 if (__PRINT_LOGO__) TMVAGlob::plot_logo();
148 TString dirname =
"movieplots";
149 TString foutname = dirname +
"/" + hName;
150 foutname.Resize( foutname.Length()-5 );
151 foutname.ReplaceAll(
"epochmonitoring___",
"");
154 cout <<
"storing file: " << foutname << endl;
160 TString fname = dataset+
"/plots/network";
161 TMVAGlob::imgconv( c, fname );
165 TMVAStyle->SetCanvasColor ( canvasColor );
166 TMVAStyle->SetTitleFillColor ( titleFillColor );
167 TMVAStyle->SetTitleTextColor ( titleTextColor );
168 TMVAStyle->SetTitleBorderSize( borderSize );
172 void TMVA::draw_layer_labels(Int_t nLayers)
174 const Double_t LABEL_HEIGHT = 0.032;
175 const Double_t LABEL_WIDTH = 0.20;
176 Double_t effWidth = 0.8*(1.0-LABEL_WIDTH)/nLayers;
177 Double_t height = 0.8*LABEL_HEIGHT;
178 Double_t margY = LABEL_HEIGHT - height;
180 for (Int_t i = 0; i < nLayers; i++) {
181 TString label = Form(
"Layer %i", i);
182 if (i == nLayers-1) label =
"Output layer";
183 Double_t cx = i*(1.0-LABEL_WIDTH)/nLayers+1.0/(2.0*nLayers)+LABEL_WIDTH;
184 Double_t x1 = cx-0.8*effWidth/2.0;
185 Double_t x2 = cx+0.8*effWidth/2.0;
187 Double_t y2 = margY + height;
189 TPaveLabel *p =
new TPaveLabel(x1, y1, x2, y2, label+
"",
"br");
190 p->SetFillColor(gStyle->GetTitleFillColor());
191 p->SetTextColor(gStyle->GetTitleTextColor());
192 p->SetFillStyle(1001);
193 p->SetBorderSize( 0 );
198 void TMVA::draw_input_labels(TString dataset,Int_t nInputs, Double_t* cy,
199 Double_t rad, Double_t layerWidth)
201 const Double_t LABEL_HEIGHT = 0.04;
202 const Double_t LABEL_WIDTH = 0.20;
203 Double_t width = LABEL_WIDTH + (layerWidth-4*rad);
204 Double_t margX = 0.01;
205 Double_t effHeight = 0.8*LABEL_HEIGHT;
207 TString *varNames = get_var_names(dataset,nInputs);
208 if (varNames == 0) exit(1);
212 for (Int_t i = 0; i < nInputs; i++) {
213 if (i != nInputs-1) input = varNames[i];
214 else input =
"Bias node";
215 Double_t x = margX + width;
216 Double_t y = cy[i] - effHeight;
218 TText* t =
new TText();
219 t->SetTextColor(gStyle->GetTitleTextColor());
221 t->SetTextSize(LABEL_HEIGHT);
222 if (i == nInputs-1) t->SetTextColor( TColor::GetColor(
"#AFDCEC" ) );
223 t->DrawText( x, y+0.018, input +
" :");
229 TString* TMVA::get_var_names(TString dataset, Int_t nVars )
231 const TString directories[6] = {
"InputVariables_NoTransform",
232 "InputVariables_DecorrTransform",
233 "InputVariables_PCATransform",
235 "InputVariables_Norm",
236 "InputVariables_Deco"};
239 for (Int_t i=0; i<6; i++) {
240 dir = (TDirectory*)Network_GFile->GetDirectory(dataset.Data())->Get( directories[i] );
244 cout <<
"*** Big troubles in macro \"network.cxx\": could not find directory for input variables, "
245 <<
"and hence could not determine variable names --> abort" << endl;
250 TString* vars =
new TString[nVars];
254 TIter next(dir->GetListOfKeys());
256 while ((key = (TKey*)next())) {
257 if (key->GetCycle() != 1)
continue;
259 if (!TString(key->GetName()).Contains(
"__S") &&
260 !TString(key->GetName()).Contains(
"__r") &&
261 !TString(key->GetName()).Contains(
"Regression"))
263 if (TString(key->GetName()).Contains(
"target"))
267 TClass *cl = gROOT->GetClass(key->GetClassName());
268 if (!cl->InheritsFrom(
"TH1"))
continue;
269 TH1 *sig = (TH1*)key->ReadObj();
270 TString hname = sig->GetTitle();
272 vars[ivar] = hname; ivar++;
274 if (ivar > nVars-1)
break;
277 if (ivar != nVars-1) {
278 cout <<
"*** Troubles in \"network.cxx\": did not reproduce correct number of "
279 <<
"input variables: " << ivar <<
" != " << nVars << endl;
285 void TMVA::draw_activation(TCanvas* c, Double_t cx, Double_t cy,
286 Double_t radx, Double_t rady, Int_t whichActivation)
288 TImage *activation = NULL;
290 switch (whichActivation) {
292 activation = TMVA::TMVAGlob::findImage(
"sigmoid-small.png");
295 activation = TMVA::TMVAGlob::findImage(
"line-small.png");
298 cout <<
"Activation index " << whichActivation <<
" is not known." << endl;
299 cout <<
"You messed up or you need to modify network.cxx to introduce a new "
300 <<
"activation function (and image) corresponding to this index" << endl;
303 if (activation == NULL) {
304 cout <<
"Could not create an image... exit" << endl;
308 activation->SetConstRatio(kFALSE);
312 TString name = Form(
"activation%f%f", cx, cy);
313 TPad* p =
new TPad(name+
"", name+
"", cx-radx, cy-rady, cx+radx, cy+rady);
322 void TMVA::draw_layer(TString dataset,TCanvas* c, TH2F* h, Int_t iHist,
323 Int_t nLayers, Double_t maxWeight)
325 const Double_t MAX_NEURONS_NICE = 12;
326 const Double_t LABEL_HEIGHT = 0.03;
327 const Double_t LABEL_WIDTH = 0.20;
328 Double_t ratio = ((Double_t)(c->GetWindowHeight())) / c->GetWindowWidth();
329 Double_t rad, cx1, *cy1, cx2, *cy2;
332 rad = 0.04*650/c->GetWindowHeight();
334 Int_t nNeurons1 = h->GetNbinsX();
335 cx1 = iHist*(1.0-LABEL_WIDTH)/nLayers + 1.0/(2.0*nLayers) + LABEL_WIDTH;
336 cy1 =
new Double_t[nNeurons1];
338 Int_t nNeurons2 = h->GetNbinsY();
339 cx2 = (iHist+1)*(1.0-LABEL_WIDTH)/nLayers + 1.0/(2.0*nLayers) + LABEL_WIDTH;
340 cy2 =
new Double_t[nNeurons2];
342 Double_t effRad1 = rad;
343 if (nNeurons1 > MAX_NEURONS_NICE)
344 effRad1 = 0.8*(1.0-LABEL_HEIGHT)/(2.0*nNeurons1);
346 for (Int_t i = 0; i < nNeurons1; i++) {
347 cy1[nNeurons1-i-1] = i*(1.0-LABEL_HEIGHT)/nNeurons1 + 1.0/(2.0*nNeurons1) + LABEL_HEIGHT;
351 TEllipse *ellipse =
new TEllipse( cx1, cy1[nNeurons1-i-1],
352 effRad1*ratio, effRad1, 0, 360, 0 );
353 ellipse->SetFillColor(TColor::GetColor(
"#fffffd" ));
354 ellipse->SetFillStyle(1001);
357 if (i == 0) ellipse->SetLineColor(9);
359 if (nNeurons1 > MAX_NEURONS_NICE)
continue;
361 Int_t whichActivation = 0;
362 if (iHist==0 || iHist==nLayers-1 || i==0) whichActivation = 1;
363 draw_activation(c, cx1, cy1[nNeurons1-i-1],
364 rad*ratio, rad, whichActivation);
368 if (iHist == 0) draw_input_labels(dataset,nNeurons1, cy1, rad, (1.0-LABEL_WIDTH)/nLayers);
370 Double_t effRad2 = rad;
371 if (nNeurons2 > MAX_NEURONS_NICE)
372 effRad2 = 0.8*(1.0-LABEL_HEIGHT)/(2.0*nNeurons2);
374 for (Int_t i = 0; i < nNeurons2; i++) {
375 cy2[nNeurons2-i-1] = i*(1.0-LABEL_HEIGHT)/nNeurons2 + 1.0/(2.0*nNeurons2) + LABEL_HEIGHT;
378 new TEllipse(cx2, cy2[nNeurons2-i-1], effRad2*ratio, effRad2, 0, 360, 0);
379 ellipse->SetFillColor(TColor::GetColor(
"#fffffd" ));
380 ellipse->SetFillStyle(1001);
383 if (i == 0 && nNeurons2 > 1) ellipse->SetLineColor(9);
385 if (nNeurons2 > MAX_NEURONS_NICE)
continue;
387 Int_t whichActivation = 0;
388 if (iHist+1==0 || iHist+1==nLayers-1 || i==0) whichActivation = 1;
389 draw_activation(c, cx2, cy2[nNeurons2-i-1], rad*ratio, rad, whichActivation);
392 for (Int_t i = 0; i < nNeurons1; i++) {
393 for (Int_t j = 0; j < nNeurons2; j++) {
394 draw_synapse(cx1, cy1[i], cx2, cy2[j], effRad1*ratio, effRad2*ratio,
395 h->GetBinContent(i+1, j+1)/maxWeight);
403 void TMVA::draw_synapse(Double_t cx1, Double_t cy1, Double_t cx2, Double_t cy2,
404 Double_t rad1, Double_t rad2, Double_t weightNormed)
406 const Double_t TIP_SIZE = 0.01;
407 const Double_t MAX_WEIGHT = 8;
408 const Double_t MAX_COLOR = 100;
409 const Double_t MIN_COLOR = 60;
411 if (weightNormed == 0)
return;
415 TArrow *arrow =
new TArrow(cx1+rad1, cy1, cx2-rad2, cy2, TIP_SIZE,
">");
416 arrow->SetFillColor(1);
417 arrow->SetFillStyle(1001);
418 arrow->SetLineWidth((Int_t)(TMath::Abs(weightNormed)*MAX_WEIGHT+0.5));
419 arrow->SetLineColor((Int_t)((weightNormed+1.0)/2.0*(MAX_COLOR-MIN_COLOR)+MIN_COLOR+0.5));
425 void TMVA::network(TString dataset, TString fin , Bool_t useTMVAStyle )
428 TMVAGlob::Initialize( useTMVAStyle );
431 TFile* file = TMVAGlob::OpenFile( fin );
432 TIter next(file->GetDirectory(dataset.Data())->GetListOfKeys());
434 while( (key = (TKey*)next()) ) {
435 if (!TString(key->GetName()).BeginsWith(
"Method_MLP"))
continue;
436 if( ! gROOT->GetClass(key->GetClassName())->InheritsFrom(
"TDirectory") )
continue;
438 cout <<
"--- Found directory: " << ((TDirectory*)key->ReadObj())->GetName() << endl;
440 TDirectory* mDir = (TDirectory*)key->ReadObj();
442 TIter keyIt(mDir->GetListOfKeys());
444 while((titkey = (TKey*)keyIt())) {
445 if( ! gROOT->GetClass(titkey->GetClassName())->InheritsFrom(
"TDirectory") )
continue;
447 TDirectory* dir = (TDirectory *)titkey->ReadObj();
450 UInt_t ni = TMVAGlob::GetListOfTitles( dir, titles );
452 cout <<
"No titles found for Method_MLP" << endl;
455 draw_network(dataset, file, dir );