scikit-image

Форк
0
/
test_colocalization.py 
110 строк · 4.6 Кб
1
import numpy as np
2
import pytest
3
from skimage.measure import (
4
    intersection_coeff,
5
    manders_coloc_coeff,
6
    manders_overlap_coeff,
7
    pearson_corr_coeff,
8
)
9

10

11
def test_invalid_input():
12
    # images are not same size
13
    img1 = np.array([[i + j for j in range(4)] for i in range(4)])
14
    img2 = np.ones((3, 5, 6))
15
    mask = np.array([[i <= 1 for i in range(5)] for _ in range(5)])
16
    non_binary_mask = np.array([[2 for __ in range(4)] for _ in range(4)])
17

18
    with pytest.raises(ValueError, match=". must have the same dimensions"):
19
        pearson_corr_coeff(img1, img1, mask)
20
    with pytest.raises(ValueError, match=". must have the same dimensions"):
21
        pearson_corr_coeff(img1, img2)
22
    with pytest.raises(ValueError, match=". must have the same dimensions"):
23
        pearson_corr_coeff(img1, img1, mask)
24
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
25
        pearson_corr_coeff(img1, img1, non_binary_mask)
26
    with pytest.raises(ValueError, match=". must have the same dimensions"):
27
        manders_coloc_coeff(img1, mask)
28
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
29
        manders_coloc_coeff(img1, non_binary_mask)
30
    with pytest.raises(ValueError, match=". must have the same dimensions"):
31
        manders_coloc_coeff(img1, img1 > 0, mask)
32
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
33
        manders_coloc_coeff(img1, img1 > 0, non_binary_mask)
34
    with pytest.raises(ValueError, match=". must have the same dimensions"):
35
        manders_overlap_coeff(img1, img1, mask)
36
    with pytest.raises(ValueError, match=". must have the same dimensions"):
37
        manders_overlap_coeff(img1, img2)
38
    with pytest.raises(ValueError, match=". must have the same dimensions"):
39
        manders_overlap_coeff(img1, img1, mask)
40
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
41
        manders_overlap_coeff(img1, img1, non_binary_mask)
42
    with pytest.raises(ValueError, match=". must have the same dimensions"):
43
        intersection_coeff(img1 > 2, img2 > 1, mask)
44
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
45
        intersection_coeff(img1, img2)
46
    with pytest.raises(ValueError, match=". must have the same dimensions"):
47
        intersection_coeff(img1 > 2, img1 > 1, mask)
48
    with pytest.raises(ValueError, match=". array is not of dtype boolean"):
49
        intersection_coeff(img1 > 2, img1 > 1, non_binary_mask)
50

51

52
def test_pcc():
53
    # simple example
54
    img1 = np.array([[i + j for j in range(4)] for i in range(4)])
55
    np.testing.assert_almost_equal(
56
        pearson_corr_coeff(img1, img1), (1.0, 0.0), decimal=14
57
    )
58

59
    img2 = np.where(img1 <= 2, 0, img1)
60
    np.testing.assert_almost_equal(
61
        pearson_corr_coeff(img1, img2), (0.944911182523068, 3.5667540654536515e-08)
62
    )
63

64
    # change background of roi and see if values are same
65
    roi = np.where(img1 <= 2, 0, 1)
66
    np.testing.assert_almost_equal(
67
        pearson_corr_coeff(img1, img1, roi), pearson_corr_coeff(img1, img2, roi)
68
    )
69

70

71
def test_mcc():
72
    img1 = np.array([[j for j in range(4)] for i in range(4)])
73
    mask = np.array([[i <= 1 for j in range(4)] for i in range(4)])
74
    assert manders_coloc_coeff(img1, mask) == 0.5
75

76
    # test negative values
77
    img_negativeint = np.where(img1 == 1, -1, img1)
78
    img_negativefloat = img_negativeint / 2.0
79
    with pytest.raises(ValueError):
80
        manders_coloc_coeff(img_negativeint, mask)
81
    with pytest.raises(ValueError):
82
        manders_coloc_coeff(img_negativefloat, mask)
83

84

85
def test_moc():
86
    img1 = np.ones((4, 4))
87
    img2 = 2 * np.ones((4, 4))
88
    assert manders_overlap_coeff(img1, img2) == 1
89

90
    # test negative values
91
    img_negativeint = np.where(img1 == 1, -1, img1)
92
    img_negativefloat = img_negativeint / 2.0
93
    with pytest.raises(ValueError):
94
        manders_overlap_coeff(img_negativeint, img2)
95
    with pytest.raises(ValueError):
96
        manders_overlap_coeff(img1, img_negativeint)
97
    with pytest.raises(ValueError):
98
        manders_overlap_coeff(img_negativefloat, img2)
99
    with pytest.raises(ValueError):
100
        manders_overlap_coeff(img1, img_negativefloat)
101
    with pytest.raises(ValueError):
102
        manders_overlap_coeff(img_negativefloat, img_negativefloat)
103

104

105
def test_intersection_coefficient():
106
    img1_mask = np.array([[j <= 1 for j in range(4)] for i in range(4)])
107
    img2_mask = np.array([[i <= 1 for j in range(4)] for i in range(4)])
108
    img3_mask = np.array([[1 for j in range(4)] for i in range(4)])
109
    assert intersection_coeff(img1_mask, img2_mask) == 0.5
110
    assert intersection_coeff(img1_mask, img3_mask) == 1
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.