core.pyx
Код: Выделить всё
import numpy as np
cimport numpy as np
cdef class MyClass():
cdef public np.uint8_t[:] mask # uint8 has the same data structure of a boolean array
cdef public np.float64_t[:] data
def __init__(self, size):
self.data = np.random.rand(size).astype(np.float64)
self.mask = np.zeros(size, np.uint8)
Код: Выделить всё
import numpy as np
import pyximport
pyximport.install(setup_args={'include_dirs': np.get_include()})
from core import MyClass
mc = MyClass(1000000)
mc.mask = np.asarray(mc.data) > 0.5
Когда я запускаю script.py, он успешно компилирует Cython, но выдает ошибку:
Код: Выделить всё
Traceback (most recent call last):
File "script.py", line 8, in
mc.mask = np.asarray(mc.data) > 0.5
File "core.pyx", line 6, in core.MyClass.mask.__set__
cdef public np.uint8_t[:] mask
ValueError: Does not understand character buffer dtype format string ('?')
Мой текущий обходной путь — передать маску всем нужным функциям, используя cast=True, например:
Код: Выделить всё
cpdef func(MyClass mc, np.ndarray[np.uint8_t, ndim=1, cast=True] mask):
return np.asarray(mc.data)[mask]
Есть ли какие-нибудь идеи о том, как маску можно было сохранить в классе Cython?
Подробнее здесь: https://stackoverflow.com/questions/582 ... thon-class
Мобильная версия