forked from AIM-Harvard/pyradiomics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addTest.py
78 lines (54 loc) · 2.47 KB
/
addTest.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
import argparse
import logging
import six
import radiomics
from radiomics import featureextractor
from testUtils import RadiomicsTestUtils
def main(argv=None):
logger = logging.getLogger('radiomics.addTest')
logger.setLevel(logging.INFO)
radiomics.setVerbosity(logging.INFO)
handler = logging.FileHandler(filename='testLog.txt', mode='w')
formatter = logging.Formatter("%(levelname)s:%(name)s: %(message)s")
handler.setFormatter(formatter)
radiomics.logger.addHandler(handler)
testcases = list(radiomics.testCases)
testcases += [t + '_2D' for t in testcases]
parser = argparse.ArgumentParser()
parser.add_argument('TestName', type=str, help='Name for the new test, must not be already present in the baseline')
parser.add_argument('TestCase', type=str, choices=testcases, help='Test image and segmentation to '
'use in the new test')
parser.add_argument('Configuration', metavar='FILE', default=None,
help='Parameter file containing the settings to be used in extraction')
parser.add_argument('--force', '-f', action='store_true', help='When the test is already known for the class, '
'overwrite it in stead of just skipping that class')
args = parser.parse_args(argv)
logger.info('Input accepted, starting addTest...')
try:
testutils = RadiomicsTestUtils()
try:
assert args.TestCase.lower().replace('_2d', '') in radiomics.testCases
except AssertionError:
logger.error('Input not valid, cancelling addTest!')
exit(1)
logger.debug('Initializing extractor')
extractor = featureextractor.RadiomicsFeatureExtractor(args.Configuration)
logger.debug('Starting extraction')
featurevector = extractor.execute(*radiomics.getTestCase(args.TestCase))
configuration = {}
baselines = {}
for k, v in six.iteritems(featurevector):
if 'diagnostics' in k:
configuration[k] = v
else:
image_filter, feature_class, feature_name = k.split('_')
if feature_class not in baselines:
baselines[feature_class] = {}
baselines[feature_class][k] = v
configuration['diagnostics_Configuration_TestCase'] = args.TestCase
testutils.addTest(args.TestName, configuration, baselines)
logger.info('addTest Done')
except Exception:
logger.error('Error running addTest!', exc_info=True)
if __name__ == '__main__':
main()