NpzFile#

class numpy::NpzFile#

numpy C++ class.

Example#

#include <numpy/np_ndarray.h>
using namespace numpy;

// Use NpzFile
NpzFile obj;
// ... operations ...

Constructors#

Signature

Location

Example

NpzFile(const std::string &filename)

NP_NPZ_UTILS.H:377

I/O#

Signature

Return Type

Location

Example

void load(const std::string &filename)

void

NP_NPZ_UTILS.H:385

View

Other Methods#

Signature

Return Type

Location

Example

void close()

void

NP_NPZ_UTILS.H:489

View

bool contains(const std::string &name)

bool

NP_NPZ_UTILS.H:485

View

Code Examples#

The following examples are extracted from the test suite.

load (np_test_2_all.cpp:9819)
9809    void testBinaryIOBasic() {
9810      std::cout << "========= testBasicBinaryIO =======================";
9811
9812      // Test 1D integer array
9813      auto array_1d = createInt32Array({ 5 }, 0);
9814      for (size_t i = 0; i < 5; ++i) {
9815        array_1d.setElementAt({ i }, static_cast<int32_t>(i + 1));
9816      }
9817
9818      save(array_1d, TEMP_DIR + "test_1d.npy");
9819      auto loaded_1d = load<int32_t>(TEMP_DIR + "test_1d.npy");
9820
9821      if (!(loaded_1d.getShape() == array_1d.getShape())) {
9822          std::string description = std::string("testBinaryIOBasic():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(loaded_1d.getShape() == array_1d.getShape())";
9823          std::cout << std::string("[FAIL] ") + description << std::endl;
9824          throw std::runtime_error(description);
9825      }
9826      for (size_t i = 0; i < 5; ++i) {
9827        if (!(loaded_1d.getElementAt({ i }) == array_1d.getElementAt({ i }))) {
9828            std::string description = std::string("testBinaryIOBasic():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(loaded_1d.getElementAt({ i }) == array_1d.getElementAt({ i }))";
9829            std::cout << std::string("[FAIL] ") + description << std::endl;
close (np_test_1_all.cpp:26157)
26147    void np_test_recarray_fromfile() {
26148      std::cout << "========= fromfile: create RecordArray from file =======================";
26149
26150      // Create temporary test file
26151      std::string filename = "D:/Projects/Cpp2/temp/test_recarray.csv";
26152      std::ofstream outfile(filename);
26153      outfile << "1,10.5\n";
26154      outfile << "2,20.5\n";
26155      outfile << "3,30.5\n";
26156      outfile.close();
26157
26158      // Define dtype
26159      std::vector<std::pair<std::string, numpy::DType>> fields = {
26160          {"id", numpy::DType::INT32},
26161          {"value", numpy::DType::FLOAT64}
26162      };
26163      auto dtype = std::make_shared<numpy::StructuredDType>(fields);
26164
26165      // Load from file
26166      auto result = numpy::fromfile(filename, dtype, ",");
contains (np_test_2_all.cpp:10521)
10511      auto npz_file = load_npz(TEMP_DIR + "test_multiple.npz");
10512
10513      // Verify keys
10514      auto keys = npz_file.keys();
10515      if (!(keys.size() == 3)) {
10516          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(keys.size() == 3)";
10517          std::cout << std::string("[FAIL] ") + description << std::endl;
10518          throw std::runtime_error(description);
10519      }
10520      if (!(npz_file.contains("integers"))) {
10521          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"integers\"))";
10522          std::cout << std::string("[FAIL] ") + description << std::endl;
10523          throw std::runtime_error(description);
10524      }
10525      if (!(npz_file.contains("floats"))) {
10526          std::string description = std::string("testNPZOperations():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(npz_file.contains(\"floats\"))";
10527          std::cout << std::string("[FAIL] ") + description << std::endl;
10528          throw std::runtime_error(description);
10529      }
10530      if (!(npz_file.contains("complex_nums"))) {