import os
import csv
import shutil
def read_csv(csv_file_path):
'''
csv_file_path: csv file absolute path
todo: get csv content
'''
with open(csv_file_path, 'r') as fp:
reader = csv.DictReader(fp)
reader.fieldnames = [name.strip() for name in reader.fieldnames]
metal_artifact_list, motion_artifact_list, foreign_body_artifact_list, beam_hardening_artifact_list =[],[],[],[]
for line in reader:
if line['metal_artifact'] == '1':
metal_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
if line['motion_artifact'] == '1':
motion_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
if line['foreign_body_artifact'] == '1':
foreign_body_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
if line['beam_hardening_artifact'] == '1':
beam_hardening_artifact_list.append(str(int(line['slice'])-1).zfill(3) + '.dcm')
return metal_artifact_list,motion_artifact_list,foreign_body_artifact_list,beam_hardening_artifact_list
def specific_dcm_copy(input_path,output_path,metal_list,motion_list,foreign_list,beam_list):
'''
four input: four artifact list
todo: copy specific dcm according artifact list
return: no return
'''
if metal_list:
metal_artifact_path = os.path.join(output_path,'metal_artifact')
if not os.path.exists(metal_artifact_path):
os.mkdir(metal_artifact_path)
for dcm in metal_list:
dcm_path = os.path.join(input_path, dcm)
shutil.copy(dcm_path,metal_artifact_path)
os.rename(os.path.join(metal_artifact_path,dcm), os.path.join(metal_artifact_path,os.path.basename(input_path)+'_'+dcm))
if motion_list:
motion_artifact_path = os.path.join(output_path,'motion_artifact')
if not os.path.exists(motion_artifact_path):
os.mkdir(motion_artifact_path)
for dcm in motion_list:
dcm_path = os.path.join(input_path, dcm)
shutil.copy(dcm_path,motion_artifact_path)
os.rename(os.path.join(motion_artifact_path,dcm), os.path.join(motion_artifact_path,os.path.basename(input_path)+'_'+dcm))
if foreign_list:
foreign_body_artifact_path = os.path.join(output_path,'foreign_body_artifact')
if not os.path.exists(foreign_body_artifact_path):
os.mkdir(foreign_body_artifact_path)
for dcm in foreign_list:
dcm_path = os.path.join(input_path, dcm)
shutil.copy(dcm_path,foreign_body_artifact_path)
os.rename(os.path.join(foreign_body_artifact_path,dcm), os.path.join(foreign_body_artifact_path,os.path.basename(input_path)+'_'+dcm))
if beam_list:
beam_hardening_artifact_path = os.path.join(output_path,'beam_hardening_artifact')
if not os.path.exists(beam_hardening_artifact_path):
os.mkdir(beam_hardening_artifact_path)
for dcm in beam_list:
dcm_path = os.path.join(input_path, dcm)
shutil.copy(dcm_path,beam_hardening_artifact_path)
os.rename(os.path.join(beam_hardening_artifact_path,dcm), os.path.join(beam_hardening_artifact_path,os.path.basename(input_path)+'_'+dcm))
def main():
csv_root_path = r'F:\CTBrain\brain_artifact\ann'
dcm_target_path = r'F:\CTBrain\brain_artifact\dcm'
dcm_ori_path = r'F:\CTBrain\brain_artifact\image'
csv_file_list = os.listdir(csv_root_path)
for csv_file in csv_file_list:
csv_file_path = os.path.join(csv_root_path,csv_file)
print('csv file name {}'.format(csv_file_path))
metal_list,motion_list,foreign_list,beam_list = read_csv(csv_file_path)
if not os.path.exists(dcm_target_path):
os.mkdir(dcm_target_path)
dcm_ori_sub_path = os.path.join(dcm_ori_path, csv_file.split('.')[0])
print(dcm_ori_sub_path)
specific_dcm_copy(dcm_ori_sub_path,dcm_target_path,metal_list,motion_list,foreign_list,beam_list)
if __name__ == '__main__':
main()
写的很蠢,先记录下来,以后有时间再优化