forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestOpStreamingHdf5SequenceReaderS.py
210 lines (169 loc) · 8.61 KB
/
testOpStreamingHdf5SequenceReaderS.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
import unittest
import contextlib
import tempfile
import shutil
import os
import h5py
import numpy
from lazyflow.graph import Graph
from lazyflow.operators.ioOperators import OpStreamingHdf5SequenceReaderS
import vigra
@contextlib.contextmanager
def tempdir():
d = tempfile.mkdtemp()
yield d
shutil.rmtree(d)
class TestOpStreamingHdf5SequenceReader(unittest.TestCase):
def setUp(self):
self.graph = Graph()
def test_2d_vigra_along_z(self):
"""Test if 2d files generated through vigra are recognized correctly"""
# Prepare some data set for this case
data = numpy.random.randint(0, 255, (20, 100, 200, 3)).astype(numpy.uint8)
axistags = vigra.defaultAxistags('yxc')
expected_axistags = vigra.defaultAxistags('zyxc')
op = OpStreamingHdf5SequenceReaderS(graph=self.graph)
with tempdir() as d:
try:
testDataFileName = '{}/test.h5'.format(d)
# Write the dataset to an hdf5 file
# (Note: Don't use vigra to do this, which may reorder the axes)
h5File = h5py.File(testDataFileName)
try:
h5File.create_group('volumes')
internalPathString = "subvolume-{sliceIndex:02d}"
for sliceIndex, zSlice in enumerate(data):
subpath = internalPathString.format(sliceIndex=sliceIndex)
h5File['volumes'].create_dataset(subpath, data=zSlice)
# Write the axistags attribute
current_path = 'volumes/{}'.format(subpath)
h5File[current_path].attrs['axistags'] = axistags.toJSON()
finally:
h5File.close()
# Read the data with an operator
hdf5GlobString = "{}/volumes/subvolume-*".format(testDataFileName)
op.SequenceAxis.setValue('z')
op.GlobString.setValue(hdf5GlobString)
assert op.OutputImage.ready()
assert op.OutputImage.meta.axistags == expected_axistags
assert (op.OutputImage[5:10, 50:100, 100:150].wait() == data[5:10, 50:100, 100:150]).all()
finally:
op.cleanUp()
def test_2d_vigra_along_t(self):
"""Test if 2d files generated through vigra are recognized correctly"""
# Prepare some data set for this case
data = numpy.random.randint(0, 255, (20, 100, 200, 3)).astype(numpy.uint8)
axistags = vigra.defaultAxistags('yxc')
expected_axistags = vigra.defaultAxistags('tyxc')
op = OpStreamingHdf5SequenceReaderS(graph=self.graph)
with tempdir() as d:
try:
testDataFileName = '{}/test.h5'.format(d)
# Write the dataset to an hdf5 file
# (Note: Don't use vigra to do this, which may reorder the axes)
h5File = h5py.File(testDataFileName)
try:
h5File.create_group('volumes')
internalPathString = "subvolume-{sliceIndex:02d}"
for sliceIndex, zSlice in enumerate(data):
subpath = internalPathString.format(sliceIndex=sliceIndex)
h5File['volumes'].create_dataset(subpath, data=zSlice)
# Write the axistags attribute
current_path = 'volumes/{}'.format(subpath)
h5File[current_path].attrs['axistags'] = axistags.toJSON()
finally:
h5File.close()
# Read the data with an operator
hdf5GlobString = "{}/volumes/subvolume-*".format(testDataFileName)
op.SequenceAxis.setValue('t')
op.GlobString.setValue(hdf5GlobString)
assert op.OutputImage.ready()
assert op.OutputImage.meta.axistags == expected_axistags
assert (op.OutputImage[5:10, 50:100, 100:150].wait() == data[5:10, 50:100, 100:150]).all()
finally:
op.cleanUp()
def test_3d_vigra_along_t(self):
"""Test if 3d volumes generated through vigra are recognized correctly"""
# Prepare some data set for this case
data = numpy.random.randint(0, 255, (10, 15, 50, 100, 3)).astype(numpy.uint8)
axistags = vigra.defaultAxistags('zyxc')
expected_axistags = vigra.defaultAxistags('tzyxc')
op = OpStreamingHdf5SequenceReaderS(graph=self.graph)
with tempdir() as d:
try:
testDataFileName = '{}/test.h5'.format(d)
# Write the dataset to an hdf5 file
# (Note: Don't use vigra to do this, which may reorder the axes)
h5File = h5py.File(testDataFileName)
try:
h5File.create_group('volumes')
internalPathString = "subvolume-{sliceIndex:02d}"
for sliceIndex, tSlice in enumerate(data):
subpath = internalPathString.format(sliceIndex=sliceIndex)
h5File['volumes'].create_dataset(subpath, data=tSlice)
# Write the axistags attribute
current_path = 'volumes/{}'.format(subpath)
h5File[current_path].attrs['axistags'] = axistags.toJSON()
finally:
h5File.close()
# Read the data with an operator
hdf5GlobString = "{}/volumes/subvolume-*".format(testDataFileName)
op.SequenceAxis.setValue('t')
op.GlobString.setValue(hdf5GlobString)
assert op.OutputImage.ready()
assert op.OutputImage.meta.axistags == expected_axistags
assert (op.OutputImage[0:2, 5:10, 20:50, 40:70].wait() ==
data[0:2, 5:10, 20:50, 40:70]).all()
finally:
op.cleanUp()
def test_globStringValidity(self):
"""Check whether globStrings are correctly verified"""
testGlobString = '/tmp/test.h5'
with self.assertRaises(OpStreamingHdf5SequenceReaderS.NoInternalPlaceholderError):
OpStreamingHdf5SequenceReaderS.checkGlobString(testGlobString)
testGlobString = '/tmp/test.h5/a'+os.pathsep+'/tmp/test2.h5/a'
with self.assertRaises(OpStreamingHdf5SequenceReaderS.NotTheSameFileError):
OpStreamingHdf5SequenceReaderS.checkGlobString(testGlobString)
testGlobString = '/tmp/test*.h5/a'+os.pathsep+'/tmp/test*.h5/a'
with self.assertRaises(OpStreamingHdf5SequenceReaderS.ExternalPlaceholderError):
OpStreamingHdf5SequenceReaderS.checkGlobString(testGlobString)
testGlobString = '/tmp/test.jpg/*'
with self.assertRaises(OpStreamingHdf5SequenceReaderS.WrongFileTypeError):
OpStreamingHdf5SequenceReaderS.checkGlobString(testGlobString)
validGlobStrings = [
'/tmp/test.h5/*',
'/tmp/test.h5/data1'+os.pathsep+'/tmp/test.h5/data2',
'/tmp/test.h5/data*'
]
# Implicit test for validity; test fails if an exception is raised
for testGlobString in validGlobStrings:
OpStreamingHdf5SequenceReaderS.checkGlobString(testGlobString)
self.assertTrue(True)
def test_expandGlobStrings(self):
expected_datasets = ['g1/g2/data2', 'g1/g2/data3']
with tempdir() as d:
file_name = '{}/test.h5'.format(d)
try:
f = h5py.File(file_name, mode='w')
g1 = f.create_group('g1')
g2 = g1.create_group('g2')
g3 = f.create_group('g3')
g1.create_dataset('data1', data=numpy.ones((10, 10)))
g2.create_dataset('data2', data=numpy.ones((10, 10)))
g2.create_dataset('data3', data=numpy.ones((10, 10)))
g3.create_dataset('data4', data=numpy.ones((10, 10)))
f.flush()
glob_res1 = OpStreamingHdf5SequenceReaderS.expandGlobStrings(
f, '{}/g1/g2/data*'.format(file_name))
self.assertEqual(glob_res1, expected_datasets)
finally:
f.close()
glob_res2 = OpStreamingHdf5SequenceReaderS.expandGlobStrings(
file_name, '{}/g1/g2/data*'.format(file_name))
self.assertEqual(glob_res2, expected_datasets)
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.
nose.run(defaultTest=__file__)