Alexandria 2.31.0
SDC-CH common library for the Euclid project
|
Classes | |
struct | BorrowedRange |
class | MappedContainer |
class | NdArray |
struct | NpyDtype |
struct | NpyDtype< double > |
struct | NpyDtype< float > |
struct | NpyDtype< int16_t > |
struct | NpyDtype< int32_t > |
struct | NpyDtype< int64_t > |
struct | NpyDtype< int8_t > |
struct | NpyDtype< uint16_t > |
struct | NpyDtype< uint32_t > |
struct | NpyDtype< uint64_t > |
struct | NpyDtype< uint8_t > |
Functions | |
void | parseSingleValue (const std::string &descr, bool &big_endian, std::string &dtype) |
void | parseFieldValues (const std::string &descr, bool &big_endian, std::vector< std::string > &attrs, std::string &dtype) |
void | parseNpyDict (const std::string &header, bool &fortran_order, bool &big_endian, std::string &dtype, std::vector< size_t > &shape, std::vector< std::string > &attrs, size_t &n_elements) |
void | readNpyHeader (std::istream &input, std::string &dtype, std::vector< size_t > &shape, std::vector< std::string > &attrs, size_t &n_elements) |
std::string | npyShape (std::vector< size_t > shape) |
std::string | typeDescription (const std::string &type, const std::vector< std::string > &attrs) |
template<typename T > | |
void | writeNpyHeader (std::ostream &out, std::vector< size_t > shape, const std::vector< std::string > &attrs) |
template<typename T > | |
void | writeNpy (std::ostream &out, const NdArray< T > &array) |
template<typename T > | |
NdArray< T > | readNpy (std::istream &input) |
template<typename T > | |
void | writeNpy (const boost::filesystem::path &path, const NdArray< T > &array) |
template<typename T > | |
NdArray< T > | readNpy (const boost::filesystem::path &path) |
template<typename T > | |
NdArray< T > | mmapNpy (const boost::filesystem::path &path, boost::iostreams::mapped_file_base::mapmode mode=boost::iostreams::mapped_file_base::readwrite, size_t max_size=0) |
template<typename T > | |
NdArray< T > | createMmapNpy (const boost::filesystem::path &path, const std::vector< size_t > &shape, const std::vector< std::string > &attr_names, size_t max_size=0) |
template<typename T > | |
NdArray< T > | createMmapNpy (const boost::filesystem::path &path, const std::vector< size_t > &shape, size_t max_size=0) |
template<typename T > | |
std::ostream & | operator<< (std::ostream &out, const NdArray< T > &ndarray) |
std::vector< std::size_t > | unravel_index (std::size_t index, const std::vector< std::size_t > &shape) |
template<typename T > | |
T | sum (const NdArray< T > &array) |
template<typename T > | |
NdArray< T > | sum (const NdArray< T > &array, int axis) |
template<typename T , typename Iterator > | |
NdArray< T > | trapz (const NdArray< T > &array, const Iterator kbegin, const Iterator kend, int axis) |
template<typename T > | |
std::vector< std::size_t > | argmax (const NdArray< T > &array) |
template<typename T > | |
std::vector< std::size_t > | argmin (const NdArray< T > &array) |
template<typename T > | |
void | sort (NdArray< T > &array, const std::vector< std::string > &attrs) |
Variables | |
constexpr const char | NPY_MAGIC [] = {'\x93', 'N', 'U', 'M', 'P', 'Y'} |
constexpr const char * | ENDIAN_MARKER = "<" |
constexpr const uint8_t | NPY_VERSION [] = {'\x02', '\x00'} |
std::vector< std::size_t > Euclid::NdArray::argmax | ( | const NdArray< T > & | array | ) |
Return the coordinates for the maximum element
T | NdArray type |
array | The ndarray |
std::vector< std::size_t > Euclid::NdArray::argmin | ( | const NdArray< T > & | array | ) |
Return the coordinates for the minimum element
T | NdArray type |
array | The ndarray |
NdArray< T > Euclid::NdArray::createMmapNpy | ( | const boost::filesystem::path & | path, |
const std::vector< size_t > & | shape, | ||
const std::vector< std::string > & | attr_names, | ||
size_t | max_size = 0 |
||
) |
Create using mmap an NdArray backed by a numpy file
T | NdArray cell type |
path | Output path |
shape | NdArray shape |
attr_names | Attribute names |
max_size | Maximum size of the file. If you are going to call NdArray<T>::concatenate, mind this parameter! |
NdArray< T > Euclid::NdArray::createMmapNpy | ( | const boost::filesystem::path & | path, |
const std::vector< size_t > & | shape, | ||
size_t | max_size = 0 |
||
) |
Create using mmap an NdArray backed by a numpy file
T | NdArray cell type |
path | Output path |
shape | NdArray shape |
max_size | Maximum size of the file. If you are going to call NdArray<T>::concatenate, mind this parameter! |
NdArray< T > Euclid::NdArray::mmapNpy | ( | const boost::filesystem::path & | path, |
boost::iostreams::mapped_file_base::mapmode | mode = boost::iostreams::mapped_file_base::readwrite , |
||
size_t | max_size = 0 |
||
) |
Open using mmap an existing numpy file
T | NdArray cell type |
path | Input path |
mode | Open mode. By default read/write, so changes are persisted to disk. boost::iostreams::mapped_file_base::priv enabled a Copy-On-Write, so the memory can be modified but the changes will not persist |
max_size | Maximum size of the file. If you are going to call NdArray<T>::concatenate, mind this parameter! |
|
inline |
Generate a string that represents an NdArray shape vector as a Python tuple
shape | A string with the Python representation of a tuple |
Definition at line 168 of file NpyCommon.h.
References std::stringstream::str().
Referenced by writeNpyHeader().
std::ostream & Euclid::NdArray::operator<< | ( | std::ostream & | out, |
const NdArray< T > & | ndarray | ||
) |
Serialize a NdArray
|
inline |
Parse the description field from npy arrays with named fields, which are stored as the string representation of a list of tuples (name, dtype). i.e: [('a', '<i4'), ('b', '<i4')]
std::invalid_argument | NdArrays only support uniform types, so this method will fail if there are mixed types on the npy file |
Definition at line 30 of file NpyCommon.cpp.
References std::string::begin(), std::vector< T >::emplace_back(), std::string::empty(), std::string::end(), and parseSingleValue().
Referenced by parseNpyDict().
|
inline |
Parse the dictionary serialized on the npy file
header | String representation of the dictionary |
fortran_order | [out] Put here if the numpy array follows the Fortran convention |
big_endian | [out] Put here if the numpy array layout is big-endian |
dtype | Put here the read dtype |
shape | [out] Put here the read shape |
attrs[out] | Put here the attribute names |
n_elements | [out] Total number of elements (multiplication of shape) |
Definition at line 55 of file NpyCommon.cpp.
References std::accumulate(), std::vector< T >::begin(), std::vector< T >::end(), std::string::find(), parseFieldValues(), parseSingleValue(), and std::string::substr().
Referenced by readNpyHeader().
void Euclid::NdArray::parseSingleValue | ( | const std::string & | descr, |
bool & | big_endian, | ||
std::string & | dtype | ||
) |
Parse a single dtype description (i.e. '<f8')
Definition at line 25 of file NpyCommon.cpp.
References std::string::front(), and std::string::substr().
Referenced by parseFieldValues(), and parseNpyDict().
NdArray< T > Euclid::NdArray::readNpy | ( | const boost::filesystem::path & | path | ) |
NdArray< T > Euclid::NdArray::readNpy | ( | std::istream & | input | ) |
void Euclid::NdArray::readNpyHeader | ( | std::istream & | input, |
std::string & | dtype, | ||
std::vector< size_t > & | shape, | ||
std::vector< std::string > & | attrs, | ||
size_t & | n_elements | ||
) |
Read the npy header
input | Input stream |
dtype | [out] Put here the read dtype |
shape | [out] Put here the read shape |
attrs | [out] Put here the attribute names |
n_elements | [out] Total number of elements (multiplication of shape) |
Definition at line 81 of file NpyCommon.cpp.
References std::memcmp(), NPY_MAGIC, parseNpyDict(), and std::istream::read().
void Euclid::NdArray::sort | ( | NdArray< T > & | array, |
const std::vector< std::string > & | attrs | ||
) |
T Euclid::NdArray::sum | ( | const NdArray< T > & | array | ) |
Sum all elements in an ndarray
array | The ndarray |
Sum elements in an ndarray along the given axis
array | The ndarray |
axis | The axis. 0 is the first. Negative values index from the end. |
NdArray< T > Euclid::NdArray::trapz | ( | const NdArray< T > & | array, |
const Iterator | kbegin, | ||
const Iterator | kend, | ||
int | axis | ||
) |
Integrate elements in an ndarray along the given axis
array | The ndarray |
kbegin | Iterator to the beginning of the knot values |
kend | Iterator to the end of the knot values |
axis | The axis. 0 is the first. Negative values index from the end. |
|
inline |
Definition at line 178 of file NpyCommon.h.
References std::vector< T >::empty(), ENDIAN_MARKER, and std::stringstream::str().
Referenced by writeNpyHeader().
std::vector< std::size_t > Euclid::NdArray::unravel_index | ( | std::size_t | index, |
const std::vector< std::size_t > & | shape | ||
) |
Given an "flat" index (i.e a memory offset), return the appropriate coordinates for an ndarray of the given shape
index | Flat index |
shape | Array shape |
std::out_of_range | If the index goes beyond the size of an ndarray with the given shape |
Definition at line 24 of file Operations.cpp.
References std::vector< T >::size().
Referenced by Euclid::MathUtils::createSamplerFromGrid().
void Euclid::NdArray::writeNpy | ( | const boost::filesystem::path & | path, |
const NdArray< T > & | array | ||
) |
path | Output path |
array | NdArray to write |
Definition at line 74 of file Npy.h.
References writeNpy().
void Euclid::NdArray::writeNpy | ( | std::ostream & | out, |
const NdArray< T > & | array | ||
) |
Write an NdArray to a file following numpy format
out | Output stream |
array | NdArray to write |
Referenced by writeNpy().
void Euclid::NdArray::writeNpyHeader | ( | std::ostream & | out, |
std::vector< size_t > | shape, | ||
const std::vector< std::string > & | attrs | ||
) |
Write header
Definition at line 196 of file NpyCommon.h.
References std::vector< T >::back(), std::vector< T >::empty(), NPY_MAGIC, NPY_VERSION, npyShape(), std::vector< T >::pop_back(), std::vector< T >::size(), std::stringstream::str(), typeDescription(), and std::ostream::write().
|
constexpr |
Endianness marker for the numpy array
Definition at line 43 of file NpyCommon.h.
Referenced by typeDescription().
|
constexpr |
Magic string for .npy files
Definition at line 37 of file NpyCommon.h.
Referenced by readNpyHeader(), and writeNpyHeader().
|
constexpr |
We write arrays following 2.0 version (32 bits header size)
Definition at line 161 of file NpyCommon.h.
Referenced by writeNpyHeader().