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 | # -*- coding: utf-8 -*-
import csv
import logging
import os
import datetime
from pathlib import Path
from subprocess import Popen, PIPE
from dcm2bids.version import __version__, __BIDSversion__
from dcm2bids.utils.schema import load_schema
class DEFAULT(object):
""" Default values of the package"""
doc = "Documentation at https://unfmontreal.github.io/Dcm2Bids/"
link_bids_validator = "https://github.com/bids-standard/bids-validator#quickstart"
link_doc_intended_for = f"https://unfmontreal.github.io/Dcm2Bids/{__version__}/tutorial/first-steps/#populating-the-config-file" # noqa: E501
# cli dcm2bids
cli_session = ""
cli_log_level = "INFO"
bids_version = __BIDSversion__
# Archives
arch_extensions = "tar, tar.bz2, tar.gz or zip"
# dcm2bids.py
output_dir = Path.cwd()
session = "" # also Participant object
bids_validate = False
auto_extract_entities = False
do_not_reorder_entities = False
clobber = False
force_dcm2bids = False
post_op = []
logLevel = "WARNING"
entity_dir = {"j-": "AP",
"j": "PA",
"i-": "LR",
"i": "RL",
"AP": "AP",
"PA": "PA",
"LR": "LR",
"RL": "RL"}
# dcm2niix.py
dcm2niixOptions = "-b y -ba y -z y -f '%3s_%f_%p_%t'"
skip_dcm2niix = False
# sidecar.py
extractors = {}
auto_extractors = {'SeriesDescription': ["task-(?P<task>[a-zA-Z0-9]+)"],
'PhaseEncodingDirection': ["(?P<dir>(j|i)-?)"],
'EchoNumber': ["(?P<echo>[0-9])"]}
# Compute schema-derived defaults once from the default schema.
_, _SCHEMA_DERIVED = load_schema(__BIDSversion__, log_dir=None)
# Default schema-derived entity table keys derived from it.
# If schema loading fails, dcm2bids will raise at import time
entityTableKeys = _SCHEMA_DERIVED["entity_table_keys"]
# Default schema-derived auto entities.
auto_entities = _SCHEMA_DERIVED["auto_entities"]
compKeys = ["AcquisitionTime", "SeriesNumber", "SidecarFilename"]
search_methodChoices = ["fnmatch", "re"]
search_method = "fnmatch"
dup_method_choices = ["dup", "run"]
dup_method = "run"
runTpl = "_run-{:02d}"
dupTpl = "_dup-{:02d}"
bids_uri_choices = ["URI", "relative"]
bids_uri = "URI"
case_sensitive = True
keyWithPathsidecar_changes = ['IntendedFor', 'Sources']
# misc
tmp_dir_name = "tmp_dcm2bids"
helper_dir = "helper"
old_keys = ["dataType", "modalityLabel", "customLabels",
"sidecarChanges", "intendedFor"]
def write_participants(filename, participants):
with open(filename, "w") as f:
writer = csv.DictWriter(f, delimiter="\t", fieldnames=participants[0].keys())
writer.writeheader()
writer.writerows(participants)
def read_participants(filename):
if not os.path.exists(filename):
return []
with open(filename, "r") as f:
reader = csv.DictReader(f, delimiter="\t")
return [row for row in reader]
def splitext_(path, extensions=None):
""" Split the extension from a pathname
Handle case with extensions with '.' in it
Args:
path (str): A path to split
extensions (list): List of special extensions
Returns:
(root, ext): ext may be empty
"""
if extensions is None:
extensions = [".nii.gz"]
for ext in extensions:
if path.endswith(ext):
return path[: -len(ext)], path[-len(ext) :]
return os.path.splitext(path)
def normalize_acquisition_time(value):
"""
Normalize acquisition time strings into a tuple
(hour, minute, second, microsecond).
Accepts both colon-separated times (H:M:S[.fraction]) and compact
HHMMSS[.fraction]. Returns the original value if it cannot be parsed.
"""
if not isinstance(value, str):
return value
s = value.strip()
# Try a small set of strict formats using only datetime.strptime
for fmt in ("%H:%M:%S.%f", "%H:%M:%S", "%H%M%S.%f", "%H%M%S"):
try:
dt = datetime.datetime.strptime(s, fmt)
return (dt.hour, dt.minute, dt.second, dt.microsecond)
except ValueError:
continue
return value
def run_shell_command(commandLine, log=True):
""" Wrapper of subprocess.check_output
Returns:
Run command with arguments and return its output
"""
if log:
logger = logging.getLogger(__name__)
logger.info("Running: %s", " ".join(str(item) for item in commandLine))
pipes = Popen(commandLine, stdout=PIPE, stderr=PIPE)
std_out, std_err = pipes.communicate()
return std_out
def convert_dir(dir):
""" Convert Direction
Args:
dir (str): direction - dcm format
Returns:
str: direction - bids format
"""
return DEFAULT.entity_dir[dir]
def combine_dict_extractors(d1, d2):
""" combine dict
Args:
d1 (dic): dictionary
d2 (dic): dictionary
Returns:
dict: dictionary with combined information
if d1 d2 use the same keys, return dict will return a list of items.
"""
return {
k: [d[k][0] for d in (d1, d2) if k in d]
for k in set(d1.keys()) | set(d2.keys())
}
class TreePrinter:
"""
Generates and prints a tree representation of a given a directory.
"""
BRANCH = "│"
LAST = "└──"
JUNCTION = "├──"
BRANCH_PREFIX = "│ "
SPACE = " "
def __init__(self, root_dir):
self.root_dir = Path(root_dir)
def print_tree(self):
"""
Prints the tree representation of the root directory and
its subdirectories and files.
"""
tree = self._generate_tree(self.root_dir)
logger = logging.getLogger(__name__)
logger.info(f"Tree representation of {self.root_dir}{os.sep}")
logger.info(f"{self.root_dir}{os.sep}")
for item in tree:
logger.info(item.encode("utf-8", errors="ignore").decode("utf-8"))
def _generate_tree(self, directory, prefix=""):
"""
Generates the tree representation of the <directory> recursively.
Parameters:
- directory: Path
The directory for which a tree representation is needed.
- prefix: str
The prefix to be added to each entry in the tree.
Returns a list of strings representing the tree.
"""
tree = []
entries = sorted(directory.iterdir(), key=lambda path: str(path).lower())
entries = sorted(entries, key=lambda entry: entry.is_file())
entries_count = len(entries)
for index, entry in enumerate(entries):
connector = self.LAST if index == entries_count - 1 else self.JUNCTION
if entry.is_dir():
sub_tree = self._generate_tree(
entry,
prefix=prefix
+ (
self.BRANCH_PREFIX if index != entries_count - 1 else self.SPACE
),
)
tree.append(f"{prefix}{connector} {entry.name}{os.sep}")
tree.extend(sub_tree)
else:
tree.append(f"{prefix}{connector} {entry.name}")
return tree
|