//------------------------------------------------------------------------ void undo_wave_temp(fstream &in, fstream &out, string file_name) { if(in.is_open()) in.close(); if(out.is_open()) out.close(); remove(file_name.c_str()); rename( (file_name + ".tmp").c_str() , file_name.c_str() ); return; }
/* * Given a file stream, opens that filestream to log.txt * Throws log_fail_ex on failure */ void open_log_file(fstream &log) { if (log.is_open()) { throw log_fail_ex; } log.open("log.txt", ios::out | ios::app); if (!log.is_open()) { throw log_fail_ex; } }
bool RBMQuantizer::saveModelToFile(fstream &file) const{ if( !file.is_open() ){ errorLog << "saveModelToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the header file << "RBM_QUANTIZER_FILE_V1.0" << endl; //Save the base feature extraction settings to the file if( !saveBaseSettingsToFile( file ) ){ errorLog << "saveModelToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl; return false; } file << "QuantizerTrained: " << trained << endl; file << "NumClusters: " << numClusters << endl; if( trained ){ if( !rbm.saveModelToFile( file ) ){ errorLog << "saveModelToFile(fstream &file) - Failed to save RBM settings to file!" << endl; return false; } } return true; }
bool TimeseriesBuffer::loadSettingsFromFile(fstream &file){ if( !file.is_open() ){ errorLog << "loadSettingsFromFile(fstream &file) - The file is not open!" << endl; return false; } string word; //Load the header file >> word; if( word != "GRT_TIMESERIES_BUFFER_FILE_V1.0" ){ errorLog << "loadSettingsFromFile(fstream &file) - Invalid file format!" << endl; return false; } if( !loadBaseSettingsFromFile( file ) ){ errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl; return false; } file >> word; if( word != "BufferSize:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read BufferSize header!" << endl; return false; } file >> bufferSize; //Init the TimeseriesBuffer module to ensure everything is initialized correctly return init(bufferSize,numInputDimensions); }
void open_file_update(fstream& fp, const char* name, const char* folder_name) { char file_name[BUFLEN]; if (folder_name == NULL) snprintf(file_name, BUFLEN-1, "%s/%s", FOLDER_STATE, name); else snprintf(file_name, BUFLEN-1, "%s/%s", folder_name, name); fp.open(file_name, ios_base::in | ios_base::out); if (!fp.is_open()) { fp.open(file_name, ios_base::out); if (!fp.is_open()) { cout <<"Warning: could not operate file "<<file_name << endl; exit(1); } } }
bool LogisticRegression::saveModelToFile(fstream &file) const{ if(!file.is_open()) { errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl; return false; } //Write the header info file<<"GRT_LOGISTIC_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!" << endl; return false; } if( trained ){ file << "Weights: "; file << w0; for(UINT j=0; j<numInputDimensions; j++){ file << " " << w[j]; } file << endl; } return true; }
bool Softmax::saveModelToFile(fstream &file) const{ if(!file.is_open()) { errorLog <<"loadModelFromFile(fstream &file) - The file is not open!" << endl; return false; } //Write the header info file<<"GRT_SOFTMAX_MODEL_FILE_V1.0\n"; file<<"NumFeatures: "<<numFeatures<<endl; file<<"NumClasses: "<<numClasses<<endl; file <<"UseScaling: " << useScaling << endl; file<<"UseNullRejection: " << useNullRejection << endl; ///Write the ranges if needed if( useScaling ){ file << "Ranges: \n"; for(UINT n=0; n<ranges.size(); n++){ file << ranges[n].minValue << "\t" << ranges[n].maxValue << endl; } } file << "Models:\n"; for(UINT k=0; k<numClasses; k++){ file << "ClassLabel: " << models[k].classLabel << endl; file << "Weights: " << models[k].w0; for(UINT n=0; n<numFeatures; n++){ file << " " << models[k].w[n]; } file << endl; } return true; }
bool FeatureExtraction::loadBaseSettingsFromFile(fstream &file){ featureVector.clear(); if( !file.is_open() ){ errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << endl; return false; } string word; //Load the NumInputDimensions file >> word; if( word != "NumInputDimensions:" ){ errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumInputDimensions header!" << endl; return false; } file >> numInputDimensions; //Load the NumOutputDimensions file >> word; if( word != "NumOutputDimensions:" ){ errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << endl; return false; } file >> numOutputDimensions; //Resize the feature vector featureVector.resize(numOutputDimensions,0); return true; }
int printFile(){ if(fs.is_open()) { // get the starting position streampos start = fs.tellg(); // go to the end fs.seekg(0, std::ios::end); // get the ending position streampos end = fs.tellg(); // go back to the start fs.seekg(0, std::ios::beg); // create a vector to hold the data that // is resized to the total size of the file std::vector<char> contents; contents.resize(static_cast<size_t>(end - start)); // read it in fs.read(&contents[0], contents.size()); // print it out (for clarity) cout <<"\n Full File:\n"; for(int i=0; i < contents.size(); i++){ cout << i; }; cout<<"\n File Read Done!!\n"; } }
bool LeakyIntegrator::loadModelFromFile(fstream &file){ if( !file.is_open() ){ errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl; return false; } string word; //Load the header file >> word; if( word != "GRT_LEAKY_INTEGRATOR_FILE_V1.0" ){ errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << endl; return false; } if( !PreProcessing::loadPreProcessingSettingsFromFile( file ) ){ errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load preprocessing settings from file!" << endl; return false; } //Load the LeakRate file >> word; if( word != "LeakRate:" ){ errorLog << "loadModelFromFile(fstream &file) - Failed to read LeakRate header!" << endl; return false; } file >> leakRate; //Init the LeakyIntegrator module to ensure everything is initialized correctly return init(leakRate,numInputDimensions); }
bool KMeans::saveModelToFile(fstream &file) const{ if( !file.is_open() ){ errorLog << "saveModelToFile(fstream &file) - Failed to save model, file is not open!" << 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!" << 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 << endl; } } return true; }
bool RegressionTree::saveModelToFile(fstream &file) const{ if(!file.is_open()) { Regressifier::errorLog <<"saveModelToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the header info file << "GRT_REGRESSION_TREE_MODEL_FILE_V1.0\n"; //Write the classifier settings to the file if( !Regressifier::saveBaseSettingsToFile(file) ){ Regressifier::errorLog <<"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << endl; return false; } file << "NumSplittingSteps: " << numSplittingSteps << endl; file << "MinNumSamplesPerNode: " << minNumSamplesPerNode << endl; file << "MaxDepth: " << maxDepth << endl; file << "RemoveFeaturesAtEachSpilt: " << removeFeaturesAtEachSpilt << endl; file << "TrainingMode: " << trainingMode << endl; file << "TreeBuilt: " << (tree != NULL ? 1 : 0) << endl; if( tree != NULL ){ file << "Tree:\n"; if( !tree->saveToFile( file ) ){ Regressifier::errorLog << "saveModelToFile(fstream &file) - Failed to save tree to file!" << endl; return false; } } return true; }
bool MovementTrajectoryFeatures::saveSettingsToFile(fstream &file) const{ if( !file.is_open() ){ errorLog << "saveSettingsToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the file header file << "GRT_MOVEMENT_TRAJECTORY_FEATURES_FILE_V1.0" << endl; //Save the base settings to the file if( !saveBaseSettingsToFile( file ) ){ errorLog << "saveSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl; return false; } //Write the movement trajectory settings to the file file << "TrajectoryLength: " << trajectoryLength << endl; file << "NumCentroids: " << numCentroids << endl; file << "FeatureMode: " << featureMode << endl; file << "NumHistogramBins: " << numHistogramBins << endl; file << "UseTrajStartAndEndValues: " << useTrajStartAndEndValues << endl; file << "UseWeightedMagnitudeValues: " << useWeightedMagnitudeValues << endl; return true; }
bool is_terminated() { // check if one of the programs terminates //read the existing files file.open(filename.c_str(), ios::in); if (file.is_open()) //if there is a program end first return true; return false; }
bool FFT::loadSettingsFromFile(fstream &file){ if( !file.is_open() ){ errorLog << "loadSettingsFromFile(fstream &file) - The file is not open!" << endl; return false; } string word; //Load the header file >> word; if( word != "GRT_FFT_FILE_V1.0" ){ errorLog << "loadSettingsFromFile(fstream &file) - Invalid file format!" << endl; return false; } if( !loadBaseSettingsFromFile( file ) ){ errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl; return false; } file >> word; if( word != "HopSize:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read HopSize header!" << endl; return false; } file >> hopSize; file >> word; if( word != "FftWindowSize:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read FftWindowSize header!" << endl; return false; } file >> fftWindowSize; file >> word; if( word != "FftWindowFunction:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read FftWindowFunction header!" << endl; return false; } file >> fftWindowFunction; file >> word; if( word != "ComputeMagnitude:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read ComputeMagnitude header!" << endl; return false; } file >> computeMagnitude; file >> word; if( word != "ComputePhase:" ){ errorLog << "loadSettingsFromFile(fstream &file) - Failed to read ComputePhase header!" << endl; return false; } file >> computePhase; //Init the FFT module to ensure everything is initialized correctly return init(fftWindowSize,hopSize,numInputDimensions,fftWindowFunction,computeMagnitude,computePhase); }
MStatus XmlCacheFormat::readHeader() { bool rtn = false; if (kWrite != fMode) { if( fFile.is_open() ) { string tag; readXmlTag( tag ); if( tag == XMLSTARTTAG(cacheTag) ) { MStringArray value; readXmlTagValue( versionTag, value ); readXmlTagValue( startTimeTag, value ); readXmlTagValue( endTimeTag, value ); readXmlTag( tag ); // Should be header close tag, check if( tag != XMLENDTAG(cacheTag) ) { // TODO - handle this error case } rtn = true; } } } return rtn ? MS::kSuccess : MS::kFailure ; }
bool FFT::saveSettingsToFile(fstream &file){ if( !file.is_open() ){ errorLog << "saveSettingsToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the file header file << "GRT_FFT_FILE_V1.0" << endl; //Save the base settings to the file if( !saveBaseSettingsToFile( file ) ){ errorLog << "saveSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl; return false; } //Write the FFT settings file << "HopSize: " << hopSize << endl; file << "FftWindowSize: " << fftWindowSize << endl; file << "FftWindowFunction: " << fftWindowFunction << endl; file << "ComputeMagnitude: " << computeMagnitude << endl; file << "ComputePhase: " << computePhase << endl; return true; }
bool AdaBoost::saveModelToFile(fstream &file) const{ if(!file.is_open()) { errorLog <<"saveModelToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the header info file<<"GRT_ADABOOST_MODEL_FILE_V2.0\n"; //Write the classifier settings to the file if( !Classifier::saveBaseSettingsToFile(file) ){ errorLog <<"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << endl; return false; } //Write the AdaBoost settings to the file file << "PredictionMethod: " << predictionMethod << endl; //If the model has been trained then write the model if( trained ){ file << "Models: " << endl; for(UINT i=0; i<models.size(); i++){ if( !models[i].saveModelToFile( file ) ){ errorLog <<"saveModelToFile(fstream &file) - Failed to write model " << i << " to file!" << endl; file.close(); return false; } } } return true; }
MStatus XmlCacheFormat::open( const MString& fileName, FileAccessMode mode ) { bool rtn = true; assert((fileName.length() > 0)); fFileName = fileName; if( mode == kWrite ) { fFile.open(fFileName.asChar(), ios::out); } else if (mode == kReadWrite) { fFile.open(fFileName.asChar(), ios::app); } else { fFile.open(fFileName.asChar(), ios::in); } if (!fFile.is_open()) { rtn = false; } else { if (mode == kRead) { rtn = readHeader(); } } return rtn ? MS::kSuccess : MS::kFailure ; }
bool KMeansQuantizer::saveSettingsToFile(fstream &file) const{ if( !file.is_open() ){ errorLog << "saveSettingsToFile(fstream &file) - The file is not open!" << endl; return false; } //First, you should add a header (with no spaces) e.g. file << "KMEANS_QUANTIZER_FILE_V1.0" << endl; //Second, you should save the base feature extraction settings to the file if( !saveBaseSettingsToFile( file ) ){ errorLog << "saveSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl; return false; } file << "QuantizerTrained: " << quantizerTrained << endl; file << "NumClusters: " << numClusters << endl; if( quantizerTrained ){ file << "Clusters: \n"; for(UINT k=0; k<numClusters; k++){ for(UINT j=0; j<numInputDimensions; j++){ file << clusters[k][j]; if( j != numInputDimensions-1 ) file << "\t"; else file << endl; } } } return true; }
bool TimeDomainFeatures::saveModelToFile(fstream &file) const{ if( !file.is_open() ){ errorLog << "saveModelToFile(fstream &file) - The file is not open!" << endl; return false; } //Write the file header file << "GRT_TIME_DOMAIN_FEATURES_FILE_V1.0" << endl; //Save the base settings to the file if( !saveFeatureExtractionSettingsToFile( file ) ){ errorLog << "saveFeatureExtractionSettingsToFile(fstream &file) - Failed to save base feature extraction settings to file!" << endl; return false; } //Write the time domain settings to the file file << "BufferLength: " << bufferLength << endl; file << "NumFrames: " << numFrames << endl; file << "OffsetInput: " << offsetInput << endl; file << "UseMean: " << useMean << endl; file << "UseStdDev: " << useStdDev << endl; file << "UseEuclideanNorm: " << useEuclideanNorm << endl; file << "UseRMS: " << useRMS << endl; return true; }
bool MovementIndex::loadModelFromFile(fstream &file) { if( !file.is_open() ) { errorLog << "loadModelFromFile(fstream &file) - The file is not open!" << endl; return false; } string word; //Load the header file >> word; if( word != "GRT_MOVEMENT_INDEX_FILE_V1.0" ) { errorLog << "loadModelFromFile(fstream &file) - Invalid file format!" << endl; return false; } if( !loadFeatureExtractionSettingsFromFile( file ) ) { errorLog << "loadFeatureExtractionSettingsFromFile(fstream &file) - Failed to load base feature extraction settings from file!" << endl; return false; } //Load the BufferLength file >> word; if( word != "BufferLength:" ) { errorLog << "loadModelFromFile(fstream &file) - Failed to read BufferLength header!" << endl; return false; } file >> bufferLength; //Init the MovementIndex module to ensure everything is initialized correctly return init(bufferLength,numInputDimensions); }
ElectronPhononExchangeHertel::ElectronPhononExchangeHertel(fstream &fileId, map<string,double> & parameters, Material * material) : ElectronPhononExchange(), exchangeCoef_(0), debeyeTemperature_(1), massEnhancement_(0), material_(material) { if (!fileId.is_open()) throw ATC_Error("cannot open material file"); vector<string> line; while(fileId.good()) { command_line(fileId, line); if (line.size() == 0) continue; if (line[0] == "end") break; else if (line[0] == "debeye_temperature") { debeyeTemperature_ = str2dbl(line[1]); parameters["debeye_temperature"] = debeyeTemperature_; } else if (line[0] == "mass_enhancement") { massEnhancement_ = str2dbl(line[1]); parameters["mass_enhancement"] = massEnhancement_; } else { throw ATC_Error( "unrecognized material function "+line[0]); } } // coupling coefficient, eqn. 15 of Hertel 2002 double kb = LammpsInterface::instance()->kBoltzmann(); double hbar = LammpsInterface::instance()->hbar(); double PI = 3.141592653589793238; exchangeCoef_ = 144.*1.0369*kb/(PI*hbar); exchangeCoef_ *= massEnhancement_/pow(debeyeTemperature_,2); }
bool Softmax::saveModelToFile(fstream &file) const{ if(!file.is_open()) { errorLog <<"loadModelFromFile(fstream &file) - The file is not open!" << 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 <<"saveModelToFile(fstream &file) - Failed to save classifier base settings to file!" << endl; return false; } if( trained ){ file << "Models:\n"; for(UINT k=0; k<numClasses; k++){ file << "ClassLabel: " << models[k].classLabel << endl; file << "Weights: " << models[k].w0; for(UINT n=0; n<numInputDimensions; n++){ file << " " << models[k].w[n]; } file << endl; } } return true; }
bool PreProcessing::loadPreProcessingSettingsFromFile(fstream &file){ if( !file.is_open() ){ errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - The file is not open!" << endl; return false; } //Try and load the base settings from the file if( !MLBase::loadBaseSettingsFromFile( file ) ){ errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to load base settings from file!" << endl; return false; } string word; //Load if the filter has been initialized file >> word; if( word != "Initialized:" ){ errorLog << "loadPreProcessingSettingsFromFile(fstream &file) - Failed to read Initialized header!" << 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; }
/* * Given a string containing an action that occurred and the result of that action, appends the string and the result to the log file * Places a timestamp at the start of the string * Places a newline at the end of the string * Throws log_fail_ex if the log is not open */ void append_action_to_log(fstream& log, string s, bool result) { if (!log.is_open()) { throw log_fail_ex; } time_t current_time; time(¤t_time); //FIXME remove newline char at the end of ctime log << ctime(¤t_time) << ": " << s; if (result) { log << " (SUCCESS)"; }else { log << " (FAILURE)"; } log << endl; if (log.fail()) { throw log_fail_ex; } }
/* * Given a (pre-formatted) string and file stream, appends the string to the log file * Places a timestamp at the start of the string * Places a newline at the end of the string if one is not at the end of the string * Throws log_fail_ex if the log is not open */ void append_raw_to_log(fstream& log, string s) { if (!log.is_open()) { throw log_fail_ex; } time_t current_time; time(¤t_time); log << ctime(¤t_time) << s; if (s[s.size() - 1] != '\n') { log << endl; }else { flush(log); } if (log.fail()) { throw log_fail_ex; } }
bool BernoulliRBM::saveModelToFile(fstream &file) const{ if(!file.is_open()) { errorLog <<"saveModelToFile(fstream &file) - The file is not open!" << 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!" << endl; return false; } file << "NumVisibleUnits: " << numVisibleUnits << endl; file << "NumHiddenUnits: " << numHiddenUnits << endl; file << "BatchSize: " << batchSize << endl; file << "BatchStepSize: " << batchStepSize << endl; file << "LearningRate: " << learningRate << endl; file << "LearningRateUpdate: " << learningRateUpdate << endl; file << "Momentum: " << momentum << endl; file << "RandomizeWeightsForTraining: " << randomizeWeightsForTraining << endl; file << "Ranges: \n"; for(UINT n=0; n<ranges.size(); n++){ file << ranges[n].minValue << "\t" << ranges[n].maxValue << endl; } //If the model has been trained then write the model if( trained ){ file << "WeightsMatrix: " << 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 << endl; } file << "VisibleLayerBias: "; for(unsigned int i=0; i<visibleLayerBias.size(); i++){ file << visibleLayerBias[i]; if( i < visibleLayerBias.size()-1 ) file << " "; } file << endl; file << "HiddenLayerBias: "; for(unsigned int i=0; i<hiddenLayerBias.size(); i++){ file << hiddenLayerBias[i]; if( i < hiddenLayerBias.size()-1 ) file << " "; } file << endl; } return true; }
//自分が入力したファイル名のファイルを開けるようになった! bool DumpFile::Open(){ string filename; cout << "ファイル名 > " << flush; getline(cin, filename); m_file.open(filename.c_str(),ios::in|ios::binary); m_page = 0; return m_file.is_open(); }
/* * Given a file stream, cleanly closes the file */ void close_log_file(fstream& log) { if (log.is_open()) { log.close(); } //If the log is already closed, we have nothing to worry about }