You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does numpy.broadcast not accept a single input array? #6899
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).shapeTraceback (mostrecentcalllast):
...
ValueError: Needatleasttwoandfewerthan (32) arrayobjects.
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.
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.
Seems to work right away with the proposed (trivial) change:
>>>importnumpyasnp>>>np.__path__# My local GH copy
['/home/hkohr/Software/numpy/numpy']
>>>a=np.arange(3)
>>>bc=np.broadcast(a)
>>>bc.nd1>>>bc.numiter1>>>bc.shape
(3,)
>>>bc.size3
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
----------------------------------------------------------------------
+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.
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.
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.
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?
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 ;).
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.
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).
Activity
kohr-h commentedon Dec 30, 2015
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.
shoyer commentedon Dec 30, 2015
Well, maybe worth some playing around, then?
I also find this surprising.
kohr-h commentedon Dec 30, 2015
Seems to work right away with the proposed (trivial) change:
I get three errors when running
np.test(), but they're probably unrelated. I just post them for the record:jaimefrio commentedon Dec 30, 2015
+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.
kohr-h commentedon Dec 30, 2015
I opened a PR (#6905), Travis seems to be happy with the change.
The last two errors in my previous post come from
/tmpbeing mounted withnoexec, 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.seberg commentedon Dec 30, 2015
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.
kohr-h commentedon Dec 30, 2015
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?
seberg commentedon Dec 30, 2015
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 ;).
kohr-h commentedon Dec 30, 2015
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.
njsmith commentedon Dec 31, 2015
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: