Skip to content

Commit

Permalink
Fixed pyFAST bug with Pipeline::getPipelineOutputData and Pipeline::g…
Browse files Browse the repository at this point in the history
…etAllPipelineOutputData.

Also created Progress utility class and used that in pipeline.
  • Loading branch information
smistad committed Nov 17, 2023
1 parent ad10be4 commit 33422d2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
18 changes: 18 additions & 0 deletions source/FAST/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,24 @@ std::shared_ptr<Window> Pipeline::getWindow() {
return m_window;
}

DataObject::pointer Pipeline::getPipelineOutputData(std::string name) {
Progress progress(1000);
progress.setText("Running pipeline");
return getPipelineOutputData(name, [&](float x) {
// x is between 0 and 1
progress.update(round(x*1000));
});
}

std::map<std::string, DataObject::pointer> Pipeline::getAllPipelineOutputData() {
Progress progress(1000);
progress.setText("Running pipeline");
return getAllPipelineOutputData([&](float x) {
// x is between 0 and 1
progress.update(round(x*1000));
});
}

PipelineWidget::PipelineWidget(Pipeline pipeline, QWidget* parent) : QToolBox(parent) {
auto processObjects = pipeline.getProcessObjects();
for(auto object : processObjects) {
Expand Down
28 changes: 26 additions & 2 deletions source/FAST/Pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class FAST_EXPORT Pipeline {
* @return
*/
std::map<std::string, std::string> getRequiredPipelineInputData() const;

#ifndef SWIG
/**
* @brief Get all data objects which has been declared as output data in this pipeline
*
Expand All @@ -121,7 +123,19 @@ class FAST_EXPORT Pipeline {
* @param progressFunction
* @return map of pipeline output data
*/
std::map<std::string, DataObject::pointer> getAllPipelineOutputData(std::function<void(float)> progressFunction = nullptr);
std::map<std::string, DataObject::pointer> getAllPipelineOutputData(std::function<void(float)> progressFunction);
#endif
/**
* @brief Get all data objects which has been declared as output data in this pipeline
*
* Pipeline output data is declared using "PipelineOutputData <name> <process object> <output port id>"
* This function will block until done.
*
* @return map of pipeline output data
*/
std::map<std::string, DataObject::pointer> getAllPipelineOutputData();

#ifndef SWIG
/**
* @brief Get data object which has been declared as output data in this pipeline with a given name.
*
Expand All @@ -131,7 +145,17 @@ class FAST_EXPORT Pipeline {
* @param progressFunction
* @return pipeline output data object
*/
DataObject::pointer getPipelineOutputData(std::string name, std::function<void(float)> progressFunction = nullptr);
DataObject::pointer getPipelineOutputData(std::string name, std::function<void(float)> progressFunction);
#endif
/**
* @brief Get data object which has been declared as output data in this pipeline with a given name.
*
* Pipeline output data is declared using "PipelineOutputData <name> <process object> <output port id>"
* This function will block until done.
*
* @return pipeline output data object
*/
DataObject::pointer getPipelineOutputData(std::string name);

/**
* @brief Check whether pipeline has a window specified or not
Expand Down
78 changes: 78 additions & 0 deletions source/FAST/Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
#endif
#include <zip/zip.h>
Expand Down Expand Up @@ -1140,4 +1142,80 @@ std::string roundToString(float value, int decimals) {
stream << std::fixed << std::setprecision(decimals) << value;
return stream.str();
}

Progress::Progress(uint64_t max, bool print, bool autoStart) {
m_max = max;
m_print = print;
if(autoStart)
start();
}

void Progress::start() {
m_startTime = std::chrono::high_resolution_clock::now();
m_started = true;
}

static int getConsoleWidth() {
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;

GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return columns;
#else
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col;
#endif
}

void Progress::update(uint64_t current) {
if(!m_started)
start();
m_current = current;

const float percent = getPercent();
std::chrono::duration<float, std::milli> duration = std::chrono::high_resolution_clock::now() - m_startTime;
const float speed = (duration.count() / 1000.0f)/percent;
const float ETA = (speed * (1.0f - percent) / 60.0f);
if(m_print) {
std::cout << "\r"; // Replace line
std::stringstream ss;
ss << std::setprecision(1)
<< std::fixed;
//ss << "] " << (int)(percent * 100.0f) << "% | " << m_current/(1024*1024) << "MB / " << m_max/(1024*1024) << "MB | ETA " << ETA << " mins";
ss << "] " << (int)(percent * 100.0f) << "% | ETA " << ETA << " mins";
std::string startString = m_text + (m_text.empty() ? "" : " ") + "[";
const int totalWidth = getConsoleWidth();
const int progressbarWidth = totalWidth - ss.str().size() - startString.size() - 1; // have to leave last line open to avoid jumping to next line (windows)
std::cout << startString;
int pos = progressbarWidth * percent;
for (int i = 0; i < progressbarWidth; ++i) {
if (i < pos) std::cout << "=";
else if (i == pos) std::cout << ">";
else std::cout << " ";
}
std::cout << ss.str();
std::cout.flush();
if(percent == 1.0f) {
std::cout << "\n";
std::cout.flush();
}
}
}

float Progress::getPercent() const {
return (float)m_current / m_max;
}

uint64_t Progress::getCurrent() const {
return m_current;
}

void Progress::setText(std::string text) {
m_text = text;
}

} // end namespace fast
19 changes: 19 additions & 0 deletions source/FAST/Utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,24 @@ FAST_EXPORT std::string roundToString(double value, int decimals = 0);
*/
FAST_EXPORT std::string roundToString(float value, int decimals = 0);

/**
* @brief A utility class for keeping track of progress and printing progress to the console.
*/
class FAST_EXPORT Progress {
public:
explicit Progress(uint64_t max, bool print = true, bool autoStart = false);
void start();
void update(uint64_t current);
float getPercent() const;
void setText(std::string text);
uint64_t getCurrent() const;
private:
std::chrono::high_resolution_clock::time_point m_startTime;
bool m_print = true;
bool m_started = false;
uint64_t m_max;
uint64_t m_current;
std::string m_text = "";
};
} // end namespace fast

0 comments on commit 33422d2

Please sign in to comment.