NpzFile ======= .. cpp:class:: numpy::NpzFile numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use NpzFile NpzFile obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``NpzFile(const std::string &filename)`` - NP_NPZ_UTILS.H:377 - I/O --- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void load(const std::string &filename)`` - void - NP_NPZ_UTILS.H:385 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void close()`` - void - NP_NPZ_UTILS.H:489 - :ref:`View ` * - ``bool contains(const std::string &name)`` - bool - NP_NPZ_UTILS.H:485 - :ref:`View ` Code Examples ------------- The following examples are extracted from the test suite. .. _example-npzfile-load-0: .. dropdown:: load (np_test_2_all.cpp:9819) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9809 :emphasize-lines: 11 void testBinaryIOBasic() { std::cout << "========= testBasicBinaryIO ======================="; // Test 1D integer array auto array_1d = createInt32Array({ 5 }, 0); for (size_t i = 0; i < 5; ++i) { array_1d.setElementAt({ i }, static_cast(i + 1)); } save(array_1d, TEMP_DIR + "test_1d.npy"); auto loaded_1d = load(TEMP_DIR + "test_1d.npy"); if (!(loaded_1d.getShape() == array_1d.getShape())) { std::string description = std::string("testBinaryIOBasic():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(loaded_1d.getShape() == array_1d.getShape())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } for (size_t i = 0; i < 5; ++i) { if (!(loaded_1d.getElementAt({ i }) == array_1d.getElementAt({ i }))) { std::string description = std::string("testBinaryIOBasic():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(loaded_1d.getElementAt({ i }) == array_1d.getElementAt({ i }))"; std::cout << std::string("[FAIL] ") + description << std::endl; .. _example-npzfile-close-1: .. dropdown:: close (np_test_1_all.cpp:26157) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26147 :emphasize-lines: 11 void np_test_recarray_fromfile() { std::cout << "========= fromfile: create RecordArray from file ======================="; // Create temporary test file std::string filename = "D:/Projects/Cpp2/temp/test_recarray.csv"; std::ofstream outfile(filename); outfile << "1,10.5\n"; outfile << "2,20.5\n"; outfile << "3,30.5\n"; outfile.close(); // Define dtype std::vector> fields = { {"id", numpy::DType::INT32}, {"value", numpy::DType::FLOAT64} }; auto dtype = std::make_shared(fields); // Load from file auto result = numpy::fromfile(filename, dtype, ","); .. _example-npzfile-contains-2: .. dropdown:: contains (np_test_2_all.cpp:10521) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 10511 :emphasize-lines: 11 auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz"); // Verify keys auto keys = npz_file.keys(); if (!(keys.size() == 3)) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("integers"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("floats"))) { std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } if (!(npz_file.contains("complex_nums"))) {