RecArrayCreation#

class numpy::RecArrayCreation#

numpy C++ class.

Example#

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

// Use RecArrayCreation
RecArrayCreation obj;
// ... operations ...

Array Creation#

Signature

Return Type

Location

Example

RecordArray ones(const std::vector<size_t>&shape, std::shared_ptr<StructuredDType>dtype)

RecordArray

NP_REC_CREATION.H:233

View

RecordArray zeros(const std::vector<size_t>&shape, std::shared_ptr<StructuredDType>dtype)

RecordArray

NP_REC_CREATION.H:208

View

Indexing / Selection#

Signature

Return Type

Location

Example

DType getDTypeFromType()

DType

NP_REC_CREATION.H:279

DType getDTypeFromValue(const StructuredValue &value)

DType

NP_REC_CREATION.H:272

I/O#

Signature

Return Type

Location

Example

RecordArray fromfile(const std::string &filename, std::shared_ptr<StructuredDType>dtype, const std::string &separator = ",", size_tcount = SIZE_MAX, boolskip_header = false)

RecordArray

NP_REC_CREATION.H:173

View

RecordArray fromstring(const std::string &data, std::shared_ptr<StructuredDType>dtype, const std::string &separator = ",", size_tcount = SIZE_MAX)

RecordArray

NP_REC_CREATION.H:155

View

Other Methods#

Signature

Return Type

Location

Example

RecordArray array(const std::vector<std::tuple<Args...>>&data, const std::vector<std::pair<std::string,DType>>&field_specs)

RecordArray

NP_REC_CREATION.H:202

View

RecordArray fromarrays(const std::vector<std::pair<std::string,NDArray<T>>>&field_arrays)

RecordArray

NP_REC_CREATION.H:63

View

RecordArray fromarrays(const std::vector<std::pair<std::string,std::pair<DType,std::shared_ptr<void>>>>&field_data, const std::vector<size_t>&shape)

RecordArray

NP_REC_CREATION.H:107

View

RecordArray fromrecords(const std::vector<std::tuple<Args...>>&records, const std::vector<std::pair<std::string,DType>>&field_specs)

RecordArray

NP_REC_CREATION.H:14

View

RecordArray fromrecords(const std::vector<StructuredRecord>&records, std::shared_ptr<StructuredDType>dtype)

RecordArray

NP_REC_CREATION.H:31

View

RecordArray fromrecords(const std::vector<StructuredRecord>&records)

RecordArray

NP_REC_CREATION.H:46

View

StructuredRecord parseRecordFromString(const std::string &line, std::shared_ptr<StructuredDType>dtype, const std::string &separator)

StructuredRecord

NP_REC_CREATION.H:320

StructuredValue parseValueFromString(const std::string &str, DTypedtype)

StructuredValue

NP_REC_CREATION.H:342

void setFieldFromRawData(RecordArray &array, const std::vector<size_t>&indices, const std::string &field_name, DTypedtype, void \*data, size_toffset)

void

NP_REC_CREATION.H:296

void setFieldFromTupleElement(RecordArray &array, const std::vector<size_t>&indices, const std::string &field_name, const T &value)

void

NP_REC_CREATION.H:266

void setOneValue(RecordArray &array, const std::vector<size_t>&indices, const std::string &field_name, DTypedtype)

void

NP_REC_CREATION.H:393

void setRecordFromTuple(RecordArray &array, const std::vector<size_t>&indices, const Tuple &tuple, std::index_sequence<Is...>)

void

NP_REC_CREATION.H:259

void setZeroValue(RecordArray &array, const std::vector<size_t>&indices, const std::string &field_name, DTypedtype)

void

NP_REC_CREATION.H:373

Code Examples#

The following examples are extracted from the test suite.

