generated from FNNDSC/python-chrisapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bichamfer.py
74 lines (59 loc) · 2.39 KB
/
bichamfer.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
#!/usr/bin/env python
import itertools
import os
import shlex
import subprocess as sp
from argparse import ArgumentParser, Namespace, ArgumentDefaultsHelpFormatter
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from chris_plugin import chris_plugin, PathMapper
from loguru import logger
__version__ = '1.0.1'
DISPLAY_TITLE = r"""
_ _ _ _ __
| | | | (_) | | / _|
_ __ | |______| |__ _ ___| |__ __ _ _ __ ___ | |_ ___ _ __
| '_ \| |______| '_ \| |/ __| '_ \ / _` | '_ ` _ \| _/ _ \ '__|
| |_) | | | |_) | | (__| | | | (_| | | | | | | || __/ |
| .__/|_| |_.__/|_|\___|_| |_|\__,_|_| |_| |_|_| \___|_|
| |
|_|
"""
parser = ArgumentParser(description='Create radial distance maps of masks using mincchamfer',
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-p', '--pattern', default='**/*.mnc', type=str,
help='input file filter glob')
parser.add_argument('-b', '--boundary', default=10.0, type=float,
help='boundary distance 0 value')
parser.add_argument('-V', '--version', action='version',
version=f'%(prog)s {__version__}')
@chris_plugin(
parser=parser,
title='Radial Distance Map',
category='MRI Processing',
min_memory_limit='1Gi',
min_cpu_limit='1000m',
min_gpu_limit=0
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
print(DISPLAY_TITLE, flush=True)
# if output directory is the same as input directory,
# then it's necessary to rename output files.
mo = {}
if inputdir == outputdir:
mo['suffix'] = '.chamfer.mnc'
mapper = PathMapper.file_mapper(inputdir, outputdir, glob=options.pattern, **mo)
input_files, output_files = zip(*mapper)
proc = len(os.sched_getaffinity(0))
logger.debug('Using {} threads', proc)
with ThreadPoolExecutor(max_workers=proc) as pool:
results = pool.map(bichamfer, input_files, output_files, itertools.repeat(options.boundary))
# raise any exceptions which occurred
for _ in results:
pass
def bichamfer(mask: os.PathLike, chamfer: os.PathLike, boundary: float):
cmd = ('chamfer.sh', '-c', str(boundary), mask, chamfer)
logger.info(shlex.join(map(str, cmd)))
sp.run(cmd, check=True)
if __name__ == '__main__':
main()