cytometer/utils.py
def prop_of_pixels_in_label(lab, mask):
    """
    Proportion of pixels in each label that belong to a mask.

    For example, if label "7" contains a total of 20 pixels, and 5 of those pixels have mask != 0, then the proportion
    is 5/20 = 0.25.

    :param lab: (row, col) np.ndarray with a label segmentation (all pixels with the same integer value belong to the
    same label).
    :param mask: (row, col) np.ndarray. Mask of pixels that will be counted.
    :return:

    * seg_labs: Vector with the list of unique labels in lab.
    * seg_prop: Vector with the proportion of masked pixels in each label.
    """

    # look-up tables so that lut[label] = npixels
    lut = np.zeros(shape=(np.max(lab)+1,), dtype=np.float32)
    lut_masked = np.zeros(shape=(np.max(lab)+1,), dtype=np.float32)

    # number of pixels in each label, after masking the labels
    seg_labs, seg_labs_counts = np.unique(lab * (mask != 0), return_counts=True)
    lut_masked[seg_labs] = seg_labs_counts

    # number of pixels in each label
    seg_labs, seg_labs_counts = np.unique(lab, return_counts=True)
    lut[seg_labs] = seg_labs_counts

    # proportion is the number of masked pixels / number of pixels per label
    idx = lut != 0
    lut_masked[idx] = lut_masked[idx] / lut[idx]

    # remove "0" label, which corresponds to the background
    seg_labs = seg_labs[seg_labs != 0]

    return seg_labs, lut_masked[seg_labs]

    Proportion of pixels in each label that belong to a mask.

    For example, if label "7" contains a total of 20 pixels, and 5 of those pixels have mask != 0, then the proportion
    is 5/20 = 0.25.

    :param lab: (row, col) np.ndarray with a label segmentation (all pixels with the same integer value belong to the
    same label).
    :param mask: (row, col) np.ndarray. Mask of pixels that will be counted.
    :return:

    * seg_labs: Vector with the list of unique labels in lab.
    * seg_prop: Vector with the proportion of masked pixels in each label.
    
Proportion of pixels in each label that belong to a mask.
For example, if label "7" contains a total of 20 pixels, and 5 of those pixels have mask != 0, then the proportion
is 5/20 = 0.25.
Proportion of pixels in each label that belong to a mask . For example if label " 7 " contains a total of 20 pixels and 5 of those pixels have mask ! = 0 then the proportion is 5 / 20 = 0 . 25 .
def prop_of_pixels_in_label(lab, mask):
    lut = np.zeros(shape=(np.max(lab)+1,), dtype=np.float32)
    lut_masked = np.zeros(shape=(np.max(lab)+1,), dtype=np.float32)
    seg_labs, seg_labs_counts = np.unique(lab * (mask != 0), return_counts=True)
    lut_masked[seg_labs] = seg_labs_counts
    seg_labs, seg_labs_counts = np.unique(lab, return_counts=True)
    lut[seg_labs] = seg_labs_counts
    idx = lut != 0
    lut_masked[idx] = lut_masked[idx] / lut[idx]
    seg_labs = seg_labs[seg_labs != 0]
    return seg_labs, lut_masked[seg_labs]
def prop_of_pixels_in_label ( lab , mask ) : lut = np . zeros ( shape = ( np . max ( lab ) + 1 , ) , dtype = np . float32 ) lut_masked = np . zeros ( shape = ( np . max ( lab ) + 1 , ) , dtype = np . float32 ) seg_labs , seg_labs_counts = np . unique ( lab * ( mask != 0 ) , return_counts = True ) lut_masked [ seg_labs ] = seg_labs_counts seg_labs , seg_labs_counts = np . unique ( lab , return_counts = True ) lut [ seg_labs ] = seg_labs_counts idx = lut != 0 lut_masked [ idx ] = lut_masked [ idx ] / lut [ idx ] seg_labs = seg_labs [ seg_labs != 0 ] return seg_labs , lut_masked [ seg_labs ]
Proportion of pixels in each label that belong to a mask.
Proportion of pixels in each label that belong to a mask .
"""
    Proportion of pixels in each label that belong to a mask.

    For example, if label "7" contains a total of 20 pixels, and 5 of those pixels have mask != 0, then the proportion
    is 5/20 = 0.25.

    :param lab: (row, col) np.ndarray with a label segmentation (all pixels with the same integer value belong to the
    same label).
    :param mask: (row, col) np.ndarray. Mask of pixels that will be counted.
    :return:

    * seg_labs: Vector with the list of unique labels in lab.
    * seg_prop: Vector with the proportion of masked pixels in each label.
    """ # look-up tables so that lut[label] = npixels # number of pixels in each label, after masking the labels # number of pixels in each label # proportion is the number of masked pixels / number of pixels per label # remove "0" label, which corresponds to the background
Vector with the list of unique labels in lab.
seg_prop: Vector with the proportion of masked pixels in each label.
Vector with the list of unique labels in lab . seg_prop : Vector with the proportion of masked pixels in each label .
lab
(row, col) np.ndarray with a label segmentation (all pixels with the same integer value belong to the
same label).
( row col ) np . ndarray with a label segmentation ( all pixels with the same integer value belong to the same label ) .
mask
(row, col) np.ndarray. Mask of pixels that will be counted.
( row col ) np . ndarray . Mask of pixels that will be counted .