import numpy as np


def shuffle(im_label):
    """
    Shuffles labels in a label image to improve visualization and enhance
    object boundaries.

    Parameters
    ----------
    im_label : array_like
        A label image generated by segmentation methods.

    Returns
    -------
    Shuffled : array_like
        A label image where all values > 0 are randomly shuffled.

    See Also
    --------
    histomicstk.segmentation.label.CondenseLabel

    """
    from skimage import measure as ms

    # get list of unique object labels
    Unique = np.unique(im_label.flatten())

    # remove background objects (im_label == 0)
    Unique = np.delete(Unique, (Unique == 0).nonzero())

    # generate shuffled list of object values
    np.random.shuffle(Unique)

    # initialize output
    Shuffled = np.zeros(im_label.shape, dtype=np.uint32)

    # get pixel list for each object
    Props = ms.regionprops(im_label.astype(int))

    # fill in new values
    for i in range(len(Unique)):
        Coords = Props[i].coords
        Shuffled[Coords[:, 0], Coords[:, 1]] = Unique[i]

    return Shuffled
