generated from FNNDSC/python-chrisapp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
niis2mncs.py
executable file
·129 lines (102 loc) · 4.15 KB
/
niis2mncs.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
#!/usr/bin/env python
import itertools
import os
import shlex
import shutil
import subprocess as sp
import sys
from argparse import ArgumentParser, Namespace
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Optional
from chris_plugin import chris_plugin, PathMapper
from loguru import logger
parser = ArgumentParser(description='ChRIS ds plugin wrapper around nii2mnc')
parser.add_argument('-p', '--pattern', default='**/*.nii,**/*.nii.gz',
help='pattern for file names to include')
parser.add_argument('-u', '--unsigned', action='store_true',
help='Write integer voxel data in unsigned format.')
parser.add_argument('-b', '--byte', action='store_true',
help='Write voxel data in 8-bit integer format.')
parser.add_argument('-r', '--rename',
help='Rename output files.')
def niigz2mnc(niigz: Path, mnc: Path, flags: list[str], log_prefix='') -> tuple[str, Optional[Path], int]:
"""
If input file is ``.gz`` compressed, copy it to a temporary location and run ``gzip -d``
before calling `nii2mnc`. Descriptions of these actions are added to the accumulator ``log_prefix``.
"""
if niigz.suffix != '.gz':
return nii2mnc(niigz, mnc, flags, log_prefix)
# copy NIFTI to temporary location
temp_niigz = temp_file('.nii.gz')
shutil.copy(niigz, temp_niigz)
# decompress NIFTI
cmd = ['gzip', '-d', temp_niigz]
cmd_str = shlex.join(cmd)
p = sp.run(cmd)
if p.returncode != 0:
return shlex.join(cmd), None, p.returncode
# check decompressed NIFTI exists
created_nii = Path(temp_niigz[:-len('.gz')])
if not created_nii.is_file():
log_file = mnc.with_suffix('.gzip.log')
log_file.write_text(f'expected {created_nii} to be created, but it was not')
return cmd_str, log_file, 0
logs_so_far = [
shlex.join(['cp', str(niigz), temp_niigz]),
cmd_str,
''
]
# call nii2mnc by calling niigz2mnc recursively
ret = niigz2mnc(created_nii, mnc, flags, log_prefix=' && '.join(logs_so_far))
# clean up intermediary decompressed NIFTI
created_nii.unlink()
return ret
def nii2mnc(nii: Path, mnc: Path, flags: list[str], log_prefix='') -> tuple[str, Path, int]:
log_file = mnc.with_suffix('.nii2mnc.log')
cmd = ['nii2mnc', *flags, str(nii), str(mnc)]
cmd_str = shlex.join(cmd)
logger.info('{}{}', log_prefix, cmd_str)
with log_file.open('wb') as out:
p = sp.run(cmd, stdout=out, stderr=out)
return cmd_str, log_file, p.returncode
def temp_file(suffix: str) -> str:
with NamedTemporaryFile(suffix=suffix) as temp:
return temp.name
def name_mapper(file: Path, outputdir: Path, rename: Optional[str]):
if file.suffix == '.gz':
return name_mapper(file.with_suffix(''), outputdir, rename)
output_path = outputdir / file.with_suffix('.mnc')
if rename is None:
return output_path
name = rename.replace('{}', output_path.name)
return output_path.with_name(name)
def __curry_name_mapper(rename: Optional[str]):
return lambda f, o: name_mapper(f, o, rename)
# documentation: https://fnndsc.github.io/chris_plugin/
@chris_plugin(
parser=parser,
title='nii2mnc',
category='MRI Processing',
min_memory_limit='500Mi',
min_cpu_limit='1000m'
)
def main(options: Namespace, inputdir: Path, outputdir: Path):
flags = ['-quiet']
if options.unsigned:
flags.append('-unsigned')
if options.byte:
flags.append('-byte')
name_mapper_func = __curry_name_mapper(options.rename)
mapper = PathMapper.file_mapper(inputdir, outputdir, glob=options.pattern.split(','), name_mapper=name_mapper_func)
with ThreadPoolExecutor(max_workers=len(os.sched_getaffinity(0))) as pool:
results = pool.map(lambda t, f: niigz2mnc(*t, f), mapper, itertools.repeat(flags))
any_failed = False
for cmd, log_file, rc in filter(lambda t: t[2] != 0, results):
any_failed = True
logger.error('FAILED: {} > {} ({})', cmd, log_file, rc)
if any_failed:
sys.exit(1)
if __name__ == '__main__':
main()