void DelimitedDataReader::readData(void) { std::vector<std::string> tmpChars; // open file std::ifstream readStream; RbFileManager* f = new RbFileManager(filename); if (!f->openFile(readStream)) std::cout << "ERROR: Could not open file " << filename << "\n"; chars.clear(); // read file // bool firstLine = true; std::string readLine = ""; while (std::getline(readStream,readLine)) { std::string field = ""; std::stringstream ss(readLine); int pos = 0; while (std::getline(ss,field,delimiter)) { tmpChars.push_back(field); pos++; }; chars.push_back(tmpChars); tmpChars.clear(); }; }
RbHelpType* RbHelpParser::parseHelpType(const std::string &fn) { // first we need to load the file std::ifstream readStream; RbFileManager fm = RbFileManager(fn); fm.openFile( readStream ); // try to load the xml file pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(fn.c_str(), pugi::parse_default); if (result.status != pugi::status_ok) { std::cerr << "Problem while parsing file " << fn << std::endl; throw RbException( result.description() ); } RbHelpType* helpEntry = new RbHelpType(); pugi::xpath_node node = doc.select_single_node( "//type-help-entry" ); parseInternalHelpType( node, helpEntry ); // now return the help entry return helpEntry; }
RbHelpFunction RbHelpParser::parseHelpFunction(const std::string &fn) { // first we need to load the file std::ifstream readStream; RbFileManager fm = RbFileManager(fn); fm.openFile( readStream ); // try to load the xml file pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(fn.c_str(), pugi::parse_default); if (result.status != pugi::status_ok) { std::cerr << "Problem while parsing file " << fn << std::endl; throw RbException( result.description() ); } std::vector<RbHelpFunction> functions; // pugi::xpath_node node = doc.select_single_node( "//function-help-entry" ); pugi::xpath_node_set nodeSet = doc.select_nodes( "//function-help-entry" ); for (pugi::xpath_node_set::const_iterator it = nodeSet.begin(); it != nodeSet.end(); ++it) { pugi::xpath_node node = *it; RbHelpFunction helpEntryFunction = parseInternalHelpFunction( node ); functions.push_back( helpEntryFunction ); } RbHelpFunction helpEntry = functions[0]; return helpEntry; }
/** * Open the file stream to a file with the name used in the constructor. */ void NexusWriter::openStream( void ) { RbFileManager f = RbFileManager(fileName); f.createDirectoryForFile(); // open the stream to the file outStream.open( fileName.c_str(), std::fstream::out ); // write header line outStream << "#NEXUS" << std::endl; }
RbHelpParser::HelpEntryType RbHelpParser::testHelpEntry(const std::string &fn) { // first we need to load the file std::ifstream readStream; RbFileManager fm = RbFileManager(fn); fm.openFile( readStream ); // try to load the xml file pugi::xml_document doc; pugi::xml_parse_result result = doc.load_file(fn.c_str(), pugi::parse_default); if (result.status != pugi::status_ok) { std::cerr << "Problem while parsing file " << fn << std::endl; throw RbException( result.description() ); } pugi::xpath_node_set nodeSet = doc.select_nodes("//function-help-entry"); if ( nodeSet.size() > 0 ) { return FUNCTION; } nodeSet = doc.select_nodes("//type-help-entry"); if ( nodeSet.size() > 0 ) { return TYPE; } nodeSet = doc.select_nodes("//distribution-help-entry"); if ( nodeSet.size() > 0 ) { return DISTRIBUTION; } nodeSet = doc.select_nodes("//move-help-entry"); if ( nodeSet.size() > 0 ) { return MOVE; } nodeSet = doc.select_nodes("//monitor-help-entry"); if ( nodeSet.size() > 0 ) { return MONITOR; } throw RbException("Unknown help entry type in file '" + fn + "'."); }
/** * Should we stop now? * Yes, if the minimum ESS is larger than the provided threshold. */ bool GewekeStoppingRule::stop( size_t g ) { bool passed = true; for ( size_t i = 1; i <= numReplicates; ++i) { std::string fn = filename; if ( numReplicates > 1 ) { RbFileManager fm = RbFileManager(filename); fn = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + "_run_" + StringUtilities::to_string(i) + "." + fm.getFileExtension(); } TraceContinuousReader reader = TraceContinuousReader( fn ); // get the vector of traces from the reader std::vector<Trace> &data = reader.getTraces(); size_t maxBurnin = 0; for ( size_t j = 0; j < data.size(); ++j) { Trace &t = data[j]; const std::vector<double> &v = t.getValues(); size_t b = burninEst->estimateBurnin( v ); if ( maxBurnin < b ) { maxBurnin = b; } } GewekeTest gTest = GewekeTest( alpha, frac1, frac2 ); for ( size_t j = 0; j < data.size(); ++j) { RevBayesCore::Trace &t = data[j]; const std::vector<double> &v = t.getValues(); t.setBurnin( maxBurnin ); t.computeStatistics(); passed &= gTest.assessConvergenceSingleChain( v, maxBurnin ); } } return passed; }
/** open the file stream for printing */ void AbstractFileMonitor::openStream(void) { RbFileManager f = RbFileManager(working_file_name); f.createDirectoryForFile(); // open the stream to the file if ( append == true ) { out_stream.open( working_file_name.c_str(), std::fstream::in | std::fstream::out | std::fstream::app); } else { out_stream.open( working_file_name.c_str(), std::fstream::out); out_stream.close(); out_stream.open( working_file_name.c_str(), std::fstream::in | std::fstream::out); } // out_stream.close(); }
/** * This method simply writes a character data object into a file in Fasta format. * * \param[in] fileName The name of the file into which the objects is to be written. * \param[in] data The character data object which is written out. */ void FastaWriter::writeData(std::string const &fileName, const AbstractHomologousDiscreteCharacterData &data) { // the filestream object std::fstream outStream; RbFileManager f = RbFileManager(fileName); f.createDirectoryForFile(); // open the stream to the file outStream.open( fileName.c_str(), std::fstream::out ); const std::vector<Taxon> &taxa = data.getTaxa(); for (std::vector<Taxon>::const_iterator it = taxa.begin(); it != taxa.end(); ++it) { if ( !data.isTaxonExcluded( it->getName() ) ) { const AbstractDiscreteTaxonData &taxon = data.getTaxonData( it->getName() ); outStream << ">" << it->getName() << std::endl; size_t nChars = taxon.getNumberOfCharacters(); for (size_t i = 0; i < nChars; ++i) { if ( !data.isCharacterExcluded( i ) ) { const CharacterState &c = taxon.getCharacter( i ); outStream << c.getStringValue(); } } outStream << std::endl; } } // close the stream outStream.close(); }
/** * Set the file extension. */ void AbstractFileMonitor::addFileExtension(const std::string &s, bool dir) { // compute the working filename if ( dir == false ) { RbFileManager fm = RbFileManager(filename); working_file_name = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + s + "." + fm.getFileExtension(); } else { RbFileManager fm = RbFileManager(filename); working_file_name = fm.getFilePath() + fm.getPathSeparator() + s + fm.getPathSeparator() + fm.getFileName(); } }
void PowerPosteriorAnalysis::summarizeStones( void ) { // create the directory if necessary RbFileManager f = RbFileManager(filename); f.createDirectoryForFile(); std::ofstream outStream; outStream.open( filename.c_str(), std::fstream::out); outStream << "state\t" << "power\t" << "likelihood" << std::endl; /* Append each stone */ for (size_t idx = 0; idx < powers.size(); ++idx) { RbFileManager fm = RbFileManager(filename); std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension(); RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName); // read the i-th stone std::ifstream inStream; inStream.open( f.getFullFileName().c_str(), std::fstream::in); if (inStream.is_open()) { bool header = true; std::string line = ""; while ( std::getline (inStream,line) ) { // we need to skip the header line if ( header == true ) { header = false; } else { outStream << line << std::endl; } } inStream.close(); } else { std::cerr << "Problem reading stone " << idx+1 << " from file " << stoneFileName << "." << std::endl; } } }
void RevBayesCore::PosteriorPredictiveSimulation::run( int thinning ) { // some general constant variables RbFileManager fm = RbFileManager( directory ); const std::string path_separator = fm.getPathSeparator(); // this is where we need to implement the posterior predictive simulation size_t n_samples = traces[0].size(); size_t n_traces = traces.size(); std::vector<DagNode*> nodes = model.getDagNodes(); size_t sim_pid_start = size_t(floor( (double(pid) / num_processes * (n_samples/double(thinning) ) ) ) ); size_t sim_pid_end = std::max( int(sim_pid_start), int(floor( (double(pid+1) / num_processes * (n_samples/double(thinning) ) ) ) - 1) ); for (size_t i=sim_pid_start; i<=sim_pid_end; ++i) { // create a new directory name for this simulation std::stringstream s; s << directory << path_separator << "posterior_predictive_sim_" << (i+1); std::string sim_directory_name = s.str(); // now for the numerical parameters for ( size_t j=0; j<n_traces; ++j ) { std::string parameter_name = traces[j].getParameterName(); // iterate over all DAG nodes (variables) for ( std::vector<DagNode*>::iterator it = nodes.begin(); it!=nodes.end(); ++it ) { DagNode *the_node = *it; if ( the_node->getName() == parameter_name ) { // set the value for the variable with the i-th sample the_node->setValueFromString( traces[j].objectAt( i ) ); } } } // next we need to simulate the data and store it // iterate over all DAG nodes (variables) for ( std::vector<DagNode*>::iterator it = nodes.begin(); it!=nodes.end(); ++it ) { DagNode *the_node = *it; if ( the_node->isClamped() == true ) { // redraw new values the_node->redraw(); // we need to store the new simulated data the_node->writeToFile(sim_directory_name); } } } // end for over all samples }
/** * Combine output for the monitor. * Overwrite this method for specialized behavior. */ void AbstractFileMonitor::combineReplicates( size_t n_reps ) { if ( enabled == true ) { std::fstream combined_output_stream; int sample_number = 0; // open the stream to the file combined_output_stream.open( filename.c_str(), std::fstream::out); combined_output_stream.close(); combined_output_stream.open( filename.c_str(), std::fstream::in | std::fstream::out); for (size_t i=0; i<n_reps; ++i) { std::stringstream ss; ss << "_run_" << (i+1); std::string s = ss.str(); RbFileManager fm = RbFileManager(filename); std::string current_file_name = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + s + "." + fm.getFileExtension(); RbFileManager current_fm = RbFileManager(current_file_name); std::ifstream current_input_stream; if ( current_fm.openFile(current_input_stream) == false ) { throw RbException( "Could not open file '" + current_file_name + "'." ); } std::string read_line = ""; size_t lines_skipped = 0; size_t lines_to_skip = 1; while (std::getline(current_input_stream,read_line)) { ++lines_skipped; if ( lines_skipped <= lines_to_skip) { if ( i == 0 ) { // write output combined_output_stream << read_line; // add a new line combined_output_stream << std::endl; } continue; } std::vector<std::string> fields; StringUtilities::stringSplit(read_line, separator, fields); // add the current sample number combined_output_stream << sample_number; ++sample_number; for (size_t j=1; j<fields.size(); ++j) { // add a separator before every new element combined_output_stream << separator; // write output combined_output_stream << fields[j]; } // add a new line combined_output_stream << std::endl; }; fm.closeFile( current_input_stream ); } combined_output_stream.close(); } }
void PowerPosteriorAnalysis::runStone(size_t idx, size_t gen) { // create the directory if necessary RbFileManager fm = RbFileManager(filename); std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension(); RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName); f.createDirectoryForFile(); std::fstream outStream; outStream.open( f.getFullFileName().c_str(), std::fstream::out); outStream << "state\t" << "power\t" << "likelihood" << std::endl; // reset the counters for the move schedules sampler->reset(); // if ( sampler->getCurrentGeneration() == 0 ) // { // } /* Reset the monitors */ // for (size_t i=0; i<replicates; ++i) // { // for (size_t j=0; i<runs[i].getMonitors().size(); i++) // { // runs[i].getMonitors()[j].reset( kIterations); // } // } // reset the stopping rules // for (size_t i=0; i<rules.size(); ++i) // { // rules[i].runStarted(); // } size_t burnin = size_t( ceil( 0.25*gen ) ); size_t printInterval = size_t( round( fmax(1,gen/20.0) ) ); size_t digits = size_t( ceil( log10( powers.size() ) ) ); /* Run the chain */ if ( processActive ) { std::cout << "Step "; for (size_t d = size_t( ceil( log10( idx+1.1 ) ) ); d < digits; d++ ) { std::cout << " "; } std::cout << (idx+1) << " / " << powers.size(); std::cout << "\t\t"; } // set the power of this sampler sampler->setLikelihoodHeat( powers[idx] ); sampler->setStoneIndex( idx ); // Monitor sampler->startMonitors(gen); sampler->monitor(0); double p = powers[idx]; for (size_t k=1; k<=gen; k++) { if ( processActive ) { if ( k % printInterval == 0 ) { std::cout << "**"; std::cout.flush(); } } sampler->nextCycle( true ); // Monitor sampler->monitor(k); // sample the likelihood if ( k > burnin && k % sampleFreq == 0 ) { // compute the joint likelihood double likelihood = sampler->getModelLnProbability(); outStream << k << "\t" << p << "\t" << likelihood << std::endl; } } if ( processActive ) { std::cout << std::endl; } outStream.close(); }
void PosteriorPredictiveAnalysis::runAll(size_t gen) { // print some information to the screen but only if we are the active process if ( process_active == true ) { std::cout << std::endl; std::cout << "Running posterior predictive analysis ..." << std::endl; } // create the directory if necessary RbFileManager fm = RbFileManager( directory ); if ( fm.testFile() == false && fm.testDirectory() == false ) { std::string errorStr = ""; fm.formatError(errorStr); throw RbException("Could not find file or path with name \"" + directory + "\""); } // set up a vector of strings containing the name or names of the files to be read std::vector<std::string> dir_names; if ( fm.isDirectory() == true ) { fm.setStringWithNamesOfFilesInDirectory( dir_names, false ); } else { throw RbException( "\"" + directory + "\" is not a directory."); } size_t num_runs = dir_names.size(); processors_per_likelihood = ceil( double(num_processes) / num_runs ); size_t run_pid_start = floor( pid / double(num_processes) * num_runs ); size_t run_pid_end = floor( (pid+1) / double(num_processes) * num_runs ); if ( run_pid_start == run_pid_end ) { ++run_pid_end; } // int number_processes_per_run = int(run_pid_end) - int(run_pid_start) + 1; // set the processors for this analysis size_t active_proc = floor( pid / double(processors_per_likelihood) ) * processors_per_likelihood; template_sampler.setActivePID( active_proc, processors_per_likelihood ); #ifdef RB_MPI MPI_Comm analysis_comm; MPI_Comm_split(MPI_COMM_WORLD, active_proc, pid, &analysis_comm); #endif for ( size_t i = run_pid_start; i < run_pid_end; ++i) { // size_t run_pid_start = size_t(floor( double(i) / num_processes * num_runs ) ); // size_t run_pid_end = std::max( int(run_pid_start), int(floor( double(i+1) / num_processes * num_runs ) ) - 1); // create an independent copy of the analysis MonteCarloAnalysis *current_analysis = template_sampler.clone(); current_analysis->disableScreenMonitors( true ); // get the model of the analysis Model* current_model = current_analysis->getModel().clone(); // get the DAG nodes of the model std::vector<DagNode*> ¤t_nodes = current_model->getDagNodes(); // initialize values from files for (size_t j = 0; j < current_nodes.size(); ++j) { DagNode *the_node = current_nodes[j]; if ( the_node->isClamped() == true ) { the_node->setValueFromFile( dir_names[i] ); } } RbFileManager tmp = RbFileManager( dir_names[i] ); // now set the model of the current analysis current_analysis->setModel( current_model ); // set the monitor index current_analysis->addFileMonitorExtension(tmp.getLastPathComponent(), true); // print some info if ( process_active == true ) { size_t digits = size_t( ceil( log10( num_runs ) ) ); std::cout << "Sim "; for (size_t d = size_t( ceil( log10( i+1.1 ) ) ); d < digits; ++d ) { std::cout << " "; } std::cout << (i+1) << " / " << num_runs; std::cout << std::endl; } // run the i-th analysis #ifdef RB_MPI runSim(current_analysis, gen, analysis_comm); #else runSim(current_analysis, gen); #endif // free memory delete current_analysis; } #ifdef RB_MPI MPI_Comm_free(&analysis_comm); // wait until all chains complete MPI::COMM_WORLD.Barrier(); #endif }