| |
| """ |
| Specific renaming script for ACDC dataset (adapted for HuggingFace download structure) |
| Input: flat Images/ and Masks/ folders with patient###_frameXX[_gt].nii.gz |
| Output: per-patient subdirectories with renamed files: |
| - patient###_frameXX.nii.gz -> lvsa_SR_ED.nii.gz (lower frame) or lvsa_SR_ES.nii.gz (higher frame) |
| - patient###_frameXX_gt.nii.gz -> seg_lvsa_SR_ED.nii.gz (lower frame) or seg_lvsa_SR_ES.nii.gz (higher frame) |
| """ |
|
|
| import re |
| import shutil |
| from pathlib import Path |
| from collections import defaultdict |
| import argparse |
|
|
|
|
| def rename_acdc_files(source_dir: str, output_dir: str, dry_run: bool = True): |
| """ |
| Reorganise flat Images/Masks folders into per-patient directories with renamed files. |
| |
| Args: |
| source_dir: Directory containing Images/ and Masks/ subdirectories |
| output_dir: Directory where per-patient folders will be created |
| dry_run: If True, only show what would be done |
| """ |
| source_path = Path(source_dir) |
| output_path = Path(output_dir) |
| images_dir = source_path / "Images" |
| masks_dir = source_path / "Masks" |
|
|
| if not images_dir.exists() or not masks_dir.exists(): |
| print(f"Error: Expected Images/ and Masks/ subdirectories in {source_path}") |
| return |
|
|
| print(f"\n{'DRY RUN MODE' if dry_run else 'EXECUTING'}") |
| print("=" * 50) |
|
|
| |
| patients = defaultdict(dict) |
|
|
| for img_file in sorted(images_dir.glob("patient*_frame*.nii.gz")): |
| match = re.match(r'(patient\d+)_frame(\d+)\.nii\.gz', img_file.name) |
| if match: |
| pid, fnum = match.group(1), int(match.group(2)) |
| patients[pid].setdefault(fnum, {})['img'] = img_file |
|
|
| for gt_file in sorted(masks_dir.glob("patient*_frame*_gt.nii.gz")): |
| match = re.match(r'(patient\d+)_frame(\d+)_gt\.nii\.gz', gt_file.name) |
| if match: |
| pid, fnum = match.group(1), int(match.group(2)) |
| patients[pid].setdefault(fnum, {})['gt'] = gt_file |
|
|
| for pid in sorted(patients): |
| frame_files = patients[pid] |
| frame_numbers = sorted(frame_files.keys()) |
|
|
| print(f"\nProcessing: {pid}") |
|
|
| if len(frame_numbers) < 2: |
| print(f" Warning: Only {len(frame_numbers)} frame found, expected 2") |
| continue |
|
|
| ed_frame = frame_numbers[0] |
| es_frame = frame_numbers[-1] |
| print(f" ED frame: {ed_frame}, ES frame: {es_frame}") |
|
|
| patient_out = output_path / pid |
| if not dry_run: |
| patient_out.mkdir(parents=True, exist_ok=True) |
|
|
| rename_map = { |
| (ed_frame, 'img'): 'lvsa_SR_ED.nii.gz', |
| (ed_frame, 'gt'): 'seg_lvsa_SR_ED.nii.gz', |
| (es_frame, 'img'): 'lvsa_SR_ES.nii.gz', |
| (es_frame, 'gt'): 'seg_lvsa_SR_ES.nii.gz', |
| } |
|
|
| for (fnum, ftype), new_name in rename_map.items(): |
| if ftype in frame_files.get(fnum, {}): |
| src = frame_files[fnum][ftype] |
| dst = patient_out / new_name |
| if dry_run: |
| print(f" Would copy: {src.name} -> {pid}/{new_name}") |
| else: |
| shutil.copy2(src, dst) |
| print(f" Copied: {src.name} -> {pid}/{new_name}") |
|
|
| print("\n" + "=" * 50) |
| if dry_run: |
| print("DRY RUN COMPLETE - No files were actually copied") |
| print("To execute, run with --execute flag") |
| else: |
| print(f"COMPLETE - Output at {output_path}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Reorganise and rename ACDC dataset files') |
| parser.add_argument('--source', type=str, |
| default='/mnt/storage/home/ym1413/QC_data_preprocessing/raw_dataset/ACDC', |
| help='Source directory containing Images/ and Masks/') |
| parser.add_argument('--output', type=str, |
| default='/mnt/storage/home/ym1413/QC_data_preprocessing/sabotaged_dataset', |
| help='Output directory for per-patient folders') |
| parser.add_argument('--dry-run', action='store_true', default=True, |
| help='Show what would be done without executing (default)') |
| parser.add_argument('--execute', action='store_true', |
| help='Actually perform the copy and rename') |
|
|
| args = parser.parse_args() |
|
|
| if args.execute: |
| args.dry_run = False |
|
|
| rename_acdc_files(args.source, args.output, dry_run=args.dry_run) |
|
|
|
|
| if __name__ == "__main__": |
| main() |