Commit ebe35be5 authored by Tiago Peixoto's avatar Tiago Peixoto

Improve numpy binding to boost::multi_array

parent 6a5b5703
......@@ -91,8 +91,8 @@ struct get_avg_correlation
python::list ret_bins;
ret_bins.append(wrap_vector_owned(bins[0]));
_ret_bins = ret_bins;
_avg = wrap_multi_array_owned<avg_type,1>(sum.get_array());
_dev = wrap_multi_array_owned<avg_type,1>(sum2.get_array());
_avg = wrap_multi_array_owned(sum.get_array());
_dev = wrap_multi_array_owned(sum2.get_array());
}
python::object& _avg;
python::object& _dev;
......
......@@ -73,7 +73,7 @@ struct get_correlation_histogram
ret_bins.append(wrap_vector_owned(bins[0]));
ret_bins.append(wrap_vector_owned(bins[1]));
_ret_bins = ret_bins;
_hist = wrap_multi_array_owned<count_type,2>(hist.get_array());
_hist = wrap_multi_array_owned(hist.get_array());
}
python::object& _hist;
const std::array<vector<long double>,2>& _bins;
......
......@@ -138,17 +138,19 @@ boost::python::object wrap_vector_not_owned(const std::vector<std::array<ValueTy
return o;
}
template <class ValueType, size_t Dim>
template <class Array>
boost::python::object
wrap_multi_array_owned(const boost::multi_array<ValueType,Dim>& array)
wrap_multi_array_owned(const Array& array)
{
size_t val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
npy_intp shape[Dim];
for (size_t i = 0; i < Dim; ++i)
typedef typename Array::element value_t;
constexpr int dim = Array::dimensionality;
size_t val_type = boost::mpl::at<numpy_types,value_t>::type::value;
npy_intp shape[dim];
for (size_t i = 0; i < dim; ++i)
shape[i] = array.shape()[i];
PyArrayObject* ndarray =
(PyArrayObject*) PyArray_SimpleNew(Dim, shape, val_type);
memcpy(PyArray_DATA(ndarray), array.data(), array.num_elements() * sizeof(ValueType));
(PyArrayObject*) PyArray_SimpleNew(dim, shape, val_type);
memcpy(PyArray_DATA(ndarray), array.data(), array.num_elements() * sizeof(value_t));
PyArray_ENABLEFLAGS(ndarray, NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_OWNDATA | NPY_ARRAY_WRITEABLE);
boost::python::handle<> x((PyObject*) ndarray);
......@@ -156,14 +158,19 @@ wrap_multi_array_owned(const boost::multi_array<ValueType,Dim>& array)
return o;
}
template <class ValueType, size_t Dim>
template <class Array>
boost::python::object
wrap_multi_array_not_owned(boost::multi_array<ValueType,Dim>& array)
wrap_multi_array_not_owned(Array& array)
{
size_t val_type = boost::mpl::at<numpy_types,ValueType>::type::value;
typedef typename Array::element value_t;
constexpr int dim = Array::dimensionality;
size_t val_type = boost::mpl::at<numpy_types,value_t>::type::value;
npy_intp shape[dim];
for (size_t i = 0; i < dim; ++i)
shape[i] = array.shape()[i];
PyArrayObject* ndarray =
(PyArrayObject*) PyArray_SimpleNewFromData(Dim, array.shape(), val_type,
array.origin());
(PyArrayObject*) PyArray_SimpleNewFromData(dim, shape, val_type,
array.data());
PyArray_ENABLEFLAGS(ndarray, NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS |
NPY_ARRAY_WRITEABLE);
boost::python::handle<> x((PyObject*) ndarray);
......
......@@ -106,7 +106,7 @@ struct get_distance_histogram
s_hist.gather();
python::list ret;
ret.append(wrap_multi_array_owned<size_t,1>(hist.get_array()));
ret.append(wrap_multi_array_owned(hist.get_array()));
ret.append(wrap_vector_owned<val_type>(hist.get_bins()[0]));
phist = ret;
}
......
......@@ -123,7 +123,7 @@ struct get_sampled_distance_histogram
s_hist.gather();
python::list ret;
ret.append(wrap_multi_array_owned<size_t,1>(hist.get_array()));
ret.append(wrap_multi_array_owned(hist.get_array()));
ret.append(wrap_vector_owned<val_type>(hist.get_bins()[0]));
phist = ret;
}
......
......@@ -132,7 +132,7 @@ struct get_histogram
bin_list = hist.get_bins();
python::object ret_bins = wrap_vector_owned(bin_list[0]);
_ret_bins = ret_bins;
_hist = wrap_multi_array_owned<size_t,1>(hist.get_array());
_hist = wrap_multi_array_owned(hist.get_array());
}
python::object& _hist;
const vector<long double>& _bins;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment