|
|
import SimpleITK as sitk |
|
|
import os |
|
|
import glob |
|
|
import nrrd |
|
|
import nibabel as nib |
|
|
import numpy as np |
|
|
import cv2 |
|
|
import traceback |
|
|
import psutil |
|
|
from concurrent.futures import ProcessPoolExecutor, as_completed |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
def _reorient_niigz_RASplus(nifti_path, output_path): |
|
|
""" |
|
|
Load a NIfTI file, reorient it to RAS+ (right-anterior-superior) using as_closest_canonical, |
|
|
and save the result while preserving the original data type. |
|
|
""" |
|
|
|
|
|
img = nib.load(nifti_path) |
|
|
|
|
|
original_dtype = img.get_fdata().dtype |
|
|
|
|
|
current_orientation = nib.aff2axcodes(img.affine) |
|
|
if current_orientation == ("R", "A", "S"): |
|
|
msg = f"{nifti_path} is already in RAS+ orientation.\n" |
|
|
if nifti_path != output_path: |
|
|
nib.save(img, output_path) |
|
|
return msg |
|
|
|
|
|
canonical_img = nib.as_closest_canonical(img) |
|
|
|
|
|
reoriented_data = canonical_img.get_fdata().astype(original_dtype) |
|
|
new_img = nib.Nifti1Image(reoriented_data, canonical_img.affine, header=img.header) |
|
|
|
|
|
new_img.header.set_data_dtype(original_dtype) |
|
|
|
|
|
nib.save(new_img, output_path) |
|
|
msg = f"Converted {nifti_path} to RAS+ orientation and saved as {output_path}.\n" |
|
|
return msg |
|
|
|
|
|
|
|
|
def reorient_niigz_RASplus_batch_inplace(dataset_dir, workers_limit=1): |
|
|
""" |
|
|
Reorient all NIfTI files in a directory and its subdirectories to RAS+ orientation in place. |
|
|
This function modifies the original files rather than creating new ones. |
|
|
|
|
|
Args: |
|
|
dataset_dir (str): Directory containing .nii.gz files |
|
|
workers_limit (int): Maximum number of worker processes. Defaults to 1. |
|
|
""" |
|
|
|
|
|
nii_files = list(glob.glob(f"{dataset_dir}/**/*.nii.gz", recursive=True)) |
|
|
total_files = len(nii_files) |
|
|
num_workers = min(workers_limit, total_files) if workers_limit > 0 else 1 |
|
|
print(f"Reorienting {total_files} files to RAS+ orientation...\n") |
|
|
|
|
|
|
|
|
preprocessed_files_count = 0 |
|
|
failed_cases = [] |
|
|
with ProcessPoolExecutor(max_workers=num_workers) as executor: |
|
|
futures = { |
|
|
executor.submit(_reorient_niigz_RASplus, nii_file, nii_file): nii_file |
|
|
for nii_file in nii_files |
|
|
} |
|
|
|
|
|
for fut in as_completed(futures): |
|
|
nii_file = futures[fut] |
|
|
try: |
|
|
msg = fut.result() |
|
|
preprocessed_files_count += 1 |
|
|
print( |
|
|
f"✓ Reoriented {os.path.basename(nii_file)}: ({preprocessed_files_count}/{total_files})" |
|
|
) |
|
|
print(f" - {msg}") |
|
|
|
|
|
mem = psutil.virtual_memory().percent |
|
|
if mem > 80: |
|
|
print(f"⚠️ High memory usage: {mem}%") |
|
|
|
|
|
except Exception: |
|
|
err = traceback.format_exc() |
|
|
print( |
|
|
f"❌ Reorienting {os.path.basename(nii_file)} generated an exception:\n{err}" |
|
|
) |
|
|
failed_cases.append((nii_file, err)) |
|
|
|
|
|
if failed_cases: |
|
|
print(f"❌ Failed to reorient {len(failed_cases)} files:") |
|
|
for nii_file, e in failed_cases: |
|
|
print(f" - {os.path.basename(nii_file)}: {e.splitlines()[-1]}") |
|
|
raise RuntimeError("Some tasks failed to reorient. See logs above.") |
|
|
|
|
|
|
|
|
def convert_nrrd_to_nifti(input_dir, output_dir, recursive=False): |
|
|
""" |
|
|
Convert all .nrrd files in input_dir to .nii.gz files in output_dir |
|
|
|
|
|
Args: |
|
|
input_dir (str): Directory containing .nrrd files |
|
|
output_dir (str): Directory to save .nii.gz files |
|
|
recursive (bool): If True, search for .nrrd files in subdirectories |
|
|
""" |
|
|
|
|
|
Path(output_dir).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
pattern = "**/*.nrrd" if recursive else "*.nrrd" |
|
|
nrrd_files = list(Path(input_dir).glob(pattern)) |
|
|
|
|
|
print(f"Found {len(nrrd_files)} .nrrd files") |
|
|
|
|
|
for nrrd_file in nrrd_files: |
|
|
try: |
|
|
print(f"Converting {nrrd_file.name}") |
|
|
|
|
|
|
|
|
data, header = nrrd.read(str(nrrd_file)) |
|
|
|
|
|
|
|
|
space_directions = header.get("space directions") |
|
|
if space_directions is not None: |
|
|
voxel_size = np.array( |
|
|
[np.linalg.norm(dir) for dir in space_directions if dir is not None] |
|
|
) |
|
|
print("Voxel dimensions calculated from spatial direction matrix") |
|
|
else: |
|
|
raise ValueError( |
|
|
"No space directions found in NRRD header. Cannot determine voxel size." |
|
|
) |
|
|
|
|
|
|
|
|
origin = header.get("space origin", [0.0, 0.0, 0.0]) |
|
|
|
|
|
|
|
|
affine = np.eye(4) |
|
|
if space_directions is not None: |
|
|
affine[:3, :3] = np.array( |
|
|
[dir if dir is not None else [0, 0, 0] for dir in space_directions] |
|
|
) |
|
|
else: |
|
|
affine[:3, :3] = np.diag(voxel_size) |
|
|
affine[:3, 3] = origin |
|
|
|
|
|
|
|
|
nifti_img = nib.Nifti1Image(data, affine) |
|
|
|
|
|
|
|
|
nifti_header = nifti_img.header |
|
|
nifti_header.set_zooms(voxel_size) |
|
|
|
|
|
|
|
|
output_file = Path(output_dir) / f"{nrrd_file.stem}.nii.gz" |
|
|
|
|
|
|
|
|
nib.save(nifti_img, str(output_file)) |
|
|
print(f"Saved to {output_file}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {nrrd_file.name}: {e}") |
|
|
|
|
|
|
|
|
def convert_mha_to_nifti(input_dir, output_dir, recursive=False): |
|
|
""" |
|
|
Convert all .mha files in input_dir to .nii.gz files in output_dir |
|
|
|
|
|
Args: |
|
|
input_dir (str): Directory containing .mha files |
|
|
output_dir (str): Directory to save .nii.gz files |
|
|
recursive (bool): If True, search for .mha files in subdirectories |
|
|
""" |
|
|
|
|
|
Path(output_dir).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
pattern = "**/*.mha" if recursive else "*.mha" |
|
|
mha_files = list(Path(input_dir).glob(pattern)) |
|
|
|
|
|
print(f"Found {len(mha_files)} .mha files") |
|
|
|
|
|
for mha_file in mha_files: |
|
|
try: |
|
|
|
|
|
print(f"Converting {mha_file.name}") |
|
|
image = sitk.ReadImage(str(mha_file)) |
|
|
|
|
|
|
|
|
output_file = Path(output_dir) / f"{mha_file.stem}.nii.gz" |
|
|
|
|
|
|
|
|
sitk.WriteImage(image, str(output_file)) |
|
|
print(f"Saved to {output_file}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {mha_file.name}: {e}") |
|
|
|
|
|
|
|
|
def convert_nii_to_niigz(input_dir, output_dir, recursive=False): |
|
|
""" |
|
|
Convert all .nii files in input_dir to .nii.gz files in output_dir |
|
|
|
|
|
Args: |
|
|
input_dir (str): Directory containing .nii files |
|
|
output_dir (str): Directory to save .nii.gz files |
|
|
recursive (bool): If True, search for .nii files in subdirectories |
|
|
""" |
|
|
|
|
|
Path(output_dir).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
pattern = "**/*.nii" if recursive else "*.nii" |
|
|
nii_files = list(Path(input_dir).glob(pattern)) |
|
|
|
|
|
print(f"Found {len(nii_files)} .nii files") |
|
|
|
|
|
for nii_file in nii_files: |
|
|
try: |
|
|
|
|
|
print(f"Converting {nii_file.name}") |
|
|
image = sitk.ReadImage(str(nii_file)) |
|
|
|
|
|
|
|
|
output_file = Path(output_dir) / f"{nii_file.stem}.nii.gz" |
|
|
|
|
|
|
|
|
sitk.WriteImage(image, str(output_file)) |
|
|
print(f"Saved to {output_file}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {nii_file.name}: {e}") |
|
|
|
|
|
|
|
|
def _convert_mask_to_uint16(mask_path): |
|
|
|
|
|
nii = nib.load(mask_path) |
|
|
hdr = nii.header.copy() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = np.rint(nii.get_fdata()).astype(np.uint16) |
|
|
|
|
|
|
|
|
if hdr.get_data_dtype() != np.dtype("uint16"): |
|
|
hdr.set_data_dtype(np.uint16) |
|
|
|
|
|
|
|
|
slope, inter = hdr.get_slope_inter() |
|
|
|
|
|
|
|
|
slope_valid = slope is not None and np.isfinite(slope) and slope == 1 |
|
|
inter_valid = inter is not None and np.isfinite(inter) and inter == 0 |
|
|
if not (slope_valid and inter_valid): |
|
|
hdr.set_slope_inter(1.0, 0.0) |
|
|
|
|
|
out = nib.Nifti1Image(data, nii.affine, hdr) |
|
|
nib.save(out, mask_path) |
|
|
|
|
|
|
|
|
def convert_mask_to_uint16_per_dir(mask_folder, workers_limit=1): |
|
|
""" |
|
|
Convert all .nii.gz mask files in a folder to uint16 data type with proper header settings. |
|
|
This is useful for segmentation masks where we want integer labels without scaling. |
|
|
|
|
|
Args: |
|
|
mask_folder (str): Path to folder containing mask files |
|
|
""" |
|
|
|
|
|
mask_files = [f for f in os.listdir(mask_folder) if f.endswith(".nii.gz")] |
|
|
total_files = len(mask_files) |
|
|
num_workers = min(workers_limit, total_files) if workers_limit > 0 else 1 |
|
|
print(f"Found {total_files} .nii.gz mask files to convert") |
|
|
|
|
|
|
|
|
preprocessed_files_count = 0 |
|
|
failed_cases = [] |
|
|
with ProcessPoolExecutor(max_workers=num_workers) as executor: |
|
|
futures = { |
|
|
executor.submit( |
|
|
_convert_mask_to_uint16, os.path.join(mask_folder, mask_file) |
|
|
): mask_file |
|
|
for mask_file in mask_files |
|
|
} |
|
|
|
|
|
for fut in as_completed(futures): |
|
|
mask_file = futures[fut] |
|
|
try: |
|
|
fut.result() |
|
|
preprocessed_files_count += 1 |
|
|
print( |
|
|
f"✓ Converted {mask_file}: ({preprocessed_files_count}/{total_files})" |
|
|
) |
|
|
|
|
|
mem = psutil.virtual_memory().percent |
|
|
if mem > 80: |
|
|
print(f"⚠️ High memory usage: {mem}%") |
|
|
|
|
|
except Exception: |
|
|
err = traceback.format_exc() |
|
|
print(f"❌ Converting {mask_file} generated an exception:\n{err}") |
|
|
failed_cases.append((mask_file, err)) |
|
|
if failed_cases: |
|
|
print(f"❌ Failed to preprocessed {len(failed_cases)} files:") |
|
|
for mask_file, e in failed_cases: |
|
|
print(f" - {mask_file}: {e.splitlines()[-1]}") |
|
|
raise RuntimeError("Some tasks failed to load. See logs above.") |
|
|
|
|
|
|
|
|
def _copy_img_header_to_mask(img_file, mask_dir): |
|
|
base_name = os.path.basename(img_file) |
|
|
mask_file = os.path.join(mask_dir, base_name) |
|
|
if os.path.exists(mask_file): |
|
|
img = nib.load(img_file) |
|
|
mask = nib.load(mask_file) |
|
|
new_mask = nib.Nifti1Image(mask.get_fdata(), img.affine) |
|
|
nib.save(new_mask, mask_file) |
|
|
return mask_file |
|
|
|
|
|
|
|
|
def copy_img_header_to_mask(img_files, mask_dir, workers_limit=1): |
|
|
assert os.path.exists(mask_dir), "mask_dir must exist" |
|
|
total_files = len(img_files) |
|
|
num_workers = min(workers_limit, total_files) if workers_limit > 0 else 1 |
|
|
print(f"Found {total_files} .nii.gz mask files to convert") |
|
|
|
|
|
|
|
|
preprocessed_files_count = 0 |
|
|
failed_cases = [] |
|
|
with ProcessPoolExecutor(max_workers=num_workers) as executor: |
|
|
futures = { |
|
|
executor.submit(_copy_img_header_to_mask, img_file, mask_dir): img_file |
|
|
for img_file in img_files |
|
|
} |
|
|
|
|
|
for fut in as_completed(futures): |
|
|
img_file = futures[fut] |
|
|
try: |
|
|
mask_file = fut.result() |
|
|
preprocessed_files_count += 1 |
|
|
print( |
|
|
f"✓ Converted {mask_file}: ({preprocessed_files_count}/{total_files})" |
|
|
) |
|
|
|
|
|
mem = psutil.virtual_memory().percent |
|
|
if mem > 80: |
|
|
print(f"⚠️ High memory usage: {mem}%") |
|
|
|
|
|
except Exception: |
|
|
err = traceback.format_exc() |
|
|
print( |
|
|
f"❌ Copying header from {img_file} generated an exception:\n{err}" |
|
|
) |
|
|
failed_cases.append((img_file, err)) |
|
|
if failed_cases: |
|
|
print(f"❌ Failed to preprocessed {len(failed_cases)} files:") |
|
|
for img_file, e in failed_cases: |
|
|
print(f" - {img_file}: {e.splitlines()[-1]}") |
|
|
raise RuntimeError("Some tasks failed to load. See logs above.") |
|
|
|
|
|
|
|
|
def convert_bmp_to_niigz( |
|
|
bmp_dir, |
|
|
niigz_dir, |
|
|
slice_dim_type, |
|
|
pseudo_voxel_size, |
|
|
flip_dim0=False, |
|
|
flip_dim1=False, |
|
|
swap_dim01=False, |
|
|
): |
|
|
""" |
|
|
Convert BMP image files to NIfTI (.nii.gz) format. |
|
|
This function converts 2D BMP images to 3D NIfTI volumes with specified slice orientation. |
|
|
The output NIfTI files will have RAS+ orientation with specified voxel size. |
|
|
Args: |
|
|
bmp_dir (str): Input directory containing BMP files to convert |
|
|
niigz_dir (str): Output directory where NIfTI files will be saved |
|
|
slice_dim_type (int): Slice dimension/orientation type: |
|
|
0: Sagittal (YZ plane) |
|
|
1: Coronal (XZ plane) |
|
|
2: Axial (XY plane) |
|
|
pseudo_voxel_size (list): List of 3 floats specifying voxel dimensions in mm [x,y,z] |
|
|
flip_dim0 (bool, optional): If True, flip image along dimension 0. Defaults to False. |
|
|
flip_dim1 (bool, optional): If True, flip image along dimension 1. Defaults to False. |
|
|
swap_dim01 (bool, optional): If True, swap dimensions 0 and 1. Defaults to False. |
|
|
Returns: |
|
|
tuple: Original image dimensions (height, width) of the first converted BMP |
|
|
""" |
|
|
|
|
|
|
|
|
if slice_dim_type not in [0, 1, 2]: |
|
|
raise ValueError("slice_dim_type must be 0, 1, or 2") |
|
|
|
|
|
|
|
|
pseudo_voxel_size = list(pseudo_voxel_size) |
|
|
|
|
|
|
|
|
Path(niigz_dir).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
bmp_files = list(Path(bmp_dir).glob("*.bmp")) |
|
|
print(f"Found {len(bmp_files)} .bmp files") |
|
|
|
|
|
for bmp_file in bmp_files: |
|
|
try: |
|
|
print(f"Converting {bmp_file.name}") |
|
|
|
|
|
|
|
|
img_2d = cv2.imread(str(bmp_file), cv2.IMREAD_GRAYSCALE) |
|
|
img_size_dim0, img_size_dim1 = img_2d.shape |
|
|
|
|
|
|
|
|
if flip_dim0: |
|
|
img_2d = cv2.flip(img_2d, 0) |
|
|
if flip_dim1: |
|
|
img_2d = cv2.flip(img_2d, 1) |
|
|
if swap_dim01: |
|
|
img_2d = np.swapaxes(img_2d, 0, 1) |
|
|
|
|
|
|
|
|
if slice_dim_type == 0: |
|
|
img_3d = np.zeros( |
|
|
(1, img_2d.shape[0], img_2d.shape[1]), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[0, :, :] = img_2d |
|
|
elif slice_dim_type == 1: |
|
|
img_3d = np.zeros( |
|
|
(img_2d.shape[0], 1, img_2d.shape[1]), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[:, 0, :] = img_2d |
|
|
else: |
|
|
img_3d = np.zeros( |
|
|
(img_2d.shape[0], img_2d.shape[1], 1), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[:, :, 0] = img_2d |
|
|
|
|
|
|
|
|
|
|
|
affine = np.diag(pseudo_voxel_size + [1]) |
|
|
|
|
|
|
|
|
nii_img = nib.Nifti1Image(img_3d, affine) |
|
|
|
|
|
|
|
|
nii_img.header.set_zooms(pseudo_voxel_size) |
|
|
|
|
|
|
|
|
output_file = Path(niigz_dir) / f"{bmp_file.stem}.nii.gz" |
|
|
nib.save(nii_img, str(output_file)) |
|
|
print(f"Saved to {output_file}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {bmp_file.name}: {e}") |
|
|
|
|
|
return img_size_dim0, img_size_dim1 |
|
|
|
|
|
|
|
|
def convert_jpg_to_niigz( |
|
|
jpg_dir, |
|
|
niigz_dir, |
|
|
slice_dim_type, |
|
|
pseudo_voxel_size, |
|
|
flip_dim0=False, |
|
|
flip_dim1=False, |
|
|
swap_dim01=False, |
|
|
): |
|
|
""" |
|
|
Convert JPG image files to NIfTI (.nii.gz) format. |
|
|
This function converts 2D JPG images to 3D NIfTI volumes with specified slice orientation. |
|
|
The output NIfTI files will have RAS+ orientation with specified voxel size. |
|
|
Args: |
|
|
jpg_dir (str): Input directory containing JPG files to convert |
|
|
niigz_dir (str): Output directory where NIfTI files will be saved |
|
|
slice_dim_type (int): Slice dimension/orientation type: |
|
|
0: Sagittal (YZ plane) |
|
|
1: Coronal (XZ plane) |
|
|
2: Axial (XY plane) |
|
|
pseudo_voxel_size (list): List of 3 floats specifying voxel dimensions in mm [x,y,z] |
|
|
flip_dim0 (bool, optional): If True, flip image along dimension 0. Defaults to False. |
|
|
flip_dim1 (bool, optional): If True, flip image along dimension 1. Defaults to False. |
|
|
swap_dim01 (bool, optional): If True, swap dimensions 0 and 1. Defaults to False. |
|
|
Returns: |
|
|
tuple: Original image dimensions (height, width) of the first converted JPG |
|
|
""" |
|
|
|
|
|
|
|
|
if slice_dim_type not in [0, 1, 2]: |
|
|
raise ValueError("slice_dim_type must be 0, 1, or 2") |
|
|
|
|
|
|
|
|
pseudo_voxel_size = list(pseudo_voxel_size) |
|
|
|
|
|
|
|
|
Path(niigz_dir).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
jpg_files = list(Path(jpg_dir).glob("*.jpg")) |
|
|
print(f"Found {len(jpg_files)} .jpg files") |
|
|
|
|
|
for jpg_file in jpg_files: |
|
|
try: |
|
|
print(f"Converting {jpg_file.name}") |
|
|
|
|
|
|
|
|
img_2d = cv2.imread(str(jpg_file), cv2.IMREAD_GRAYSCALE) |
|
|
img_size_dim0, img_size_dim1 = img_2d.shape |
|
|
|
|
|
|
|
|
if flip_dim0: |
|
|
img_2d = cv2.flip(img_2d, 0) |
|
|
if flip_dim1: |
|
|
img_2d = cv2.flip(img_2d, 1) |
|
|
if swap_dim01: |
|
|
img_2d = np.swapaxes(img_2d, 0, 1) |
|
|
|
|
|
|
|
|
if slice_dim_type == 0: |
|
|
img_3d = np.zeros( |
|
|
(1, img_2d.shape[0], img_2d.shape[1]), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[0, :, :] = img_2d |
|
|
elif slice_dim_type == 1: |
|
|
img_3d = np.zeros( |
|
|
(img_2d.shape[0], 1, img_2d.shape[1]), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[:, 0, :] = img_2d |
|
|
else: |
|
|
img_3d = np.zeros( |
|
|
(img_2d.shape[0], img_2d.shape[1], 1), dtype=img_2d.dtype |
|
|
) |
|
|
img_3d[:, :, 0] = img_2d |
|
|
|
|
|
|
|
|
|
|
|
affine = np.diag(pseudo_voxel_size + [1]) |
|
|
|
|
|
|
|
|
nii_img = nib.Nifti1Image(img_3d, affine) |
|
|
|
|
|
|
|
|
nii_img.header.set_zooms(pseudo_voxel_size) |
|
|
|
|
|
|
|
|
output_file = Path(niigz_dir) / f"{jpg_file.stem}.nii.gz" |
|
|
nib.save(nii_img, str(output_file)) |
|
|
print(f"Saved to {output_file}") |
|
|
|
|
|
except Exception as e: |
|
|
print(f"Error converting {jpg_file.name}: {e}") |
|
|
|
|
|
return img_size_dim0, img_size_dim1 |
|
|
|