1 #ifndef __OPT0FINDER_PYUTILS_CXX__
2 #define __OPT0FINDER_PYUTILS_CXX__
5 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
7 #include "numpy/arrayobject.h"
12 namespace flashmatch {
15 static bool once =
false;
24 std::vector<size_t> dims(2,0);
25 dims[0] = traj.size();
27 auto pyarray = numpy_array<double>(dims);
30 const int dtype = NPY_DOUBLE;
31 PyArray_Descr *descr = PyArray_DescrFromType(dtype);
33 if (PyArray_AsCArray(&pyarray, (
void **)&carray, pydims, 2, descr) < 0) {
34 std::cerr<<
"Failed to create 2D numpy array"<<std::endl;
35 throw std::exception();
37 assert(pydims[0] == ((
int)(dims[0])) && pydims[1] == ((
int)(dims[1])));
39 for(
size_t idx=0; idx<dims[0]; ++idx) {
40 carray[idx][0] = traj[idx].x;
41 carray[idx][1] = traj[idx].y;
42 carray[idx][2] = traj[idx].z;
43 carray[idx][3] = traj[idx].q;
45 PyArray_Free(pyarray, (
void *)carray);
51 std::vector<size_t> dims(2,0);
52 dims[0] = traj.size();
54 auto pyarray = numpy_array<double>(dims);
57 const int dtype = NPY_DOUBLE;
58 PyArray_Descr *descr = PyArray_DescrFromType(dtype);
60 if (PyArray_AsCArray(&pyarray, (
void **)&carray, pydims, 2, descr) < 0) {
61 std::cerr<<
"Failed to create 2D numpy array"<<std::endl;
62 throw std::exception();
64 assert(pydims[0] == ((
int)(dims[0])) && pydims[1] == ((
int)(dims[1])));
66 for(
size_t idx=0; idx<dims[0]; ++idx) {
67 carray[idx][0] = traj[idx][0];
68 carray[idx][1] = traj[idx][1];
69 carray[idx][2] = traj[idx][2];
71 PyArray_Free(pyarray, (
void *)carray);
77 std::vector<size_t> dims(1);
78 dims[0] = flash.
pe_v.size();
79 auto pyarray = numpy_array<double>(dims);
82 const int dtype = NPY_DOUBLE;
83 PyArray_Descr *descr = PyArray_DescrFromType(dtype);
85 if (PyArray_AsCArray(&pyarray, (
void **)&carray, pydims, 1, descr) < 0) {
86 std::cerr<<
"Failed to create 2D numpy array"<<std::endl;
87 throw std::exception();
89 assert(pydims[0] == ((
int)(dims[0])));
91 for(
size_t idx=0; idx<dims[0]; ++idx)
92 carray[idx] = flash.
pe_v[idx];
93 PyArray_Free(pyarray, (
void *)carray);
126 PyArrayObject *ptr = (PyArrayObject *)(arrayin);
133 if (PyArray_NDIM(ptr) != 1){
134 throw std::exception();
137 if ((
long)(cvec.size()) != PyArray_SIZE(ptr))
138 throw std::exception();
141 auto fptr = (T *)(PyArray_GetPtr(ptr, loc));
142 for (
size_t i = 0; i < size_t(PyArray_SIZE(ptr)); ++i) {
196 if (vec.size() >= INT_MAX) {
197 std::cerr <<
"Length of data vector too long to specify ndarray. " << std::endl;
198 throw std::exception();
202 dims[0] = (int)vec.size();
203 PyArrayObject *
array = (PyArrayObject *)PyArray_SimpleNewFromData(
204 nd, dims, ctype_to_numpy<T>(), (
char *)&(vec[0]));
205 return PyArray_Return(array);
230 int nd_ = dims.size();
232 for(
size_t i=0; i<dims.size(); ++i) dims_[i] = dims[i];
242 PyObject* res = PyArray_ZEROS(nd_,dims_,ctype_to_numpy<T>(),0);
248 PyArrayObject *ptr = (PyArrayObject*)(res);
void SetPyUtil()
Utility function: call one-time-only numpy module initialization (you don't have to call) ...
BEGIN_PROLOG could also be cerr
template void _copy_array< unsigned short >(PyObject *arrayin, const std::vector< unsigned short > &cvec)
PyObject * _as_ndarray(const std::vector< T > &vec)
convert vectors into np array
template PyObject * _as_ndarray< long long >(const std::vector< long long > &vec)
template void _copy_array< int >(PyObject *arrayin, const std::vector< int > &cvec)
int ctype_to_numpy< short >()
template PyObject * _as_ndarray< float >(const std::vector< float > &vec)
template PyObject * _as_ndarray< double >(const std::vector< double > &vec)
template PyObject * _as_ndarray< short >(const std::vector< short > &vec)
Struct to represent an optical flash.
template void _copy_array< unsigned int >(PyObject *arrayin, const std::vector< unsigned int > &cvec)
template PyObject * numpy_array< float >(std::vector< size_t >dims)
int ctype_to_numpy< float >()
void copy_array(PyObject *arrayin, const std::vector< unsigned short > &cvec)
then echo ***************************************echo array
int ctype_to_numpy< unsigned long long >()
std::vector< double > pe_v
PE distribution over photo-detectors.
PyObject * as_ndarray(const QCluster_t &traj)
template PyObject * _as_ndarray< unsigned short >(const std::vector< unsigned short > &vec)
int ctype_to_numpy< unsigned int >()
int ctype_to_numpy< long long >()
Collection of charge deposition 3D point (cluster)
template void _copy_array< long long >(PyObject *arrayin, const std::vector< long long > &cvec)
template PyObject * _as_ndarray< unsigned int >(const std::vector< unsigned int > &vec)
int ctype_to_numpy< double >()
void _copy_array(PyObject *arrayin, const std::vector< T > &cvec)
copy array
PyObject * numpy_array(std::vector< size_t > dims)
template void _copy_array< float >(PyObject *arrayin, const std::vector< float > &cvec)
template void _copy_array< short >(PyObject *arrayin, const std::vector< short > &cvec)
template void _copy_array< double >(PyObject *arrayin, const std::vector< double > &cvec)
int ctype_to_numpy< unsigned short >()
int ctype_to_numpy< int >()
template PyObject * _as_ndarray< unsigned long long >(const std::vector< unsigned long long > &vec)
template PyObject * _as_ndarray< int >(const std::vector< int > &vec)