From 8376a533ee279747ad67a957b5da5543050e185c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 08:34:10 +0000 Subject: [PATCH] Tools return the corrected image with its axes, for Fiji hyperstacks With labconstrictor-tools 0.3.0a4 or later (WithAxes), the corrected movie and the aligned image carry the axes the person gave, so Fiji opens a time-lapse with its time points and slices instead of one long stack. With an older Tools the array is returned as before. The tests read the array from either form, and check the axes when WithAxes exists. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Scma6VXx7mT1FYsKmB1184 --- src/fast4dreg_lc_tools/__init__.py | 15 +++++++++++++-- tests/test_lc_tools.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/fast4dreg_lc_tools/__init__.py b/src/fast4dreg_lc_tools/__init__.py index 2027e14..073d518 100644 --- a/src/fast4dreg_lc_tools/__init__.py +++ b/src/fast4dreg_lc_tools/__init__.py @@ -73,6 +73,17 @@ def _axes(image, axes: str, needs: str) -> str: return normalized +def _with_axes(array, axes: str): + """The corrected image with its axes (the same as the input's), so Fiji opens a + time-lapse or a stack with its dimensions. Tools before 0.3.0a4 has no WithAxes: + the array is then returned as it is, and Fiji shows one long stack.""" + try: + from labconstrictor_tools import WithAxes + except ImportError: + return array + return WithAxes(array, axes) + + def _shift_table(shifts, index_name: str) -> dict: """The (dz, dy, dx) translation applied to each timepoint or channel, in px. @@ -196,7 +207,7 @@ def correct_drift( ) except ValueError as error: # a setting the image does not allow, or a failed fit raise ToolError("invalid_settings", str(error)) from error - return corrected, _shift_table(shifts, "timepoint") + return _with_axes(corrected, normalized), _shift_table(shifts, "timepoint") @tool("Align channels") @@ -280,4 +291,4 @@ def align_channels( ) except ValueError as error: raise ToolError("invalid_settings", str(error)) from error - return aligned, _shift_table(shifts, "channel") + return _with_axes(aligned, normalized), _shift_table(shifts, "channel") diff --git a/tests/test_lc_tools.py b/tests/test_lc_tools.py index d3462bc..208803e 100644 --- a/tests/test_lc_tools.py +++ b/tests/test_lc_tools.py @@ -38,6 +38,11 @@ def fixtures(tmp_path_factory): return folder +def image_of(result): + """The array of an image result (returned with its axes when Tools has WithAxes).""" + return getattr(result, "array", result) + + def table_shifts(table, index_name): assert list(table) == [index_name, "dz", "dy", "dx"] assert table[index_name] == list(range(len(table["dz"]))) @@ -51,6 +56,7 @@ def table_shifts(table, index_name): def test_correct_drift_finds_the_known_drift(fixtures, name, axes, correct_z): image = tifffile.imread(fixtures / name) corrected, table = tools.correct_drift(image, axes=axes, correct_z=correct_z) + corrected = image_of(corrected) assert corrected.shape == image.shape assert corrected.dtype == np.float32 shifts = table_shifts(table, "timepoint") @@ -64,6 +70,7 @@ def test_correct_drift_finds_the_known_drift(fixtures, name, axes, correct_z): def test_align_channels_finds_the_known_shifts(fixtures, name, axes, correct_z): image = tifffile.imread(fixtures / name) aligned, table = tools.align_channels(image, axes=axes, correct_z=correct_z) + aligned = image_of(aligned) assert aligned.shape == image.shape shifts = table_shifts(table, "channel") np.testing.assert_allclose(shifts, make_fixtures.KNOWN[name], atol=TOLERANCE_PX) @@ -108,3 +115,24 @@ def test_a_setting_fast4dregpy_refuses_is_a_tool_error(fixtures): with pytest.raises(ToolError) as caught: tools.align_channels(image, axes="CYX", reference_channel=5) assert caught.value.code == "invalid_settings" + + +@pytest.mark.parametrize( + ("tool", "name", "axes"), + [ + (tools.correct_drift, "drift_tzyx.tif", "TZYX"), + (tools.align_channels, "channels_zcyx.tif", "ZCYX"), + ], +) +def test_the_image_result_carries_the_input_axes(fixtures, tool, name, axes): + """With Tools 0.3.0a4 or later, Fiji opens the result as a hyperstack: the tool + returns it with the axes the person gave (normalised), in the same order.""" + import labconstrictor_tools + + if not hasattr(labconstrictor_tools, "WithAxes"): + pytest.skip("labconstrictor-tools before 0.3.0a4 has no WithAxes") + image = tifffile.imread(fixtures / name) + result, _ = tool(image, axes=axes.lower()) + assert isinstance(result, labconstrictor_tools.WithAxes) + assert result.axes == axes + assert result.array.shape == image.shape