Skip to content

Array assert_eq doesn't check for equality on scalars #2680

Closed

Description

@TomAugspurger

Not sure if this is intentional, but I thought I would check:

In [41]: import dask.array as da; import dask.array.utils

In [42]: import numpy as np

In [43]: a = da.from_array(np.array([0]), chunks=1)

In [44]: x = np.array([1])

In [45]: da.utils.assert_eq(a[0], x[0])
# None

Activity

  1. TomAugspurger commented on Sep 14, 2017

    @TomAugspurger
    MemberAuthor

    This check seems fragile:

    dask/dask/array/utils.py

    Lines 99 to 103 in a1a75a7

    if _not_empty(a) and _not_empty(b):
    # Treat all empty arrays as equivalent
    assert a.shape == b.shape
    assert allclose(a, b, **kwargs)
    return

    I think that will short-circuit when a is empty.

    (Pdb++) _not_empty(a) and _not_empty(True)  # the second condition isn't checked
    ()
    (Pdb++) _not_empty(True)  # it is hit now, raises an error
    *** AttributeError: 'bool' object has no attribute 'shape'
  2. TomAugspurger commented on Sep 14, 2017

    @TomAugspurger
    MemberAuthor

    Huh

    In [40]: np.isscalar(np.array(1))
    Out[40]: False

    I'm not quite sure how to distinguish between empty arrays and a scalar.

  3. bmerry commented on Sep 15, 2017

    @bmerry
    Contributor

    I think that will short-circuit when a is empty.

    I imagine it should probably have been or rather than and.

  4. shoyer commented on Sep 15, 2017

    @shoyer
    Member

    I'm not quite sure how to distinguish between empty arrays and a scalar.

    getattr(x, 'size', 1) will be 1 for scalars and 0 for empty arrays.

    np.isscalar() has a pretty strange design. It checks for instances of np.generic types or their built-in Python equivalents (int, float, str, etc).

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