Skip to content

Add reorder_axes function

Thomas Kluyver requested to merge feat/reorder-axes into master

Description

For !852 (merged), I needed to understand the axis order from calls to np.transpose, with arguments like (2, 0, 1, 3). It might just be me, but understanding what this meant in terms of which axis is which required:

  • Reading other code to understand what the order was meant to be after transposing
  • Looking up docs for np.transpose() to see how it worked
    • In particular, does (2, ...) mean input axis 0 moves to position 2, or input axis 2 moves to position 0? (It's the latter)
  • Some mental effort to combine this information and figure out what the input order must be.

reorder_axes provides a far more verbose way to do this, but which makes the input and output order explicit, so it's easier to understand and check what's going on.

saved_axes  = ('slow_scan', 'fast_scan', 'memory_cells', 'gain')
target_axes = ('memory_cells', 'slow_scan', 'fast_scan', 'gain')
const = reorder_axes(const, saved_axes, target_axes)

This will also simplify using a saved axis order from the file, when we get to that.

This is a tiny subset of what Xarray can do, but I think it's worth having this without bringing in all the complexity of wrapping arrays in Xarray objects. We may end up copying this function to use in other places, but that's OK.

How Has This Been Tested?

Test added to the codebase. 🙂

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the code style of this project.
  • I added tests where appropriate.

Reviewers

@ahmedk @schmidtp

Merge request reports