-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
264 lines (226 loc) · 9.4 KB
/
utils.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# -*- coding: utf-8
#
# Collection of useful functions used by other scripts
#
# Authors: Jan Valosek, Sandrine Bédard, Julien Cohen-Adad
#
import os
import re
import logging
import sys
import textwrap
import argparse
import subprocess
import shutil
import yaml
import numpy as np
import nibabel as nib
# BIDS utility tool
def fetch_subject_and_session(filename_path):
"""
Get subject ID, session ID and filename from the input BIDS-compatible filename or file path
The function works both on absolute file path as well as filename
:param filename_path: input nifti filename (e.g., sub-001_ses-01_T1w.nii.gz) or file path
(e.g., /home/user/MRI/bids/derivatives/labels/sub-001/ses-01/anat/sub-001_ses-01_T1w.nii.gz
:return: subjectID: subject ID (e.g., sub-001)
:return: sessionID: session ID (e.g., ses-01)
:return: filename: nii filename (e.g., sub-001_ses-01_T1w.nii.gz)
"""
_, filename = os.path.split(filename_path) # Get just the filename (i.e., remove the path)
subject = re.search('sub-(.*?)[_/]', filename_path)
subjectID = subject.group(0)[:-1] if subject else "" # [:-1] removes the last underscore or slash
session = re.findall(r'ses-..', filename_path)
sessionID = session[0] if session else "" # Return None if there is no session
contrast = 'dwi' if 'dwi' in filename_path else 'anat' # Return contrast (dwi or anat)
# REGEX explanation
# \d - digit
# \d? - no or one occurrence of digit
# *? - match the previous element as few times as possible (zero or more times)
return subjectID, sessionID, filename, contrast
class SmartFormatter(argparse.HelpFormatter):
"""
Custom formatter that inherits from HelpFormatter, which adjusts the default width to the current Terminal size,
and that gives the possibility to bypass argparse's default formatting by adding "R|" at the beginning of the text.
Inspired from: https://pythonhosted.org/skaff/_modules/skaff/cli.html
"""
def __init__(self, *args, **kw):
self._add_defaults = None
super(SmartFormatter, self).__init__(*args, **kw)
# Update _width to match Terminal width
try:
self._width = shutil.get_terminal_size()[0]
except (KeyError, ValueError):
logging.warning('Not able to fetch Terminal width. Using default: %s'.format(self._width))
# this is the RawTextHelpFormatter._fill_text
def _fill_text(self, text, width, indent):
# print("splot",text)
if text.startswith('R|'):
paragraphs = text[2:].splitlines()
rebroken = [textwrap.wrap(tpar, width) for tpar in paragraphs]
rebrokenstr = []
for tlinearr in rebroken:
if (len(tlinearr) == 0):
rebrokenstr.append("")
else:
for tlinepiece in tlinearr:
rebrokenstr.append(tlinepiece)
return '\n'.join(rebrokenstr) # (argparse._textwrap.wrap(text[2:], width))
return argparse.RawDescriptionHelpFormatter._fill_text(self, text, width, indent)
def _split_lines(self, text, width):
if text.startswith('R|'):
lines = text[2:].splitlines()
while lines[0] == '': # Discard empty start lines
lines = lines[1:]
offsets = [re.match("^[ \t]*", l).group(0) for l in lines]
wrapped = []
for i in range(len(lines)):
li = lines[i]
if len(li) > 0:
o = offsets[i]
ol = len(o)
init_wrap = textwrap.fill(li, width).splitlines()
first = init_wrap[0]
rest = "\n".join(init_wrap[1:])
rest_wrap = textwrap.fill(rest, width - ol).splitlines()
offset_lines = [o + wl for wl in rest_wrap]
wrapped = wrapped + [first] + offset_lines
else:
wrapped = wrapped + [li]
return wrapped
return argparse.HelpFormatter._split_lines(self, text, width)
def splitext(fname):
"""
Split a fname (folder/file + ext) into a folder/file and extension.
Note: for .nii.gz the extension is understandably .nii.gz, not .gz
(``os.path.splitext()`` would want to do the latter, hence the special case).
"""
dir, filename = os.path.split(fname)
for special_ext in ['.nii.gz', '.tar.gz']:
if filename.endswith(special_ext):
stem, ext = filename[:-len(special_ext)], special_ext
return os.path.join(dir, stem), ext
# If no special case, behaves like the regular splitext
stem, ext = os.path.splitext(filename)
return os.path.join(dir, stem), ext
def add_suffix(fname, suffix):
"""
Add suffix between end of file name and extension.
:param fname: absolute or relative file name. Example: t2.nii
:param suffix: suffix. Example: _mean
:return: file name with suffix. Example: t2_mean.nii
Examples:
- add_suffix(t2.nii, _mean) -> t2_mean.nii
- add_suffix(t2.nii.gz, a) -> t2a.nii.gz
"""
stem, ext = splitext(fname)
return os.path.join(stem + suffix + ext)
def remove_suffix(fname, suffix):
"""
Remove suffix between end of file name and extension.
:param fname: absolute or relative file name with suffix. Example: t2_mean.nii
:param suffix: suffix. Example: _mean
:return: file name without suffix. Example: t2.nii
Examples:
- remove_suffix(t2_mean.nii, _mean) -> t2.nii
- remove_suffix(t2a.nii.gz, a) -> t2.nii.gz
"""
stem, ext = splitext(fname)
return os.path.join(stem.replace(suffix, '') + ext)
def fetch_yaml_config(config_file):
"""
Fetch configuration from YAML file
:param config_file: YAML file
:return: dictionary with configuration
"""
config_file = get_full_path(config_file)
# Check if input yml file exists
if os.path.isfile(config_file):
fname_yml = config_file
else:
sys.exit("ERROR: Input yml file {} does not exist or path is wrong.".format(config_file))
# Fetch input yml file as dict
with open(fname_yml, 'r') as stream:
try:
dict_yml = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return dict_yml
def curate_dict_yml(dict_yml):
"""
Curate dict_yml to only have filenames instead of absolute path
:param dict_yml: dict: input yml file as dict
:return: dict_yml_curate
"""
dict_yml_curate = {}
for task, files in dict_yml.items():
dict_yml_curate[task] = [os.path.basename(file) for file in files]
return dict_yml_curate
def get_full_path(path):
"""
Return full path. If ~ is passed, expand it to home directory.
:param path: str: Input path
:return: str: Full path
"""
return os.path.abspath(os.path.expanduser(path))
def check_files_exist(dict_files, path_data):
"""
Check if all files listed in the input dictionary exist
:param dict_files:
:param path_data: folder where BIDS dataset is located
:return:
"""
missing_files = []
for task, files in dict_files.items():
if files is not None:
for file in files:
subject, ses, filename, contrast = fetch_subject_and_session(file)
fname = os.path.join(path_data, subject, ses, contrast, filename)
if not os.path.exists(fname):
missing_files.append(fname)
if missing_files:
logging.error("The following files are missing: \n{}. \nPlease check that the files listed "
"in the yaml file and the input path are correct.".format(missing_files))
def check_output_folder(path_bids, folder_derivatives):
"""
Make sure path exists, has writing permissions, and create derivatives folder if it does not exist.
:param path_bids:
:return: folder_derivatives:
"""
if path_bids is None:
logging.error("-path-out should be provided.")
if not os.path.exists(path_bids):
logging.error("Output path does not exist: {}".format(path_bids))
path_bids_derivatives = os.path.join(path_bids, folder_derivatives)
os.makedirs(path_bids_derivatives, exist_ok=True)
return path_bids_derivatives
def check_software_installed(list_software=['sct']):
"""
Make sure software are installed
:param list_software: {'sct'}
:return:
"""
install_ok = True
software_cmd = {
'sct': 'sct_version'
}
logging.info("Checking if required software are installed...")
for software in list_software:
try:
output = subprocess.check_output(software_cmd[software], shell=True)
logging.info("'{}' (version: {}) is installed.".format(software, output.decode('utf-8').strip('\n')))
except:
logging.error("'{}' is not installed. Please install it before using this program.".format(software))
install_ok = False
return install_ok
def create_empty_mask(fname, fname_label):
"""
Create empty mask from reference image
:param fname: path to reference image
:param fname_label: path to output mask under derivatives
"""
img = nib.load(fname)
data = np.zeros(img.shape)
img_mask = nib.Nifti1Image(data, img.affine, img.header)
nib.save(img_mask, fname_label)
print("Empty mask created: {}".format(fname_label))