FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xtensor-python/include/xtensor-python/pyarray.hpp at master · BenJamesbabala/xtensor-python · GitHub
BenJamesbabala
xtensor-python
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
xtensor-python
/
include
/
xtensor-python
/
pyarray.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
378 lines (308 loc) · 11.3 KB
Breadcrumbs
xtensor-python
/
include
/
xtensor-python
/
pyarray.hpp
Copy path
File metadata and controls
378 lines (308 loc) · 11.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
**************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
***************************************************************************
*/
#
ifndef
PY_ARRAY_HPP
#
define
PY_ARRAY_HPP
#
include
<
cstddef
>
#
include
<
vector
>
#
include
<
algorithm
>
#
include
"
xtensor/xsemantic.hpp
"
#
include
"
xtensor/xiterator.hpp
"
#
include
"
pycontainer.hpp
"
#
include
"
pybuffer_adaptor.hpp
"
namespace
xt
{
template
<
class
T
>
class
pyarray
;
}
namespace
pybind11
{
namespace
detail
{
template
<
class
T
>
struct
handle_type_name
<xt::pyarray<T>>
{
static
PYBIND11_DESCR
name
()
{
return
_
(
"
numpy.ndarray[
"
) + make_caster<T>::
name
() +
_
(
"
]
"
);
}
};
template
<
typename
T>
struct
pyobject_caster
<xt::pyarray<T>>
{
using
type = xt::pyarray<T>;
bool
load
(handle src,
bool
)
{
value =
type::ensure
(src);
return
static_cast
<
bool
>(value);
}
static
handle
cast
(
const
handle &src, return_value_policy, handle)
{
return
src.
inc_ref
();
}
PYBIND11_TYPE_CASTER
(type, handle_type_name<type>::name());
};
}
}
namespace
xt
{
template
<
class
A
>
class
pyarray_backstrides
{
public:
using
array_type = A;
using
value_type =
typename
array_type::size_type;
using
size_type =
typename
array_type::size_type;
pyarray_backstrides
() =
default
;
pyarray_backstrides
(
const
array_type& a);
value_type
operator
[](size_type i)
const
;
private:
const
array_type* p_a;
};
template
<
class
T
>
struct
xiterable_inner_types
<pyarray<T>>
: xcontainer_iterable_types<pyarray<T>>
{
};
template
<
class
T
>
struct
xcontainer_inner_types
<pyarray<T>>
{
using
container_type = pybuffer_adaptor<T>;
using
shape_type = std::vector<
typename
container_type::size_type>;
using
strides_type = shape_type;
using
backstrides_type = pyarray_backstrides<pyarray<T>>;
using
inner_shape_type = pybuffer_adaptor<std::
size_t
>;
using
inner_strides_type = pystrides_adaptor<
sizeof
(T)>;
using
inner_backstrides_type = backstrides_type;
using
temporary_type = pyarray<T>;
};
/*
*
* @class pyarray
* @brief Wrapper on the Python buffer protocol.
*/
template
<
class
T
>
class
pyarray
:
public
pycontainer
<pyarray<T>>,
public xcontainer_semantic<pyarray<T>>
{
public:
using
self_type = pyarray<T>;
using
semantic_base = xcontainer_semantic<self_type>;
using
base_type = pycontainer<self_type>;
using
container_type =
typename
base_type::container_type;
using
value_type =
typename
base_type::value_type;
using
pointer =
typename
base_type::pointer;
using
size_type =
typename
base_type::size_type;
using
shape_type =
typename
base_type::shape_type;
using
strides_type =
typename
base_type::strides_type;
using
backstrides_type =
typename
base_type::backstrides_type;
using
inner_shape_type =
typename
base_type::inner_shape_type;
using
inner_strides_type =
typename
base_type::inner_strides_type;
using
inner_backstrides_type =
typename
base_type::inner_backstrides_type;
pyarray
() =
default
;
pyarray
(
const
value_type& t);
pyarray
(
nested_initializer_list_t
<T,
1
> t);
pyarray
(
nested_initializer_list_t
<T,
2
> t);
pyarray
(
nested_initializer_list_t
<T,
3
> t);
pyarray
(
nested_initializer_list_t
<T,
4
> t);
pyarray
(
nested_initializer_list_t
<T,
5
> t);
pyarray
(pybind11::handle h, pybind11::object::
borrowed_t
);
pyarray
(pybind11::handle h, pybind11::object::
stolen_t
);
pyarray
(
const
pybind11::object &o);
explicit
pyarray
(
const
shape_type& shape, layout l = layout::row_major);
pyarray
(
const
shape_type& shape,
const
strides_type& strides);
template
<
class
E
>
pyarray
(
const
xexpression<E>& e);
template
<
class
E
>
self_type&
operator
=(
const
xexpression<E>& e);
using
base_type::begin;
using
base_type::end;
static
self_type
ensure
(pybind11::handle h);
static
bool
check_
(pybind11::handle h);
private:
inner_shape_type m_shape;
inner_strides_type m_strides;
mutable
inner_backstrides_type m_backstrides;
container_type m_data;
void
init_array
(
const
shape_type& shape,
const
strides_type& strides);
void
init_from_python
();
const
inner_shape_type&
shape_impl
()
const
;
const
inner_strides_type&
strides_impl
()
const
;
const
inner_backstrides_type&
backstrides_impl
()
const
;
container_type&
data_impl
();
const
container_type&
data_impl
()
const
;
friend
class
xcontainer
<pyarray<T>>;
};
/*
*************************************
* pyarray_backstrides implementation *
*************************************
*/
template
<
class
A
>
inline
pyarray_backstrides<A>::pyarray_backstrides(
const
array_type& a)
: p_a(&a)
{
}
template
<
class
A
>
inline
auto
pyarray_backstrides<A>::
operator
[](size_type i)
const
-> value_type
{
value_type sh = p_a->
shape
()[i];
value_type res = sh ==
1
?
0
: (sh -
1
) * p_a->
strides
()[i];
return
res;
}
/*
*************************
* pyarray implementation *
*************************
*/
template
<
class
T
>
inline
pyarray<T>::pyarray(
const
value_type& t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
nested_initializer_list_t
<T,
1
> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
nested_initializer_list_t
<T,
2
> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
nested_initializer_list_t
<T,
3
> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
nested_initializer_list_t
<T,
4
> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
nested_initializer_list_t
<T,
5
> t)
{
base_type::reshape
(xt::shape<shape_type>(t), layout::row_major);
nested_copy
(m_data.
begin
(), t);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(pybind11::handle h, pybind11::object::
borrowed_t
)
: base_type(h, pybind11::object::borrowed)
{
init_from_python
();
}
template
<
class
T
>
inline
pyarray<T>::pyarray(pybind11::handle h, pybind11::object::
stolen_t
)
: base_type(h, pybind11::object::stolen)
{
init_from_python
();
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
const
pybind11::object &o)
: base_type(o)
{
init_from_python
();
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
const
shape_type& shape, layout l)
{
strides_type strides;
base_type::fill_default_strides
(shape, l, strides);
init_array
(shape, strides);
}
template
<
class
T
>
inline
pyarray<T>::pyarray(
const
shape_type& shape,
const
strides_type& strides)
{
init_array
(shape, strides);
}
template
<
class
T
>
template
<
class
E
>
inline
pyarray<T>::pyarray(
const
xexpression<E>& e)
{
semantic_base::assign
(e);
}
template
<
class
T
>
template
<
class
E
>
inline
auto
pyarray<T>::
operator
=(
const
xexpression<E>& e) -> self_type&
{
return
semantic_base::
operator
=(e);
}
template
<
class
T
>
inline
auto
pyarray<T>::ensure(pybind11::handle h) -> self_type
{
return
base_type::ensure
(h);
}
template
<
class
T
>
inline
bool
pyarray<T>::check_(pybind11::handle h)
{
return
base_type::check_
(h);
}
template
<
class
T
>
inline
void
pyarray<T>::init_array(
const
shape_type& shape,
const
strides_type& strides)
{
strides_type
adapted_strides
(strides);
std::transform
(strides.
begin
(), strides.
end
(), adapted_strides.
begin
(),
[](
auto
v) {
return
sizeof
(T) * v; });
int
flags =
NPY_ARRAY_ALIGNED
;
if
(!std::is_const<T>::value)
{
flags |=
NPY_ARRAY_WRITEABLE
;
}
int
type_num = detail::numpy_traits<T>::type_num;
npy_intp* shape_data =
reinterpret_cast
<npy_intp*>(
const_cast
<size_type*>(shape.
data
()));
npy_intp* strides_data =
reinterpret_cast
<npy_intp*>(adapted_strides.
data
());
auto
tmp = pybind11::reinterpret_steal<pybind11::object>(
PyArray_New
(&PyArray_Type,
static_cast
<
int
>(shape.
size
()), shape_data, type_num, strides_data,
nullptr
,
static_cast
<
int
>(
sizeof
(T)), flags,
nullptr
)
);
if
(!tmp)
throw
std::runtime_error
(
"
NumPy: unable to create ndarray
"
);
this
->
m_ptr
= tmp.
release
().
ptr
();
init_from_python
();
}
template
<
class
T
>
inline
void
pyarray<T>::init_from_python()
{
m_shape =
inner_shape_type
(
reinterpret_cast
<size_type*>(
PyArray_SHAPE
(
this
->
python_array
())),
static_cast
<size_type>(
PyArray_NDIM
(
this
->
python_array
())));
m_strides =
inner_strides_type
(
reinterpret_cast
<size_type*>(
PyArray_STRIDES
(
this
->
python_array
())),
static_cast
<size_type>(
PyArray_NDIM
(
this
->
python_array
())));
m_backstrides =
backstrides_type
(*
this
);
m_data =
container_type
(
reinterpret_cast
<pointer>(
PyArray_DATA
(
this
->
python_array
())),
static_cast
<size_type>(
PyArray_SIZE
(
this
->
python_array
())));
}
template
<
class
T
>
inline
auto
pyarray<T>::shape_impl()
const
->
const
inner_shape_type&
{
return
m_shape;
}
template
<
class
T
>
inline
auto
pyarray<T>::strides_impl()
const
->
const
inner_strides_type&
{
return
m_strides;
}
template
<
class
T
>
inline
auto
pyarray<T>::backstrides_impl()
const
->
const
inner_backstrides_type&
{
//
The pyarray object may be copied, invalidating m_backstrides. Building
//
it each time it is needed avoids tricky bugs.
m_backstrides =
backstrides_type
(*
this
);
return
m_backstrides;
}
template
<
class
T
>
inline
auto
pyarray<T>::data_impl() -> container_type&
{
return
m_data;
}
template
<
class
T
>
inline
auto
pyarray<T>::data_impl()
const
->
const
container_type&
{
return
m_data;
}
}
#
endif
Back
|
FazBrowse Home
|
New Git URL