[英]How can i test numba vectorized functions with pytest
我目前正在編寫幾個函數,它們都使用numba
進行了優化(一個使用@guvectorize
,一個使用@vectorize
。我還為這兩個函數編寫了一些測試,但是當我運行pytest --cov --cov-report term-missing
我知道缺失的行對應於優化的函數。
這是 pytest 如何對函數運行測試的問題還是由於其他(我的)問題?
這兩個函數中最簡單的是:
@vectorize(["float64(float64, float64)", "float32(float32, float32)"], nopython=True)
def binarize_mask(mask_data, threshold):
"""Binarize the mask array based on a threshold.
:param mask_data: Mask array.
:param threshold: Threshold to apply to the mask.
"""
# Binarize the mask array
return 1 if mask_data >= threshold else 0
我用以下測試進行測試:
def test_binarize_mask_return_value():
threshold = np.float32(0.5)
assert dl.binarize_mask(np.float32(0.3), threshold) == 0
assert dl.binarize_mask(np.float32(0.7), threshold) == 1
def test_binarize_mask_float32():
test_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=np.float32)
test_mask = np.array([0, 0, 1, 1, 1, 1, 1, 1, 1, 1], dtype=np.float32)
binarized = dl.binarize_mask(test_data, 3.0)
assert binarized.dtype == np.float64
assert binarized.shape == test_mask.shape
assert np.all(binarized == test_mask)
只要代碼被編譯, coverage.py
就不能再測量代碼的覆蓋率。 您可以找到一些關於此的問題。
您可以通過從 coverage.py 中排除代碼來簡單地將未經測試的代碼隱藏在地毯下。
但我知道你對你的代碼很認真,你真的想檢查你的算法。 然后你可以運行你的測試兩次。 通過設置環境變量NUMBA_DISABLE_JIT=1
來檢查您的代碼,另一個僅用於覆蓋范圍,如here所述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.