52 lines
2 KiB
Python
52 lines
2 KiB
Python
|
|
import os
|
||
|
|
import pandas as pd
|
||
|
|
|
||
|
|
# 要扫描的根目录
|
||
|
|
root_dir = '/home/onlylian/sorted_modality_files'
|
||
|
|
|
||
|
|
# 为 DataFrame 准备数据
|
||
|
|
data = []
|
||
|
|
|
||
|
|
# 遍历目录
|
||
|
|
for subdir, _, files in os.walk(root_dir):
|
||
|
|
for file in files:
|
||
|
|
if file.endswith('.nii.gz'):
|
||
|
|
parts = subdir.split('/')
|
||
|
|
try:
|
||
|
|
if len(parts) >= 8: # 标准结构
|
||
|
|
modalities = parts[4] # 提取模态类型
|
||
|
|
patient_id = parts[5] # 提取患者信息
|
||
|
|
date = parts[6] # 提取日期
|
||
|
|
name = file # 提取文件名
|
||
|
|
elif len(parts) == 7: # 当模态类型与具体模态合并时
|
||
|
|
modalities = parts[4] # 提取模态类型
|
||
|
|
patient_id = parts[5] # 提取患者信息
|
||
|
|
date = parts[6] # 提取日期
|
||
|
|
name = file # 提取文件名
|
||
|
|
elif len(parts) == 6: # 当只有日期和模态类型时
|
||
|
|
modalities = parts[4] # 提取模态类型
|
||
|
|
patient_id = parts[5] # 提取患者信息
|
||
|
|
date = '' # 没有日期信息
|
||
|
|
name = file # 提取文件名
|
||
|
|
else:
|
||
|
|
print(f"Path {subdir} does not have enough parts to extract data")
|
||
|
|
continue
|
||
|
|
data.append([modalities, patient_id, date, name])
|
||
|
|
print(f"Added: {modalities}/{patient_id}/{date}/{name}") # 调试信息
|
||
|
|
except IndexError as e:
|
||
|
|
print(f"Error processing path {subdir}: {e}")
|
||
|
|
|
||
|
|
# 检查数据是否已经添加
|
||
|
|
if not data:
|
||
|
|
print("No data found. Please check the directory structure and files.")
|
||
|
|
|
||
|
|
# 创建 DataFrame
|
||
|
|
df = pd.DataFrame(data, columns=['Modalities', 'patient_id', 'date', 'name'])
|
||
|
|
# 输出到 Excel
|
||
|
|
output_path = '/home/onlylian/patient_modalities_files.xlsx'
|
||
|
|
df.to_excel(output_path, index=False)
|
||
|
|
|
||
|
|
print(f"Excel 文件已生成在: {output_path}")
|
||
|
|
|
||
|
|
|