def apply_affine(coords, affine):
    """
    Apply affine transform to the given `coords`(x, y, z coordinates). The
    affine matrix needs to be provided.
    """
    assert coords is not None and len(coords) == 3, 'Must provide a list of'\
        'coordinates: [x, y, z]'
    M = affine[:3, :3]
    abc = affine[:3, 3]

    return M.dot(coords) + abc
    # return affine.dot(img)  # This is correct too.