def get_frames_for_sample(sample): path = os.path.join(cg.local_dir, 'images', sample['Patient_Class'], sample['Patient_ID'], sample['video_name_no_ext']) files = ff.find_all_target_files(['*.jpg'], path) images = sorted(files) return images
print(excel_file.shape) # build the excel sheet result = [] for i in range(excel_file.shape[0]): case = excel_file.iloc[i] patient_id = case['Patient_ID'] patient_class = case['Patient_Class'] print(patient_class, patient_id) # find all the movies belonged to this case movie_list = ff.sort_timeframe( ff.find_all_target_files(['*.avi'], os.path.join(cg.fc_main_dir, 'avi_movie_collection', patient_class, patient_id, 'Volume_Rendering_Movies')), 1, '_') assert len(movie_list) == 6 for m in movie_list: # get video_name and video_name_no_ext video_name = os.path.basename(m) video_name_sep = video_name.split('.') # remove .avi video_name_no_ext = video_name_sep[0] if len(video_name_sep) > 2: for ii in range(1, len(video_name_sep) - 1): video_name_no_ext += '.' video_name_no_ext += video_name_sep[ii] # get angle
# get patient_list: patient_id_list = np.unique(excel_file['Patient_ID']) patient_list = [] for p in patient_id_list: d = excel_file[excel_file['Patient_ID'] == p].iloc[0] patient_list.append([d['Patient_Class'],p]) # get their segmentations and find the ES timeframe_pick = [] for p in patient_list: patient_class = p[0] patient_id = p[1] # find segmentations segmentations = ff.sort_timeframe(ff.find_all_target_files(['pred_*.nii.gz'],os.path.join(cg.nas_main_dir,'predicted_seg',patient_class,patient_id,'seg-pred-0.625-4classes-connected-retouch-downsample')),2,'_') lv_volume_list = [] for s in segmentations: data = nb.load(s).get_fdata() count,_ = ff.count_pixel(data,1) lv_volume_list.append(count) min_val = min(lv_volume_list) lv_volume_list = np.asarray(lv_volume_list) index = np.where(lv_volume_list == min_val)[0] if index.shape[0] > 1: ValueError('two mins') ED = 0 ES = ff.find_timeframe(segmentations[index[0]],2,'_') if (ES-ED) % 2 == 0: mid_ES = (ES-ED) // 2
model_BASAL_t = 'model_batch2/model-U2_batch2_BASAL_t-032-*' model_BASAL_r = 'model_batch1/model-U2_batch1_BASAL_r-018-*' MODEL = [ model_s, model_2C_t, model_2C_r, model_3C_t, model_3C_r, model_4C_t, model_4C_r, model_BASAL_t, model_BASAL_r ] # prediction task list task_list = [ 's', '2C_t', '2C_r', '3C_t', '3C_r', '4C_t', '4C_r', 'BASAL_t', 'BASAL_r' ] task_num_list = [0] # define patient CT image list patient_list = ff.find_all_target_files(['Abnormal/*', 'Normal/*'], os.path.join(cg.main_dir, 'nii-images')) patient_list = np.repeat(patient_list, 30) print(len(patient_list)) print('finish loading all patient images') # define save folder: save_folder = os.path.join(cg.main_dir, 'predicted_seg') # load time assignment sheet two_timeframes = 1 ################################ # build the model shape = cg.dim + (1, ) model_inputs = [Input(shape)]
'_TO_', '_to_', 'CCTA', 'CTA', 'ccta', 'cta', 'ccta', 'HALF', 'Function', 'function' ] dicom_parameter_list = [ 'AccessionNumber', 'Manufacturer', 'ManufacturerModelName', 'StudyDescription', 'PatientSex', 'PatientAge', 'ProtocolName' ] # put each year into one excel file year_list = ['2020'] for Y in year_list: #year = ff.find_all_target_files([Y],os.path.join('/Data/McVeighLabSuper/wip/zhennong/')) #patients = ff.find_all_target_files(['AN*','CVC*','cvc*','Cvc*'],year[0]) patients = ff.find_all_target_files( ['0*/*', '1*/*'], os.path.join('/Data/McVeighLabSuper/dicom_images/', Y)) patient_id = [] scan_year = [] function = [] directories = [] directories_sub = [] Dicom = [] AccessionNumber = [] Manufacturer = [] ModelName = [] StudyDescription = [] Sex = [] Age = []
## # this script can do file copy and removal ## import os import numpy as np import function_list_VR as ff import shutil import pandas as pd #### copy files main_folder = '/Data/McVeighLabSuper/wip/zhennong/' patient_list = ff.find_all_target_files(['Abnormal/*/img-nii-0.625'], os.path.join(main_folder, 'nii-images')) save_folder = os.path.join(main_folder, 'upsample-nii-images') for p in patient_list: patient_id = os.path.basename(os.path.dirname(p)) patient_class = os.path.basename(os.path.dirname(os.path.dirname(p))) print(patient_id, patient_class) ff.make_folder([ os.path.join(save_folder, patient_class, patient_id), os.path.join(save_folder, patient_class, patient_id, 'img-nii-0.625') ]) img_list = ff.find_all_target_files(['*.nii.gz'], p) for i in img_list: print(os.path.basename(i))
break if (frameId % math.floor(frameRate) == 0): if count < 10: number = '000'+str(count) if count >=10: number = '00'+str(count) dest = os.path.join(save_folder,case['video_name_no_ext']+'-'+number+'.jpg') cv2.imwrite(dest,frame) count += 1 cap.release() # call(["ffmpeg", "-i", src, dest]) % this will cause some error (not extract exact 20 frames) in some avis # Now get how many frames it is. nb_frames = len(ff.find_all_target_files(['*.jpg'],save_folder)) data.append([case['video_name'],nb_frames]) print('done extraction') data_df = pd.DataFrame(data,columns = ['video_name','nb_frames']) data_file = pd.merge(excel_file,data_df,on = "video_name") data_file.to_excel(os.path.join(cg.fc_main_dir,'Patient_List/movie_list_w_classes_w_picked_timeframes_test.xlsx'),index=False) def main(): main_path = cg.local_dir movie_path = os.path.join(main_path,'original_movie') excel_file = os.path.join(cg.fc_main_dir,'Patient_List/movie_list_w_classes_w_picked_timeframes_test.xlsx') extract_timeframes(main_path,movie_path,excel_file)
# this script can assign the time frame of each patient that pre-trained AI will predict on # the main point here is that to collect a dataset with pre-trained AI predicted segmentation on some # time frame, we should not only have time frame 0 but should half ED half ES import os import numpy as np import function_list_VR as ff import shutil import pandas as pd np.random.seed(0) main_folder = '/Data/McVeighLabSuper/wip/zhennong/' # find all patients in the training dataset patient_list = ff.find_all_target_files(['Abnormal/*', 'Normal/*'], os.path.join(main_folder, 'nii-images')) total_num = len(patient_list) print(total_num) # 375 # make ED = 0 and ES = 1 randoml shuffle list a = [0] * int((total_num / 2)) # 187 a.extend([1] * int(total_num - int(total_num / 2))) a = np.asarray(a) np.random.shuffle(a) print(a, a.shape) # assign to each patient result = [] for i in range(0, len(patient_list)): p = patient_list[i]