-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathAttributable.cpp
More file actions
653 lines (616 loc) · 24.1 KB
/
Attributable.cpp
File metadata and controls
653 lines (616 loc) · 24.1 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/* Copyright 2018-2025 Axel Huebl, Franz Poeschel, Luca Fedeli
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/backend/Attributable.hpp"
#include "openPMD/DatatypeHelpers.hpp"
#include "openPMD/auxiliary/Variant.hpp"
#include "openPMD/backend/Attribute.hpp"
#include "openPMD/backend/Variant_internal.hpp"
#include "openPMD/binding/python/Common.hpp"
#include "openPMD/binding/python/Numpy.hpp"
#include <pybind11/detail/common.h>
#include <algorithm>
#include <array>
#include <complex>
#include <exception>
#include <iterator>
#include <map>
#include <optional>
#include <pybind11/pytypes.h>
#include <string>
#include <type_traits>
#include <vector>
using PyAttributeKeys = std::vector<std::string>;
bool setAttributeFromBufferInfo(
Attributable &attr, std::string const &key, py::buffer &a)
{
using DT = Datatype;
py::buffer_info buf = a.request();
// Numpy: Handling of arrays and scalars
// work-around for https://github.com/pybind/pybind11/issues/1224
// -> passing numpy scalars as buffers needs numpy 1.15+
// https://github.com/numpy/numpy/issues/10265
// https://github.com/pybind/pybind11/issues/1224#issuecomment-354357392
// scalars, see PEP 3118
// requires Numpy 1.15+
if (buf.ndim == 0)
{
// refs:
// https://docs.scipy.org/doc/numpy-1.15.0/reference/arrays.interface.html
// https://docs.python.org/3/library/struct.html#format-characters
// std::cout << " scalar type '" << buf.format << "'" << std::endl;
// typestring: encoding + type + number of bytes
switch (dtype_from_bufferformat(buf.format))
{
case DT::BOOL:
return attr.setAttribute(key, *static_cast<bool *>(buf.ptr));
break;
case DT::SHORT:
return attr.setAttribute(key, *static_cast<short *>(buf.ptr));
break;
case DT::INT:
return attr.setAttribute(key, *static_cast<int *>(buf.ptr));
break;
case DT::LONG:
return attr.setAttribute(key, *static_cast<long *>(buf.ptr));
break;
case DT::LONGLONG:
return attr.setAttribute(key, *static_cast<long long *>(buf.ptr));
break;
case DT::USHORT:
return attr.setAttribute(
key, *static_cast<unsigned short *>(buf.ptr));
break;
case DT::UINT:
return attr.setAttribute(
key, *static_cast<unsigned int *>(buf.ptr));
break;
case DT::ULONG:
return attr.setAttribute(
key, *static_cast<unsigned long *>(buf.ptr));
break;
case DT::ULONGLONG:
return attr.setAttribute(
key, *static_cast<unsigned long long *>(buf.ptr));
break;
case DT::FLOAT:
return attr.setAttribute(key, *static_cast<float *>(buf.ptr));
break;
case DT::DOUBLE:
return attr.setAttribute(key, *static_cast<double *>(buf.ptr));
break;
case DT::LONG_DOUBLE:
return attr.setAttribute(key, *static_cast<long double *>(buf.ptr));
break;
case DT::CFLOAT:
return attr.setAttribute(
key, *static_cast<std::complex<float> *>(buf.ptr));
break;
case DT::CDOUBLE:
return attr.setAttribute(
key, *static_cast<std::complex<double> *>(buf.ptr));
break;
case DT::CLONG_DOUBLE:
return attr.setAttribute(
key, *static_cast<std::complex<long double> *>(buf.ptr));
break;
default:
throw std::runtime_error(
"set_attribute: Unknown "
"Python type '" +
buf.format + "' for attribute '" + key + "'");
}
return false;
}
// lists & ndarrays: all will be flattended to 1D lists
else
{
// std::cout << " array type '" << buf.format << "'" << std::endl;
/* required are contiguous buffers
*
* - not strided with paddings
* - not a view in another buffer that results in striding
*/
auto *view = new Py_buffer();
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
if (PyObject_GetBuffer(a.ptr(), view, flags) != 0)
{
delete view;
throw py::error_already_set();
}
bool isContiguous = (PyBuffer_IsContiguous(view, 'A') != 0);
PyBuffer_Release(view);
delete view;
if (!isContiguous)
throw py::index_error(
"non-contiguous buffer provided, handling not implemented!");
// @todo in order to implement stride handling, one needs to
// loop over the input data strides during write below,
// also data might not be owned!
// dtype handling
/*if( buf.format.find("?") != std::string::npos )
return attr.setAttribute( key,
std::vector<bool>(
static_cast<bool*>(buf.ptr),
static_cast<bool*>(buf.ptr) + buf.size
) );
else */
// std::cout << "+++++++++++ BUFFER: " << buf.format << std::endl;
if (buf.format.find('b') != std::string::npos)
return attr.setAttribute(
key,
std::vector<char>(
static_cast<char *>(buf.ptr),
static_cast<char *>(buf.ptr) + buf.size));
else if (buf.format.find('h') != std::string::npos)
return attr.setAttribute(
key,
std::vector<short>(
static_cast<short *>(buf.ptr),
static_cast<short *>(buf.ptr) + buf.size));
else if (buf.format.find('i') != std::string::npos)
return attr.setAttribute(
key,
std::vector<int>(
static_cast<int *>(buf.ptr),
static_cast<int *>(buf.ptr) + buf.size));
else if (buf.format.find('l') != std::string::npos)
return attr.setAttribute(
key,
std::vector<long>(
static_cast<long *>(buf.ptr),
static_cast<long *>(buf.ptr) + buf.size));
else if (buf.format.find('q') != std::string::npos)
return attr.setAttribute(
key,
std::vector<long long>(
static_cast<long long *>(buf.ptr),
static_cast<long long *>(buf.ptr) + buf.size));
else if (buf.format.find('B') != std::string::npos)
return attr.setAttribute(
key,
std::vector<unsigned char>(
static_cast<unsigned char *>(buf.ptr),
static_cast<unsigned char *>(buf.ptr) + buf.size));
else if (buf.format.find('H') != std::string::npos)
return attr.setAttribute(
key,
std::vector<unsigned short>(
static_cast<unsigned short *>(buf.ptr),
static_cast<unsigned short *>(buf.ptr) + buf.size));
else if (buf.format.find('I') != std::string::npos)
return attr.setAttribute(
key,
std::vector<unsigned int>(
static_cast<unsigned int *>(buf.ptr),
static_cast<unsigned int *>(buf.ptr) + buf.size));
else if (buf.format.find('L') != std::string::npos)
return attr.setAttribute(
key,
std::vector<unsigned long>(
static_cast<unsigned long *>(buf.ptr),
static_cast<unsigned long *>(buf.ptr) + buf.size));
else if (buf.format.find('Q') != std::string::npos)
return attr.setAttribute(
key,
std::vector<unsigned long long>(
static_cast<unsigned long long *>(buf.ptr),
static_cast<unsigned long long *>(buf.ptr) + buf.size));
else if (buf.format.find("Zf") != std::string::npos)
return attr.setAttribute(
key,
std::vector<std::complex<float>>(
static_cast<std::complex<float> *>(buf.ptr),
static_cast<std::complex<float> *>(buf.ptr) + buf.size));
else if (buf.format.find("Zd") != std::string::npos)
return attr.setAttribute(
key,
std::vector<std::complex<double>>(
static_cast<std::complex<double> *>(buf.ptr),
static_cast<std::complex<double> *>(buf.ptr) + buf.size));
else if (buf.format.find("Zg") != std::string::npos)
return attr.setAttribute(
key,
std::vector<std::complex<long double>>(
static_cast<std::complex<long double> *>(buf.ptr),
static_cast<std::complex<long double> *>(buf.ptr) +
buf.size));
else if (buf.format.find('f') != std::string::npos)
return attr.setAttribute(
key,
std::vector<float>(
static_cast<float *>(buf.ptr),
static_cast<float *>(buf.ptr) + buf.size));
else if (buf.format.find('d') != std::string::npos)
return attr.setAttribute(
key,
std::vector<double>(
static_cast<double *>(buf.ptr),
static_cast<double *>(buf.ptr) + buf.size));
else if (buf.format.find('g') != std::string::npos)
return attr.setAttribute(
key,
std::vector<long double>(
static_cast<long double *>(buf.ptr),
static_cast<long double *>(buf.ptr) + buf.size));
else
throw std::runtime_error(
"set_attribute: Unknown "
"Python type '" +
buf.format + "' for attribute '" + key + "'");
return false;
}
}
namespace detail
{
template <typename RequestedType>
bool setAttributeFromObject_default(
Attributable &attr, std::string const &key, py::object &obj)
{
if (std::string(py::str(py::type::of(obj))) == "<class 'list'>")
{
using ListType = std::vector<RequestedType>;
return attr.setAttribute<ListType>(key, obj.cast<ListType>());
}
else
{
return attr.setAttribute<RequestedType>(key, obj.cast<RequestedType>());
}
}
bool setAttributeFromObject_double(
Attributable &attr, std::string const &key, py::object &obj)
{
if (std::string(py::str(py::type::of(obj))) == "<class 'list'>")
{
using ListType = std::vector<double>;
using ArrayType = std::array<double, 7>;
ListType const &asVector = obj.cast<ListType>();
if (asVector.size() == 7 && key == "unitDimension")
{
ArrayType asArray;
std::copy_n(asVector.begin(), 7, asArray.begin());
return attr.setAttribute<ArrayType>(key, asArray);
}
else
{
return attr.setAttribute<ListType>(key, asVector);
}
}
else
{
return attr.setAttribute<double>(key, obj.cast<double>());
}
}
bool setAttributeFromObject_bool(
Attributable &attr, std::string const &key, py::object &obj)
{
return attr.setAttribute<bool>(key, obj.cast<bool>());
}
template <bool is_signed = std::is_signed_v<char>>
struct char_to_explicit_char;
template <>
struct char_to_explicit_char<true>
{
using type = signed char;
using opposite_type = unsigned char;
};
template <>
struct char_to_explicit_char<false>
{
using type = unsigned char;
using opposite_type = signed char;
};
template <typename TargetType>
std::optional<TargetType> tryCast(py::object const &obj)
{
// Do a check to avoid throwing exceptions
if constexpr (std::is_default_constructible_v<TargetType>)
{
TargetType val{};
auto python_val = py::cast(std::move(val));
if (!py::isinstance(obj, py::type::of(python_val)))
{
return std::nullopt;
}
}
try
{
return obj.cast<TargetType>();
}
catch (py::cast_error const &)
{
return std::nullopt;
}
catch (py::value_error const &err)
{
return std::nullopt;
}
}
template <typename Char_t>
bool setAttributeFromObject_char(
Attributable &attr, std::string const &key, py::object &obj)
{
using explicit_char_type = std::conditional_t<
std::is_same_v<Char_t, char>,
typename char_to_explicit_char<>::type,
Char_t>;
using ListString = std::vector<std::string>;
/*
* We cannot distinguish strings with length 1 from chars at this place,
* so avoid this cast. If the attribute is actually a char, skipping this
* branch means that it might be upcasted to string.
*/
#if 0
using ListChar = std::vector<char>;
if (auto casted_char = tryCast<char>(obj); casted_char.has_value())
{
return attr.setAttribute<char>(key, *casted_char);
} else
#endif
// This must come after tryCast<char>
// because tryCast<string> implicitly covers chars as well.
if (auto casted_string = tryCast<std::string>(obj);
casted_string.has_value())
{
return attr.setAttribute<std::string>(key, std::move(*casted_string));
}
// Assuming `char` is signed on the current platform,
// then this cast will cover `signed char`.
// It's a bit weird: The Numpy datatype will be the same as `char`
// (.ie. 'b'), but the `py::object` will contain an integer.
// Similar for `unsigned char` if `char`s are unsigned.
else if (auto casted_int = tryCast<int>(obj); casted_int.has_value())
{
return attr.setAttribute<explicit_char_type>(
key, explicit_char_type(*casted_int));
}
// NOW: List casts.
// All list casts must come after all scalar casts,
// because list casts implicitly cover scalars too.
/*
* We cannot distinguish strings with length 1 from chars at this place,
* so avoid this cast. If the attribute is actually a vector of char,
* skipping this branch means that it might be upcasted to a vector of
* string.
*/
#if 0
else if (auto list_of_char = tryCast<ListChar>(obj);
list_of_char.has_value())
{
return attr.setAttribute<ListChar>(key, std::move(*list_of_char));
}
#endif
// this must come after tryCast<vector<char>>,
// because tryCast<vector<string>> implicitly covers chars as well
else if (
auto list_of_string = tryCast<ListString>(obj);
list_of_string.has_value())
{
return attr.setAttribute<ListString>(key, std::move(*list_of_string));
}
// Again: `char` vs. `signed char`, resp. `char` vs. `unsigned char`
// depending on `char`'s signedness.
else if (
auto list_of_int = tryCast<std::vector<int>>(obj);
list_of_int.has_value())
{
std::vector<explicit_char_type> casted;
casted.reserve(list_of_int->size());
std::transform(
list_of_int->begin(),
list_of_int->end(),
std::back_inserter(casted),
[](int const val) { return explicit_char_type(val); });
return attr.setAttribute<std::vector<explicit_char_type>>(
key, std::move(casted));
}
else
{
throw std::runtime_error(
"[Python SetAttributeFromObject<char>] Was not able to use passed "
"object as any char-based type.");
}
}
} // namespace detail
struct SetAttributeFromObject
{
static constexpr char const *errorMsg = "Attributable.set_attribute()";
template <typename RequestedType>
static bool
call(Attributable &attr, std::string const &key, py::object &obj)
{
if constexpr (std::is_same_v<RequestedType, double>)
{
return ::detail::setAttributeFromObject_double(attr, key, obj);
}
else if constexpr (std::is_same_v<RequestedType, bool>)
{
return ::detail::setAttributeFromObject_bool(attr, key, obj);
}
else if constexpr (
std::is_same_v<RequestedType, char> ||
std::is_same_v<RequestedType, signed char> ||
std::is_same_v<RequestedType, unsigned char>)
{
return ::detail::setAttributeFromObject_char<RequestedType>(
attr, key, obj);
}
else
{
return ::detail::setAttributeFromObject_default<RequestedType>(
attr, key, obj);
}
}
};
bool setAttributeFromObject(
Attributable &attr,
std::string const &key,
py::object &obj,
pybind11::dtype datatype)
{
Datatype requestedDatatype = dtype_from_numpy(std::move(datatype));
return switchNonVectorType<SetAttributeFromObject>(
requestedDatatype, attr, key, obj);
}
void init_Attributable(py::module &m)
{
py::class_<Attributable::MyPath>(m, "AttributablePath")
.def_readonly("directory", &Attributable::MyPath::directory)
.def_readonly("series_name", &Attributable::MyPath::seriesName)
.def_readonly(
"series_extension", &Attributable::MyPath::seriesExtension)
.def_readonly("group", &Attributable::MyPath::group)
.def_readonly("access", &Attributable::MyPath::access)
.def_property_readonly("file_path", &Attributable::MyPath::filePath)
.def_property_readonly(
"openPMD_path", &Attributable::MyPath::openPMDPath);
py::class_<Attributable>(m, "Attributable")
.def(py::init<Attributable const &>())
.def(
"__repr__",
[](Attributable const &attr) {
return "<openPMD.Attributable with '" +
std::to_string(attr.numAttributes()) + "' attribute(s)>";
})
.def(
"series_flush",
py::overload_cast<std::string>(&Attributable::seriesFlush),
py::arg("backend_config") = "{}")
.def(
"iteration_flush",
py::overload_cast<std::string>(&Attributable::iterationFlush),
py::arg("backend_config") = "{}")
.def_property_readonly(
"attributes",
[](Attributable &attr) { return attr.attributes(); },
// ref + keepalive
py::return_value_policy::move)
// C++ pass-through API: Setter
// note that the order of overloads is important!
// all buffer protocol compatible objects, including numpy arrays if not
// specialized specifically...
.def(
"set_attribute",
[](Attributable &attr, std::string const &key, py::buffer &a) {
// std::cout << "set attr via py::buffer: " << key << std::endl;
return setAttributeFromBufferInfo(attr, key, a);
})
.def(
"set_attribute",
[](Attributable &attr,
std::string const &key,
py::object &obj,
pybind11::dtype datatype) {
return setAttributeFromObject(
attr, key, obj, std::move(datatype));
},
py::arg("key"),
py::arg("value"),
py::arg("datatype"))
// fundamental Python types
.def("set_attribute", &Attributable::setAttribute<bool>)
.def(
"set_attribute",
&Attributable::setAttribute<
typename ::detail::char_to_explicit_char<>::opposite_type>)
// -> handle all native python integers as long
// .def("set_attribute", &Attributable::setAttribute< short >)
// .def("set_attribute", &Attributable::setAttribute< int >)
// .def("set_attribute", &Attributable::setAttribute< long >)
// .def("set_attribute", &Attributable::setAttribute< long long >)
// .def("set_attribute", &Attributable::setAttribute< unsigned short >)
// .def("set_attribute", &Attributable::setAttribute< unsigned int >)
// .def("set_attribute", &Attributable::setAttribute< unsigned long >)
// .def("set_attribute", &Attributable::setAttribute< unsigned long long
// >)
.def("set_attribute", &Attributable::setAttribute<long>)
// work-around for https://github.com/pybind/pybind11/issues/1512
// -> handle all native python floats as double
// .def("set_attribute", &Attributable::setAttribute< float >)
// .def("set_attribute", &Attributable::setAttribute< long double >)
.def("set_attribute", &Attributable::setAttribute<double>)
// work-around for https://github.com/pybind/pybind11/issues/1509
// -> since there is only str in Python, chars are strings
// .def("set_attribute", &Attributable::setAttribute< char >)
.def(
"set_attribute",
[](Attributable &attr,
std::string const &key,
std::string const &value) {
return attr.setAttribute(key, value);
})
// Plain Python arrays and plain python lists of homogeneous,
// fundamental Python types not specialized in C++ API
// .def("set_attribute", &Attributable::setAttribute< std::vector< bool
// > >) there is only str in Python, chars are strings
// .def("set_attribute", &Attributable::setAttribute< std::vector< char
// > >)
.def(
"set_attribute",
&Attributable::setAttribute<std::vector<unsigned char>>)
.def("set_attribute", &Attributable::setAttribute<std::vector<long>>)
.def(
"set_attribute",
&Attributable::setAttribute<std::vector<
double>>) // TODO: this implicitly casts list of complex
// probably affected by bug
// https://github.com/pybind/pybind11/issues/1258
.def(
"set_attribute",
[](Attributable &attr,
std::string const &key,
std::vector<std::string> const &value) {
return attr.setAttribute(key, value);
})
// .def("set_attribute", &Attributable::setAttribute< std::array<
// double, 7 > >)
// C++ pass-through API: Getter
.def(
"get_attribute",
[](Attributable &attr, std::string const &key) {
auto v = attr.getAttribute(key);
return std::visit(
[](auto const &val) { return py::cast(val); },
v.getVariant<attribute_types>());
// TODO instead of returning lists, return all arrays (ndim > 0)
// as numpy arrays?
})
.def_property_readonly(
"attribute_dtypes",
[](Attributable const &attributable) {
std::map<std::string, pybind11::dtype> dtypes;
for (auto const &attr : attributable.attributes())
{
dtypes[attr] =
dtype_to_numpy(attributable.getAttribute(attr).dtype);
}
return dtypes;
})
.def("delete_attribute", &Attributable::deleteAttribute)
.def("contains_attribute", &Attributable::containsAttribute)
// @todo _ipython_key_completions_ if we find a way to add a []
// interface
.def_property(
"comment", &Attributable::comment, &Attributable::setComment)
// TODO remove in future versions (deprecated)
.def("set_comment", &Attributable::setComment)
.def("my_path", &Attributable::myPath);
py::bind_vector<PyAttributeKeys>(m, "Attribute_Keys");
}