MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ encode_npy()

std::optional< std::vector< std::byte > > MayaFlux::IO::Detail::encode_npy ( const Kakshya::TensorData & tensor,
std::string & why )
nodiscard

Encode a tensor as a complete .npy array in dense C order.

Parameters
whyReceives the reason when numpy cannot represent the tensor.

Definition at line 201 of file NpyCodec.cpp.

202{
203 const auto& spec = tensor.spec();
204 const auto typestr = npy_typestr(spec.dtype);
205 if (!typestr) {
206 why = "numpy has no type for element type " + spec.dtype.name();
207 return std::nullopt;
208 }
209
210 std::optional<Kakshya::TensorData> gathered;
211 const auto payload = dense_bytes(tensor, gathered);
212 if (!payload) {
213 why = "tensor '" + spec.name + "' could not be read";
214 return std::nullopt;
215 }
216
217 const std::string body = "{'descr': '" + *typestr + "', 'fortran_order': False, 'shape': "
218 + shape_text(spec.shape) + ", }";
219 auto bytes = build_header(body, 1);
220 if (bytes.size() - 10 > 0xFFFF) {
221 bytes = build_header(body, 2);
222 }
223
224 const size_t data_start = bytes.size();
225 bytes.insert(bytes.end(), payload->begin(), payload->end());
226 if (!host_is_little_endian()) {
227 swap_components(std::span<std::byte>(bytes).subspan(data_start), component_bytes(spec.dtype));
228 }
229 return bytes;
230}
const TensorSpec & spec() const noexcept
Layout of the stored bytes.
Definition Tensor.hpp:105
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
std::optional< std::string > npy_typestr(Kakshya::TensorDType dtype)
Little-endian numpy type string for an element type, when numpy has one.
std::optional< std::span< const std::byte > > dense_bytes(const Kakshya::TensorData &tensor, std::optional< Kakshya::TensorData > &dense)
Elements of a tensor as dense C-order bytes.
void swap_components(std::span< std::byte > data, size_t component_bytes) noexcept
Reverse the byte order of every component in place.
bool host_is_little_endian() noexcept
Whether this machine stores multi-byte values least significant byte first.

References component_bytes(), dense_bytes(), host_is_little_endian(), npy_typestr(), MayaFlux::Kakshya::TensorData::spec(), and swap_components().

Referenced by MayaFlux::IO::NpyWriter::write(), and MayaFlux::IO::NpzWriter::write().

Here is the call graph for this function:
Here is the caller graph for this function: