def calculate_agreement(pixel_image_1, pixel_image_2, substrate_idx=None):
    """
    Given two images containing pixel annotations, calculates agreement.
    If a substrate_idx is provided, calculates IoU over that substrate,
    otherwise calculates pixel accuracy
    
    Args:
        pixel_image_1: a numpy array  
        pixel_image_2: a numpy array
        substrate_idx: a valid substrate index

    Returns:
        Agreement metric (accuracy or IoU)
    """

    intersection = ((pixel_image_1 == substrate_idx) *
                    (pixel_image_2 == substrate_idx)).sum()

    union = ((pixel_image_1 == substrate_idx) +
             (pixel_image_2 == substrate_idx)).sum()

    # If there is no pixels with that class, IoU is undefined
    return intersection, union