Turi Create  4.0
turi::table_printer Class Reference

#include <core/logging/table_printer/table_printer.hpp>

Public Member Functions

 table_printer ()
 
 table_printer (const std::vector< std::pair< std::string, size_t > > &_format, size_t track_interval=1)
 
 ~table_printer ()
 
void set_output_stream (std::ostream &out_stream)
 
void print_header () const
 
void print_line_break () const
 
void print_footer () const
 
template<typename... Args>
void print_row (const Args &... columns) const
 
template<typename T >
void print_row (const std::vector< T > &row_string) const
 
template<typename... Args>
GL_HOT_INLINE void print_timed_progress_row (const Args &... columns)
 
template<typename... Args>
GL_HOT_INLINE void print_progress_row (size_t tick, const Args &... columns)
 
GL_HOT_INLINE void print_progress_row_strs (size_t tick, const std::vector< std::string > &cols)
 
double elapsed_time () const
 
sframe get_tracked_table ()
 

Detailed Description

A simple table printer for consistent information.

The constructor takes a list of (column name, width) strings. If any column name is longer than the specified width, then the column is sized to just fit the header.

The header is printed with print_header().

Each row is shown with print_row(...), where ... contains the arguments of each row. If this doesn't

numeric: double precision is printed so that it fits in the proper row width. It is recommended that columns of floats be at least of width 8. integers are printed without any decimal point.

strings: strings are printed as is. If they are longer than the specified line, they are truncated with a "..." printed after them.

progress_time instance: This tells the printer to print the elapsed time. If progress_time() is passed in without any arguments, then it prints the time elapsed since the class was constructed. If progress_time() is constructed with a timer object or the elapsed number of seconds, then that is printed. See the example below for more information.

The footer is printed with print_footer().

Example:

 progress_table_printer table( { {"Iteration", 0}, {"Time", 10}, {"RMSE", 8}, {"Top String", 16} } );

 table.print_header();

 table.print_row(0, progress_time(),           1e6,   "Alphabetical.");
 table.print_row(1, progress_time(),           10,    "Alphabet soup.");
 table.print_row(2, progress_time(0.1),        1,     "Mine!!!!");
 table.print_row(4, progress_time(100),        0.1,   "Now it's a really long string.");
 table.print_row(5, progress_time(1000),       0.01,  "Yours!!!!");
 table.print_row(6, progress_time(1000.0001),  0.001, "");
 table.print_row(7, progress_time(5e5),        1e-6,  "Turi");

 table.print_row("FINAL", progress_time(5e6), 1e-6, "Turi");

 table.print_footer();

This prints out the table:

 +-----------+------------+----------+------------------+
 | Iteration | Time       | RMSE     | Top String       |
 +-----------+------------+----------+------------------+
 | 0         | 15us       | 1000000  | Alphabetical.    |
 | 1         | 93us       | 10       | Alphabet soup.   |
 | 2         | 100ms      | 1        | Mine!!!!         |
 | 4         | 1m 40s     | 0.1      | Now it's a rea...|
 | 5         | 16m 40s    | 0.01     | Yours!!!!        |
 | 6         | 16m 40s    | 0.001    |                  |
 | 7         | 5d 18h 53m | 1e-06    | Turi             |
 | FINAL     | 57d 20h    | 1e-06    | Turi             |
 +-----------+------------+----------+------------------+

TIMED PROGRESS PRINTING

Using the print_progress_row(...) method instead of print_row(...), you can control how often progress messages are printed. It automatically adjusts the printing interval so that the rows are printed, on average, every 1-5 seconds. The first argument to print_progress_row is the tick variable, which must simply be a monotonically increasing integer.

Example 1:

Code:

 table_printer table( { {"Iteration", 0}, {"Elapsed Time", 10}, {"RMSE", 8} } );

 table.print_header();

 for(size_t i = 0; i < 2000; ++i) {
   table.print_progress_row(i, i, progress_time(), std::exp(-double(i) / 5000));
   usleep(8000); // Sleep for 8 milliseconds
 }

 table.print_row("FINAL", progress_time(), 1e-6);

 table.print_footer();

Output:

+-----------+--------------+----------+
| Iteration | Elapsed Time | RMSE     |
+-----------+--------------+----------+
| 0         | 30us         | 1        |
| 500       | 4.05s        | 0.904837 |
| 1000      | 8.10s        | 0.818731 |
| 1500      | 12.15s       | 0.740818 |
| FINAL     | 16.21s       | 1e-06    |
+-----------+--------------+----------+

Example 2:

Code:

random::seed(0);

table_printer table( { {"samples_processed", 0}, {"Elapsed Time", 10}, {"A value", 8} } );

table.print_header();

size_t proc = 0;

for(size_t i = 0; i < 50000; ++i) {
  table.print_progress_row(proc, proc, progress_time(), i);
  proc += random::fast_uniform<size_t>(0, 100);
  usleep(100);  // sleep for 200 microseconds
}

table.print_row("FINAL", progress_time(), 1e-6);

table.print_footer();

Output:

