Skip to content

Commit

Permalink
Merge c1b7c10 into 775ee5d
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Jan 6, 2021
2 parents 775ee5d + c1b7c10 commit f211a12
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,8 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s
#### General
- Added a builtin sandboxed header-only version of fmt. The namespace and directory paths were changed to `conduit_fmt` to avoid potential symbol collisions with other codes using fmt. Downstream software can use by including `conduit_fmt/conduit_fmt.h`.
- Added support for using C++11 initializer lists to set Node and DataArray values from numeric arrays. See C++ tutorial docs (https://llnl-conduit.readthedocs.io/en/latest/tutorial_cpp_numeric.html#c-11-initializer-lists) for more details.
- Added a Node::describe() method. This method creates a new node that mirrors the current Node, however each leaf is replaced by summary stats and a truncated display of the values. For use cases with large leaves, printing the describe() output Node is much more helpful for debugging and understanding vs wall of text from other to_string() methods.


#### Relay
- Added Relay IO Handle mode support for `a` (append) and `t` (truncate). Truncate allows you to overwrite files when the handle is opened. The default is append, which preserves prior IO Handle behavior.
Expand Down
202 changes: 202 additions & 0 deletions src/libs/conduit/conduit_data_array.cpp
Expand Up @@ -279,6 +279,81 @@ DataArray<T>::diff_compatible(const DataArray<T> &array, Node &info, const float
return res;
}

//---------------------------------------------------------------------------//
///
/// Summary Stats Helpers
///
//---------------------------------------------------------------------------//

//---------------------------------------------------------------------------//
template <typename T>
T
DataArray<T>::min() const
{
T res = std::numeric_limits<T>::max();
for(index_t i = 0; i < number_of_elements(); i++)
{
const T &val = element(i);
if(val < res)
{
res = val;
}
}

return res;
}

//---------------------------------------------------------------------------//
template <typename T>
T
DataArray<T>::max() const
{
T res = std::numeric_limits<T>::min();
for(index_t i = 0; i < number_of_elements(); i++)
{
const T &val = element(i);
if(val > res)
{
res = val;
}
}

return res;
}


//---------------------------------------------------------------------------//
template <typename T>
T
DataArray<T>::sum() const
{
T res =0;
for(index_t i = 0; i < number_of_elements(); i++)
{
const T &val = element(i);
res += val;
}

return res;
}

//---------------------------------------------------------------------------//
template <typename T>
float64
DataArray<T>::mean() const
{
float64 res =0;
for(index_t i = 0; i < number_of_elements(); i++)
{
const T &val = element(i);
res += val;
}

res = res / float64(number_of_elements());
return res;
}


//---------------------------------------------------------------------------//
template <typename T>
std::string
Expand Down Expand Up @@ -1436,6 +1511,133 @@ DataArray<T>::compact_elements_to(uint8 *data) const
}


//---------------------------------------------------------------------------//
template <typename T>
std::string
DataArray<T>::to_summary_string_default() const
{
return to_summary_string();
}

//---------------------------------------------------------------------------//
template <typename T>
std::string
DataArray<T>::to_summary_string(index_t threshold) const
{
std::ostringstream oss;
to_summary_string_stream(oss, threshold);
return oss.str();
}

//---------------------------------------------------------------------------//
template <typename T>
void
DataArray<T>::to_summary_string_stream(std::ostream &os,
index_t threshold) const
{
// if we are less than or equal to threshold, we use to_yaml
index_t nele = number_of_elements();

if(nele <= threshold)
{
to_yaml_stream(os);
}
else
{
// if above threshold only show threshold # of values
int half = threshold / 2;
int bottom = half;
int top = half;

//
// if odd, show 1/2 +1 first
//

if( (threshold % 2) > 0)
{
bottom++;
}

if(nele > 1)
os << "[";

bool done = (nele == 0);
int idx = 0;

while(!done)
{
// if not first, add a comma prefix
if(idx > 0 )
os << ", ";

switch(m_dtype.id())
{
// ints
case DataType::INT8_ID:
case DataType::INT16_ID:
case DataType::INT32_ID:
case DataType::INT64_ID:
{
os << (int64) element(idx);
break;
}
// uints
case DataType::UINT8_ID:
case DataType::UINT16_ID:
case DataType::UINT32_ID:
case DataType::UINT64_ID:
{
os << (uint64) element(idx);
break;
}
// floats
case DataType::FLOAT32_ID:
case DataType::FLOAT64_ID:
{
std::string fs = utils::float64_to_string((float64)element(idx));
//check for inf and nan
// looking for 'n' covers inf and nan
bool inf_or_nan = fs.find('n') != std::string::npos;

if(inf_or_nan)
os << "\"";

os << fs;

if(inf_or_nan)
os << "\"";
break;
}
default:
{
CONDUIT_ERROR("Leaf type \""
<< m_dtype.name()
<< "\""
<< "is not supported in conduit::DataArray.")
}
}

idx++;

if(idx == bottom)
{
idx = nele - top;
os << ", ...";
}

if(idx == nele)
{
done = true;
}
}

if(nele > 1)
os << "]";
}
}



//-----------------------------------------------------------------------------
//
// -- conduit::DataArray explicit instantiations for supported array types --
Expand Down
19 changes: 17 additions & 2 deletions src/libs/conduit/conduit_data_array.hpp
Expand Up @@ -103,6 +103,14 @@ class CONDUIT_API DataArray
Node &info,
const float64 epsilon = CONDUIT_EPSILON) const;

///
/// Summary Stats Helpers
///
T min() const;
T max() const;
T sum() const;
float64 mean() const;

//-----------------------------------------------------------------------------
// Setters
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -207,7 +215,7 @@ class CONDUIT_API DataArray
#endif

#ifndef CONDUIT_USE_DOUBLE
void setconst std::initializer_list<double> &values);
void set(const std::initializer_list<double> &values);
#endif

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -311,7 +319,14 @@ class CONDUIT_API DataArray
void to_yaml_stream(std::ostream &os) const;

void compact_elements_to(uint8 *data) const;


/// Creates a string repression for printing that limits
/// the number of elements shown to a max number
std::string to_summary_string_default() const;
std::string to_summary_string(index_t threshold=5) const;
void to_summary_string_stream(std::ostream &os,
index_t threshold=5) const;

//-----------------------------------------------------------------------------
// -- stdout print methods ---
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit f211a12

Please sign in to comment.