KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
kids_dataset.cc
Go to the documentation of this file.
1py::class_<Shape> PyShape(m, "Shape", py::dynamic_attr());
2
3PyShape.def(py::init<std::vector<std::size_t>>()).def(py::init<std::size_t>());
4
5py::class_<DataSet> PyDataSet(m, "DataSet", py::dynamic_attr());
6
7PyDataSet.def(py::init<>());
8
10 "_def",
11 [](DataSet& self, const std::string& key, py::tuple pyTuple, const std::string& dtype, const std::string& doc) {
12 std::vector<std::size_t> shape;
13 shape.reserve(pyTuple.size()); // Reserve space for efficiency
14 for (size_t i = 0; i < pyTuple.size(); ++i) {
15 int value = pyTuple[i].cast<int>();
16 shape.push_back(value);
17 }
18 if (dtype == "int") {
19 self._def_int(key, Shape{shape}, doc);
20 } else if (dtype == "real") {
21 self._def_real(key, Shape{shape}, doc);
22 } else if (dtype == "complex") {
23 self._def_complex(key, Shape{shape}, doc);
24 } else {
25 // throw std::runtime_error("can not define with this dtype!");
26 }
27 },
28 py::arg("key"), py::arg("shape"), py::arg("dtype") = "int", py::arg("doc") = "");
29
30PyDataSet.def("_undef", &DataSet::_undef, py::arg("key") = "0");
31
32PyDataSet.def("numpy", [](DataSet& self, const std::string& key) {
33 auto inode = self.node(key);
34 kids_dtype n_type = inode->type();
35 switch (n_type) {
36 case kids_int_type: {
37 std::size_t n_size = static_cast<Tensor<kids_int>*>(inode)->size();
38 void* n_data = static_cast<Tensor<kids_int>*>(inode)->data();
39 return py::array({n_size}, // shape
40 {sizeof(int)}, // stride
41 (int*) n_data, // data pointer
42 py::capsule(n_data, [](void* _void_n_data) { ; }) // zero-copy cost
43 );
44 break;
45 }
46 case kids_real_type: {
47 std::size_t n_size = static_cast<Tensor<kids_real>*>(inode)->size();
48 void* n_data = static_cast<Tensor<kids_real>*>(inode)->data();
49 return py::array({n_size}, // shape
50 {sizeof(kids_real)}, // stride
51 (kids_real*) n_data, // data pointer
52 py::capsule(n_data, [](void* _void_n_data) { ; }) // zero-copy cost
53 );
54 break;
55 }
56 case kids_complex_type: {
57 std::size_t n_size = static_cast<Tensor<kids_complex>*>(inode)->size();
58 void* n_data = static_cast<Tensor<kids_complex>*>(inode)->data();
59 return py::array({n_size}, // shape
60 {sizeof(kids_complex)}, // stride
61 (kids_complex*) n_data, // data pointer
62 py::capsule(n_data, [](void* _void_n_data) { ; }) // zero-copy cost
63 );
64 break;
65 }
66 default:
67 throw std::runtime_error("can not converted to numpy!");
68 }
69 return py::array();
70});
71
73 "help", [](DataSet& self, const std::string& name) { return self.help(name); }, py::arg("name") = "");
74
75PyDataSet.def("__repr__", [](DataSet& self) { return self.repr(); });
py::class_< Shape > PyShape(m, "Shape", py::dynamic_attr())
PyShape def(py::init< std::vector< std::size_t > >()).def(py py::class_< DataSet > PyDataSet(m, "DataSet", py::dynamic_attr())
Param::LoadOption::fromString value("fromFile", Param::LoadOption::fromFile) .export_values()