|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
|
|
|
def main(): |
|
|
import argparse |
|
|
|
|
|
parser = argparse.ArgumentParser( |
|
|
description="Rename BraTS2023 case files to short names (t1c/t1n/t2f/t2w/seg.nii.gz)." |
|
|
) |
|
|
parser.add_argument( |
|
|
"--data_dir", |
|
|
type=str, |
|
|
default="./data/raw_data/BraTS2023/ASNR-MICCAI-BraTS2023-GLI-Challenge-TrainingData/", |
|
|
help="BraTS2023 directory that contains case folders.", |
|
|
) |
|
|
args = parser.parse_args() |
|
|
|
|
|
data_dir = args.data_dir |
|
|
if not os.path.isdir(data_dir): |
|
|
raise FileNotFoundError(f"data_dir not found: {data_dir}") |
|
|
|
|
|
all_cases = sorted(os.listdir(data_dir)) |
|
|
for case_name in all_cases: |
|
|
case_dir = os.path.join(data_dir, case_name) |
|
|
if not os.path.isdir(case_dir): |
|
|
continue |
|
|
|
|
|
for data_name in os.listdir(case_dir): |
|
|
if "-" not in data_name: |
|
|
continue |
|
|
|
|
|
new_name = data_name.split("-")[-1] |
|
|
new_path = os.path.join(case_dir, new_name) |
|
|
old_path = os.path.join(case_dir, data_name) |
|
|
|
|
|
if os.path.exists(new_path): |
|
|
|
|
|
continue |
|
|
|
|
|
os.rename(old_path, new_path) |
|
|
print(f"{new_path} 命名成功") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |
|
|
|