Skip to content

Why does numpy.broadcast not accept a single input array? #6899

Closed

Description

@kohr-h

I'm wondering what's the reasoning behind forbidding a single array as input to numpy.broadcast. When writing code which is supposed to work in N dimensions with N >= 1, it would be nice to be able to use broadcast as-is. A standard use case is the calculation of the shape of an output array when using meshgrids as input for function evaluation:

>>> coord_vecs = [[1, 2], [3, 4, 5]]
>>> mesh = np.meshgrid(*coord_vecs, indexing='ij', sparse=True)  # for large stuff
>>> out_shape = np.broadcast(*mesh).shape
>>> out_shape
(2, 3)
>>> coord_vecs = [[1, 2]]
>>> mesh = np.meshgrid(*coord_vecs, indexing='ij', sparse=True)  # still works
>>> out_shape = np.broadcast(*mesh).shape
Traceback (most recent call last):
...
ValueError: Need at least two and fewer than (32) array objects.

As noted above, meshgrid works and returns a list with one element, as expected. Why does broadcast not return a broadcast object with shape (2,) in the example above? The other public methods of such an object would probably also make sense with a single input array.

EDIT: Last sentence was wrong in the first version.

Activity

  1. kohr-h commented on Dec 30, 2015

    @kohr-h
    ContributorAuthor

    Apparently, there is one single place where this situation is caught. Without knowing much about internals, the function around that check seems to be completely generic, and simply replacing 2 with 1 could be the only change necessary to allow single-array(-like) input. No idea about side effects of such a change, though.

  2. shoyer commented on Dec 30, 2015

    @shoyer
    Member

    Well, maybe worth some playing around, then?

    I also find this surprising.

  3. kohr-h commented on Dec 30, 2015

    @kohr-h
    ContributorAuthor

    Seems to work right away with the proposed (trivial) change:

    >>> import numpy as np
    >>> np.__path__  # My local GH copy
    ['/home/hkohr/Software/numpy/numpy']
    >>> a = np.arange(3)
    >>> bc = np.broadcast(a)
    >>> bc.nd
    1
    >>> bc.numiter
    1
    >>> bc.shape
    (3,)
    >>> bc.size
    3

    I get three errors when running np.test(), but they're probably unrelated. I just post them for the record:

    ======================================================================
    ERROR: Failure: ImportError (cannot import name ccompiler)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
        addr.filename, addr.module)
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
        return self.importFromDir(dir_path, fqname)
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
        mod = load_module(part_fqname, fh, filename, desc)
      File "numpy/distutils/__init__.py", line 8, in <module>
        from . import ccompiler
      File "numpy/distutils/ccompiler.py", line 8, in <module>
        from distutils import ccompiler
      File "numpy/distutils/__init__.py", line 8, in <module>
        from . import ccompiler
      File "numpy/distutils/ccompiler.py", line 8, in <module>
        from distutils import ccompiler
    ImportError: cannot import name ccompiler
    
    ======================================================================
    ERROR: test suite for <module 'test_array_from_pyobj' from '/home/hkohr/Software/numpy/numpy/f2py/tests/test_array_from_pyobj.py'>
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/suite.py", line 209, in run
        self.setUp()
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/suite.py", line 292, in setUp
        self.setupContext(ancestor)
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/suite.py", line 315, in setupContext
        try_run(context, names)
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
        return func()
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/test_array_from_pyobj.py", line 42, in setup
        'test_array_from_pyobj_ext')
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 78, in wrapper
        memo[key] = func(*a, **kw)
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 312, in build_module_distutils
        __import__(module_name)
    ImportError: /tmp/tmpqyEUs_/test_array_from_pyobj_ext.so: failed to map segment from shared object
    
    ======================================================================
    ERROR: test_callback.TestF77Callback.test_string_callback
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/case.py", line 381, in setUp
        try_run(self.inst, ('setup', 'setUp'))
      File "/home/hkohr/.virtualenvs/numpy-py2.7/lib/python2.7/site-packages/nose/util.py", line 471, in try_run
        return func()
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 361, in setUp
        module_name=self.module_name)
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 78, in wrapper
        memo[key] = func(*a, **kw)
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 169, in build_code
        module_name=module_name)
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 78, in wrapper
        memo[key] = func(*a, **kw)
      File "/home/hkohr/Software/numpy/numpy/f2py/tests/util.py", line 149, in build_module
        __import__(module_name)
    ImportError: /tmp/tmpqyEUs_/_test_ext_module_5403.so: failed to map segment from shared object
    
    ----------------------------------------------------------------------
    
  4. jaimefrio commented on Dec 30, 2015

    @jaimefrio
    Member

    +1 on exploring this change: looking at the code it certainly seems like an unnecessary restriction.

    Your test errors do not seem related, do you get the same errors if testing master unchanged? Would be good to put a PR together and see what Travis thinks.

  5. kohr-h commented on Dec 30, 2015

    @kohr-h
    ContributorAuthor

    I opened a PR (#6905), Travis seems to be happy with the change.

    The last two errors in my previous post come from /tmp being mounted with noexec, see #4858. This may be worth a mention in the dev docs.

    The first error only appears in a Py2 environment. Not sure what happens there, the pure command python -c "from distutils import ccompiler" succeeds. Probably something for a new issue.

  6. seberg commented on Dec 30, 2015

    @seberg
    Member

    I actually prefer the spelled out version (I thought the general style guidlines say that small numbers are spelled out usually), though not sure if that rule is still true when mixing small and large ;). Anyway, does not matter much.

  7. kohr-h commented on Dec 30, 2015

    @kohr-h
    ContributorAuthor

    The exact wording of the error message is subject to another PR #6900. Is there anything written about general guidelines regarding whether or not to spell out numbers?

  8. seberg commented on Dec 30, 2015

    @seberg
    Member

    Oh right, was the other place, hehe. Anyway, no, don't worry about it, since @jaimefrio seemed to prefer the number, I just thought I would be a smartass and mention that style guidelines (general english writing ones) as far as I know all say that at least numbers smaller than 10 should be spelled out ;).

  9. kohr-h commented on Dec 30, 2015

    @kohr-h
    ContributorAuthor

    Clear rules would be nice, I guess , but that's too much asked for in general language :-) Perhaps being consistent with the rest (if possible) is the way to go here.

  10. njsmith commented on Dec 31, 2015

    @njsmith
    Member

    Style guidelines intended for general written prose don't necessarily apply
    to short technical error messages :-). Plusone to leaving the numbers as
    digits.
    On Dec 30, 2015 2:46 PM, "Holger Kohr" [email protected] wrote:

    Clear rules would be nice, I guess , but that's too much asked for in
    general language :-) Perhaps being consistent with the rest (if possible)
    is the way to go here.

    —
    Reply to this email directly or view it on GitHub
    #6899 (comment).

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions