<commit_before>/****************************************************************
 * This is a test class to evaluate the needs and musts to add my own
 * Classifier (e.g. NeuroBayes) to the TMVA Toolkit.
 *
 * <NAME> 2007, Oct Karlsruhe, Germany, EKP
 *
 * 
 *
 * *************************************************************/

#include <iostream>
#include <fstream>
#include <TSystem.h>
#include <TString.h>
#include <TObjString.h>
#include <TDirectory.h>
#include "TMVA/Ranking.h"
#include "TMVA/Tools.h"
#include "TMVA/Timer.h"
#ifndef ROOT_TMVA_MethodBase
#include "TMVA/MethodBase.h"
#include "TMVA/ClassifierFactory.h"
#endif
#include <RVersion.h>

#include "MethodNeuroBayes.h"

ClassImp(TMVA::MethodNeuroBayes)

int TMVA::MethodNeuroBayes::CountInstanzes = 0;

TMVA::MethodNeuroBayes::MethodNeuroBayes(DataSetInfo& theData, 
                                       const TString& theWeightFile,  
                                       TDirectory* theTargetDir)
	:TMVA::MethodBase( Types::kPlugins, theData, theWeightFile, theTargetDir ){
	fTask=0;

	InitNeuroBayes(fTask);
	Log() << kINFO << "Expert Constructor was called" << Endl;
} 

TMVA::MethodNeuroBayes::MethodNeuroBayes( const TString& jobName,
					  const TString& methodTitle, 
					  DataSetInfo& theData, 
					  const TString& theOption, 
					  TDirectory* theTargetDir )
	   : TMVA::MethodBase( jobName, Types::kPlugins, methodTitle, theData, theOption, theTargetDir )
{
	fTask = 1;
	InitNeuroBayes(fTask);
	MyID = CountInstanzes;
	//Log() << kINFO << methodTitle << " got ID " << MyID << " theTargetDir =  " << theTargetDir << Endl;

	CountInstanzes++;
	if(MyID > 0) {
		Log() << kWARNING << "This NeuroBayes instance was not created as first one."<<Endl;
		Log() << kWARNING << "Because Teacher is a singleton you won't get useful results from this Method" << Endl;
		Log() << kWARNING << "Use only one Teacher at a time until this is fixed" << Endl;
	}

	TeacherConfigured = false;
	Log() << kINFO << "Teacher Constructor was called" << Endl;
}

TMVA::MethodNeuroBayes::~MethodNeuroBayes(){
	delete Net;
}

Bool_t TMVA::MethodNeuroBayes::HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets )
{
	//NeuroBayes handles Classification
	if( type == Types::kClassification && numberClasses == 2 ) return kTRUE;
	return kFALSE;
}

//_______________________________________________________________________
void TMVA::MethodNeuroBayes::Init(){}

void TMVA::MethodNeuroBayes::InitNeuroBayes( int task ) //task = 1 means Teacher, task = 0 means Expert 
{
	TString filedir = "weights";
	NBOutputFile = filedir + "/" +  GetJobName() + "_" + GetMethodName() + ".NB_weights" ;
	
	if( task == 1 ) {
		//set an instance of Teacher
		nb = NeuroBayesTeacher::Instance();
	
		nb->SetOutputFile(NBOutputFile + ".nb");  	// expert file

  		//setup network topology
  		const int  nvar = GetNvar();    //number of input variables
  		nb->NB_DEF_NODE1(nvar+1);       // nodes in input layer 
  		nb->NB_DEF_NODE2(nvar+2);      	// nodes in hidden layer 
  		nb->NB_DEF_NODE3(1);       	// nodes in output layer
  
  		nb->NB_DEF_TASK("CLA");    	// binominal classification

  		int i= 4701;
  		int j=21; 
  		nb->NB_RANVIN(i,j,2);   	// random number seed initialisation,
						// i has to be an odd number, the third argument is a debugging flag
	}
	else if ( task == 0 ) {
	}
}


