Open a file for reading.
93{
96
97 m_archive = std::make_unique<Detail::NpzArchive>();
99 const std::string reason = mz_zip_get_error_string(mz_zip_get_last_error(&
m_archive->zip));
101 return fail(
"cannot open archive '" +
m_path +
"': " + reason);
102 }
104
105 const mz_uint
count = mz_zip_reader_get_num_files(&
m_archive->zip);
106 for (mz_uint i = 0; i <
count; ++i) {
107 mz_zip_archive_file_stat stat {};
108 if (!mz_zip_reader_file_stat(&
m_archive->zip, i, &stat) || stat.m_is_directory) {
109 continue;
110 }
111
112 const std::string entry_name = stat.m_filename;
113 if (!entry_name.ends_with(npy_suffix)) {
114 continue;
115 }
116 const std::string name = entry_name.substr(0, entry_name.size() - npy_suffix.size());
117
118 std::string why;
119 const auto prefix = read_entry_prefix(
m_archive->zip, i, why);
121 if (!header) {
123 "NpzReader: skipping '{}': {}", entry_name, why);
124 continue;
125 }
126
127 const auto bytes = header->spec.dtype.storage_bytes(header->spec.element_count().value_or(0));
128 if (!bytes || header->data_offset + *bytes > stat.m_uncomp_size) {
130 "NpzReader: skipping '{}': data is shorter than its header declares", entry_name);
131 continue;
132 }
133
134 m_entries.push_back({ .index = i, .spec = header->spec, .data_offset = header->data_offset, .big_endian = header->big_endian });
135 }
136
139 return true;
140}
#define MF_WARN(comp, ctx,...)
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
void close() override
Close the currently open file.
std::vector< Entry > m_entries
std::unique_ptr< Detail::NpzArchive > m_archive
bool fail(std::string reason)
Record a failure reason and return false.
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.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).