<p>I have been mulling over writing a peak-fitting library for a while. I know Python fairly well and plan on implementing everything in Python to begin with but envisage that I may have to re-implement some core routines in a compiled language eventually.</p>
<p>IIRC, one of Python's original remits was as a prototyping language, however Python is pretty liberal in allowing functions, functors, objects to be passed to functions and methods, whereas I suspect the same is not true of say C or Fortran.</p>
<p>What should I know about designing functions/classes which I envisage will have to interface into the compiled language? And how much of these potential problems are dealt with by libraries such as cTypes, bgen, <a href="http://www.swig.org/" rel="nofollow noreferrer">SWIG</a>, <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/index.html" rel="nofollow noreferrer">Boost.Python</a>, <a href="http://cython.org/" rel="nofollow noreferrer">Cython</a> or <a href="http://www.riverbankcomputing.co.uk/software/sip/intro" rel="nofollow noreferrer">Python SIP</a>?</p>
<p>For this particular use case (a fitting library), I imagine allowing users to define mathematical functions (Guassian, Lorentzian etc.) as Python functions which can then to be passed an interpreted by the compiled code fitting library. Passing and returning arrays is also essential.</p>

[{'answer_id': 16191, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 1, 'selected': False, 'text': '<p>In my experience, there are two easy ways to call into C code from Python code.  There are other approaches, all of which are more annoying and/or verbose.</p>\n\n<p>The first and easiest is to compile a bunch of C code as a separate shared library and then call functions in that library using ctypes.  Unfortunately, passing anything other than basic data types is non-trivial.</p>\n\n<p>The second easiest way is to write a Python module in C and then call functions in that module.  You can pass anything you want to these C functions without having to jump through any hoops.  And it\'s easy to call Python functions or methods from these C functions, as described here: <a href="https://docs.python.org/extending/extending.html#calling-python-functions-from-c" rel="nofollow noreferrer">https://docs.python.org/extending/extending.html#calling-python-functions-from-c</a></p>\n\n<p>I don\'t have enough experience with SWIG to offer intelligent commentary.  And while it is possible to do things like pass custom Python objects to C functions through ctypes, or to define new Python classes in C, these things are annoying and verbose and I recommend taking one of the two approaches described above.</p>\n'}, {'answer_id': 17300, 'author': 'Jason Pratt', 'author_id': 99, 'author_profile': 'https://Stackoverflow.com/users/99', 'pm_score': 3, 'selected': False, 'text': '<p>The best way to plan for an eventual transition to compiled code is to write the performance sensitive portions as a module of simple functions in a <a href="https://en.wikipedia.org/wiki/Functional_programming" rel="nofollow noreferrer">functional style</a> (stateless and without side effects), which accept and return basic data types.</p>\n<p>This will provide a one-to-one mapping from your Python prototype code to the eventual compiled code, and will let you use <a href="https://docs.python.org/3/library/ctypes.html" rel="nofollow noreferrer">ctypes</a> easily and avoid a whole bunch of headaches.</p>\n<p>For peak fitting, you\'ll almost certainly need to use arrays, which will complicate things a little, but is still very doable with ctypes.</p>\n<p>If you really want to use more complicated data structures, or modify the passed arguments, <a href="https://www.swig.org/" rel="nofollow noreferrer">SWIG</a> or <a href="https://docs.python.org/3/extending/" rel="nofollow noreferrer">Python\'s standard C-extension interface</a> will let you do what you want, but with some amount of hassle.</p>\n<p>For what you\'re doing, you may also want to check out <a href="https://numpy.org/" rel="nofollow noreferrer">NumPy</a>, which might do some of the work you would want to push to C, as well as offering <a href="https://web.archive.org/web/20090206165047/http://projects.scipy.org:80/scipy/numpy/wiki/NumPyCAPI" rel="nofollow noreferrer">some additional help in moving data back and forth between Python and C</a>.</p>\n'}, {'answer_id': 28467, 'author': 'Moe', 'author_id': 3051, 'author_profile': 'https://Stackoverflow.com/users/3051', 'pm_score': 3, 'selected': False, 'text': '<p>I haven\'t used SWIG or SIP, but I find writing Python wrappers with <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/index.html" rel="nofollow noreferrer">boost.python</a> to be very powerful and relatively easy to use.</p>\n\n<p>I\'m not clear on what your requirements are for passing types between C/C++ and python, but you can do that easily by either exposing a C++ type to python, or by using a generic <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/object.html" rel="nofollow noreferrer">boost::python::object</a> argument to your C++ API. You can also register converters to automatically convert python types to C++ types and vice versa.</p>\n\n<p>If you plan use boost.python, the <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/tutorial/doc/html/index.html" rel="nofollow noreferrer">tutorial</a> is a good place to start.</p>\n\n<p>I have implemented something somewhat similar to what you need. I have a C++ function that \naccepts a python function and an image as arguments, and applies the python function to each pixel in the image.</p>\n\n<pre><code>Image* unary(boost::python::object op, Image&amp; im)\n{\n    Image* out = new Image(im.width(), im.height(), im.channels());\n    for(unsigned int i=0; i&lt;im.size(); i++)\n    {\n        (*out)[i] == extract&lt;float&gt;(op(im[i]));\n    }\n    return out;\n}\n</code></pre>\n\n<p>In this case, Image is a C++ object exposed to python (an image with float pixels), and op is a python defined function  (or really any python object with a &#95;&#95;call&#95;&#95; attribute). You can then use this function as follows (assuming unary is located in the called image that also contains Image and a load function):</p>\n\n<pre><code>import image\nim = image.load(\'somefile.tiff\')\ndouble_im = image.unary(lambda x: 2.0*x, im)\n</code></pre>\n\n<p>As for using arrays with boost, I personally haven\'t done this, but I know the functionality to expose arrays to python using boost is available - <a href="http://www.boost.org/doc/libs/1_35_0/libs/python/doc/v2/faq.html#question2" rel="nofollow noreferrer">this</a> might be helpful.</p>\n'}, {'answer_id': 151006, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 2, 'selected': False, 'text': '<p><a href="https://web.archive.org/web/20140531071235/http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#the-quick-and-smart-way" rel="nofollow noreferrer">f2py</a> (part of <code>numpy</code>) is a simpler alternative to SWIG and boost.python for wrapping C/Fortran number-crunching code.</p>\n'}, {'answer_id': 151084, 'author': 'David Locke', 'author_id': 1447, 'author_profile': 'https://Stackoverflow.com/users/1447', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>Python is pretty liberal in allowing functions, functors, objects to be passed to functions and methods, whereas I suspect the same is not true of say C or Fortran.</p>\n</blockquote>\n\n<p>In C you cannot pass a function as an argument to a function but you can pass a function pointer which is just as good a function.</p>\n\n<p>I don't know how much that would help when you are trying to integrate C and Python code but I just wanted to clear up one misconception.</p>\n"}, {'answer_id': 872600, 'author': 'Roman Zeyde', 'author_id': 104044, 'author_profile': 'https://Stackoverflow.com/users/104044', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to the tools above, I can recommend using <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/" rel="nofollow noreferrer" title="Pyrex">Pyrex</a> \n(for creating Python extension modules) or <a href="http://psyco.sourceforge.net/" rel="nofollow noreferrer" title="Psyco">Psyco</a> (as JIT compiler for Python).</p>\n'}, {'answer_id': 1661276, 'author': 'SirVer', 'author_id': 200945, 'author_profile': 'https://Stackoverflow.com/users/200945', 'pm_score': 6, 'selected': True, 'text': '<p>Finally a question that I can really put a value answer to :). </p>\n\n<p>I have investigated f2py, boost.python, swig, cython and pyrex for my work (PhD in optical measurement techniques). I used swig extensively, boost.python some and pyrex and cython a lot. I also used ctypes. This is my breakdown:</p>\n\n<p><strong>Disclaimer</strong>: This is my personal experience. I am not involved with any of these projects. </p>\n\n<p><strong>swig:</strong>\ndoes not play well with c++. It should, but name mangling problems in the linking step was a major headache for me on linux &amp; Mac OS X. If you have C code and want it interfaced to python, it is a good solution. I wrapped the GTS for my needs and needed to write basically a C shared library which I could connect to. I would not recommend it.</p>\n\n<p><strong>Ctypes:</strong>\nI wrote a libdc1394 (IEEE Camera library) wrapper using ctypes and it was a very straigtforward experience. You can find the code on <a href="https://launchpad.net/pydc1394" rel="noreferrer">https://launchpad.net/pydc1394</a>. It is a lot of work to convert headers to python code, but then everything works reliably. This is a good way if you want to interface an external library. Ctypes is also in the stdlib of python, so everyone can use your code right away. This is also a good way to play around with a new lib in python quickly. I can recommend it to interface to external libs. </p>\n\n<p><strong>Boost.Python</strong>: Very enjoyable. If you already have C++ code of your own that you want to use in python, go for this. It is very easy to translate c++ class structures into python class structures this way. I recommend it if you have c++ code that you need in python. </p>\n\n<p><strong>Pyrex/Cython:</strong> Use Cython, not Pyrex. Period. Cython is more advanced and more enjoyable to use. Nowadays, I do everything with cython that i used to do with SWIG or Ctypes. It is also the best way if you have python code that runs too slow. The process is absolutely fantastic: you convert your python modules into cython modules, build them and keep profiling and optimizing like it still was python (no change of tools needed). You can then apply as much (or as little) C code mixed with your python code. This is by far faster then having to rewrite whole parts of your application in C; you only rewrite the inner loop. </p>\n\n<p><strong>Timings</strong>: ctypes has the highest call overhead (~700ns), followed by boost.python (322ns), then directly by swig (290ns). Cython has the lowest call overhead (124ns) and the best feedback where it spends time on (cProfile support!). The numbers are from my box calling a trivial function that returns an integer from an interactive shell; module import overhead is therefore not timed, only function call overhead is. It is therefore easiest and most productive to get python code fast by profiling and using cython.</p>\n\n<p><strong>Summary</strong>: For your problem, use Cython ;). I hope this rundown will be useful for some people. I\'ll gladly answer any remaining question.</p>\n\n<hr>\n\n<p><strong>Edit</strong>: I forget to mention: for numerical purposes (that is, connection to NumPy) use Cython; they have support for it (because they basically develop cython for this purpose). So this should be another +1 for your decision. </p>\n'}]
['https://Stackoverflow.com/questions/16067', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/199/']