Attrs#

class numpy::Attrs#

numpy C++ class.

Example#

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

// Use Attrs
Attrs obj;
// ... operations ...

Constructors#

Signature

Location

Example

Attrs() = default

df_attrs.h:51

Attrs(const Attrs&) = default

df_attrs.h:56

Attrs(Attrs&&) = default

df_attrs.h:61

Array Creation#

Signature

Return Type

Location

Example

bool empty() const

bool

df_attrs.h:179

View

Indexing / Selection#

Signature

Return Type

Location

Example

T get(const std::string& key) const

T

df_attrs.h:102

View

T get(const std::string& key, T default_value) const

T

df_attrs.h:118

View

Type Handling#

Signature

Return Type

Location

Example

Attrs copy() const

Attrs

df_attrs.h:228

View

Other Methods#

Signature

Return Type

Location

Example

void clear()

void

df_attrs.h:163

View

bool contains(const std::string& key) const

bool

df_attrs.h:78

View

bool has_type(const std::string& key) const

bool

df_attrs.h:203

std::vector<std::string> keys() const

std::vector<std::string>

df_attrs.h:187

View

void merge(const Attrs& other, bool overwrite = true)

void

df_attrs.h:216

bool remove(const std::string& key)

bool

df_attrs.h:156

View

void set(const std::string& key, T&& value)

void

df_attrs.h:89

View

size_t size() const

size_t

df_attrs.h:171

View

bool try_get(const std::string& key, T& out_value) const

bool

df_attrs.h:138

Code Examples#

The following examples are extracted from the test suite.

