我目前正在撰寫幾個函式,它們都使用numba(一個 with@guvectorize一個 with進行了優化@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)
uj5u.com熱心網友回復:
只要代碼被編譯,coverage.py就不能再測量你的代碼的覆寫率。您可以找到一些關于此的問題 。
您可以通過從 coverage.py 中排除代碼來簡單地將未經測驗的代碼隱藏在地毯下。
但我知道你對你的代碼很認真,你真的想檢查你的演算法。然后你可以運行你的測驗兩次。一來檢查你的代碼,另一個僅覆寫,通過設定環境變數NUMBA_DISABLE_JIT=1,如所描述這里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/360608.html
