libnpy 2.0.0
Lightweight C++ library for reading and writing NPY and NPZ files
Loading...
Searching...
No Matches
Classes | Enumerations | Functions
npy Namespace Reference

Classes

struct  file_entry
 Struct representing a file in the NPZ archive. More...
 
struct  header_info
 Class representing the header info for an NPY file. More...
 
class  npzfilereader
 Class handling reading of an NPZ from a file on disk. More...
 
class  npzfilewriter
 Class which handles writing of an NPZ archive to disk. More...
 
class  npzstringreader
 Class handling reading of an NPZ from an in-memory string stream. More...
 
class  npzstringwriter
 Class which handles writing of an NPZ to an in-memory string stream. More...
 
class  tensor
 The default tensor class. More...
 

Enumerations

enum class  endian_t : char { NATIVE , BIG , LITTLE }
 Enumeration which represents a type of endianness. More...
 
enum class  data_type_t : char {
  INT8 , UINT8 , INT16 , UINT16 ,
  INT32 , UINT32 , INT64 , UINT64 ,
  FLOAT32 , FLOAT64 , COMPLEX64 , COMPLEX128 ,
  UNICODE_STRING
}
 This enum represents the different types of tensor data that can be stored. More...
 
enum class  compression_method_t : std::uint16_t { STORED = 0 , DEFLATED = 8 }
 Enumeration indicating the compression method to use for data in the NPZ archive. More...
 

Functions

endian_t native_endian ()
 This function will return the endianness of the current hardware.
 
const std::string & to_dtype (data_type_t dtype, endian_t endian=endian_t::NATIVE)
 Convert a data type and endianness to a NPY dtype string.
 
const std::pair< data_type_t, endian_t > & from_dtype (const std::string &dtype)
 
std::ostream & operator<< (std::ostream &os, const endian_t &obj)
 
std::ostream & operator<< (std::ostream &os, const data_type_t &obj)
 
template<typename CHAR >
void write_npy_header (std::basic_ostream< CHAR > &output, const std::string &dtype, bool fortran_order, const std::vector< size_t > &shape)
 Writes an NPY header to the provided stream.
 
template<typename T , typename CHAR >
void write_values (std::basic_ostream< CHAR > &output, const T *data_ptr, size_t num_elements, endian_t endianness)
 Write values to the provided stream.
 
template<typename T , typename CHAR >
void save (std::basic_ostream< CHAR > &output, const T &tensor, endian_t endianness=npy::endian_t::NATIVE)
 Saves a tensor to the provided stream.
 
template<typename T , template< typename > class TENSOR, typename CHAR >
void save (std::basic_ostream< CHAR > &output, const TENSOR< T > &tensor, endian_t endianness=npy::endian_t::NATIVE)
 Saves a tensor to the provided stream.
 
template<typename T >
void save (const std::string &path, T &tensor, endian_t endianness=npy::endian_t::NATIVE)
 Saves a tensor to the provided location on disk.
 
template<typename T , template< typename > class TENSOR>
void save (const std::string &path, T &tensor, endian_t endianness=npy::endian_t::NATIVE)
 Saves a tensor to the provided location on disk.
 
template<typename CHAR >
header_info read_npy_header (std::basic_istream< CHAR > &input)
 Read an NPY header from the provided stream.
 
template<typename T , typename CHAR >
void read_values (std::basic_istream< CHAR > &input, T *data_ptr, size_t num_elements, const header_info &info)
 Read values from the provided stream.
 
template<typename T , typename CHAR >
load (std::basic_istream< CHAR > &input)
 
template<typename T >
load (const std::string &path)
 Loads a tensor in NPY format from the specified location on the disk. The type of the tensor must match the data to be read.
 
template<typename T , template< typename > class TENSOR>
TENSOR< T > load (const std::string &path)
 Loads a tensor in NPY format from the specified location on the disk. The type of the tensor must match the data to be read.
 
template<typename CHAR >
header_info peek (std::basic_istream< CHAR > &input)
 Return the header information for an NPY file.
 
header_info peek (const std::string &path)
 Return the header information for an NPY file.
 

Enumeration Type Documentation

◆ compression_method_t

enum class npy::compression_method_t : std::uint16_t
strong

Enumeration indicating the compression method to use for data in the NPZ archive.

Enumerator
STORED 

Store the data with no compression.

DEFLATED 

Use the DEFLATE algorithm to compress the data.

◆ data_type_t

enum class npy::data_type_t : char
strong

This enum represents the different types of tensor data that can be stored.

Enumerator
INT8 

8 bit signed integer

UINT8 

8 bit unsigned integer

INT16 

16-bit signed integer (short)

UINT16 

16-bit unsigned integer (ushort)

INT32 

32-bit signed integer (int)

UINT32 

32-bit unsigned integer (uint)

INT64 

64-bit integer (long)

UINT64 

64-bit unsigned integer (long)

FLOAT32 

32-bit floating-point value (float)

FLOAT64 

64-bit floating-point value (double)

COMPLEX64 

64-bit complex number (std::complex<float>)

COMPLEX128 

128-bit complex number (std::complex<double>)

UNICODE_STRING 

Unicode string (std::wstring)

◆ endian_t

enum class npy::endian_t : char
strong

Enumeration which represents a type of endianness.

Enumerator
NATIVE 

Indicates that the native endianness should be used. Native in this case means that of the hardware the program is currently running on.

BIG 

Indicates the use of big-endian encoding.

LITTLE 

Indicates the use of little-endian encoding.

Function Documentation

◆ from_dtype()

