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

fail gracefully when no initial polygons generated – preprocessing.FaceArtifacts #664

Closed
jGaboardi opened this issue Nov 10, 2024 · 1 comment · Fixed by #666
Closed
Assignees

Comments

@jGaboardi
Copy link
Member

jGaboardi commented Nov 10, 2024

This is an edge case, but currently preprocessing.FaceArtifacts() fails rather cryptically when an edge skeleton is passed which polygons can be generated from – see here.

import geopandas, shapely, momepy 

p10 = shapely.Point(1,0)
p20 = shapely.Point(2,0)
p30 = shapely.Point(3,0)
p21 = shapely.Point(2,1)

line1020 = shapely.LineString((p10, p20))
line2030 = shapely.LineString((p20, p30))
line2021 = shapely.LineString((p20, p21))

edges = geopandas.GeoDataFrame(geometry=[line1020, line2030, line2021])

momepy.FaceArtifacts(edges)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[1], line 14
     10 line2021 = shapely.LineString((p20, p21))
     12 edges = geopandas.GeoDataFrame(geometry=[line1020, line2030, line2021])
---> 14 momepy.FaceArtifacts(edges)

File [~/miniforge3/envs/py312_sgeop-latest/lib/python3.12/site-packages/momepy/preprocessing.py:1637](http://localhost:8888/~/miniforge3/envs/py312_sgeop-latest/lib/python3.12/site-packages/momepy/preprocessing.py#line=1636), in FaceArtifacts.__init__(self, gdf, index, height_mins, height_maxs, prominence)
   1626 peak_parameters = {
   1627     "height_mins": height_mins,
   1628     "height_maxs": height_maxs,
   1629     "prominence": prominence,
   1630 }
   1631 mylinspace = np.linspace(
   1632     self.polygons["face_artifact_index"].min(),
   1633     self.polygons["face_artifact_index"].max(),
   1634     1000,
   1635 )
-> 1637 self.kde = gaussian_kde(
   1638     self.polygons["face_artifact_index"], bw_method="silverman"
   1639 )
   1640 self.pdf = self.kde.pdf(mylinspace)
   1642 # find peaks

File [~/miniforge3/envs/py312_sgeop-latest/lib/python3.12/site-packages/scipy/stats/_kde.py:199](http://localhost:8888/~/miniforge3/envs/py312_sgeop-latest/lib/python3.12/site-packages/scipy/stats/_kde.py#line=198), in gaussian_kde.__init__(self, dataset, bw_method, weights)
    197 self.dataset = atleast_2d(asarray(dataset))
    198 if not self.dataset.size > 1:
--> 199     raise ValueError("`dataset` input should have multiple elements.")
    201 self.d, self.n = self.dataset.shape
    203 if weights is not None:

ValueError: `dataset` input should have multiple elements.

I propose adding a simple check following that step which either raises or warns+returns immediatley if the polygon result data frame is empty.

@martinfleis I think a raise is more appropriate. Thoughts?

@martinfleis
Copy link
Member

I'd probably warn and return empty when there are no polygons.

jGaboardi added a commit to jGaboardi/momepy that referenced this issue Nov 11, 2024
@jGaboardi jGaboardi changed the title fail gracefully when no initial polygons generated – preprocessing. FaceArtifacts fail gracefully when no initial polygons generated – preprocessing.FaceArtifacts Nov 11, 2024
martinfleis pushed a commit that referenced this issue Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants