forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestSlotCallbacks.py
287 lines (215 loc) · 8.34 KB
/
testSlotCallbacks.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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/
###############################################################################
import nose
from lazyflow.graph import Graph, Operator, InputSlot, OutputSlot
from lazyflow import stype
from lazyflow import operators
import numpy
class OpS(Operator):
name = "OpA"
Output1 = OutputSlot()
Output2 = OutputSlot()
Output3 = OutputSlot()
Output4 = OutputSlot(level = 1)
def __init__(self, parent=None, graph=None):
Operator.__init__(self,parent,graph)
def setupOutputs(self):
self._configured = True
self.Output1.meta.shape = (1,)
self.Output1.meta.dtype = numpy.float32
self.Output2.meta.shape = (2,)
self.Output2.meta.dtype = numpy.uint8
self.Output3.meta.shape = (3,)
self.Output3.meta.dtype = numpy.uint32
def execute(self, slot, subindex, roi, result):
pass
class OpA(Operator):
name = "OpA"
Input1 = InputSlot() # required slot
Input2 = InputSlot(optional = True) # optional slot
Input3 = InputSlot(value = 3) # required slot with default value, i.e. already connected
Input4 = InputSlot(level = 1)
Output1 = OutputSlot()
Output2 = OutputSlot()
Output3 = OutputSlot()
Output4 = OutputSlot(level =1)
def __init__(self, parent=None, graph=None):
Operator.__init__(self,parent=parent, graph=graph)
self._configured = False
def setupOutputs(self):
self._configured = True
self.Output1.meta.shape = self.Input1.meta.shape
self.Output1.meta.dtype = self.Input1.meta.dtype
self.Output2.meta.shape = self.Input2.meta.shape
self.Output2.meta.dtype = self.Input2.meta.dtype
self.Output3.meta.shape = self.Input3.meta.shape
self.Output3.meta.dtype = self.Input3.meta.dtype
self.Output4.meta.shape = self.Input4.meta.shape
self.Output4.meta.dtype = self.Input4.meta.dtype
def execute(self, slot, subindex, roi, result):
pass
def propagateDirty(self, inputSlot, subindex, roi):
pass
class TestSlot_notifyConnect(object):
def setUp(self):
self.g = Graph()
def test_connect(self):
# test that the notifyConnect callback is called
# when the slot is connected
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
opa.Input2.connect(ops.Output2)
upval = [False]
def callBack(slot):
upval[0] = True
# check the connect callback is called
opa.Input1._notifyConnect(callBack)
opa.Input1.connect(ops.Output1)
assert upval[0] == True
# check the connect callback is called for a slot with default value
upval[0] = False
opa.Input3._notifyConnect(callBack)
opa.Input3.connect(ops.Output3)
assert upval[0] == True
# check the connect callback is called for a multi inputslot
upval[0] = False
opa.Input4._notifyConnect(callBack)
opa.Input4.connect(ops.Output4)
assert upval[0] == True
def test_no_connect(self):
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
opa.Input1._notifyConnect(callBack)
# check the connect callback is not called when reconnecting to the same slot
opa.Input1.connect(ops.Output1)
upval[0] = False
opa.Input1.connect(ops.Output1)
assert upval[0] == False
# check the connect callback is not called when setting the same value again
opa.Input1.disconnect()
opa.Input1.setValue(10)
upval[0] = False
opa.Input1.setValue(10)
assert upval[0] == False
def test_unregister_connect(self):
# check that unregistering a connect callback works
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
opa.Input1._notifyConnect(callBack)
opa.Input1._unregisterConnect(callBack)
opa.Input1.connect(ops.Output1)
assert upval[0] == False
class TestSlot_notifyDisconnect(object):
def setUp(self):
self.g = Graph()
def test_disconnect(self):
# test that the notifyConnect callback is called
# when the slot is connected
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
# check the disconnect callback is called upon disconnect
opa.Input1.connect(ops.Output1)
opa.Input1.notifyDisconnect(callBack)
opa.Input1.disconnect()
assert upval[0] == True
# check the disconnect callback is called upon reconnect
opa.Input1.connect(ops.Output1)
upval[0] = False
opa.Input1.connect(ops.Output2)
assert upval[0] == True
# check the disconnect callback is called upon setValue when being already connected
opa.Input1.connect(ops.Output1)
upval[0] = False
opa.Input1.disconnect()
opa.Input1.setValue(19)
assert upval[0] == True
def test_no_disconnect(self):
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
opa.Input1.notifyDisconnect(callBack)
# check the disconnect callback is not called when reconnecting to the same slot
opa.Input1.connect(ops.Output1)
upval[0] = False
opa.Input1.connect(ops.Output1)
assert upval[0] == False
# check the disconnect callback is not called when setting the same value again
opa.Input1.disconnect()
opa.Input1.setValue(10)
upval[0] = False
opa.Input1.setValue(10)
assert upval[0] == False
def test_unregister_disconnect(self):
# check that unregistering a disconnect callback works
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
opa.Input1.connect(ops.Output1)
opa.Input1.notifyDisconnect(callBack)
opa.Input1.unregisterDisconnect(callBack)
opa.Input1.disconnect()
assert upval[0] == False
class TestSlot_notifyMetaChanged(object):
def setUp(self):
self.g = Graph()
def test_inputslot_changed(self):
# test that the changed callback is called
# when the slot meta information changes
ops = OpS(graph=self.g)
opa = OpA(graph=self.g)
upval = [False]
def callBack(slot):
upval[0] = True
# test normal InputSlot
opa.Input1.notifyMetaChanged(callBack)
opa.Input1.connect(ops.Output1)
assert upval[0] == True
upval[0] = False
opa.Input1.connect(ops.Output2)
assert upval[0] == True
# test InputSlot with default value
upval[0] = False
opa.Input3.notifyMetaChanged(callBack)
opa.Input3.connect(ops.Output1)
assert upval[0] == True
upval[0] = False
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)