Datasets:
File size: 20,714 Bytes
33736c9 | 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | 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.
"""
# Load the image
img = nib.load(nifti_path)
# Get original data type
original_dtype = img.get_fdata().dtype
# Check current orientation
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
# Convert to RAS+ orientation
canonical_img = nib.as_closest_canonical(img)
# Create new image with original dtype
reoriented_data = canonical_img.get_fdata().astype(original_dtype)
new_img = nib.Nifti1Image(reoriented_data, canonical_img.affine, header=img.header)
# Preserve original header information where possible
new_img.header.set_data_dtype(original_dtype)
# Save the reoriented image
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.
"""
# Find all .nii.gz files recursively in directory
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")
# Multi-process dataset reorientation
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
"""
# Create output directory if it doesn't exist
Path(output_dir).mkdir(parents=True, exist_ok=True)
# Get all .nrrd files in input directory
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}")
# Read NRRD file
data, header = nrrd.read(str(nrrd_file))
# Get spacing (voxel size)
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."
)
# Get origin
origin = header.get("space origin", [0.0, 0.0, 0.0])
# Create affine matrix
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
# Create NIfTI image
nifti_img = nib.Nifti1Image(data, affine)
# Set additional header information
nifti_header = nifti_img.header
nifti_header.set_zooms(voxel_size)
# Create output filename
output_file = Path(output_dir) / f"{nrrd_file.stem}.nii.gz"
# Save NIfTI file
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
"""
# Create output directory if it doesn't exist
Path(output_dir).mkdir(parents=True, exist_ok=True)
# Get all .mha files in input directory
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:
# Read .mha file
print(f"Converting {mha_file.name}")
image = sitk.ReadImage(str(mha_file))
# Create output filename
output_file = Path(output_dir) / f"{mha_file.stem}.nii.gz"
# Write as .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
"""
# Create output directory if it doesn't exist
Path(output_dir).mkdir(parents=True, exist_ok=True)
# Get all .nii files in input directory
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:
# Read .nii file
print(f"Converting {nii_file.name}")
image = sitk.ReadImage(str(nii_file))
# Create output filename
output_file = Path(output_dir) / f"{nii_file.stem}.nii.gz"
# Write as .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):
# Load nii
nii = nib.load(mask_path)
hdr = nii.header.copy()
# Convert data to uint16 type
# NOTE: When you cast to uint16 in NumPy, it truncates toward zero, it does not round
# e.g., 1.99995422.astype(np.uint16) → 1
data = np.rint(nii.get_fdata()).astype(np.uint16)
# Force header consistency
if hdr.get_data_dtype() != np.dtype("uint16"):
hdr.set_data_dtype(np.uint16)
# Force no scaling
slope, inter = hdr.get_slope_inter()
# NOTE: In NIfTI headers, scl_slope and scl_inter can be stored as NaN to mean "no scaling", i.e., both (1, 0) or (NaN, NaN) mean "no scaling"
# Check if slope and inter are numeric before using np.isfinite
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
"""
# List all .nii.gz files in the mask folder
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")
# Multi-process dataset concatenation
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")
# Multi-process dataset concatenation
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
"""
# Validate slice_dim_type
if slice_dim_type not in [0, 1, 2]:
raise ValueError("slice_dim_type must be 0, 1, or 2")
# Convert pseudo_voxel_size to list if it's not already
pseudo_voxel_size = list(pseudo_voxel_size)
# Create output directory
Path(niigz_dir).mkdir(parents=True, exist_ok=True)
# Get all BMP files
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}")
# Read BMP image
img_2d = cv2.imread(str(bmp_file), cv2.IMREAD_GRAYSCALE)
img_size_dim0, img_size_dim1 = img_2d.shape
# Note: this is definitely correct, DO NOT SWAP the order of transformations
if flip_dim0:
img_2d = cv2.flip(img_2d, 0) # 0 means flip vertically
if flip_dim1:
img_2d = cv2.flip(img_2d, 1) # 1 means flip horizontally
if swap_dim01: # this line should be AFTER slip_x and slip_y
img_2d = np.swapaxes(img_2d, 0, 1)
# Create 3D array based on slice_dim_type
if slice_dim_type == 0: # Sagittal (YZ plane)
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: # Coronal (XZ plane)
img_3d = np.zeros(
(img_2d.shape[0], 1, img_2d.shape[1]), dtype=img_2d.dtype
)
img_3d[:, 0, :] = img_2d
else: # Axial (XY plane)
img_3d = np.zeros(
(img_2d.shape[0], img_2d.shape[1], 1), dtype=img_2d.dtype
)
img_3d[:, :, 0] = img_2d
# Create affine matrix for RAS+ orientation
# Set voxel size to 0.1mm in all dimensions
affine = np.diag(pseudo_voxel_size + [1])
# Create NIfTI image
nii_img = nib.Nifti1Image(img_3d, affine)
# Set header information
nii_img.header.set_zooms(pseudo_voxel_size)
# Save as NIfTI file
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
"""
# Validate slice_dim_type
if slice_dim_type not in [0, 1, 2]:
raise ValueError("slice_dim_type must be 0, 1, or 2")
# Convert pseudo_voxel_size to list if it's not already
pseudo_voxel_size = list(pseudo_voxel_size)
# Create output directory
Path(niigz_dir).mkdir(parents=True, exist_ok=True)
# Get all JPG files
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}")
# Read JPG image
img_2d = cv2.imread(str(jpg_file), cv2.IMREAD_GRAYSCALE)
img_size_dim0, img_size_dim1 = img_2d.shape
# Note: this is definitely correct, DO NOT SWAP the order of transformations
if flip_dim0:
img_2d = cv2.flip(img_2d, 0) # 0 means flip vertically
if flip_dim1:
img_2d = cv2.flip(img_2d, 1) # 1 means flip horizontally
if swap_dim01: # this line should be AFTER flip_dim0 and flip_dim1
img_2d = np.swapaxes(img_2d, 0, 1)
# Create 3D array based on slice_dim_type
if slice_dim_type == 0: # Sagittal (YZ plane)
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: # Coronal (XZ plane)
img_3d = np.zeros(
(img_2d.shape[0], 1, img_2d.shape[1]), dtype=img_2d.dtype
)
img_3d[:, 0, :] = img_2d
else: # Axial (XY plane)
img_3d = np.zeros(
(img_2d.shape[0], img_2d.shape[1], 1), dtype=img_2d.dtype
)
img_3d[:, :, 0] = img_2d
# Create affine matrix for RAS+ orientation
# Set voxel size to 0.1mm in all dimensions
affine = np.diag(pseudo_voxel_size + [1])
# Create NIfTI image
nii_img = nib.Nifti1Image(img_3d, affine)
# Set header information
nii_img.header.set_zooms(pseudo_voxel_size)
# Save as NIfTI file
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
|