Skip to content

Instantly share code, notes, and snippets.

@maweigert
Created May 11, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maweigert/17956ee161de844c2bc4957206cd338a to your computer and use it in GitHub Desktop.
Save maweigert/17956ee161de844c2bc4957206cd338a to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import label, zoom
from stardist.matching import matching
from stardist.plot import render_label, render_label_pred
np.random.seed(42)
def create_example():
rand_gt = np.random.uniform(0,1,(8,8))
rand_pred = rand_gt+.1*np.random.uniform(-1,1,(8,8))
y_true, _ = label(zoom(rand_gt, (32,32))>.7)
y_pred, _ = label(zoom(rand_pred, (32,32))>.7)
return y_true, y_pred
# create some example data
y_true, y_pred = create_example()
# Calculate the matching (with IoU threshold `thresh`) and all metrics
res = matching(y_true, y_pred, thresh=0.5)
print(res)
# plot the label masks
for i, (img,title) in enumerate(zip((render_label(y_true),
render_label(y_pred),
render_label_pred(y_true, y_pred)),
("True","Pred"," TP/FP/FN (green/red/blue)"))):
plt.subplot(1,3,i+1)
plt.imshow(img)
plt.axis("off")
plt.title(title, fontsize=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment