18 return (((
char *)&x)[0]) ?
'<' :
'>';
23 if(t ==
typeid(
float) )
return 'f';
24 if(t ==
typeid(
double) )
return 'f';
25 if(t ==
typeid(
long double) )
return 'f';
27 if(t ==
typeid(
int) )
return 'i';
28 if(t ==
typeid(
char) )
return 'i';
29 if(t ==
typeid(
short) )
return 'i';
30 if(t ==
typeid(
long) )
return 'i';
31 if(t ==
typeid(
long long) )
return 'i';
33 if(t ==
typeid(
unsigned char) )
return 'u';
34 if(t ==
typeid(
unsigned short) )
return 'u';
35 if(t ==
typeid(
unsigned long) )
return 'u';
36 if(t ==
typeid(
unsigned long long) )
return 'u';
37 if(t ==
typeid(
unsigned int) )
return 'u';
39 if(t ==
typeid(
bool) )
return 'b';
41 if(t ==
typeid(std::complex<float>) )
return 'c';
42 if(t ==
typeid(std::complex<double>) )
return 'c';
43 if(t ==
typeid(std::complex<long double>) )
return 'c';
49 lhs.insert(lhs.end(),rhs.begin(),rhs.end());
53 template<> std::vector<char>&
cnpy::operator+=(std::vector<char>& lhs,
const char* rhs) {
55 size_t len = strlen(rhs);
58 lhs.push_back(rhs[
byte]);
68 std::string header(reinterpret_cast<char*>(buffer+9),header_len);
73 loc1 = header.find(
"fortran_order")+16;
74 fortran_order = (header.substr(loc1,4) ==
"True" ?
true :
false);
77 loc1 = header.find(
"(");
78 loc2 = header.find(
")");
80 std::regex num_regex(
"[0-9][0-9]*");
84 std::string str_shape = header.substr(loc1+1,loc2-loc1-1);
85 while(std::regex_search(str_shape, sm, num_regex)) {
86 shape.push_back(std::stoi(sm[0].
str()));
87 str_shape = sm.suffix().str();
93 loc1 = header.find(
"descr")+9;
94 bool littleEndian = (header[loc1] ==
'<' || header[loc1] ==
'|' ?
true :
false);
96 throw std::runtime_error(
"parse_npy_header: header is not little endian");
103 loc2 = str_ws.find(
"'");
104 word_size = atoi(str_ws.substr(0,loc2).c_str());
109 size_t res = fread(buffer,
sizeof(
char),11,fp);
111 throw std::runtime_error(
"parse_npy_header: failed fread");
113 if (header[header.size()-1] !=
'\n') {
114 throw std::runtime_error(
"parse_npy_header: header is not newline terminated");
120 loc1 = header.find(
"fortran_order");
121 if (loc1 == std::string::npos)
122 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'fortran_order'");
124 fortran_order = (header.substr(loc1,4) ==
"True" ?
true :
false);
127 loc1 = header.find(
"(");
128 loc2 = header.find(
")");
129 if (loc1 == std::string::npos || loc2 == std::string::npos)
130 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: '(' or ')'");
132 std::regex num_regex(
"[0-9][0-9]*");
136 std::string str_shape = header.substr(loc1+1,loc2-loc1-1);
137 while(std::regex_search(str_shape, sm, num_regex)) {
138 shape.push_back(std::stoi(sm[0].
str()));
139 str_shape = sm.suffix().str();
145 loc1 = header.find(
"descr");
146 if (loc1 == std::string::npos)
147 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'descr'");
149 bool littleEndian = (header[loc1] ==
'<' || header[loc1] ==
'|' ?
true :
false);
151 throw std::runtime_error(
"parse_npy_header: header is not little endian");
158 loc2 = str_ws.find(
"'");
159 word_size = atoi(str_ws.substr(0,loc2).c_str());
164 std::vector<char> footer(22);
165 fseek(fp,-22,SEEK_END);
166 size_t res = fread(&footer[0],
sizeof(
char),22,fp);
168 throw std::runtime_error(
"parse_zip_footer: failed fread");
170 uint16_t disk_no, disk_start, nrecs_on_disk, comment_len;
172 disk_start = *(
uint16_t*) &footer[6];
173 nrecs_on_disk = *(
uint16_t*) &footer[8];
175 global_header_size = *(
uint32_t*) &footer[12];
176 global_header_offset = *(
uint32_t*) &footer[16];
177 comment_len = *(
uint16_t*) &footer[20];
180 throw std::runtime_error(
"parse_zip_footer: non-zero disk number");
183 if (disk_start != 0) {
184 throw std::runtime_error(
"parse_zip_footer: non-zero disk start");
187 if (nrecs_on_disk != nrecs) {
188 throw std::runtime_error(
"parse_zip_footer: number of recrods do not match");
190 if (comment_len != 0) {
191 throw std::runtime_error(
"parse_zip_footer: unexepcted comment");
196 std::vector<size_t> shape;
204 throw std::runtime_error(
"load_the_npy_file: failed fread");
210 std::vector<unsigned char> buffer_compr(compr_bytes);
211 std::vector<unsigned char> buffer_uncompr(uncompr_bytes);
212 size_t nread = fread(&buffer_compr[0],1,compr_bytes,fp);
213 if(nread != compr_bytes)
214 throw std::runtime_error(
"load_the_npy_file: failed fread");
218 d_stream.zalloc = Z_NULL;
219 d_stream.zfree = Z_NULL;
220 d_stream.opaque = Z_NULL;
221 d_stream.avail_in = 0;
222 d_stream.next_in = Z_NULL;
223 inflateInit2(&d_stream, -MAX_WBITS);
225 d_stream.avail_in = compr_bytes;
226 d_stream.next_in = &buffer_compr[0];
227 d_stream.avail_out = uncompr_bytes;
228 d_stream.next_out = &buffer_uncompr[0];
230 inflate(&d_stream, Z_FINISH);
231 inflateEnd(&d_stream);
233 std::vector<size_t> shape;
240 size_t offset = uncompr_bytes - array.
num_bytes();
241 memcpy(array.
data<
unsigned char>(),&buffer_uncompr[0]+offset,array.
num_bytes());
247 FILE*
fp = fopen(fname.c_str(),
"rb");
250 throw std::runtime_error(
"npz_load: Error! Unable to open file "+fname+
"!");
256 std::vector<char> local_header(30);
257 size_t headerres = fread(&local_header[0],
sizeof(
char),30,fp);
259 throw std::runtime_error(
"npz_load: failed fread");
262 if(local_header[2] != 0x03 || local_header[3] != 0x04)
break;
267 size_t vname_res = fread(&varname[0],
sizeof(
char),name_len,fp);
268 if(vname_res != name_len)
269 throw std::runtime_error(
"npz_load: failed fread");
272 varname.erase(varname.end()-4,varname.end());
276 if(extra_field_len > 0) {
277 std::vector<char> buff(extra_field_len);
278 size_t efield_res = fread(&buff[0],
sizeof(
char),extra_field_len,fp);
279 if(efield_res != extra_field_len)
280 throw std::runtime_error(
"npz_load: failed fread");
296 FILE*
fp = fopen(fname.c_str(),
"rb");
298 if(!fp)
throw std::runtime_error(
"npz_load: Unable to open file "+fname);
301 std::vector<char> local_header(30);
302 size_t header_res = fread(&local_header[0],
sizeof(
char),30,fp);
304 throw std::runtime_error(
"npz_load: failed fread");
307 if(local_header[2] != 0x03 || local_header[3] != 0x04)
break;
312 size_t vname_res = fread(&vname[0],
sizeof(
char),name_len,fp);
313 if(vname_res != name_len)
314 throw std::runtime_error(
"npz_load: failed fread");
315 vname.erase(vname.end()-4,vname.end());
319 fseek(fp,extra_field_len,SEEK_CUR);
325 if(vname == varname) {
333 fseek(fp,size,SEEK_CUR);
340 throw std::runtime_error(
"npz_load: Variable name "+varname+
" not found in "+fname);
345 FILE*
fp = fopen(fname.c_str(),
"rb");
347 if(!fp)
throw std::runtime_error(
"npy_load: Unable to open file "+fname);
char map_type(const std::type_info &t)
cnpy::NpyArray load_the_npy_file(FILE *fp)
void parse_zip_footer(FILE *fp, uint16_t &nrecs, size_t &global_header_size, size_t &global_header_offset)
npz_t npz_load(std::string fname)
cnpy::NpyArray load_the_npz_array(FILE *fp, uint32_t compr_bytes, uint32_t uncompr_bytes)
NpyArray npy_load(std::string fname)
std::map< std::string, NpyArray > npz_t
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
auto array(Array const &a)
Returns a manipulator which will print the specified array.
std::string str(const std::pair< Type, Type > &tt)
void parse_npy_header(FILE *fp, size_t &word_size, std::vector< size_t > &shape, bool &fortran_order)
byte_as<> byte
Type of data size stored in bytes, in long long precision.
std::vector< char > & operator+=(std::vector< char > &lhs, const T rhs)