18 return (((
char *) &x)[0]) ?
'<' :
'>';
22 if (t ==
typeid(
float))
return 'f';
23 if (t ==
typeid(
double))
return 'f';
24 if (t ==
typeid(
long double))
return 'f';
26 if (t ==
typeid(
int))
return 'i';
27 if (t ==
typeid(
char))
return 'i';
28 if (t ==
typeid(
short))
return 'i';
29 if (t ==
typeid(
long))
return 'i';
30 if (t ==
typeid(
long long))
return 'i';
32 if (t ==
typeid(
unsigned char))
return 'u';
33 if (t ==
typeid(
unsigned short))
return 'u';
34 if (t ==
typeid(
unsigned long))
return 'u';
35 if (t ==
typeid(
unsigned long long))
return 'u';
36 if (t ==
typeid(
unsigned int))
return 'u';
38 if (t ==
typeid(
bool))
return 'b';
40 if (t ==
typeid(std::complex < float > ))
return 'c';
41 if (t ==
typeid(std::complex < double > ))
return 'c';
42 if (t ==
typeid(std::complex < long double > ))
return 'c';
49 lhs.insert(lhs.end(), rhs.begin(), rhs.end());
56 size_t len = strlen(rhs);
58 for (
size_t byte = 0;
byte < len;
byte++) {
59 lhs.push_back(rhs[
byte]);
67 uint8_t major_version = *
reinterpret_cast<uint8_t *
>(buffer + 6);
68 uint8_t minor_version = *
reinterpret_cast<uint8_t *
>(buffer + 7);
69 uint16_t header_len = *
reinterpret_cast<uint16_t *
>(buffer + 8);
70 std::string header(
reinterpret_cast<char *
>(buffer + 9), header_len);
75 loc1 = header.find(
"fortran_order") + 16;
76 fortran_order = (header.substr(loc1, 4) ==
"True" ? true :
false);
79 loc1 = header.find(
"(");
80 loc2 = header.find(
")");
82 std::regex num_regex(
"[0-9][0-9]*");
86 std::string str_shape = header.substr(loc1 + 1, loc2 - loc1 - 1);
87 while (std::regex_search(str_shape, sm, num_regex)) {
88 shape.push_back(std::stoi(sm[0].str()));
89 str_shape = sm.suffix().str();
95 loc1 = header.find(
"descr") + 9;
96 bool littleEndian = (header[loc1] ==
'<' || header[loc1] ==
'|' ? true :
false);
102 std::string str_ws = header.substr(loc1 + 2);
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");
112 std::string header = fgets(buffer, 256, fp);
113 assert(header[header.size() - 1] ==
'\n');
118 loc1 = header.find(
"fortran_order");
119 if (loc1 == std::string::npos)
120 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'fortran_order'");
122 fortran_order = (header.substr(loc1, 4) ==
"True" ? true :
false);
125 loc1 = header.find(
"(");
126 loc2 = header.find(
")");
127 if (loc1 == std::string::npos || loc2 == std::string::npos)
128 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: '(' or ')'");
130 std::regex num_regex(
"[0-9][0-9]*");
134 std::string str_shape = header.substr(loc1 + 1, loc2 - loc1 - 1);
135 while (std::regex_search(str_shape, sm, num_regex)) {
136 shape.push_back(std::stoi(sm[0].str()));
137 str_shape = sm.suffix().str();
143 loc1 = header.find(
"descr");
144 if (loc1 == std::string::npos)
145 throw std::runtime_error(
"parse_npy_header: failed to find header keyword: 'descr'");
147 bool littleEndian = (header[loc1] ==
'<' || header[loc1] ==
'|' ? true :
false);
148 assert(littleEndian);
153 std::string str_ws = header.substr(loc1 + 2);
154 loc2 = str_ws.find(
"'");
155 word_size = atoi(str_ws.substr(0, loc2).c_str());
159 std::vector<char> footer(22);
160 fseek(fp, -22, SEEK_END);
161 size_t res = fread(&footer[0],
sizeof(
char), 22, fp);
163 throw std::runtime_error(
"parse_zip_footer: failed fread");
165 uint16_t disk_no, disk_start, nrecs_on_disk, comment_len;
166 disk_no = *(uint16_t * ) & footer[4];
167 disk_start = *(uint16_t * ) & footer[6];
168 nrecs_on_disk = *(uint16_t * ) & footer[8];
169 nrecs = *(uint16_t * ) & footer[10];
170 global_header_size = *(uint32_t * ) & footer[12];
171 global_header_offset = *(uint32_t * ) & footer[16];
172 comment_len = *(uint16_t * ) & footer[20];
174 assert(disk_no == 0);
175 assert(disk_start == 0);
176 assert(nrecs_on_disk == nrecs);
177 assert(comment_len == 0);
181 std::vector <size_t> shape;
187 size_t nread = fread(arr.
data<
char>(), 1, arr.
num_bytes(), fp);
189 throw std::runtime_error(
"load_the_npy_file: failed fread");
195 std::vector<unsigned char> buffer_compr(compr_bytes);
196 std::vector<unsigned char> buffer_uncompr(uncompr_bytes);
197 size_t nread = fread(&buffer_compr[0], 1, compr_bytes, fp);
198 if (nread != compr_bytes)
199 throw std::runtime_error(
"load_the_npy_file: failed fread");
204 d_stream.zalloc = Z_NULL;
205 d_stream.zfree = Z_NULL;
206 d_stream.opaque = Z_NULL;
207 d_stream.avail_in = 0;
208 d_stream.next_in = Z_NULL;
209 err = inflateInit2(&d_stream, -MAX_WBITS);
211 d_stream.avail_in = compr_bytes;
212 d_stream.next_in = &buffer_compr[0];
213 d_stream.avail_out = uncompr_bytes;
214 d_stream.next_out = &buffer_uncompr[0];
216 err = inflate(&d_stream, Z_FINISH);
217 err = inflateEnd(&d_stream);
219 std::vector <size_t> shape;
226 size_t offset = uncompr_bytes - array.
num_bytes();
227 memcpy(array.
data<
unsigned char>(), &buffer_uncompr[0] + offset, array.
num_bytes());
233 FILE *fp = fopen(fname.c_str(),
"rb");
236 throw std::runtime_error(
"npz_load: Error! Unable to open file " + fname +
"!");
242 std::vector<char> local_header(30);
243 size_t headerres = fread(&local_header[0],
sizeof(
char), 30, fp);
245 throw std::runtime_error(
"npz_load: failed fread");
248 if (local_header[2] != 0x03 || local_header[3] != 0x04)
break;
251 uint16_t name_len = *(uint16_t * ) & local_header[26];
252 std::string varname(name_len,
' ');
253 size_t vname_res = fread(&varname[0],
sizeof(
char), name_len, fp);
254 if (vname_res != name_len)
255 throw std::runtime_error(
"npz_load: failed fread");
258 varname.erase(varname.end() - 4, varname.end());
261 uint16_t extra_field_len = *(uint16_t * ) & local_header[28];
262 if (extra_field_len > 0) {
263 std::vector<char> buff(extra_field_len);
264 size_t efield_res = fread(&buff[0],
sizeof(
char), extra_field_len, fp);
265 if (efield_res != extra_field_len)
266 throw std::runtime_error(
"npz_load: failed fread");
269 uint16_t compr_method = *
reinterpret_cast<uint16_t *
>(&local_header[0] + 8);
270 uint32_t compr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0] + 18);
271 uint32_t uncompr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0] + 22);
282 FILE *fp = fopen(fname.c_str(),
"rb");
284 if (!fp)
throw std::runtime_error(
"npz_load: Unable to open file " + fname);
287 std::vector<char> local_header(30);
288 size_t header_res = fread(&local_header[0],
sizeof(
char), 30, fp);
289 if (header_res != 30)
290 throw std::runtime_error(
"npz_load: failed fread");
293 if (local_header[2] != 0x03 || local_header[3] != 0x04)
break;
296 uint16_t name_len = *(uint16_t * ) & local_header[26];
297 std::string vname(name_len,
' ');
298 size_t vname_res = fread(&vname[0],
sizeof(
char), name_len, fp);
299 if (vname_res != name_len)
300 throw std::runtime_error(
"npz_load: failed fread");
301 vname.erase(vname.end() - 4, vname.end());
304 uint16_t extra_field_len = *(uint16_t * ) & local_header[28];
305 fseek(fp, extra_field_len, SEEK_CUR);
307 uint16_t compr_method = *
reinterpret_cast<uint16_t *
>(&local_header[0] + 8);
308 uint32_t compr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0] + 18);
309 uint32_t uncompr_bytes = *
reinterpret_cast<uint32_t *
>(&local_header[0] + 22);
311 if (vname == varname) {
318 uint32_t size = *(uint32_t * ) & local_header[22];
319 fseek(fp, size, SEEK_CUR);
326 throw std::runtime_error(
"npz_load: Variable name " + varname +
" not found in " + fname);
331 FILE *fp = fopen(fname.c_str(),
"rb");
333 if (!fp)
throw std::runtime_error(
"npy_load: Unable to open file " + fname);