import logging


def mask_sanity_fail(mask, shape, description):
    """
    Sanity check on training masks; shape needs to match.
    description affects the logging, on failure.
    """
    if mask is None:
        logging.warn("{} : mask is None".format(description))
        return True
    if mask.shape != shape:
        logging.warn("{} : shape is {}, (expecting {})".format(
            description, repr(mask.shape), repr(shape)))
        return True
    mm = mask[..., 1].mean()
    if mm > .5 or mm < .02:  # mostly should be .07 to .12
        logging.warn("{} : foreground mean {}".format(description, mm))
        return True