/usr/include/dlib/python/numpy.h is in libdlib-dev 18.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Copyright (C) 2014 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_PYTHON_NuMPY_Hh_
#define DLIB_PYTHON_NuMPY_Hh_
#include <boost/python.hpp>
#include <dlib/error.h>
#include <dlib/algs.h>
#include <dlib/string.h>
// ----------------------------------------------------------------------------------------
template <typename T>
void validate_numpy_array_type (
boost::python::object& obj
)
{
using namespace boost::python;
const char ch = extract<char>(obj.attr("dtype").attr("char"));
if (dlib::is_same_type<T,double>::value && ch != 'd')
throw dlib::error("Expected numpy.ndarray of float64");
if (dlib::is_same_type<T,float>::value && ch != 'f')
throw dlib::error("Expected numpy.ndarray of float32");
if (dlib::is_same_type<T,dlib::int32>::value && ch != 'i')
throw dlib::error("Expected numpy.ndarray of int32");
if (dlib::is_same_type<T,unsigned char>::value && ch != 'B')
throw dlib::error("Expected numpy.ndarray of uint8");
}
// ----------------------------------------------------------------------------------------
template <typename T, int dims>
void get_numpy_ndarray_parts (
boost::python::object& obj,
T*& data,
long (&shape)[dims]
)
/*!
ensures
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
of the array into #shape.
!*/
{
Py_buffer pybuf;
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_ND | PyBUF_WRITABLE ))
throw dlib::error("Expected contiguous and writable numpy.ndarray.");
try
{
validate_numpy_array_type<T>(obj);
data = (T*)pybuf.buf;
if (pybuf.ndim > dims)
throw dlib::error("Expected array with " + dlib::cast_to_string(dims) + " dimensions.");
for (int i = 0; i < dims; ++i)
{
if (i < pybuf.ndim)
shape[i] = pybuf.shape[i];
else
shape[i] = 1;
}
}
catch(...)
{
PyBuffer_Release(&pybuf);
throw;
}
PyBuffer_Release(&pybuf);
}
// ----------------------------------------------------------------------------------------
template <typename T, int dims>
void get_numpy_ndarray_parts (
const boost::python::object& obj,
const T*& data,
long (&shape)[dims]
)
/*!
ensures
- extracts the pointer to the data from the given numpy ndarray. Stores the shape
of the array into #shape.
!*/
{
Py_buffer pybuf;
if (PyObject_GetBuffer(obj.ptr(), &pybuf, PyBUF_ND ))
throw dlib::error("Expected contiguous numpy.ndarray.");
try
{
validate_numpy_array_type<T>(obj);
data = (const T*)pybuf.buf;
if (pybuf.ndim > dims)
throw dlib::error("Expected array with " + dlib::cast_to_string(dims) + " dimensions.");
for (int i = 0; i < dims; ++i)
{
if (i < pybuf.ndim)
shape[i] = pybuf.shape[i];
else
shape[i] = 1;
}
}
catch(...)
{
PyBuffer_Release(&pybuf);
throw;
}
PyBuffer_Release(&pybuf);
}
// ----------------------------------------------------------------------------------------
#endif // DLIB_PYTHON_NuMPY_Hh_
|