Awkward Array: include/awkward/python/content.h Source File

Loading...
Searching...
No Matches
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
10#include "awkward/builder/ArrayBuilder.h"
11#include "awkward/layoutbuilder/LayoutBuilder.h"
12#include "awkward/Iterator.h"
13#include "awkward/Content.h"
14#include "awkward/array/EmptyArray.h"
15#include "awkward/array/IndexedArray.h"
16#include "awkward/array/ByteMaskedArray.h"
17#include "awkward/array/BitMaskedArray.h"
18#include "awkward/array/UnmaskedArray.h"
19#include "awkward/array/ListArray.h"
20#include "awkward/array/ListOffsetArray.h"
21#include "awkward/array/None.h"
22#include "awkward/array/NumpyArray.h"
23#include "awkward/array/Record.h"
24#include "awkward/array/RecordArray.h"
25#include "awkward/array/RegularArray.h"
26#include "awkward/array/UnionArray.h"
27#include "awkward/array/VirtualArray.h"
28
29namespace py = pybind11;
30namespace ak = awkward;
31
32py::object
33box(const std::shared_ptr<ak::Content>& content);
34
35std::shared_ptr<ak::Content>
36unbox_content(const py::handle& obj);
37
38template <typename T>
39std::string
40repr(const T& self) {
41 return self.tostring();
42}
43
44template <typename T>
45int64_t
46len(const T& self) {
47 return self.length();
48}
49
50int64_t
51check_maxdecimals(const py::object& maxdecimals);
52
53ak::util::Parameters
54dict2parameters(const py::object& in);
55
56py::dict
57parameters2dict(const ak::util::Parameters& in);
58
60 ak::Slice
61 toslice(py::object obj);
62
64py::class_<ak::ArrayBuilder>
65 make_ArrayBuilder(const py::handle& m, const std::string& name);
66
68template <typename T, typename I>
69py::class_<ak::LayoutBuilder<T, I>>
70 make_LayoutBuilder(const py::handle& m, const std::string& name);
71
73py::class_<ak::Iterator, std::shared_ptr<ak::Iterator>>
74 make_Iterator(const py::handle& m, const std::string& name);
75
76namespace {
77 class NumpyBuffersContainer: public ak::BuffersContainer {
78 public:
79 py::dict container() {
80 return container_;
81 }
82
83 void*
84 empty_buffer(const std::string& name, 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 container_[py::str(name)] = pyarray;
89 return rawinfo.ptr;
90 }
91
92 void
93 copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override {
94 py::object pyarray = py::module::import("numpy").attr("empty")(num_bytes, "u1");
95 py::array_t<uint8_t> rawarray = pyarray.cast<py::array_t<uint8_t>>();
96 py::buffer_info rawinfo = rawarray.request();
97 std::memcpy(rawinfo.ptr, source, num_bytes);
98 container_[py::str(name)] = pyarray;
99 }
100
101 void
102 full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) override {
103 py::object pyarray = py::module::import("numpy").attr("full")(py::int_(length), py::int_(value), py::str(dtype));
104 container_[py::str(name)] = pyarray;
105 }
106
107 private:
108 py::dict container_;
109 };
110
111 class EmptyBuffersContainer: public ak::BuffersContainer {
112 public:
113 void*
114 empty_buffer(const std::string& name, int64_t num_bytes) override {
115 return nullptr;
116 }
117
118 void
119 copy_buffer(const std::string& name, const void* source, int64_t num_bytes) override { }
120
121 void
122 full_buffer(const std::string& name, int64_t length, int64_t value, const std::string& dtype) override { }
123 };
124
134 py::object
135 builder_snapshot(const ak::BuilderPtr builder) {
136 ::NumpyBuffersContainer container;
137 int64_t form_key_id = 0;
138 std::string form = builder.get()->to_buffers(container, form_key_id);
139 py::dict kwargs;
140 kwargs[py::str("form")] = py::str(form);
141 kwargs[py::str("length")] = py::int_(builder.get()->length());
142 kwargs[py::str("container")] = container.container();
143 kwargs[py::str("key_format")] = py::str("{form_key}-{attribute}");
144 kwargs[py::str("highlevel")] = py::bool_(false);
145 return py::module::import("awkward").attr("from_buffers")(**kwargs);
146 }
147}
148
153 class PersistentSharedPtr {
154public:
157 PersistentSharedPtr(const std::shared_ptr<ak::Content>& ptr);
158
160 py::object
161 layout() const;
162
165 size_t
166 ptr() const;
167
168private:
170 const std::shared_ptr<ak::Content> ptr_;
171};
172
175py::class_<PersistentSharedPtr>
176 make_PersistentSharedPtr(const py::handle& m, const std::string& name);
177
180py::class_<ak::Content, std::shared_ptr<ak::Content>>
181 make_Content(const py::handle& m, const std::string& name);
182
184py::class_<ak::EmptyArray, std::shared_ptr<ak::EmptyArray>, ak::Content>
185 make_EmptyArray(const py::handle& m, const std::string& name);
186
190template <typename T, bool ISOPTION>
191py::class_<ak::IndexedArrayOf<T, ISOPTION>,
192 std::shared_ptr<ak::IndexedArrayOf<T, ISOPTION>>,
193 ak::Content>
194 make_IndexedArrayOf(const py::handle& m, const std::string& name);
195
197py::class_<ak::ByteMaskedArray,
198 std::shared_ptr<ak::ByteMaskedArray>,
199 ak::Content>
200 make_ByteMaskedArray(const py::handle& m, const std::string& name);
201
203py::class_<ak::BitMaskedArray,
204 std::shared_ptr<ak::BitMaskedArray>,
205 ak::Content>
206 make_BitMaskedArray(const py::handle& m, const std::string& name);
207
209py::class_<ak::UnmaskedArray,
210 std::shared_ptr<ak::UnmaskedArray>,
211 ak::Content>
212 make_UnmaskedArray(const py::handle& m, const std::string& name);
213
216template <typename T>
217py::class_<ak::ListArrayOf<T>,
218 std::shared_ptr<ak::ListArrayOf<T>>,
219 ak::Content>
220 make_ListArrayOf(const py::handle& m, const std::string& name);
221
224template <typename T>
225py::class_<ak::ListOffsetArrayOf<T>,
226 std::shared_ptr<ak::ListOffsetArrayOf<T>>,
227 ak::Content>
228 make_ListOffsetArrayOf(const py::handle& m, const std::string& name);
229
231py::class_<ak::NumpyArray, std::shared_ptr<ak::NumpyArray>, ak::Content>
232 make_NumpyArray(const py::handle& m, const std::string& name);
233
240py::class_<ak::Record, std::shared_ptr<ak::Record>>
241 make_Record(const py::handle& m, const std::string& name);
242
244py::class_<ak::RecordArray, std::shared_ptr<ak::RecordArray>, ak::Content>
245 make_RecordArray(const py::handle& m, const std::string& name);
246
248py::class_<ak::RegularArray, std::shared_ptr<ak::RegularArray>, ak::Content>
249 make_RegularArray(const py::handle& m, const std::string& name);
250
253template <typename T, typename I>
254py::class_<ak::UnionArrayOf<T, I>,
255 std::shared_ptr<ak::UnionArrayOf<T, I>>,
256 ak::Content>
257 make_UnionArrayOf(const py::handle& m, const std::string& name);
258
260py::class_<ak::VirtualArray, std::shared_ptr<ak::VirtualArray>, ak::Content>
261 make_VirtualArray(const py::handle& m, const std::string& name);
262
263#endif // AWKWARDPY_CONTENT_H_
ArrayBuilder.h
BitMaskedArray.h
ByteMaskedArray.h
Content.h
EmptyArray.h
IndexedArray.h
Iterator.h
ListArray.h
ListOffsetArray.h
None.h
NumpyArray.h
RecordArray.h
Record.h
RegularArray.h
UnionArray.h
UnmaskedArray.h
VirtualArray.h
PersistentSharedPtr
Array nodes are frequently copied, but for some applications (one in Numba) it's better to keep a per...
Definition: content.h:153
PersistentSharedPtr::PersistentSharedPtr
PersistentSharedPtr(const std::shared_ptr< ak::Content > &ptr)
Creates a PersistentSharedPtr from a std::shared_ptr to a Content array.
PersistentSharedPtr::layout
py::object layout() const
Returns a layout object (Content in Python) from this ptr.
PersistentSharedPtr::ptr
size_t ptr() const
Returns a raw pointer to the persistent std::shared_ptr (a raw pointer to a smart pointer).
awkward::BitMaskedArray
Represents potentially missing data by overlaying a bit mask over its content.
Definition: BitMaskedArray.h:127
awkward::BuffersContainer
Abstract class to represent the output of ak.to_buffers. In Python, this would be a dict of NumPy arr...
Definition: Builder.h:20
awkward::ByteMaskedArray
Represents potentially missing data by overlaying a byte mask over its content.
Definition: ByteMaskedArray.h:117
awkward::Content
Abstract superclass of all array node types (flat hierarchy). Any Content can be nested within any ot...
Definition: Content.h:276
awkward::Slice
A sequence of SliceItem objects representing a tuple passed to Python's __getitem__.
Definition: Slice.h:585
awkward::UnmaskedArray
Converts content of any type into an OptionType in which all values happen to be valid.
Definition: UnmaskedArray.h:107
unbox_content
std::shared_ptr< ak::Content > unbox_content(const py::handle &obj)
make_ArrayBuilder
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++.
make_ListOffsetArrayOf
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...
make_BitMaskedArray
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++.
make_UnionArrayOf
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 ...
make_ListArrayOf
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++.
make_EmptyArray
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++.
box
py::object box(const std::shared_ptr< ak::Content > &content)
make_Content
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++.
dict2parameters
ak::util::Parameters dict2parameters(const py::object &in)
make_Record
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++.
make_NumpyArray
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++.
make_PersistentSharedPtr
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++.
make_Iterator
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++.
make_RecordArray
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++.
make_LayoutBuilder
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++.
make_IndexedArrayOf
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...
len
int64_t len(const T &self)
Definition: content.h:46
make_RegularArray
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++.
make_VirtualArray
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++.
check_maxdecimals
int64_t check_maxdecimals(const py::object &maxdecimals)
make_ByteMaskedArray
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++.
parameters2dict
py::dict parameters2dict(const ak::util::Parameters &in)
make_UnmaskedArray
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++.
toslice
ak::Slice toslice(py::object obj)
Converts Python objects in a slice into a C++ Slice.
repr
std::string repr(const T &self)
Definition: content.h:40
LayoutBuilder.h
awkward
Definition: BitMaskedArray.h:15
awkward::BuilderPtr
std::shared_ptr< Builder > BuilderPtr
Definition: ArrayBuilder.h:16