Skip to content

Commit

Permalink
Add progress bar and verbose flag to lfmcmc (#43)
Browse files Browse the repository at this point in the history
* Add progress bar and verbose flag to lfmcmc

* Update verbose_on/off to return lfmcmc object
  • Loading branch information
apulsipher authored Dec 16, 2024
1 parent 2062297 commit e673f04
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
38 changes: 38 additions & 0 deletions epiworld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ class LFMCMC {
std::chrono::time_point<std::chrono::steady_clock> m_start_time;
std::chrono::time_point<std::chrono::steady_clock> m_end_time;

// Timing
// std::chrono::milliseconds
std::chrono::duration<epiworld_double,std::micro> m_elapsed_time =
std::chrono::duration<epiworld_double,std::micro>::zero();
Expand All @@ -1245,6 +1246,10 @@ class LFMCMC {

void chrono_start();
void chrono_end();

// Progress
bool verbose = true;
Progress progress_bar;

public:

Expand Down Expand Up @@ -1316,6 +1321,8 @@ class LFMCMC {
std::vector< epiworld_double > get_mean_stats();

// Printing
LFMCMC<TData> & verbose_off();
LFMCMC<TData> & verbose_on();
void print(size_t burnin = 0u) const;

};
Expand Down Expand Up @@ -1523,6 +1530,7 @@ class LFMCMC {
std::chrono::time_point<std::chrono::steady_clock> m_start_time;
std::chrono::time_point<std::chrono::steady_clock> m_end_time;

// Timing
// std::chrono::milliseconds
std::chrono::duration<epiworld_double,std::micro> m_elapsed_time =
std::chrono::duration<epiworld_double,std::micro>::zero();
Expand All @@ -1536,6 +1544,10 @@ class LFMCMC {

void chrono_start();
void chrono_end();

// Progress
bool verbose = true;
Progress progress_bar;

public:

Expand Down Expand Up @@ -1607,6 +1619,8 @@ class LFMCMC {
std::vector< epiworld_double > get_mean_stats();

// Printing
LFMCMC<TData> & verbose_off();
LFMCMC<TData> & verbose_on();
void print(size_t burnin = 0u) const;

};
Expand Down Expand Up @@ -1882,6 +1896,13 @@ inline void LFMCMC<TData>::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
Expand Down Expand Up @@ -1938,6 +1959,9 @@ inline void LFMCMC<TData>::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
Expand Down Expand Up @@ -2412,6 +2436,20 @@ inline std::vector< epiworld_double > LFMCMC<TData>::get_mean_stats()

}

template<typename TData>
inline LFMCMC<TData> & LFMCMC<TData>::verbose_off()
{
verbose = false;
return *this;
}

template<typename TData>
inline LFMCMC<TData> & LFMCMC<TData>::verbose_on()
{
verbose = true;
return *this;
}

#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions include/epiworld/math/lfmcmc/lfmcmc-bones.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class LFMCMC {
std::chrono::time_point<std::chrono::steady_clock> m_start_time;
std::chrono::time_point<std::chrono::steady_clock> m_end_time;

// Timing
// std::chrono::milliseconds
std::chrono::duration<epiworld_double,std::micro> m_elapsed_time =
std::chrono::duration<epiworld_double,std::micro>::zero();
Expand All @@ -183,6 +184,10 @@ class LFMCMC {

void chrono_start();
void chrono_end();

// Progress
bool verbose = true;
Progress progress_bar;

public:

Expand Down Expand Up @@ -254,6 +259,8 @@ class LFMCMC {
std::vector< epiworld_double > get_mean_stats();

// Printing
LFMCMC<TData> & verbose_off();
LFMCMC<TData> & verbose_on();
void print(size_t burnin = 0u) const;

};
Expand Down
24 changes: 24 additions & 0 deletions include/epiworld/math/lfmcmc/lfmcmc-meat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ inline void LFMCMC<TData>::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
Expand Down Expand Up @@ -319,6 +326,9 @@ inline void LFMCMC<TData>::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
Expand Down Expand Up @@ -544,4 +554,18 @@ inline std::vector< epiworld_double > LFMCMC<TData>::get_mean_stats()

}

template<typename TData>
inline LFMCMC<TData> & LFMCMC<TData>::verbose_off()
{
verbose = false;
return *this;
}

template<typename TData>
inline LFMCMC<TData> & LFMCMC<TData>::verbose_on()
{
verbose = true;
return *this;
}

#endif

0 comments on commit e673f04

Please sign in to comment.