63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import pandas as pd
|
||
|
|
|
||
|
|
# 讀取 Excel 文件
|
||
|
|
file_path = '/home/onlylian/patient_modalities_files.xlsx'
|
||
|
|
df = pd.read_excel(file_path)
|
||
|
|
|
||
|
|
# 定義來源和目標資料夾
|
||
|
|
source_dir = '/home/onlylian/Complete_NEW_ORGAN_Target_Files'
|
||
|
|
target_dir = '/home/onlylian/sorted_OAR_Target_files'
|
||
|
|
modalities = ['CT', 'CTC', 'T1', 'T1C', 'T2', 'FLAIR', 'CT+CTC']
|
||
|
|
|
||
|
|
# 創建目標資料夾結構
|
||
|
|
for modality in modalities:
|
||
|
|
modality_path = os.path.join(target_dir, modality)
|
||
|
|
os.makedirs(modality_path, exist_ok=True)
|
||
|
|
|
||
|
|
# 遍歷每個病人ID和日期
|
||
|
|
for _, row in df.iterrows():
|
||
|
|
modality = row['Modalities']
|
||
|
|
patient_id = row['patient_id']
|
||
|
|
date = str(row['date'])
|
||
|
|
|
||
|
|
if modality in modalities:
|
||
|
|
# 構建來源資料夾路徑
|
||
|
|
source_path = os.path.join(source_dir, patient_id, date)
|
||
|
|
print(f"Checking source path: {source_path}") # 檢查路徑
|
||
|
|
if os.path.exists(source_path):
|
||
|
|
# 分別處理 ORGAN 和 RT 目錄
|
||
|
|
organ_path = os.path.join(source_path, 'ORGAN')
|
||
|
|
rt_path = os.path.join(source_path, 'RT')
|
||
|
|
|
||
|
|
# 構建目標資料夾路徑
|
||
|
|
modality_path = os.path.join(target_dir, modality, patient_id, date)
|
||
|
|
os.makedirs(modality_path, exist_ok=True)
|
||
|
|
|
||
|
|
# 遍歷 ORGAN 目錄中的所有檔案
|
||
|
|
if os.path.exists(organ_path):
|
||
|
|
for file_name in os.listdir(organ_path):
|
||
|
|
if file_name.endswith('.nii.gz'): # 檢查檔案格式
|
||
|
|
source_file = os.path.join(organ_path, file_name)
|
||
|
|
target_file = os.path.join(modality_path, file_name)
|
||
|
|
print(f"Copying {source_file} to {target_file}")
|
||
|
|
shutil.copy2(source_file, target_file)
|
||
|
|
else:
|
||
|
|
print(f"ORGAN folder not found: {organ_path}")
|
||
|
|
|
||
|
|
# 遍歷 RT 目錄中的所有檔案
|
||
|
|
if os.path.exists(rt_path):
|
||
|
|
for file_name in os.listdir(rt_path):
|
||
|
|
if file_name.endswith('.nii.gz'): # 檢查檔案格式
|
||
|
|
source_file = os.path.join(rt_path, file_name)
|
||
|
|
target_file = os.path.join(modality_path, file_name)
|
||
|
|
print(f"Copying {source_file} to {target_file}")
|
||
|
|
shutil.copy2(source_file, target_file)
|
||
|
|
else:
|
||
|
|
print(f"RT folder not found: {rt_path}")
|
||
|
|
else:
|
||
|
|
print(f"Source path not found: {source_path}")
|
||
|
|
else:
|
||
|
|
print(f"Unknown modality {modality} for patient {patient_id} on {date}")
|