MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NpyCodec.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::IO::Detail {
6
7/** @brief Decoded header of one .npy array. */
13
14/**
15 * @brief Total prefix size (magic, version, length field and header) of a .npy array.
16 * @param leading At least the first twelve bytes, or the whole array if shorter.
17 * @param why Receives the reason on failure.
18 * @return Offset at which the array data starts, or std::nullopt.
19 */
20[[nodiscard]] std::optional<size_t> npy_prefix_size(std::span<const std::byte> leading, std::string& why);
21
22/**
23 * @brief Decode a .npy prefix into a tensor spec.
24 *
25 * Fortran-ordered arrays become strided specs, so no data is rearranged.
26 *
27 * @param prefix Exactly the bytes npy_prefix_size() reported.
28 * @param name Name given to the tensor.
29 * @param why Receives the reason on failure.
30 */
31[[nodiscard]] std::optional<NpyHeader> parse_npy_header(
32 std::span<const std::byte> prefix, std::string_view name, std::string& why);
33
34/**
35 * @brief Encode a tensor as a complete .npy array in dense C order.
36 * @param why Receives the reason when numpy cannot represent the tensor.
37 */
38[[nodiscard]] std::optional<std::vector<std::byte>> encode_npy(
39 const Kakshya::TensorData& tensor, std::string& why);
40
41}
Owning host tensor: a TensorSpec and the bytes it describes.
Definition Tensor.hpp:42
std::optional< NpyHeader > parse_npy_header(std::span< const std::byte > prefix, std::string_view name, std::string &why)
Decode a .npy prefix into a tensor spec.
Definition NpyCodec.cpp:137
std::optional< std::vector< std::byte > > encode_npy(const Kakshya::TensorData &tensor, std::string &why)
Encode a tensor as a complete .npy array in dense C order.
Definition NpyCodec.cpp:201
std::optional< size_t > npy_prefix_size(std::span< const std::byte > leading, std::string &why)
Total prefix size (magic, version, length field and header) of a .npy array.
Definition NpyCodec.cpp:107
Kakshya::TensorSpec spec
Definition NpyCodec.hpp:9
Decoded header of one .npy array.
Definition NpyCodec.hpp:8
Complete description of a tensor: type, shape, layout, meaning.