Plasmapy: Cython integration

Created on 2 Jan 2018  路  14Comments  路  Source: PlasmaPy/PlasmaPy

Astropy's template has a way to integrate Cython and C extensions. We should get this working so we can have super optimized code for simulations!

high Optimization Refactoring

All 14 comments

Relates to spectroscoPy issue

I will take a crack at this as I need to learn Cython for my own simulations anyway.

I've been meaning to learn Cython as well. If you find any neat materials, it would be awesome if you could share them (perhaps here)! :smiley:

This stackoverflow response has some good links (and reference to His Noodliness). Otherwise I am reading Cython by Kurt W. Smith.

For distribution, Cython recommends to distribute the .c files generated by our Cython .pyx files so that end users don't need to have Cython to install our package.

I think this means we need to explicitly include the generated .c files using the get_extensions() function in setup_package.py as mentioned in astropy docs.

We should include guidelines for inclusion of C and Cython code into plasmaPy. Here are astropy's guidelines.

Examples from Cython book.

Reference for how I added the setup_package.py file in #223 . Adding .c files to the build should also be done via the get_extensions() function within this file.

To get the newly built .c file to appear next to the original .pyx file when building cython modules we need to use python setup.py build_ext --inplace instead of pip install .

This will only be necessary in development when building new .c modules. These then get added to the repo for distribution so end users don't need to do the cythonize step.

The aforementioned inplace setup seems to correctly place the cythonized .c file next to the original .pyx file, but the compiled .so gets placed at the top-level of the package. Not sure why that is, but it doesn't seem right.

The more I read about Cython the cooler it gets. Apparently the whole cdef structure can also be used for creating things that behave like classes, but are actually extensions that work at the Python/C API level, so they're super fast!

Once Cython integration is done, we can further optimize dispersion function calculations using wofz from SciPy's Cython API for special functions. Kappa distribution calculations can also be optimized by using Gamma from the API.

Oh wow, that sounds pretty cool! Do you think we could get this done by mid February, so the 0.1 release supports cython for a visible reason that's already in the repo? How can we help?

I think that's doable.
You can take a look at this to do list and see if you can investigate one of these.
Also I've got the nagging feeling that I may not be setting up the Cython integration correctly in setup.py, so if you can double check that it would be helpful. At present, I added code to pop the list of .c and .pyx extensions from the dictionary returned by astropy_helpers and then I explicitly cythonize in setup() even though I think the default was to not cythonize, but rather just pass this dictionary with all the other additional arguments to setup(). I couldn't get that default to work properly, which is why I changed it. Perhaps the astropy template package or someone in the astropy community has some hints on how to do this?

Was this page helpful?
0 / 5 - 0 ratings