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

◆ open()

bool MayaFlux::IO::NpzReader::open ( const std::string & filepath,
FileReadOptions options = FileReadOptions::ALL )
overridevirtual

Open a file for reading.

Parameters
filepathPath to the file.
optionsReading options (see FileReadOptions).
Returns
true if file was successfully opened.

Implements MayaFlux::IO::FileReader.

Definition at line 92 of file NpzFormat.cpp.

93{
94 close();
95 m_path = resolve_path(filepath);
96
97 m_archive = std::make_unique<Detail::NpzArchive>();
98 if (!mz_zip_reader_init_file(&m_archive->zip, m_path.c_str(), 0)) {
99 const std::string reason = mz_zip_get_error_string(mz_zip_get_last_error(&m_archive->zip));
100 close();
101 return fail("cannot open archive '" + m_path + "': " + reason);
102 }
103 m_archive->active = true;
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);
120 const auto header = prefix.empty() ? std::nullopt : Detail::parse_npy_header(prefix, name, 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
137 m_open = true;
138 m_last_error.clear();
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.
Definition NpzFormat.cpp:85
std::vector< Entry > m_entries
Definition NpzFormat.hpp:57
std::unique_ptr< Detail::NpzArchive > m_archive
Definition NpzFormat.hpp:56
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.
Definition NpyCodec.cpp:137
@ 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).
Definition Analysis.cpp:506

References close(), MayaFlux::IO::TensorReader::fail(), MayaFlux::Journal::FileIO, MayaFlux::Journal::IO, m_archive, m_entries, MayaFlux::IO::TensorReader::m_last_error, m_open, MayaFlux::IO::TensorReader::m_path, MF_WARN, MayaFlux::IO::Detail::parse_npy_header(), and MayaFlux::IO::FileReader::resolve_path().

Referenced by operator=().

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