forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_edgedetection.py
107 lines (68 loc) · 2.38 KB
/
test_edgedetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import cellprofiler.image
import numpy
import numpy.random
import numpy.testing
import skimage.color
import skimage.filters
import edgedetection
instance = edgedetection.EdgeDetection
def test_run_without_mask(image, image_set, module, workspace):
module.x_name.value = "example"
module.y_name.value = "EdgeDetection"
module.mask.value = "Leave blank"
module.run(workspace)
actual = image_set.get_image("EdgeDetection")
data = image.pixel_data
if image.multichannel:
data = skimage.color.rgb2gray(data)
if image.dimensions == 2:
expected_data = skimage.filters.sobel(data)
else:
expected_data = numpy.zeros_like(data)
for idx, img in enumerate(data):
expected_data[idx] = skimage.filters.sobel(img)
expected = cellprofiler.image.Image(
image=expected_data,
parent_image=image,
dimensions=image.dimensions
)
numpy.testing.assert_array_equal(expected.pixel_data, actual.pixel_data)
def test_run_with_mask(image, image_set, module, workspace):
module.x_name.value = "example"
module.y_name.value = "EdgeDetection"
module.mask.value = "mask"
mask_shape = image.pixel_data.shape
if image.dimensions == 2:
mask_data = numpy.random.rand(mask_shape[0], mask_shape[1])
mask_data[:5] = 0
mask_data[-5:] = 0
mask_data[:, :5] = 0
mask_data[:, -5:] = 0
else:
mask_data = numpy.random.rand(*mask_shape)
mask_data[:, :5] = 0
mask_data[:, -5:] = 0
mask_data[:, :, :5] = 0
mask_data[:, :, -5:] = 0
mask_data = mask_data != 0
mask = cellprofiler.image.Image(
image=mask_data
)
image_set.add("mask", mask)
module.run(workspace)
actual = image_set.get_image("EdgeDetection")
data = image.pixel_data
if image.multichannel:
data = skimage.color.rgb2gray(data)
if image.dimensions == 2:
expected_data = skimage.filters.sobel(data, mask=mask_data)
else:
expected_data = numpy.zeros_like(data)
for idx, img in enumerate(data):
expected_data[idx] = skimage.filters.sobel(img, mask=mask_data[idx])
expected = cellprofiler.image.Image(
image=expected_data,
parent_image=image,
dimensions=image.dimensions
)
numpy.testing.assert_array_equal(expected.pixel_data, actual.pixel_data)