def scorer(pred,label,vxlspacing): """ :param pred: :param label: :param voxelspacing: :return: """ volscores = {} volscores['dice'] = metric.dc(pred,label) volscores['jaccard'] = metric.binary.jc(pred,label) volscores['voe'] = 1. - volscores['jaccard'] volscores['rvd'] = metric.ravd(label,pred) if np.count_nonzero(pred) ==0 or np.count_nonzero(label)==0: volscores['assd'] = 0 volscores['msd'] = 0 else: evalsurf = Surface(pred,label,physical_voxel_spacing = vxlspacing,mask_offset = [0.,0.,0.], reference_offset = [0.,0.,0.]) volscores['assd'] = evalsurf.get_average_symmetric_surface_distance() volscores['msd'] = metric.hd(label,pred,voxelspacing=vxlspacing) logging.info("\tDice " + str(volscores['dice'])) logging.info("\tJaccard " + str(volscores['jaccard'])) logging.info("\tVOE " + str(volscores['voe'])) logging.info("\tRVD " + str(volscores['rvd'])) logging.info("\tASSD " + str(volscores['assd'])) logging.info("\tMSD " + str(volscores['msd'])) return volscores
def get_assd(preds_path, labels_path, dicoms_path, result_path, ): test_time = time.strftime("%Y.%m.%d.%H.%M.%S", time.localtime()) result = os.path.join(result_path, "results.txt") assd = {} all_assd = 0 # 获取病人姓名 patients = os.listdir(preds_path) for patient in patients: # 获取标签集合和预测集合 pred_path = os.path.join(preds_path, patient) label_path = os.path.join(labels_path, patient) dicom_path = os.path.join(dicoms_path, patient) preds = sorted(glob.glob(os.path.join(pred_path, "*.bmp"))) labels = sorted(glob.glob(os.path.join(label_path, "*.bmp"))) # 创建array变量,存储每个病人图片数据 [width, height] = imageio.imread(preds[0]).shape pred_rawData = np.zeros((width, height, len(preds))) label_rawData = np.zeros((width, height, len(labels))) i = 0 for pred, label in zip(preds, labels): # 读取图片,获取numpy array pred_array = imageio.imread(pred) label_array = imageio.imread(label) # 进行二值化处理 pred_rawData[:, :, i] = pred_array / 255 label_rawData[:, :, i] = label_array / 255 # 循环将每张图片数据进行转换并保存 i += 1 # 对array进行数据格式转换 pred_rawData = pred_rawData.astype(int) label_rawData = label_rawData.astype(int) # print("*"*30) print(dicom_path) pixel_info = get_pixel_info(dicom_path) print(patient, "_像素间距:", pixel_info) # print("*"*30) evalsurf = Surface(pred_rawData, label_rawData, physical_voxel_spacing=pixel_info, mask_offset=[0., 0., 0.], reference_offset=[0., 0., 0.]) assd[patient] = evalsurf.get_average_symmetric_surface_distance() per_result = "'%s'_the average symmetric surface distance:%.2f" % (patient, assd[patient]) print(per_result) with open(result, mode='a') as file: file.write(test_time) file.write(" ") file.write(per_result) file.write("\n") for key in assd.keys(): all_assd += assd.get(key) average_assd = all_assd / len(assd) average_result = "AllPatients_the average symmetric surface distance:%.2f" % (average_assd) print(average_result) with open(result, mode='a') as file: file.write(test_time) file.write(" ") file.write(average_result) file.write("\n") return assd
def compute_segmentation_scores(prediction_mask, reference_mask, voxel_spacing): """ Calculates metrics scores from numpy arrays and returns an dict. Assumes that each object in the input mask has an integer label that defines object correspondence between prediction_mask and reference_mask. :param prediction_mask: numpy.array, int :param reference_mask: numpy.array, int :param voxel_spacing: list with x,y and z spacing :return: dict with dice, jaccard, voe, rvd, assd, rmsd, and msd """ scores = { 'dice': [], 'jaccard': [], 'voe': [], 'rvd': [], 'assd': [], 'rmsd': [], 'msd': [] } p = (prediction_mask > 0) r = (reference_mask > 0) if np.any(p) and np.any(r): dice = metric.dc(p, r) jaccard = dice / (2. - dice) scores['dice'].append(dice) scores['jaccard'].append(jaccard) scores['voe'].append(1. - jaccard) scores['rvd'].append(metric.ravd(r, p)) evalsurf = Surface(p, r, physical_voxel_spacing=voxel_spacing, mask_offset=[0., 0., 0.], reference_offset=[0., 0., 0.]) assd = evalsurf.get_average_symmetric_surface_distance() rmsd = evalsurf.get_root_mean_square_symmetric_surface_distance() msd = evalsurf.get_maximum_symmetric_surface_distance() scores['assd'].append(assd) scores['rmsd'].append(rmsd) scores['msd'].append(msd) else: # There are no objects in the prediction, in the reference, or both scores['dice'].append(0) scores['jaccard'].append(0) scores['voe'].append(1.) # Surface distance (and volume difference) metrics between the two # masks are meaningless when any one of the masks is empty. Assign # maximum penalty. The average score for these metrics, over all # objects, will thus also not be finite as it also loses meaning. scores['rvd'].append(LARGE) scores['assd'].append(LARGE) scores['rmsd'].append(LARGE) scores['msd'].append(LARGE) return scores
def get_scores(pred,label,vxlspacing): volscores = {} volscores['dice'] = metric.dc(pred,label) volscores['jaccard'] = metric.binary.jc(pred,label) volscores['voe'] = 1. - volscores['jaccard'] volscores['rvd'] = metric.ravd(label,pred) if np.count_nonzero(pred) ==0 or np.count_nonzero(label)==0: volscores['assd'] = 0 volscores['msd'] = 0 else: evalsurf = Surface(pred,label,physical_voxel_spacing = vxlspacing,mask_offset = [0.,0.,0.], reference_offset = [0.,0.,0.]) volscores['assd'] = evalsurf.get_average_symmetric_surface_distance() volscores['msd'] = metric.hd(label,pred,voxelspacing=vxlspacing) return volscores