Skip to content

Commit

Permalink
Only show time remaining after 3 seconds. And now only update it once…
Browse files Browse the repository at this point in the history
… per second to reduce the up/down jitter
  • Loading branch information
akenmorris committed Feb 3, 2023
1 parent 49ed309 commit 987e4f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
13 changes: 5 additions & 8 deletions Applications/shapeworks/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,23 @@ bool Example::execute(const optparse::Values &options, SharedCommandData &shared
static void setup_callbacks(bool show_progress, bool xml_status) {
if (show_progress) {
auto progress_callback = [](double progress, std::string message) {
// show percentage complete
std::cout << fmt::format("{} ({:.1f}%) ", message, progress) << "\r";
// show status message and percentage complete
std::cout << fmt::format("{} ({:.1f}%) \r", message, progress);
std::cout.flush();
};
Logging::Instance().set_progress_callback(progress_callback);
}

if (xml_status) {
auto progress_callback = [](double progress, std::string message) {
// show percentage complete
std::cout << fmt::format("<xml><status>{}</status><progress>{:.1f}</progress></xml>", message, progress)
<< "\n";
// print status message and percentage complete
std::cout << fmt::format("<xml><status>{}</status><progress>{:.1f}</progress></xml>\n", message, progress);
std::cout.flush();
};
Logging::Instance().set_progress_callback(progress_callback);

auto error_callback = [](std::string message) {
std::cout << fmt::format("<xml><error>{}</error></xml>", message) << "\n";
std::cout << fmt::format("<xml><error>{}</error></xml>\n", message);
std::cout.flush();
};
Logging::Instance().set_error_callback(error_callback);
Expand Down Expand Up @@ -121,8 +120,6 @@ bool OptimizeCommand::execute(const optparse::Values& options, SharedCommandData
bool isProject = StringUtils::hasSuffix(projectFile, "xlsx") || StringUtils::hasSuffix(projectFile, "swproj");

Optimize app;
app.SetShowProgress(show_progress);

setup_callbacks(show_progress, xml_status);

if (isProject) {
Expand Down
24 changes: 11 additions & 13 deletions Libs/Optimize/Optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2204,10 +2204,7 @@ void Optimize::ComputeTotalIterations() {
void Optimize::UpdateProgress() {
auto now = std::chrono::system_clock::now();

// compute time since last update in milliseconds
auto time_since_last_update = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_update_time);

if (time_since_last_update.count() > 100) {
if ((now - m_last_update_time) > std::chrono::milliseconds(100)) {
m_last_update_time = now;
std::chrono::duration<double> elapsed_seconds =
std::chrono::duration_cast<std::chrono::seconds>(now - m_start_time);
Expand All @@ -2230,19 +2227,20 @@ void Optimize::UpdateProgress() {

message = fmt::format("{} : Particles: {}, Iteration: {} / {}", message, num_particles, stage_num_iterations,
stage_total_iterations);
message = fmt::format("{} ({:02d}:{:02d}:{:02d} remaining)", message, hours, minutes, seconds);

if ((now - m_last_remaining_update_time) > std::chrono::seconds(1)) {
m_remaining_time_message = fmt::format("({:02d}:{:02d}:{:02d} remaining)", hours, minutes, seconds);
m_last_remaining_update_time = now;
}

// only show the time remaining if it's been more than 3 seconds
if (elapsed_seconds > std::chrono::seconds(3)) {
message = fmt::format("{} {}", message, m_remaining_time_message);
}

double progress =
static_cast<double>(current_particle_iterations_) * 100 / static_cast<double>(total_particle_iterations_);
SW_PROGRESS(progress, message);
/*
if (m_show_progress) {
// show percentage complete
std::cout << fmt::format("{} ({:.2f}%) ", message, progress) << "\r";
// flush stdout
std::cout.flush();
}
*/
}
}
} // namespace shapeworks
4 changes: 2 additions & 2 deletions Libs/Optimize/Optimize.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ class Optimize {
vnl_vector_fixed<double, 3> TransformPoint(int domain, vnl_vector_fixed<double, 3> input);

void UpdateProgress();
void SetShowProgress(bool show) { m_show_progress = show; }

protected:
//! Set the iteration callback. Derived classes should override to set their own callback
Expand Down Expand Up @@ -466,7 +465,8 @@ class Optimize {

std::chrono::system_clock::time_point m_start_time;
std::chrono::system_clock::time_point m_last_update_time;
bool m_show_progress = false;
std::chrono::system_clock::time_point m_last_remaining_update_time;
std::string m_remaining_time_message;

};

Expand Down

0 comments on commit 987e4f8

Please sign in to comment.