ones (np_test_1_all.cpp:319)
309  //int_range.printArray();
310
311  auto reshaped = int_range.reshapeArray({ 3, 2 });
312  // std::cout << "Reshaped to 2x2 matrix:";
313  //reshaped.printArray();
314
315  auto zeros = createInt32Zeros({ 2, 3 });
316  // std::cout << "zeros (2,3):";
317  //zeros.printArray();
318  auto ones = createInt32Ones({ 2, 3 });
319  // std::cout << "ones (2,3):";
320  //ones.printArray();
321
322  // std::cout << "Addition of zeros and ones:" << std::endl;
323  auto sum = zeros.addArrays(ones);
324  //sum.printArray();
325
326  // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl;
327  // std::cout << "Mean: " << sum.meanArray() << std::endl;
328
329  // std::cout << std::endl << "Type introspection with get_typename():" << std::endl;
zeros (np_test_1_all.cpp:316)
306  auto int_range = NDArray<int32>::createRange(1, 12, 2);
307  // std::cout << "Int32 range array (1 to 10, step 2):" << std::endl;
308  //int_range.printArray();
309
310  auto reshaped = int_range.reshapeArray({ 3, 2 });
311  // std::cout << "Reshaped to 2x2 matrix:";
312  //reshaped.printArray();
313
314  auto zeros = createInt32Zeros({ 2, 3 });
315  // std::cout << "zeros (2,3):";
316  //zeros.printArray();
317  auto ones = createInt32Ones({ 2, 3 });
318  // std::cout << "ones (2,3):";
319  //ones.printArray();
320
321  // std::cout << "Addition of zeros and ones:" << std::endl;
322  auto sum = zeros.addArrays(ones);
323  //sum.printArray();
324
325  // std::cout << "Sum of all elements: " << sum.sumArray() << std::endl;
fromfile (np_test_1_all.cpp:26167)
26157      outfile.close();
26158
26159      // Define dtype
26160      std::vector<std::pair<std::string, numpy::DType>> fields = {
26161          {"id", numpy::DType::INT32},
26162          {"value", numpy::DType::FLOAT64}
26163      };
26164      auto dtype = std::make_shared<numpy::StructuredDType>(fields);
26165
26166      // Load from file
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());
fromstring (np_test_1_all.cpp:21885)
21875    std::cout << "========= testStringParsing =======================";
21876
21877    auto dtype = std::make_shared<StructuredDType>(std::vector<std::pair<std::string, DType>>{
21878      {"name", DType::UNICODE32},
21879      { "age", DType::INT32 },
21880      { "score", DType::FLOAT64 }
21881    });
21882
21883    std::string data = "Alice,25,85.5\nBob,30,92.3\nCharlie,35,78.9";
21884
21885    auto arr = fromstring(data, dtype, ",", 3);
21886
21887    // std::cout << "Array created from string data:" << std::endl;
21888    //arr.printArray();
21889
21890    auto name_1 = arr.getFieldValue<std::string>({ 1 }, "name");
21891    auto age_1 = arr.getFieldValue<int32>({ 1 }, "age");
21892    auto score_1 = arr.getFieldValue<float64>({ 1 }, "score");
21893
21894    // std::cout << "Verification of record 1:" << std::endl;
21895    // std::cout << "Name: " << (name_1.size() > 0 ? "Bob" : "empty") << std::endl;
array (np_test_1_all.cpp:243)
233    // std::cout << "Min: " << array.minArray() << std::endl;
234    // std::cout << "Max: " << array.maxArray() << std::endl;
235
236    std::cout << " -> tests passed" << std::endl;
237}
238
239void testReshapeAndFlatten() {
240    std::cout << "========= testReshapeAndFlatten =======================";
241
242    auto range_array = NDArray<int32>::createRange(0, 12, 1);
243    // std::cout << "Range array (0-11):" << std::endl;
244    //range_array.printArray();
245
246    auto reshaped = range_array.reshapeArray({3, 4});
247    // std::cout << "Reshaped to 3x4:";
248    //reshaped.printArray();
249
250    auto flattened = reshaped.flattenArray();
251    // std::cout << "Flattened:" << std::endl;
252    //flattened.printArray();
fromarrays (np_test_1_all.cpp:21655)
21645    ids.setElementAt({ 2 }, 300);
21646
21647    NDArray<float64> scores({ 3 });
21648    scores.setElementAt({ 0 }, 85.5);
21649    scores.setElementAt({ 1 }, 92.3);
21650    scores.setElementAt({ 2 }, 78.9);
21651
21652    std::vector<std::pair<std::string, NDArray<int32>>> int_arrays = { {"id", ids} };
21653    std::vector<std::pair<std::string, NDArray<float64>>> float_arrays = { {"score", scores} };
21654
21655    auto arr1 = fromarrays(int_arrays);
21656    auto arr2 = fromarrays(float_arrays);
21657
21658    // std::cout << "Array from int arrays:" << std::endl;
21659    //arr1.printArray();
21660
21661    // std::cout << "Array from float arrays:" << std::endl;
21662    //arr2.printArray();
21663
21664    auto id_check = arr1.getFieldValue<int32>({ 1 }, "id");
21665    auto score_check = arr2.getFieldValue<float64>({ 2 }, "score");
fromarrays (np_test_1_all.cpp:21655)
21645    ids.setElementAt({ 2 }, 300);
21646
21647    NDArray<float64> scores({ 3 });
21648    scores.setElementAt({ 0 }, 85.5);
21649    scores.setElementAt({ 1 }, 92.3);
21650    scores.setElementAt({ 2 }, 78.9);
21651
21652    std::vector<std::pair<std::string, NDArray<int32>>> int_arrays = { {"id", ids} };
21653    std::vector<std::pair<std::string, NDArray<float64>>> float_arrays = { {"score", scores} };
21654
21655    auto arr1 = fromarrays(int_arrays);
21656    auto arr2 = fromarrays(float_arrays);
21657
21658    // std::cout << "Array from int arrays:" << std::endl;
21659    //arr1.printArray();
21660
21661    // std::cout << "Array from float arrays:" << std::endl;
21662    //arr2.printArray();
21663
21664    auto id_check = arr1.getFieldValue<int32>({ 1 }, "id");
21665    auto score_check = arr2.getFieldValue<float64>({ 2 }, "score");
fromrecords (np_test_1_all.cpp:21622)
21612    record2.set("value", static_cast<float64>(20.7));
21613    record2.set("active", static_cast<bool_>(false));
21614    records.push_back(record2);
21615
21616    StructuredRecord record3;
21617    record3.set("id", static_cast<int32>(3));
21618    record3.set("value", static_cast<float64>(15.3));
21619    record3.set("active", static_cast<bool_>(true));
21620    records.push_back(record3);
21621
21622    auto arr = fromrecords(records);
21623
21624    // std::cout << "Array created from records:" << std::endl;
21625    //arr.printArray();
21626
21627    auto id_1 = arr.getFieldValue<int32>({ 1 }, "id");
21628    auto value_2 = arr.getFieldValue<float64>({ 2 }, "value");
21629    auto active_0 = arr.getFieldValue<bool_>({ 0 }, "active");
21630
21631    // std::cout << "Verification:" << std::endl;
21632    // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl;
fromrecords (np_test_1_all.cpp:21622)
21612    record2.set("value", static_cast<float64>(20.7));
21613    record2.set("active", static_cast<bool_>(false));
21614    records.push_back(record2);
21615
21616    StructuredRecord record3;
21617    record3.set("id", static_cast<int32>(3));
21618    record3.set("value", static_cast<float64>(15.3));
21619    record3.set("active", static_cast<bool_>(true));
21620    records.push_back(record3);
21621
21622    auto arr = fromrecords(records);
21623
21624    // std::cout << "Array created from records:" << std::endl;
21625    //arr.printArray();
21626
21627    auto id_1 = arr.getFieldValue<int32>({ 1 }, "id");
21628    auto value_2 = arr.getFieldValue<float64>({ 2 }, "value");
21629    auto active_0 = arr.getFieldValue<bool_>({ 0 }, "active");
21630
21631    // std::cout << "Verification:" << std::endl;
21632    // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl;
fromrecords (np_test_1_all.cpp:21622)
21612    record2.set("value", static_cast<float64>(20.7));
21613    record2.set("active", static_cast<bool_>(false));
21614    records.push_back(record2);
21615
21616    StructuredRecord record3;
21617    record3.set("id", static_cast<int32>(3));
21618    record3.set("value", static_cast<float64>(15.3));
21619    record3.set("active", static_cast<bool_>(true));
21620    records.push_back(record3);
21621
21622    auto arr = fromrecords(records);
21623
21624    // std::cout << "Array created from records:" << std::endl;
21625    //arr.printArray();
21626
21627    auto id_1 = arr.getFieldValue<int32>({ 1 }, "id");
21628    auto value_2 = arr.getFieldValue<float64>({ 2 }, "value");
21629    auto active_0 = arr.getFieldValue<bool_>({ 0 }, "active");
21630
21631    // std::cout << "Verification:" << std::endl;
21632    // std::cout << "Record 1 ID: " << id_1 << " (expected: 2)" << std::endl;