forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestOperatorWrapper.py
179 lines (145 loc) · 6.08 KB
/
testOperatorWrapper.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
173
174
175
176
177
178
179
from builtins import object
###############################################################################
# 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 lazyflow.graph import Graph, Operator, InputSlot, OutputSlot, OperatorWrapper
import numpy
import copy
class OpSimple(Operator):
InputA = InputSlot()
InputB = InputSlot()
Output = OutputSlot()
def setupOutputs(self):
self.Output.meta.shape = self.InputA.meta.shape
self.Output.meta.dtype = self.InputA.meta.dtype
def execute(self, slot, subindex, roi, result):
assert slot == self.Output
result[...] = self.InputA(roi.start, roi.stop).wait() * self.InputB[0:1].wait()
def propagateDirty(self, inputSlot, subindex, roi):
if inputSlot == self.InputA:
self.Output.setDirty(roi)
elif (inputSlot == self.InputB
and roi.start[0] == 0
and roi.stop[0] >= 1):
dirtyRoi = copy.copy(roi)
dirtyRoi.stop[0] = 1
self.Output.setDirty(dirtyRoi)
else:
assert False
class OpExplicitMulti(Operator):
Output = OutputSlot(level=1)
def setupOutputs(self):
pass
class OpCopyInput(Operator):
Input = InputSlot()
Output = OutputSlot()
def setupOutputs(self):
self.Output.setValue(self.Input.value)
def propagateDirty(self, inputSlot, subindex, roi):
self.Output.setDirty(roi)
class TestBasic(object):
@classmethod
def setupClass(cls):
cls.graph = Graph()
@classmethod
def teardownClass(cls):
pass
def test_fullWrapping(self):
"""
Test basic wrapping functionality (all slots are promoted)
"""
wrapped = OperatorWrapper( OpSimple, graph=self.graph )
assert type(wrapped.InputA) == InputSlot
assert type(wrapped.InputB) == InputSlot
assert type(wrapped.Output) == OutputSlot
assert wrapped.InputA.level == 1
assert wrapped.InputB.level == 1
assert wrapped.Output.level == 1
assert len(wrapped.InputA) == 0
assert len(wrapped.InputB) == 0
assert len(wrapped.Output) == 0
wrapped.InputA.resize(2)
assert len(wrapped.InputB) == 2
assert len(wrapped.Output) == 2
a = numpy.array([[1,2],[3,4]])
b = numpy.array([2])
wrapped.InputA[0].setValue(a)
wrapped.InputB[0].setValue(b)
wrapped.InputA[1].setValue(2*a)
wrapped.InputB[1].setValue(3*b)
result0 = wrapped.Output[0][0:2,0:2].wait()
result1 = wrapped.Output[1][0:2,0:2].wait()
assert ( result0 == a * b[0] ).all()
assert ( result1 == 2*a * 3*b[0] ).all()
def test_partialWrapping(self):
"""
By default, OperatorWrapper promotes all slots.
This function tests what happens when only a subset of the inputs are promoted.
"""
wrapped = OperatorWrapper( OpSimple, graph=self.graph, promotedSlotNames=set(['InputA']) )
assert type(wrapped.InputA) == InputSlot
assert type(wrapped.InputB) == InputSlot
assert type(wrapped.Output) == OutputSlot
assert wrapped.InputA.level == 1 # Promoted because it was listed in the constructor call
assert wrapped.InputB.level == 0 # NOT promoted
assert wrapped.Output.level == 1 # Promoted because it's an output
assert len(wrapped.InputA) == 0
assert len(wrapped.InputB) == 0
assert len(wrapped.Output) == 0
wrapped.InputA.resize(2)
assert len(wrapped.InputB) == 0 # Not promoted
assert len(wrapped.Output) == 2
a = numpy.array([[1,2],[3,4]])
b = numpy.array([2])
wrapped.InputA[0].setValue(a)
wrapped.InputB.setValue(b)
wrapped.InputA[1].setValue(2*a)
result0 = wrapped.Output[0][0:2,0:2].wait()
result1 = wrapped.Output[1][0:2,0:2].wait()
assert ( result0 == a * b[0] ).all()
assert ( result1 == 2*a * b[0] ).all()
class TestMultiOutputToWrapped(object):
@classmethod
def setupClass(cls):
cls.graph = Graph()
def test_input_output_resize(self):
exMulti = OpExplicitMulti(graph=self.graph)
wrappedSimple = OperatorWrapper( OpSimple, graph=self.graph )
assert len(wrappedSimple.InputA) == 0
wrappedSimple.InputA.connect( exMulti.Output )
assert len(wrappedSimple.InputA) == 0
exMulti.Output.resize( 1 )
assert len(wrappedSimple.InputA) == 1
assert len(wrappedSimple.InputB) == 1
assert len(wrappedSimple.Output) == 1
def test_setValues(self):
wrappedCopier = OperatorWrapper( OpCopyInput, graph=self.graph )
values = ["Subslot One", "Subslot Two"]
wrappedCopier.Input.setValues( values )
assert wrappedCopier.Output[0].value == values[0]
assert wrappedCopier.Output[1].value == values[1]
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)