Skip to content

723 Fix illumination correction task not copying tables #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
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
48 changes: 48 additions & 0 deletions fractal_tasks_core/tasks/_zarr_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import copy
import logging

import anndata as ad
import zarr
from filelock import FileLock

from fractal_tasks_core.ngff.zarr_utils import load_NgffWellMeta
from fractal_tasks_core.tables import write_table
from fractal_tasks_core.tables.v1 import get_tables_list_v1

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,3 +168,48 @@ def _get_matching_ref_acquisition_path_heuristic(
f"option {sorted_path_list[0]}."
)
return sorted_path_list[0]


def _copy_tables_from_zarr_url(
origin_zarr_url: str,
target_zarr_url: str,
table_type: str = None,
overwrite: bool = True,
) -> None:
"""
Copies all ROI tables from one Zarr into a new Zarr

Args:
origin_zarr_url: url of the OME-Zarr image that contains tables.
e.g. /path/to/my_plate.zarr/B/03/0
target_zarr_url: url of the new OME-Zarr image where tables are copied
to. e.g. /path/to/my_plate.zarr/B/03/0_illum_corr
table_type: Filter for specific table types that should be copied.
overwrite: Whether existing tables of the same name in the
target_zarr_url should be overwritten.
"""
table_list = get_tables_list_v1(
zarr_url=origin_zarr_url, table_type=table_type
)

if table_list:
logger.info(
f"Copying the tables {table_list} from {origin_zarr_url} to "
f"{target_zarr_url}."
)
new_image_group = zarr.group(target_zarr_url)

for table in table_list:
logger.info(f"Copying table: {table}")
# Get the relevant metadata of the Zarr table & add it
table_url = f"{origin_zarr_url}/tables/{table}"
old_table_group = zarr.open_group(table_url, mode="r")
# Write the Zarr table
curr_table = ad.read_zarr(table_url)
write_table(
new_image_group,
table,
curr_table,
table_attrs=old_table_group.attrs.asdict(),
overwrite=overwrite,
)
4 changes: 4 additions & 0 deletions fractal_tasks_core/tasks/illumination_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
convert_ROI_table_to_indices,
)
from fractal_tasks_core.tasks._zarr_utils import _copy_hcs_ome_zarr_metadata
from fractal_tasks_core.tasks._zarr_utils import _copy_tables_from_zarr_url

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -228,6 +229,9 @@ def illumination_correction(
dimension_separator="/",
)
_copy_hcs_ome_zarr_metadata(zarr_url, zarr_url_new)
# Copy ROI tables from the old zarr_url to keep ROI tables and other
# tables available in the new Zarr
_copy_tables_from_zarr_url(zarr_url, zarr_url_new)

# Iterate over FOV ROIs
num_ROIs = len(list_indices)
Expand Down
11 changes: 11 additions & 0 deletions tests/tasks/test_unit_illumination_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dask.array as da
import numpy as np
import pytest
from devtools import debug
from pytest import LogCaptureFixture
from pytest import MonkeyPatch

Expand All @@ -15,6 +16,7 @@
from fractal_tasks_core.roi import (
convert_ROI_table_to_indices,
)
from fractal_tasks_core.tables.v1 import get_tables_list_v1
from fractal_tasks_core.tasks._registration_utils import (
_split_well_path_image_path,
)
Expand Down Expand Up @@ -68,6 +70,9 @@ def test_illumination_correction(
)
num_FOVs = len(list_indices)

# Get existing tables before illumination correction
tables = get_tables_list_v1(zarr_url)

# Prepared expected number of calls
expected_tot_calls_correct = num_channels * num_FOVs

Expand Down Expand Up @@ -136,3 +141,9 @@ def patched_correct(*args, **kwargs):
assert well_paths == ["0"]
else:
assert well_paths == ["0", "0" + suffix]

# Assert that the image has the same tables after illumination correction
# as before
debug(tables)
new_tables = get_tables_list_v1(new_zarr_url)
assert tables == new_tables
Loading