-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_task_rest_to_BIDS.py
638 lines (510 loc) · 29.2 KB
/
process_task_rest_to_BIDS.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
"""
process_task_rest_to_BIDS.py
Description:
This script is designed to process Resting-State fMRI DICOM files into NIFTI format in
compliance with Brain Imaging Data Structure (BIDS) conventions.
It encompasses DICOM to NIFTI conversion using the dcm2niix tool,
along with the application of additional BIDS-compliant metadata processing using cubids.
Before executing, the script verifies the installation of dcm2niix and cubids.
It features robust error handling and detailed logging for each processing step,
ensuring reliability and transparency in data conversion.
Usage:
Invoke the script from the command line with the following format:
python process_task_rest_to_BIDS.py <dicom_root_dir> <bids_root_dir>
Example usage:
python process_task_rest_to_BIDS.py /path/to/dicom_root_dir /path/to/bids_root_dir
Author: PAMcConnell
Created on: 20231112
Last Modified: 20231112
Version: 1.0.0
License:
This software is released under the MIT License.
Dependencies:
- Python 3.12
- dcm2niix (command-line tool): Essential for converting DICOM to NIFTI format. (https://github.com/rordenlab/dcm2niix)
- CuBIDS (command-line tool): Utilized for handling BIDS-compliant metadata. (https://cubids.readthedocs.io/en/latest/index.html)
- Python standard libraries: logging, os, tempfile, shutil, glob, subprocess, argparse, sys, re, json.
Environment Setup:
- Ensure Python 3.12 is installed in your environment.
- Install dcm2niix and pydeface command-line tools. Use `conda install -c conda-forge dcm2niix` for dcm2niix installation.
- Install cubids as per the instructions in its documentation. Try using 'pip install cubids'.
- To establish the required environment, utilize the provided 'environment.yml' file with Conda.
Change Log:
- 20231111: Initial release of the script with basic functionality for DICOM to NIFTI conversion.
- 20231112: Enhanced functionality with verbose logging from dcm2niix, standardized output file paths, and improved error handling.
"""
import os # Used for operating system dependent functionalities like file path manipulation.
import shutil # Provides high-level file operations like file copying and removal.
import logging # Logging library, for tracking events that happen when running the software.
import argparse # Parser for command-line options, arguments, and sub-commands.
import re # Regular expressions, useful for text matching and manipulation.
import subprocess # Spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
import sys # Provides access to some variables used or maintained by the interpreter.
import json # JSON encoder and decoder.
import glob # Unix style pathname pattern expansion.
import tempfile # Generate temporary files and directories.
# Set up logging for individual archive logs.
def setup_logging(subject_id, session_id, bids_root_dir):
"""
Sets up logging for the script, creating log files in a specified directory.
Parameters:
- subject_id (str): The identifier for the subject.
- session_id (str): The identifier for the session.
- bids_root_dir (str): The root directory of the BIDS dataset.
This function sets up a logging system that writes logs to both a file and the console.
The log file is named based on the subject ID, session ID, and the script name.
It's stored in a 'logs' directory within the 'doc' folder by subject ID, which is located at the same
level as the BIDS root directory.
The logging level is set to INFO, meaning it captures all informational, warning, and error messages.
Usage Example:
setup_logging('sub-01', 'ses-1', '/path/to/bids_root_dir')
"""
# Extract the base name of the script without the .py extension.
script_name = os.path.basename(__file__).replace('.py', '')
# Construct the log directory path within 'doc/logs'
log_dir = os.path.join(os.path.dirname(bids_root_dir), 'doc', 'logs', script_name, subject_id)
# Create the log directory if it doesn't exist.
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Construct the log file name using subject ID, session ID, and script name.
log_file_name = f"{subject_id}_{session_id}_{script_name}.log"
log_file_path = os.path.join(log_dir, log_file_name)
# Configure file logging.
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename=log_file_path,
filemode='w' # 'w' mode overwrites existing log file.
)
# If you also want to log to console.
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(formatter)
logging.getLogger().addHandler(console_handler)
logging.info(f"Logging setup complete. Log file: {log_file_path}")
return log_file_path
# Checks if Resting-state fmri NIFTI files already exist in the specified BIDS output directory.
def check_existing_nifti(output_dir_func, subject_id, session_id):
"""
Parameters:
- output_dir_func (str): The BIDS output directory where NIFTI files are stored.
- subject_id (str): The subject ID.
- session_id (str): The session ID.
Returns:
- bool: True if Resting-State fMRI NIFTI files exist, False otherwise.
"""
expected_nifti_file = os.path.join(output_dir_func, f'{subject_id}_{session_id}_task-rest_run-01_bold.nii')
if os.path.isfile(expected_nifti_file):
print(f"Resting-state fmri NIFTI file already exists: {expected_nifti_file}")
return True
else:
return False
# Extract the subject and session IDs from the provided physio root directory path.
def extract_subject_session(dicom_root_dir):
"""
Parameters:
- dicom_root_dir (str): The directory path that includes subject and session information.
This path should follow the BIDS convention, containing 'sub-' and 'ses-' prefixes.
Returns:
- subject_id (str): The extracted subject ID.
- session_id (str): The extracted session ID.
Raises:
- ValueError: If the subject_id and session_id cannot be extracted from the path.
This function assumes that the directory path follows the Brain Imaging Data Structure (BIDS) naming convention.
It uses regular expressions to find and extract the subject and session IDs from the path.
Usage Example:
subject_id, session_id = extract_subject_session('/path/to/data/sub-01/ses-1/dicom_sorted')
Note: This function will raise an error if it cannot find a pattern matching the BIDS convention in the path.
"""
# Normalize the path to remove any trailing slashes for consistency.
dicom_root_dir = os.path.normpath(dicom_root_dir)
# The pattern looks for 'sub-' followed by any characters until a slash, and similar for 'ses-'.
match = re.search(r'(sub-[^/]+)/(ses-[^/]+)', dicom_root_dir)
if not match:
raise ValueError("Unable to extract subject_id and session_id from path: %s", dicom_root_dir)
subject_id, session_id = match.groups()
return subject_id, session_id
# Updates the JSON sidecar file with specific fields required for BIDS compliance in Resting-State FMRI datasets.
def update_json_file(json_filepath):
"""
Parameters:
- json_filepath (str): Path to the JSON sidecar file.
This function updates the specified JSON file with fields relevant to Resting-State imaging.
The function handles the reading and writing of the JSON file, ensuring that the file is properly updated
and formatted.
Usage Example:
update_json_file('/path/to/sidecar.json')
Dependencies:
- json module for reading and writing JSON files.
- os and sys modules for file operations and system-level functionalities.
*** Note: Refer to MR protocol documentation in /doc/MR_protocols for more information. ***
"""
try:
with open(json_filepath, 'r+') as file:
data = json.load(file)
# Update with specific Resting-State metadata
data['TaskName'] = 'rest'
logging.info(f"Task Name: {data['TaskName']}")
data['B0FieldSource2'] = "*fm2d2r"
logging.info(f"B0 Field Source 2: {data['B0FieldSource2']}")
data['B0FieldSource']= "*epse2d1_104"
logging.info(f"B0 Field Source: {data['B0FieldSource']}")
# Write back the updated data and truncate the file to the new data length.
file.seek(0)
json.dump(data, file, indent=4)
file.truncate()
logging.info(f"Updated JSON file at {json_filepath} with Resting-State FMRI-specific metadata.")
# Catch issues with reading or writing to the JSON file.
except IOError as e:
logging.error(f"Error reading or writing to JSON file at {json_filepath}. Error: {e}")
raise
except json.JSONDecodeError as e:
logging.error(f"Error parsing JSON data in file at {json_filepath}. Error: {e}")
raise
except Exception as e:
logging.error(f"Unexpected error occurred while updating JSON file at {json_filepath}. Error: {e}")
raise
# Runs the dcm2niix conversion tool to convert DICOM files to NIFTI format.
def run_dcm2niix(input_dir, temp_dir, subject_id, session_id):
"""
The output files are named according to BIDS (Brain Imaging Data Structure) conventions.
Parameters:
- input_dir (str): Directory containing the DICOM files to be converted.
- output_dir_func (str): Directory where the converted NIFTI files will be saved.
- subject_id (str): Subject ID, extracted from the DICOM directory path.
- session_id (str): Session ID, extracted from the DICOM directory path.
This function uses the dcm2niix tool to convert DICOM files into NIFTI format.
It saves the output in the specified output directory, structuring the filenames
according to BIDS conventions. The function assumes that dcm2niix is installed
and accessible in the system's PATH.
Usage Example:
run_dcm2niix('/dicom_root_dir/dicom_sorted/<func_dicoms>', '/bids_root_dir/sub-01/ses-01/func', 'sub-01', 'sub-01', 'ses-01')
"""
try:
# Ensure the output directory for functional scans exists.
os.makedirs(temp_dir, exist_ok=True)
base_cmd = [
'dcm2niix',
'-f', f'{subject_id}_{session_id}_%p', # Naming convention.
'-l', 'y', # Losslessly scale 16-bit integers to use maximal dynamic range.
'-b', 'y', # Save BIDS metadata to .json sidecar.
'-p', 'n', # Do not use Use Philips precise float (rather than display) scaling.
'-x', 'y', # Crop images. This will attempt to remove excess neck from 3D acquisitions.
'-z', 'n', # Do not compress files.
'-ba', 'n', # Do not anonymize files (anonymized at MR console).
'-i', 'n', # Do not ignore derived, localizer and 2D images.
'-m', '2', # Merge slices from same series automatically based on modality.
'-o', temp_dir,
input_dir
]
# Create a temporary directory for the verbose output run.
with tempfile.TemporaryDirectory() as temp_dir:# Run the actual conversion without verbose output.
subprocess.run(base_cmd) #capture_output=False, text=False)
logging.info(f"dcm2niix conversion completed successfully to {temp_dir}.")
# Log conversion errors.
except subprocess.CalledProcessError as e:
logging.error("dcm2niix conversion failed: %s", e)
raise
# Log other errors.
except Exception as e:
logging.error("An error occurred during dcm2niix conversion: %s", e)
raise
# Runs the dcm2niix conversion tool to produce verbose output to logfile.
def run_dcm2niix_verbose(input_dir, temp_dir_verbose, subject_id, session_id, log_file_path):
"""
The output files are named according to BIDS (Brain Imaging Data Structure) conventions.
Parameters:
- input_dir (str): Directory containing the DICOM files to be converted.
- temp_dir (str): Directory where the converted NIFTI files will be saved and deleted.
- subject_id (str): Subject ID, extracted from the DICOM directory path.
- session_id (str): Session ID, extracted from the DICOM directory path.
The function logs verbose output to the specified log file.
Usage Example:
run_dcm2niix('/dicom_root_dir/dicom_sorted/<func_dicoms>, 'temp_dir', 'sub-01', 'ses-01')
"""
try:
verbose_cmd = [
'dcm2niix',
'-f', f'{subject_id}_{session_id}_%p', # Naming convention.
'-l', 'y', # Losslessly scale 16-bit integers to use maximal dynamic range.
'-b', 'y', # Save BIDS metadata to .json sidecar.
'-p', 'n', # Do not use Use Philips precise float (rather than display) scaling.
'-x', 'n', # Do notCrop images. This will attempt to remove excess neck from 3D acquisitions.
'-z', 'n', # Do not compress files.
'-ba', 'n', # Do not anonymize files (anonymized at MR console).
'-i', 'n', # Do not ignore derived, localizer and 2D images.
'-m', '2', # Merge slices from same series automatically based on modality.
'-v', 'y', # Print verbose output to logfile.
'-o', temp_dir_verbose,
input_dir
]
# Create a temporary directory for the verbose output run.
with tempfile.TemporaryDirectory() as temp_dir_verbose:
with open(log_file_path, 'a') as log_file:
result = subprocess.run(verbose_cmd, check=True, stdout=log_file, stderr=log_file)
logging.info(result.stdout)
if result.stderr:
logging.error(result.stderr)
# Log conversion errors.
except subprocess.CalledProcessError as e:
logging.error("dcm2niix conversion failed: %s", e)
raise
# Log other errors.
except Exception as e:
logging.error("An error occurred during dcm2niix conversion: %s", e)
raise
# Executes the cubids-add-nifti-info command to add nifti header information to the BIDS dataset.
def run_cubids_add_nifti_info(bids_root_dir):
"""
Executes the cubids-add-nifti-info command and logs changes made to .json sidecar files.
Parameters:
- bids_root_dir (str): Path to the BIDS dataset directory.
"""
# Store the original contents of JSON files.
json_files = glob.glob(os.path.join(bids_root_dir, '**', '*.json'), recursive=True)
original_contents = {file: read_json_file(file) for file in json_files}
try:
cubids_add_nii_hdr_command = ['cubids-add-nifti-info', bids_root_dir]
logging.info(f"Executing add nifti info: {cubids_add_nii_hdr_command}")
# Run the command and capture output.
result = subprocess.run(cubids_add_nii_hdr_command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Log the standard output and error.
logging.info("cubids-add-nifti-info output:\n%s", result.stdout)
if result.stderr:
logging.error("cubids-add-nifti-info error output:\n%s", result.stderr)
logging.info("cubids-add-nifti-info executed successfully.")
# Check and log changes in JSON files.
for file in json_files:
new_content = read_json_file(file)
log_json_differences(original_contents[file], new_content)
# Log successful execution of the command.
logging.info("cubids-add-nifti-info executed and changes logged successfully.")
# Catch cubids execution errors.
except subprocess.CalledProcessError as e:
logging.error(f"cubids-add-nifti-info execution failed: {e}")
raise
# Executes the cubids-remove-metadata-fields command to remove metadata fields from the BIDS dataset.
def run_cubids_remove_metadata_fields(bids_root_dir, fields):
"""
Executes the cubids-remove-metadata-fields command on the specified BIDS directory.
Parameters:
- bids_root_dir (str): Path to the BIDS dataset directory.
- fields (list of str): List of metadata fields to remove.
"""
fields_args = ['--fields', ','.join(fields)] # Joining fields with commas for the command argument.
try:
cubids_remove_fields_command = ['cubids-remove-metadata-fields', bids_root_dir] + fields_args
logging.info(f"Executing remove metadata fields: {cubids_remove_fields_command}")
# Run the command and capture output.
result = subprocess.run(cubids_remove_fields_command, check=True, stdout=subprocess.PIPE, text=True)
# Log the standard output.
logging.info("cubids-remove-metadata-fields output:\n%s", result.stdout)
# Log successful execution.
logging.info("cubids-remove-metadata-fields executed successfully.")
# Catch cubids execution errors.
except subprocess.CalledProcessError as e:
logging.error(f"cubids-remove-metadata-fields execution failed: {e}")
raise
# Check if dcm2niix is installed and accessible in the system's PATH.
def check_dcm2niix_installed():
"""
Returns:
bool: True if dcm2niix is installed, False otherwise.
"""
dcm2niix_version_output = subprocess.getoutput('dcm2niix --version')
if 'version' in dcm2niix_version_output:
logging.info(f"dcm2niix is installed: {dcm2niix_version_output.splitlines()[0]}")
return True
else:
logging.warning("dcm2niix is not installed.")
return False
# Reads and returns the contents of a JSON file.
def read_json_file(filepath):
"""
Parameters:
- filepath (str): The full path to the JSON file.
Returns:
- dict: Contents of the JSON file.
This function opens and reads a JSON file, then returns its contents as a Python dictionary.
It handles file reading errors and logs any issues encountered while opening or reading the file.
Usage Example:
json_content = read_json_file('/path/to/file.json')
"""
try:
with open(filepath, 'r') as file:
return json.load(file)
except Exception as e:
logging.error(f"Error reading JSON file {filepath}: {e}")
raise
# Logs the differences between two JSON objects.
def log_json_differences(before, after):
"""
Parameters:
- before (dict): JSON object representing the state before changes.
- after (dict): JSON object representing the state after changes.
This function compares two JSON objects and logs any differences found.
It's useful for tracking changes made to JSON files, such as metadata updates.
The function assumes that 'before' and 'after' are dictionaries representing JSON objects.
Differences are logged as INFO with the key and the changed values.
Usage Example:
log_json_differences(original_json, updated_json)
"""
for key, after_value in after.items():
before_value = before.get(key)
if before_value != after_value:
logging.info(f"Changed {key}: from '{before_value}' to '{after_value}'")
elif key not in before:
logging.info(f"Added {key}: {after_value}")
# Checks if cubids is installed and accessible in the system's PATH.
def check_cubids_installed():
"""
Returns:
bool: True if cubids is installed, False otherwise.
"""
cubids_version_output = subprocess.getoutput('cubids-add-nifti-info -h')
if 'cubids-add-nifti-info' in cubids_version_output:
logging.info(f"cubids is installed: {cubids_version_output.splitlines()[0]}")
return True
else:
logging.warning("cubids is not installed.")
return False
# Processes DICOM files in the given root directory and converts them to NIFTI format in the BIDS dataset.
def main(dicom_root_dir, bids_root_dir, num_runs):
"""
Main function to process resting-state fMRI DICOM files into BIDS-compliant NIFTI format.
The function systematically processes each run of resting-state fMRI data. It uses dcm2niix for DICOM to NIFTI conversion,
organizes the converted files into the BIDS format, and applies necessary metadata adjustments using cubids.
The process includes checking for existing files, handling naming conventions, and organizing outputs in the 'func'
directory of the BIDS dataset.
Parameters:
- dicom_root_dir (str): Root directory containing the DICOM directories.
- bids_root_dir (str): Root directory of the BIDS dataset where the NIFTI files will be saved.
- num_runs (int): Number of fMRI runs to process.
Each run's data is expected to be in a separate directory following a consistent naming pattern.
The script assumes the presence of the necessary tools (dcm2niix, cubids) and handles errors gracefully,
logging issues and exiting if critical tools are missing.
Usage:
- Call this function with the paths to the DICOM root directory, BIDS root directory, and the number of runs.
Example: main('/path/to/dicom', '/path/to/bids', 4)
Dependencies:
- dcm2niix for DICOM to NIFTI conversion.
- cubids for BIDS metadata processing.
- Standard Python libraries: os, logging, tempfile, shutil.
Note: Ensure that dcm2niix and cubids are installed and properly configured in your environment before running this script.
Exceptions:
- The function raises an exception and logs an error message if there's an issue during the processing.
"""
# Extract subject and session IDs from the DICOM directory path.
subject_id, session_id = extract_subject_session(dicom_root_dir)
# Specify the exact directory where the NIFTI files will be saved.
output_dir_func = os.path.join(bids_root_dir, f'{subject_id}', f'{session_id}', 'func')
os.makedirs(output_dir_func, exist_ok=True)
# Check if Resting-State fMRI files already exist.
if check_existing_nifti(output_dir_func, subject_id, session_id):
# print(f"Resting-state fmri NIFTI files already exist: {output_dir_func}")
return # Skip processing if Resting-State fMRI files already exist.
# Otherwise:
try:
# Specify the exact directory where the DICOM files are located within the root directory
base_dicom_dir = os.path.join(args.dicom_root_dir)
# Setup logging after extracting subject_id and session_id.
log_file_path = setup_logging(subject_id, session_id, bids_root_dir)
logging.info(f"Processing Resting-State FMRI data for subject: {subject_id}, session: {session_id}")
# Check if dcm2niix is installed and accessible in the system's PATH.
if check_dcm2niix_installed():
for run in range(1, args.num_runs+1):
for suffix in ['', '_SBref']:
dicom_dir = os.path.join(base_dicom_dir, f'Resting_{run}{suffix}')
logging.info(f"Converting DICOM files in {dicom_dir}")
# Open temporary directory for dcm2nixx conversion.
with tempfile.TemporaryDirectory() as temp_dir:
run_dcm2niix(dicom_dir, temp_dir, subject_id, session_id)
logging.info(f"Files after conversion: {os.listdir(temp_dir)}")
for old_file in os.listdir(temp_dir):
try:
old_filepath = os.path.join(temp_dir, old_file)
if suffix == '_SBref':
# Seperate handling of SBref files.
new_file = f"{subject_id}_{session_id}_task-rest_run-{run:02d}_sbref{os.path.splitext(old_file)[-1]}"
new_filepath = os.path.join(output_dir_func, new_file)
else:
# Seperate handling of _bold files.
new_file = f"{subject_id}_{session_id}_task-rest_run-{run:02d}{suffix}_bold{os.path.splitext(old_file)[-1]}"
new_filepath = os.path.join(output_dir_func, new_file)
# Move the file from the temporary directory to the output directory and rename according to BIDS.
shutil.move(old_filepath, new_filepath)
logging.info(f"Moved {old_file} to {new_filepath}")
# Update JSON file with necessary BIDS metadata
if new_filepath.endswith('.json'):
update_json_file(new_filepath)
# Catch errors.
except FileNotFoundError:
logging.error(f"File not found: {old_file}")
except Exception as e:
logging.error(f"Error processing file {old_file}: {e}")
# Check if cubids is installed
if check_cubids_installed():
# Run cubids commands to add NIFTI metadata.
logging.info(f"Adding NIFTI metadata for subject: {subject_id}, session: {session_id}")
run_cubids_add_nifti_info(bids_root_dir)
# Run cubids commands to remove metadata fields.
logging.info(f"Removing metadata fields for subject: {subject_id}, session: {session_id}")
run_cubids_remove_metadata_fields(bids_root_dir, ['PatientBirthDate'])
# Run dcm2niix for verbose output.
with tempfile.TemporaryDirectory() as temp_dir_verbose:
run_dcm2niix_verbose(dicom_dir, temp_dir_verbose, subject_id, session_id, log_file_path)
# Catch error if cubids is not installed.
else:
logging.error("cubids is not installed. Skipping cubids commands.")
return # Exit the function if cubids is not installed.
# Catch error if dcm2niix is not installed.
else:
logging.error("dcm2niix is not installed. Cannot proceed with DICOM to NIFTI conversion.")
return # Exit the function if dcm2niix is not installed.
# Log other errors.
except Exception as e:
logging.error(f"An error occurred in the main function: {e}")
raise
# Entry point of the script when executed from the command line.
if __name__ == "__main__":
"""
Script's entry point when executed from the command line.
This script processes resting-state fMRI DICOM files, converting them into NIFTI format following
the Brain Imaging Data Structure (BIDS) conventions. It handles multiple runs of fMRI data
and organizes the output into a BIDS-compliant dataset structure.
The script requires three command-line arguments:
- The root directory containing the DICOM directories.
- The root directory of the BIDS dataset where the converted NIFTI files will be stored.
- The number of runs (sessions) of fMRI data to process.
Usage:
python process_resting_state_fmri_to_BIDS.py <dicom_root_dir> <bids_root_dir> <num_runs>
Example: python process_resting_state_fmri_to_BIDS.py /path/to/dicom /path/to/bids 4
The script ensures that the necessary tools (dcm2niix and cubids) are installed and accessible.
It performs detailed logging of each step and robust error handling for reliable processing.
"""
# Set up an argument parser to handle command-line arguments.
parser = argparse.ArgumentParser(description='Process DICOM files for Resting-State FMRI and convert to NIFTI.')
# Add arguments to the parser.
# The first argument is the root directory containing the DICOM directories.
parser.add_argument('dicom_root_dir', type=str, help='Root directory containing the DICOM directories.')
# The second argument is the root directory of the BIDS dataset.
parser.add_argument('bids_root_dir', type=str, help='Root directory of the BIDS dataset.')
# The third argument is the number of runs to iterate through.
parser.add_argument('num_runs', type=int, help='Number of runs.')
# Parse the arguments provided by the user.
args = parser.parse_args()
# Starting script messages
print(f"Starting script with provided arguments.")
print(f"Dicom data directory: {args.dicom_root_dir}")
print(f"BIDS root directory: {args.bids_root_dir}")
print(f"Number of runs: {args.num_runs}")
# Call the main function with the parsed arguments.
try:
# Call the main function with the parsed arguments.
main(args.dicom_root_dir, args.bids_root_dir, args.num_runs)
except Exception as e:
logging.error("An error occurred during script execution: %s", e, exc_info=True)
logging.info("Script execution completed with errors.")
else:
logging.info("Script executed successfully.")