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

◆ write()

bool MayaFlux::IO::NpzWriter::write ( const std::string & filepath,
const Kakshya::TensorSet & tensors )
overridevirtual

Write every tensor in the set to a file.

Parameters
filepathDestination path.
tensorsTensors with unique, non-empty names.
Returns
True only if the complete file was written.

Implements MayaFlux::IO::TensorWriter.

Definition at line 191 of file NpzFormat.cpp.

192{
193 m_last_error.clear();
194 if (!tensors.is_consistent()) {
195 m_last_error = "tensors must be valid with unique, non-empty names";
196 return false;
197 }
198
199 std::vector<std::vector<std::byte>> encoded;
200 encoded.reserve(tensors.tensors.size());
201 for (const auto& tensor : tensors.tensors) {
202 auto bytes = Detail::encode_npy(tensor, m_last_error);
203 if (!bytes) {
204 return false;
205 }
206 encoded.push_back(std::move(*bytes));
207 }
208
209 mz_zip_archive zip {};
210 if (!mz_zip_writer_init_file(&zip, filepath.c_str(), 0)) {
211 m_last_error = "cannot create '" + filepath + "'";
212 return false;
213 }
214
215 for (size_t i = 0; i < encoded.size(); ++i) {
216 const std::string entry = tensors.tensors[i].spec().name + std::string(npy_suffix);
217 if (!mz_zip_writer_add_mem(&zip, entry.c_str(), encoded[i].data(), encoded[i].size(), MZ_NO_COMPRESSION)) {
218 m_last_error = std::string("cannot add '") + entry + "': " + mz_zip_get_error_string(mz_zip_get_last_error(&zip));
219 mz_zip_writer_end(&zip);
220 return false;
221 }
222 }
223
224 const bool finalized = mz_zip_writer_finalize_archive(&zip) != 0;
225 mz_zip_writer_end(&zip);
226 if (!finalized) {
227 m_last_error = "cannot finalize '" + filepath + "'";
228 return false;
229 }
230 return true;
231}
std::optional< std::vector< std::byte > > encode_npy(const Kakshya::TensorData &tensor, std::string &why)
Encode a tensor as a complete .npy array in dense C order.
Definition NpyCodec.cpp:201

References MayaFlux::IO::Detail::encode_npy(), MayaFlux::Kakshya::TensorSet::is_consistent(), m_last_error, and MayaFlux::Kakshya::TensorSet::tensors.

Here is the call graph for this function: