LoicGoulefert / occult

vpype plug-in to remove occulted lines in SVG files
MIT License
68 stars 6 forks source link

ValueError: zero-size array to reduction operation minimum which has no identity #9

Closed very-meanly closed 3 years ago

very-meanly commented 3 years ago

Using occult sometimes produces the following error:

Traceback (most recent call last):
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vsketch_cli/threads.py", line 25, in run
    sketch = self._sketch_class.execute(seed=self._seed, finalize=False)
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vsketch/sketch_class.py", line 78, in execute
    bounds = vsk.document.bounds()
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vpype/model.py", line 680, in bounds
    [
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vpype/model.py", line 681, in <listcomp>
    self._layers[vid].bounds()
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vpype/model.py", line 359, in bounds
    raise e
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/vpype/model.py", line 353, in bounds
    line.real.min()
  File "~/.pyenv/versions/3.9.5/envs/creative-coding/lib/python3.9/site-packages/numpy/core/_methods.py", line 43, in _amin
    return umr_minimum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation minimum which has no identity

This happens when a LineString doesn't have any items, so the resulting Numpy array has zero items. I have attached a sketch that demonstrates the issue - if it doesn't reproduce on your machine, try randomizing the seed - you should eventually trigger the error. Requires vype (obviously) and vsketch to run - can't provide a requirements.txt since it's installed from a git clone (sorry) but I am using vpype 1.7.0.

cvety.zip

very-meanly commented 3 years ago

I was able to fix locally by changing the add_to_linecollection method to do this but I'm not sure if this is the preferred approach (specifically, the MultiLineString change might not be the right approach) - let me know if you want me to PR:

def add_to_linecollection(lc, line):
    """Helper function to add a LineString or a MultiLineString to a LineCollection"""
    if isinstance(line, LineString):
        if len(np.array(line).view(dtype=complex).reshape(-1)):
            lc.append(line)
    elif isinstance(line, MultiLineString):
        for l in line:
            add_to_linecollection(lc, l)
LoicGoulefert commented 3 years ago

Hi !

I recently updated the add_to_linecollection() function to adress a similar issue, can you try updating occult and tell me if this issue persists?

very-meanly commented 3 years ago

Sure! It's a bit late here but I'll give it a go tomorrow evening and report back.

very-meanly commented 3 years ago

I upgraded it and it seems using len(line.coords) != 0 does indeed fix the issue. Thanks!