scikit-image
110 строк · 4.6 Кб
1import numpy as np
2import pytest
3from skimage.measure import (
4intersection_coeff,
5manders_coloc_coeff,
6manders_overlap_coeff,
7pearson_corr_coeff,
8)
9
10
11def test_invalid_input():
12# images are not same size
13img1 = np.array([[i + j for j in range(4)] for i in range(4)])
14img2 = np.ones((3, 5, 6))
15mask = np.array([[i <= 1 for i in range(5)] for _ in range(5)])
16non_binary_mask = np.array([[2 for __ in range(4)] for _ in range(4)])
17
18with pytest.raises(ValueError, match=". must have the same dimensions"):
19pearson_corr_coeff(img1, img1, mask)
20with pytest.raises(ValueError, match=". must have the same dimensions"):
21pearson_corr_coeff(img1, img2)
22with pytest.raises(ValueError, match=". must have the same dimensions"):
23pearson_corr_coeff(img1, img1, mask)
24with pytest.raises(ValueError, match=". array is not of dtype boolean"):
25pearson_corr_coeff(img1, img1, non_binary_mask)
26with pytest.raises(ValueError, match=". must have the same dimensions"):
27manders_coloc_coeff(img1, mask)
28with pytest.raises(ValueError, match=". array is not of dtype boolean"):
29manders_coloc_coeff(img1, non_binary_mask)
30with pytest.raises(ValueError, match=". must have the same dimensions"):
31manders_coloc_coeff(img1, img1 > 0, mask)
32with pytest.raises(ValueError, match=". array is not of dtype boolean"):
33manders_coloc_coeff(img1, img1 > 0, non_binary_mask)
34with pytest.raises(ValueError, match=". must have the same dimensions"):
35manders_overlap_coeff(img1, img1, mask)
36with pytest.raises(ValueError, match=". must have the same dimensions"):
37manders_overlap_coeff(img1, img2)
38with pytest.raises(ValueError, match=". must have the same dimensions"):
39manders_overlap_coeff(img1, img1, mask)
40with pytest.raises(ValueError, match=". array is not of dtype boolean"):
41manders_overlap_coeff(img1, img1, non_binary_mask)
42with pytest.raises(ValueError, match=". must have the same dimensions"):
43intersection_coeff(img1 > 2, img2 > 1, mask)
44with pytest.raises(ValueError, match=". array is not of dtype boolean"):
45intersection_coeff(img1, img2)
46with pytest.raises(ValueError, match=". must have the same dimensions"):
47intersection_coeff(img1 > 2, img1 > 1, mask)
48with pytest.raises(ValueError, match=". array is not of dtype boolean"):
49intersection_coeff(img1 > 2, img1 > 1, non_binary_mask)
50
51
52def test_pcc():
53# simple example
54img1 = np.array([[i + j for j in range(4)] for i in range(4)])
55np.testing.assert_almost_equal(
56pearson_corr_coeff(img1, img1), (1.0, 0.0), decimal=14
57)
58
59img2 = np.where(img1 <= 2, 0, img1)
60np.testing.assert_almost_equal(
61pearson_corr_coeff(img1, img2), (0.944911182523068, 3.5667540654536515e-08)
62)
63
64# change background of roi and see if values are same
65roi = np.where(img1 <= 2, 0, 1)
66np.testing.assert_almost_equal(
67pearson_corr_coeff(img1, img1, roi), pearson_corr_coeff(img1, img2, roi)
68)
69
70
71def test_mcc():
72img1 = np.array([[j for j in range(4)] for i in range(4)])
73mask = np.array([[i <= 1 for j in range(4)] for i in range(4)])
74assert manders_coloc_coeff(img1, mask) == 0.5
75
76# test negative values
77img_negativeint = np.where(img1 == 1, -1, img1)
78img_negativefloat = img_negativeint / 2.0
79with pytest.raises(ValueError):
80manders_coloc_coeff(img_negativeint, mask)
81with pytest.raises(ValueError):
82manders_coloc_coeff(img_negativefloat, mask)
83
84
85def test_moc():
86img1 = np.ones((4, 4))
87img2 = 2 * np.ones((4, 4))
88assert manders_overlap_coeff(img1, img2) == 1
89
90# test negative values
91img_negativeint = np.where(img1 == 1, -1, img1)
92img_negativefloat = img_negativeint / 2.0
93with pytest.raises(ValueError):
94manders_overlap_coeff(img_negativeint, img2)
95with pytest.raises(ValueError):
96manders_overlap_coeff(img1, img_negativeint)
97with pytest.raises(ValueError):
98manders_overlap_coeff(img_negativefloat, img2)
99with pytest.raises(ValueError):
100manders_overlap_coeff(img1, img_negativefloat)
101with pytest.raises(ValueError):
102manders_overlap_coeff(img_negativefloat, img_negativefloat)
103
104
105def test_intersection_coefficient():
106img1_mask = np.array([[j <= 1 for j in range(4)] for i in range(4)])
107img2_mask = np.array([[i <= 1 for j in range(4)] for i in range(4)])
108img3_mask = np.array([[1 for j in range(4)] for i in range(4)])
109assert intersection_coeff(img1_mask, img2_mask) == 0.5
110assert intersection_coeff(img1_mask, img3_mask) == 1
111