def _get_most_aligned_peak(pred, orig): orig = np.array(orig) angle1 = abs(peak_utils.angle_last_dim(pred, orig[0])) angle2 = abs(peak_utils.angle_last_dim(pred, orig[1])) angle3 = abs(peak_utils.angle_last_dim(pred, orig[2])) argmax = np.argmax(np.stack([angle1, angle2, angle3], axis=-1), axis=-1) x, y, z = (orig.shape[1], orig.shape[2], orig.shape[3]) return orig[tuple([argmax] + np.ogrid[:x, :y, :z])]
def calc_peak_length_dice(classes, y_pred, y_true, max_angle_error=[0.9], max_length_error=0.1): score_per_bundle = {} bundles = dataset_specific_utils.get_bundle_names(classes)[1:] for idx, bundle in enumerate(bundles): y_pred_bund = y_pred[:, :, :, (idx * 3):(idx * 3) + 3] y_true_bund = y_true[:, :, :, (idx * 3):(idx * 3) + 3] # [x, y, z, 3] angles = abs(peak_utils.angle_last_dim(y_pred_bund, y_true_bund)) lenghts_pred = np.linalg.norm(y_pred_bund, axis=-1) lengths_true = np.linalg.norm(y_true_bund, axis=-1) lengths_binary = abs(lenghts_pred - lengths_true) < (max_length_error * lengths_true) lengths_binary = lengths_binary.flatten() gt_binary = y_true_bund.sum(axis=-1) > 0 gt_binary = gt_binary.flatten() # [bs*x*y] angles_binary = angles > max_angle_error[0] angles_binary = angles_binary.flatten() combined = lengths_binary * angles_binary f1 = my_f1_score(gt_binary, combined) score_per_bundle[bundle] = f1 return score_per_bundle
def get_weighted_mean_of_peaks(best_orig, tom, weight=0.5): """ Calculate weighted mean between best_orig peaks and TOM peaks. Args: best_orig: original peaks tom: prior weight: how much to apply prior (0: only original signal, 1: only prior) Returns: weighted mean """ angles = peak_utils.angle_last_dim(best_orig, tom) # make sure to take mean along smaller angle (<90 degree), not along the bigger one (>90 degree) tom[angles < 0] *= -1 # flip peak stacked = np.stack([best_orig, tom]) return np.average(stacked, axis=0, weights=[1 - weight, weight])
def calc_peak_dice(classes, y_pred, y_true, max_angle_error=[0.9]): score_per_bundle = {} bundles = dataset_specific_utils.get_bundle_names(classes)[1:] for idx, bundle in enumerate(bundles): y_pred_bund = y_pred[:, :, :, (idx * 3):(idx * 3) + 3] y_true_bund = y_true[:, :, :, (idx * 3):(idx * 3) + 3] # (x,y,z,3) angles = abs(peak_utils.angle_last_dim(y_pred_bund, y_true_bund)) angles_binary = angles > max_angle_error[0] gt_binary = y_true_bund.sum(axis=-1) > 0 f1 = f1_score(gt_binary.flatten(), angles_binary.flatten(), average="binary") score_per_bundle[bundle] = f1 return score_per_bundle