Skip to content

ufunc 'create_collection' not supported for the input types #2098

Closed

Description

@mwtoews

This is a recent issue identified using nightly numpy binary wheels with the main branch of shapely.

The error looks like this:

=========================== test session starts ============================
platform linux -- Python 3.10.12, pytest-8.2.2, pluggy-1.5.0
benchmark: 4.0.0 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/mtoews/src/shapely/shapely
configfile: setup.cfg
plugins: syrupy-4.6.1, anyio-4.4.0, dotenv-0.5.2, flaky-3.8.1, hypothesis-6.108.5, cov-5.0.0, xdist-3.6.1, benchmark-4.0.0
collected 9 items / 1 error                                                

================================== ERRORS ==================================
__________ ERROR collecting shapely/tests/geometry/test_coords.py __________
shapely/tests/geometry/test_coords.py:5: in <module>
    from shapely.tests.common import line_string, line_string_z, point, point_z
shapely/tests/common.py:26: in <module>
    multi_point = shapely.MultiPoint([(0, 0), (1, 2)])
shapely/geometry/multipoint.py:62: in __new__
    return shapely.multipoints(subs)
shapely/decorators.py:77: in wrapped
    return func(*args, **kwargs)
shapely/creation.py:433: in multipoints
    return lib.create_collection(geometries, typ, out=out, **kwargs)
E   TypeError: ufunc 'create_collection' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
========================= short test summary info ==========================
ERROR shapely/tests/geometry/test_coords.py - TypeError: ufunc 'create_collection' not supported for the input types,...
!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!
============================= 1 error in 0.41s =============================

I've bisected the issue to numpy/numpy@f3b669d from numpy/numpy#26905 (ping @seberg) which is related to NEP 50.

