8000 MAINT: Simplify block implementation by j-towns · Pull Request #9667 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Simplify block implementation #9667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 12, 2017
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Re-add atleast_nd function.
  • Loading branch information
j-towns committed Sep 18, 2017
commit 997ac2cfbe3edba968e1ee140763b53b88c3afca
9 changes: 7 additions & 2 deletions numpy/core/shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,23 @@ def format_index(index):


def _block(arrays, depth=0):
def atleast_nd(a, ndim):
# Ensures `a` has at least `ndim` dimensions by prepending
# ones to `a.shape` as necessary
return array(a, ndmin=ndim, copy=False, subok=True)

if type(arrays) is list:
if len(arrays) == 0:
raise ValueError('Lists cannot be empty')
arrs, list_ndims = zip(*(_block(arr, depth+1) for arr in arrays))
list_ndim = list_ndims[0]
arr_ndim = max(arr.ndim for arr in arrs)
ndim = max(list_ndim, arr_ndim)
arrs = [array(a, ndmin=ndim, copy=False, subok=True) for a in arrs]
arrs = [atleast_nd(a, ndim) for a in arrs]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me a little uneasy that this happens inside the recursion - I think there aren't many tests for block with different dimensioned arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the logic a little and added two tests for cases where arr_ndim > list_ndim and the inner arrays have different ndims.

return _nx.concatenate(arrs, axis=depth+ndim-list_ndim), list_ndim
else:
# We've 'bottomed out'
return array(arrays, ndmin=depth, copy=False, subok=True), depth
return atleast_nd(arrays, depth), depth


def block(arrays):
Expand Down
0