//_______________________________________________________________________
void TMVA::MethodNeuroBayes::InitEventSample( void )
{
   	if (!HasTrainingTree()) Log() << kFATAL << "<Init> Data().TrainingTree() is zero pointer" << Endl;

   	Int_t nevents = Data()->GetNTrainingEvents();
	Int_t nsignal=0;

	// event loop on Training Data
 	for (Int_t ievt=0; ievt<nevents; ievt++) {

		//ReadTrainingEvent( ievt,  Types::kTrueType );
		//const Event* origEv = Data()->GetEvent(ievt);
		Event* event = new Event( *GetTrainingEvent(ievt) );

   		int nvar = GetNvar();

		float InputArray[nvar];   
		memset(InputArray,-999,nvar*sizeof(float)); //reset<commit_msg>Don't copy and leak events anymore. Thanks to <NAME><commit_after>/****************************************************************
 * This is a test class to evaluate the needs and musts to add my own
 * Classifier (e.g. NeuroBayes) to the TMVA Toolkit.
 *
 * <NAME> 2007, Oct Karlsruhe, Germany, EKP
 *
 * 
 *
 * *************************************************************/

#include <iostream>
#include <fstream>
#include <TSystem.h>
#include <TString.h>
#include <TObjString.h>
#include <TDirectory.h>
#include "TMVA/Ranking.h"
#include "TMVA/Tools.h"
#include "TMVA/Timer.h"
#ifndef ROOT_TMVA_MethodBase
#include "TMVA/MethodBase.h"
#include "TMVA/ClassifierFactory.h"
#endif
#include <RVersion.h>

#include "MethodNeuroBayes.h"

ClassImp(TMVA::MethodNeuroBayes)

int TMVA::MethodNeuroBayes::CountInstanzes = 0;

TMVA::MethodNeuroBayes::MethodNeuroBayes(DataSetInfo& theData, 
                                       const TString& theWeightFile,  
                                       TDirectory* theTargetDir)
	:TMVA::MethodBase( Types::kPlugins, theData, theWeightFile, theTargetDir ){
	fTask=0;

	InitNeuroBayes(fTask);
	Log() << kINFO << "Expert Constructor was called" << Endl;
} 

TMVA::MethodNeuroBayes::MethodNeuroBayes( const TString& jobName,
					  const TString& methodTitle, 
					  DataSetInfo& theData, 
					  const TString& theOption, 
					  TDirectory* theTargetDir )
	   : TMVA::MethodBase( jobName, Types::kPlugins, methodTitle, theData, theOption, theTargetDir )
{
	fTask = 1;
	InitNeuroBayes(fTask);
	MyID = CountInstanzes;
	//Log() << kINFO << methodTitle << " got ID " << MyID << " theTargetDir =  " << theTargetDir << Endl;

	CountInstanzes++;
	if(MyID > 0) {
		Log() << kWARNING << "This NeuroBayes instance was not created as first one."<<Endl;
		Log() << kWARNING << "Because Teacher is a singleton you won't get useful results from this Method" << Endl;
		Log() << kWARNING << "Use only one Teacher at a time until this is fixed" << Endl;
	}

	TeacherConfigured = false;
	Log() << kINFO << "Teacher Constructor was called" << Endl;
}

TMVA::MethodNeuroBayes::~MethodNeuroBayes(){
	delete Net;
}

Bool_t TMVA::MethodNeuroBayes::HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets )
{
	//NeuroBayes handles Classification
	if( type == Types::kClassification && numberClasses == 2 ) return kTRUE;
	return kFALSE;
}

//_______________________________________________________________________
void TMVA::MethodNeuroBayes::Init(){}

void TMVA::MethodNeuroBayes::InitNeuroBayes( int task ) //task = 1 means Teacher, task = 0 means Expert 
{
	TString filedir = "weights";
	NBOutputFile = filedir + "/" +  GetJobName() + "_" + GetMethodName() + ".NB_weights" ;
	
	if( task == 1 ) {
		//set an instance of Teacher
		nb = NeuroBayesTeacher::Instance();
	
		nb->SetOutputFile(NBOutputFile + ".nb");  	// expert file

  		//setup network topology
  		const int  nvar = GetNvar();    //number of input variables
  		nb->NB_DEF_NODE1(nvar+1);       // nodes in input layer 
  		nb->NB_DEF_NODE2(nvar+2);      	// nodes in hidden layer 
  		nb->NB_DEF_NODE3(1);       	// nodes in output layer
  
  		nb->NB_DEF_TASK("CLA");    	// binominal classification

  		int i= 4701;
  		int j=21; 
  		nb->NB_RANVIN(i,j,2);   	// random number seed initialisation,
						// i has to be an odd number, the third argument is a debugging flag
	}
	else if ( task == 0 ) {
	}
}


//_______________________________________________________________________
void TMVA::MethodNeuroBayes::InitEventSample( void )
{
   	if (!HasTrainingTree()) Log() << kFATAL << "<Init> Data().TrainingTree() is zero pointer" << Endl;

   	Int_t nevents = Data()->GetNTrainingEvents();
	Int_t nsignal=0;

	// event loop on Training Data
 	for (Int_t ievt=0; ievt<nevents; ievt++) {

		const Event* event = GetTrainingEvent(ievt);

   		int nvar = GetNvar();

		float InputArray[nvar];   
		memset(InputArray,-999,nvar*sizeof(float)); //reset
{'fraction_non_alphanumeric': 0.15738771424996872, 'fraction_numerical': 0.007756787188790191, 'mean_word_length': 3.9498452012383902, 'pattern_counts': {'":': 0, '<': 124, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4193762', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2615, 'n_words': 667}