Activity

  1. mwtoews commented on Jul 28, 2024

    @mwtoews
    MemberAuthor

    Here is a short reproducer:

    >>> import shapely
    >>> from shapely import GeometryType
    >>> pts = shapely.from_wkt(["POINT(1 2)", "POINT(3 4)"])
    >>> shapely.lib.create_collection(pts, GeometryType.MULTIPOINT)

    where the last line would normally return <MULTIPOINT ((1 2), (3 4))> before the referenced numpy commit or the error:

    TypeError: ufunc 'create_collection' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

    But also, this works:

    >>> GeometryType.MULTIPOINT
    <GeometryType.MULTIPOINT: 4>
    >>> shapely.lib.create_collection(pts, 4)
    <MULTIPOINT ((1 2), (3 4))>

    so there is maybe unsafe casting from the IntEnum GeometryType.MULTIPOINT to the int 4 (NPY_INT)?

    Other ufunc info for shapely.lib.create_collection:

    >>> np.info(shapely.lib.create_collection)
    create_collection(x1, x2, /, out=None, *, casting='same_kind', order='K', dtype=None, subok=True[, signature, axes, axis])
    >>> shapely.lib.create_collection.types
    ['Oi->O']
    >>> shapely.lib.create_collection.signature
    '(i),()->()'
  2. seberg commented on Jul 28, 2024

    @seberg

    Makes sense, I guess intenums may be int subclasses, is it a problem to cast to int() to make it i compatible (rather than int64/intp?)

  3. mwtoews commented on Jul 28, 2024

    @mwtoews
    MemberAuthor

    Thanks @seberg for taking a look; a few follow-up items/questions:

    • Firstly, yes casting int(GeometryType.MULTIPOINT) before sending to the ufunc is a possible change we can make.
    • But why does (e.g.) np.sqrt(GeometryType.MULTIPOINT) cast the enum to float64 without error? Is there a change that we could make on the C-side?
    • For what it's worth, I see np.result_type(GeometryType.MULTIPOINT) return dtype('int64').
    • Should there have been a warning that this API change was about to happen? I didn't see any before the breaking commit.
  4. seberg commented on Jul 29, 2024

    @seberg

    You can support the dfault int in C mainly. It's a pretty niche change and a warning is hard, because it is rare that it will trigger any change. Since most ufuncs will support the default integer explicitly.

  5. jorisvandenbossche commented on Jul 29, 2024

    @jorisvandenbossche
    Member

    @mwtoews thanks for the investigation and @seberg for the explanation.

    I was quickly checking in which places we should need such a cast, and opened a PR for that -> #2099

    We do have a set of other enums for modelling parameter options, and in all those cases it seems we were already casting the enum to an int. For example:

    if isinstance(mode, str):
    mode = SetPrecisionMode.get_value(mode)
    elif not np.isscalar(mode):
    raise TypeError("mode only accepts scalar values")
    if mode == SetPrecisionMode.pointwise and geos_version < (3, 10, 0):
    warnings.warn(
    "'pointwise' is only supported for GEOS 3.10",
    UserWarning,
    stacklevel=2,
    )
    return lib.set_precision(geometry, grid_size, np.intc(mode), **kwargs)

    or the various buffer arguments:

    return lib.buffer(
    geometry,
    distance,
    np.intc(quad_segs),
    np.intc(cap_style),
    np.intc(join_style),
    mitre_limit,
    np.bool_(single_sided),
    **kwargs,
    )

    (although I see we consistently use np.intc(..) for the casting while in the PR I just opened I used a python integer int(..), will change that for consistency)

  6. seberg commented on Jul 29, 2024

    @seberg

    Sounds right. Is this a big enough problem to worry about delaying things in NumPy (also since 2.1 is probably be going to be relatively soon for Python 3.13 support).

  7. jorisvandenbossche commented on Jul 29, 2024

    @jorisvandenbossche
    Member

    I don't think so, we can always include this small fix in another 2.0.x release

  8. AlessandroMondin commented on Aug 19, 2024

    @AlessandroMondin

    Same problem with numpy 2.1.0 (released yesterday)

  9. ethan-moss commented on Aug 19, 2024

    @ethan-moss

    Same problem with numpy 2.1.0 (released yesterday)

    @AlessandroMondin - as a short-term solution, reverting back to numpy 2.0.1 has at least patched the issue for me. Hope that helps!

  10. jorisvandenbossche commented on Aug 19, 2024

    @jorisvandenbossche
    Member

    Shapely 2.0.6 is released that includes the fix (it's on pypi, conda-forge can still take a few hours)

  11. ChrisBarker-NOAA commented on Sep 4, 2024

    @ChrisBarker-NOAA

    I jsut found this issue with:

    python 3.11
    shapely 2.0.5
    numpy 2.1.0
    

    all conda-forge builds.

    updating to shapely 2.0.6 fixed it -- nice work!

    But maybe the 2.0.5 builds on conda-forge should be marked as broken?

    https://github.com/conda-forge/admin-requests?tab=readme-ov-file#mark-packages-as-broken-on-conda-forge

  12. martinfleis commented on Sep 4, 2024

    @martinfleis
    Contributor

    They're not broken, just not compatible with the latest numpy. Following this logic, we would have to mark as broken all releases prior to 2.0.6.

  13. ChrisBarker-NOAA commented on Sep 5, 2024

    @ChrisBarker-NOAA

    Got it -- but the dependencies are wrong, yes?

    That can be updated with:

    https://prefix.dev/blog/repodata_patching

    https://github.com/conda-forge/conda-forge-repodata-patches-feedstock

    I think shapely 2.0.5 should have:

    `numpy >=1.19,<2.1`
    

    rather than:

    `numpy >=1.19,<3`
    

    I could help with this if need be, though I don't have permissions on the feedstock, so maybe not?

  14. jorisvandenbossche commented on Sep 5, 2024

    @jorisvandenbossche
    Member

    Yeah, on conda-forge the numpy 2.0 support was added later, and for packages built before that, conda-forge automatically ads a numpy<2 constraint (even though shapely itself didn't have that).
    So in practice, it seems that indeed only shapely 2.0.5 could be patched with a numpy<2.1 (shapely 2.0.4 still has a numpy<2 upper pin). I suppose opening a PR for that in https://github.com/conda-forge/conda-forge-repodata-patches-feedstock is welcome (I don't have any permissions their either, it will need to be reviewed by a core conda-forge team member anyway)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions