-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.py
More file actions
104 lines (92 loc) · 3.91 KB
/
Copy pathconvert.py
File metadata and controls
104 lines (92 loc) · 3.91 KB
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
#!/usr/bin/env python3
import argparse
import json
import os
def parse_args():
parser = argparse.ArgumentParser(description="Convert DICOM files to BIDS format.")
parser.add_argument("input_dir", type=str, help="Input directory containing DICOM files.")
parser.add_argument("output_dir", type=str, help="Output directory for BIDS formatted files.")
parser.add_argument(
"--heuristic",
type=str,
help="Path to the heuristic JSON file.",
required=True
)
parser.add_argument(
"--subject-map",
type=str,
default=None,
help="Optional CSV/XLS/XLSX mapping file for source identifiers -> bids_subject/session_id.",
)
parser.add_argument(
"--use-session-dates",
action=argparse.BooleanOptionalAction,
default=None,
help="Use acquisition dates for session names (ses-YYYYMMDD).",
)
parser.add_argument(
"--combine-sessions",
action=argparse.BooleanOptionalAction,
default=None,
help="Combine all scans into a single no-session directory per subject.",
)
parser.add_argument(
"--source-id",
action="append",
choices=["patient_name", "patient_id"],
default=None,
help=(
"Repeatable source identifier(s) used to match rows in --subject-map. "
"patient_id is DICOM PatientID (0010,0020; same concept as dcm2niix %%i) and is not guaranteed to be MRN. "
"patient_name uses DICOM PatientName (0010,0010) with exact string matching."
),
)
parser.add_argument(
"--no-anonymize",
action="store_true",
help="Disable anonymization of sensitive patient data during conversion (not recommended).",
)
parser.add_argument("--verbose", action="store_true", help="Enable verbose output.")
parser.add_argument("--debug", action="store_true", help="Enable debug output.")
return parser.parse_args()
def main():
from bidsmanager.read.dicom_reader import convert_dicom_directory
args = parse_args()
# Load heuristic from JSON file
with open(args.heuristic, 'r') as f:
heuristic = json.load(f)
# CLI args override heuristic values when provided.
subject_map = args.subject_map if args.subject_map else heuristic.get("subject_map")
# Backward compatibility for existing heuristic files.
if not subject_map:
subject_map = heuristic.get("subject_map_csv") or heuristic.get("subject_map_excel")
use_session_dates = args.use_session_dates
if use_session_dates is None:
use_session_dates = heuristic.get("use_session_dates", False)
combine_sessions = args.combine_sessions
if combine_sessions is None:
combine_sessions = heuristic.get("combine_sessions", False)
source_ids = args.source_id if args.source_id else heuristic.get("source_id")
if isinstance(source_ids, str):
source_ids = [source_ids]
input_dir = os.path.abspath(args.input_dir)
output_dir = os.path.abspath(args.output_dir)
verbose = args.verbose
if args.debug:
verbose = True
# Create a CSV file to store the DICOM files found
output_file = os.path.join(output_dir, "source", "dicom_files.csv")
os.makedirs(os.path.dirname(output_file), exist_ok=True)
convert_dicom_directory(input_directory=input_dir,
heuristic=heuristic,
anonymize=not args.no_anonymize,
bids_directory=output_dir,
delete_intermediates=True,
verbose=verbose,
use_session_dates=use_session_dates,
combine_sessions=combine_sessions,
subject_map=subject_map,
source_ids=source_ids,
cleanup_temp_directory=not args.debug)
if __name__ == "__main__":
main()