46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import os
|
|
import shutil
|
|
import pandas as pd
|
|
|
|
# 讀取 Excel 文件
|
|
file_path = '/mnt/1218/onlylian/patient_modalities_files_20241031_181028.xlsx'
|
|
df = pd.read_excel(file_path)
|
|
|
|
# 定義來源和目標資料夾
|
|
source_dir = '/mnt/1218/onlylian/Complete_NEW_OAR_Files'
|
|
target_dir = '/mnt/1218/onlylian/sorted_OAR_files_test'
|
|
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):
|
|
# 構建目標資料夾路徑
|
|
modality_path = os.path.join(target_dir, modality, patient_id, date)
|
|
os.makedirs(modality_path, exist_ok=True)
|
|
|
|
# 遍歷來源資料夾中的所有檔案
|
|
for file_name in os.listdir(source_path):
|
|
# 更新條件檢查
|
|
if file_name.endswith('.nii.gz'): # 確保是正確的檔案類型
|
|
source_file = os.path.join(source_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"Source path not found: {source_path}")
|
|
else:
|
|
print(f"Unknown modality {modality} for patient {patient_id} on {date}")
|