Commit 0fa4cc78 authored by David Frank's avatar David Frank
Browse files

Minor improvments to DataContainerFormatter

* Pass a default config to DataContainer.format, such that it can be
  passed in from the outside
* Move `format_config` into separate file to not create dependency cyles
* Add functionality to suppress or round small numbers to 0, such that
  instead of -1.49012e-08, only 0 is printed
parent 51fdb28e
Pipeline #766487 passed with stages
in 22 minutes and 19 seconds
......@@ -5,6 +5,7 @@ set(MODULE_HEADERS
Cloneable.h
Utilities/Badge.hpp
Utilities/DataContainerFormatter.hpp
Utilities/FormatConfig.h
Utilities/Statistics.hpp
Utilities/TypeCasts.hpp
Descriptors/DataDescriptor.h
......
#include "DataContainer.h"
#include "DataContainerFormatter.hpp"
#include "FormatConfig.h"
#include "DataHandlerCPU.h"
#include "DataHandlerMapCPU.h"
#include "BlockDescriptor.h"
......@@ -509,9 +510,9 @@ namespace elsa
}
template <typename data_t>
void DataContainer<data_t>::format(std::ostream& os) const
void DataContainer<data_t>::format(std::ostream& os, format_config cfg) const
{
DataContainerFormatter<data_t> fmt;
DataContainerFormatter<data_t> fmt{cfg};
fmt.format(os, *this);
}
......
......@@ -8,6 +8,7 @@
#include "DataContainerIterator.h"
#include "Error.h"
#include "Expression.h"
#include "FormatConfig.h"
#include "TypeCasts.hpp"
#ifdef ELSA_CUDA_VECTOR
......@@ -20,7 +21,6 @@
namespace elsa
{
/**
* @brief class representing and storing a linearized n-dimensional signal
*
......@@ -444,15 +444,15 @@ namespace elsa
friend constexpr auto evaluateOrReturn(Operand const& operand);
/// write a pretty-formatted string representation to stream
void format(std::ostream& os) const;
void format(std::ostream& os, format_config cfg = {}) const;
/**
* @brief Factory function which returns GPU based DataContainers
*
* @return the GPU based DataContainer
*
* Note that if this function is called on a container which is already GPU based, it will
* throw an exception.
* Note that if this function is called on a container which is already GPU based, it
* will throw an exception.
*/
DataContainer loadToGPU();
......
......@@ -2,6 +2,7 @@
#include "elsaDefines.h"
#include "DataContainer.h"
#include "FormatConfig.h"
#include <iomanip>
#include <iostream>
......@@ -27,29 +28,7 @@ namespace elsa
template <typename data_t>
class DataContainerFormatter
{
public:
/**
* Formatting output configuration.
*/
struct format_config {
/// for too many elements, abbreviate the output
bool summary_enabled = true;
/// number of summary items to display - this also triggers the summary enabling
/// if theres more than 2 * summary_items elements.
index_t summary_items = 6;
/// what is inserted between the summary items
std::string summary_elem = "...";
// what is inserted vertically between summary items
std::string summary_elem_vertical = "\u22EE";
// what's inserted between elements and newlines
std::string separator = ", ";
};
/**
* Create a formatter with default config.
*/
......@@ -273,9 +252,14 @@ namespace elsa
teststream.str("");
teststream.clear();
auto&& elem = dc[idx];
auto&& elem = config.suppress_close_to_zero
&& std::abs(dc[idx]) < config.suppression_epsilon
? static_cast<data_t>(0)
: dc[idx];
teststream << elem;
auto len = static_cast<int>(teststream.tellp());
if (len > maxlen) {
maxlen = len;
}
......@@ -283,11 +267,14 @@ namespace elsa
auto streamflags = teststream.flags();
return [maxlen, streamflags](std::ostream & os, const T& elem) -> auto&
return [
maxlen, streamflags, do_suppress = config.suppress_close_to_zero,
eps = config.suppression_epsilon
](std::ostream & os, const T& elem) -> auto&
{
os.flags(streamflags);
os << std::setw(maxlen);
os << elem;
os << (do_suppress && std::abs(elem) < eps ? static_cast<data_t>(0) : elem);
return os;
};
} else {
......
#pragma once
#include "elsaDefines.h"
#include <string>
namespace elsa
{
/**
* Formatting output configuration.
*/
struct format_config {
/// for too many elements, abbreviate the output
bool summary_enabled = true;
/// number of summary items to display - this also triggers the summary enabling
/// if theres more than 2 * summary_items elements.
index_t summary_items = 6;
/// what is inserted between the summary items
std::string summary_elem = "...";
/// what is inserted vertically between summary items
std::string summary_elem_vertical = "\u22EE";
/// what's inserted between elements and newlines
std::string separator = ", ";
/// if a value is smaller than some epsilon (`suppression_epsilon`), just print 0
/// instead
bool suppress_close_to_zero = false;
/// epsilon value for suppressing small numbers
real_t suppression_epsilon = static_cast<real_t>(0.0000001);
};
} // namespace elsa
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment