Skip to content
Snippets Groups Projects
Commit 5086ffd7 authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Merge branch 'feat/reorder-axes' into 'master'

Add reorder_axes function

See merge request !853
parents 4c950e6c 74d9cb86
No related branches found
No related tags found
1 merge request!853Add reorder_axes function
......@@ -1030,3 +1030,17 @@ def write_compressed_frames(
dataset.id.write_direct_chunk(chunk_start, compressed)
return dataset
def reorder_axes(a, from_order, to_order):
"""Rearrange axes of array a from from_order to to_order
This does the same as np.transpose(), but making the before & after axes
more explicit. from_order is a sequence of strings labelling the axes of a,
and to_order is a similar sequence for the axes of the result.
"""
assert len(from_order) == a.ndim
assert sorted(from_order) == sorted(to_order)
from_order = list(from_order)
order = tuple([from_order.index(lbl) for lbl in to_order])
return a.transpose(order)
......@@ -22,6 +22,7 @@ from cal_tools.tools import (
recursive_update,
send_to_db,
write_constants_fragment,
reorder_axes,
)
# AGIPD operating conditions.
......@@ -614,3 +615,13 @@ def test_write_constants_fragment(tmp_path: Path):
},
}
}
def test_reorder_axes():
a = np.zeros((10, 32, 256, 3))
from_order = ('cells', 'slow_scan', 'fast_scan', 'gain')
to_order = ('slow_scan', 'fast_scan', 'cells', 'gain')
assert reorder_axes(a, from_order, to_order).shape == (32, 256, 10, 3)
to_order = ('gain', 'fast_scan', 'slow_scan', 'cells')
assert reorder_axes(a, from_order, to_order).shape == (3, 256, 32, 10)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment