import glob import csv df_list = [] ROOT_DIR = '../../../datasets/anonymized_mri/only_nii_directory/*/*' x = glob.glob(ROOT_DIR) for patient_dir in sorted(x): T2 = [] adc = [] highb = [] patient_id = [] age_year = [] patient_id = patient_dir.split('/')[-2] age_year = patient_dir.split('/')[-1] for path in sorted(glob.glob(f'../../../datasets/anonymized_mri/only_nii_directory/{patient_dir.split("/")[-2]}/{patient_dir.split("/")[-1]}/*/*')): if ('T2' in path or 't2' in path) and ('tra' in path or 'TRA' in path) and not ('seg' in path): T2 = path for path in sorted(glob.glob(f'../../../datasets/anonymized_mri/only_nii_directory/{patient_dir.split("/")[-2]}/{patient_dir.split("/")[-1]}/*')): if 'manual_adc' in path: manual_adc = path else: if 'ADC' in path and 'nii.gz' in path: adc = path print(path) if 'manual_b-1400' in path: highb1400 = path else: if 'b-2000' in path and 'nii.gz' in path: highb = path print(path) if manual_adc: adc = manual_adc if highb1400: highb = highb1400 df_list.append({'patient_id':patient_id,'age_year':age_year,'t2':T2,'adc':adc,'high_b':highb}) if not T2 or not adc or not highb: input(f'{patient_id} {age_year} {T2} {adc} {highb}') with open('csv_file.csv', 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=['patient_id','age_year','t2','adc','high_b'],delimiter=';') writer.writeheader() for data in df_list: writer.writerow(data)