/
__init__.py
366 lines (302 loc) · 12 KB
/
__init__.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# #########################################################################
# Copyright (c) 2015, UChicago Argonne, LLC. All rights reserved. #
# #
# Copyright 2015. UChicago Argonne, LLC. This software was produced #
# under U.S. Government contract DE-AC02-06CH11357 for Argonne National #
# Laboratory (ANL), which is operated by UChicago Argonne, LLC for the #
# U.S. Department of Energy. The U.S. Government has rights to use, #
# reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR #
# UChicago Argonne, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR #
# ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is #
# modified to produce derivative works, such modified software should #
# be clearly marked, so as not to confuse it with the version available #
# from ANL. #
# #
# Additionally, redistribution and use in source and binary forms, with #
# or without modification, are permitted provided that the following #
# conditions are met: #
# #
# * Redistributions of source code must retain the above copyright #
# notice, this list of conditions and the following disclaimer. #
# #
# * Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in #
# the documentation and/or other materials provided with the #
# distribution. #
# #
# * Neither the name of UChicago Argonne, LLC, Argonne National #
# Laboratory, ANL, the U.S. Government, nor the names of its #
# contributors may be used to endorse or promote products derived #
# from this software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY UChicago Argonne, LLC AND CONTRIBUTORS #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT #
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS #
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UChicago #
# Argonne, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, #
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, #
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; #
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT #
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN #
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE #
# POSSIBILITY OF SUCH DAMAGE. #
# #########################################################################
"""
PyCTest driver functions
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import os
import sys
import glob
import shutil
import argparse
import platform
import warnings
import traceback
import subprocess as sp
import multiprocessing as mp
import pyctest.pyctest as pyctest
import pyctest.pycmake as pycmake
import pyctest.helpers as helpers
__version__ = '1.3.0'
__all__ = [
# variable section
'default_nitr',
'default_phantom_size',
# find section
'find_python_nosetest',
'find_python_coverage',
'find_ctest_token',
# build section
'build_option_append',
'build_name_append',
# create test section
'create_correct_module_test',
'create_nosetest_test',
'create_coverage_test',
'create_phantom_test',
'create_globus_test'
]
########################################################################################
#
# VARIABLE SECTION
#
########################################################################################
default_nitr = 10
""" if iterations is changed, test name will change """
default_phantom_size = 512
""" if phantom size is changed, test name will change """
########################################################################################
#
# FIND SECTION
#
########################################################################################
def find_python_nosetest():
"""
Find the python nosetests script
"""
pypath = os.path.dirname(pyctest.PYTHON_EXECUTABLE)
pyexe = helpers.FindExePath("nosetests", path=pypath)
if pyexe is None:
pyexe = helpers.FindExePath("nosetests")
return pyexe
def find_python_coverage():
"""
Find the python coverage script
"""
pypath = os.path.dirname(pyctest.PYTHON_EXECUTABLE)
pyexe = helpers.FindExePath("coverage", path=pypath)
if pyexe is None:
pyexe = helpers.FindExePath("coverage")
return pyexe
def find_ctest_token():
"""
Add authentification token if it exists
"""
token_string = os.environ.get("CTEST_TOKEN")
token_path = os.environ.get("CTEST_TOKEN_FILE")
home = helpers.GetHomePath()
if token_string is not None:
return
elif token_path is not None:
pyctest.set("CTEST_TOKEN_FILE", token_path)
elif home is not None:
for fname in ["nersc-tomopy", "nersc-cdash"]:
token_path = os.path.join(home, ".tokens", fname)
if os.path.exists(token_path):
pyctest.set("CTEST_TOKEN_FILE", token_path)
return
message = "Warning! No CTEST_TOKEN or CTEST_TOKEN_FILE set. Submission will fail"
warnings.warn(message)
########################################################################################
#
# COMMAND SECTION
#
########################################################################################
def build_option_append(check, var, val):
pyctest.BUILD_COMMAND += " -D{}={}".format(var, val) if check else ""
def build_name_append(item, separate=True, prefix="[", suffix="]", check=True, reset=False):
if reset:
pyctest.BUILD_NAME = ""
# if not list, check if is None
if item is None or not item:
return
if check:
pyctest.BUILD_NAME += " "
item = item.strip()
pyctest.BUILD_NAME += "{}".format(prefix)
pyctest.BUILD_NAME += "{}".format(item)
pyctest.BUILD_NAME += "{}".format(suffix) if separate else ""
if not separate:
pyctest.BUILD_NAME += "{}".format(suffix)
pyctest.BUILD_NAME += " "
pyctest.BUILD_NAME = pyctest.BUILD_NAME.strip()
while " " in pyctest.BUILD_NAME:
pyctest.BUILD_NAME = pyctest.BUILD_NAME.replace(" ", " ")
########################################################################################
#
# TEST SECTION
#
########################################################################################
def create_correct_module_test():
"""
Create a test that checks we are using the locally build module
"""
pyexe = pyctest.PYTHON_EXECUTABLE
binary_dir = pyctest.BINARY_DIRECTORY
# test properties
props = {
"WORKING_DIRECTORY": binary_dir,
"RUN_SERIAL": "ON",
"LABEL": "unit"
}
# test command
cmd = [pyexe, "-c",
"\"import os, sys, tomopy; " +
"print('using tomopy module: {}'.format(tomopy.__file__)); " +
"ret=0 if os.getcwd() in tomopy.__file__ else 1; " +
"sys.exit(ret)\""]
pyctest.test("correct_module", cmd, props)
def create_nosetest_test(args):
"""
Create a test that runs nosetests
"""
pyexe = pyctest.PYTHON_EXECUTABLE
binary_dir = pyctest.BINARY_DIRECTORY
pynosetest = find_python_nosetest()
pycoverage = find_python_coverage()
# test properties
props = {
"DEPENDS": "correct_module",
"RUN_SERIAL": "ON",
"LABEL": "unit",
"WORKING_DIRECTORY": binary_dir,
"ENVIRONMENT": "TOMOPY_USE_C_ALGORITHMS=1"
}
# test command: python $(which coverage) run $(which nosetest)
cmd = [pyexe, pycoverage, "run", pynosetest]
# create test
pyctest.test("nosetests", cmd, props)
def create_coverage_test(args):
"""
Create test that generates the coverage
"""
source_dir = pyctest.SOURCE_DIRECTORY
binary_dir = pyctest.BINARY_DIRECTORY
pycoverage = find_python_coverage()
# test properties
props = {
"WORKING_DIRECTORY": binary_dir,
"DEPENDS": "nosetests",
"RUN_SERIAL": "ON",
"LABEL": "unit"
}
# test command
cmd = [os.path.join(source_dir, ".coverage.sh"), source_dir]
if platform.system() == "Windows":
cmd = [pycoverage, "xml"]
pyctest.test("coverage", cmd, props)
def create_phantom_test(args, bench_props, phantom):
"""
Create test(s) for the specified algorithms
"""
pyexe = pyctest.PYTHON_EXECUTABLE
this_dir = os.path.dirname(__file__)
# skip when generating C coverage
if args.coverage or args.disable_phantom_tests:
return
nalgs = len(args.algorithms)
psize = args.phantom_size
# construct test name
name = ((phantom + "_") +
("".join(args.algorithms) if nalgs == 1 else "comparison"))
# determine phantom size
nsize = psize if phantom != "shepp3d" else psize // 2
# customize name
if psize != default_phantom_size:
name = "{}_pix{}".format(name, nsize)
if args.num_iter != default_nitr:
name = "{}_itr{}".format(name, args.num_iter)
# test arguments
test_args = [
"-A", "360",
"-f", "jpeg",
"-S", "1",
"-p", phantom,
"-s", "{}".format(nsize),
"-n", "{}".format(args.ncores),
"-i", "{}".format(args.num_iter),
"--output-dir", os.path.join(this_dir, name)
]
test_args.append("-a" if nalgs == 1 else "--compare")
test_args.extend(args.algorithms)
# test properties
test_props = bench_props
if phantom.lower() == "shepp3d":
test_props["RUN_SERIAL"] = "ON"
# test command
cmd = [pyexe, "-Om", "benchmarking.phantom"] + test_args
# create test
pyctest.test(name, cmd, properties=test_props)
def create_globus_test(args, bench_props, algorithm, phantom):
"""
Create a test from TomoBank (data provided by globus)
"""
# skip when generating C coverage when globus enabled
if args.coverage or args.globus_path is None:
return
pyexe = pyctest.PYTHON_EXECUTABLE
this_dir = os.path.dirname(__file__)
name = "{}_{}".format(phantom, algorithm)
# original number of iterations before num-iter added to test name
if args.num_iter != 10:
name = "{}_itr{}".format(name, args.num_iter)
global_args = [
"--type", "slice",
"-f", "jpeg",
"-S", "1",
"-c", "4",
"-n", "{}".format(args.ncores),
"-i", "{}".format(args.num_iter)
]
# try this path
h5file = os.path.join(args.globus_path, phantom + ".h5")
# alternative path
if not os.path.exists(h5file):
h5file = os.path.join(args.globus_path, phantom, phantom + ".h5")
# could not locate
if not os.path.exists(h5file):
print("HDF5 file '{}' does not exist.".format(h5file))
cmd = [pyexe, "-c",
"print(\"No valid path to '{}'\")".format(h5file)]
h5file = None
else:
cmd = [pyexe, "-Om", "benchmarking.rec", h5file]
cmd += (global_args +
["-a", algorithm,
"-o", os.path.join(this_dir, name)])
pyctest.test(name, cmd, properties=bench_props)