From 70f09486d3759ffeba88b667705e20f05f20fd4c Mon Sep 17 00:00:00 2001 From: Andrew Pulsipher Date: Thu, 12 Dec 2024 10:20:06 -0700 Subject: [PATCH] Add progress bar and verbose flag to lfmcmc --- epiworld.hpp | 24 +++++++++++++++++++ include/epiworld/math/lfmcmc/lfmcmc-bones.hpp | 7 ++++++ include/epiworld/math/lfmcmc/lfmcmc-meat.hpp | 10 ++++++++ 3 files changed, 41 insertions(+) diff --git a/epiworld.hpp b/epiworld.hpp index 5ce8e0ea..c548dd0b 100644 --- a/epiworld.hpp +++ b/epiworld.hpp @@ -1232,6 +1232,7 @@ class LFMCMC { std::chrono::time_point m_start_time; std::chrono::time_point m_end_time; + // Timing // std::chrono::milliseconds std::chrono::duration m_elapsed_time = std::chrono::duration::zero(); @@ -1245,6 +1246,10 @@ class LFMCMC { void chrono_start(); void chrono_end(); + + // Progress + bool verbose = true; + Progress progress_bar; public: @@ -1316,6 +1321,8 @@ class LFMCMC { std::vector< epiworld_double > get_mean_stats(); // Printing + void verbose_off() { verbose = false; }; + void verbose_on() { verbose = true; }; void print(size_t burnin = 0u) const; }; @@ -1523,6 +1530,7 @@ class LFMCMC { std::chrono::time_point m_start_time; std::chrono::time_point m_end_time; + // Timing // std::chrono::milliseconds std::chrono::duration m_elapsed_time = std::chrono::duration::zero(); @@ -1536,6 +1544,10 @@ class LFMCMC { void chrono_start(); void chrono_end(); + + // Progress + bool verbose = true; + Progress progress_bar; public: @@ -1607,6 +1619,8 @@ class LFMCMC { std::vector< epiworld_double > get_mean_stats(); // Printing + void verbose_off() { verbose = false; }; + void verbose_on() { verbose = true; }; void print(size_t burnin = 0u) const; }; @@ -1882,6 +1896,13 @@ inline void LFMCMC::run( for (size_t k = 0u; k < m_n_params; ++k) m_accepted_params[k] = m_initial_params[k]; + // Init progress bar + progress_bar = Progress(m_n_samples, 80); + if (verbose) { + progress_bar.next(); + } + + // Run LFMCMC for (size_t i = 1u; i < m_n_samples; ++i) { // Step 1: Generate a proposal and store it in m_current_params @@ -1938,6 +1959,9 @@ inline void LFMCMC::run( for (size_t k = 0u; k < m_n_params; ++k) m_accepted_params[i * m_n_params + k] = m_previous_params[k]; + if (verbose) { + progress_bar.next(); + } } // End timing diff --git a/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp b/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp index 4ec81ccd..79a90237 100755 --- a/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp +++ b/include/epiworld/math/lfmcmc/lfmcmc-bones.hpp @@ -170,6 +170,7 @@ class LFMCMC { std::chrono::time_point m_start_time; std::chrono::time_point m_end_time; + // Timing // std::chrono::milliseconds std::chrono::duration m_elapsed_time = std::chrono::duration::zero(); @@ -183,6 +184,10 @@ class LFMCMC { void chrono_start(); void chrono_end(); + + // Progress + bool verbose = true; + Progress progress_bar; public: @@ -254,6 +259,8 @@ class LFMCMC { std::vector< epiworld_double > get_mean_stats(); // Printing + void verbose_off() { verbose = false; }; + void verbose_on() { verbose = true; }; void print(size_t burnin = 0u) const; }; diff --git a/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp b/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp index a44b6f0a..caf4b853 100755 --- a/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp +++ b/include/epiworld/math/lfmcmc/lfmcmc-meat.hpp @@ -263,6 +263,13 @@ inline void LFMCMC::run( for (size_t k = 0u; k < m_n_params; ++k) m_accepted_params[k] = m_initial_params[k]; + // Init progress bar + progress_bar = Progress(m_n_samples, 80); + if (verbose) { + progress_bar.next(); + } + + // Run LFMCMC for (size_t i = 1u; i < m_n_samples; ++i) { // Step 1: Generate a proposal and store it in m_current_params @@ -319,6 +326,9 @@ inline void LFMCMC::run( for (size_t k = 0u; k < m_n_params; ++k) m_accepted_params[i * m_n_params + k] = m_previous_params[k]; + if (verbose) { + progress_bar.next(); + } } // End timing