def _from_singleton(x, ndim=2):
    """
    If x is an array of shape (1,1,...1) with ndim dimensions it returns the
    one single element in it. For other shapes it returns an error
    """
    s = (1,) * ndim
    if x.shape != s:
        raise ValueError(
            "Expected singleton shape %s for %s, found %s"
            % (
                s,
                x,
                x.shape,
            )
        )
    return x[(0,) * ndim]