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

Definitions for reading and writing NPY and NPZ files The libnpy library provides a means to read and write NPY and NPY files from C++. methods for reading and writing the numpy lib (NPY) format. The implementation is based upon the description available at: https://docs.scipy.org/doc/numpy/reference/generated/numpy.lib.format.html The NPZ implementation draws heavily from the PKZIP Application note: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT. More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
Include dependency graph for npy.h:

Go to the source code of this file.

Classes

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

Namespaces

namespace  npy
 

Macros

#define NPY_VERSION_MAJOR   2
 
#define NPY_VERSION_MINOR   0
 
#define NPY_VERSION_PATCH   0
 
#define NPY_VERSION_STRING   "2.0.0"
 

Enumerations

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

Functions

endian_t npy::native_endian ()
 This function will return the endianness of the current hardware.
 
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.
 
const std::pair< data_type_t, endian_t > & npy::from_dtype (const std::string &dtype)
 
std::ostream & npy::operator<< (std::ostream &os, const endian_t &obj)
 
std::ostream & npy::operator<< (std::ostream &os, const data_type_t &obj)
 
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.
 
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<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<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<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<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<typename CHAR >
header_info npy::read_npy_header (std::basic_istream< CHAR > &input)
 Read an NPY header from the provided stream.
 
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<typename T , typename CHAR >
npy::load (std::basic_istream< CHAR > &input)
 
template<typename 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<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<typename CHAR >
header_info npy::peek (std::basic_istream< CHAR > &input)
 Return the header information for an NPY file.
 
header_info npy::peek (const std::string &path)
 Return the header information for an NPY file.
 

Variables

const int STATIC_HEADER_LENGTH = 10
 

Detailed Description

Definitions for reading and writing NPY and NPZ files The libnpy library provides a means to read and write NPY and NPY files from C++. methods for reading and writing the numpy lib (NPY) format. The implementation is based upon the description available at: https://docs.scipy.org/doc/numpy/reference/generated/numpy.lib.format.html The NPZ implementation draws heavily from the PKZIP Application note: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT.


Copyright (C) 2021 Matthew Johnson

For conditions of distribution and use, see copyright notice in LICENSE


Macro Definition Documentation

◆ NPY_VERSION_MAJOR

#define NPY_VERSION_MAJOR   2

◆ NPY_VERSION_MINOR

#define NPY_VERSION_MINOR   0

◆ NPY_VERSION_PATCH

#define NPY_VERSION_PATCH   0

◆ NPY_VERSION_STRING

#define NPY_VERSION_STRING   "2.0.0"

Variable Documentation

◆ STATIC_HEADER_LENGTH

const int STATIC_HEADER_LENGTH = 10