summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdoardo Pasca <edo.paskino@gmail.com>2017-08-23 12:56:09 +0100
committerEdoardo Pasca <edo.paskino@gmail.com>2017-08-23 12:56:09 +0100
commit5ed47a3fc9839b1803731fe5f422d43689f66763 (patch)
tree89e34b9bebd88505e2a5d25209a0ef45d8f3e110 /src
parent97e0c63f883f62ed0cc84c969756517fe4bedfe8 (diff)
downloadregularization-5ed47a3fc9839b1803731fe5f422d43689f66763.tar.gz
regularization-5ed47a3fc9839b1803731fe5f422d43689f66763.tar.bz2
regularization-5ed47a3fc9839b1803731fe5f422d43689f66763.tar.xz
regularization-5ed47a3fc9839b1803731fe5f422d43689f66763.zip
Test module for Boost Python
currently can pass a function to the C++ layer to be evaluated.
Diffstat (limited to 'src')
-rw-r--r--src/Python/Matlab2Python_utils.cpp68
-rw-r--r--src/Python/setup_test.py6
-rw-r--r--src/Python/test.py34
3 files changed, 99 insertions, 9 deletions
diff --git a/src/Python/Matlab2Python_utils.cpp b/src/Python/Matlab2Python_utils.cpp
index 6aaad90..e15d738 100644
--- a/src/Python/Matlab2Python_utils.cpp
+++ b/src/Python/Matlab2Python_utils.cpp
@@ -188,6 +188,71 @@ bp::list mexFunction( np::ndarray input ) {
return result;
}
+bp::list doSomething(np::ndarray input, PyObject *pyobj , PyObject *pyobj2) {
+
+ boost::python::object output(boost::python::handle<>(boost::python::borrowed(pyobj)));
+ int isOutput = !(output == boost::python::api::object());
+
+ boost::python::object calculate(boost::python::handle<>(boost::python::borrowed(pyobj2)));
+ int isCalculate = !(calculate == boost::python::api::object());
+
+ int number_of_dims = input.get_nd();
+ int dim_array[3];
+
+ dim_array[0] = input.shape(0);
+ dim_array[1] = input.shape(1);
+ if (number_of_dims == 2) {
+ dim_array[2] = -1;
+ }
+ else {
+ dim_array[2] = input.shape(2);
+ }
+
+ /**************************************************************************/
+ np::ndarray zz = zeros(3, dim_array, (int)0);
+ np::ndarray fzz = zeros(3, dim_array, (float)0);
+ /**************************************************************************/
+
+ int * A = reinterpret_cast<int *>(input.get_data());
+ int * B = reinterpret_cast<int *>(zz.get_data());
+ float * C = reinterpret_cast<float *>(fzz.get_data());
+
+ //Copy data and cast
+ for (int i = 0; i < dim_array[0]; i++) {
+ for (int j = 0; j < dim_array[1]; j++) {
+ for (int k = 0; k < dim_array[2]; k++) {
+ int index = k + dim_array[2] * j + dim_array[2] * dim_array[1] * i;
+ int val = (*(A + index));
+ float fval = sqrt((float)val);
+ std::memcpy(B + index, &val, sizeof(int));
+ std::memcpy(C + index, &fval, sizeof(float));
+ // if the PyObj is not None evaluate the function
+ if (isOutput)
+ output(fval);
+ if (isCalculate) {
+ float nfval = (float)bp::extract<float>(calculate(val));
+ if (isOutput)
+ output(nfval);
+ std::memcpy(C + index, &nfval, sizeof(float));
+ }
+ }
+ }
+ }
+
+
+ bp::list result;
+
+ result.append<int>(number_of_dims);
+ result.append<int>(dim_array[0]);
+ result.append<int>(dim_array[1]);
+ result.append<int>(dim_array[2]);
+ result.append<np::ndarray>(zz);
+ result.append<np::ndarray>(fzz);
+
+ //result.append<bp::tuple>(tup);
+ return result;
+
+}
BOOST_PYTHON_MODULE(prova)
@@ -196,7 +261,7 @@ BOOST_PYTHON_MODULE(prova)
//To specify that this module is a package
bp::object package = bp::scope();
- package.attr("__path__") = "fista";
+ package.attr("__path__") = "prova";
np::dtype dt1 = np::dtype::get_builtin<uint8_t>();
np::dtype dt2 = np::dtype::get_builtin<uint16_t>();
@@ -207,4 +272,5 @@ BOOST_PYTHON_MODULE(prova)
//numpy_boost_python_register_type<float, 3>();
//numpy_boost_python_register_type<double, 3>();
def("mexFunction", mexFunction);
+ def("doSomething", doSomething);
} \ No newline at end of file
diff --git a/src/Python/setup_test.py b/src/Python/setup_test.py
index ffb9c02..7c86175 100644
--- a/src/Python/setup_test.py
+++ b/src/Python/setup_test.py
@@ -30,13 +30,13 @@ extra_compile_args = ['-fopenmp','-O2', '-funsigned-char', '-Wall', '-std=c++0x'
extra_libraries = []
if platform.system() == 'Windows':
extra_compile_args[0:] = ['/DWIN32','/EHsc','/DBOOST_ALL_NO_LIB']
- extra_include_dirs += ["..\\ContourTree\\", "..\\win32\\" , "..\\Core\\","."]
+ #extra_include_dirs += ["..\\ContourTree\\", "..\\win32\\" , "..\\Core\\","."]
if sys.version_info.major == 3 :
extra_libraries += ['boost_python3-vc140-mt-1_64', 'boost_numpy3-vc140-mt-1_64']
else:
extra_libraries += ['boost_python-vc90-mt-1_64', 'boost_numpy-vc90-mt-1_64']
else:
- extra_include_dirs += ["../ContourTree/", "../Core/","."]
+ #extra_include_dirs += ["../ContourTree/", "../Core/","."]
if sys.version_info.major == 3:
extra_libraries += ['boost_python3', 'boost_numpy3','gomp']
else:
@@ -47,7 +47,7 @@ setup(
description='CCPi Core Imaging Library - FISTA Reconstruction Module',
version=cil_version,
cmdclass = {'build_ext': build_ext},
- ext_modules = [Extension("fista",
+ ext_modules = [Extension("prova",
sources=[ "Matlab2Python_utils.cpp",
],
include_dirs=extra_include_dirs, library_dirs=extra_library_dirs, extra_compile_args=extra_compile_args, libraries=extra_libraries ),
diff --git a/src/Python/test.py b/src/Python/test.py
index e283f89..db47380 100644
--- a/src/Python/test.py
+++ b/src/Python/test.py
@@ -5,14 +5,38 @@ Created on Thu Aug 3 14:08:09 2017
@author: ofn77899
"""
-import fista
+import prova
import numpy as np
-a = np.asarray([i for i in range(3*4*5)])
-a = a.reshape([3,4,5])
+a = np.asarray([i for i in range(1*2*3)])
+a = a.reshape([1,2,3])
print (a)
-b = fista.mexFunction(a)
+b = prova.mexFunction(a)
#print (b)
print (b[4].shape)
print (b[4])
-print (b[5]) \ No newline at end of file
+print (b[5])
+
+def print_element(input):
+ print ("f: {0}".format(input))
+
+prova.doSomething(a, print_element, None)
+
+c = []
+def append_to_list(input, shouldPrint=False):
+ c.append(input)
+ if shouldPrint:
+ print ("{0} appended to list {1}".format(input, c))
+
+def element_wise_algebra(input, shouldPrint=True):
+ ret = input - 7
+ if shouldPrint:
+ print ("element_wise {0}".format(ret))
+ return ret
+
+prova.doSomething(a, append_to_list, None)
+#print ("this is c: {0}".format(c))
+
+b = prova.doSomething(a, None, element_wise_algebra)
+#print (a)
+print (b[5])