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

◆ read()

std::optional< Kakshya::TensorData > MayaFlux::IO::NpyReader::read ( std::string_view name)
nodiscardoverridevirtual

Read one tensor by name.

Returns
The tensor, or std::nullopt with get_last_error() set.

Implements MayaFlux::IO::TensorReader.

Definition at line 107 of file NpyFormat.cpp.

108{
109 if (!m_open) {
110 fail("no file is open");
111 return std::nullopt;
112 }
113 if (!name.empty() && name != m_spec.name) {
114 fail("no tensor named '" + std::string(name) + "'");
115 return std::nullopt;
116 }
117
119 if (!tensor) {
120 fail("cannot allocate tensor '" + m_spec.name + "'");
121 return std::nullopt;
122 }
123
124 const auto storage = tensor->storage();
125 if (!storage.empty()) {
126 m_file.clear();
127 m_file.seekg(static_cast<std::streamoff>(m_data_offset));
128 if (!m_file.read(reinterpret_cast<char*>(storage.data()), static_cast<std::streamsize>(storage.size()))) {
129 fail("numpy data is truncated");
130 return std::nullopt;
131 }
134 }
135 }
136 return tensor;
137}
Kakshya::TensorSpec m_spec
Definition NpyFormat.hpp:51
bool fail(std::string reason)
Record a failure reason and return false.
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Definition Tensor.cpp:72
size_t component_bytes(Kakshya::TensorDType dtype) noexcept
Bytes of one independently ordered component of an element.
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 MayaFlux::Kakshya::TensorData::allocate(), MayaFlux::IO::Detail::component_bytes(), MayaFlux::IO::TensorReader::fail(), MayaFlux::IO::Detail::host_is_little_endian(), m_big_endian, m_data_offset, m_file, m_open, m_spec, and MayaFlux::IO::Detail::swap_components().

Here is the call graph for this function: