content.h
Go to the documentation of this file.
1 // BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
2 
3 #ifndef AWKWARDPY_CONTENT_H_
4 #define AWKWARDPY_CONTENT_H_
5 
6 #include <pybind11/numpy.h>
7 #include <pybind11/pybind11.h>
8 #include <pybind11/stl.h>
9 
12 #include "awkward/Iterator.h"
13 #include "awkward/Content.h"
21 #include "awkward/array/None.h"
23 #include "awkward/array/Record.h"
28 
29 namespace py = pybind11;
30 namespace ak = awkward;
31 
32 py::object
33 box(const std::shared_ptr<ak::Content>& content);
34 
35 std::shared_ptr<ak::Content>
36 unbox_content(const py::handle& obj);
37 
38 template <typename T>
39 std::string
40 repr(const T& self) {
41  return self.tostring();
42 }
43 
44 template <typename T>
45 int64_t
46 len(const T& self) {
47  return self.length();
48 }
49 
50 int64_t
51 check_maxdecimals(const py::object& maxdecimals);
52 
54 dict2parameters(const py::object& in);
55 
56 py::dict
58 
61  toslice(py::object obj);
62 
64 py::class_<ak::ArrayBuilder>
65  make_ArrayBuilder(const py::handle& m, const std::string& name);
66 
68 template <typename T, typename I>
69 py::class_<ak::LayoutBuilder<T, I>>
70  make_LayoutBuilder(const py::handle& m, const std::string& name);
71 
73 py::class_<ak::Iterator, std::shared_ptr<ak::Iterator>>
74  make_Iterator(const py::handle& m, const std::string& name);
75 
76 namespace {
77  class NumpyBuffersContainer: public ak::BuffersContainer {
78  public:
79  py::dict container() {
80  return container_;
81  }
82 
83  void
84  copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override {
85  py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1");
86  py::array_t<uint8_t> rawarray = pyarray.cast<py::array_t<uint8_t>>();
87  py::buffer_info rawinfo = rawarray.request();
88  std::memcpy(rawinfo.ptr, source, num_bytes);
89  container_[py::str(name)] = pyarray;
90  }
91 
92  void
93  full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) override {
94  py::object pyarray = py::module::import("numpy").attr("full")(py::int_(length), py::int_(value), py::str(dtype));
95  container_[py::str(name)] = pyarray;
96  }
97 
98  private:
99  py::dict container_;
100  };
101 
102  class EmptyBuffersContainer: public ak::BuffersContainer {
103  public:
104  void
105  copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override { }
106 
107  void
108  full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) override { }
109  };
110 
120  py::object
121  builder_snapshot(const ak::BuilderPtr builder) {
122  ::NumpyBuffersContainer container;
123  int64_t form_key_id = 0;
124  std::string form = builder.get()->to_buffers(container, form_key_id);
125  py::dict kwargs;
126  kwargs[py::str("form")] = py::str(form);
127  kwargs[py::str("length")] = py::int_(builder.get()->length());
128  kwargs[py::str("container")] = container.container();
129  kwargs[py::str("key_format")] = py::str("{form_key}-{attribute}");
130  kwargs[py::str("highlevel")] = py::bool_(false);
131  return py::module::import("awkward").attr("from_buffers")(**kwargs);
132  }
133 }
134 
140 public:
143  PersistentSharedPtr(const std::shared_ptr<ak::Content>& ptr);
144 
146  py::object
147  layout() const;
148 
151  size_t
152  ptr() const;
153 
154 private:
156  const std::shared_ptr<ak::Content> ptr_;
157 };
158 
161 py::class_<PersistentSharedPtr>
162  make_PersistentSharedPtr(const py::handle& m, const std::string& name);
163 
166 py::class_<ak::Content, std::shared_ptr<ak::Content>>
167  make_Content(const py::handle& m, const std::string& name);
168 
170 py::class_<ak::EmptyArray, std::shared_ptr<ak::EmptyArray>, ak::Content>
171  make_EmptyArray(const py::handle& m, const std::string& name);
172 
176 template <typename T, bool ISOPTION>
177 py::class_<ak::IndexedArrayOf<T, ISOPTION>,
178  std::shared_ptr<ak::IndexedArrayOf<T, ISOPTION>>,
179  ak::Content>
180  make_IndexedArrayOf(const py::handle& m, const std::string& name);
181 
183 py::class_<ak::ByteMaskedArray,
184  std::shared_ptr<ak::ByteMaskedArray>,
185  ak::Content>
186  make_ByteMaskedArray(const py::handle& m, const std::string& name);
187 
189 py::class_<ak::BitMaskedArray,
190  std::shared_ptr<ak::BitMaskedArray>,
191  ak::Content>
192  make_BitMaskedArray(const py::handle& m, const std::string& name);
193 
195 py::class_<ak::UnmaskedArray,
196  std::shared_ptr<ak::UnmaskedArray>,
197  ak::Content>
198  make_UnmaskedArray(const py::handle& m, const std::string& name);
199 
202 template <typename T>
203 py::class_<ak::ListArrayOf<T>,
204  std::shared_ptr<ak::ListArrayOf<T>>,
205  ak::Content>
206  make_ListArrayOf(const py::handle& m, const std::string& name);
207 
210 template <typename T>
211 py::class_<ak::ListOffsetArrayOf<T>,
212  std::shared_ptr<ak::ListOffsetArrayOf<T>>,
213  ak::Content>
214  make_ListOffsetArrayOf(const py::handle& m, const std::string& name);
215 
217 py::class_<ak::NumpyArray, std::shared_ptr<ak::NumpyArray>, ak::Content>
218  make_NumpyArray(const py::handle& m, const std::string& name);
219 
226 py::class_<ak::Record, std::shared_ptr<ak::Record>>
227  make_Record(const py::handle& m, const std::string& name);
228 
230 py::class_<ak::RecordArray, std::shared_ptr<ak::RecordArray>, ak::Content>
231  make_RecordArray(const py::handle& m, const std::string& name);
232 
234 py::class_<ak::RegularArray, std::shared_ptr<ak::RegularArray>, ak::Content>
235  make_RegularArray(const py::handle& m, const std::string& name);
236 
239 template <typename T, typename I>
240 py::class_<ak::UnionArrayOf<T, I>,
241  std::shared_ptr<ak::UnionArrayOf<T, I>>,
242  ak::Content>
243  make_UnionArrayOf(const py::handle& m, const std::string& name);
244 
246 py::class_<ak::VirtualArray, std::shared_ptr<ak::VirtualArray>, ak::Content>
247  make_VirtualArray(const py::handle& m, const std::string& name);
248 
249 #endif // AWKWARDPY_CONTENT_H_
py::class_< ak::UnmaskedArray, std::shared_ptr< ak::UnmaskedArray >, ak::Content > make_UnmaskedArray(const py::handle &m, const std::string &name)
Makes a UnmaskedArray in Python that mirrors the one in C++.
Abstract superclass of all array node types (flat hierarchy). Any Content can be nested within any ot...
Definition: Content.h:276
Definition: BitMaskedArray.h:15
py::class_< ak::ByteMaskedArray, std::shared_ptr< ak::ByteMaskedArray >, ak::Content > make_ByteMaskedArray(const py::handle &m, const std::string &name)
Makes a ByteMaskedArray in Python that mirrors the one in C++.
Abstract class to represent the output of ak.to_buffers. In Python, this would be a dict of NumPy arr...
Definition: Builder.h:22
int64_t len(const T &self)
Definition: content.h:46
std::string name(Mapping a, V value)
Definition: datetime_util.h:39
py::class_< ak::Record, std::shared_ptr< ak::Record > > make_Record(const py::handle &m, const std::string &name)
Makes a Record in Python that mirrors the one in C++.
py::dict parameters2dict(const ak::util::Parameters &in)
std::string repr(const T &self)
Definition: content.h:40
ak::Slice toslice(py::object obj)
Converts Python objects in a slice into a C++ Slice.
py::class_< ak::BitMaskedArray, std::shared_ptr< ak::BitMaskedArray >, ak::Content > make_BitMaskedArray(const py::handle &m, const std::string &name)
Makes a BitMaskedArray in Python that mirrors the one in C++.
py::class_< ak::NumpyArray, std::shared_ptr< ak::NumpyArray >, ak::Content > make_NumpyArray(const py::handle &m, const std::string &name)
Makes a NumpyArray in Python that mirrors the one in C++.
std::shared_ptr< ak::Content > unbox_content(const py::handle &obj)
Array nodes are frequently copied, but for some applications (one in Numba) it&#39;s better to keep a per...
Definition: content.h:139
ak::util::Parameters dict2parameters(const py::object &in)
dtype
NumPy dtypes that can be interpreted within Awkward C++ (only the primitive, fixed-width types)...
Definition: util.h:26
std::map< std::string, std::string > Parameters
Definition: util.h:165
py::object box(const std::shared_ptr< ak::Content > &content)
int64_t check_maxdecimals(const py::object &maxdecimals)
py::class_< ak::RegularArray, std::shared_ptr< ak::RegularArray >, ak::Content > make_RegularArray(const py::handle &m, const std::string &name)
Makes a RegularArray in Python that mirrors the one in C++.
Mapping::value_type::value_type value(Mapping a, const std::string &name)
Definition: datetime_util.h:49
A sequence of SliceItem objects representing a tuple passed to Python&#39;s __getitem__.
Definition: Slice.h:585
py::class_< ak::ListOffsetArrayOf< T >, std::shared_ptr< ak::ListOffsetArrayOf< T > >, ak::Content > make_ListOffsetArrayOf(const py::handle &m, const std::string &name)
Makes ListOffsetArray32, ListOffsetArrayU32, and ListOffsetArray64 classes in Python that mirror List...
py::class_< ak::EmptyArray, std::shared_ptr< ak::EmptyArray >, ak::Content > make_EmptyArray(const py::handle &m, const std::string &name)
Makes an EmptyArray in Python that mirrors the one in C++.
std::shared_ptr< Builder > BuilderPtr
Definition: ArrayBuilder.h:19
py::class_< ak::Content, std::shared_ptr< ak::Content > > make_Content(const py::handle &m, const std::string &name)
Makes an abstract Content class in Python that mirrors the one in C++.
py::class_< ak::LayoutBuilder< T, I > > make_LayoutBuilder(const py::handle &m, const std::string &name)
Makes a LayoutBuilder class in Python that mirrors the one in C++.
py::class_< ak::Iterator, std::shared_ptr< ak::Iterator > > make_Iterator(const py::handle &m, const std::string &name)
Makes an Iterator class in Python that mirrors the one in C++.
py::class_< ak::ArrayBuilder > make_ArrayBuilder(const py::handle &m, const std::string &name)
Makes an ArrayBuilder class in Python that mirrors the one in C++.
Represents potentially missing data by overlaying a bit mask over its content.
Definition: BitMaskedArray.h:127
py::class_< PersistentSharedPtr > make_PersistentSharedPtr(const py::handle &m, const std::string &name)
Makes a PersistentSharedPtr class in Python that mirrors the one in C++.
py::class_< ak::IndexedArrayOf< T, ISOPTION >, std::shared_ptr< ak::IndexedArrayOf< T, ISOPTION > >, ak::Content > make_IndexedArrayOf(const py::handle &m, const std::string &name)
Makes IndexedArray32, IndexedArrayU32, IndexedArray64, IndexedOptionArray32, and IndexedOptionArray64...
py::class_< ak::UnionArrayOf< T, I >, std::shared_ptr< ak::UnionArrayOf< T, I > >, ak::Content > make_UnionArrayOf(const py::handle &m, const std::string &name)
Makes UnionArray8_32, UnionArray8_U32, and UnionArray8_64 classes in Python that mirror UnionArrayOf ...
py::class_< ak::RecordArray, std::shared_ptr< ak::RecordArray >, ak::Content > make_RecordArray(const py::handle &m, const std::string &name)
Makes a RecordArray in Python that mirrors the one in C++.
py::class_< ak::ListArrayOf< T >, std::shared_ptr< ak::ListArrayOf< T > >, ak::Content > make_ListArrayOf(const py::handle &m, const std::string &name)
Makes ListArray32, ListArrayU32, and ListArray64 classes in Python that mirror ListArrayOf in C++...
Represents potentially missing data by overlaying a byte mask over its content.
Definition: ByteMaskedArray.h:117
py::class_< ak::VirtualArray, std::shared_ptr< ak::VirtualArray >, ak::Content > make_VirtualArray(const py::handle &m, const std::string &name)
Makes a VirtualArray in Python that mirrors the one in C++.
Converts content of any type into an OptionType in which all values happen to be valid.
Definition: UnmaskedArray.h:107