empty (np_test_1_all.cpp:6316)
6306}
6307
6308void test_data_generator_emptyBenchmarkSorting() {
6309    std::cout << "========= test_data_generator_empty =======================";
6310
6311    DataGenerator<int> gen(42);
6312
6313    // Test empty data generation
6314    std::vector<int> data = gen.generate(0, DataPattern::RANDOM);
6315
6316    if (!(data.empty())) {
6317        std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())";
6318        std::cout << std::string("[FAIL] ") + description << std::endl;
6319        throw std::runtime_error(description);
6320    }
6321
6322    // std::cout << "Empty data generation test passed" << std::endl;
6323
6324    std::cout << " -> tests passed" << std::endl;
6325}
get (np_test_1_all.cpp:28526)
28516      std::cout << " -> tests passed" << std::endl;
28517    }
28518
28519    void np_test_indexing_mask_indices() {
28520      std::cout << "========= mask_indices: triangular mask indices =======================";
28521
28522      // Get upper triangular indices for 3x3 matrix
28523      auto triu_idx = numpy::mask_indices(3, "triu", 0);
28524
28525      // Should return tuple of 2 arrays
28526      bool passed = (std::get<0>(triu_idx).getSize() > 0);
28527      passed = passed && (std::get<1>(triu_idx).getSize() > 0);
28528      passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize());
28529
28530      // Get lower triangular indices
28531      auto tril_idx = numpy::mask_indices(3, "tril", 0);
28532      passed = passed && (std::get<0>(tril_idx).getSize() > 0);
28533      passed = passed && (std::get<1>(tril_idx).getSize() > 0);
28534
28535      if (!passed) {
28536        std::cout << "  [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect";
get (np_test_1_all.cpp:28526)
28516      std::cout << " -> tests passed" << std::endl;
28517    }
28518
28519    void np_test_indexing_mask_indices() {
28520      std::cout << "========= mask_indices: triangular mask indices =======================";
28521
28522      // Get upper triangular indices for 3x3 matrix
28523      auto triu_idx = numpy::mask_indices(3, "triu", 0);
28524
28525      // Should return tuple of 2 arrays
28526      bool passed = (std::get<0>(triu_idx).getSize() > 0);
28527      passed = passed && (std::get<1>(triu_idx).getSize() > 0);
28528      passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize());
28529
28530      // Get lower triangular indices
28531      auto tril_idx = numpy::mask_indices(3, "tril", 0);
28532      passed = passed && (std::get<0>(tril_idx).getSize() > 0);
28533      passed = passed && (std::get<1>(tril_idx).getSize() > 0);
28534
28535      if (!passed) {
28536        std::cout << "  [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect";
copy (np_test_1_all.cpp:9812)
9802    //original.printArray();
9803
9804    // The modification should be at position (1,1) in original
9805    if (!(original.getElementAt({1, 1}) == 9999)) {
9806        std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)";
9807        std::cout << std::string("[FAIL] ") + description << std::endl;
9808        throw std::runtime_error(description);
9809    }
9810    // std::cout << "[OK] Slice view shares memory with original";
9811
9812    // Test slice copy (should be independent)
9813    auto slice_copy = original.sliceArray({{2, 4}, {2, 4}});
9814    // std::cout << "Slice copy [2:4, 2:4]:";
9815    //slice_copy.printArray();
9816
9817    slice_copy.setElementAt({0, 0}, 7777);
9818
9819    // std::cout << "After modifying slice copy at (0,0):" << std::endl;
9820    // std::cout << "Original array:" << std::endl;
9821    //original.printArray();
9822    // std::cout << "Slice copy:" << std::endl;
clear (np_test_2_all.cpp:4161)
4151        auto array = createFloat32Array({ 10, 10 }, static_cast<float>(i));
4152        temp_arrays.push_back(array);
4153
4154        auto view = array.view();
4155        temp_views.push_back(view);
4156      }
4157
4158      // std::cout << "Created 100 arrays and 100 views" << std::endl;
4159
4160      // Clear all arrays (views will keep memory alive)
4161      temp_arrays.clear();
4162      // std::cout << "Cleared original arrays" << std::endl;
4163
4164      // Verify views still work
4165      if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) {
4166          std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)";
4167          std::cout << std::string("[FAIL] ") + description << std::endl;
4168          throw std::runtime_error(description);
4169      }
4170      // std::cout << "[OK] Views still valid after originals cleared";
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"))) {
keys (np_test_2_all.cpp:8614)
8604          if (!indices.empty()) {
8605            std::cout << "   IPP empty array test:                                                                   -> [FAIL]";
8606            throw std::runtime_error("IPP empty array test: FAILED - should return empty");
8607          }
8608          // std::cout << "[OK] IPP empty array test: PASSED" << std::endl;
8609        }
8610
8611        // Test 6: Large array performance
8612        {
8613          const size_t n = 10000;
8614          std::vector<std::vector<int>> keys(2);
8615          keys[0].resize(n);
8616          keys[1].resize(n);
8617
8618          std::mt19937 gen(42);
8619          std::uniform_int_distribution<int> dist(0, 1000);
8620
8621          for (size_t i = 0; i < n; ++i) {
8622            keys[0][i] = dist(gen);
8623            keys[1][i] = dist(gen);
8624          }
remove (np_test_1_all.cpp:26177)
26167      auto result = numpy::fromfile(filename, dtype, ",");
26168
26169      // Verify loaded correctly
26170      bool passed = (result.getSize() == 3);
26171      if (passed) {
26172        passed = (result.getFieldValue<numpy::int32>({ 0 }, "id") == 1);
26173        passed = passed && (std::abs(result.getFieldValue<numpy::float64>({ 1 }, "value") - 20.5) < 0.001);
26174      }
26175
26176      // Clean up temp file
26177      std::remove(filename.c_str());
26178
26179      if (!passed) {
26180        std::cout << "  [FAIL] : in np_test_recarray_fromfile() : From file failed";
26181        throw std::runtime_error("np_test_recarray_fromfile failed");
26182      }
26183
26184      std::cout << " -> tests passed" << std::endl;
26185    }
26186
26187    void np_test_recarray_getDTypeFromType() {
set (np_test_1_all.cpp:6747)
6737    std::cout << "========= testArrayPrinting =======================";
6738
6739    // Test 1D array printing
6740    std::vector<std::string> words = {"Hello", "World", "Test"};
6741    auto arr_1d = array<16>(words);
6742    // std::cout << "1D CharArray:" << std::endl;
6743    // arr_1d.print();
6744
6745    // Test 2D array printing
6746    chararray16 arr_2d({2, 2}, "test");
6747    arr_2d.set({0, 0}, "A");
6748    arr_2d.set({0, 1}, "B");
6749    arr_2d.set({1, 0}, "C");
6750    arr_2d.set({1, 1}, "D");
6751
6752    // std::cout << "2D CharArray:" << std::endl;
6753    // arr_2d.print();
6754
6755    std::cout << " -> tests passed" << std::endl;
6756}
size (np_test_1_all.cpp:47)
37using namespace numpy;
38using namespace numpy::benchmark;
39using namespace numpy::char_;
40
41// Helper functions for array comparison tests
42namespace {
43    const double TOLERANCE = 1e-10;
44
45    bool allTrue(const NDArray<bool>& arr) {
46        std::vector<size_t> indices(arr.getShape().size(), 0);
47        do {
48            if (!arr.getElementAt(indices)) {
49                return false;
50            }
51        } while (incrementIndices(indices, arr.getShape()));
52        return true;
53    }
54
55    bool allFalse(const NDArray<bool>& arr) {
56        std::vector<size_t> indices(arr.getShape().size(), 0);