MCPcopy Create free account
hub / github.com/PyMesh/PyMesh / SaveArrayAsNumpy

Function SaveArrayAsNumpy

src/Misc/numpy.hpp:164–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

162
163template<typename Scalar>
164void SaveArrayAsNumpy(
165 const std::string& filename, bool fortran_order,
166 int n_dims, const int shape[], const Scalar* data)
167{
168 if(n_dims <= 0)
169 throw std::invalid_argument("received an invalid argument");
170
171 std::string preamble, header;
172 std::string descriptor = detail::CreateDescriptor<Scalar>();
173 detail::CreateMetaData(
174 preamble, header, descriptor, fortran_order, n_dims, shape);
175 const size_t metadata_length = preamble.size() + header.size();
176 if(metadata_length % 16 != 0) {
177 throw std::runtime_error(
178 "formatting error: metadata length is not divisible by 16.");
179 }
180 std::ofstream stream(
181 filename.c_str(), std::ios::out|std::ios::binary|std::ios::trunc);
182 if(!stream) {
183 throw std::runtime_error("io error: failed to open a file.");
184 }
185 stream << preamble << header;
186 int size = 1;
187 for(int i=0; i<n_dims; ++i) { size *= shape[i]; }
188 stream.write(reinterpret_cast<const char*>(data), sizeof(Scalar)*size);
189}
190
191template<typename Scalar>
192void SaveArrayAsNumpy(

Callers 1

save_matrixFunction · 0.85

Calls 3

CreateMetaDataFunction · 0.85
sizeMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected