forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestRoiInterface.py
172 lines (130 loc) · 7.72 KB
/
testRoiInterface.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from builtins import range
###############################################################################
# lazyflow: data flow based lazy parallel computation framework
#
# Copyright (C) 2011-2014, the ilastik developers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Lesser GNU General Public License
# as published by the Free Software Foundation; either version 2.1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# See the files LICENSE.lgpl2 and LICENSE.lgpl3 for full text of the
# GNU Lesser General Public License version 2.1 and 3 respectively.
# This information is also available on the ilastik web site at:
# http://ilastik.org/license/
###############################################################################
from unittest import TestCase
import vigra,numpy
from lazyflow.graph import Graph,Operator,InputSlot,OutputSlot
from lazyflow.operators import OpArrayPiper
from lazyflow.utility.helpers import generateRandomRoi
import lazyflow.roi
class OpRoiTest(Operator):
input = InputSlot()
output = OutputSlot()
def setupOutputs(self):
self.outputs["output"].meta.dtype = self.inputs["input"].meta.dtype
self.outputs["output"].meta.shape = self.inputs["input"].meta.shape
self.outputs["output"].meta.axistags = self.inputs["input"].meta.axistags
def execute(self, slot, subindex, roi, result):
tmpRes = self.inputs["input"](start=roi.start,stop=roi.stop).wait()
result[:] = tmpRes
return result
def propagateDirty(self, inputSlot, subindex, roi):
self.output.setDirty(roi)
class TestRoiInterdace(TestCase):
def setUp(self):
self.testVol = vigra.VigraArray((200,200,200))
self.testVol[:] = numpy.random.rand(200,200,200)
self.graph = Graph()
self.op = OpArrayPiper(graph=self.graph)
self.op.inputs["Input"].setValue(self.testVol)
self.roiOp = OpRoiTest(graph=self.graph)
self.roiOp.inputs["input"].setValue(self.testVol)
def test_roi(self):
for i in range(20):
roi = generateRandomRoi((200,200,200))
result=self.op.outputs["Output"](start=roi[0], stop=roi[1]).wait()
def test_RoiOp(self):
for i in range(20):
roi = generateRandomRoi((200,200,200))
result=self.roiOp.outputs["output"](start=roi[0], stop=roi[1]).wait()
class TestRoiUtilities(TestCase):
def test_expandSlicing(self):
shape = (2,4,6,8,10)
# Single index
assert lazyflow.roi.expandSlicing(1, shape) == (1,) + 4*(slice(None),)
# Single slice
assert lazyflow.roi.expandSlicing(slice(None), shape) == 5*(slice(None),)
assert lazyflow.roi.expandSlicing(slice(0,1), shape) == (slice(0,1),) + 4*(slice(None),)
# As list
assert lazyflow.roi.expandSlicing([slice(0,1)], shape) == (slice(0,1),) + 4*(slice(None),)
assert lazyflow.roi.expandSlicing([slice(0,1), 3, 4], shape) == (slice(0,1),3, 4) + 2*(slice(None),)
# As tuple
assert lazyflow.roi.expandSlicing((slice(0,1),), shape) == (slice(0,1),) + 4*(slice(None),)
assert lazyflow.roi.expandSlicing((slice(0,1), 3, 4), shape) == (slice(0,1),3, 4) + 2*(slice(None),)
# Single Ellipsis
assert lazyflow.roi.expandSlicing(Ellipsis, shape) == 5*(slice(None),)
# Tuple/list with Ellipsis
assert lazyflow.roi.expandSlicing((0, 1, Ellipsis, 9), shape) == (0, 1, slice(None), slice(None), 9)
assert lazyflow.roi.expandSlicing([0, 1, Ellipsis, 9], shape) == (0, 1, slice(None), slice(None), 9)
# Special cases for empty shapes
emptyShape = ()
assert lazyflow.roi.expandSlicing(Ellipsis, emptyShape ) == emptyShape
assert lazyflow.roi.expandSlicing(slice(None), emptyShape ) == emptyShape
def test_sliceToRoi(self):
from lazyflow.roi import TinyVector
shape = (2,4,6,8,10)
# -- With extendSingleton=True
# Single index
assert lazyflow.roi.sliceToRoi(1, shape) == (TinyVector((1,0,0,0,0)), TinyVector((2,4,6,8,10)))
# Single slice
assert lazyflow.roi.sliceToRoi(slice(None), shape) == (TinyVector((0,0,0,0,0)), TinyVector((2,4,6,8,10)))
assert lazyflow.roi.sliceToRoi(slice(0,1), shape) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
# As list
assert lazyflow.roi.sliceToRoi([slice(0,1)], shape) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
assert lazyflow.roi.sliceToRoi([slice(0,1), 3, 4], shape) == (TinyVector((0,3,4,0,0)), TinyVector((1,4,5,8,10)))
# As tuple
assert lazyflow.roi.sliceToRoi((slice(0,1),), shape) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
assert lazyflow.roi.sliceToRoi((slice(0,1), 3, 4), shape) == (TinyVector((0,3,4,0,0)), TinyVector((1,4,5,8,10)))
# Single Ellipsis
assert lazyflow.roi.sliceToRoi(Ellipsis, shape) == (TinyVector((0,0,0,0,0)), TinyVector((2,4,6,8,10)))
# Tuple/list with Ellipsis
assert lazyflow.roi.sliceToRoi((0, 1, Ellipsis, 9), shape) == (TinyVector((0,1,0,0,9)), TinyVector((1,2,6,8,10)))
assert lazyflow.roi.sliceToRoi([0, 1, Ellipsis, 9], shape) == (TinyVector((0,1,0,0,9)), TinyVector((1,2,6,8,10)))
# -- With extendSingleton=False (for some of these, it makes no difference)
# Single index
assert lazyflow.roi.sliceToRoi(1, shape, extendSingleton=False) == (TinyVector((1,0,0,0,0)), TinyVector((1,4,6,8,10)))
# Single slice
assert lazyflow.roi.sliceToRoi(slice(None), shape, extendSingleton=False) == (TinyVector((0,0,0,0,0)), TinyVector((2,4,6,8,10)))
assert lazyflow.roi.sliceToRoi(slice(0,1), shape, extendSingleton=False) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
# As list
assert lazyflow.roi.sliceToRoi([slice(0,1)], shape, extendSingleton=False) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
assert lazyflow.roi.sliceToRoi([slice(0,1), 3, 4], shape, extendSingleton=False) == (TinyVector((0,3,4,0,0)), TinyVector((1,3,4,8,10)))
# As tuple
assert lazyflow.roi.sliceToRoi((slice(0,1),), shape, extendSingleton=False) == (TinyVector((0,0,0,0,0)), TinyVector((1,4,6,8,10)))
assert lazyflow.roi.sliceToRoi((slice(0,1), 3, 4), shape, extendSingleton=False) == (TinyVector((0,3,4,0,0)), TinyVector((1,3,4,8,10)))
# Single Ellipsis
assert lazyflow.roi.sliceToRoi(Ellipsis, shape, extendSingleton=False) == (TinyVector((0,0,0,0,0)), TinyVector((2,4,6,8,10)))
# Tuple/list with Ellipsis
assert lazyflow.roi.sliceToRoi((0, 1, Ellipsis, 9), shape, extendSingleton=False) == (TinyVector((0,1,0,0,9)), TinyVector((0,1,6,8,9)))
assert lazyflow.roi.sliceToRoi([0, 1, Ellipsis, 9], shape, extendSingleton=False) == (TinyVector((0,1,0,0,9)), TinyVector((0,1,6,8,9)))
def test_roiToSlice(self):
from lazyflow.roi import TinyVector
shape = (2,4,6,8,10)
roi = (TinyVector((1,2,3,4,5)), TinyVector(shape))
assert lazyflow.roi.roiToSlice(roi[0], roi[1]) == (slice(1,2), slice(2,4), slice(3,6), slice(4,8), slice(5,10))
if __name__ == "__main__":
import sys
import nose
sys.argv.append("--nocapture") # Don't steal stdout. Show it on the console as usual.
sys.argv.append("--nologcapture") # Don't set the logging level to DEBUG. Leave it alone.
ret = nose.run(defaultTest=__file__)
if not ret: sys.exit(1)