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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import shutil
|
| 4 |
+
import argparse
|
| 5 |
+
import zipfile
|
| 6 |
+
import gdown
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# ====================================
|
| 10 |
+
# Dataset Info [!]
|
| 11 |
+
# ====================================
|
| 12 |
+
# Dataset: SegRap23
|
| 13 |
+
# Challenge: https://segrap2023.grand-challenge.org
|
| 14 |
+
# Data: https://segrap2023.grand-challenge.org/dataset/
|
| 15 |
+
# Format: .nii.gz
|
| 16 |
+
# ====================================
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def download_and_extract(dataset_dir, dataset_name):
|
| 20 |
+
# Download files
|
| 21 |
+
current_dir = os.getcwd()
|
| 22 |
+
os.chdir(dataset_dir)
|
| 23 |
+
tmp_dir = os.path.join(dataset_dir, "tmp")
|
| 24 |
+
os.makedirs(tmp_dir, exist_ok=True)
|
| 25 |
+
os.chdir(tmp_dir)
|
| 26 |
+
print(f"Downloading {dataset_name} dataset to {dataset_dir}...")
|
| 27 |
+
|
| 28 |
+
# ====================================
|
| 29 |
+
# Add download logic here [!]
|
| 30 |
+
# ====================================
|
| 31 |
+
# Download dataset
|
| 32 |
+
folder_id = "115mzmNlZRIewnSR2QFDwW_-RkNM0LC9D"
|
| 33 |
+
url = f"https://drive.google.com/drive/folders/{folder_id}"
|
| 34 |
+
gdown.download_folder(url, quiet=False, use_cookies=False)
|
| 35 |
+
|
| 36 |
+
# Extract training set zip files
|
| 37 |
+
zip_files = [
|
| 38 |
+
"SegRap2023/SegRap2023_Training_Set_120cases_OneHot_Labels.zip",
|
| 39 |
+
"SegRap2023/SegRap2023_Training_Set_120cases.zip",
|
| 40 |
+
]
|
| 41 |
+
passwords = [None, "segrap2023@uestc"]
|
| 42 |
+
for zip_file, password in zip(zip_files, passwords):
|
| 43 |
+
if os.path.exists(zip_file):
|
| 44 |
+
print(f"Extracting {zip_file}...")
|
| 45 |
+
if password:
|
| 46 |
+
# Use 7z for password-protected files
|
| 47 |
+
subprocess.run(["7z", "x", zip_file, f"-p{password}"], check=True)
|
| 48 |
+
else:
|
| 49 |
+
# Use zipfile for non-protected files
|
| 50 |
+
with zipfile.ZipFile(zip_file) as zf:
|
| 51 |
+
zf.extractall()
|
| 52 |
+
|
| 53 |
+
# Create destination directories
|
| 54 |
+
for dir_name in ["Images-contrastCT", "Images-CT", "Masks-Task1", "Masks-Task2"]:
|
| 55 |
+
os.makedirs(dir_name, exist_ok=True)
|
| 56 |
+
|
| 57 |
+
# Move training images
|
| 58 |
+
training_dir = "SegRap2023_Training_Set_120cases"
|
| 59 |
+
if not os.path.exists(training_dir):
|
| 60 |
+
raise FileNotFoundError(f"Training directory '{training_dir}' does not exist")
|
| 61 |
+
|
| 62 |
+
for subject_id in os.listdir(training_dir):
|
| 63 |
+
subject_path = os.path.join(training_dir, subject_id)
|
| 64 |
+
if not os.path.isdir(subject_path):
|
| 65 |
+
continue
|
| 66 |
+
for img_type, filename in [
|
| 67 |
+
("contrastCT", "image_contrast.nii.gz"),
|
| 68 |
+
("CT", "image.nii.gz"),
|
| 69 |
+
]:
|
| 70 |
+
src = os.path.join(subject_path, filename)
|
| 71 |
+
if os.path.exists(src):
|
| 72 |
+
dst = os.path.join(f"Images-{img_type}", f"{subject_id}.nii.gz")
|
| 73 |
+
shutil.move(src, dst)
|
| 74 |
+
|
| 75 |
+
# Move mask files
|
| 76 |
+
for task_num in [1, 2]:
|
| 77 |
+
labels_dir = os.path.join(
|
| 78 |
+
"SegRap2023_Training_Set_120cases_OneHot_Labels", f"Task00{task_num}"
|
| 79 |
+
)
|
| 80 |
+
if os.path.exists(labels_dir):
|
| 81 |
+
for file in os.listdir(labels_dir):
|
| 82 |
+
if file.endswith(".nii.gz"):
|
| 83 |
+
shutil.move(
|
| 84 |
+
os.path.join(labels_dir, file),
|
| 85 |
+
os.path.join(f"Masks-Task{task_num}", file),
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Move JSON files from SegRap2023 directory to current directory
|
| 89 |
+
segrap_dir = "SegRap2023"
|
| 90 |
+
if not os.path.exists(segrap_dir):
|
| 91 |
+
print(f"Error: Directory '{segrap_dir}' does not exist")
|
| 92 |
+
else:
|
| 93 |
+
for file in os.listdir(segrap_dir):
|
| 94 |
+
if file.endswith(".json"):
|
| 95 |
+
shutil.move(os.path.join(segrap_dir, file), file)
|
| 96 |
+
|
| 97 |
+
# Move folder to dataset_dir
|
| 98 |
+
folders_to_move = [
|
| 99 |
+
"Images-CT",
|
| 100 |
+
"Images-contrastCT",
|
| 101 |
+
"Masks-Task1",
|
| 102 |
+
"Masks-Task2",
|
| 103 |
+
]
|
| 104 |
+
for folder in folders_to_move:
|
| 105 |
+
move_folder(
|
| 106 |
+
os.path.join(tmp_dir, folder),
|
| 107 |
+
os.path.join(dataset_dir, folder),
|
| 108 |
+
create_dest=True,
|
| 109 |
+
)
|
| 110 |
+
# ====================================
|
| 111 |
+
|
| 112 |
+
print(f"Download and extraction completed for {dataset_name}")
|
| 113 |
+
os.chdir(dataset_dir)
|
| 114 |
+
shutil.rmtree(tmp_dir)
|
| 115 |
+
os.chdir(current_dir)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def move_folder(source_folder, destination_folder, create_dest=True):
|
| 119 |
+
"""
|
| 120 |
+
Moves a folder from source to destination.
|
| 121 |
+
|
| 122 |
+
Args:
|
| 123 |
+
source_folder (str): Path to the source folder to move
|
| 124 |
+
destination_folder (str): Path to the destination location
|
| 125 |
+
create_dest (bool): Whether to create the destination parent directory if it doesn't exist
|
| 126 |
+
|
| 127 |
+
Returns:
|
| 128 |
+
bool: True if successful, False otherwise
|
| 129 |
+
|
| 130 |
+
Raises:
|
| 131 |
+
FileNotFoundError: If the source folder doesn't exist
|
| 132 |
+
"""
|
| 133 |
+
# Check if source folder exists
|
| 134 |
+
if not os.path.exists(source_folder):
|
| 135 |
+
raise FileNotFoundError(f"Source folder does not exist: {source_folder}")
|
| 136 |
+
|
| 137 |
+
# Create destination directory if it doesn't exist and create_dest is True
|
| 138 |
+
if create_dest and not os.path.exists(os.path.dirname(destination_folder)):
|
| 139 |
+
os.makedirs(os.path.dirname(destination_folder), exist_ok=True)
|
| 140 |
+
|
| 141 |
+
try:
|
| 142 |
+
# Move the folder
|
| 143 |
+
shutil.move(source_folder, destination_folder)
|
| 144 |
+
print(f"Successfully moved '{source_folder}' to '{destination_folder}'")
|
| 145 |
+
return True
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"Failed to move folder: {e}")
|
| 148 |
+
return False
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
# Set up argument parser
|
| 153 |
+
parser = argparse.ArgumentParser(description="Download and extract dataset")
|
| 154 |
+
parser.add_argument(
|
| 155 |
+
"-d",
|
| 156 |
+
"--dir_datasets_data",
|
| 157 |
+
help="Directory path where datasets will be stored",
|
| 158 |
+
required=True,
|
| 159 |
+
)
|
| 160 |
+
parser.add_argument(
|
| 161 |
+
"-n",
|
| 162 |
+
"--dataset_name",
|
| 163 |
+
help="Name of the dataset",
|
| 164 |
+
required=True,
|
| 165 |
+
)
|
| 166 |
+
args = parser.parse_args()
|
| 167 |
+
|
| 168 |
+
# Create dataset directory
|
| 169 |
+
dataset_dir = os.path.join(args.dir_datasets_data, args.dataset_name)
|
| 170 |
+
os.makedirs(dataset_dir, exist_ok=True)
|
| 171 |
+
|
| 172 |
+
# Change to dataset directory
|
| 173 |
+
os.chdir(dataset_dir)
|
| 174 |
+
|
| 175 |
+
# Download and extract dataset
|
| 176 |
+
download_and_extract(dataset_dir, args.dataset_name)
|