Attrs ===== .. cpp:class:: numpy::Attrs numpy C++ class. Example ------- .. code-block:: cpp #include using namespace numpy; // Use Attrs Attrs obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - 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 -------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool empty() const`` - bool - df_attrs.h:179 - :ref:`View ` Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``T get(const std::string& key) const`` - T - df_attrs.h:102 - :ref:`View ` * - ``T get(const std::string& key, T default_value) const`` - T - df_attrs.h:118 - :ref:`View ` Type Handling ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Attrs copy() const`` - Attrs - df_attrs.h:228 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``void clear()`` - void - df_attrs.h:163 - :ref:`View ` * - ``bool contains(const std::string& key) const`` - bool - df_attrs.h:78 - :ref:`View ` * - ``bool has_type(const std::string& key) const`` - bool - df_attrs.h:203 - * - ``std::vector keys() const`` - std::vector - df_attrs.h:187 - :ref:`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 - :ref:`View ` * - ``void set(const std::string& key, T&& value)`` - void - df_attrs.h:89 - :ref:`View ` * - ``size_t size() const`` - size_t - df_attrs.h:171 - :ref:`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. .. _example-attrs-empty-0: .. dropdown:: empty (np_test_1_all.cpp:6316) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6306 :emphasize-lines: 11 } void test_data_generator_emptyBenchmarkSorting() { std::cout << "========= test_data_generator_empty ======================="; DataGenerator gen(42); // Test empty data generation std::vector data = gen.generate(0, DataPattern::RANDOM); if (!(data.empty())) { std::string description = std::string("test_data_generator_emptyBenchmarkSorting():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(data.empty())"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "Empty data generation test passed" << std::endl; std::cout << " -> tests passed" << std::endl; } .. _example-attrs-get-1: .. dropdown:: get (np_test_1_all.cpp:28526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28516 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_indexing_mask_indices() { std::cout << "========= mask_indices: triangular mask indices ======================="; // Get upper triangular indices for 3x3 matrix auto triu_idx = numpy::mask_indices(3, "triu", 0); // Should return tuple of 2 arrays bool passed = (std::get<0>(triu_idx).getSize() > 0); passed = passed && (std::get<1>(triu_idx).getSize() > 0); passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize()); // Get lower triangular indices auto tril_idx = numpy::mask_indices(3, "tril", 0); passed = passed && (std::get<0>(tril_idx).getSize() > 0); passed = passed && (std::get<1>(tril_idx).getSize() > 0); if (!passed) { std::cout << " [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect"; .. _example-attrs-get-2: .. dropdown:: get (np_test_1_all.cpp:28526) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 28516 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void np_test_indexing_mask_indices() { std::cout << "========= mask_indices: triangular mask indices ======================="; // Get upper triangular indices for 3x3 matrix auto triu_idx = numpy::mask_indices(3, "triu", 0); // Should return tuple of 2 arrays bool passed = (std::get<0>(triu_idx).getSize() > 0); passed = passed && (std::get<1>(triu_idx).getSize() > 0); passed = passed && (std::get<0>(triu_idx).getSize() == std::get<1>(triu_idx).getSize()); // Get lower triangular indices auto tril_idx = numpy::mask_indices(3, "tril", 0); passed = passed && (std::get<0>(tril_idx).getSize() > 0); passed = passed && (std::get<1>(tril_idx).getSize() > 0); if (!passed) { std::cout << " [FAIL] : in np_test_indexing_mask_indices() : Mask indices incorrect"; .. _example-attrs-copy-3: .. dropdown:: copy (np_test_1_all.cpp:9812) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 9802 :emphasize-lines: 11 //original.printArray(); // The modification should be at position (1,1) in original if (!(original.getElementAt({1, 1}) == 9999)) { std::string description = std::string("testSliceCopyVsViewMemoryOwnership():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(original.getElementAt({1, 1}) == 9999)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Slice view shares memory with original"; // Test slice copy (should be independent) auto slice_copy = original.sliceArray({{2, 4}, {2, 4}}); // std::cout << "Slice copy [2:4, 2:4]:"; //slice_copy.printArray(); slice_copy.setElementAt({0, 0}, 7777); // std::cout << "After modifying slice copy at (0,0):" << std::endl; // std::cout << "Original array:" << std::endl; //original.printArray(); // std::cout << "Slice copy:" << std::endl; .. _example-attrs-clear-4: .. dropdown:: clear (np_test_2_all.cpp:4161) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4151 :emphasize-lines: 11 auto array = createFloat32Array({ 10, 10 }, static_cast(i)); temp_arrays.push_back(array); auto view = array.view(); temp_views.push_back(view); } // std::cout << "Created 100 arrays and 100 views" << std::endl; // Clear all arrays (views will keep memory alive) temp_arrays.clear(); // std::cout << "Cleared original arrays" << std::endl; // Verify views still work if (!(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)) { std::string description = std::string("testMemoryLeakPrevention():") + __FILE__ + ":" + std::to_string(__LINE__) + ": !(temp_views[50].getElementAt({ 5, 5 }) == 50.0f)"; std::cout << std::string("[FAIL] ") + description << std::endl; throw std::runtime_error(description); } // std::cout << "[OK] Views still valid after originals cleared"; .. _example-attrs-contains-5: .. 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"))) { .. _example-attrs-keys-6: .. dropdown:: keys (np_test_2_all.cpp:8614) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 8604 :emphasize-lines: 11 if (!indices.empty()) { std::cout << " IPP empty array test: -> [FAIL]"; throw std::runtime_error("IPP empty array test: FAILED - should return empty"); } // std::cout << "[OK] IPP empty array test: PASSED" << std::endl; } // Test 6: Large array performance { const size_t n = 10000; std::vector> keys(2); keys[0].resize(n); keys[1].resize(n); std::mt19937 gen(42); std::uniform_int_distribution dist(0, 1000); for (size_t i = 0; i < n; ++i) { keys[0][i] = dist(gen); keys[1][i] = dist(gen); } .. _example-attrs-remove-7: .. dropdown:: remove (np_test_1_all.cpp:26177) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 26167 :emphasize-lines: 11 auto result = numpy::fromfile(filename, dtype, ","); // Verify loaded correctly bool passed = (result.getSize() == 3); if (passed) { passed = (result.getFieldValue({ 0 }, "id") == 1); passed = passed && (std::abs(result.getFieldValue({ 1 }, "value") - 20.5) < 0.001); } // Clean up temp file std::remove(filename.c_str()); if (!passed) { std::cout << " [FAIL] : in np_test_recarray_fromfile() : From file failed"; throw std::runtime_error("np_test_recarray_fromfile failed"); } std::cout << " -> tests passed" << std::endl; } void np_test_recarray_getDTypeFromType() { .. _example-attrs-set-8: .. dropdown:: set (np_test_1_all.cpp:6747) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 6737 :emphasize-lines: 11 std::cout << "========= testArrayPrinting ======================="; // Test 1D array printing std::vector words = {"Hello", "World", "Test"}; auto arr_1d = array<16>(words); // std::cout << "1D CharArray:" << std::endl; // arr_1d.print(); // Test 2D array printing chararray16 arr_2d({2, 2}, "test"); arr_2d.set({0, 0}, "A"); arr_2d.set({0, 1}, "B"); arr_2d.set({1, 0}, "C"); arr_2d.set({1, 1}, "D"); // std::cout << "2D CharArray:" << std::endl; // arr_2d.print(); std::cout << " -> tests passed" << std::endl; } .. _example-attrs-size-9: .. dropdown:: size (np_test_1_all.cpp:47) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 37 :emphasize-lines: 11 using namespace numpy; using namespace numpy::benchmark; using namespace numpy::char_; // Helper functions for array comparison tests namespace { const double TOLERANCE = 1e-10; bool allTrue(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0); do { if (!arr.getElementAt(indices)) { return false; } } while (incrementIndices(indices, arr.getShape())); return true; } bool allFalse(const NDArray& arr) { std::vector indices(arr.getShape().size(), 0);