forked from emmanuelle/random_walker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_random_walker.py
133 lines (116 loc) · 4.09 KB
/
test_random_walker.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
from random_walker import random_walker, random_walker_prior
try:
from pyamg import smoothed_aggregation_solver
amg_loaded = True
except ImportError:
amg_loaded = False
def make_2d_syntheticdata(lx, ly=None):
if ly is None:
ly = lx
data = np.zeros((lx, ly)) + 0.1*np.random.randn(lx, ly)
small_l = int(lx / 5)
data[lx/2 - small_l:lx/2+small_l, ly/2-small_l:ly/2+small_l] = 1
data[lx/2 - small_l+1:lx/2+small_l-1, \
ly/2-small_l+1:ly/2+small_l-1] = \
0.1 * np.random.randn(2*small_l-2, 2*small_l-2)
data[lx/2-small_l, ly/2-small_l/8:ly/2+small_l/8] = 0
seeds = np.zeros_like(data)
seeds[lx/5, ly/5] = 1
seeds[lx/2 + small_l/4, ly/2 - small_l/4] = 2
return data, seeds
def make_2d_syntheticdata_more_seeds(lx, ly=None):
if ly is None:
ly = lx
data = np.zeros((lx, ly)) + 0.1*np.random.randn(lx, ly)
small_l = int(lx / 5)
data[lx/2 - small_l:lx/2+small_l, ly/2-small_l:ly/2+small_l] = 1
data[lx/2 - small_l+1:lx/2+small_l-1, \
ly/2-small_l+1:ly/2+small_l-1] = \
0.1 * np.random.randn(2*small_l-2, 2*small_l-2)
data[lx/2-small_l, ly/2-small_l/8:ly/2+small_l/8] = 0
seeds = np.zeros_like(data)
seeds[lx/10:-lx/10, ly/5] = 1
seeds[lx/2 + small_l/4, ly/2 - small_l/2:ly/2 + small_l/2] = 2
return data, seeds
def make_3d_syntheticdata(lx, ly=None, lz=None):
if ly is None:
ly = lx
if lz is None:
lz = lx
data = np.zeros((lx, ly, lz)) + 0.1*np.random.randn(lx, ly, lz)
small_l = int(lx/5)
data[lx/2-small_l:lx/2+small_l,\
ly/2-small_l:ly/2+small_l,\
lz/2-small_l:lz/2+small_l] = 1
data[lx/2-small_l+1:lx/2+small_l-1,\
ly/2-small_l+1:ly/2+small_l-1,
lz/2-small_l+1:lz/2+small_l-1] = 0
# make a hole
hole_size = np.max([1, small_l/8])
data[lx/2-small_l,\
ly/2-hole_size:ly/2+hole_size,\
lz/2-hole_size:lz/2+hole_size] = 0
seeds = np.zeros_like(data)
seeds[lx/5, ly/5, lz/5] = 1
seeds[lx/2+small_l/4, ly/2-small_l/4, lz/2-small_l/4] = 2
return data, seeds
def test_2d():
lx = 70
ly = 100
data, labels = make_2d_syntheticdata(lx, ly)
labels_bf = random_walker(data, labels, beta=90)
assert (labels_bf[25:45, 40:60] == 2).all()
return data, labels_bf
def test_2d_cg():
lx = 70
ly = 100
data, labels = make_2d_syntheticdata(lx, ly)
labels_cg = random_walker(data, labels, beta=90, mode='cg')
assert (labels_cg[25:45, 40:60] == 2).all()
return data, labels_cg
def test_2d_cg():
lx = 70
ly = 100
data, labels = make_2d_syntheticdata(lx, ly)
labels_cg = random_walker(data, labels, beta=90, mode='cg_mg')
assert (labels_cg[25:45, 40:60] == 2).all()
return data, labels_cg
def test_2d_inactive():
lx = 70
ly = 100
data, labels = make_2d_syntheticdata(lx, ly)
labels[10:20, 10:20] = -1
labels[46:50, 33:38] = -2
labels = random_walker(data, labels, beta=90)
assert (labels.reshape((lx, ly))[25:45, 40:60] == 2).all()
return data, labels
def test_3d():
n=30
lx, ly, lz = n, n, n
data, labels = make_3d_syntheticdata(lx, ly, lz)
labels = random_walker(data, labels)
assert (labels.reshape(data.shape)[13:17,13:17,13:17] == 2).all()
return data, labels
def test_3d_inactive():
n=30
lx, ly, lz = n, n, n
data, labels = make_3d_syntheticdata(lx, ly, lz)
old_labels = np.copy(labels)
labels[5:25, 26:29, 26:29] = -1
after_labels = np.copy(labels)
labels = random_walker(data, labels)
assert (labels.reshape(data.shape)[13:17,13:17,13:17] == 2).all()
return data, labels, old_labels, after_labels
def test_rw_with_prior():
a = np.zeros((40, 40))
a[10:-10, 10:-10] = 1
a += 0.7*np.random.random((40, 40))
p = a.max() - a.ravel()
q = a.ravel()
prior = np.array([p, q])
labs = random_walker_prior(a, prior)
assert (labs[11:-11, 11:-11] == 1).all()
if amg_loaded:
labs_amg = random_walker_prior(a, prior, mode='amg')
assert (labs_amg[11:-11, 11:-11] == 1).all()