forked from ilastik/lazyflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestOpDvidRoi.py
111 lines (98 loc) · 4.21 KB
/
testOpDvidRoi.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
from future import standard_library
standard_library.install_aliases()
###############################################################################
# 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 os
import http.client
import json
import shutil
import tempfile
import unittest
import platform
import numpy
import vigra
import h5py
from lazyflow.graph import Graph
from lazyflow.roi import roiToSlice
try:
from lazyflow.operators.ioOperators import OpDvidRoi
from libdvid import DVIDConnection, ConnectionMethod, DVIDNodeService
from libdvid.voxels import VoxelsMetadata
TEST_DVID_SERVER = os.getenv("TEST_DVID_SERVER", "127.0.0.1:8000")
def get_testrepo_root_uuid():
connection = DVIDConnection(TEST_DVID_SERVER)
status, body, error_message = connection.make_request( "/repos/info", ConnectionMethod.GET)
assert status == http.client.OK, "Request for /repos/info returned status {}".format( status )
assert error_message == ""
repos_info = json.loads(body)
test_repos = [uuid_repo_info for uuid_repo_info in list(repos_info.items()) if uuid_repo_info[1] and uuid_repo_info[1]['Alias'] == 'testrepo']
if test_repos:
uuid = test_repos[0][0]
return str(uuid)
else:
from libdvid import DVIDServerService
server = DVIDServerService(TEST_DVID_SERVER)
uuid = server.create_new_repo("testrepo", "This repo is for unit tests to use and abuse.");
return str(uuid)
except ImportError:
have_dvid = False
else:
have_dvid = True
class TestOpDvidRoi(unittest.TestCase):
@classmethod
@unittest.skipIf(not have_dvid, "optional module libdvid not available.")
@unittest.skipIf(platform.system() == "Windows", "DVID not tested on Windows. Skipping.")
def setUpClass(cls):
"""
Override. Called by nosetests.
"""
cls.uuid = get_testrepo_root_uuid()
cls.roi_name = "OpDvidRoi_test_roi"
node_service = DVIDNodeService(TEST_DVID_SERVER, cls.uuid)
node_service.create_roi(cls.roi_name)
# Create an upside-down L-shaped roi, something like this:
#
# 1 1 1 1
# 1 1 1 1
# 1 1 0 0
# 1 1 0 0
cls.expected_data = numpy.ones((64,128,128), dtype=numpy.uint8, order='C')
cls.expected_data[:, 64:, 64:] = 0
block_values = cls.expected_data[::32,::32,::32]
coords = numpy.transpose(numpy.nonzero(block_values))
node_service.post_roi(cls.roi_name, coords)
def test(self):
# Retrieve from server
graph = Graph()
opRoi = OpDvidRoi( TEST_DVID_SERVER, self.uuid, self.roi_name, graph=graph )
roi_vol = opRoi.Output( (0,0,0), self.expected_data.shape ).wait()
assert (roi_vol == self.expected_data).all()
# Test a non-aligned roi
subvol = ((30,60,50), (40, 70, 70))
roi_vol = opRoi.Output( *subvol ).wait()
assert (roi_vol == self.expected_data[roiToSlice(*subvol)]).all()
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__)