当运行MONAI框架一直报错的情况下。
1. 不要慌
2. 查看数据格式是否有问题,dicom转nii批处理代码如下,使用dcmrtstruct2nii包
- #!/usr/bin/env python
- # coding: utf-8
-
- # In[21]:
-
-
- #separate labels from folder to dicom_label
- import os
- import shutil
-
- def find_file_starts(path):
- finds = []
- for i, j, k in os.walk(path):
- for file in k:
- if file.startswith("RS"):
- print(os.path.join(i, file))
- finds.append(os.path.join(i, file))
- return finds
-
-
- def movefile(oripath,tardir):
- filename = os.path.basename(oripath)
- tarpath = os.path.join(tardir, filename)
- #判断原始文件路劲是否存在
- if not os.path.exists(oripath):
- print('the dir is not exist:%s' % oripath)
- status = 0
- else:
- #判断目标文件夹是否存在
- if os.path.exists(tardir):
- #判断目标文件夹里原始文件是否存在,存在则删除
- if os.path.exists(tarpath):
- os.remove(tarpath)
- else:
- #目标文件夹不存在则创建目标文件夹
- os.makedirs(tardir)
- #移动文件
- shutil.move(oripath, tardir)
-
- status = 1
-
- return status
-
- ##for CTV
- path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_CT'
- label_files = find_file_starts(path_2_all_patients)
- tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\CTV\dicom_label'
- for file in label_files:
- movefile(file,tardir_loc)
-
- ##for OAR
- path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_CT'
- patient_names = os.listdir(path_2_all_patients)
-
- label_files = find_file_starts(path_2_all_patients)
- tardir_loc = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\OAR\dicom_label'
- #Due to different sort for label files, have to rename the labels and CTs
- for i,file in enumerate(label_files):
- new_name = file.replace("RS",str(i)+"RS")
- new_name = os.path.join(tardir_loc,os.path.basename(new_name))
- print(new_name)
- os.rename(file,new_name)#rename and copy the label
- #movefile(file,tardir_loc)
-
- for i,patient in enumerate(patient_names):
- os.rename(os.path.join(path_2_all_patients,patient),os.path.join(path_2_all_patients,str(i)+patient))#rename the dicom CT
-
-
- # In[ ]:
-
-
- '''
- #optional
- #transform dicom_CT to nii_CT
- !python -c "import dicom2nifti" || pip install -q dicom2nifti
- import os
- path_2_all_patients = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\dicom_CT'
- patients_folders = os.listdir(path_2_all_patients)
- path_out_data = r'C:\Users\jinpe\Desktop\MONAI_SEG\dataset\sis_trial\nii_CT'
- print(patients_folders)
- for i, patient in enumerate(patients_folders):
- dicom2nifti.dicom_series_to_nifti(os.path.join(path_2_all_patients, patient), os.path.join(path_out_data, str(i)+"_"+pati