const std::pair< data_type_t, endian_t > & npy::from_dtype ( const std::string &  dtype)

Converts from an NPY dtype string to a data type and endianness.

Parameters
dtypethe NPY dtype string
Returns
a pair of data type and endianness corresponding to the input

◆ load() [1/3]

template<typename T >
T npy::load ( const std::string &  path)

Loads a tensor in NPY format from the specified location on the disk. The type of the tensor must match the data to be read.

Template Parameters
Tthe data type
TENSORthe tensor type
Parameters
patha valid location on the disk
Returns
an object of type TENSOR<T> read from the stream

◆ load() [2/3]

template<typename T , template< typename > class TENSOR>
TENSOR< T > npy::load ( const std::string &  path)

Loads a tensor in NPY format from the specified location on the disk. The type of the tensor must match the data to be read.

Template Parameters
Tthe data type
TENSORthe tensor type
Parameters
patha valid location on the disk
Returns
an object of type TENSOR<T> read from the stream

◆ load() [3/3]

template<typename T , typename CHAR >
T npy::load ( std::basic_istream< CHAR > &  input)

◆ native_endian()

endian_t npy::native_endian ( )
inline

This function will return the endianness of the current hardware.

◆ operator<<() [1/2]

std::ostream & npy::operator<< ( std::ostream &  os,
const data_type_t obj 
)

◆ operator<<() [2/2]

std::ostream & npy::operator<< ( std::ostream &  os,
const endian_t obj 
)

◆ peek() [1/2]

header_info npy::peek ( const std::string &  path)

Return the header information for an NPY file.

Parameters
paththe path to the NPY file on disk
Returns
the NPY header information

◆ peek() [2/2]

template<typename CHAR >
header_info npy::peek ( std::basic_istream< CHAR > &  input)

Return the header information for an NPY file.

Parameters
inputthe input stream containing the NPY-encoded bytes
Returns
the NPY header information

◆ read_npy_header()

template<typename CHAR >
header_info npy::read_npy_header ( std::basic_istream< CHAR > &  input)

Read an NPY header from the provided stream.

Parameters
inputthe input stream
Returns
the header information

◆ read_values()

template<typename T , typename CHAR >
void npy::read_values ( std::basic_istream< CHAR > &  input,
T *  data_ptr,
size_t  num_elements,
const header_info info 
)

Read values from the provided stream.

Template Parameters
Tthe data type
CHARthe character type of the input stream
Parameters
inputthe input stream
data_ptrpointer to the start of the data buffer
num_elementsthe number of elements to read
infothe header information

◆ save() [1/4]

template<typename T >
void npy::save ( const std::string &  path,
T &  tensor,
endian_t  endianness = npy::endian_t::NATIVE 
)

Saves a tensor to the provided location on disk.

Template Parameters
Tthe tensor type
Parameters
patha path to a valid location on disk
tensorthe tensor
endiannessthe endianness to use in saving the tensor

◆ save() [2/4]

template<typename T , template< typename > class TENSOR>
void npy::save ( const std::string &  path,
T &  tensor,
endian_t  endianness = npy::endian_t::NATIVE 
)

Saves a tensor to the provided location on disk.

Template Parameters
Tthe data type
TENSORthe tensor type
Parameters
patha path to a valid location on disk
tensorthe tensor
endiannessthe endianness to use in saving the tensor

◆ save() [3/4]

template<typename T , typename CHAR >
void npy::save ( std::basic_ostream< CHAR > &  output,
const T &  tensor,
endian_t  endianness = npy::endian_t::NATIVE 
)

Saves a tensor to the provided stream.

Template Parameters
Tthe tensor type
CHARthe character type of the output stream
Parameters
outputthe output stream
tensorthe tensor
endiannessthe endianness to use in saving the tensor

◆ save() [4/4]

template<typename T , template< typename > class TENSOR, typename CHAR >
void npy::save ( std::basic_ostream< CHAR > &  output,
const TENSOR< T > &  tensor,
endian_t  endianness = npy::endian_t::NATIVE 
)

Saves a tensor to the provided stream.

Template Parameters
Tthe data type
TENSORthe tensor type
CHARthe character type of the output stream
Parameters
outputthe output stream
tensorthe tensor
endiannessthe endianness to use in saving the tensor

◆ to_dtype()

const std::string & npy::to_dtype ( data_type_t  dtype,
endian_t  endian = endian_t::NATIVE 
)

Convert a data type and endianness to a NPY dtype string.

Parameters
dtypethe data type
endianthe endianness. Defaults to the current endianness of the caller.
Returns
the NPY dtype string

◆ write_npy_header()

template<typename CHAR >
void npy::write_npy_header ( std::basic_ostream< CHAR > &  output,
const std::string &  dtype,
bool  fortran_order,
const std::vector< size_t > &  shape 
)

Writes an NPY header to the provided stream.

Parameters
outputthe output stream
dtypethe NPY-encoded dtype string (includes data type and endianness)
fortran_orderwhether the data is encoded in FORTRAN (i.e. column major) order
shapea sequence of values indicating the shape of each dimension of the tensor
See also
npy::to_dtype

◆ write_values()

template<typename T , typename CHAR >
void npy::write_values ( std::basic_ostream< CHAR > &  output,
const T *  data_ptr,
size_t  num_elements,
endian_t  endianness 
)

Write values to the provided stream.

Template Parameters
Tthe data type
CHARthe character type of the output stream
Parameters
outputthe output stream
data_ptrpointer to the start of the data buffer
num_elementsthe number of elements to write
endiannessthe endianness to use in writing the data