Skip to content

Commit

Permalink
Enforce sorting of FOVs for ROI table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jluethi committed Jun 2, 2024
1 parent cf9f49b commit 6ae2aed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/fractal_faim_ipa/roi_tables.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import anndata as ad
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -76,7 +78,12 @@ def create_fov_ROI_table(
min_z = tile.position.z * pixel_size_zyx[0]
max_z = (tile.position.z + 1) * pixel_size_zyx[0]
fov_counter = 1
for tile in tiles:
# Initial robust sort
sorted_tiles = sorted(tiles)
# Sort again by trying to find site information as usually contained in
# MD filenames
sorted_tiles = sorted(sorted_tiles, key=_extract_fov_sort_key)
for tile in sorted_tiles:
z_start = tile.position.z * pixel_size_zyx[0]
z_end = (tile.position.z + 1) * pixel_size_zyx[0]
if z_start < min_z:
Expand Down Expand Up @@ -105,3 +112,13 @@ def create_fov_ROI_table(
# Cast the values to float to avoid anndata type issues
roi_table = roi_table.astype(np.float32)
return ad.AnnData(roi_table)


def _extract_fov_sort_key(tile):
"""Extract the FOV site information from an MD filename."""
filename = tile.path.split("/")[-1]
match = re.search(r"_s(\d+)_", filename)
if match:
return int(match.group(1)), filename
# Use a large number to push files without 's' to the end
return float("inf"), filename
24 changes: 23 additions & 1 deletion tests/test_roi_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from faim_ipa.hcs.acquisition import TileAlignmentOptions
from fractal_faim_ipa.md_converter_utils import ModeEnum
from fractal_faim_ipa.roi_tables import create_ROI_tables
from fractal_faim_ipa.roi_tables import _extract_fov_sort_key, create_ROI_tables


@pytest.mark.parametrize(
Expand Down Expand Up @@ -128,3 +128,25 @@ def test_roi_table_overlaps(alignment):
target_values_well[alignment],
)
)


def test_roi_sorting():
ROOT_DIR = Path(__file__).parent
image_dir = str(join(ROOT_DIR.parent, "resources", "Projection-Mix"))

mode = ModeEnum("MD Stack Acquisition")
plate_acquisition = mode.get_plate_acquisition(
acquisition_dir=image_dir,
alignment=TileAlignmentOptions.GRID,
)
well_acquisition = plate_acquisition.get_well_acquisitions(selection=["E07"])[0]
tiles = well_acquisition.get_tiles()
sorted_tiles = sorted(tiles, key=_extract_fov_sort_key)
assert (
sorted_tiles[0].path.split("/")[-1]
== "Projection-Mix_E07_s1_w1091EB8A5-272A-466D-B8A0-7547C6BA392B.tif"
)
assert (
sorted_tiles[-1].path.split("/")[-1]
== "Projection-Mix_E07_s2_w4F95A8A9F-0939-47C2-8D3E-F6E91AF0C4ED.tif"
)

0 comments on commit 6ae2aed

Please sign in to comment.