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

◆ parse_npy_header()

std::optional< NpyHeader > MayaFlux::IO::Detail::parse_npy_header ( std::span< const std::byte > prefix,
std::string_view name,
std::string & why )
nodiscard

Decode a .npy prefix into a tensor spec.

Fortran-ordered arrays become strided specs, so no data is rearranged.

Parameters
prefixExactly the bytes npy_prefix_size() reported.
nameName given to the tensor.
whyReceives the reason on failure.

Definition at line 137 of file NpyCodec.cpp.

139{
140 const auto total = npy_prefix_size(prefix, why);
141 if (!total || *total != prefix.size()) {
142 if (total) {
143 why = "numpy header is truncated";
144 }
145 return std::nullopt;
146 }
147
148 const size_t header_start = std::to_integer<uint8_t>(prefix[6]) == 1 ? 10 : 12;
149 const std::string_view text(reinterpret_cast<const char*>(prefix.data()) + header_start,
150 prefix.size() - header_start);
151
152 const auto descr = value_after_key(text, "descr");
153 if (descr.empty() || (descr[0] != '\'' && descr[0] != '"')) {
154 why = descr.empty() ? "numpy header has no descr" : "structured numpy dtypes are not supported";
155 return std::nullopt;
156 }
157 const auto descr_end = descr.find(descr[0], 1);
158 if (descr_end == std::string_view::npos) {
159 why = "numpy descr is malformed";
160 return std::nullopt;
161 }
162 const auto typestr = npy_dtype(descr.substr(1, descr_end - 1), why);
163 if (!typestr) {
164 return std::nullopt;
165 }
166
167 const auto order = value_after_key(text, "fortran_order");
168 bool fortran = false;
169 if (order.starts_with("True")) {
170 fortran = true;
171 } else if (!order.starts_with("False")) {
172 why = "numpy header has no fortran_order";
173 return std::nullopt;
174 }
175
176 const auto shape = parse_shape(value_after_key(text, "shape"));
177 if (!shape) {
178 why = "numpy header has no valid shape";
179 return std::nullopt;
180 }
181
182 NpyHeader header;
183 header.big_endian = typestr->big_endian;
184 header.data_offset = *total;
185 header.spec = Kakshya::TensorSpec::contiguous(typestr->dtype, *shape, std::string(name));
186
187 if (fortran && shape->size() > 1) {
188 header.spec.strides.assign(shape->size(), 1);
189 for (size_t i = 1; i < shape->size(); ++i) {
190 header.spec.strides[i] = header.spec.strides[i - 1] * static_cast<int64_t>(std::max<uint64_t>((*shape)[i - 1], 1));
191 }
192 }
193
194 if (const auto reason = header.spec.diagnose(); !reason.empty()) {
195 why = reason;
196 return std::nullopt;
197 }
198 return header;
199}
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
std::optional< NpyTypestr > npy_dtype(std::string_view typestr, std::string &why)
Parse a numpy type string such as <f4, >i8 or |b1.
Decoded header of one .npy array.
Definition NpyCodec.hpp:8
static TensorSpec contiguous(TensorDType dtype, std::vector< uint64_t > shape, std::string name={})
C-ordered spec with no offset, axes or quantization.

References MayaFlux::IO::Detail::NpyHeader::big_endian, MayaFlux::Kakshya::TensorSpec::contiguous(), MayaFlux::IO::Detail::NpyHeader::data_offset, MayaFlux::Kakshya::TensorSpec::diagnose(), npy_dtype(), npy_prefix_size(), MayaFlux::IO::Detail::NpyHeader::spec, and MayaFlux::Kakshya::TensorSpec::strides.

Referenced by MayaFlux::IO::NpyReader::open(), and MayaFlux::IO::NpzReader::open().

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