-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Added atleast_nd #7804
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
Closed
Closed
ENH: Added atleast_nd #7804
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8000
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
from __future__ import division, absolute_import, print_function | ||
|
||
__all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack', | ||
'stack', 'vstack'] | ||
__all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'atleast_nd', 'block', | ||
'hstack', 'stack', 'vstack'] | ||
|
||
|
||
from . import numeric as _nx | ||
from .numeric import array, asanyarray, newaxis | ||
from .multiarray import normalize_axis_index | ||
from ._internal import recursive | ||
|
||
|
||
def atleast_1d(*arys): | ||
""" | ||
Convert inputs to arrays with at least one dimension. | ||
|
@@ -29,7 +30,7 @@ def atleast_1d(*arys): | |
|
||
See Also | ||
-------- | ||
atleast_2d, atleast_3d | ||
atleast_2d, atleast_3d, atleast_nd | ||
|
||
Examples | ||
-------- | ||
|
@@ -48,18 +49,10 @@ def atleast_1d(*arys): | |
< 8000 span class='blob-code-inner blob-code-marker ' data-code-marker=" "> [array([1]), array([3, 4])] | ||
|
||
""" | ||
res = [] | ||
for ary in arys: | ||
ary = asanyarray(ary) | ||
if ary.ndim == 0: | ||
result = ary.reshape(1) | ||
else: | ||
result = ary | ||
res.append(result) | ||
if len(res) == 1: | ||
return res[0] | ||
else: | ||
return res | ||
if len(arys) == 1: | ||
return atleast_nd(arys[0], 1) | ||
return [atleast_nd(x, 1) for x in arys] | ||
|
||
|
||
def atleast_2d(*arys): | ||
""" | ||
|
@@ -68,9 +61,9 @@ def atleast_2d(*arys): | |
Parameters | ||
---------- | ||
arys1, arys2, ... : array_like | ||
One or more array-like sequences. Non-array inputs are converted | ||
to arrays. Arrays that already have two or more dimensions are | ||
preserved. | ||
One or more array-like sequences. Non-array inputs are | ||
converted to arrays. Arrays that already have two or more | ||
dimensions are preserved. | ||
|
||
Returns | ||
------- | ||
|
@@ -81,7 +74,7 @@ def atleast_2d(*arys): | |
|
||
See Also | ||
-------- | ||
atleast_1d, atleast_3d | ||
atleast_1d, atleast_3d, atleast_nd | ||
|
||
Examples | ||
-------- | ||
|
@@ -98,20 +91,10 @@ def atleast_2d(*arys): | |
[array([[1]]), array([[1, 2]]), array([[1, 2]])] | ||
|
||
""" | ||
res = [] | ||
for ary in arys: | ||
ary = asanyarray(ary) | ||
if ary.ndim == 0: | ||
result = ary.reshape(1, 1) | ||
elif ary.ndim == 1: | ||
result = ary[newaxis,:] | ||
else: | ||
result = ary | ||
res.append(result) | ||
if len(res) == 1: | ||
return res[0] | ||
else: | ||
return res | ||
if len(arys) == 1: | ||
return atleast_nd(arys[0], 2) | ||
return [atleast_nd(x, 2) for x in arys] | ||
|
||
|
||
def atleast_3d(*arys): | ||
""" | ||
|
@@ -120,22 +103,31 @@ def atleast_3d(*arys): | |
Parameters | ||
---------- | ||
arys1, arys2, ... : array_like | ||
One or more array-like sequences. Non-array inputs are converted to | ||
arrays. Arrays that already have three or more dimensions are | ||
preserved. | ||
One or more array-like sequences. Non-array inputs are | ||
converted to arrays. Arrays that already have three or more | ||
dimensions are preserved. | ||
|
||
Returns | ||
------- | ||
res1, res2, ... : ndarray | ||
An array, or list of arrays, each with ``a.ndim >= 3``. Copies are | ||
avoided where possible, and views with three or more dimensions are | ||
returned. For example, a 1-D array of shape ``(N,)`` becomes a view | ||
of shape ``(1, N, 1)``, and a 2-D array of shape ``(M, N)`` becomes a | ||
view of shape ``(M, N, 1)``. | ||
An array, or list of arrays, each with ``a.ndim >= 3``. Copies | ||
are avoided where possible, and views with three or more | ||
dimensions are returned. For example, a 1-D array of shape | ||
``(N,)`` becomes a view of shape ``(1, N, 1)``, and a 2-D array | ||
of shape ``(M, N)`` becomes a view of shape ``(M, N, 1)``. | ||
|
||
See Also | ||
-------- | ||
atleast_1d, atleast_2d | ||
atleast_1d, atleast_2d, atleast_nd | ||
|
||
Notes | ||
----- | ||
As mentioned in the `Returns` section, the results of this fuction | ||
are not compatible with any of the other `atleast*` functions. | ||
`atleast_2d` prepends the unit dimension to a 1D array while | ||
`atleast_3d` appends it to a 2D array. The 1D array case both | ||
appends and prepends a dimension, while `atleast_nd` can only add | ||
dimensions to one end at a time. | ||
|
||
Examples | ||
-------- | ||
|
@@ -168,9 +160,9 @@ def atleast_3d(*arys): | |
if ary.ndim == 0: | ||
result = ary.reshape(1, 1, 1) | ||
elif ary.ndim == 1: | ||
result = ary[newaxis,:, newaxis] | ||
result = ary[newaxis, :, newaxis] | ||
elif ary.ndim == 2: | ||
result = ary[:,:, newaxis] | ||
result = ary[:, :, newaxis] | ||
else: | ||
result = ary | ||
res.append(result) | ||
|
@@ -180,6 +172,100 @@ def atleast_3d(*arys): | |
return res | ||
|
||
|
||
def atleast_nd(ary, ndim, pos=0): | ||
""" | ||
View input as array with at least `ndim` dimensions. | ||
|
||
New unit dimensions are inserted at the index given by `pos` if | ||
necessary. | ||
|
||
Parameters | ||
---------- | ||
ary : array_like | ||
The input array. Non-array inputs are converted to arrays. | ||
Arrays that already have `ndim` or more dimensions are | ||
preserved. | ||
ndim : scalar | ||
The minimum number of dimensions required. | ||
pos : int, optional | ||
The index to insert the new dimensions. May range from | ||
``-ary.ndim - 1`` to ``+ary.ndim`` (inclusive). Non-negative | ||
indices indicate locations before the corresponding axis: | ||
``pos=0`` means to insert at the very beginning. Negative | ||
indices indicate locations after the corresponding axis: | ||
``pos=-1`` means to insert at the very end. 0 and -1 are always | ||
guaranteed to work. Any other number will depend on the | ||
dimensions of the existing array. Default is 0. | ||
|
||
Returns | ||
------- | ||
res : ndarray | ||
An array with ``res.ndim >= ndim``. A view is returned for array | ||
inputs. Dimensions are prepended if `pos` is 0, so for example, | ||
a 1-D array of shape ``(N,)`` with ``ndim=4`` becomes a view of | ||
shape ``(1, 1, 1, N)``. Dimensions are appended if `pos` is -1, | ||
so for example a 2-D array of shape ``(M, N)`` becomes a view of | ||
shape ``(M, N, 1, 1)`` when ``ndim=4``. | ||
|
||
See Also | ||
-------- | ||
atleast_1d, atleast_2d, atleast_3d | ||
|
||
Notes | ||
----- | ||
This function does not follow the convention of the other atleast_*d | ||
functions in numpy in that it only accepts a single array argument. | ||
To process multiple arrays, use a comprehension or loop around the | ||
function call. See examples below. | ||
|
||
Setting ``pos=0`` is equivalent to how the array would be | ||
interpreted by numpy's broadcasting rules. There is no need to call | ||
this function for simple broadcasting. This is also roughly | ||
(but not exactly) equivalent to | ||
``np.array(ary, copy=False, subok=True, ndmin=ndim)``. | ||
|
||
It is easy to create functions for specific dimensions similar to | ||
the other atleast_*d functions using Python's `functools.partial` | ||
function. An example is shown below. | ||
|
||
Examples | ||
-------- | ||
>>> np.atleast_nd(3.0, 4) | ||
array([[[[ 3.]]]]) | ||
|
||
>>> x = np.arange(3.0) | ||
>>> np.atleast_nd(x, 2).shape | ||
(1, 3) | ||
|
||
>>> x = np.arange(12.0).reshape(4, 3) | ||
>>> np.atleast_nd(x, 5).shape | ||
(1, 1, 1, 4, 3) | ||
>>> np.atleast_nd(x, 5).base is x.base | ||
True | ||
|
||
>>> [np.atleast_nd(x) for x in ((1, 2), [[1, 2]], [[[1, 2]]])]: | ||
[array([[1, 2]]), array([[1, 2]]), array([[[1, 2]]])] | ||
|
||
>>> np.atleast_nd((1, 2), 5, pos=0).shape | ||
(1, 1, 1, 1, 2) | ||
>>> np.atleast_nd((1, 2), 5, pos=-1).shape | ||
(2, 1, 1, 1, 1) | ||
|
||
>>> from functools import partial | ||
>>> atleast_4d = partial(np.atleast_nd, ndim=4) | ||
>>> atleast_4d([1, 2, 3]) | ||
[[[[1, 2, 3]]]] | ||
""" | ||
ary = array(ary, copy=False, subok=True) | ||
if ary.ndim: | ||
pos = normalize_axis_index(pos, ary.ndim + 1) | ||
extra = ndim - ary.ndim | ||
if extra > 0: | ||
ind = pos * (slice(None),) + extra * (None,) + (Ellipsis,) | ||
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. This code reminds me it would be nice to have an |
||
ary = ary[ind] | ||
return ary | ||
|
||
|
||
def vstack(tup): | ||
""" | ||
Stack arrays in sequence vertically (row wise). | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why this if?
Why not move the
pos =
line into theif extra >0
block?