|
| 1 | +""" |
| 2 | +This is the Python module for my_task |
| 3 | +""" |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import zarr |
| 10 | +import dask.array as da |
| 11 | + |
| 12 | +from pydantic.decorator import validate_arguments |
| 13 | + |
| 14 | +from fractal_tasks_core.ngff import load_NgffImageMeta |
| 15 | +from fractal_tasks_core.pyramids import build_pyramid |
| 16 | + |
| 17 | +@validate_arguments |
| 18 | +def thresholding_task( |
| 19 | + *, |
| 20 | + input_paths: list[str], |
| 21 | + output_path: str, |
| 22 | + component: str, |
| 23 | + metadata: dict[str, Any], |
| 24 | +) -> None: |
| 25 | + """ |
| 26 | + Short description of thresholding_task. |
| 27 | +
|
| 28 | + Long description of thresholding_task. |
| 29 | +
|
| 30 | + Args: |
| 31 | + input_paths: Path to the parent folder of the NGFF image. |
| 32 | + This task only supports a single input path. |
| 33 | + (standard argument for Fractal tasks, managed by Fractal server). |
| 34 | + output_path: This argument is not used in this task. |
| 35 | + (standard argument for Fractal tasks, managed by Fractal server). |
| 36 | + component: Path of the NGFF image, relative to `input_paths[0]`. |
| 37 | + (standard argument for Fractal tasks, managed by Fractal server). |
| 38 | + metadata: This argument is not used in this task. |
| 39 | + (standard argument for Fractal tasks, managed by Fractal server). |
| 40 | + """ |
| 41 | + |
| 42 | + # Use the first of input_paths |
| 43 | + input_path = (Path(input_paths[0]) / component).as_posix() |
| 44 | + logging.info(f"input_path set to {input_path}") |
| 45 | + |
| 46 | + # Parse and log several NGFF-image metadata attributes |
| 47 | + ngff_image_meta = load_NgffImageMeta(input_path) |
| 48 | + logging.info(f" Axes: {ngff_image_meta.axes_names}") |
| 49 | + logging.info(f" Number of pyramid levels: {ngff_image_meta.num_levels}") |
| 50 | + logging.info(f" Linear coarsening factor for YX axes: {ngff_image_meta.coarsening_xy}") |
| 51 | + logging.info(f" Full-resolution ZYX pixel sizes (micrometer): {ngff_image_meta.get_pixel_sizes_zyx(level=0)}") |
| 52 | + logging.info(f" Coarsening-level-1 ZYX pixel sizes (micrometer): {ngff_image_meta.get_pixel_sizes_zyx(level=1)}") |
| 53 | + |
| 54 | + # Load the highest-resolution multiscale array through dask.array |
| 55 | + array_czyx = da.from_zarr(f"{input_path}/0") |
| 56 | + logging.info(f"{array_czyx=}") |
| 57 | + |
| 58 | + # Set values below 100 to 0 |
| 59 | + array_max = array_czyx.max().compute() |
| 60 | + array_min = array_czyx.min().compute() |
| 61 | + logging.info(f"Pre thresholding: {array_min=}, {array_max=}") |
| 62 | + array_czyx[array_czyx < 99] = 99 |
| 63 | + array_czyx[array_czyx > 1000] = 1000 |
| 64 | + array_max = array_czyx.max().compute() |
| 65 | + array_min = array_czyx.min().compute() |
| 66 | + logging.info(f"Post thresholding: {array_min=}, {array_max=}") |
| 67 | + |
| 68 | + # Write the processed array back to the same full-resolution Zarr array |
| 69 | + array_czyx.to_zarr(f"{input_path}/0", overwrite=True) |
| 70 | + |
| 71 | + # Starting from on-disk full-resolution data, build and write to disk a |
| 72 | + # pyramid of coarser levels |
| 73 | + build_pyramid( |
| 74 | + zarrurl=input_path, |
| 75 | + overwrite=True, |
| 76 | + num_levels=ngff_image_meta.num_levels, |
| 77 | + coarsening_xy=ngff_image_meta.coarsening_xy, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + from fractal_tasks_core.tasks._utils import run_fractal_task |
| 83 | + |
| 84 | + run_fractal_task(task_function=thresholding_task) |
0 commit comments