generated from FNNDSC/python-chrisapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdiv_mask.py
163 lines (134 loc) · 5.58 KB
/
subdiv_mask.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
#!/usr/bin/env python
import os
import sys
import subprocess as sp
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Union, Tuple, List, Optional, Sequence
from loguru import logger
from chris_plugin import chris_plugin, PathMapper
__version__ = '1.0.0'
DISPLAY_TITLE = r"""
_ _ _ _ _
| | | | | (_) (_)
_ __ | |______ ___ _ _| |__ __| |___ ________ _ __ ___ _ _ __ ___
| '_ \| |______/ __| | | | '_ \ / _` | \ \ / /______| '_ ` _ \| | '_ \ / __|
| |_) | | \__ \ |_| | |_) | (_| | |\ V / | | | | | | | | | | (__
| .__/|_| |___/\__,_|_.__/ \__,_|_| \_/ |_| |_| |_|_|_| |_|\___|
| |
|_|
"""
parser = ArgumentParser(description='A ChRIS plugin wrapper around mincresample '
'for increasing the resolution of mask images.',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-d', '--divisions', type=float, default=2.0,
help='number of cuts along voxel edge. Float values are accepted, mincresample '
'performs interpolation.')
parser.add_argument('-o', '--options', type=str, default='-tricubic',
help='Additional options to pass to mincresample as space-separated list, e.g.'
' specify interpolation as -tricubic or -trilinear')
parser.add_argument('-n', '--no-binarize', dest='binarize', action='store_false',
help='Skip extra step minccalc -expr A[0]>0.5, '
'allowing for floating point values')
parser.add_argument('-t', '--threads', type=int, default=0,
help='number of threads to use (pass 0 to use number of visible CPU cores)')
parser.add_argument('--no-fail', dest='no_fail', action='store_true',
help='Exit normally even when failed to process a subject')
parser.add_argument('-p', '--pattern', default='**/*.mnc', type=str,
help='input file filter glob')
parser.add_argument('-V', '--version', action='version',
version=f'%(prog)s {__version__}')
@chris_plugin(
parser=parser,
title='Subdivide Masks',
category='MRI Processing',
min_memory_limit='2Gi',
min_cpu_limit='1000m',
min_gpu_limit=0
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
print(DISPLAY_TITLE, file=sys.stderr, flush=True)
def curried_resample(t: tuple[Path, Path]):
resample(
*t,
divisions=options.divisions,
binarize=options.binarize,
verbose=False,
options=ssv_str(options.options)
)
logger.info('{} -> {}', *t)
suffix = __suffix(options.divisions, options.pattern)
mapper = PathMapper.file_mapper(inputdir, outputdir, glob=options.pattern, suffix=suffix)
proc = len(os.sched_getaffinity(0)) if options.threads <= 0 else options.threads
with ThreadPoolExecutor(max_workers=proc) as pool:
results = pool.map(curried_resample, mapper)
if not options.no_fail:
for _ in results:
pass
def __suffix(divisions: float, pattern: str) -> str:
ext = pattern.split('.')[-1]
if divisions - int(divisions) < 0.001:
d = int(divisions)
else:
d = divisions
return f'.subdiv.{d}.{ext}'
SPACES = ('xspace', 'yspace', 'zspace')
@dataclass(frozen=True)
class MincInfo:
"""
Dimensional information of 3D volume. 3-tuples are always in the order of
(xspace, yspace, zspace)
"""
length: Tuple[int, int, int]
step: Tuple[int, int, int]
@dataclass(frozen=True)
class MincFile:
fname: str
def dimlength(self, dim: str) -> int:
cmd = ['mincinfo', '-dimlength', dim, self.fname]
length = sp.check_output(cmd, text=True)
return int(length)
def step(self, dim: str) -> float:
cmd = ['mincinfo', '-attvalue', f'{dim}:step', self.fname]
step = sp.check_output(cmd, text=True)
return float(step)
def mincinfo(self) -> MincInfo:
# noinspection PyTypeChecker
return MincInfo(
length=tuple(map(self.dimlength, SPACES)),
step=tuple(map(self.step, SPACES))
)
def resample(input_file: Union[str, os.PathLike], output_file: Union[str, os.PathLike], divisions: float,
binarize: bool = True,
verbose: bool = False, options: Optional[Sequence[str]] = None) -> None:
"""
Wrapper for ``mincresample``.
"""
info = MincFile(input_file).mincinfo()
quiet_flag = [] if verbose else ['-quiet']
cmd = [
'mincresample',
*quiet_flag,
'-nelements',
*(str(int(divisions * l)) for l in info.length),
'-step',
*(str(s / divisions) for s in info.step),
*(options if options else []),
input_file,
output_file
]
sp.run(cmd, check=True)
if binarize:
tmp = str(output_file) + '.binarized.mnc'
cmd = ['minccalc', *quiet_flag, '-unsigned', '-byte', '-expression', 'A[0]>0.5', output_file, tmp]
sp.run(cmd, check=True)
os.rename(tmp, output_file)
def ssv_str(s: str) -> List[str]:
"""
Parse a space-separated list of strings.
"""
return s.strip().split()
if __name__ == '__main__':
main()