hdf5.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
  2. // Copyright 2017 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. #include <cstdio>
  16. #include <armadillo>
  17. #include "catch.hpp"
  18. using namespace arma;
  19. #if defined(ARMA_USE_HDF5)
  20. TEST_CASE("hdf5_u8_test")
  21. {
  22. arma::Mat<u8> a;
  23. a.randu(20, 20);
  24. // Save first.
  25. a.save("file.h5", hdf5_binary);
  26. // Load as different matrix.
  27. arma::Mat<u8> b;
  28. b.load("file.h5", hdf5_binary);
  29. // Check that they are the same.
  30. for (uword i = 0; i < a.n_elem; ++i)
  31. {
  32. REQUIRE( a[i] == b[i] );
  33. }
  34. // Now autoload.
  35. arma::Mat<u8> c;
  36. c.load("file.h5");
  37. // Check that they are the same.
  38. for (uword i = 0; i < a.n_elem; ++i)
  39. {
  40. REQUIRE( a[i] == c[i] );
  41. }
  42. std::remove("file.h5");
  43. }
  44. TEST_CASE("hdf5_u16_test")
  45. {
  46. arma::Mat<u16> a;
  47. a.randu(20, 20);
  48. // Save first.
  49. a.save("file.h5", hdf5_binary);
  50. // Load as different matrix.
  51. arma::Mat<u16> b;
  52. b.load("file.h5", hdf5_binary);
  53. // Check that they are the same.
  54. for (uword i = 0; i < a.n_elem; ++i)
  55. {
  56. REQUIRE( a[i] == b[i] );
  57. }
  58. // Now autoload.
  59. arma::Mat<u16> c;
  60. c.load("file.h5");
  61. // Check that they are the same.
  62. for (uword i = 0; i < a.n_elem; ++i)
  63. {
  64. REQUIRE( a[i] == c[i] );
  65. }
  66. std::remove("file.h5");
  67. }
  68. TEST_CASE("hdf5_u32_test")
  69. {
  70. arma::Mat<u32> a;
  71. a.randu(20, 20);
  72. // Save first.
  73. a.save("file.h5", hdf5_binary);
  74. // Load as different matrix.
  75. arma::Mat<u32> b;
  76. b.load("file.h5", hdf5_binary);
  77. // Check that they are the same.
  78. for (uword i = 0; i < a.n_elem; ++i)
  79. {
  80. REQUIRE( a[i] == b[i] );
  81. }
  82. // Now autoload.
  83. arma::Mat<u32> c;
  84. c.load("file.h5");
  85. // Check that they are the same.
  86. for (uword i = 0; i < a.n_elem; ++i)
  87. {
  88. REQUIRE( a[i] == c[i] );
  89. }
  90. std::remove("file.h5");
  91. }
  92. #ifdef ARMA_USE_U64S64
  93. TEST_CASE("hdf5_u64_test")
  94. {
  95. arma::Mat<u64> a;
  96. a.randu(20, 20);
  97. // Save first.
  98. a.save("file.h5", hdf5_binary);
  99. // Load as different matrix.
  100. arma::Mat<u64> b;
  101. b.load("file.h5", hdf5_binary);
  102. // Check that they are the same.
  103. for (uword i = 0; i < a.n_elem; ++i)
  104. {
  105. REQUIRE( a[i] == b[i] );
  106. }
  107. // Now autoload.
  108. arma::Mat<u64> c;
  109. c.load("file.h5");
  110. // Check that they are the same.
  111. for (uword i = 0; i < a.n_elem; ++i)
  112. {
  113. REQUIRE( a[i] == c[i] );
  114. }
  115. std::remove("file.h5");
  116. }
  117. #endif
  118. TEST_CASE("hdf5_s8_test")
  119. {
  120. arma::Mat<s8> a;
  121. a.randu(20, 20);
  122. // Save first.
  123. a.save("file.h5", hdf5_binary);
  124. // Load as different matrix.
  125. arma::Mat<s8> b;
  126. b.load("file.h5", hdf5_binary);
  127. // Check that they are the same.
  128. for (uword i = 0; i < a.n_elem; ++i)
  129. {
  130. REQUIRE( a[i] == b[i] );
  131. }
  132. // Now autoload.
  133. arma::Mat<s8> c;
  134. c.load("file.h5");
  135. // Check that they are the same.
  136. for (uword i = 0; i < a.n_elem; ++i)
  137. {
  138. REQUIRE( a[i] == c[i] );
  139. }
  140. std::remove("file.h5");
  141. }
  142. TEST_CASE("hdf5_s16_test")
  143. {
  144. arma::Mat<s16> a;
  145. a.randu(20, 20);
  146. // Save first.
  147. a.save("file.h5", hdf5_binary);
  148. // Load as different matrix.
  149. arma::Mat<s16> b;
  150. b.load("file.h5", hdf5_binary);
  151. // Check that they are the same.
  152. for (uword i = 0; i < a.n_elem; ++i)
  153. {
  154. REQUIRE( a[i] == b[i] );
  155. }
  156. // Now autoload.
  157. arma::Mat<s16> c;
  158. c.load("file.h5");
  159. // Check that they are the same.
  160. for (uword i = 0; i < a.n_elem; ++i)
  161. {
  162. REQUIRE( a[i] == c[i] );
  163. }
  164. std::remove("file.h5");
  165. }
  166. TEST_CASE("hdf5_s32_test")
  167. {
  168. arma::Mat<s32> a;
  169. a.randu(20, 20);
  170. // Save first.
  171. a.save("file.h5", hdf5_binary);
  172. // Load as different matrix.
  173. arma::Mat<s32> b;
  174. b.load("file.h5", hdf5_binary);
  175. // Check that they are the same.
  176. for (uword i = 0; i < a.n_elem; ++i)
  177. {
  178. REQUIRE( a[i] == b[i] );
  179. }
  180. // Now autoload.
  181. arma::Mat<s32> c;
  182. c.load("file.h5");
  183. // Check that they are the same.
  184. for (uword i = 0; i < a.n_elem; ++i)
  185. {
  186. REQUIRE( a[i] == c[i] );
  187. }
  188. std::remove("file.h5");
  189. }
  190. #ifdef ARMA_USE_U64S64
  191. TEST_CASE("hdf5_s64_test")
  192. {
  193. arma::Mat<s64> a;
  194. a.randu(20, 20);
  195. // Save first.
  196. a.save("file.h5", hdf5_binary);
  197. // Load as different matrix.
  198. arma::Mat<s64> b;
  199. b.load("file.h5", hdf5_binary);
  200. // Check that they are the same.
  201. for (uword i = 0; i < a.n_elem; ++i)
  202. {
  203. REQUIRE( a[i] == b[i] );
  204. }
  205. // Now autoload.
  206. arma::Mat<s64> c;
  207. c.load("file.h5");
  208. // Check that they are the same.
  209. for (uword i = 0; i < a.n_elem; ++i)
  210. {
  211. REQUIRE( a[i] == c[i] );
  212. }
  213. std::remove("file.h5");
  214. }
  215. #endif
  216. TEST_CASE("hdf5_char_test")
  217. {
  218. arma::Mat<char> a;
  219. a.randu(20, 20);
  220. // Save first.
  221. a.save("file.h5", hdf5_binary);
  222. // Load as different matrix.
  223. arma::Mat<char> b;
  224. b.load("file.h5", hdf5_binary);
  225. // Check that they are the same.
  226. for (uword i = 0; i < a.n_elem; ++i)
  227. {
  228. REQUIRE( a[i] == b[i] );
  229. }
  230. // Now autoload.
  231. arma::Mat<char> c;
  232. c.load("file.h5");
  233. // Check that they are the same.
  234. for (uword i = 0; i < a.n_elem; ++i)
  235. {
  236. REQUIRE( a[i] == c[i] );
  237. }
  238. std::remove("file.h5");
  239. }
  240. TEST_CASE("hdf5_int_test")
  241. {
  242. arma::Mat<signed int> a;
  243. a.randu(20, 20);
  244. // Save first.
  245. a.save("file.h5", hdf5_binary);
  246. // Load as different matrix.
  247. arma::Mat<signed int> b;
  248. b.load("file.h5", hdf5_binary);
  249. // Check that they are the same.
  250. for (uword i = 0; i < a.n_elem; ++i)
  251. {
  252. REQUIRE( a[i] == b[i] );
  253. }
  254. // Now autoload.
  255. arma::Mat<signed int> c;
  256. c.load("file.h5");
  257. // Check that they are the same.
  258. for (uword i = 0; i < a.n_elem; ++i)
  259. {
  260. REQUIRE( a[i] == c[i] );
  261. }
  262. std::remove("file.h5");
  263. }
  264. TEST_CASE("hdf5_uint_test")
  265. {
  266. arma::Mat<unsigned int> a;
  267. a.randu(20, 20);
  268. // Save first.
  269. a.save("file.h5", hdf5_binary);
  270. // Load as different matrix.
  271. arma::Mat<unsigned int> b;
  272. b.load("file.h5", hdf5_binary);
  273. // Check that they are the same.
  274. for (uword i = 0; i < a.n_elem; ++i)
  275. {
  276. REQUIRE( a[i] == b[i] );
  277. }
  278. // Now autoload.
  279. arma::Mat<unsigned int> c;
  280. c.load("file.h5");
  281. // Check that they are the same.
  282. for (uword i = 0; i < a.n_elem; ++i)
  283. {
  284. REQUIRE( a[i] == c[i] );
  285. }
  286. std::remove("file.h5");
  287. }
  288. TEST_CASE("hdf5_short_test")
  289. {
  290. arma::Mat<signed short> a;
  291. a.randu(20, 20);
  292. // Save first.
  293. a.save("file.h5", hdf5_binary);
  294. // Load as different matrix.
  295. arma::Mat<signed short> b;
  296. b.load("file.h5", hdf5_binary);
  297. // Check that they are the same.
  298. for (uword i = 0; i < a.n_elem; ++i)
  299. {
  300. REQUIRE( a[i] == b[i] );
  301. }
  302. // Now autoload.
  303. arma::Mat<signed short> c;
  304. c.load("file.h5");
  305. // Check that they are the same.
  306. for (uword i = 0; i < a.n_elem; ++i)
  307. {
  308. REQUIRE( a[i] == c[i] );
  309. }
  310. std::remove("file.h5");
  311. }
  312. TEST_CASE("hdf5_ushort_test")
  313. {
  314. arma::Mat<unsigned short> a;
  315. a.randu(20, 20);
  316. // Save first.
  317. a.save("file.h5", hdf5_binary);
  318. // Load as different matrix.
  319. arma::Mat<unsigned short> b;
  320. b.load("file.h5", hdf5_binary);
  321. // Check that they are the same.
  322. for (uword i = 0; i < a.n_elem; ++i)
  323. {
  324. REQUIRE( a[i] == b[i] );
  325. }
  326. // Now autoload.
  327. arma::Mat<unsigned short> c;
  328. c.load("file.h5");
  329. // Check that they are the same.
  330. for (uword i = 0; i < a.n_elem; ++i)
  331. {
  332. REQUIRE( a[i] == c[i] );
  333. }
  334. std::remove("file.h5");
  335. }
  336. TEST_CASE("hdf5_long_test")
  337. {
  338. arma::Mat<signed long> a;
  339. a.randu(20, 20);
  340. // Save first.
  341. a.save("file.h5", hdf5_binary);
  342. // Load as different matrix.
  343. arma::Mat<signed long> b;
  344. b.load("file.h5", hdf5_binary);
  345. // Check that they are the same.
  346. for (uword i = 0; i < a.n_elem; ++i)
  347. {
  348. REQUIRE( a[i] == b[i] );
  349. }
  350. // Now autoload.
  351. arma::Mat<signed long> c;
  352. c.load("file.h5");
  353. // Check that they are the same.
  354. for (uword i = 0; i < a.n_elem; ++i)
  355. {
  356. REQUIRE( a[i] == c[i] );
  357. }
  358. std::remove("file.h5");
  359. }
  360. TEST_CASE("hdf5_ulong_test")
  361. {
  362. arma::Mat<unsigned long> a;
  363. a.randu(20, 20);
  364. // Save first.
  365. a.save("file.h5", hdf5_binary);
  366. // Load as different matrix.
  367. arma::Mat<unsigned long> b;
  368. b.load("file.h5", hdf5_binary);
  369. // Check that they are the same.
  370. for (uword i = 0; i < a.n_elem; ++i)
  371. {
  372. REQUIRE( a[i] == b[i] );
  373. }
  374. // Now autoload.
  375. arma::Mat<unsigned long> c;
  376. c.load("file.h5");
  377. // Check that they are the same.
  378. for (uword i = 0; i < a.n_elem; ++i)
  379. {
  380. REQUIRE( a[i] == c[i] );
  381. }
  382. std::remove("file.h5");
  383. }
  384. #ifdef ARMA_USE_U64S64
  385. TEST_CASE("hdf5_llong_test")
  386. {
  387. arma::Mat<signed long long> a;
  388. a.randu(20, 20);
  389. // Save first.
  390. a.save("file.h5", hdf5_binary);
  391. // Load as different matrix.
  392. arma::Mat<signed long long> b;
  393. b.load("file.h5", hdf5_binary);
  394. // Check that they are the same.
  395. for (uword i = 0; i < a.n_elem; ++i)
  396. {
  397. REQUIRE( a[i] == b[i] );
  398. }
  399. // Now autoload.
  400. arma::Mat<signed long long> c;
  401. c.load("file.h5");
  402. // Check that they are the same.
  403. for (uword i = 0; i < a.n_elem; ++i)
  404. {
  405. REQUIRE( a[i] == c[i] );
  406. }
  407. std::remove("file.h5");
  408. }
  409. TEST_CASE("hdf5_ullong_test")
  410. {
  411. arma::Mat<unsigned long long> a;
  412. a.randu(20, 20);
  413. // Save first.
  414. a.save("file.h5", hdf5_binary);
  415. // Load as different matrix.
  416. arma::Mat<unsigned long long> b;
  417. b.load("file.h5", hdf5_binary);
  418. // Check that they are the same.
  419. for (uword i = 0; i < a.n_elem; ++i)
  420. {
  421. REQUIRE( a[i] == b[i] );
  422. }
  423. // Now autoload.
  424. arma::Mat<unsigned long long> c;
  425. c.load("file.h5");
  426. // Check that they are the same.
  427. for (uword i = 0; i < a.n_elem; ++i)
  428. {
  429. REQUIRE( a[i] == c[i] );
  430. }
  431. std::remove("file.h5");
  432. }
  433. #endif
  434. TEST_CASE("hdf5_float_test")
  435. {
  436. arma::Mat<float> a;
  437. a.randu(20, 20);
  438. // Save first.
  439. a.save("file.h5", hdf5_binary);
  440. // Load as different matrix.
  441. arma::Mat<float> b;
  442. b.load("file.h5", hdf5_binary);
  443. // Check that they are the same.
  444. for (uword i = 0; i < a.n_elem; ++i)
  445. {
  446. REQUIRE( a[i] == b[i] );
  447. }
  448. // Now autoload.
  449. arma::Mat<float> c;
  450. c.load("file.h5");
  451. // Check that they are the same.
  452. for (uword i = 0; i < a.n_elem; ++i)
  453. {
  454. REQUIRE( a[i] == c[i] );
  455. }
  456. std::remove("file.h5");
  457. }
  458. TEST_CASE("hdf5_double_test")
  459. {
  460. arma::Mat<double> a;
  461. a.randu(20, 20);
  462. // Save first.
  463. a.save("file.h5", hdf5_binary);
  464. // Load as different matrix.
  465. arma::Mat<double> b;
  466. b.load("file.h5", hdf5_binary);
  467. // Check that they are the same.
  468. for (uword i = 0; i < a.n_elem; ++i)
  469. {
  470. REQUIRE( a[i] == b[i] );
  471. }
  472. // Now autoload.
  473. arma::Mat<double> c;
  474. c.load("file.h5");
  475. // Check that they are the same.
  476. for (uword i = 0; i < a.n_elem; ++i)
  477. {
  478. REQUIRE( a[i] == c[i] );
  479. }
  480. std::remove("file.h5");
  481. }
  482. TEST_CASE("hdf5_complex_float_test")
  483. {
  484. arma::Mat<std::complex<float> > a;
  485. a.randu(20, 20);
  486. // Save first.
  487. a.save("file.h5", hdf5_binary);
  488. // Load as different matrix.
  489. arma::Mat<std::complex<float> > b;
  490. b.load("file.h5", hdf5_binary);
  491. // Check that they are the same.
  492. for (uword i = 0; i < a.n_elem; ++i)
  493. {
  494. REQUIRE( a[i] == b[i] );
  495. }
  496. // Now autoload.
  497. arma::Mat<std::complex<float> > c;
  498. c.load("file.h5");
  499. // Check that they are the same.
  500. for (uword i = 0; i < a.n_elem; ++i)
  501. {
  502. REQUIRE( a[i] == c[i] );
  503. }
  504. std::remove("file.h5");
  505. }
  506. TEST_CASE("hdf5_complex_double_test")
  507. {
  508. arma::Mat<std::complex<double> > a;
  509. a.randu(20, 20);
  510. // Save first.
  511. a.save("file.h5", hdf5_binary);
  512. // Load as different matrix.
  513. arma::Mat<std::complex<double> > b;
  514. b.load("file.h5", hdf5_binary);
  515. // Check that they are the same.
  516. for (uword i = 0; i < a.n_elem; ++i)
  517. REQUIRE( a[i] == b[i] );
  518. // Now autoload.
  519. arma::Mat<std::complex<double> > c;
  520. c.load("file.h5");
  521. // Check that they are the same.
  522. for (uword i = 0; i < a.n_elem; ++i)
  523. {
  524. REQUIRE( a[i] == c[i] );
  525. }
  526. std::remove("file.h5");
  527. }
  528. TEST_CASE("hdf5_dataset_append_test")
  529. {
  530. arma::Mat<double> a;
  531. a.randu(20, 20);
  532. // Save first dataset.
  533. a.save( hdf5_name("file.h5", "dataset1") );
  534. arma::Mat<double> b;
  535. b.randu(10, 10);
  536. // Save second dataset.
  537. b.save( hdf5_name("file.h5", "dataset2", hdf5_opts::append) );
  538. // Load first dataset as different matrix.
  539. arma::Mat<double> c;
  540. c.load( hdf5_name("file.h5", "dataset1") );
  541. // Check that they are the same.
  542. for (uword i = 0; i < a.n_elem; ++i)
  543. {
  544. REQUIRE( a[i] == c[i] );
  545. }
  546. // Load second dataset as different matrix.
  547. arma::Mat<double> d;
  548. d.load( hdf5_name("file.h5", "dataset2") );
  549. // Check that they are the same.
  550. for (uword i = 0; i < b.n_elem; ++i)
  551. {
  552. REQUIRE( b[i] == d[i] );
  553. }
  554. std::remove("file.h5");
  555. }
  556. TEST_CASE("hdf5_cube_dataset_append_test")
  557. {
  558. arma::Mat<double> a;
  559. a.randu(20, 20);
  560. // Save first dataset.
  561. a.save( hdf5_name("file.h5", "dataset1") );
  562. arma::Cube<double> b;
  563. b.randu(10, 10, 10);
  564. // Save second dataset.
  565. b.save( hdf5_name("file.h5", "dataset2", hdf5_opts::append) );
  566. // Load first dataset as different matrix.
  567. arma::Mat<double> c;
  568. c.load( hdf5_name("file.h5", "dataset1") );
  569. // Check that they are the same.
  570. for (uword i = 0; i < a.n_elem; ++i)
  571. {
  572. REQUIRE( a[i] == c[i] );
  573. }
  574. // Load second dataset as different matrix.
  575. arma::Cube<double> d;
  576. d.load( hdf5_name("file.h5", "dataset2") );
  577. // Check that they are the same.
  578. for (uword i = 0; i < b.n_elem; ++i)
  579. {
  580. REQUIRE( b[i] == d[i] );
  581. }
  582. std::remove("file.h5");
  583. }
  584. TEST_CASE("hdf5_dataset_append-overwrite-test")
  585. {
  586. arma::Mat<double> a;
  587. a.randu(20, 20);
  588. // Save first dataset.
  589. a.save( hdf5_name("file.h5", "dataset1") );
  590. arma::Mat<double> b;
  591. b.randu(10, 10);
  592. // Save second dataset.
  593. b.save( hdf5_name("file.h5", "dataset2") );
  594. // Load first dataset as different matrix and check that first dataset has been overwritten.
  595. arma::Mat<double> c;
  596. REQUIRE_FALSE( c.load( hdf5_name("file.h5", "dataset1") ) );
  597. // Load second dataset as different matrix.
  598. arma::Mat<double> d;
  599. d.load( hdf5_name("file.h5", "dataset2") );
  600. // Check that they are the same.
  601. for (uword i = 0; i < b.n_elem; ++i)
  602. {
  603. REQUIRE( b[i] == d[i] );
  604. }
  605. std::remove("file.h5");
  606. }
  607. TEST_CASE("hdf5_dataset_same_dataset_twice_test")
  608. {
  609. arma::Mat<double> a;
  610. a.randu(20, 20);
  611. // Save first dataset.
  612. a.save(hdf5_name("file.h5", "dataset1"), hdf5_binary);
  613. arma::Mat<double> b;
  614. b.randu(10, 10);
  615. // Append second dataset with same name, causing failure.
  616. REQUIRE_FALSE( b.save(hdf5_name("file.h5", "dataset1", hdf5_opts::append) ) );
  617. std::remove("file.h5");
  618. }
  619. #endif