def test_predict2D(model2d, use_channel): model = model2d img = real_image2d()[0] img = normalize(img, 1, 99.8) img = repeat(img, 2) axes = 'YX' if use_channel: img = img[...,np.newaxis] axes += 'C' ref_labels, ref_polys = model.predict_instances(img, axes=axes) res_labels, res_polys = model.predict_instances_big(img, axes=axes, block_size=288, min_overlap=32, context=96) m = matching(ref_labels, res_labels) assert (1.0, 1.0) == (m.accuracy, m.mean_true_score) m = matching(render_polygons(ref_polys, shape=img.shape), render_polygons(res_polys, shape=img.shape)) assert (1.0, 1.0) == (m.accuracy, m.mean_true_score) # sort them first lexicographic ref_inds = np.lexsort(ref_polys["points"].T) res_inds = np.lexsort(res_polys["points"].T) assert np.allclose(ref_polys["coord"][ref_inds], res_polys["coord"][res_inds],atol=1e-2) assert np.allclose(ref_polys["points"][ref_inds], res_polys["points"][res_inds],atol=1e-2) assert np.allclose(ref_polys["prob"][ref_inds], res_polys["prob"][res_inds],atol=1e-2) return ref_polys, res_polys
def test_predict3D(model3d): model = model3d img = real_image3d()[0] img = normalize(img, 1, 99.8) img = repeat(img, 2) ref_labels, ref_polys = model.predict_instances(img) res_labels, res_polys = model.predict_instances_big(img, axes='ZYX', block_size=(55, 105, 105), min_overlap=(13, 25, 25), context=(17, 30, 30)) m = matching(ref_labels, res_labels) assert (1.0, 1.0) == (m.accuracy, m.mean_true_score) # sort them first lexicographic ref_inds = np.lexsort(ref_polys["points"].T) res_inds = np.lexsort(res_polys["points"].T) assert np.allclose(ref_polys["dist"][ref_inds], res_polys["dist"][res_inds], atol=1e-2) assert np.allclose(ref_polys["points"][ref_inds], res_polys["points"][res_inds], atol=1e-2) assert np.allclose(ref_polys["prob"][ref_inds], res_polys["prob"][res_inds], atol=1e-2) return ref_polys, res_polys
def test_acc(img, grid): prob = edt_prob(img)[::grid[0],::grid[1]] dist = star_dist(img, n_rays=32, mode="cpp")[::grid[0],::grid[1]] points, probi, disti = non_maximum_suppression(dist, prob, grid = grid, prob_thresh=0.4) img2 = polygons_to_label(disti, points, shape=img.shape) m = matching(img, img2) acc = m.accuracy print("accuracy {acc:.2f}".format(acc=acc)) assert acc > 0.9
def test_acc(img): prob = edt_prob(img) dist = star_dist(img, n_rays=32, mode="cpp") coord = dist_to_coord(dist) points = non_maximum_suppression(coord, prob, prob_thresh=0.4) img2 = polygons_to_label(coord, prob, points, shape=img.shape) m = matching(img, img2) acc = m.accuracy print("accuracy {acc:.2f}".format(acc=acc)) assert acc > 0.9
def test_load_and_predict(model3d): model = model3d img, mask = real_image3d() x = normalize(img, 1, 99.8) prob, dist = model.predict(x, n_tiles=(1, 2, 2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x) assert labels.shape == img.shape[:3] stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (0, 30, 21) return model, labels
def test_acc_old(img, grid): from stardist.geometry.geom2d import _polygons_to_label_old, _dist_to_coord_old from stardist.nms import _non_maximum_suppression_old prob = edt_prob(img)[::grid[0],::grid[1]] dist = star_dist(img, n_rays=32, mode="cpp")[::grid[0],::grid[1]] coord = _dist_to_coord_old(dist, grid = grid) points = _non_maximum_suppression_old(coord, prob, prob_thresh=0.4, grid=grid) img2 = _polygons_to_label_old(coord, prob, points, shape=img.shape) m = matching(img, img2) acc = m.accuracy print("accuracy {acc:.2f}".format(acc=acc)) assert acc > 0.9
def test_load_and_predict(): model_path = path_model3d() model = StarDist3D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image3d() x = normalize(img,1,99.8) prob, dist = model.predict(x, n_tiles=(1,2,2)) assert prob.shape == dist.shape[:3] assert model.config.n_rays == dist.shape[-1] labels, _ = model.predict_instances(x) assert labels.shape == img.shape[:3] stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (0, 30, 21) return model, labels
def test_load_and_predict(): model_path = path_model2d() model = StarDist2D(None, name=model_path.name, basedir=str(model_path.parent)) img, mask = real_image2d() x = normalize(img,1,99.8) prob, dist = model.predict(x, n_tiles=(2,3)) assert prob.shape == dist.shape[:2] assert model.config.n_rays == dist.shape[-1] labels, polygons = model.predict_instances(x) assert labels.shape == img.shape[:2] assert labels.max() == len(polygons['coord']) assert len(polygons['coord']) == len(polygons['points']) == len(polygons['prob']) stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (1, 48, 17)
def test_load_and_predict(model2d): model = model2d img, mask = real_image2d() x = normalize(img, 1, 99.8) prob, dist = model.predict(x, n_tiles=(2, 3)) assert prob.shape == dist.shape[:2] assert model.config.n_rays == dist.shape[-1] labels, polygons = model.predict_instances(x) assert labels.shape == img.shape[:2] assert labels.max() == len(polygons['coord']) assert len(polygons['coord']) == len( polygons['points']) == len(polygons['prob']) stats = matching(mask, labels, thresh=0.5) assert (stats.fp, stats.tp, stats.fn) == (1, 48, 17) return labels
def test_pretrained_scales(): from scipy.ndimage import zoom from stardist.matching import matching from skimage.measure import regionprops model = StarDist2D.from_pretrained("2D_versatile_fluo") img, mask = real_image2d() x = normalize(img, 1, 99.8) def pred_scale(scale=2): x2 = zoom(x, scale, order=1) labels2, _ = model.predict_instances(x2) labels = zoom(labels2, tuple(_s1/_s2 for _s1, _s2 in zip(mask.shape, labels2.shape)), order=0) return labels scales = np.linspace(.5,5,10) accs = tuple(matching(mask, pred_scale(s)).accuracy for s in scales) print("scales ", np.round(scales,2)) print("accuracy ", np.round(accs,2)) return accs