def test_eval(): mdl = deepCR() var = np.zeros((10, 24, 24)) tpr, fpr = evaluate.roc(mdl, image=var, mask=var, thresholds=np.linspace(0, 1, 10)) assert tpr.shape == (10, ) (tpr, fpr), (tpr1, fpr1) = evaluate.roc(mdl, image=var, mask=var, thresholds=np.linspace(0, 1, 10), dilate=True) assert tpr1.shape == (10, )
def test(mdl_name, test_data_dir, save_path): print('Loading data...\n') test_data = np.load(test_data_dir, allow_pickle=True)[()] print('Data loaded...\n') images = [] masks = [] ignores = [] length = len(list(test_data.keys())) print('Data parsing...\n') for i, val in enumerate(test_data.values()): print(f'{i+1}/{length}', end='\r') img, mask, ign = val images.append(img) mask = np.ones_like(mask) - mask ign = np.ones_like(ign).astype(int) - ign masks.append(mask) ignores.append(ign) del img, mask, ign test_images = np.array(images) test_masks = np.array(masks) test_ignores = np.array(ignores) print('Data parsed...\n') del test_data, images, masks, ignores gc.collect() print('Model loading...\n') mdl = deepCR( mask= f'/global/home/users/kgb0255/projects/deepCR/james_codes/{mdl_name}/{mdl_name}.pth', hidden=32) print('Model testing...\n') tpr, fpr = evaluate.roc(mdl, image=test_images, mask=test_masks, ignore=test_ignores) np.save(save_path, np.array([tpr, fpr])) return
def test_eval_gen(): mdl = deepCR() # Generate fake data files cwd = os.getcwd() + '/' # Remove generated files if 'temp' in os.listdir(cwd): for root, dirs, files in os.walk(cwd + 'temp', topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) os.rmdir('temp') os.mkdir('temp') os.mkdir('temp/image') os.mkdir('temp/dark') var = np.zeros((2, 24, 24)) for i in range(6): np.save(cwd + 'temp/image/%d.npy' % i, var) np.save(cwd + 'temp/dark/%d.npy' % i, var) image_list = [ cwd + 'temp/image/' + f for f in os.listdir(cwd + 'temp/image') ] dark_list = [cwd + 'temp/dark/' + f for f in os.listdir(cwd + 'temp/dark')] # Evaluate tpr, fpr = evaluate.roc(mdl, image=image_list, mask=dark_list, sky=100, thresholds=np.linspace(0, 1, 10)) assert tpr.shape == (10, ) # Remove generated files for root, dirs, files in os.walk(cwd + 'temp', topdown=False): for name in files: os.remove(os.path.join(root, name)) for name in dirs: os.rmdir(os.path.join(root, name)) os.rmdir('temp')
def model_test(mdl_names): from deepCR import evaluate from deepCR import deepCR from deepCR.evaluate import roc_lacosmic _mdl_f435, _mdl_f606, _mdl_f814, _mdl_3xF=mdl_names test_dirs = np.load(os.path.join(base_dir,'test_dirs.npy'),allow_pickle = True) field_type = {'10595_2': 'GC', '10595_7': 'GC', '9442_1': 'GC', '9442_3': 'GC', '9442_5': 'GC', '10760_2': 'GAL', '10760_4': 'GAL', '10631_3': 'EX', '10631_1': 'EX', '10631_4': 'EX', '12103_a3': 'EX', '13498_b1': 'EX', '13737_2': 'GAL', '13737_3': 'GAL', '9490_a3': 'GAL', '10349_30': 'GC', '10005_10': 'GC', '10120_3': 'GC', '12513_2': 'GAL', '12513_3': 'GAL', '14164_9': 'EX', '13718_6': 'EX', '10524_7': 'GC', '10182_pb': 'GAL', '10182_pd': 'GAL', '9425_2': 'EX', '9425_4': 'EX', '9583_99': 'EX', '10584_13': 'GAL', '9978_5e': 'EX', '15100_2': 'EX', '15647_13': 'EX', '11340_11': 'GC', '13389_10': 'EX', '9694_6': 'EX', '10342_3': 'GAL', '14343_1': 'GAL', '10536_13': 'EX', '13057_1': 'GAL', '10260_7': 'GAL', '10260_5': 'GAL', '10407_3': 'GAL', '13375_4': 'EX', '13375_7': 'EX', '13364_95': 'GAL', '10190_28': 'GAL', '10190_13': 'GAL', '10146_4': 'GC', '10146_3': 'GC', '10775_ab': 'GC', '11586_5':'GC', '12438_1': 'EX', '13671_35': 'EX', '14164_1': 'GC', '9490_a2': 'GAL', '9405_6d': 'EX', '9405_4b': 'EX', '9450_14': 'EX', '10092_1': 'EX', '13691_11': 'GAL', '12058_12': 'GAL', '12058_16': 'GAL', '12058_1': 'GAL', '9450_16': 'EX', '10775_52': 'GC', '12602_1': 'GC', '12602_2': 'GC', '10775_29': 'GC', '10775_ad': 'GC', '12058_6': 'GAL', #NEW '14704_1': 'GAL', #NEW '13804_6': 'GAL' #NEW } f814_test_field_dirs = {'GC': [], 'EX': [], 'GAL': []} f606_test_field_dirs = {'GC': [], 'EX': [], 'GAL': []} f435_test_field_dirs = {'GC': [], 'EX': [], 'GAL': []} for _dir in test_dirs: arr = _dir.split('/') _filter = arr[-4] key = f'{arr[-3]}_{arr[-2]}' f_type = field_type[key] if _filter == 'f435w': f435_test_field_dirs[f_type].append(_dir) elif _filter == 'f606w': f606_test_field_dirs[f_type].append(_dir) elif _filter == 'f814w': f814_test_field_dirs[f_type].append(_dir) ''' ***************************************************************************************************************************** Testing F435W model ***************************************************************************************************************************** ''' print('------------------------------------------------------------') print('Testing F435W model on F435W testset') print('------------------------------------------------------------') f435w_mdl=deepCR(mask=os.path.join(base_dir,'deepCR.ACS-WFC.F435W',_mdl_f435)+'.pth') for f_type in list(f435_test_field_dirs.keys()): filter_field_test_dirs = f435_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(f435w_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC.F435W',f'F435W_{f_type}_deepCR.ACS-WFC.F435W.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) del f435w_mdl ''' ***************************************************************************************************************************** Testing F606W model ***************************************************************************************************************************** ''' print('------------------------------------------------------------') print('Testing F606W model on F606W testset') print('------------------------------------------------------------') f606w_mdl=deepCR(mask=os.path.join(base_dir,'deepCR.ACS-WFC.F606W',_mdl_f606)+'.pth') for f_type in list(f606_test_field_dirs.keys()): filter_field_test_dirs = f606_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(f606w_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC.F606W',f'F606W_{f_type}_deepCR.ACS-WFC.F606W.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) del f606w_mdl print('------------------------------------------------------------') print('Testing F606W model on F435W testset') print('------------------------------------------------------------') f606w_mdl=deepCR(mask=os.path.join(base_dir,'deepCR.ACS-WFC.F606W',_mdl_f606)+'.pth') for f_type in list(f435_test_field_dirs.keys()): filter_field_test_dirs = f435_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(f606w_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC.F606W',f'F435W_{f_type}_deepCR.ACS-WFC.F606W.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) del f606w_mdl ''' ***************************************************************************************************************************** Testing F814W model ***************************************************************************************************************************** ''' print('------------------------------------------------------------') print('Testing F814W model on F814W testset') print('------------------------------------------------------------') f814w_mdl=deepCR(mask=os.path.join(base_dir,'deepCR.ACS-WFC.F814W',_mdl_f814)+'.pth') for f_type in list(f814_test_field_dirs.keys()): filter_field_test_dirs = f814_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(f814w_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC.F814W',f'F814W_{f_type}_deepCR.ACS-WFC.F814W.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) del f814w_mdl ''' ***************************************************************************************************************************** Testing ACS/WFC model ***************************************************************************************************************************** ''' print('------------------------------------------------------------') print('Testing ACS/WFC model on F435W testset') print('------------------------------------------------------------') global_mdl=deepCR(mask=os.path.join(base_dir,'deepCR.ACS-WFC',_mdl_3xF)+'.pth') for f_type in list(f435_test_field_dirs.keys()): filter_field_test_dirs = f435_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(global_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC',f'F435W_{f_type}_deepCR.ACS-WFC.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) print('------------------------------------------------------------') print('Testing ACS/WFC model on F606W testset') print('------------------------------------------------------------') for f_type in list(f606_test_field_dirs.keys()): filter_field_test_dirs = f606_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(global_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC',f'F606W_{f_type}_deepCR.ACS-WFC.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) print('------------------------------------------------------------') print('Testing ACS/WFC model on F814W testset') print('------------------------------------------------------------') for f_type in list(f814_test_field_dirs.keys()): filter_field_test_dirs = f814_test_field_dirs[f_type] tpr_fpr, tpr_fpr_dilate = evaluate.roc(global_mdl,filter_field_test_dirs[::], mode = 'pair', dilate = True) tpr,fpr = tpr_fpr tpr_d,fpr_d = tpr_fpr_dilate fpr_d = fpr np.save(os.path.join(base_dir,'deepCR.ACS-WFC',f'F814W_{f_type}_deepCR.ACS-WFC.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) #DELETE gal del global_mdl # ***************************************************************************************************************************** # Testing LACosmic # ***************************************************************************************************************************** # ''' # if not os.path.exists(os.path.join(base_dir,'LACosmic')): # os.makedirs(os.path.join(base_dir,'LACosmic')) # print('------------------------------------------------------------') # print('Testing LACosmic on F435W testset') # print('------------------------------------------------------------') # for f_type in list(f435_test_field_dirs.keys()): # filter_field_test_dirs = f435_test_field_dirs[f_type] # tpr_fpr, tpr_fpr_dilate = roc_lacosmic(np.linspace(1,1000,200),filter_field_test_dirs[::20], mode = 'pair', dilate = True) # tpr,fpr = tpr_fpr # tpr_d,fpr_d = tpr_fpr_dilate # fpr_d = fpr # np.save(os.path.join(base_dir,'LACosmic',f'F435W_{f_type}_LACosmic.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) # print('------------------------------------------------------------') # print('Testing LACosmic on F606W testset') # print('------------------------------------------------------------') # for f_type in list(f606_test_field_dirs.keys()): # filter_field_test_dirs = f606_test_field_dirs[f_type] # tpr_fpr, tpr_fpr_dilate = roc_lacosmic(np.linspace(1,1000,200),filter_field_test_dirs[::20], mode = 'pair', dilate = True) # tpr,fpr = tpr_fpr # tpr_d,fpr_d = tpr_fpr_dilate # fpr_d = fpr # np.save(os.path.join(base_dir,'LACosmic',f'F606W_{f_type}_LACosmic.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) # print('------------------------------------------------------------') # print('Testing LACosmic on F814W testset') # print('------------------------------------------------------------') # for f_type in list(f814_test_field_dirs.keys()): # filter_field_test_dirs = f814_test_field_dirs[f_type] # tpr_fpr, tpr_fpr_dilate = roc_lacosmic(np.linspace(1,1000,200),filter_field_test_dirs[::20], mode = 'pair', dilate = True) # tpr,fpr = tpr_fpr # tpr_d,fpr_d = tpr_fpr_dilate # fpr_d = fpr # np.save(os.path.join(base_dir,'LACosmic',f'F814W_{f_type}_LACosmic.npy'),[[tpr,fpr],[tpr_d,fpr_d]]) return None