def test(ba, bb): print("Testing evaluation") print("==================") print print("groundtruth with background: " + str(ba)) print("reconstruction with background: " + str(bb)) (a,b) = create_test_data(background_in_a = ba, background_in_b = bb, randomized = True) av = cremi.Volume(a) bv = cremi.Volume(b) evaluate = cremi.evaluation.NeuronIds(av) cremi_result_voi = evaluate.voi(bv) cremi_result_rand = evaluate.adapted_rand(bv) t = pyted.Ted() ted_result = t.create_report(a, b) print("CREMI:") print("\tvoi split : " + str(cremi_result_voi[0])) print("\tvoi merge : " + str(cremi_result_voi[1])) print("\tadapted Rand: " + str(cremi_result_rand)) print("TED :") print("\tvoi split : " + str(ted_result['voi_split'])) print("\tvoi merge : " + str(ted_result['voi_merge'])) print("\tadapted Rand: " + str(ted_result['adapted_rand_error']))
def prepare_file(self): if self.raw is not None: self.cremi_file.write_raw( cremi.Volume(self.raw[:], resolution=(40.0, 4.0, 4.0))) self.cremi_file.write_neuron_ids( cremi.Volume(self.seg[:], resolution=(40.0, 4.0, 4.0), offset=self.offset)) self.cremi_file.write_clefts( cremi.Volume(self.cleft_cc_np, resolution=(40.0, 4.0, 4.0), offset=self.offset)) self.cremi_file.write_volume( cremi.Volume(self.pre[:], resolution=(40.0, 4.0, 4.0), offset=self.offset), "volumes/pre_dist", np.uint8, ) self.cremi_file.write_volume( cremi.Volume(self.post[:], resolution=(40.0, 4.0, 4.0), offset=self.offset), "volumes/post_dist", np.uint8, )
def prepare_file(self): if self.raw is not None: self.cremi_file.write_raw( cremi.Volume(self.raw[:], resolution=(40.0, 4.0, 4.0))) self.cremi_file.write_neuron_ids( cremi.Volume(self.seg[:], resolution=(40.0, 4.0, 4.0), offset=self.offset)) self.cremi_file.write_clefts( cremi.Volume(self.cleft_cc[:], resolution=(40.0, 4.0, 4.0), offset=self.offset))
def labels_to_cremi(v): label_data = v.label_data.copy() if hasattr(v, 'bounds'): label_data = label_data[list(map(slice, list(v.bounds[0]), list(v.bounds[1])))] volume = cremi.Volume(label_data, resolution=v.resolution) return volume
def evaluate_volume( volumes, gt_name, pred_name, partition=False, border_threshold=None, use_gt_mask=True, relabel=False): # TODO: This is very intrusive into Volumes and should be refactored to # handle much of the partioned access and resampling there. import cremi if partition: _, volumes = partition_volumes(volumes, downsample=False) def labels_to_cremi(v): label_data = v.label_data.copy() if hasattr(v, 'bounds'): label_data = label_data[list(map(slice, list(v.bounds[0]), list(v.bounds[1])))] volume = cremi.Volume(label_data, resolution=v.resolution) return volume gt_vol = volumes[gt_name] pred_vol = volumes[pred_name] logging.info('GT shape: %s\t Prediction shape:%s', gt_vol.shape, pred_vol.shape) pred_upsample = gt_vol._get_downsample_from_resolution(pred_vol.resolution) if np.any(pred_upsample > 0): scale = np.exp2(pred_upsample).astype(np.int64) logging.warn('Segmentation is different resolution than groundtruth. Upsampling by %s.', scale) pred_data = pred_vol.label_data if hasattr(pred_vol, 'bounds'): pred_data = pred_data[list(map(slice, list(pred_vol.bounds[0]), list(pred_vol.bounds[1])))] orig_shape = pred_data.shape pred_data = np.lib.stride_tricks.as_strided(pred_data, [b for a in zip(orig_shape, scale) for b in a], [b for a in zip(pred_data.strides, [0, 0, 0]) for b in a]) new_shape = np.array(orig_shape) * scale pred_data = pred_data.reshape(list(new_shape)) padding = np.array(gt_vol.shape) - new_shape if np.any(padding > 0): logging.warn('Padding segmentation (%s) to be groundtruth size (%s)', new_shape, gt_vol.shape) pred_data = np.pad(pred_data, zip([0, 0, 0], list(padding)), 'edge') pred = cremi.Volume(pred_data, resolution=gt_vol.resolution) else: pred = labels_to_cremi(pred_vol) gt = labels_to_cremi(gt_vol) # Some augmented CREMI volumes have not just a uint64 -1 as background, but # several large values. Set these all to background to avoid breaking # coo_matrix. gt.data[gt.data > np.uint64(-10)] = np.uint64(-1) background_label_id = 0 pred.data[pred.data > np.uint64(-10)] = background_label_id if use_gt_mask and gt_vol.mask_data is not None: logging.warn('Groundtruth has a mask channel that will be applied to segmentation.') mask_data = gt_vol.mask_data if hasattr(gt_vol, 'bounds'): mask_data = mask_data[list(map(slice, list(gt_vol.bounds[0]), list(gt_vol.bounds[1])))] if relabel: mask_exiting_bodies = np.unique(pred.data[np.logical_not(mask_data)]) pred.data[np.logical_not(mask_data)] = background_label_id if relabel: from skimage import morphology pred_copy = np.zeros_like(pred.data) exiting_bodies_mask = np.isin(pred.data, mask_exiting_bodies) pred_copy[exiting_bodies_mask] = pred.data[exiting_bodies_mask] new_pred = morphology.label(pred_copy, background=background_label_id, connectivity=2) pred.data[exiting_bodies_mask] = new_pred[exiting_bodies_mask] gt_neuron_ids = cremi.evaluation.NeuronIds(gt, border_threshold=border_threshold) (voi_split, voi_merge) = gt_neuron_ids.voi(pred) adapted_rand = gt_neuron_ids.adapted_rand(pred) print('VOI split :', voi_split) print('VOI merge :', voi_merge) print('Adapted Rand-index:', adapted_rand) print('CREMI :', np.sqrt((voi_split + voi_merge) * adapted_rand))