|
1 | 1 | import argparse
|
2 | 2 | import json
|
3 | 3 | import os
|
| 4 | +from pathlib import Path |
| 5 | +import subprocess |
4 | 6 | import nibabel as nib
|
5 | 7 | from src.wrappers.OsipiBase import OsipiBase
|
6 | 8 | import numpy as np
|
7 | 9 | from tqdm import tqdm
|
8 | 10 |
|
| 11 | +def dicom_to_niix(vol_dir: Path): |
| 12 | + """ |
| 13 | + For converting DICOM images to a (compresssed) 4d nifti image |
| 14 | + """ |
| 15 | + |
| 16 | + try: |
| 17 | + res = subprocess.run( |
| 18 | + [ |
| 19 | + "dcm2niix", |
| 20 | + "-f", "%s_%p", # dcm2niix attempts to provide a sensible file naming scheme |
| 21 | + "-o", vol_dir, |
| 22 | + "-z", "y", #specifying compressed nii.gz file |
| 23 | + # https://www.nitrc.org/plugins/mwiki/index.php/dcm2nii:MainPage |
| 24 | + # for further configuration for general usage see page above |
| 25 | + vol_dir |
| 26 | + ], |
| 27 | + capture_output=True, |
| 28 | + text=True, |
| 29 | + check=True |
| 30 | + ) |
| 31 | + |
| 32 | + nifti_files = list(Path(vol_dir).glob("*.nii.gz")) |
| 33 | + if not nifti_files: |
| 34 | + raise RuntimeError("No NIfTI file generated by dcm2niix.") |
| 35 | + if len(nifti_files) > 1: |
| 36 | + raise RuntimeError("Multiple NIfTI files generated; expected one 4D NIfTI.") |
| 37 | + |
| 38 | + bval_files = list(vol_dir.glob("*.bval")) |
| 39 | + bvec_files = list(vol_dir.glob("*.bvec")) |
| 40 | + bval_path = str(bval_files[0]) if bval_files else None |
| 41 | + bvec_path = str(bvec_files[0]) if bvec_files else None |
| 42 | + |
| 43 | + |
| 44 | + return vol_dir / "dicom2nifti.nii.gz", bval_path, bvec_path |
| 45 | + |
| 46 | + except subprocess.CalledProcessError as e: |
| 47 | + raise RuntimeError(f"dcm2niix failed: {e.stderr}") |
| 48 | + |
9 | 49 |
|
10 | 50 | def read_nifti_file(input_file):
|
11 | 51 | """
|
@@ -81,22 +121,35 @@ def loop_over_first_n_minus_1_dimensions(arr):
|
81 | 121 |
|
82 | 122 | if __name__ == "__main__":
|
83 | 123 | parser = argparse.ArgumentParser(description="Read a 4D NIfTI phantom file along with BIDS JSON, b-vector, and b-value files.")
|
84 |
| - parser.add_argument("input_file", type=str, help="Path to the input 4D NIfTI file.") |
85 |
| - parser.add_argument("bvec_file", type=str, help="Path to the b-vector file.") |
86 |
| - parser.add_argument("bval_file", type=str, help="Path to the b-value file.") |
| 124 | + parser.add_argument("input_file", type=Path, help="Path to the input 4D NIfTI file or DICOM files 4D images like fMRI and DTI/DKI are supported..") |
| 125 | + parser.add_argument("bvec_file", type=Path, help="Path to the b-vector file.") |
| 126 | + parser.add_argument("bval_file", type=Path, help="Path to the b-value file.") |
87 | 127 | parser.add_argument("--affine", type=float, nargs="+", help="Affine matrix for NIfTI image.")
|
88 | 128 | parser.add_argument("--algorithm", type=str, default="OJ_GU_seg", help="Select the algorithm to use.")
|
89 | 129 | parser.add_argument("--algorithm_args", nargs=argparse.REMAINDER, help="Additional arguments for the algorithm.")
|
90 | 130 |
|
91 | 131 | args = parser.parse_args()
|
92 | 132 |
|
93 | 133 | try:
|
| 134 | + if args.input_file.is_dir(): |
| 135 | + # Convert DICOM to NIfTI using dicom2niix |
| 136 | + nifti_file, bval_path, bvec_path = dicom_to_niix(args.input_file) # read dir |
| 137 | + if bval_path is None or bvec_path is None: |
| 138 | + raise RuntimeError("dcm2niix did not generate bval/bvec files.") |
| 139 | + bval_file = Path(bval_path) |
| 140 | + bvec_file = Path(bvec_path) |
| 141 | + else: |
| 142 | + # Assume input is already a NIfTI file |
| 143 | + nifti_file = args.input_file |
| 144 | + bval_file = args.bval_file |
| 145 | + bvec_file = args.bvec_file |
| 146 | + |
94 | 147 | # Read the 4D NIfTI file
|
95 |
| - data, _ = read_nifti_file(args.input_file) |
| 148 | + data, _ = read_nifti_file(nifti_file) |
96 | 149 |
|
97 | 150 | # Read the b-vector, and b-value files
|
98 |
| - bvecs = read_bvec_file(args.bvec_file) |
99 |
| - bvals = read_bval_file(args.bval_file) |
| 151 | + bvecs = read_bvec_file(bvec_file) |
| 152 | + bvals = read_bval_file(bval_file) |
100 | 153 |
|
101 | 154 | # Pass additional arguments to the algorithm
|
102 | 155 |
|
|
0 commit comments