-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (110 loc) · 3.93 KB
/
main.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
"""Run HRCT PRM + topoligcal mapping pipeline."""
import argparse
import glob
import logging
import time
from configparser import ConfigParser
from os.path import join
from subject_classmap import Subject
# set logging level to that logging.info statements are printed
logging.basicConfig(level=logging.INFO)
# set command line flags/args
parser = argparse.ArgumentParser(description="Run HRCT voxel-wise lung image analysis")
parser.add_argument(
"--config",
type=str,
metavar="",
required=True,
help="single subject: config file path. batch processing: directory of config files",
)
parser.add_argument(
"--batch",
action="store_true",
required=False,
help="indicate to run a batch process",
)
parser.add_argument(
"--glbl",
action="store_true",
required=False,
help="indicate to calc only prm and global topology",
)
args = parser.parse_args()
def processSubject(config, args):
"""Process a single subject from config file.
Args:
config: configuration file containing input/ouput file locations
args: ArgumentParser object containing command line flags
"""
# record start time for processing subject
t1 = time.perf_counter()
subject = Subject(config)
logging.info("*****Processing subject %s*****" % subject.subjID)
if config.has_option("io", "inFilePrm"):
# if PRM file specified in config, read in PRM map
logging.info("Reading in PRM map")
subject.readPrmFile()
if config.has_option("io", "inFileMask"):
# if mask file specified in config, use it
subject.readMaskFile()
else:
# if nomask file in config, generate mask from binned regions in prm map
logging.info(
"No mask file in config, generating mask from binned voxels in input PRM"
)
subject.genMaskFromPrm()
elif config.has_option("io", "inFileExp"):
# if HRCT file specified in config, generate PRM maps from HRCT
# generate PRM maps
logging.info("Generating PRM maps")
subject.readCtFiles()
subject.applyMedFilts()
subject.excludeVoxels()
subject.classifyVoxelsPrm()
# save PRM maps, calculate PRM stats, and plot representative slice of PRM map
subject.savePrmNiis()
subject.calcPrmStats()
subject.plotPrmColor()
# calculate global topology metrics
logging.info("Calculating global topology metrics")
subject.genDictOfImageArrays()
subject.calcTopologyGlobal()
subject.saveTopologyStats()
if not args.glbl:
# if 'glbl' flag not specified, process local topology
# generate local PRM topology maps
logging.info("Generating PRM topology maps")
subject.genLocalTopoMaps()
subject.saveTopoNiis()
subject.calcMeanLocalTopoStats()
subject.plotTopoColor()
# save topology stats
subject.saveTopologyStats()
logging.info("Program complete")
# record end time for processing subject
t2 = time.perf_counter()
elapsedTime = (t2 - t1) / 60
logging.info(f"Program runtime: {elapsedTime} mins")
def main():
"""Run PRM and topological mapping HRCT analysis.
If no batch processing indicated, run analysis with single config file.
If batch processing indicated, run analysis on each config file in specified directory.
"""
if not args.batch:
# read in config file
config = ConfigParser()
config.read(args.config)
# process subject
processSubject(config, args)
else:
# get list of config files in specified directory
configList = glob.glob(join(args.config, "*.ini"))
# loop over config files
for configDir in configList:
# read in config file
config = ConfigParser()
config.read(configDir)
# process subject
processSubject(config, args)
if __name__ == "__main__":
main()