Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Convex Hull areas are actually perimeters for 2-dimensional input #12290

Closed
ljwolf opened this issue Jun 1, 2020 · 5 comments
Closed

DOC: Convex Hull areas are actually perimeters for 2-dimensional input #12290

ljwolf opened this issue Jun 1, 2020 · 5 comments
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org
Milestone

Comments

@ljwolf
Copy link
Contributor

ljwolf commented Jun 1, 2020

In scipy.spatial.ConvexHull, convex hulls expose an area and volume attribute. These are built on top of QHull. A user who computes a convex hull on 2-dimensional data will be surprised to find QHull's definitions of volume and area are dimension-dependent.

In 2-d, the convex hull is a polygon. Its surface is the edges of a polygon. So in 2-d, the 'area' is the length of the polygon's edges, while the 'volume' is the area of the polygon. (source)

So, convex_hull.area returns the perimeter of a shape in 2-d and the area of the shape in 3-d. In order for you to get the area of a 2-d convex hull, you need to use convex_hull.volume.

I would think it's appropriate to force volume to zero and area to Qhull's volume so that area has a consistent meaning across 2d and 3d+ for other packages (like shapely). Otherwise, we should modify the docstrings to read:

area : float
    Surface area of the convex hull when input dimension > 2. 
    When input is 2-dimensional, this is the perimeter of the convex hull. 

    .. versionadded:: 0.17.0
volume : float
    Volume of the convex hull when input dimension > 2. 
    When input is 2-dimensional, this is the area of the convex hull

    .. versionadded:: 0.17.0

Let me know what's preferred & I can submit a fix.

Reproducing code example:

import numpy
from scipy import spatial
import geopandas # for expected behavior

triangle = numpy.array([[0,0],[0,1],[1,0]])
square = numpy.array([[0,0], 
                           [0,1], 
                           [1,1,], 
                           [1,0]])
triangle_df = geopandas.GeoDataFrame(geometry=geopandas.points_from_xy(*triangle.T))

triangle_chull = spatial.ConvexHull(triangle)
triangle_chull_shapely = triangle_df.unary_union.convex_hull

triangle_chull.points[triangle_chull.vertices] # looks correct

assert triangle_chull_shapely.area == .5 * 1 * 1 # passes
assert triangle_chull.area == .5 * 1 * 1 # fails

Scipy/Numpy/Python version information:

I'm on scipy 1.4.1, numpy 1.18.1, and Python 3.8.2

@sjsrey
Copy link

sjsrey commented Jun 1, 2020

I think going for the consistent defn across dimensions is the way to go.

@pmla
Copy link
Contributor

pmla commented Jun 1, 2020

These are generalized surface areas and volumes, sometimes called "n-dimensional areas" and "n-dimensional volumes". This could be made clearer in the docstring. The description you provided from the QHull FAQ is very clear.

@tylerjereddy tylerjereddy added the Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org label Jun 1, 2020
@tylerjereddy tylerjereddy added this to the 1.6.0 milestone Jun 1, 2020
@tylerjereddy
Copy link
Contributor

+1 to making the documentation clearer rather than changing behavior, which could also break a fair bit of user code.

@ljwolf
Copy link
Contributor Author

ljwolf commented Jun 2, 2020

Sure, sounds good. Submitted in #12296. Thanks!

@tylerjereddy tylerjereddy changed the title Convex Hull areas are actually perimeters for 2-dimensional input DOC: Convex Hull areas are actually perimeters for 2-dimensional input Jun 3, 2020
@tylerjereddy
Copy link
Contributor

Fixed by the cross-linked merged PR, closing, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org
Projects
None yet
Development

No branches or pull requests

4 participants