Skip to content

rollaxis is weird #9473

Closed
Closed

Description

@nschloe

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).

Activity

  1. eric-wieser commented on Jul 27, 2017

    @eric-wieser
    Member

    Are you looking for np.moveaxis? I've also always found rollaxis frustrating.

  2. nschloe commented on Jul 27, 2017

    @nschloe
    ContributorAuthor

    Ah, yes. Now I'm asking myself why there is such a thing as rollaxis at all.

  3. changed the title [-]suggestion: fix inconsistent start parameter in rollaxis[/-] [+]rollaxis is weird[/+] on Jul 27, 2017
  4. eric-wieser commented on Jul 27, 2017

    @eric-wieser
    Member

    It might be nice to replace all internal uses of rollaxis with moveaxis, and eventually deprecate it

  5. nschloe commented on Jul 27, 2017

    @nschloe
    ContributorAuthor

    The 1.11 changelog says about the then-new moveaxis:

    This function should be easier to use than the current rollaxis function as well as providing more functionality.

    grepping shows it shouldn't be too hard to get rid of rollaxis in numpy's own code, let me try and prepare something.

  6. eric-wieser commented on Dec 12, 2017

    @eric-wieser
    Member

    I think this is as fixed as it can become as of #9475, so closing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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