Skip to content
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

Improve coverage of core modules #459

Merged
merged 14 commits into from
Jul 18, 2023
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
32 changes: 14 additions & 18 deletions examples/09_secondary_labeling/01_images_to_mip.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import json
from typing import Any

from devtools import debug

from fractal_tasks_core.copy_ome_zarr import copy_ome_zarr
from fractal_tasks_core.create_ome_zarr import create_ome_zarr
from fractal_tasks_core.maximum_intensity_projection import (
from fractal_tasks_core.tasks.copy_ome_zarr import copy_ome_zarr
from fractal_tasks_core.tasks.create_ome_zarr import create_ome_zarr
from fractal_tasks_core.tasks.maximum_intensity_projection import (
maximum_intensity_projection,
)
from fractal_tasks_core.yokogawa_to_ome_zarr import yokogawa_to_ome_zarr
from fractal_tasks_core.tasks.yokogawa_to_ome_zarr import yokogawa_to_ome_zarr


allowed_channels = [
{
"wavelength_id": "A01_C01",
"colormap": "00FFFF",
"end": 2000,
"color": "00FFFF",
"label": "Channel 1",
"start": 110,
"window": {"start": 110, "end": 2000},
},
{
"wavelength_id": "A02_C02",
"colormap": "FF00FF",
"end": 500,
"color": "FF00FF",
"label": "Channel 2",
"start": 110,
"window": {"start": 110, "end": 500},
},
{
"wavelength_id": "A03_C03",
"colormap": "00FF00",
"end": 1600,
"color": "00FF00",
"label": "Channel 3",
"start": 110,
"window": {"start": 110, "end": 1600},
},
{
"wavelength_id": "A04_C04",
"colormap": "FFFF00",
"end": 1600,
"color": "FFFF00",
"label": "Channel 4",
"start": 110,
"window": {"start": 110, "end": 1600},
},
]

Expand All @@ -50,7 +47,7 @@
img_path = "images/"
zarr_path = "output/"
zarr_path_mip = "output_mip/"
metadata = {}
metadata: dict[str, Any] = {}

# Create zarr structure
metadata_update = create_ome_zarr(
Expand All @@ -62,7 +59,6 @@
allowed_channels=allowed_channels,
num_levels=num_levels,
coarsening_xy=coarsening_xy,
metadata_table="mrf_mlf",
)
metadata.update(metadata_update)
debug(metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from devtools import debug

from fractal_tasks_core.cellpose_segmentation import cellpose_segmentation
from fractal_tasks_core.lib_input_models import Channel
from fractal_tasks_core.tasks.cellpose_segmentation import (
cellpose_segmentation,
)


if os.path.exists("tmp"):
Expand All @@ -28,7 +31,7 @@
output_path=zarr_path_mip,
metadata=metadata,
component=component,
wavelength_id="A01_C01",
channel=Channel(wavelength_id="A01_C01"),
level=2,
relabeling=True,
diameter_level0=400.0,
Expand All @@ -50,7 +53,7 @@
output_path=zarr_path_mip,
metadata=metadata,
component=component,
wavelength_id="A01_C01",
channel=Channel(wavelength_id="A01_C01"),
level=2,
relabeling=True,
diameter_level0=20.0,
Expand Down
49 changes: 49 additions & 0 deletions examples/tools/lib_inspect_ROI_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from typing import Sequence

import anndata as ad

from fractal_tasks_core.lib_regions_of_interest import (
convert_ROI_table_to_indices,
)


def _inspect_ROI_table(
path: str,
full_res_pxl_sizes_zyx: Sequence[float],
level: int = 0,
coarsening_xy: int = 2,
) -> None:
"""
Description

:param dummy: this is just a placeholder
:type dummy: int
"""

print(f"{full_res_pxl_sizes_zyx=}")

adata = ad.read_zarr(path)
df = adata.to_df()
print("table")
print(df)
print()

try:
list_indices = convert_ROI_table_to_indices(
adata,
level=level,
coarsening_xy=coarsening_xy,
full_res_pxl_sizes_zyx=full_res_pxl_sizes_zyx,
# verbose=True,
)
print()
print(f"level: {level}")
print(f"coarsening_xy: {coarsening_xy}")
print("list_indices:")
for indices in list_indices:
print(indices)
print()
except KeyError as e:
print("Something went wrong in convert_ROI_table_to_indices\n", str(e))

return df
2 changes: 1 addition & 1 deletion fractal_tasks_core/lib_masked_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def _preprocess_input(
image_array: np.ndarray,
*,
region: tuple[slice],
region: tuple[slice, ...],
current_label_path: str,
ROI_table_path: str,
ROI_positional_index: int,
Expand Down Expand Up @@ -90,7 +90,7 @@

# Check that image data are 4D (CZYX) - FIXME issue 340
if not image_array.ndim == 4:
raise ValueError(

Check notice on line 93 in fractal_tasks_core/lib_masked_loading.py

View workflow job for this annotation

GitHub Actions / Coverage

This line has no coverage
"_preprocess_input requires a 4D "
f"image_array argument, but {image_array.shape=}"
)
Expand All @@ -101,13 +101,13 @@
logger.info(f"[_preprocess_input] {ROI_table_path=}")
logger.info(f"[_preprocess_input] {attrs.asdict()=}")
if not attrs["type"] == "ngff:region_table":
raise ValueError("Wrong attributes for {ROI_table_path}:\n{attrs}")

Check notice on line 104 in fractal_tasks_core/lib_masked_loading.py

View workflow job for this annotation

GitHub Actions / Coverage

This line has no coverage
label_relative_path = attrs["region"]["path"]
column_name = attrs["instance_key"]

# Check that ROI_table.obs has the right column and extract label_value
if column_name not in ROI_table.obs.columns:
raise ValueError(

Check notice on line 110 in fractal_tasks_core/lib_masked_loading.py

View workflow job for this annotation

GitHub Actions / Coverage

This line has no coverage
'In _preprocess_input, "{column_name}" '
f" missing in {ROI_table.obs.columns=}"
)
Expand Down
11 changes: 6 additions & 5 deletions fractal_tasks_core/lib_pyramid_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""
import pathlib
from typing import Callable
from typing import Optional
from typing import Sequence
from typing import Union

Expand All @@ -29,9 +30,9 @@ def build_pyramid(
overwrite: bool = False,
num_levels: int = 2,
coarsening_xy: int = 2,
chunksize: Sequence[int] = None,
aggregation_function: Callable = None,
):
chunksize: Optional[Sequence[int]] = None,
aggregation_function: Optional[Callable] = None,
) -> None:

"""
Starting from on-disk highest-resolution data, build and write to disk a
Expand Down Expand Up @@ -59,7 +60,7 @@ def build_pyramid(
# Check the number of axes and identify YX dimensions
ndims = len(data_highres.shape)
if ndims not in [2, 3, 4]:
raise Exception("{data_highres.shape=}, ndims not in [2,3,4]")
raise ValueError(f"{data_highres.shape=}, ndims not in [2,3,4]")
y_axis = ndims - 2
x_axis = ndims - 1

Expand All @@ -72,7 +73,7 @@ def build_pyramid(
for ind_level in range(1, num_levels):
# Verify that coarsening is doable
if min(previous_level.shape[-2:]) < coarsening_xy:
raise Exception(
raise ValueError(
f"ERROR: at {ind_level}-th level, "
f"coarsening_xy={coarsening_xy} "
f"but previous level has shape {previous_level.shape}"
Expand Down
42 changes: 0 additions & 42 deletions fractal_tasks_core/lib_regions_of_interest.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,48 +272,6 @@ def convert_ROI_table_to_indices(
return list_indices


def _inspect_ROI_table(
path: str,
full_res_pxl_sizes_zyx: Sequence[float],
level: int = 0,
coarsening_xy: int = 2,
) -> None:
"""
Description

:param dummy: this is just a placeholder
:type dummy: int
"""

print(f"{full_res_pxl_sizes_zyx=}")

adata = ad.read_zarr(path)
df = adata.to_df()
print("table")
print(df)
print()

try:
list_indices = convert_ROI_table_to_indices(
adata,
level=level,
coarsening_xy=coarsening_xy,
full_res_pxl_sizes_zyx=full_res_pxl_sizes_zyx,
# verbose=True,
)
print()
print(f"level: {level}")
print(f"coarsening_xy: {coarsening_xy}")
print("list_indices:")
for indices in list_indices:
print(indices)
print()
except KeyError as e:
print("Something went wrong in convert_ROI_table_to_indices\n", str(e))

return df


def array_to_bounding_box_table(
mask_array: np.ndarray, pxl_sizes_zyx: list[float]
) -> pd.DataFrame:
Expand Down
18 changes: 10 additions & 8 deletions fractal_tasks_core/lib_upscale_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
"""
import logging
import warnings
from typing import Optional
from typing import Sequence
from typing import Tuple

import numpy as np


def upscale_array(
*,
array: np.ndarray,
target_shape: Tuple[int],
axis: Sequence[int] = None,
target_shape: tuple[int, ...],
axis: Optional[Sequence[int]] = None,
pad_with_zeros: bool = False,
warn_if_inhomogeneous: bool = False,
) -> np.ndarray:
Expand Down Expand Up @@ -57,6 +57,8 @@ def upscale_array(

if len(array_shape) != len(target_shape):
raise ValueError(f"{info} Dimensions-number mismatch.")
if axis == []:
raise ValueError(f"{info} Empty axis list")
if min(axis) < 0:
raise ValueError(f"{info} Negative axis specification not allowed.")

Expand Down Expand Up @@ -92,7 +94,7 @@ def upscale_array(
# Raise a warning if upscaling is non-homogeneous across all axis
if warn_if_inhomogeneous:
if len(set(upscale_factors.values())) > 1:
warnings.warn(info)
warnings.warn(f"{info} (inhomogeneous)")

# Upscale array, via np.repeat
upscaled_array = array
Expand Down Expand Up @@ -130,10 +132,10 @@ def upscale_array(

def convert_region_to_low_res(
*,
highres_region: Tuple[slice],
lowres_shape: Tuple[int],
highres_shape: Tuple[int],
) -> Tuple[slice]:
highres_region: tuple[slice, ...],
lowres_shape: tuple[int, ...],
highres_shape: tuple[int, ...],
) -> tuple[slice, ...]:
"""
Convert a region defined for a high-resolution array to the corresponding
region for a low-resolution array
Expand Down
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*_zenodo_*
data/*_zenodo_*
data/fake_multiplex
activate_venv.sh
File renamed without changes.
10 changes: 10 additions & 0 deletions tests/data/omero/channels_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
"label": "label_2",
"wavelength_id": "wavelength_id_2",
"window": {"start":0, "end": 180}
},
{
"label": "label_3",
"wavelength_id": "wavelength_id_3",
"window": {"start":0, "end": 180}
},
{
"label": "label_4",
"wavelength_id": "wavelength_id_4",
"window": {"start":0, "end": 180}
}
]
Loading
Loading