-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Changes from 1 commit
a5c6f0d
2dcc9aa
e787a9f
95adb77
07a3f43
19fc68c
997ac2c
7eb1044
ff7f726
6ecd2b4
1211b70
ffa6cf6
3ed6936
5f9f1fa
8a83a5f
5a0557a
c2b5be5
bd6729d
ad278f3
2c1734b
eaddf39
a5cbc93
a691f2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,9 +378,12 @@ def _block_check_depths_match(arrays, parent_index=[]): | |
The parameter `parent_index` is the full index of `arrays` within the | ||
nested lists passed to _block_check_depths_match at the top of the | ||
recursion. | ||
The return value is the full index of an element (specifically the | ||
first element) from the bottom of the nesting in `arrays`. An empty | ||
list at the bottom of the nesting is represented by a `None` index. | ||
The return value is a pair. The first item returned is the full index | ||
of an element (specifically the first element) from the bottom of the | ||
nesting in `arrays`. An empty list at the bottom of the nesting is | ||
represented by a `None` index. | ||
The second item is the maximum of the ndims of the arrays nested in | ||
`arrays`. | ||
""" | ||
def format_index(index): | ||
idx_str = ''.join('[{}]'.format(i) for i in index if i is not None) | ||
|
@@ -423,6 +426,13 @@ def format_index(index): | |
|
||
|
||
def _block(arrays, max_depth, result_ndim): | ||
""" | ||
Internal implementation of block. `arrays` is the argument passed to | ||
block. `max_depth` is the depth of nested lists within `arrays` and | ||
`result_ndim` is the greatest of the dimensions of the arrays in | ||
`arrays` and the depth of the lists in `arrays` (see block docstring | ||
for details). | ||
""" | ||
def atleast_nd(a, ndim): | ||
# Ensures `a` has at least `ndim` dimensions by prepending | ||
# ones to `a.shape` as necessary | ||
|
@@ -436,7 +446,7 @@ def block_recursion(arrays, depth=0): | |
return _nx.concatenate(arrs, axis=-(max_depth-depth)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also slight personal preference to having spaces around binary operators for readability. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would, yeah. The reason I wrote it that way round was because I wanted to match the docstring as closely as possible (my original motivation for this pr was to make it a bit clearer to people reading the code how block actually worked). The docstring says:
To me the correspondence between the docstring and the code is ever-so-slightly clearer with the expression the way round that it currently is, and I think the effect on performance is probably negligable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I often instinctively shorten things when they're being fed to a keyword argument, because you don't normally put spaces around the axis=-(max_depth - depth) and axis=depth - max_depth look a bit weird? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The style point is minor. Though there are many tools (e.g. |
||
else: | ||
# We've 'bottomed out' - arrays is either a scalar or an array | ||
# depth == max_depth | ||
# type(arrays) is not list | ||
return atleast_nd(arrays, result_ndim) | ||
|
||
return block_recursion(arrays) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now reflects the wording of the docstring quite closely: