Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using deprecated Numpy API

I am writing a small C function that is supposed to accelerate some compute intensive portions of a larger application that I have in Python. Naturally I have written a wrapper that ensures that my C code can talk seamlessly with my Python numpy arrays. All is well and I am using the following setup.py

from distutils.core import setup, Extension
import numpy

module1 = Extension('my_wrapper', 
    sources = ['my_c_file.c'],  
    include_dirs=[numpy.get_include()],
    extra_compile_args = ['-fopenmp'],
    extra_link_args = ['-lgomp'])

setup(name = 'my_wrapper',
    version = '1.0',
    description = 'Some description here',
    ext_modules = [module1])

Everything works when I compile this with the command python3 setup.py install and the code behaviour is as expected but I am getting the following warning,

warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^

Although this is just a warning, I would still like to avoid this if I can. Any ideas on how to do that?

like image 770
juneHunter Avatar asked Oct 10 '18 22:10

juneHunter


2 Answers

This is a known issue, and it stems from the fact that Cython historically has supported very old numpy versions. As cython's doc mentions:

Despite this, you will still get warnings like the following from the compiler, because Cython is using a deprecated Numpy API:

.../include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning
"Using deprecated NumPy API, disable it by " "#defining
NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]

For the time being, it is just a warning that you can ignore.

However, there's work in progress in several pull requests and the parent issue tracking the progress can be found here.

Soon these warnings will be gone.

like image 50
adrin Avatar answered Sep 23 '22 08:09

adrin


Until this is resolved upstream as mentioned by adrin, I found a way to insert the preprocessor symbol NPY_NO_DEPRECATED_API into the setup.py code so that the warning will be suppressed. Add the keyword define_macros=[args] to your Extension. Just putting it in your .h, .cpp, or .pyx files will not help because the auto-generated project .cpp file will not see the preprocessor defines. https://docs.python.org/2.0/dist/node13.html

Specifically for OP's case:

module1 = Extension('my_wrapper', sources = ['my_c_file.c'],  
    include_dirs=[numpy.get_include()],
    extra_compile_args = ['-fopenmp'],
    extra_link_args = ['-lgomp']),
    define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')])

This will add -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION to the compile line.

Fair warning: doing this may enable several new warnings that were suppressed because it was maintaining compatibility with old versions.

like image 32
Jim Parker Avatar answered Sep 19 '22 08:09

Jim Parker