+-------------------+--------------+----------+
| samples_processed | Elapsed Time | A value  |
+-------------------+--------------+----------+
| 0                 | 71us         | 0        |
| 500081            | 1.61s        | 10009    |
| 1000094           | 3.16s        | 19906    |
| 1500049           | 4.78s        | 29980    |
| 2000052           | 6.40s        | 39882    |
| 2500054           | 8.03s        | 49971    |
| FINAL             | 8.04s        | 1e-06    |
+-------------------+--------------+----------+

TRACKING

Optionally, the table printer can track the calls to the progress row functions by storing each call as a row in an SFrame that can be retrieved at the end. This is useful for recording the training statistics of an algorithm for reporting at the end with get_tracked_table().

Not every call to the progress functions are printed; the on-screen printing is designed for a pleasing visual report. Tracking, however, is determined by an interval specified in the constructor – a row is recorded in the sframe every track_interval calls to one of the progress printing calls. It may be turned off by setting track_interval to be 0.

Definition at line 200 of file table_printer.hpp.

Constructor & Destructor Documentation

◆ table_printer() [1/2]

turi::table_printer::table_printer ( )
inline

Constructor. Must be initialized elsewise using copy assignment ops.

Definition at line 204 of file table_printer.hpp.

◆ table_printer() [2/2]

turi::table_printer::table_printer ( const std::vector< std::pair< std::string, size_t > > &  _format,
size_t  track_interval = 1 
)

Constructor. Sets up the columns.

Parameters
_formatA vector of (column name, width) pairs. If the length of column name is larger than width, than width is set to the column name. See class header for examples.

The track_interval determines how often a result is stored in the SFrame tracking the row progress. Every track_interval calls to get_tracked_table, the row is written to the sframe. If track_interval is 0, then tracking is turned off.

◆ ~table_printer()

turi::table_printer::~table_printer ( )

Need to clean up some things.

Member Function Documentation

◆ elapsed_time()

double turi::table_printer::elapsed_time ( ) const

Returns the elapsed time since class creation. This is the value used if progress_time() is passed in to print_row.

◆ get_tracked_table()

sframe turi::table_printer::get_tracked_table ( )

Returns the current tracked table. Any rows added after this is called will cause the table to be cleared and all rows to be added to another table.

◆ print_footer()

void turi::table_printer::print_footer ( ) const

Prints the footer.

Example output:

+-----------+------------+----------+------------------+

◆ print_header()

void turi::table_printer::print_header ( ) const

Prints the header.

Example output:

+-----------+------------+----------+------------------+
| Iteration | Time       | RMSE     | Top String       |
+-----------+------------+----------+------------------+

◆ print_line_break()

void turi::table_printer::print_line_break ( ) const

Prints a line break.

Example output:

+-----------+------------+----------+------------------+

◆ print_progress_row()

template<typename... Args>
GL_HOT_INLINE void turi::table_printer::print_progress_row ( size_t  tick,
const Args &...  columns 
)
inline

Print a row associated with the progress of an algorithm.

The first argument is the tick, which can be something like samples processed or iterations. The time at which to update this is automatically determined based on the first 2 (or more, if calls are extremely frequent) calls.

Example output:

table.print_row(5,  5, progress_time(1000), 0.01,  "Yours!!!!");

Gives

| 5         | 16m 40s    | 0.01     | Yours!!!!        |

Definition at line 360 of file table_printer.hpp.

◆ print_progress_row_strs()

GL_HOT_INLINE void turi::table_printer::print_progress_row_strs ( size_t  tick,
const std::vector< std::string > &  cols 
)
inline

Print a row associated with the progress of an algorithm.

The first argument is the tick, which can be something like samples processed or iterations. The time at which to update this is automatically determined based on the first 2 (or more, if calls are extremely frequent) calls.

Definition at line 386 of file table_printer.hpp.

◆ print_row() [1/2]

template<typename... Args>
void turi::table_printer::print_row ( const Args &...  columns) const
inline

Print a row.

Example output:

table.print_row(5, progress_time(1000), 0.01,  "Yours!!!!");

Gives

| 5         | 16m 40s    | 0.01     | Yours!!!!        |

Definition at line 271 of file table_printer.hpp.

◆ print_row() [2/2]

template<typename T >
void turi::table_printer::print_row ( const std::vector< T > &  row_string) const
inline

Same as print row but take a vector of one particular type. May be flexible type.

Definition at line 287 of file table_printer.hpp.

◆ print_timed_progress_row()

template<typename... Args>
GL_HOT_INLINE void turi::table_printer::print_timed_progress_row ( const Args &...  columns)
inline

Print a row associated with the progress of an algorithm, but print at most once a second.

Definition at line 314 of file table_printer.hpp.

◆ set_output_stream()

void turi::table_printer::set_output_stream ( std::ostream &  out_stream)
inline

Sets the output stream to something custom. Only a reference to this stream is stored, so the stream must be in existance for as long as this class is.

Definition at line 229 of file table_printer.hpp.


The documentation for this class was generated from the following file: