Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

rollaxis is weird #9473

Copy link
Copy link
Closed
Closed
Copy link
@nschloe

Description

@nschloe
Issue body actions

rollaxis has an optional start parameter that, if specified, moves the axis to the position before start. Or, as the documentation puts it

The axis is rolled until it lies before this position. The default, 0, results in a “complete” roll.

(Just by the way: The last sentence is unclear. What's a "complete roll"? Better be explicit: The default, 0, moves the axis to the beginning.)

E.g.,

a = numpy.ones((1, 2, 3, 4))
b = numpy.rollaxis(a, 0, 3)
print(b.shape)

moves the axis at position 0 to the place before position 3, and hence the above will print (2, 3, 1, 4).

This before logic results in the fact that there are always two start indices which will do exactly the same thing, namely numpy.rollaxis(a, i, i) and numpy.rollaxis(a, i, i+1) – nothing. As a trade-off for this duplication, what you can't seem to do is to move the axis in the last place, since there is no axis that comes after.

I used the word seem, because you can actually do numpy.rollaxis(a, i, len(a.shape)) to insert it axis i before the imaginary axis at position len(a.shape). This is not explicitly documented.

On top of that, why is the parameter called start? I have no idea.


Edit The following suggestion is actually just what moveaxis does.

A more consistent approach would be to have a parameter target (or target_position) that can be used to specify the position of axis i after the roll. E.g.,

a = numpy.ones((1, 2, 3, 4))
numpy.rollaxis(a, 0, target=0).shape  # (1, 2, 3, 4)
numpy.rollaxis(a, 0, target=1).shape  # (2, 1, 3, 4)
numpy.rollaxis(a, 0, target=len(a.shape))  # error
numpy.rollaxis(a, 0, target=-1).shape  # (2, 3, 4, 1)

The last example shows how to move the first axis to the last position without recalling len(a.shape).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.