Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pytreat-practicals-2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FSL
pytreat-practicals-2020
Commits
43609d94
Commit
43609d94
authored
7 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
Martin's python-cpp interfacing talk
parent
20a10d7b
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
talks/python_cpp/python_cpp.ipynb
+772
-0
772 additions, 0 deletions
talks/python_cpp/python_cpp.ipynb
with
772 additions
and
0 deletions
talks/python_cpp/python_cpp.ipynb
0 → 100644
+
772
−
0
View file @
43609d94
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"# Calling C++ code from Python\n",
"\n",
"## Problem\n",
"\n",
" - We have some existing C++ code which operates on array/image data\n",
" - We want to call it from Python\n",
" - We want to use Numpy arrays to pass input and receive output\n",
" - **Ideally, want to avoid too much copying of large data**\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution I will present\n",
"\n",
" - Build a **Cython** extension\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Alternative solutions I will mention briefly\n",
"\n",
" - Create a pure-C API and use `ctypes`\n",
" - Wrapper-generators (e.g. `swig`)\n",
" - Wrapping a command line tool\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sample C++ code\n",
" #include \"newimage/newimageall.h\"\n",
"\n",
" void process_volume(NEWIMAGE::volume4D<float> &invol)\n",
" {\n",
" // Do some clever stuff\n",
" invol.binarise(0.5);\n",
" }\n",
"\n",
" int main(int argc, char **argv)\n",
" {\n",
" char *input_file = argv[1];\n",
" char *output_file = argv[2];\n",
"\n",
" NEWIMAGE::volume4D<float> invol;\n",
" read_volume4D(invol, input_file);\n",
"\n",
" process_volume(invol);\n",
" save_volume4D(invol, output_file);\n",
" }\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## First provide an entry point using C++ native types\n",
"\n",
" #include <vector>\n",
" #include <iostream>\n",
" \n",
" std::vector<float> process_vectors(std::vector<float> &input, int nx, int ny, int nz, int nt)\n",
" {\n",
" // This is just so we can see if the data has been copied\n",
" std::cerr << \"In C++ the input vector starts at address \" << &input[0] << std::endl;\n",
" \n",
" // Here we ought to check that nx, ny, nz, nt is consistent with overall length of input\n",
" \n",
" // Create a volume4D using an existing data buffer\n",
" // when we do this, NEWIMAGE will not try to delete the data buffer\n",
" NEWIMAGE::volume4D<float> invol(nx, ny, nz, nt, &input[0]);\n",
"\n",
" // Do our processing step\n",
" process_volume(invol);\n",
" \n",
" // Input data has been modified, so return it directly\n",
" return input;\n",
" }\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Array ordering\n",
"\n",
"\n",
"\n",
"If `input` is a 4D image, it's pretty clear that the first element is the voxel with co-ordinates `(0, 0, 0, 0)`\n",
"\n",
"But what is the next element?\n",
"\n",
"Is it voxel `(1, 0, 0, 0)`?\n",
"\n",
"Or `(0, 0, 0, 1)`?\n",
"\n",
"If the *first* axis is the one which varies fastest, we are using **Column-Major** ordering\n",
"If the *last* axis is the one which varies fastest, we are using **Row-Major** ordering"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## So, which is the standard?\n",
"\n",
"| Row-major | Column-major | \n",
"| ------------ | --------- | \n",
"| C/C++ native arrays | Fortran | \n",
"| Python/Numpy default | Matlab |\n",
"| SAS | FSL NEWIMAGE | \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### Here, we will need to make sure our Numpy arrays are passed as 1-dimensional float arrays in Column-major order to match NEWIMAGE\n",
"\n",
" data.flatten(order='F').astype(np.float32)\n",
" \n",
" - `'F'` stands for 'Fortran order'\n",
" - A C++ `float` is *almost* guaranteed to be 32 bits\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"\n",
"# Cython extension\n",
"\n",
"## First, the Cython wrapper\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# my_analysis_wrapper.pyx\n",
"\n",
"import numpy as np\n",
"cimport numpy as np\n",
"\n",
"from libcpp.vector cimport vector\n",
"\n",
"cdef extern from \"my_analysis.h\":\n",
" vector[float] process_vector(vector[float] &, int, int, int, int)\n",
" \n",
"def process_using_vectors(data):\n",
" # Save the dimensions of the data because we're going to flatten it to 1D array\n",
" # Should be checking the dimensions at this point!\n",
" nx, ny, nz, nt = data.shape\n",
"\n",
" # Convert data to 1D in Column-major (Fortran) order\n",
" # This always copies the data\n",
" data = data.flatten(order='F').astype(np.float32)\n",
"\n",
" # This line is just so we can see if the data is being copied\n",
" print(\"In python the input data starts at %X\" % data.__array_interface__['data'][0])\n",
"\n",
" # Call the C++ code\n",
" output = process_vectors(data, nx, ny, nz, nt)\n",
"\n",
" # Output is a 1D array in Fortran order - turn it back into a multidimensional array\n",
" # This should not copy the data\n",
" output = np.reshape(output, [nx, ny, nz, nt], order='F')\n",
" print(\"In python the reshaped data starts at %X\" % output.__array_interface__['data'][0])\n",
" \n",
" return output\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" \n",
"## Next, build the extension\n",
"\n",
"This would normally go in `setup.py`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import numpy\n",
"\n",
"from setuptools import setup\n",
"from Cython.Build import cythonize\n",
"from setuptools.extension import Extension\n",
"\n",
"# My Cython extension\n",
"fsldir = os.environ[\"FSLDIR\"]\n",
"\n",
"ext = Extension(\"my_analysis_wrapper\",\n",
" sources=['my_analysis_wrapper.pyx',\n",
" 'my_analysis.cpp'],\n",
" language=\"c++\",\n",
" include_dirs=[\".\", numpy.get_include(), \n",
" os.path.join(fsldir, \"include\"), \n",
" os.path.join(fsldir, \"extras/include\"), \n",
" os.path.join(fsldir, \"extras/include/newmat\")], \n",
" libraries=['newimage', 'miscmaths', 'fslio', 'niftiio', 'newmat', 'znz', \"zlib\"],\n",
" library_dirs=[os.path.join(fsldir, \"lib\"), os.path.join(fsldir, \"extras/lib\")])\n",
"\n",
"# setup parameters\n",
"setup(name='my_app',\n",
" description='My Python application which calls C++',\n",
" ext_modules=cythonize(ext))"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"running build_ext\n",
"copying build\\lib.win-amd64-2.7\\my_analysis_wrapper.pyd -> \n"
]
}
],
"source": [
"%run setup.py build_ext"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAADKRJREFUeJzt3W1sXvV5x/HfL3Yc7DQkTbulysPq\nwChrSFeBPMaDxgR0E5SMrGonkYpqY9WiTQVS2q2ibBKa9mJvECqqKjqPPkhrVMbSjFUdg9LSFqFp\nVpyEKhiXNktYHoGUpCTQ0Djk2gt7UsqI72P8/3PsS9+PhBSbw8UlJ9+c2/d9fG5HhADkNKftBQDU\nQ+BAYgQOJEb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"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"In python the input data starts at A55A980\n",
"In C++ the input vector starts at address 000000000A87D5A0\n",
"\n",
"In python the reshaped data starts at A6B2040\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAACnZJREFUeJzt3duvnXMex/HPZ3ar1RpxGDfaZpBg\nphGG7DgmLlSCIdzMBQnJuOnNOEYizI1/QIQLkTQON4SLciEiauJwMTcdWwlqIw2GOkSHjAoZdfjM\nxd6TlNG9nnY/P89+vt6vRNK9PZZPlvX2rLX26lMnEYCafjX0AADtEDhQGIEDhRE4UBiBA4UROFAY\ngQOFEThQGIEDhS1rcaMHeUVWanXvt3vCyV/1fputvPXKqqEnlPZLfyz8R19qT772pOPc4qOqh/qI\nnOENvd/ulg9f7v02W7ng6D8MPaG0X/pjYWue0e58NjFwnqIDhRE4UBiBA4UROFAYgQOFEThQWKfA\nbV9o+03bO2zf0noUgH5MDNz2lKS7JV0kab2kK2yvbz0MwOJ1OYOfLmlHkreT7JH0iKTL2s4C0Icu\nga+R9P5eX++c/94P2N5oe8b2zDf6uq99ABahS+A/9XG4//t8a5JNSaaTTC/XisUvA7BoXQLfKWnd\nXl+vlfRhmzkA+tQl8BckHW/7WNsHSbpc0uNtZwHow8TfLprkW9vXSNoiaUrS/Um2N18GYNE6/X7w\nJE9KerLxFgA945NsQGEEDhRG4EBhBA4URuBAYU2uqjo2Y7pAYquLDY7pPpDGt3conMGBwggcKIzA\ngcIIHCiMwIHCCBwojMCBwggcKIzAgcIIHCiMwIHCCBwojMCBwggcKIzAgcIIHCiMwIHCCBwojMCB\nwggcKIzAgc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P21459KaFDBK4\n7SlJd0u6SNJ6SVfYXj/Elg6+lXRTkt9LOlPSX5bw1r1dL2l26BEd3CXpqSS/k3SKlvBm22skXSdp\nOslJkqYkXT7sqoUNdQY/XdKOJG8n2SPpEUmXDbRlQUk+SrJt/tdfaO4BuGbYVQuzvVbSxZLuHXrL\nQmwfKulcSfdJUpI9Sf497KqJlkk62PYySaskfTjwngUNFfgaSe/v9fVOLfFoJMn2MZJOlbR12CUT\n3SnpZknfDz1kguMk7ZL0wPzLiXttrx561L4k+UDS7ZLek/SRpM+TPD3sqoUNFfhP/cHlS/rtfNuH\nSHpU0g1Jdg+9Z19sXyLpkyQvDr2lg2WSTpN0T5JTJX0paSm/H3O45p5pHivpaEmrbV857KqFDRX4\nTknr9vp6rZbwUx3byzUX90NJHht6zwTnSLrU9ruae+lznu0Hh520Tzsl7Uzyv2dEmzUX/FJ1vqR3\nkuxK8o2kxySdPfCmBQ0V+AuSjrd9rO2DNPdGxeMDbVmQbWvuNeJskjuG3jNJkluTrE1yjObu12eT\nLMmzTJKPJb1v+8T5b22Q9PqAkyZ5T9KZtlfNPy42aAm/KSjNPUX62SX51vY1krZo7p3I+5NsH2JL\nB+dIukrSq7Zfnv/eX5M8OeCmSq6V9ND8/+jflnT1wHv2KclW25slbdPcT1de0hL/VBufZAMK45Ns\nQGEEDhRG4EBhBA4URuBAYQQOFEbgQGEEDhT2X771bWTJmQ2jAAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x9ab35f8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy\n",
"import my_analysis_wrapper\n",
"import matplotlib.pyplot as plt\n",
"\n",
"data = numpy.random.rand(10, 10, 10, 10)\n",
"\n",
"plt.imshow(data[:,:,5,5])\n",
"plt.show()\n",
"\n",
"output = my_analysis_wrapper.process_with_vectors(data)\n",
"plt.imshow(output[:,:,5,5])\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Great, it worked\n",
"\n",
"## But we did copy our data - several times\n",
"\n",
" - We copied the input once in Python to flatten it in the right order\n",
" - Cython copied it again, because the pointer to the memory in the `vector` is different from the pointer to the data in the Numpy array.\n",
" - Similarly, converting the output `vector` back to a Numpy array would involve a copy\n",
" \n",
"\n",
"## Do we care?\n",
"\n",
"It depends on:\n",
"\n",
" - Is the processing time per-voxel much greater than the data copying time?\n",
" - If so, copying will not add significant overhead\n",
" - Might the data be comparable in size to system memory?\n",
" - If so, copying may result in swapping and significant slowness\n",
" \n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution with less copying\n",
"\n",
"We can't use a `vector`, it needs to be free to manage its own memory, not use an existing fixed buffer.\n",
"\n",
"Instead pass a pure C array:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
" void process_array(float *input, int nx, int ny, int nz, int nt)\n",
" {\n",
" cerr << \"In C++ the input array starts at address \" << input << std::endl;\n",
"\n",
" NEWIMAGE::volume4D<float> invol(nx, ny, nz, nt, input);\n",
"\n",
" process_volume(invol);\n",
" \n",
" // Volume data buffer is modified directly, so provided it was not copied\n",
" // we should be able to see the output directly in Python \n",
" }\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Note that we cannot check the size of the `input` buffer! It had better be correct"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# my_analysis_wrapper.pyx\n",
"\n",
"import numpy as np\n",
"cimport numpy as np\n",
"\n",
"from libcpp.vector cimport vector\n",
"\n",
"cdef extern from \"my_analysis.h\":\n",
" void process_array(float *, int, int, int, int)\n",
"\n",
"def process_c(np.ndarray[np.float32_t, ndim=1] input,\n",
" nx, ny, nz, nt):\n",
" process_array(&input[0], nx, ny, nz, nt)\n",
"\n",
"def process_with_arrays(data):\n",
" # Save the dimension of the data because we're going to flatten it to 1D array\n",
" nx, ny, nz, nt = data.shape\n",
"\n",
" # Convert data to 1D in Column-major (Fortran) order\n",
" data = data.flatten(order='F').astype(np.float32)\n",
"\n",
" print(\"In python the data starts at %X\" % data.__array_interface__['data'][0])\n",
"\n",
" process_c(data, nx, ny, nz, nt)\n",
"\n",
" data = np.reshape(data, [nx, ny, nz, nt], order='F')\n",
" print(\"In python the reshaped data starts at %X\" % data.__array_interface__['data'][0])\n",
" \n",
" return data\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAADKRJREFUeJzt3W1sXvV5x/HfL3Yc7DQkTbulysPq\nwChrSFeBPMaDxgR0E5SMrGonkYpqY9WiTQVS2q2ibBKa9mJvECqqKjqPPkhrVMbSjFUdg9LSFqFp\nVpyEKhiXNktYHoGUpCTQ0Djk2gt7UsqI72P8/3PsS9+PhBSbw8UlJ9+c2/d9fG5HhADkNKftBQDU\nQ+BAYgQOJEbgQGIEDiRG4EBiBA4kRuBAYgQOJNZdY2jPnN7o7VpQfO6C839RfKYkdelU8Zkv7l9U\nfKYkvTavylh1v1rnisbXelxl7pIlR4rPPPDKwuIzJanr5fLn0RPHDuvk8Vc6fnGrBN7btUCXLv5I\n8blXPrCr+ExJWjDn1eIzN/71muIzJemllV1V5i5+ZqzK3JfePbfK3L/c8M/FZ9659Q+Kz5SkBU/0\nFZ/5kwfubnQcD9GBxAgcSIzAgcQIHEiMwIHECBxIrFHgtq+x/YztnbZvr70UgDI6Bm67S9IXJF0r\naZWkdbZX1V4MwPQ1OYNfLGlnROyKiBOS7pe0tu5aAEpoEvgySXtP+3jfxOd+ie31todtD584dbzU\nfgCmoUngb3S96/+7cDkiBiNiICIGeub0Tn8zANPWJPB9klac9vFySQfqrAO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+qo\nW1Kv7W5JfZIOtLzPpNoKfJmkvad9vE8zPBpJst0v6UJJ5d/wuazPSfqMpFNtL9LBOZIOSfrKxLcT\n99me3/ZSZxIR+yXdJWmPpIOSXoqIb7e71eTaCvyN3rh8Rj+db/ttkr4h6ZMRcbTtfc7E9hpJL0TE\n1rZ3aaBb0kWS7o2ICyW9ImkmPx/zdo0/0lwpaamk+bZvbHerybUV+D5JK077eLlm8EMd23M1HvfG\niNjc9j4dXC7petvPavxbn6tsf63dlc5on6R9EfF/j4g2aTz4meoDknZHxKGIGJO0WdJlLe80qbYC\n3yLpPNsrbfdo/ImKb7a0y6RsW+PfI45GxN1t79NJRHw2IpZHRL/Gv66PRcSMPMtExHOS9to+f+JT\nV0t6usWVOtkj6RLbfRN/Lq7WDH5SUBp/iPSWi4iTtm+W9IjGn4n8ckSMtLFLA5dL+pikHbafnPjc\nHRHxUIs7ZXKLpI0Tf9HvknRTy/ucUUQM2d4kaZvGX13Zrhl+VRtXsgGJcSUbkBiBA4kROJAYgQOJ\nETiQGIEDiRE4kBiBA4n9L/yuy9QpJgQiAAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0xa4908d0>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"In python the data starts at A870040\n",
"In C++ the input array starts at address 000000000A870040\n",
"\n",
"In python the reshaped data starts at A870040\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAACnZJREFUeJzt3duvnXMex/HPZ3ar1RpxGDfaZpBg\nphGG7DgmLlSCIdzMBQnJuOnNOEYizI1/QIQLkTQON4SLciEiauJwMTcdWwlqIw2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+lznu0Hh520Tzsl7Uzyv2dEmzUX/FJ1vqR3\nkuxK8o2kxySdPfCmBQ0V+AuSjrd9rO2DNPdGxeMDbVmQbWvuNeJskjuG3jNJkluTrE1yjObu12eT\nLMmzTJKPJb1v+8T5b22Q9PqAkyZ5T9KZtlfNPy42aAm/KSjNPUX62SX51vY1krZo7p3I+5NsH2JL\nB+dIukrSq7Zfnv/eX5M8OeCmSq6V9ND8/+jflnT1wHv2KclW25slbdPcT1de0hL/VBufZAMK45Ns\nQGEEDhRG4EBhBA4URuBAYQQOFEbgQGEEDhT2X771bWTJmQ2jAAAAAElFTkSuQmCC\n",
"text/plain": [
"<matplotlib.figure.Figure at 0xaa4b128>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy\n",
"import my_analysis_wrapper\n",
"\n",
"plt.imshow(data[:,:,5,5])\n",
"plt.show()\n",
"\n",
"output = my_analysis_wrapper.process_with_arrays(data)\n",
"plt.imshow(output[:,:,5,5])\n",
"plt.show()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - We copied our input data once when we flattened it into Fortran order\n",
" - C++ code operated directly on that buffer\n",
" - Output data was not copied when reshaped "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Summary\n",
"\n",
" - Easy-ish recipe for passing Numpy arrays to C++ either as a `std::vector` or as a `float *` array.\n",
" - Can construct `NEWIMAGE::volume<float>` or other complex containers from within C++\n",
" - Easy modification to instead use `double` array\n",
" - Can pass Python strings to `C++ std::string` and other C++ containers in a similar way\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alternatives (Briefly!)\n",
"\n",
"## Why?\n",
"\n",
"\n",
"\n",
"## Can we assume that our newly compiled Cython/C++ code will link correctly with `libnewimage.a`?\n",
"\n",
" - Often, yes, but in general, no\n",
" - It depends on the compiler used for each - ideally they need to match\n",
" - The compiler of your Cython extension is **fixed** by the version of Python you are using\n",
" - Might need to recompile your dependency libraries with this compiler\n",
" - If you can't do this (e.g. commercial binary) you may be **stuck**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Three common problem scenarios\n",
"\n",
" - On Mac, need to use the same C++ standard library (either `libc++` or `libstdc++`)\n",
" - On Python 2, C++ compiler will be very old (may not support all of C++11)\n",
" - On Windows, no two versions of VC++ are binary compatible\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Alternative approach where this is a problem\n",
"\n",
" - Make your code a shared library with a *Pure C* API\n",
" - Use `ctypes`\n",
" \n",
"## `ctypes`\n",
"\n",
" - Part of Python standard library\n",
" - Allows you to call library functions from 'C' shared library (not C++)\n",
" - **Pure 'C' libraries are (generally) binary compatible on a given platform**\n",
" - We have to load the library manually\n",
" - We have to tell Python about the input and return types\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pure 'C' API for our processing function\n",
"\n",
" // my_analysis_purec.h\n",
"\n",
" #ifdef __cplusplus\n",
" extern \"C\" {\n",
" #endif\n",
" \n",
" void process_array(float *input, int nx, int ny, int nz, int nt);\n",
" \n",
" #ifdef __cplusplus\n",
" }\n",
" #endif\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" - Note need to use `extern \"C\" { }` if we may want to include this header from C++\n",
" - **On Windows, additional code is required to make the shared library (DLL) link correctly!**\n",
" - Note that the implementation *can* use C++"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import ctypes import CDLL, c_int, c_char_p\n",
"import numpy as np\n",
"import numpy.ctypeslib\n",
"\n",
"def process_ctypes(data):\n",
" \n",
" clib = ctypes.cdll.LoadLibrary(\"libmy_analysis.so\")\n",
"\n",
" # This is the data type of a 1-D Numpy array\n",
" c_float_arr = numpy.ctypeslib.ndpointer(dtype=np.float32, ndim=1, flags='CONTIGUOUS')\n",
"\n",
" # This specifies the argument types for the 'process_array' function\n",
" # This is not actually required but enables ctypes to do some error checking\n",
" clib.process_array.argtypes = [c_float_arr, c_int, c_int, c_int, c_int]\n",
"\n",
" # Put the Numpy data into row-major order and make sure it is contiguous in memory\n",
" item = np.ascontiguousarray(item.flatten(order='F'), dtype=np.float32)\n",
" \n",
" clib.process_carray(data, shape[0], shape[1], shape[2], shape[3])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Comparison with Cython\n",
"\n",
"## Cython advantages\n",
"\n",
" - Python wrapper is probably a little quicker and cleaner to write\n",
" - Don't need to produce a new pure-C API provided we have an entry point using C++ types\n",
" - Potential for better error-checking\n",
" - Might integrate well if you are already using Cython\n",
" - No need to build a shared library\n",
" \n",
"## `ctypes` advantages\n",
"\n",
" - Part of the Python standard library\n",
" - No additional compile step in `setup.py`\n",
" - Binary compatibility - no need to be tied to a single (perhaps old) C++ compiler\n",
" \n",
"## Conclusion?\n",
"\n",
" - Use Cython when you can, `ctypes` if you have to\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Other alternatives (briefly for completeness)\n",
"\n",
"## Wrapper Generators (SWIG, shiboken, others)\n",
"\n",
" - Run a preprocessor on your C++ code to generate an 'automatic' Python wrapper\n",
" - Generally need to write an 'interface specifier' for each C++ header to describe how it interfaces to Python\n",
" - Great when you have a large, complex C++ API which needs to be consistently exposed to Python (e.g. wx/wxpython, QT/PyQT)\n",
" - SWIG can support other languages as well as Python\n",
" - Probably more work than Cython/ctypes if you have a single simple API\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Just Wrap the Command Line\n",
"\n",
" - Quick and dirty\n",
" - Copies all data to/from filesystem\n",
" - Need to go via command line API, create temp directories, etc\n",
" - Don't overlook as a way of getting started - can move to other solution later\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdH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9Kztc8fetUrSUy2u1Mku\nSRfanjP2cbFKU/hJQWn0IdKbLiJGbH9W0sMafSbyrojY3sYuDayUdJWkJ21vG3vflyPiwRZ3yuQ6\nSfeM/UO/U9LVLe9zUhGx2fZGSVs0+tWVrZrir2rjlWxAYrySDUiMwIHECBxIjMCBxAgcSIzAgcQI\nHEiMwIHEfgdwPctS4OQ+QAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0xb047588>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAPgAAAD8CAYAAABaQGkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAACn1JREFUeJzt3c+v3XMex/HXa25JlRFUN9pmSmLM\niIwhJx0/EguVYAibWZCQjE03gxKJMBv/gAgLkTR+bAiLshARNfFjMZtylRnqMhGM1o+4UxkVYkq9\nZnHvJGXae77t/X5873l7PhJJ73Ucr5zep++5555+OIkA1PSzoQcAaIfAgcIIHCiMwIHCCBwojMCB\nwggcKIzAgcIIHChsWYs7PfGEqaxbe0SLu27iH39f0ft9/vI3X/V+ny21eAykdo/DJP2etdj6tb7U\n3vzH427nFm9VHZ25PC9tXdv7/bZy8Um/7f0+t370Wu/32VKLx0Bq9zhM0u9Zi63b8pz25LOxgfMU\nHSiMwIHCCBwojMCBwggcKIzAgcI6BW77Ettv237H9m2tRwHox9jAbU9JulfSpZJOl3S17dNbDwOw\neF2u4OslvZPk3SR7JT0m6cq2swD0oUvgqyXt3O/jXfOf+x7bG21P256e3b2vr30AFqFL4Ad6O9z/\nvb81yeYkoySjVSunFr8MwKJ1CXyXpP3fWL5G0kdt5gDoU5fAX5Z0qu2TbR8p6SpJT7adBaAPY/+4\naJJvbV8vaaukKUkPJtn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w04RJO0\nd5K2SpO1d5K28hQdKIzAgcKGDnzzwP/+QzVJeydpqzRZeydm66DfgwNoa+grOICGBgvc9iW237b9\nju3bhtoxju21tl+wPWN7h+1NQ2/qwvaU7VdtPzX0loXYPs72FttvzT/G5w69aSG2b57/OnjD9qO2\nlw+9aSGDBG57StK9ki6VdLqkq22fPsSWDr6VdEuSX0s6R9KflvDW/W2SNDP0iA7ukfRMkl9JOlNL\neLPt1ZJulDRKcoakKUlXDbtqYUNdwddLeifJu0n2SnpM0pUDbVlQko+TbJ//9Rea+wJcPeyqhdle\nI+kySfcPvWUhto+VdIGkByQpyd4k/x521VjLJB1le5mkFZI+GnjPgoYKfLWknft9vEtLPBpJsr1O\n0lmStg27ZKy7Jd0q6buhh4xxiqRZSQ/Nfztxv+2jhx51MEk+lHSnpA8kfSzp8yTPDrtqYUMF7gN8\nbkm/nG/7GEmPS7opyZ6h9xyM7cslfZrklaG3dLBM0tmS7ktylqQvJS3l12OO19wzzZMlnSTpaNvX\nDLtqYUMFvkvS2v0+XqMl/FTH9hGai/uRJE8MvWeM8yVdYft9zX3rc6Hth4eddFC7JO1K8r9nRFs0\nF/xSdZGk95LMJvlG0hOSzht404KGCvxlSafaPtn2kZp7oeLJgbYsyLY19z3iTJK7ht4zTpLbk6xJ\nsk5zj+vzSZbkVSbJJ5J22j5t/lMbJL054KRxPpB0ju0V818XG7SEXxSU5p4i/eiSfGv7eklbNfdK\n5INJdgyxpYPzJV0r6XXbr81/7s9Jnh5wUyU3SHpk/j/070q6buA9B5Vkm+0tkrZr7qcrr2qJv6uN\nd7IBhfFONqAwAgcKI3CgMAIHCiNwoDACBwojcKAwAgcK+y+Qlolk5IOqTAAAAABJRU5ErkJggg==\n",
"text/plain": [
"<matplotlib.figure.Figure at 0xb0de518>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import os\n",
"import tempfile\n",
"import subprocess\n",
"import tempfile\n",
"import shutil\n",
"\n",
"import numpy as np\n",
"import nibabel as nib\n",
"os.environ[\"FSLOUTPUTTYPE\"] = \"NIFTI_GZ\"\n",
"\n",
"def binarise(data):\n",
" # Remember the directory where we started\n",
" cwd_orig = os.getcwd()\n",
" try:\n",
" # Create a temporary directory\n",
" tempdir = tempfile.mkdtemp(\"fsl\")\n",
" \n",
" # Save input data in temp directory\n",
" os.chdir(tempdir)\n",
" tmpin = nib.Nifti1Image(data, np.identity(4))\n",
" tmpin.to_filename(\"in.nii.gz\")\n",
" \n",
" # Run a command from $FSLDIR\n",
" fslmaths = os.path.join(os.environ[\"FSLDIR\"], \"bin\", \"fslmaths\")\n",
" \n",
" # We could use os.system here if we don't care about returning the stdout/stderr\n",
" p = subprocess.Popen([fslmaths, \"in.nii.gz\", \"-thr\", \"0.5\", \"-bin\", \"out.nii.gz\"], \n",
" stdout=subprocess.PIPE, stderr=subprocess.STDOUT)\n",
" cmd_stdout = \"\"\n",
" while 1:\n",
" retcode = p.poll()\n",
" cmd_stdout += p.stdout.readline()\n",
" if retcode is not None: break\n",
" if retcode != 0:\n",
" raise RuntimeError(\"Error: %s\" % cmd_stdout)\n",
" \n",
" # Load the output file and return it with the command standard output\n",
" out_nii = nib.load(\"out.nii.gz\")\n",
" return out_nii.get_data(), cmd_stdout\n",
" finally:\n",
" # Change back to our starting directory\n",
" os.chdir(cwd_orig)\n",
"\n",
"data = np.random.rand(10, 10, 10, 10)\n",
"plt.imshow(data[:,:,5,5])\n",
"plt.show()\n",
"\n",
"output, stdout = binarise(data)\n",
"plt.imshow(output[:,:,5,5])\n",
"plt.show()\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Summary\n",
"\n",
"## What we've done\n",
"\n",
" - It's not that hard to call existing C++ code from Python\n",
" - Need to be a bit careful with Numpy arrays\n",
" - Cython is probably the easiest method\n",
" - Data copying can be minimised by passing data as C arrays (`float *` etc)\n",
" - `ctypes` may be a good alternative if you have binary compatibility issues\n",
" - Can always wrap a command line tool as a way of getting started!\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:

# Calling C++ code from Python
## Problem
-
We have some existing C++ code which operates on array/image data
-
We want to call it from Python
-
We want to use Numpy arrays to pass input and receive output
-
**Ideally, want to avoid too much copying of large data**
%% Cell type:markdown id: tags:
## Solution I will present
-
Build a
**Cython**
extension
%% Cell type:markdown id: tags:
## Alternative solutions I will mention briefly
-
Create a pure-C API and use
`ctypes`
-
Wrapper-generators (e.g.
`swig`
)
-
Wrapping a command line tool
%% Cell type:markdown id: tags:
# Sample C++ code
#include "newimage/newimageall.h"
void process_volume(NEWIMAGE::volume4D<float> &invol)
{
// Do some clever stuff
invol.binarise(0.5);
}
int main(int argc, char **argv)
{
char *input_file = argv[1];
char *output_file = argv[2];
NEWIMAGE::volume4D<float> invol;
read_volume4D(invol, input_file);
process_volume(invol);
save_volume4D(invol, output_file);
}
%% Cell type:markdown id: tags:
## First provide an entry point using C++ native types
#include <vector>
#include <iostream>
std::vector<float> process_vectors(std::vector<float> &input, int nx, int ny, int nz, int nt)
{
// This is just so we can see if the data has been copied
std::cerr << "In C++ the input vector starts at address " << &input[0] << std::endl;
// Here we ought to check that nx, ny, nz, nt is consistent with overall length of input
// Create a volume4D using an existing data buffer
// when we do this, NEWIMAGE will not try to delete the data buffer
NEWIMAGE::volume4D<float> invol(nx, ny, nz, nt, &input[0]);
// Do our processing step
process_volume(invol);
// Input data has been modified, so return it directly
return input;
}
%% Cell type:markdown id: tags:
# Array ordering

If
`input`
is a 4D image, it's pretty clear that the first element is the voxel with co-ordinates
`(0, 0, 0, 0)`
But what is the next element?
Is it voxel
`(1, 0, 0, 0)`
?
Or
`(0, 0, 0, 1)`
?
If the
*first*
axis is the one which varies fastest, we are using
**Column-Major**
ordering
If the
*last*
axis is the one which varies fastest, we are using
**Row-Major**
ordering
%% Cell type:markdown id: tags:
## So, which is the standard?
| Row-major | Column-major |
| ------------ | --------- |
| C/C++ native arrays | Fortran |
| Python/Numpy default | Matlab |
| SAS | FSL NEWIMAGE |
%% Cell type:markdown id: tags:
### Here, we will need to make sure our Numpy arrays are passed as 1-dimensional float arrays in Column-major order to match NEWIMAGE
data.flatten(order='F').astype(np.float32)
-
`'F'`
stands for 'Fortran order'
-
A C++
`float`
is
*almost*
guaranteed to be 32 bits
%% Cell type:markdown id: tags:

# Cython extension
## First, the Cython wrapper
%% Cell type:code id: tags:
```
python
# my_analysis_wrapper.pyx
import
numpy
as
np
cimport
numpy
as
np
from
libcpp
.
vector
cimport
vector
cdef
extern
from
"
my_analysis.h
"
:
vector
[
float
]
process_vector
(
vector
[
float
]
&
,
int
,
int
,
int
,
int
)
def
process_using_vectors
(
data
):
# Save the dimensions of the data because we're going to flatten it to 1D array
# Should be checking the dimensions at this point!
nx
,
ny
,
nz
,
nt
=
data
.
shape
# Convert data to 1D in Column-major (Fortran) order
# This always copies the data
data
=
data
.
flatten
(
order
=
'
F
'
).
astype
(
np
.
float32
)
# This line is just so we can see if the data is being copied
print
(
"
In python the input data starts at %X
"
%
data
.
__array_interface__
[
'
data
'
][
0
])
# Call the C++ code
output
=
process_vectors
(
data
,
nx
,
ny
,
nz
,
nt
)
# Output is a 1D array in Fortran order - turn it back into a multidimensional array
# This should not copy the data
output
=
np
.
reshape
(
output
,
[
nx
,
ny
,
nz
,
nt
],
order
=
'
F
'
)
print
(
"
In python the reshaped data starts at %X
"
%
output
.
__array_interface__
[
'
data
'
][
0
])
return
output
```
%% Cell type:markdown id: tags:
## Next, build the extension
This would normally go in
`setup.py`
%% Cell type:code id: tags:
```
python
import
os
import
sys
import
numpy
from
setuptools
import
setup
from
Cython.Build
import
cythonize
from
setuptools.extension
import
Extension
# My Cython extension
fsldir
=
os
.
environ
[
"
FSLDIR
"
]
ext
=
Extension
(
"
my_analysis_wrapper
"
,
sources
=
[
'
my_analysis_wrapper.pyx
'
,
'
my_analysis.cpp
'
],
language
=
"
c++
"
,
include_dirs
=
[
"
.
"
,
numpy
.
get_include
(),
os
.
path
.
join
(
fsldir
,
"
include
"
),
os
.
path
.
join
(
fsldir
,
"
extras/include
"
),
os
.
path
.
join
(
fsldir
,
"
extras/include/newmat
"
)],
libraries
=
[
'
newimage
'
,
'
miscmaths
'
,
'
fslio
'
,
'
niftiio
'
,
'
newmat
'
,
'
znz
'
,
"
zlib
"
],
library_dirs
=
[
os
.
path
.
join
(
fsldir
,
"
lib
"
),
os
.
path
.
join
(
fsldir
,
"
extras/lib
"
)])
# setup parameters
setup
(
name
=
'
my_app
'
,
description
=
'
My Python application which calls C++
'
,
ext_modules
=
cythonize
(
ext
))
```
%% Cell type:code id: tags:
```
python
%
run
setup
.
py
build_ext
```
%% Output
running build_ext
copying build\lib.win-amd64-2.7\my_analysis_wrapper.pyd ->
%% Cell type:code id: tags:
```
python
import
numpy
import
my_analysis_wrapper
import
matplotlib.pyplot
as
plt
data
=
numpy
.
random
.
rand
(
10
,
10
,
10
,
10
)
plt
.
imshow
(
data
[:,:,
5
,
5
])
plt
.
show
()
output
=
my_analysis_wrapper
.
process_with_vectors
(
data
)
plt
.
imshow
(
output
[:,:,
5
,
5
])
plt
.
show
()
```
%% Output
In python the input data starts at A55A980
In C++ the input vector starts at address 000000000A87D5A0
In python the reshaped data starts at A6B2040
%% Cell type:markdown id: tags:
# Great, it worked
## But we did copy our data - several times
-
We copied the input once in Python to flatten it in the right order
-
Cython copied it again, because the pointer to the memory in the
`vector`
is different from the pointer to the data in the Numpy array.
-
Similarly, converting the output
`vector`
back to a Numpy array would involve a copy
## Do we care?
It depends on:
-
Is the processing time per-voxel much greater than the data copying time?
-
If so, copying will not add significant overhead
-
Might the data be comparable in size to system memory?
-
If so, copying may result in swapping and significant slowness
%% Cell type:markdown id: tags:
# Solution with less copying
We can't use a
`vector`
, it needs to be free to manage its own memory, not use an existing fixed buffer.
Instead pass a pure C array:
%% Cell type:markdown id: tags:
void process_array(float *input, int nx, int ny, int nz, int nt)
{
cerr << "In C++ the input array starts at address " << input << std::endl;
NEWIMAGE::volume4D<float> invol(nx, ny, nz, nt, input);
process_volume(invol);
// Volume data buffer is modified directly, so provided it was not copied
// we should be able to see the output directly in Python
}
%% Cell type:markdown id: tags:
-
Note that we cannot check the size of the
`input`
buffer! It had better be correct
%% Cell type:code id: tags:
```
python
# my_analysis_wrapper.pyx
import
numpy
as
np
cimport
numpy
as
np
from
libcpp
.
vector
cimport
vector
cdef
extern
from
"
my_analysis.h
"
:
void
process_array
(
float
*
,
int
,
int
,
int
,
int
)
def
process_c
(
np
.
ndarray
[
np
.
float32_t
,
ndim
=
1
]
input
,
nx
,
ny
,
nz
,
nt
):
process_array
(
&
input
[
0
],
nx
,
ny
,
nz
,
nt
)
def
process_with_arrays
(
data
):
# Save the dimension of the data because we're going to flatten it to 1D array
nx
,
ny
,
nz
,
nt
=
data
.
shape
# Convert data to 1D in Column-major (Fortran) order
data
=
data
.
flatten
(
order
=
'
F
'
).
astype
(
np
.
float32
)
print
(
"
In python the data starts at %X
"
%
data
.
__array_interface__
[
'
data
'
][
0
])
process_c
(
data
,
nx
,
ny
,
nz
,
nt
)
data
=
np
.
reshape
(
data
,
[
nx
,
ny
,
nz
,
nt
],
order
=
'
F
'
)
print
(
"
In python the reshaped data starts at %X
"
%
data
.
__array_interface__
[
'
data
'
][
0
])
return
data
```
%% Cell type:code id: tags:
```
python
import
numpy
import
my_analysis_wrapper
plt
.
imshow
(
data
[:,:,
5
,
5
])
plt
.
show
()
output
=
my_analysis_wrapper
.
process_with_arrays
(
data
)
plt
.
imshow
(
output
[:,:,
5
,
5
])
plt
.
show
()
```
%% Output
In python the data starts at A870040
In C++ the input array starts at address 000000000A870040
In python the reshaped data starts at A870040
%% Cell type:markdown id: tags:
-
We copied our input data once when we flattened it into Fortran order
-
C++ code operated directly on that buffer
-
Output data was not copied when reshaped
%% Cell type:markdown id: tags:
# Summary
-
Easy-ish recipe for passing Numpy arrays to C++ either as a
`std::vector`
or as a
`float *`
array.
-
Can construct
`NEWIMAGE::volume<float>`
or other complex containers from within C++
-
Easy modification to instead use
`double`
array
-
Can pass Python strings to
`C++ std::string`
and other C++ containers in a similar way
%% Cell type:markdown id: tags:
# Alternatives (Briefly!)
## Why?

## Can we assume that our newly compiled Cython/C++ code will link correctly with `libnewimage.a`?
-
Often, yes, but in general, no
-
It depends on the compiler used for each - ideally they need to match
-
The compiler of your Cython extension is
**fixed**
by the version of Python you are using
-
Might need to recompile your dependency libraries with this compiler
-
If you can't do this (e.g. commercial binary) you may be
**stuck**
%% Cell type:markdown id: tags:
## Three common problem scenarios
-
On Mac, need to use the same C++ standard library (either
`libc++`
or
`libstdc++`
)
-
On Python 2, C++ compiler will be very old (may not support all of C++11)
-
On Windows, no two versions of VC++ are binary compatible
%% Cell type:markdown id: tags:
# Alternative approach where this is a problem
-
Make your code a shared library with a
*Pure C*
API
-
Use
`ctypes`
## `ctypes`
-
Part of Python standard library
-
Allows you to call library functions from 'C' shared library (not C++)
-
**Pure 'C' libraries are (generally) binary compatible on a given platform**
-
We have to load the library manually
-
We have to tell Python about the input and return types
%% Cell type:markdown id: tags:
# Pure 'C' API for our processing function
// my_analysis_purec.h
#ifdef __cplusplus
extern "C" {
#endif
void process_array(float *input, int nx, int ny, int nz, int nt);
#ifdef __cplusplus
}
#endif
%% Cell type:markdown id: tags:
-
Note need to use
`extern "C" { }`
if we may want to include this header from C++
-
**On Windows, additional code is required to make the shared library (DLL) link correctly!**
-
Note that the implementation
*can*
use C++
%% Cell type:code id: tags:
```
python
import
ctypes
import
CDLL
,
c_int
,
c_char_p
import
numpy
as
np
import
numpy.ctypeslib
def
process_ctypes
(
data
):
clib
=
ctypes
.
cdll
.
LoadLibrary
(
"
libmy_analysis.so
"
)
# This is the data type of a 1-D Numpy array
c_float_arr
=
numpy
.
ctypeslib
.
ndpointer
(
dtype
=
np
.
float32
,
ndim
=
1
,
flags
=
'
CONTIGUOUS
'
)
# This specifies the argument types for the 'process_array' function
# This is not actually required but enables ctypes to do some error checking
clib
.
process_array
.
argtypes
=
[
c_float_arr
,
c_int
,
c_int
,
c_int
,
c_int
]
# Put the Numpy data into row-major order and make sure it is contiguous in memory
item
=
np
.
ascontiguousarray
(
item
.
flatten
(
order
=
'
F
'
),
dtype
=
np
.
float32
)
clib
.
process_carray
(
data
,
shape
[
0
],
shape
[
1
],
shape
[
2
],
shape
[
3
])
```
%% Cell type:markdown id: tags:
# Comparison with Cython
## Cython advantages
-
Python wrapper is probably a little quicker and cleaner to write
-
Don't need to produce a new pure-C API provided we have an entry point using C++ types
-
Potential for better error-checking
-
Might integrate well if you are already using Cython
-
No need to build a shared library
## `ctypes` advantages
-
Part of the Python standard library
-
No additional compile step in
`setup.py`
-
Binary compatibility - no need to be tied to a single (perhaps old) C++ compiler
## Conclusion?
-
Use Cython when you can,
`ctypes`
if you have to
%% Cell type:markdown id: tags:
# Other alternatives (briefly for completeness)
## Wrapper Generators (SWIG, shiboken, others)
-
Run a preprocessor on your C++ code to generate an 'automatic' Python wrapper
-
Generally need to write an 'interface specifier' for each C++ header to describe how it interfaces to Python
-
Great when you have a large, complex C++ API which needs to be consistently exposed to Python (e.g. wx/wxpython, QT/PyQT)
-
SWIG can support other languages as well as Python
-
Probably more work than Cython/ctypes if you have a single simple API
%% Cell type:markdown id: tags:
## Just Wrap the Command Line
-
Quick and dirty
-
Copies all data to/from filesystem
-
Need to go via command line API, create temp directories, etc
-
Don't overlook as a way of getting started - can move to other solution later
%% Cell type:code id: tags:
```
python
import
os
import
tempfile
import
subprocess
import
tempfile
import
shutil
import
numpy
as
np
import
nibabel
as
nib
os
.
environ
[
"
FSLOUTPUTTYPE
"
]
=
"
NIFTI_GZ
"
def
binarise
(
data
):
# Remember the directory where we started
cwd_orig
=
os
.
getcwd
()
try
:
# Create a temporary directory
tempdir
=
tempfile
.
mkdtemp
(
"
fsl
"
)
# Save input data in temp directory
os
.
chdir
(
tempdir
)
tmpin
=
nib
.
Nifti1Image
(
data
,
np
.
identity
(
4
))
tmpin
.
to_filename
(
"
in.nii.gz
"
)
# Run a command from $FSLDIR
fslmaths
=
os
.
path
.
join
(
os
.
environ
[
"
FSLDIR
"
],
"
bin
"
,
"
fslmaths
"
)
# We could use os.system here if we don't care about returning the stdout/stderr
p
=
subprocess
.
Popen
([
fslmaths
,
"
in.nii.gz
"
,
"
-thr
"
,
"
0.5
"
,
"
-bin
"
,
"
out.nii.gz
"
],
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
cmd_stdout
=
""
while
1
:
retcode
=
p
.
poll
()
cmd_stdout
+=
p
.
stdout
.
readline
()
if
retcode
is
not
None
:
break
if
retcode
!=
0
:
raise
RuntimeError
(
"
Error: %s
"
%
cmd_stdout
)
# Load the output file and return it with the command standard output
out_nii
=
nib
.
load
(
"
out.nii.gz
"
)
return
out_nii
.
get_data
(),
cmd_stdout
finally
:
# Change back to our starting directory
os
.
chdir
(
cwd_orig
)
data
=
np
.
random
.
rand
(
10
,
10
,
10
,
10
)
plt
.
imshow
(
data
[:,:,
5
,
5
])
plt
.
show
()
output
,
stdout
=
binarise
(
data
)
plt
.
imshow
(
output
[:,:,
5
,
5
])
plt
.
show
()
```
%% Output
%% Cell type:markdown id: tags:
# Summary
## What we've done
-
It's not that hard to call existing C++ code from Python
-
Need to be a bit careful with Numpy arrays
-
Cython is probably the easiest method
-
Data copying can be minimised by passing data as C arrays (
`float *`
etc)
-
`ctypes`
may be a good alternative if you have binary compatibility issues
-
Can always wrap a command line tool as a way of getting started!
%% Cell type:code id: tags:
```
python
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment