-
Notifications
You must be signed in to change notification settings - Fork 28
/
util.py
490 lines (393 loc) · 12.9 KB
/
util.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# Copyright (C) 2009-2022, Ecole Polytechnique Federale de Lausanne (EPFL) and
# Hospital Center and University of Lausanne (UNIL-CHUV), Switzerland, and CMP3 contributors
# All rights reserved.
#
# This software is distributed under the open-source license Modified BSD.
"""Module that defines CMTK Utility functions."""
import os
import warnings
from pathlib import Path
import json
import numpy as np
warnings.simplefilter("ignore")
def unicode2str(text): # pragma: no cover
"""Convert a unicode to a string using system's encoding.
Parameters
----------
text : bytes
Unicode bytes representation of a string
Returns
-------
out_str : str
Output string
"""
out_str = str(text)
return out_str
def isavailable(file): # pragma: no cover
"""Check if file is available and return the file if it is.
Used for debugging.
Parameters
----------
file : File
Input file
Returns
-------
file : File
Output file
"""
return file
def get_basename(path):
"""Return ``os.path.basename()`` of a ``path``.
Parameters
----------
path : os.path
Path to extract the containing directory
Returns
-------
path : os.path
Path to the containing directory
"""
from os import path as op # Important to be used as Nipype connect function
return op.basename(path)
class BColors:
"""Utility class for color unicode."""
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
def print_warning(message):
"""Print yellow-colored warning message
Parameters
----------
message : string
The string of the message to be printed
"""
print(BColors.WARNING + message + BColors.ENDC)
def print_error(message):
"""Print red-colored error message
Parameters
----------
message : string
The string of the message to be printed
"""
print(BColors.FAIL + message + BColors.ENDC)
def print_blue(message):
"""Print blue-colored message
Parameters
----------
message : string
The string of the message to be printed
"""
print(BColors.OKBLUE + message + BColors.ENDC)
def return_button_style_sheet(image, image_disabled=None, verbose=False): # pragma: no cover
"""Return Qt style sheet for QPushButton with image
Parameters
----------
image : string
Path to image to use as icon when button is enabled
image_disabled : string
Path to image to use as icon when button is disabled
verbose : Bool
Print the style sheet if True
Default: False
Returns
-------
button_style_sheet : string
Qt style sheet for QPushButton with image
"""
if image_disabled is None:
button_style_sheet = f"""
QPushButton {{
border-radius: 2px;
border-image: url({image}) 0 0 0 0;
color: transparent;
background-color: transparent;
font: 12pt "Verdana";
margin: 0px 0px 0px 0px;
padding:0px 0px;
}}
QPushButton:pressed {{
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #dadbde, stop: 1 #f6f7fa);
}}
"""
else:
button_style_sheet = f"""
QPushButton {{
border-radius: 2px;
border-image: url({image}) 0 0 0 0;
color: transparent;
background-color: transparent;
font: 12pt "Verdana";
margin: 0px 0px 0px 0px;
padding:0px 0px;
}}
QPushButton:disabled {{
border-radius: 2px;
border-image: url({image_disabled}) 0 0 0 0;
color: transparent;
background-color: transparent;
font: 12pt "Verdana";
margin: 0px 0px 0px 0px;
padding:0px 0px;
}}
QPushButton:pressed {{
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #dadbde, stop: 1 #f6f7fa);
}}
"""
if verbose:
print(button_style_sheet)
return button_style_sheet
def length(xyz, along=False):
"""Euclidean length of track line.
Parameters
----------
xyz : array-like shape (N,3)
array representing x,y,z of N points in a track
along : bool, optional
If True, return array giving cumulative length along track,
otherwise (default) return scalar giving total length.
Returns
-------
L : scalar or array shape (N-1,)
scalar in case of `along` == False, giving total length, array if
`along` == True, giving cumulative lengths.
Examples
--------
>>> xyz = np.array([[1,1,1],[2,3,4],[0,0,0]])
>>> expected_lens = np.sqrt([1+2**2+3**2, 2**2+3**2+4**2])
>>> length(xyz) == expected_lens.sum()
True
>>> len_along = length(xyz, along=True)
>>> np.allclose(len_along, expected_lens.cumsum())
True
>>> length([])
0
>>> length([[1, 2, 3]])
0
>>> length([], along=True)
array([0])
"""
xyz = np.asarray(xyz)
if xyz.shape[0] < 2:
if along:
return np.array([0])
return 0
dists = np.sqrt((np.diff(xyz, axis=0) ** 2).sum(axis=1))
if along:
return np.cumsum(dists)
return np.sum(dists)
def magn(xyz, n=1):
"""Returns the vector magnitude
Parameters
----------
xyz : vector
Input vector
n : int
Tile by `n` if `n>1` before return
"""
mag = np.sum(xyz ** 2, axis=1) ** 0.5
imag = np.where(mag == 0)
mag[imag] = np.finfo(float).eps
if n > 1:
return np.tile(mag, (n, 1)).T
return mag.reshape(len(mag), 1)
def mean_curvature(xyz):
"""Calculates the mean curvature of a curve.
Parameters
------------
xyz : array-like shape (N,3)
array representing x,y,z of N points in a curve
Returns
-----------
m : float
float representing the mean curvature
Examples
--------
Create a straight line and a semi-circle and print their mean curvatures
>>> from dipy.tracking import metrics as tm
>>> import numpy as np
>>> x=np.linspace(0,1,100)
>>> y=0*x
>>> z=0*x
>>> xyz=np.vstack((x,y,z)).T
>>> m=tm.mean_curvature(xyz) # mean curvature straight line
>>> theta=np.pi*np.linspace(0,1,100)
>>> x=np.cos(theta)
>>> y=np.sin(theta)
>>> z=0*x
>>> xyz=np.vstack((x,y,z)).T
>>> m=tm.mean_curvature(xyz) # mean curvature for semi-circle
"""
xyz = np.asarray(xyz)
n_pts = xyz.shape[0]
if n_pts == 0:
raise ValueError("xyz array cannot be empty")
dxyz = np.gradient(xyz)[0]
ddxyz = np.gradient(dxyz)[0]
# Curvature
k = magn(np.cross(dxyz, ddxyz), 1) / (magn(dxyz, 1) ** 3)
return np.mean(k)
def extract_freesurfer_subject_dir(reconall_report, local_output_dir=None, debug=False):
"""Extract Freesurfer subject directory from the report created by Nipype Freesurfer Recon-all node.
Parameters
----------
reconall_report : string
Path to the recon-all report
local_output_dir : string
Local output / derivatives directory
debug : bool
If `True`, show printed outputs
Returns
-------
fs_subject_dir : string
Freesurfer subject directory
"""
# Read rst report of a datasink node
with open(reconall_report) as fp:
line = fp.readline()
cnt = 1
while line:
if debug: # pragma: no cover
print("Line {}: {}".format(cnt, line.strip()))
# Extract line containing listing of node outputs
if "* subject_id : " in line:
fs_subject_dir = line.strip()
prefix = "* subject_id : "
fs_subject_dir = str.replace(fs_subject_dir, prefix, "")
if debug: # pragma: no cover
print(fs_subject_dir)
# Update from BIDS App /output_dir to local output directory
# specified by local_output_dir
if local_output_dir is not None:
fs_subject_dir = str.replace(
fs_subject_dir, "/output_dir", local_output_dir
)
break
line = fp.readline()
cnt += 1
return fs_subject_dir
def get_freesurfer_subject_id(file):
"""Extract Freesurfer subject ID from file generated by recon-all.
Parameters
----------
file : str
File generated by recon-all
Returns
-------
out : str
Freesurfer subject ID
"""
out = file[:-18]
return out
def extract_reconall_base_dir(file):
"""Extract Recon-all base directory from a file.
Parameters
----------
file : File
File generated by Recon-all
Returns
-------
out_path : string
Recon-all base directory
"""
# print("Extract reconall base dir : %s" % file[:-17])
out_path = str(file[:-17])
return out_path
def get_pipeline_dictionary_outputs(
datasink_report, local_output_dir=None, debug=False
): # pragma: no cover
"""Read the Nipype datasink report and return a dictionary of pipeline outputs.
Parameters
----------
datasink_report : string
Path to the datasink report
local_output_dir : string
Local output / derivatives directory
debug : bool
If `True`, print output dictionary
Returns
-------
dict_outputs : dict
Dictionary of pipeline outputs
"""
# Read rst report of a datasink node
with open(datasink_report) as fp:
while True:
line = fp.readline()
if not line:
break
# Extract line containing listing of node outputs and stop
if "_outputs :" in line:
str_outputs = line.strip()
prefix = "* _outputs : "
str_outputs = str.replace(str_outputs, prefix, "")
str_outputs = str.replace(str_outputs, "'", '"')
str_outputs = str.replace(str_outputs, "<undefined>", '""')
# Update from BIDS App /output_dir to local output directory
# specified by local_output_dir
if local_output_dir is not None:
str_outputs = str.replace(
str_outputs, "/output_dir", local_output_dir
)
break
# Convert the extracted JSON-structured string to a dictionary
dict_outputs = json.loads("{}".format(str_outputs))
if debug: # pragma: no cover
print("Dictionary of datasink outputs: {}".format(dict_outputs))
return dict_outputs
def convert_list_to_tuple(lists):
"""Convert list of files to tuple of files.
(Duplicated with preprocessing, could be moved to utils in the future)
Parameters
----------
lists : [bvecs, bvals]
List of files containing bvecs and bvals
Returns
-------
out_tuple : (bvecs, bvals)
Tuple of files containing bvecs and bvals
"""
return tuple(lists)
def check_directory_exists(mandatory_dir): # pragma: no cover
"""Makes sure the mandatory directory exists.
Raises
------
FileNotFoundError
Raised when the directory is not found.
"""
f_path = Path(mandatory_dir)
if not f_path.is_dir():
raise FileNotFoundError(f"No directory is found at: {str(f_path)}")
def find_toolbox_derivatives_containing_file(bids_dir, fname, debug=False):
"""Find the toolbox derivatives directory in the derivatives folder of the BIDS dataset containing a file.
This function is used by the EEGPipeline.
Parameters
----------
bids_dir : str
Path the BIDS root directory
fname : str
Filename to find
debug : bool
If `True`, print the directory found
Returns
-------
out_tuple : (bvecs, bvals)
Tuple of files containing bvecs and bvals
"""
deriv_dir = os.path.join(bids_dir, "derivatives")
for root, _, files in os.walk(deriv_dir, topdown=False):
for name in files:
if name == fname:
# Extract the name of the toolbox directory only
toolbox_derivatives_dirname = root.split("/derivatives/")[-1].split('/')[0]
if debug:
print(toolbox_derivatives_dirname)
return toolbox_derivatives_dirname
# Raise exception if no file is found
raise FileNotFoundError(f"No file {fname} was found in directory {deriv_dir}")