NGen
|
A multi-dimensional, tagged data frame. More...
#include <mdframe.hpp>
Public Types | |
using | dimension = detail::dimension |
using | dimension_set = std::unordered_set<dimension, dimension::hash> |
using | variable = detail::variable<int, float, double> |
using | variable_map = std::unordered_map<std::string, variable> |
using | size_type = variable::size_type |
using | types = variable::types |
The variable value types this frame can support. | |
using | mdarray_variant = variable::mdarray_variant |
Public Member Functions | |
boost::optional< detail::dimension > | get_dimension (const std::string &name) const noexcept |
Return a dimension if it exists. | |
bool | has_dimension (const std::string &name) const noexcept |
Check if a dimension exists. | |
mdframe & | add_dimension (const std::string &name) |
Add a dimension with unlimited size to this mdframe. | |
mdframe & | add_dimension (const std::string &name, std::size_t size) |
Add a dimension with a specified size to this mdframe, or update an existing dimension. | |
variable & | get_variable (const std::string &name) noexcept |
Return reference to a mdarray representing a variable, if it exists. | |
variable & | operator[] (const std::string &name) noexcept |
Return reference to a mdarray representing a variable, if it exists. | |
bool | has_variable (const std::string &name) const noexcept |
Check if a variable exists. | |
template<typename T , types::enable_if_supports< T, bool > = true> | |
mdframe & | add_variable (const std::string &name, std::initializer_list< std::string > dimensions) |
Add a (vector) variable definition to this mdframe. | |
void | to_csv (const std::string &path, bool header=true) const |
Write this mdframe to a CSV file. | |
void | to_netcdf (const std::string &path) const |
Write this mdframe to a NetCDF file. | |
Private Member Functions | |
dimension_set::const_iterator | find_dimension (const std::string &name) const noexcept |
Get an iterator to a dimension, if it exists in the set. | |
variable_map::const_iterator | find_variable (const std::string &name) const noexcept |
Get an iterator to a variable, if it exists in the set. | |
Private Attributes | |
dimension_set | m_dimensions |
variable_map | m_variables |
A multi-dimensional, tagged data frame.
This structure (somewhat) mimics the conceptual format of NetCDF. In particular, we can think of an mdframe as a multimap between dimension tuples to multi-dimensional arrays. In other words, it is similar to an R data.frame or pandas dataframe where the column types are multi-dimensional arrays.
Heterogenous data types are handled via boost::variant types over mdarray types.
Consider the dimensions: x, y, z; and the variables:
Then, it follows that the corresponding mdframe (spanned over x):
x | v1 | v2 | v3 |
---|---|---|---|
0 | f_0 | [...]_0 | [[...]]_0 |
1 | f_1 | [...]_1 | [[...]]_1 |
... | ... | ... | ... |
n | f_n | [...]_n | [[...]]_n |
where:
Alternatively, we can project down to a 2D representation by unpacking the dimensions, such that:
dimensions || variables
=============== || ========================
x | y | z | v1 | v2 | v3 | |
---|---|---|---|---|---|---|
n | m | p | s[n] | d[n,m] | i[n,m,p] |
using ngen::mdframe::dimension_set = std::unordered_set<dimension, dimension::hash> |
using ngen::mdframe::types = variable::types |
The variable value types this frame can support.
These are stored as a compile-time type list to derive further type aliases.
using ngen::mdframe::variable = detail::variable<int, float, double> |
using ngen::mdframe::variable_map = std::unordered_map<std::string, variable> |
|
inline |
Add a dimension with unlimited size to this mdframe.
name | Name of the dimension. |
References m_dimensions.
|
inline |
Add a dimension with a specified size to this mdframe, or update an existing dimension.
name | Name of the dimension. |
size | Size of the dimension. |
References m_dimensions.
|
inline |
Add a (vector) variable definition to this mdframe.
{Args} being constructible to a std::initializer_list of std::string (i.e. a list of std::string).Arg | list of std::string representing dimension names. |
name | Name of the variable. |
dimensions | Names of the dimensions this variable spans. |
References get_dimension(), m_variables, and ngen::detail::variable< SupportedTypes >::make().
|
inlineprivatenoexcept |
Get an iterator to a dimension, if it exists in the set.
name | Name of the dimension |
References m_dimensions.
Referenced by get_dimension(), and has_dimension().
|
inlineprivatenoexcept |
Get an iterator to a variable, if it exists in the set.
name | Name of the variable |
References m_variables.
Referenced by has_variable().
|
inlinenoexcept |
Return a dimension if it exists.
Otherwise, returns boost::none.
name | Name of the dimension |
References find_dimension(), and m_dimensions.
Referenced by add_variable().
|
inlinenoexcept |
Return reference to a mdarray representing a variable, if it exists.
Otherwise, returns boost::none.
name | Name of the variable. |
References m_variables.
Referenced by operator[]().
|
inlinenoexcept |
Check if a dimension exists.
name | Name of the dimension |
References find_dimension(), and m_dimensions.
|
inlinenoexcept |
Check if a variable exists.
name | Name of the variable. |
References find_variable(), and m_variables.
|
inlinenoexcept |
Return reference to a mdarray representing a variable, if it exists.
Otherwise, returns boost::none.
name | Name of the variable. |
References get_variable().
void ngen::mdframe::to_csv | ( | const std::string & | path, |
bool | header = true ) const |
Write this mdframe to a CSV file.
The outputted CSV is a projected (or normalized) table spanning all possible dimensions, similar to the description of tidy data by Hadley Wickham (2014): https://www.jstatsoft.org/article/view/v059i10
path | File path to output CSV |
basis | Dimension to span CSV over, defaults to "time" |
header | Should the CSV header be included? |
References ngen::cartesian_indices(), m_dimensions, and m_variables.
void ngen::mdframe::to_netcdf | ( | const std::string & | path | ) | const |
Write this mdframe to a NetCDF file.
path | File path to the output NetCDF |
|
private |
Referenced by add_dimension(), add_dimension(), find_dimension(), get_dimension(), has_dimension(), and to_csv().
|
private |
Referenced by add_variable(), find_variable(), get_variable(), has_variable(), and to_csv().