Skip to content

Commit 10f9c57

Browse files
committed
Pair NIfTI files with matching BVAL/BVEC in DICOM batch path
- Ensures each NIfTI in dicom_to_nifti uses correct BVAL/BVEC - Handles filenames with multiple dots
1 parent fcd0649 commit 10f9c57

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

WrapImage/nifti_wrapper_kaapana.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,24 @@ def load_config():
9090
raise FileNotFoundError(f"No batch folders found in {BATCH_DIR}")
9191

9292
# Pick the first batch folder (usually one per patient/series)
93-
batch_input_dir = batch_folders[0]
94-
batch_input_dir = os.path.join(batch_input_dir, "dicom_to_nifti")
95-
96-
nifti_files = glob.glob(os.path.join(batch_input_dir, "*.nii.gz"))
97-
bvec_files = glob.glob(os.path.join(batch_input_dir, "*.bvec"))
98-
bval_files = glob.glob(os.path.join(batch_input_dir, "*.bval"))
99-
100-
if not nifti_files or not bvec_files or not bval_files:
101-
raise FileNotFoundError(
102-
f"Cannot find NIfTI or bvec/bval files in {batch_input_dir}"
103-
)
104-
105-
# Take the first matching file
106-
input_file = nifti_files[0]
107-
bvec_file = bvec_files[0]
108-
bval_file = bval_files[0]
93+
batch_input_dir = os.path.join(batch_folders[0], "dicom_to_nifti")
94+
95+
nifti_files = sorted(glob.glob(os.path.join(batch_input_dir, "*.nii.gz")))
96+
if not nifti_files:
97+
raise FileNotFoundError(f"No NIfTI files found in {batch_input_dir}")
98+
99+
paired_files = []
100+
for nifti in nifti_files:
101+
base = os.path.splitext(os.path.splitext(os.path.basename(nifti))[0])[0] # remove .nii.gz
102+
bvec_file = os.path.join(batch_input_dir, f"{base}.bvec")
103+
bval_file = os.path.join(batch_input_dir, f"{base}.bval")
104+
if os.path.exists(bvec_file) and os.path.exists(bval_file):
105+
paired_files.append((nifti, bvec_file, bval_file))
106+
else:
107+
raise FileNotFoundError(f"Missing bvec/bval for {nifti}")
108+
109+
# Use the first matched trio
110+
input_file, bvec_file, bval_file = paired_files[0]
109111

110112
print(f"Using DICOM-converted files from {batch_input_dir}:\n"
111113
f" {input_file}\n {bvec_file}\n {bval_file}")

0 commit comments

Comments
 (0)