commit 2f56f4c23ea840f9d15c43801368cf4a089efa84 Author: sophgo-forum-service <forum_service@sophgo.com> Date: Sun May 19 23:03:28 2024 +0800 [feat] cnpy opensource for cv18xx soc. - e9d84e
71 lines
2.4 KiB
C++
71 lines
2.4 KiB
C++
#include"cnpy.h"
|
|
#include<complex>
|
|
#include<cstdlib>
|
|
#include<iostream>
|
|
#include<map>
|
|
#include<string>
|
|
|
|
const int Nx = 128;
|
|
const int Ny = 64;
|
|
const int Nz = 32;
|
|
|
|
int main()
|
|
{
|
|
//set random seed so that result is reproducible (for testing)
|
|
srand(0);
|
|
|
|
//create random data
|
|
std::vector<std::complex<double>> data(Nx*Ny*Nz);
|
|
for(int i = 0;i < Nx*Ny*Nz;i++) data[i] = std::complex<double>(rand(),rand());
|
|
|
|
//save it to file
|
|
cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"w");
|
|
|
|
//load it into a new array
|
|
cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
|
|
std::complex<double>* loaded_data = arr.data<std::complex<double>>();
|
|
|
|
//make sure the loaded data matches the saved data
|
|
assert(arr.word_size == sizeof(std::complex<double>));
|
|
assert(arr.shape.size() == 3 && arr.shape[0] == Nz && arr.shape[1] == Ny && arr.shape[2] == Nx);
|
|
for(int i = 0; i < Nx*Ny*Nz;i++) assert(data[i] == loaded_data[i]);
|
|
|
|
//append the same data to file
|
|
//npy array on file now has shape (Nz+Nz,Ny,Nx)
|
|
cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"a");
|
|
|
|
//now write to an npz file
|
|
//non-array variables are treated as 1D arrays with 1 element
|
|
double myVar1 = 1.2;
|
|
char myVar2 = 'a';
|
|
cnpy::npz_save("out.npz","myVar1",&myVar1,{1},"w"); //"w" overwrites any existing file
|
|
cnpy::npz_save("out.npz","myVar2",&myVar2,{1},"a"); //"a" appends to the file we created above
|
|
cnpy::npz_save("out.npz","arr1",&data[0],{Nz,Ny,Nx},"a"); //"a" appends to the file we created above
|
|
|
|
//load a single var from the npz file
|
|
cnpy::NpyArray arr2 = cnpy::npz_load("out.npz","arr1");
|
|
|
|
//load the entire npz file
|
|
cnpy::npz_t my_npz = cnpy::npz_load("out.npz");
|
|
|
|
// add a new array
|
|
std::vector<std::complex<double>> new_data(20);
|
|
for(int i = 0;i < 20;i++)
|
|
new_data[i] = std::complex<double>(i, 20 - i);
|
|
cnpy::npz_add_array<std::complex<double> >(my_npz, "new_arr", new_data);
|
|
|
|
//save the entire npz file back
|
|
// TODO: this has some problem, because npz_save_all() is assuming
|
|
// all array in same type, this is because NPArray struct does not
|
|
// convey a type info.
|
|
cnpy::npz_save_all("out_new.npz", my_npz);
|
|
|
|
//check that the loaded myVar1 matches myVar1
|
|
cnpy::NpyArray arr_mv1 = my_npz["myVar1"];
|
|
double* mv1 = arr_mv1.data<double>();
|
|
assert(arr_mv1.shape.size() == 1 && arr_mv1.shape[0] == 1);
|
|
assert(mv1[0] == myVar1);
|
|
|
|
return 0;
|
|
}
|