# Source code for cellpose.metrics

```"""
Copright © 2023 Howard Hughes Medical Institute, Authored by Carsen Stringer and Marius Pachitariu.
"""
import numpy as np
from . import utils, dynamics
from numba import jit
from scipy.optimize import linear_sum_assignment
from scipy.ndimage import convolve, mean

n_min = min(iou.shape, iou.shape)
costs = -(iou >= 0.5).astype(float) - iou / (2*n_min)
true_ind, pred_ind = linear_sum_assignment(costs)
iout[true_ind] = iou[true_ind,pred_ind]
preds[true_ind] = pred_ind+1
return iout, preds

""" boundary precision / recall / Fscore """
diams = [utils.diameters(lbl) for lbl in masks_true]
for j, scale in enumerate(scales):
diam = max(1, scale * diams[n])
rs, ys, xs = utils.circleMask([int(np.ceil(diam)), int(np.ceil(diam))])
filt = (rs <= diam).astype(np.float32)
otrue = convolve(otrue, filt)
opred = convolve(opred, filt)
tp = np.logical_and(otrue==1, opred==1).sum()
fp = np.logical_and(otrue==0, opred==1).sum()
fn = np.logical_and(otrue==1, opred==0).sum()
precision[j,n] = tp / (tp + fp)
recall[j,n] = tp / (tp + fn)
fscore[j] = 2 * precision[j] * recall[j] / (precision[j] + recall[j])
return precision, recall, fscore

""" AJI = intersection of all matched masks / union of all masks

Parameters
------------

masks_true: list of ND-arrays (int) or ND-array (int)
masks_pred: list of ND-arrays (int) or ND-array (int)

Returns
------------

aji : aggregated jaccard index for each set of masks

"""

inds = np.arange(0, masks_true[n].max(), 1, int)
overlap = overlap[inds[preds>0]+1, preds[preds>0].astype(int)]
aji[n] = overlap.sum() / union
return aji

""" average precision estimation: AP = TP / (TP + FP + FN)

This function is based heavily on the *fast* stardist matching functions
(https://github.com/mpicbg-csbd/stardist/blob/master/stardist/matching.py)

Parameters
------------

masks_true: list of ND-arrays (int) or ND-array (int)
masks_pred: list of ND-arrays (int) or ND-array (int)

Returns
------------

average precision at thresholds
number of true positives at thresholds
number of false positives at thresholds
number of false negatives at thresholds

"""
not_list = False
not_list = True
if not isinstance(threshold, list) and not isinstance(threshold, np.ndarray):
threshold = [threshold]

if n_pred[n] > 0:
for k,th in enumerate(threshold):
tp[n,k] = _true_positive(iou, th)
fp[n] = n_pred[n] - tp[n]
fn[n] = n_true[n] - tp[n]
ap[n] = tp[n] / (tp[n] + fp[n] + fn[n])

if not_list:
ap, tp, fp, fn = ap, tp, fp, fn
return ap, tp, fp, fn

@jit(nopython=True)
def _label_overlap(x, y):
""" fast function to get pixel overlaps between masks in x and y

Parameters
------------

x: ND-array, int
y: ND-array, int

Returns
------------

overlap: ND-array, int
matrix of pixel overlaps of size [x.max()+1, y.max()+1]

"""
# put label arrays into standard form then flatten them
#     x = (utils.format_labels(x)).ravel()
#     y = (utils.format_labels(y)).ravel()
x = x.ravel()
y = y.ravel()

# preallocate a 'contact map' matrix
overlap = np.zeros((1+x.max(),1+y.max()), dtype=np.uint)

# loop over the labels in x and add to the corresponding
# overlap entry. If label A in x and label B in y share P
# pixels, then the resulting overlap is P
# len(x)=len(y), the number of pixels in the whole image
for i in range(len(x)):
overlap[x[i],y[i]] += 1
return overlap

""" intersection over union of all mask pairs

Parameters
------------

Returns
------------

iou: ND-array, float
matrix of IOU pairs of size [x.max()+1, y.max()+1]

------------
How it works:
The overlap matrix is a lookup table of the area of intersection
between each set of labels (true and predicted). The true labels
are taken to be along axis 0, and the predicted labels are taken
to be along axis 1. The sum of the overlaps along axis 0 is thus
an array giving the total overlap of the true labels with each of
the predicted labels, and likewise the sum over axis 1 is the
total overlap of the predicted labels with each of the true labels.
Because the label 0 (background) is included, this sum is guaranteed
to reconstruct the total area of each label. Adding this row and
column vectors gives a 2D array with the areas of every label pair
added together. This is equivalent to the union of the label areas
except for the duplicated overlap area, so the overlap matrix is
subtracted to find the union matrix.

"""
n_pixels_pred = np.sum(overlap, axis=0, keepdims=True)
n_pixels_true = np.sum(overlap, axis=1, keepdims=True)
iou = overlap / (n_pixels_pred + n_pixels_true - overlap)
iou[np.isnan(iou)] = 0.0
return iou

def _true_positive(iou, th):
""" true positive at threshold th

Parameters
------------

iou: float, ND-array
array of IOU pairs
th: float
threshold on IOU for positive label

Returns
------------

tp: float
number of true positives at threshold

------------
How it works:
(1) Find minimum number of masks
(2) Define cost matrix; for a given threshold, each element is negative
the higher the IoU is (perfect IoU is 1, worst is 0). The second term
gets more negative with higher IoU, but less negative with greater
n_min (but that's a constant...)
(3) Solve the linear sum assignment problem. The costs array defines the cost
of matching a true label with a predicted label, so the problem is to
find the set of pairings that minimizes this cost. The scipy.optimize
function gives the ordered lists of corresponding true and predicted labels.
(4) Extract the IoUs fro these parings and then threshold to get a boolean array
whose sum is the number of true positives that is returned.

"""
n_min = min(iou.shape, iou.shape)
costs = -(iou >= th).astype(float) - iou / (2*n_min)
true_ind, pred_ind = linear_sum_assignment(costs)
match_ok = iou[true_ind, pred_ind] >= th
tp = match_ok.sum()
return tp

""" error in flows from predicted masks vs flows predicted by network run on image

This function serves to benchmark the quality of masks, it works as follows
1. The predicted masks are used to create a flow diagram
2. The mask-flows are compared to the flows that the network predicted

If there is a discrepancy between the flows, it suggests that the mask is incorrect.
Masks with flow_errors greater than 0.4 are discarded by default. Setting can be
changed in Cellpose.eval or CellposeModel.eval.

Parameters
------------

masks produced from running dynamics on dP_net,
dP_net: ND-array (float)
ND flows where dP_net.shape[1:] = maski.shape

Returns
------------

flow_errors: float array with length maski.max()
mean squared error between predicted flows and flows from masks
ND flows produced from the predicted masks

"""