Exemplo n.º 1
0
bool XorEncryptor::encryptData(std::fstream &original, std::fstream &result)
{
	if (!original.is_open() || !result.is_open())
	{
		return false;
	}

	original.seekg(0, std::ios::beg);
	result.seekp(0, std::ios::beg);

	char c = 0;
	unsigned i = 0;
	while (original.good())
	{
		original.read(&c, 1);
		c ^= password[i];
		if(original.gcount() > 0)
		{
			result.write(&c, 1);
		}

		if (++i == passSize)
		{
			i = 0;
		}
	}

	original.seekg(0, std::ios::beg);
	result.seekg(0, std::ios::beg);
	result.flush();

	return true;
}
Exemplo n.º 2
0
NMPRKC_API nmprk_status_t NMPRK_StartDebugLogging(
	const char *filename)
{
	char dateStr[MAX_DATE_STR_LEN];
	char timeStr[MAX_DATE_STR_LEN];

	if(si_fsDebugLog.is_open() == true)
		return NMPRK_FAILURE;

	try
	{
		si_debugModule = SI_DEBUG_MODULE_ALL;
		si_debugLevel = SI_DEBUG_LEVEL_ALL;
		si_fsDebugLog.open(filename, std::fstream::out | std::fstream::app);
		if(si_fsDebugLog.is_open() != true)
			return NMPRK_FAILURE;
	}
	catch (...)
	{
		return NMPRK_FAILURE;
	}

#if defined WIN32
	_strdate_s(dateStr, MAX_DATE_STR_LEN);
	_strtime_s(timeStr, MAX_DATE_STR_LEN);
#else
	time_t mytime = time(NULL);
	strftime(dateStr, 9, "%D", localtime(&mytime));
	strftime(timeStr, 9, "%T", localtime(&mytime));
#endif
		
	SI_DEBUG_INFO(SI_THIS_MODULE, "Debug Logging Started: %s %s", dateStr, timeStr);

	return NMPRK_SUCCESS;
}
Exemplo n.º 3
0
// Ensures the file is opened/closed properly and retries 5 times.
// if choice is false, the file is closed and if it is 1, the file is opened.
bool verifiedOC ( std::fstream& file, std::string fileDir, bool choice, std::ios::openmode io ) {
    unsigned int i = 0; // Declaring a counter variable.
    
    // Choice determines if we are opening or closing the file. (True to open, False to close)
    if ( choice ) {
        do {
            file.open ( fileDir.c_str(), io );  // Open file as user selection.
            if ( file.is_open() ) {
                return true;
            } else {
                // Prints that the attempt to change the file state has failed.
                std::cout << "The file " << fileDir.c_str() << " failed to open... Retrying " << ++i << "\n";
            }
        
            // Will exit the loop after the the number of attempts FILE_OPEN_RETRIES specifies.
            if ( i >= FILE_OPEN_RETRIES ) {
                std::cout << "The file " << fileDir.c_str() << " failed to change open." << std::endl;
                return false;
            }
        } while ( !file.is_open() );
    } else {
        file.close();
    }
    
    return true;
}
Exemplo n.º 4
0
bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
{
    std::ios_base::openmode fmode = std::fstream::binary;
    if (mode == File::Write) {
        fmode |= (std::fstream::out | std::fstream::trunc);
        createCache(SNAPPY_CHUNK_SIZE);
    } else if (mode == File::Read) {
        fmode |= std::fstream::in;
    }

    m_stream.open(filename.c_str(), fmode);

    //read in the initial buffer if we're reading
    if (m_stream.is_open() && mode == File::Read) {
        m_stream.seekg(0, std::ios::end);
        m_endPos = m_stream.tellg();
        m_stream.seekg(0, std::ios::beg);

        // read the snappy file identifier
        unsigned char byte1, byte2;
        m_stream >> byte1;
        m_stream >> byte2;
        assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);

        flushReadCache();
    } else if (m_stream.is_open() && mode == File::Write) {
Exemplo n.º 5
0
bool ClassLabelChangeFilter::load( std::fstream &file ){
    
    if( !file.is_open() ){
        errorLog << "load(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    std::string word;
    
    //Load the header
    file >> word;
    
    if( word != "GRT_CLASS_LABEL_CHANGE_FILTER_FILE_V1.0" ){
        errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
        return false;
    }
    
    file >> word;
    if( word != "NumInputDimensions:" ){
        errorLog << "load(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
        return false;
    }
    file >> numInputDimensions;
    
    //Load the number of output dimensions
    file >> word;
    if( word != "NumOutputDimensions:" ){
        errorLog << "load(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
        return false;
    }
    file >> numOutputDimensions;
    
    //Init the classLabelTimeoutFilter module to ensure everything is initialized correctly
    return init();
}
Exemplo n.º 6
0
bool TimeDomainFeatures::saveModelToFile( std::fstream &file ) const{
    
    if( !file.is_open() ){
        errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    //Write the file header
    file << "GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << std::endl;	
    
    //Save the base settings to the file
    if( !saveFeatureExtractionSettingsToFile( file ) ){
        errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
        return false;
    }
    
    //Write the time domain settings to the file
    file << "BufferLength: " << bufferLength << std::endl;
    file << "NumFrames: " << numFrames << std::endl;
    file << "OffsetInput: " << offsetInput << std::endl;
    file << "UseMean: " << useMean << std::endl;
    file << "UseStdDev: " << useStdDev << std::endl;
    file << "UseEuclideanNorm: " << useEuclideanNorm << std::endl;
    file << "UseRMS: " << useRMS << std::endl;
    
    return true;
}
Exemplo n.º 7
0
bool RBMQuantizer::save( std::fstream &file ) const{
    
    if( !file.is_open() ){
        errorLog << "save(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    //Write the header
    file << "RBM_QUANTIZER_FILE_V1.0" << std::endl;
    
    //Save the base feature extraction settings to the file
    if( !saveFeatureExtractionSettingsToFile( file ) ){
        errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
        return false;
    }
    
    file << "QuantizerTrained: " << trained << std::endl;
    file << "NumClusters: " << numClusters << std::endl;
    
    if( trained ){
        if( !rbm.save( file ) ){
            errorLog << "save(fstream &file) - Failed to save RBM settings to file!" << std::endl;
            return false;
        }
    }
    
    return true;
}
Exemplo n.º 8
0
bool PostProcessing::loadPostProcessingSettingsFromFile(std::fstream &file){
    
    if( !file.is_open() ){
        errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    //Try and load the base settings from the file
    if( !MLBase::loadBaseSettingsFromFile( file ) ){
        return false;
    }
    
    std::string word;
    
    //Load if the filter has been initialized
    file >> word;
    if( word != "Initialized:" ){
        errorLog << "loadPostProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << std::endl;
        clear();
        return false;
    }
    file >> initialized;
    
    //If the module has been initalized then call the init function to setup the processed data vector
    if( initialized ){
        return init();
    }
    
    return true;
}
Exemplo n.º 9
0
bool LinearRegression::saveModelToFile( std::fstream &file ) const{
    
    if(!file.is_open())
	{
        errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
		return false;
	}
    
	//Write the header info
    file<<"GRT_LINEAR_REGRESSION_MODEL_FILE_V2.0\n";
    
    //Write the regressifier settings to the file
    if( !Regressifier::saveBaseSettingsToFile(file) ){
        errorLog <<"saveModelToFile(fstream &file) - Failed to save Regressifier base settings to file!" << std::endl;
		return false;
    }
    
    if( trained ){
        file << "Weights: ";
        file << w0;
        for(UINT j=0; j<numInputDimensions; j++){
            file << " " << w[j];
        }
        file << std::endl;
    }
    
    return true;
}
Exemplo n.º 10
0
void read_matrix_size(std::fstream& f, std::size_t & sz1, std::size_t & sz2)
{
  if(!f.is_open())
    throw std::invalid_argument("File is not opened");

  f >> sz1 >> sz2;
}
Exemplo n.º 11
0
int main(int argc, char *argv[]) {
	//Initialize symbol table code. Pin does not read symbols unless this is called
	PIN_InitSymbols();

	//initialize Pin system
	if(PIN_Init(argc,argv)) {
		return Usage();
	}

    string filename = KnobOutputFile.Value();

    //file to record all instructions
    #ifdef LOG_ASSEM
     //TraceFile.open(filename, ios::out);
     AxOpenFile(TraceFile, filename);
     if (TraceFile.is_open()) {
        PRINT_SCN(filename << " : Start to make trace at instruction #" << InsCount);
     } else {
        PRINT_SCN("cannot open");
        return -1;
     }
    #endif
    //file to record all memory accesses
    MemFile.open("mem.txt", ios::out);

	//add a function used to instrument at instruction granularity 
	INS_AddInstrumentFunction(Instruction, 0);
	//call 'Fini' immediately before the application exits
	PIN_AddFiniFunction(Fini, 0);

	//starts executing the application
	PIN_StartProgram();

	return 0;
}
Exemplo n.º 12
0
bool TimeseriesBuffer::load( std::fstream &file ){
    
    if( !file.is_open() ){
        errorLog << "load(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    std::string word;
    
    //Load the header
    file >> word;
    
    if( word != "GRT_TIMESERIES_BUFFER_FILE_V1.0" ){
        errorLog << "load(fstream &file) - Invalid file format!" << std::endl;
        return false;
    }
    
    if( !loadFeatureExtractionSettingsFromFile( file ) ){
        errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
        return false;
    }
    
    file >> word;
    if( word != "BufferSize:" ){
        errorLog << "load(fstream &file) - Failed to read BufferSize header!" << std::endl;
        return false;
    }
    file >> bufferSize;
    
    //Init the TimeseriesBuffer module to ensure everything is initialized correctly
    return init(bufferSize,numInputDimensions);
}
Exemplo n.º 13
0
bool FFT::loadModelFromFile( std::fstream &file ){
    
    if( !file.is_open() ){
        errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    std::string word;
    
    //Load the header
    file >> word;
    
    if( word != "GRT_FFT_FILE_V1.0" ){
        errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << std::endl;
        return false;     
    }
    
    if( !loadFeatureExtractionSettingsFromFile( file ) ){
        errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << std::endl;
        return false;
    }
    
    file >> word;
    if( word != "HopSize:" ){
        errorLog << "loadModelFromFile(fstream &file) - Failed to read HopSize header!" << std::endl;
        return false;     
    }
    file >> hopSize;
    
    file >> word;
    if( word != "FftWindowSize:" ){
        errorLog << "loadModelFromFile(fstream &file) - Failed to read FftWindowSize header!" << std::endl;
        return false;     
    }
    file >> fftWindowSize;
    
    file >> word;
    if( word != "FftWindowFunction:" ){
        errorLog << "loadModelFromFile(fstream &file) - Failed to read FftWindowFunction header!" << std::endl;
        return false;     
    }
    file >> fftWindowFunction;
    
    file >> word;
    if( word != "ComputeMagnitude:" ){
        errorLog << "loadModelFromFile(fstream &file) - Failed to read ComputeMagnitude header!" << std::endl;
        return false;     
    }
    file >> computeMagnitude;
    
    file >> word;
    if( word != "ComputePhase:" ){
        errorLog << "loadModelFromFile(fstream &file) - Failed to read ComputePhase header!" << std::endl;
        return false;     
    }
    file >> computePhase;
    
    //Init the FFT module to ensure everything is initialized correctly
    return init(fftWindowSize,hopSize,numInputDimensions,fftWindowFunction,computeMagnitude,computePhase);
}
Exemplo n.º 14
0
bool DecisionTreeClusterNode::loadParametersFromFile( std::fstream &file ){

    if(!file.is_open())
    {
        errorLog << __GRT_LOG__ << " File is not open!" << std::endl;
        return false;
    }

    //Load the DecisionTreeNode parameters
    if( !DecisionTreeNode::loadParametersFromFile( file ) ){
        errorLog << __GRT_LOG__ << " Failed to load DecisionTreeNode parameters from file!" << std::endl;
        return false;
    }

    std::string word;
    //Load the custom DecisionTreeThresholdNode Parameters
    file >> word;
    if( word != "FeatureIndex:" ){
        errorLog << __GRT_LOG__ << " Failed to find FeatureIndex header!" << std::endl;
        return false;
    }
    file >> featureIndex;

    file >> word;
    if( word != "Threshold:" ){
        errorLog << __GRT_LOG__ << " Failed to find Threshold header!" << std::endl;
        return false;
    }
    file >> threshold;

    return true;
}
Exemplo n.º 15
0
bool FFT::saveModelToFile( std::fstream &file ) const{
    
    if( !file.is_open() ){
        errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    //Write the file header
    file << "GRT_FFT_FILE_V1.0" << std::endl;
    
    //Save the base settings to the file
    if( !saveFeatureExtractionSettingsToFile( file ) ){
        errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
        return false;
    }
    
    //Write the FFT settings
    file << "HopSize: " << hopSize << std::endl;
    file << "FftWindowSize: " << fftWindowSize << std::endl;
    file << "FftWindowFunction: " << fftWindowFunction << std::endl;
    file << "ComputeMagnitude: " << computeMagnitude << std::endl;
    file << "ComputePhase: " << computePhase << std::endl;
    
    return true;
}
Exemplo n.º 16
0
bool FFTFeatures::saveModelToFile( std::fstream &file ) const{
    
    if( !file.is_open() ){
        errorLog << "saveModelToFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    //Write the file header
    file << "GRT_FFT_FEATURES_FILE_V1.0" << std::endl;
    
    //Save the base settings to the file
    if( !saveFeatureExtractionSettingsToFile( file ) ){
        errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << std::endl;
        return false;
    }
    
    //Write the FFT features settings
    file << "FFTWindowSize: " << fftWindowSize << std::endl;
    file << "NumChannelsInFFTSignal: " << numChannelsInFFTSignal << std::endl;
    file << "ComputeMaxFreqFeature: " << computeMaxFreqFeature << std::endl;
    file << "ComputeMaxFreqSpectrumRatio: " << computeMaxFreqSpectrumRatio << std::endl;
    file << "ComputeCentroidFeature: " << computeCentroidFeature << std::endl;	
    file << "ComputeTopNFreqFeatures: " << computeTopNFreqFeatures << std::endl;		
    file << "N: " << N << std::endl;
    
    return true;
}
Exemplo n.º 17
0
bool Softmax::save( std::fstream &file ) const{
    
    if(!file.is_open())
    {
        errorLog << __GRT_LOG__ << " The file is not open!" << std::endl;
        return false;
    }
    
    //Write the header info
    file<<"GRT_SOFTMAX_MODEL_FILE_V2.0\n";
    
    //Write the classifier settings to the file
    if( !Classifier::saveBaseSettingsToFile(file) ){
        errorLog << __GRT_LOG__ << " Failed to save classifier base settings to file!" << std::endl;
        return false;
    }
    
    if( trained ){
        file << "Models:\n";
        for(UINT k=0; k<numClasses; k++){
            file << "ClassLabel: " << models[k].classLabel << std::endl;
            file << "Weights: " << models[k].w0;
            for(UINT n=0; n<numInputDimensions; n++){
                file << " " << models[k].w[n];
            }
            file << std::endl;
        }
    }
    
    return true;
}
Exemplo n.º 18
0
bool KMeans::saveModelToFile( std::fstream &file ) const{

    if( !file.is_open() ){
        errorLog << "saveModelToFile(fstream &file) - Failed to save model, file is not open!" << std::endl;
        return false;
    }

    file << "GRT_KMEANS_MODEL_FILE_V1.0\n";
    
    if( !saveClustererSettingsToFile( file ) ){
        errorLog << "saveModelToFile(fstream &file) - Failed to save clusterer settings to file!" << std::endl;
        return false;
    }
    
    if( trained ){
        file << "Clusters:\n";

        for(UINT k=0; k<numClusters; k++){
            for(UINT n=0; n<numInputDimensions; n++){
                file << clusters[k][n] << "\t";
           }file << std::endl;
        }
    }

   return true;

}
Exemplo n.º 19
0
bool HierarchicalClustering::saveModelToFile( std::fstream &file ) const{
    
    if( !file.is_open() ){
        errorLog << "saveModelToFile(string filename) - Failed to open file!" << std::endl;
        return false;
    }
    
    file << "GRT_HIERARCHICAL_CLUSTERING_FILE_V1.0\n";
    
    if( !saveClustererSettingsToFile( file ) ){
        errorLog << "saveModelToFile(fstream &file) - Failed to save cluster settings to file!" << std::endl;
        return false;
    }
    
    if( trained ){
        file << "M: " << M << std::endl;
        file << "N: " << N << std::endl;
        file << "NumLevels: " << clusters.getSize() << std::endl;
        
        for(UINT i=0; i<clusters.getSize(); i++){
            file << "Level: " << clusters[i].getLevel() << std::endl;
            file << "NumClusters: " << clusters[i].getNumClusters() << std::endl;
        }
    }
    
    return true;
    
}
Exemplo n.º 20
0
void check_is_file_open( const std::fstream& file, const char *path)
{
    if ( !file.is_open() )
    {
        std::cerr << "Failed to open file " << path << std::endl;
        exit( 1 );
    }
}
Exemplo n.º 21
0
void PcieAccessInterfaceTest::write_file(char* data, const std::string& path, uint32_t size, uint32_t offset) {
    memory_file.open(path,  std::ios::out |std::ios::binary | std::ios::trunc);
    if (memory_file.is_open()) {
        memory_file.seekg(offset, std::ios::beg);
        memory_file.write(data, size);
        memory_file.close();
    }
}
Exemplo n.º 22
0
int deserialize(hash_map<K,V> &map, std::fstream& in) {
    if(!in.is_open())
        throw serialize_exception();
    
    // TODO
    
    return -1;
}
Exemplo n.º 23
0
int serialize(hash_map<K,V>& map, std::fstream& out) {
    if(!out.is_open())
        throw serialize_exception();
      
    // TODO

    return -1;
}
Exemplo n.º 24
0
bool BernoulliRBM::saveModelToFile( std::fstream &file ) const{
    
    if(!file.is_open())
	{
		errorLog <<"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
		return false;
	}
    
	//Write the header info
	file<<"GRT_BERNOULLI_RBM_MODEL_FILE_V1.1\n";
    
    if( !saveBaseSettingsToFile( file ) ){
        errorLog <<"saveModelToFile(fstream &file) - Failed to save base settings to file!" << std::endl;
        return false;
    }
    
    file << "NumVisibleUnits: " << numVisibleUnits << std::endl;
    file << "NumHiddenUnits: " << numHiddenUnits << std::endl;
    file << "BatchSize: " << batchSize << std::endl;
    file << "BatchStepSize: " << batchStepSize << std::endl;
    file << "LearningRate: " << learningRate << std::endl;
    file << "LearningRateUpdate: " << learningRateUpdate << std::endl;
    file << "Momentum: " << momentum << std::endl;
    file << "RandomizeWeightsForTraining: " << randomizeWeightsForTraining << std::endl;
	
    file << "Ranges: \n";
    for(UINT n=0; n<ranges.size(); n++){
        file << ranges[n].minValue << "\t" << ranges[n].maxValue << std::endl;
    }
    
    //If the model has been trained then write the model
    if( trained ){
        file << "WeightsMatrix: " << std::endl;
        for(UINT i=0; i<weightsMatrix.getNumRows(); i++){
            for(UINT j=0; j<weightsMatrix.getNumCols(); j++){
                file << weightsMatrix[i][j];
                if( j < weightsMatrix.getNumCols()-1 ) file << " ";
            }
            file << std::endl;
        }
        
        file << "VisibleLayerBias: ";
        for(unsigned int i=0; i<visibleLayerBias.size(); i++){
            file << visibleLayerBias[i];
            if( i < visibleLayerBias.size()-1 ) file << " ";
        }
        file << std::endl;
        
        file << "HiddenLayerBias: ";
        for(unsigned int i=0; i<hiddenLayerBias.size(); i++){
            file << hiddenLayerBias[i];
            if( i < hiddenLayerBias.size()-1 ) file << " ";
        }
        file << std::endl;
    }
    
    return true;
}
Exemplo n.º 25
0
bool ParticleClassifier::saveModelToFile( std::fstream &file ) const{
    
    if(!file.is_open())
    {
        errorLog <<"saveModelToFile(fstream &file) - The file is not open!" << std::endl;
        return false;
    }

    return true;
}
Exemplo n.º 26
0
NMPRKC_API nmprk_status_t NMPRK_StopDebugLogging()
{
	if(si_fsDebugLog.is_open() == true)
	{
		SI_DEBUG_INFO(SI_THIS_MODULE, "Debug Logging Stopped");
		si_fsDebugLog.close();
	}

	return NMPRK_SUCCESS;
}
Exemplo n.º 27
0
void load_terran() {
	terrain_stream.open("Data/Terrain.raw", std::ios::out | std::ios::binary | std::ios::in);
	if (terrain_stream.is_open()) {
		fprintf(stdout, "Terrain file open success\n");
	}
	else {
		fprintf(stderr, "Terrain file open fail\n");
	}
	terrain_stream.read((char*)terrain, MAP_SIZE * MAP_SIZE);
}
Exemplo n.º 28
0
bool MovementDetector::loadModelFromFile( std::fstream &file ){
    
    clear();
    
    if(!file.is_open())
    {
        errorLog << "loadModelFromFile(string filename) - Could not open file to load model!" << std::endl;
        return false;
    }
    
    std::string word;
    file >> word;
    
    //Write the header info
    if( word != "GRT_MOVEMENT_DETECTOR_MODEL_FILE_V1.0" ){
        errorLog <<"loadModelFromFile(fstream &file) - Failed to read file header!" << std::endl;
        return false;
    }
    
    //Load the base settings from the file
    if( !MLBase::loadBaseSettingsFromFile(file) ){
        errorLog << "loadModelFromFile(string filename) - Failed to load base settings from file!" << std::endl;
        return false;
    }
    
    file >> word;
    if( word != "SearchTimeout:" ){
        errorLog <<"loadModelFromFile(fstream &file) - Failed to read SearchTimeout header!" << std::endl;
        return false;
    }
    file >> searchTimeout;
    
    file >> word;
    if( word != "UpperThreshold:" ){
        errorLog <<"loadModelFromFile(fstream &file) - Failed to read UpperThreshold header!" << std::endl;
        return false;
    }
    file >> upperThreshold;
    
    file >> word;
    if( word != "LowerThreshold:" ){
        errorLog <<"loadModelFromFile(fstream &file) - Failed to read LowerThreshold header!" << std::endl;
        return false;
    }
    file >> lowerThreshold;
    
    file >> word;
    if( word != "Gamma:" ){
        errorLog <<"loadModelFromFile(fstream &file) - Failed to read Gamma header!" << std::endl;
        return false;
    }
    file >> gamma;
    
    return true;
}
Exemplo n.º 29
0
bool BAG::save( std::fstream &file ) const{
    
    if(!file.is_open())
    {
        errorLog <<"save(fstream &file) - The file is not open!" << std::endl;
        return false;
    }
    
    const UINT ensembleSize = getEnsembleSize();
    
    //Write the header info
    file << "GRT_BAG_MODEL_FILE_V2.0\n";
    
    //Write the classifier settings to the file
    if( !Classifier::saveBaseSettingsToFile(file) ){
        errorLog <<"save(fstream &file) - Failed to save classifier base settings to file!" << std::endl;
        return false;
    }
    
    if( trained ){
        
        file << "EnsembleSize: " << ensembleSize << std::endl;
        
        if( getEnsembleSize() > 0 ){
            
            //Save the weights
            file << "Weights: ";
            for(UINT i=0; i<getEnsembleSize(); i++){
                file << weights[i];
                if( i < ensembleSize-1 ) file << "\t";
                else file << "\n";
                }
            
            //Save the classifier types
            file << "ClassifierTypes: ";
            for(UINT i=0; i<getEnsembleSize(); i++){
                file << ensemble[i]->getClassifierType() << std::endl;
            }
            
            //Save the ensemble
            file << "Ensemble: \n";
            for(UINT i=0; i<getEnsembleSize(); i++){
                if( !ensemble[i]->save( file ) ){
                    errorLog <<"save(fstream &file) - Failed to save classifier " << i << " to file!" << std::endl;
                    return false;
                }
            }
        }
        
    }
    
    //NOTE: We do not need to close the file
    
    return true;
}
Exemplo n.º 30
0
void read_vector_body(std::fstream& f, std::vector<ScalarType>& v) {
    if(!f.is_open())
        throw std::invalid_argument("File is not opened");

    for(std::size_t i = 0; i < v.size(); i++)
    {
            ScalarType val = 0.0;
            f >> val;
            v[i] = val;
    }
}