Ошибка Cythonize: сбой при выходе из состояния 2. numpy и pyvista - PullRequest
0 голосов
/ 30 марта 2020

Я хотел бы Cythonize следующий код, но я получаю ошибку.

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

Мой setup.py выглядит следующим образом

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

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

Я запускаю setup.py с следующее.

python setup.py build_ext --inplace
pause

Ошибка очень длинная. В нем говорится, что bool является необъявленным идентификатором. Затем он перечисляет много синтаксических ошибок. Последняя строка

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

1 Ответ

0 голосов
/ 30 марта 2020

Поскольку вы работаете:

from libcpp cimport bool

Вы должны изменить свой язык по умолчанию c на c++ в 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()])

Пожалуйста, смотрите Как мне объявить объект типа bool? для использования bool в cython.

...