-
Notifications
You must be signed in to change notification settings - Fork 41
/
utils.py
136 lines (111 loc) · 3.54 KB
/
utils.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
# Copyright 2021 United Kingdom Research and Innovation
# Copyright 2021 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
# CIL Developers, listed at: https://github.com/TomographicImaging/CIL/blob/master/NOTICE.txt
import os
import subprocess
import sys
import importlib
import logging
import cil.version
import pprint
import unittest
# initialise_tests should be imported and called in each test file to change default behaviour
# it must be in each file as each may get run as an independent processes
def startTestRun(self):
"""Called once before any tests are executed.
"""
#set logging
logging.basicConfig(level=logging.WARNING)
print("\n----------------------------------------------------------------------")
print("TEST SYSTEM CONFIGURATION")
print("CIL version: ", cil.version.version)
pprint.pprint(system_state)
print("----------------------------------------------------------------------\n")
sys.stdout.flush()
def initialise_tests():
"""
Updates the unittests.TestResults.StartTestRun method with our version.
"""
setattr(unittest.TestResult, 'startTestRun', startTestRun)
# system state dictionary to be populated
system_state = {}
#nvidia
try:
subprocess.check_output('nvidia-smi')
has_nvidia = True
except:
if os.environ.get("TESTS_FORCE_GPU", ""):
raise ImportError
has_nvidia = False
system_state['has_nvidia']=has_nvidia
#astra
module_info = importlib.util.find_spec("astra")
if module_info is None:
if os.environ.get("TESTS_FORCE_GPU", ""):
raise ImportError
has_astra = False
else:
has_astra = True
system_state['has_astra']=has_astra
#tigre
module_info = importlib.util.find_spec("tigre")
if module_info is None:
if os.environ.get("TESTS_FORCE_GPU", ""):
raise ImportError
has_tigre = False
else:
has_tigre = True
system_state['has_tigre']=has_tigre
#cvxpy
module_info = importlib.util.find_spec("cvxpy")
if module_info is None:
has_cvxpy = False
else:
has_cvxpy = True
system_state['has_cvxpy']=has_cvxpy
#ipp
from cil.framework import cilacc
has_ipp = hasattr(cilacc, 'filter_projections_avh')
system_state['has_ipp']=has_ipp
#ccpi-regularisation toolkit
module_info = importlib.util.find_spec("ccpi")
if module_info != None:
module_info = importlib.util.find_spec("ccpi.filters.regularisers")
if module_info is None:
has_ccpi_regularisation = False
else:
has_ccpi_regularisation = True
system_state['has_ccpi_regularisation']= has_ccpi_regularisation
#tomophantom
module_info = importlib.util.find_spec("tomophantom")
if module_info is None:
has_tomophantom = False
else:
has_tomophantom = True
system_state['has_tomophantom']= has_tomophantom
#has_numba
module_info = importlib.util.find_spec("numba")
if module_info is None:
has_numba = False
else:
has_numba = True
system_state['has_numba']= has_numba
# to disable prints from 3rd part libraries and tests
def disable_print():
sys.stdout = open(os.devnull, 'w')
def enable_prints():
sys.stdout = sys.__stdout__