Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/fast4dreg_lc_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
28 changes: 28 additions & 0 deletions tests/test_lc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])))
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Loading