Skip to content

Commit

Permalink
Merge pull request #765 from fractal-analytics-platform/pydantic_v2
Browse files Browse the repository at this point in the history
Relax pydantic requirements || Switch to transitional pydantic.v1
  • Loading branch information
lorenzocerrone authored Jun 20, 2024
2 parents 371bbc0 + 600be9a commit 62c4192
Show file tree
Hide file tree
Showing 33 changed files with 385 additions and 304 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Refactor Cellpose Task inputs: Support independent normalization of 2 input channels in the Cellpose task (\#738).
* Rename `task.cellpose_transforms` into `tasks.cellpose_utils` (\#738).
* Development:
* Switch to transitional pydantic.v1 imports, changes pydantic requirement to `==1.10.16` or `>=2.6.3` (\#760).
* Support JSON-Schema generation for `Enum` task arguments (\#749).
* Make JSON-Schema generation tools more flexible, to simplify testing (\#749).
* Update documentation (\#751).
Expand Down
20 changes: 20 additions & 0 deletions fractal_tasks_core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import logging


def _check_pydantic_version():
"""
Temporary check for pydantic version.
To be removed after moving to pydantic v2 is complete.
"""
import importlib.metadata
from packaging import version

pydantic_version = version.parse(importlib.metadata.version("pydantic"))
pydantic_v1 = version.parse("1.10.16")
pydantic_v2 = version.parse("2.6.3")
if pydantic_version != pydantic_v1 and pydantic_version < pydantic_v2:
raise ImportError(
f"Pydantic version {pydantic_version} is not supported. "
f"Please use version =={pydantic_v1} or >={pydantic_v2}."
)


_check_pydantic_version()

logging.basicConfig(
level=logging.INFO, format="%(asctime)s; %(levelname)s; %(message)s"
)
Expand Down
4 changes: 2 additions & 2 deletions fractal_tasks_core/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from typing import Union

import zarr
from pydantic import BaseModel
from pydantic import validator
from pydantic.v1 import BaseModel
from pydantic.v1 import validator

from fractal_tasks_core import __OME_NGFF_VERSION__

Expand Down
10 changes: 5 additions & 5 deletions fractal_tasks_core/dev/lib_args_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from typing import Optional

from docstring_parser import parse as docparse
from pydantic.decorator import ALT_V_ARGS
from pydantic.decorator import ALT_V_KWARGS
from pydantic.decorator import V_DUPLICATE_KWARGS
from pydantic.decorator import V_POSITIONAL_ONLY_NAME
from pydantic.decorator import ValidatedFunction
from pydantic.v1.decorator import ALT_V_ARGS
from pydantic.v1.decorator import ALT_V_KWARGS
from pydantic.v1.decorator import V_DUPLICATE_KWARGS
from pydantic.v1.decorator import V_POSITIONAL_ONLY_NAME
from pydantic.v1.decorator import ValidatedFunction

from fractal_tasks_core.dev.lib_descriptions import (
_get_class_attrs_descriptions,
Expand Down
8 changes: 4 additions & 4 deletions fractal_tasks_core/dev/lib_signature_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from pathlib import Path
from typing import Callable

from pydantic.decorator import ALT_V_ARGS
from pydantic.decorator import ALT_V_KWARGS
from pydantic.decorator import V_DUPLICATE_KWARGS
from pydantic.decorator import V_POSITIONAL_ONLY_NAME
from pydantic.v1.decorator import ALT_V_ARGS
from pydantic.v1.decorator import ALT_V_KWARGS
from pydantic.v1.decorator import V_DUPLICATE_KWARGS
from pydantic.v1.decorator import V_POSITIONAL_ONLY_NAME

FORBIDDEN_PARAM_NAMES = (
"args",
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/dev/task_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from typing import Any
from typing import Optional

from pydantic import BaseModel
from pydantic.v1 import BaseModel


class _BaseTask(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Optional

import zarr.hierarchy
from pydantic.error_wrappers import ValidationError
from pydantic.v1.error_wrappers import ValidationError

from fractal_tasks_core.ngff import NgffImageMeta
from fractal_tasks_core.zarr_utils import OverwriteNotAllowedError
Expand Down
6 changes: 3 additions & 3 deletions fractal_tasks_core/ngff/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from typing import Optional
from typing import Union

from pydantic import BaseModel
from pydantic import Field
from pydantic import validator
from pydantic.v1 import BaseModel
from pydantic.v1 import Field
from pydantic.v1 import validator


logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/roi/v1_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import anndata as ad
import zarr
from pydantic.error_wrappers import ValidationError
from pydantic.v1.error_wrappers import ValidationError

from fractal_tasks_core.tables.v1 import MaskingROITableAttrs

Expand Down
6 changes: 3 additions & 3 deletions fractal_tasks_core/tables/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import anndata as ad
import zarr.hierarchy
from anndata.experimental import write_elem
from pydantic import BaseModel
from pydantic import validator
from pydantic.error_wrappers import ValidationError
from pydantic.v1 import BaseModel
from pydantic.v1 import validator
from pydantic.v1.error_wrappers import ValidationError

from fractal_tasks_core.zarr_utils import OverwriteNotAllowedError

Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/apply_registration_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import dask.array as da
import numpy as np
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.ngff import load_NgffImageMeta
from fractal_tasks_core.ngff.zarr_utils import load_NgffWellMeta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import dask.array as da
import numpy as np
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments
from skimage.registration import phase_cross_correlation

from fractal_tasks_core.channels import get_channel_from_image_zarr
Expand Down
4 changes: 2 additions & 2 deletions fractal_tasks_core/tasks/cellpose_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import pandas as pd
import zarr
from cellpose import models
from pydantic import Field
from pydantic.decorator import validate_arguments
from pydantic.v1 import Field
from pydantic.v1.decorator import validate_arguments

import fractal_tasks_core
from fractal_tasks_core.labels import prepare_label_group
Expand Down
8 changes: 4 additions & 4 deletions fractal_tasks_core/tasks/cellpose_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
from typing import Optional

import numpy as np
from pydantic import BaseModel
from pydantic import Field
from pydantic import root_validator
from pydantic import validator
from pydantic.v1 import BaseModel
from pydantic.v1 import Field
from pydantic.v1 import root_validator
from pydantic.v1 import validator

from fractal_tasks_core.channels import ChannelInputModel
from fractal_tasks_core.channels import ChannelNotFoundError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import zarr
from anndata import read_zarr
from dask.array.image import imread
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.cellvoyager.filenames import (
glob_with_multiple_patterns,
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/cellvoyager_to_ome_zarr_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Optional

import pandas as pd
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

import fractal_tasks_core
from fractal_tasks_core.cellvoyager.filenames import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import pandas as pd
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments
from zarr.errors import ContainsGroupError

import fractal_tasks_core
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/copy_ome_zarr_hcs_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Any

import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

import fractal_tasks_core
from fractal_tasks_core.ngff.specs import NgffPlateMeta
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/find_registration_consensus.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import anndata as ad
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.roi import (
are_ROI_table_columns_valid,
Expand Down
3 changes: 1 addition & 2 deletions fractal_tasks_core/tasks/illumination_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import dask.array as da
import numpy as np
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments
from skimage.io import imread

from fractal_tasks_core.channels import get_omero_channel_list
Expand Down Expand Up @@ -106,7 +106,6 @@ def illumination_correction(
# Advanced parameters
suffix: str = "_illum_corr",
) -> dict[str, Any]:

"""
Applies illumination correction to the images in the OME-Zarr.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from typing import Any

from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.tasks._registration_utils import (
create_well_acquisition_dict,
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/import_ome_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import dask.array as da
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.channels import update_omero_channels
from fractal_tasks_core.ngff import detect_ome_ngff_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
import logging

from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

from fractal_tasks_core.tasks._registration_utils import (
create_well_acquisition_dict,
Expand Down
6 changes: 3 additions & 3 deletions fractal_tasks_core/tasks/io_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import Literal
from typing import Optional

from pydantic import BaseModel
from pydantic import Field
from pydantic import validator
from pydantic.v1 import BaseModel
from pydantic.v1 import Field
from pydantic.v1 import validator

from fractal_tasks_core.channels import ChannelInputModel
from fractal_tasks_core.channels import OmeroChannel
Expand Down
2 changes: 1 addition & 1 deletion fractal_tasks_core/tasks/maximum_intensity_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import anndata as ad
import dask.array as da
import zarr
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments
from zarr.errors import ContainsArrayError

from fractal_tasks_core.ngff import load_NgffImageMeta
Expand Down
14 changes: 7 additions & 7 deletions fractal_tasks_core/tasks/napari_workflows_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pandas as pd
import zarr
from napari_workflows._io_yaml_v1 import load_workflow
from pydantic.decorator import validate_arguments
from pydantic.v1.decorator import validate_arguments

import fractal_tasks_core
from fractal_tasks_core.channels import get_channel_from_image_zarr
Expand Down Expand Up @@ -208,7 +208,7 @@ def napari_workflows_wrapper(
if image_inputs:
img_array = da.from_zarr(f"{zarr_url}/{level}")
# Loop over image inputs and assign corresponding channel of the image
for (name, params) in image_inputs:
for name, params in image_inputs:
channel = get_channel_from_image_zarr(
image_zarr_path=zarr_url,
wavelength_id=params.channel.wavelength_id,
Expand Down Expand Up @@ -261,7 +261,7 @@ def napari_workflows_wrapper(
upscale_labels = True
# Loop over label inputs and load corresponding (upscaled) image
input_label_arrays = {}
for (name, params) in label_inputs:
for name, params in label_inputs:
label_name = params.label_name
label_array_raw = da.from_zarr(
f"{zarr_url}/labels/{label_name}/{level}"
Expand Down Expand Up @@ -393,7 +393,7 @@ def napari_workflows_wrapper(

# Loop over label outputs and (1) set zattrs, (2) create zarr group
output_label_zarr_groups: dict[str, Any] = {}
for (name, out_params) in label_outputs:
for name, out_params in label_outputs:

# (1a) Rescale OME-NGFF datasets (relevant for level>0)
if not ngff_image_meta.multiscale.axes[0].name == "c":
Expand Down Expand Up @@ -467,7 +467,7 @@ def napari_workflows_wrapper(
if out_params.type == "dataframe"
]
output_dataframe_lists: dict[str, list] = {}
for (name, out_params) in dataframe_outputs:
for name, out_params in dataframe_outputs:
output_dataframe_lists[name] = []
logger.info(f"Prepared output with {name=} and {out_params=}")
logger.info(f"{output_dataframe_lists=}")
Expand Down Expand Up @@ -581,7 +581,7 @@ def napari_workflows_wrapper(

# Output handling: "dataframe" type (for each output, concatenate ROI
# dataframes, clean up, and store in a AnnData table on-disk)
for (name, out_params) in dataframe_outputs:
for name, out_params in dataframe_outputs:
table_name = out_params.table_name
# Concatenate all FOV dataframes
list_dfs = output_dataframe_lists[name]
Expand Down Expand Up @@ -617,7 +617,7 @@ def napari_workflows_wrapper(

# Output handling: "label" type (for each output, build and write to disk
# pyramid of coarser levels)
for (name, out_params) in label_outputs:
for name, out_params in label_outputs:
label_name = out_params.label_name
build_pyramid(
zarrurl=f"{zarr_url}/labels/{label_name}",
Expand Down
Loading

0 comments on commit 62c4192

Please sign in to comment.