Hi, Jure
To show you better, I re-found an example of UIH data and used ‘–tool=“dcm2niix”’ to get the import_dicom
result. I then processed it using Chris Rorden's dcm2niiX version v1.0.20240202
in python and output the information about the nii file.
The call for the import_dicom
command I use is as follows
call: gmri import_dicom sessionsfolder="/home/jiayf/qunex/test/sessions" masterinbox="/home/jiayf/qunex/field" overwrite="yes" tool="dcm2niix" sessions="BDUA_001"
The sample python code used is as follows.
import os
import subprocess
import nibabel as nib
# Chris Rorden's dcm2niiX version v1.0.20240202
def convert_dicom_to_nifti(input_root_dir, output_dir, additional_nii_dir=None):
"""
Convert DICOM folders in the input root directory to NIfTI format and output essential information and frame counts.
Also optionally read and output information from additional NIfTI files in a specified directory.
Parameters:
input_root_dir (str): Root directory containing multiple DICOM folders.
output_dir (str): Directory where NIfTI files will be output.
additional_nii_dir (str, optional): Directory containing additional NIfTI files to read and output information from.
"""
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Folder hierarchy: input_root_dir contains multiple DICOM folders
print(f"\nProcessing DICOM folders from: {input_root_dir}")
print(f"Output NIfTI files will be saved to: {output_dir}")
# Iterate through all DICOM folders in the input root directory
for folder_name in os.listdir(input_root_dir):
folder_path = os.path.join(input_root_dir, folder_name)
if os.path.isdir(folder_path):
# Check if the folder contains DICOM files
if any(file.endswith('.dcm') for file in os.listdir(folder_path)):
# Output file name matches the DICOM folder name
output_nifti_name = f"{folder_name}"
output_nifti_path = os.path.join(output_dir, f"{folder_name}.nii.gz")
# Call dcm2niix for conversion
command = [
"dcm2niix",
"-z", "y", # Compressed output
"-o", output_dir, # Output directory
"-f", output_nifti_name, # Output file name
folder_path # Input DICOM folder
]
subprocess.run(command, check=True)
# Extract and output essential NIfTI file information
try:
nifti_image = nib.load(output_nifti_path)
print(f"\n===== {folder_name} =====")
print(f"Output file: {output_nifti_path}")
print(f"Dimensions: {nifti_image.shape}")
print(f"Data type: {nifti_image.get_data_dtype()}")
print(f"Voxel size: {nifti_image.header.get_zooms()}")
# Extract frame information
if len(nifti_image.shape) >= 4:
frames = nifti_image.shape[3]
print(f"Number of frames: {frames}")
else:
print("This file does not contain frame information")
except Exception as e:
print(f"Failed to load NIfTI file: {e}")
# If additional NIfTI directory is specified, process those files as well
if additional_nii_dir and os.path.exists(additional_nii_dir):
print(f"\nProcessing additional NIfTI files from: {additional_nii_dir}")
for file_name in os.listdir(additional_nii_dir):
if file_name.endswith('.nii.gz') or file_name.endswith('.nii'):
file_path = os.path.join(additional_nii_dir, file_name)
try:
nifti_image = nib.load(file_path)
print(f"\n===== {file_name} =====")
print(f"File path: {file_path}")
print(f"Dimensions: {nifti_image.shape}")
print(f"Data type: {nifti_image.get_data_dtype()}")
print(f"Voxel size: {nifti_image.header.get_zooms()}")
# Extract frame information
if len(nifti_image.shape) >= 4:
frames = nifti_image.shape[3]
print(f"Number of frames: {frames}")
else:
print("This file does not contain frame information")
except Exception as e:
print(f"Failed to load additional NIfTI file: {e}")
if __name__ == "__main__":
input_root_directory = r"Y:\home\jiayf\qunex\field\BDUA_001\BDUA_001" # DICOM folder root directory
output_directory = r"Y:\home\jiayf\qunex\field\BDUA_001\BDUA_001" # Output directory
additional_nii_directory = r"Y:\home\jiayf\qunex\test\sessions\BDUA_001\nii" # ${STUDY_FOLDER}/sessions/BDUA_001/nii/5011.nii.gz (5011: restfMRI_mb7_240_iso2.5mm_SaveBySlc)
convert_dicom_to_nifti(input_root_directory, output_directory, additional_nii_directory)
Part of the output obtained from the above code is as follows (I only kept the fmri information), it’s easy to see that the fmri data we manually formatted has 240 time points, while the fmri converted using the import_dicom
format has only 48 time points.
Processing DICOM folders from: Y:\home\jiayf\qunex\field\BDUA_001\BDUA_001
Output NIfTI files will be saved to: Y:\home\jiayf\qunex\field\BDUA_001\BDUA_001
===== restfMRI_mb7_240_iso2.5mm_SaveBy_501 =====
Output file: Y:\home\jiayf\qunex\field\BDUA_001\BDUA_001\restfMRI_mb7_240_iso2.5mm_SaveBy_501.nii.gz
Dimensions: (84, 84, 63, 240)
Data type: uint16
Voxel size: (2.5, 2.5, 2.5, 2.0)
Number of frames: 240
Processing additional NIfTI files from: Y:\home\jiayf\qunex\test\sessions\BDUA_001\nii
===== 5011.nii.gz =====
File path: Y:\home\jiayf\qunex\test\sessions\BDUA_001\nii\5011.nii.gz
Dimensions: (84, 84, 63, 48)
Data type: uint16
Voxel size: (2.5, 2.5, 2.5, 2.0)
Number of frames: 48
It is indeed WEIRD that the same version of dcm2niix
gives different results.
Best, YJia