文件夹|
医院|
病人|
序列
医院|
病人|
序列
import os;import sys;import glob;
# 默认同级目录
strFileRoot = r"D:\VisualStudioCode\PyTorchDemo\medical_handle\test"
strFileName = "filenames.txt" # 设置医院
samples = []
with open(os.path.join(strFileRoot, strFileName)) as f:
# **设置检索指定医院**
hospital_paths = [os.path.join(strFileRoot, hospital_line) for hospital_line in f.read().splitlines()]
# 自动检索每个医院下的病人
patient_paths = [glob.glob(os.path.join(hospital_path, "*")) for hospital_path in hospital_paths]
# 自动检索每个病人的序列
for patient_path in patient_paths:
for patient in patient_path:
# **设置检索指定序列**
def get_series(patient, raw_name):
path = os.path.join(patient, raw_name)
return path if os.path.exists(path) else None
patient_T1Imagefile = get_series(patient, "T1_reg_Axial.mhd")
patient_T1CEImagefile = get_series(patient, "T1CE_reg_Axial.mhd")
patient_Maskfile = get_series(patient, "T1_Segment_dilate.mhd")
if patient_T1Imagefile and patient_Maskfile and patient_T1CEImagefile:
samples.append(",".join([patient_T1Imagefile,patient_Maskfile,patient_T1CEImagefile]))
samples.append(",".join([patient_T1CEImagefile,patient_Maskfile,patient_T1Imagefile]))
if patient_T1Imagefile and patient_Maskfile and not patient_T1CEImagefile:
samples.append(",".join([patient_T1Imagefile,patient_Maskfile,patient_T1Imagefile]))
if not patient_T1Imagefile and patient_Maskfile and patient_T1CEImagefile:
samples.append(",".join([patient_T1CEImagefile,patient_Maskfile,patient_T1CEImagefile]))
...
...
...
len(samples)