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

◆ write()

bool MayaFlux::IO::NpyWriter::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 139 of file NpyFormat.cpp.

140{
141 m_last_error.clear();
142 if (tensors.tensors.size() != 1) {
143 m_last_error = "a .npy file holds exactly one tensor, received " + std::to_string(tensors.tensors.size());
144 return false;
145 }
146
147 const auto bytes = Detail::encode_npy(tensors.tensors.front(), m_last_error);
148 if (!bytes) {
149 return false;
150 }
151
152 std::ofstream file(filepath, std::ios::binary | std::ios::trunc);
153 if (!file) {
154 m_last_error = "cannot create '" + filepath + "'";
155 return false;
156 }
157 file.write(reinterpret_cast<const char*>(bytes->data()), static_cast<std::streamsize>(bytes->size()));
158 file.close();
159 if (!file) {
160 m_last_error = "failed while writing '" + filepath + "'";
161 return false;
162 }
163 return true;
164}
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(), m_last_error, and MayaFlux::Kakshya::TensorSet::tensors.

Here is the call graph for this function: