0

I'd like to Cythonize the following code, but I receive an error.

import numpy as np
cimport numpy as np
import pyvista as pv
from mesh_funcs import *
cimport cython 
from libcpp cimport bool
#import matplotlib.pyplot as plt


#Getting mesh points from pyvista unfortunately with a for loop
cdef class pv_cell:
    def pv_grid_cell_data(mesh2,bool points_toggle=True,
                          bool centres_toggle=True,bool volumes_toggle=True, bool areas_toggle=True):

        if areas_toggle==True:
            points_toggle==True

        if centres_toggle==True:
            points_toggle==True

        cdef np.ndarray gcp=np.zeros([mesh2.n_cells,8,3])
        cdef np.ndarray gcc=np.zeros([mesh2.n_cells,3])
        cdef np.ndarray gcv=np.zeros([mesh2.n_cells,3])
        cdef np.ndarray grid_facets=np.array([[0,1,2,3], [0,1,5,4], [1,2,6,5], [7,6,2,3], [7,3,0,4], [4,5,6,7]])
        cdef np.ndarray gca=np.zeros([mesh2.n_cells,6])

        for n1 in range(0,mesh2.n_cells):
            if points_toggle==True:
                gcp[n1]=mesh2.extract_cells(n1).points

            if centres_toggle==True:
                gcc[n1]=[np.mean(gcp[n1][:,0]),np.mean(gcp[n1][:,1]),np.mean(gcp[n1][:,2])]

            if volumes_toggle==True:
                gcv[n1]=mesh2.extract_cells(n1).compute_cell_sizes()["Volume"]

            if areas_toggle==True:

                for n2 in range(0,6):
                    ph8=gcp[n1][grid_facets[n2]]
                    gca[n1,n2]=tri_area(ph8[[0,2,3]])+tri_area(ph8[[0,1,3]])
        return gcp,gcc,gcv,gca

My setup.py is as follows

from setuptools import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("pv_cell_funcs.pyx"),include_dirs=[numpy.get_include()])

I launch setup.py with the following.

python setup.py build_ext --inplace
pause

The error is very long. It states bool is an undeclared identifier. It then lists a lot of syntax errors. The last line is

error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\...x64\\cl.exe' failed with exit status 2

enter image description here

Cam K
  • 127
  • 2
  • 2
  • 13
  • Please take a look at [mcve], your example isn’t minimal (you probably don’t need that much code to trigger the error) and not complete (you present only the last line of the error message). – ead Mar 30 '20 at 16:50
  • I mean ‘from libcpp cimport bool’ would probably be enough to trigger the error... – ead Mar 30 '20 at 16:53
  • I've included the error message as a picture. The question is not specific only to cython. The question is about using numpy and pyvista in cython. Cython is of interest to people making 2d and 3d models because of the number of loops required for calculations and therefore the need for performance optimization. – Cam K Mar 31 '20 at 11:18
  • You have missed my point about minimal example completely: if example is minimal error the message isn’t long and can be pasted verbatim. Because your example isn’t minimal there are probably multiple issues. – ead Mar 31 '20 at 11:49
  • I understand. To clarify, this code worked fine in python. As soon as I tried to convert the code to cython, this issue happened. – Cam K Mar 31 '20 at 13:13

1 Answers1

0

Since you're running:

from libcpp cimport bool

You should change your language from the default c to c++ in setup.py:

from setuptools import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("pv_cell_funcs.pyx", language='c++'),
    include_dirs=[numpy.get_include()])

Please see How do I declare an object of type bool? for using bool in cython.

Alex Kaszynski
  • 1,817
  • 2
  • 17
  • 17
  • Hello Alex, I've done that, but my errors are the same. I've added the errors as a picture in the question. – Cam K Mar 31 '20 at 11:05
  • Works for me locally with `cython=0.29.14` and `numpy=1.18.1`. Could you please check if you're using a modern version of `numpy` and `cython`? – Alex Kaszynski Mar 31 '20 at 18:20
  • I installed cython 0.29.15 through anaconda. My numpy version is 1.18.1. I successfully completed the Fibonacci example in this link https://cython.readthedocs.io/en/latest/src/tutorial/cython_tutorial.html – Cam K Apr 01 '20 at 12:07
  • I'd reduce your cython script down to just a few lines and build it up from there. There's several flaws with your cython script (including repeated calls to `extract_cells`) that won't make it as performant as you wish. Cython isn't really something you can copy and paste from python, and often requires significant rework to get it more C-like. – Alex Kaszynski Apr 01 '20 at 14:46
  • 1
    I'll do that this week and mark your response as the answer and close the github issue on the weekend! Thank you! – Cam K Apr 01 '20 at 15:44