Exemple #1
0
class Array(PyBioSchema):
    name = fields.String(required=True)
    axes = fields.Axes(missing=None)
    data_type = fields.String(required=True)
    data_range = fields.Tuple((fields.Float(allow_nan=True), fields.Float(allow_nan=True)))

    shape: fields.Nested
    class ScaleRange(PyBioSchema):
        mode = fields.ProcMode(required=True,
                               valid_modes=("per_dataset", "per_sample"))
        axes = fields.Axes(required=True, valid_axes="czyx")
        min_percentile = fields.Float(required=True,
                                      validate=validate.Range(
                                          0,
                                          100,
                                          min_inclusive=True,
                                          max_inclusive=True))
        max_percentile = fields.Float(
            required=True,
            validate=validate.Range(1,
                                    100,
                                    min_inclusive=False,
                                    max_inclusive=True)
        )  # as a precaution 'max_percentile' needs to be greater than 1

        @validates_schema
        def min_smaller_max(self, data, **kwargs):
            min_p = data["min_percentile"]
            max_p = data["max_percentile"]
            if min_p >= max_p:
                raise PyBioValidationException(
                    f"min_percentile {min_p} >= max_percentile {max_p}")
    class ScaleLinear(PyBioSchema):
        axes = fields.Axes(required=True, valid_axes="czyx")
        gain = fields.Array(fields.Float(), missing=fields.Float(
            missing=1.0))  # todo: check if gain match input axes
        offset = fields.Array(
            fields.Float(), missing=fields.Float(
                missing=0.0))  # todo: check if offset match input axes

        @validates_schema
        def either_gain_or_offset(self, data, **kwargs):
            if data["gain"] == 1.0 and data["offset"] == 0:
                raise PyBioValidationException(
                    "Specify gain!=1.0 or offset!=0.0")
class Tensor(PyBioSchema):
    name = fields.String(required=True,
                         validate=validate.Predicate("isidentifier"),
                         bioimageio_description="Tensor name.")
    description = fields.String(missing=None)
    axes = fields.Axes(
        required=True,
        bioimageio_description=
        """Axes identifying characters from: bitczyx. Same length and order as the axes in `shape`.

    | character | description |
    | --- | --- |
    |  b  |  batch (groups multiple samples) |
    |  i  |  instance/index/element |
    |  t  |  time |
    |  c  |  channel |
    |  z  |  spatial dimension z |
    |  y  |  spatial dimension y |
    |  x  |  spatial dimension x |""",
    )
    data_type = fields.String(
        required=True,
        bioimageio_description=
        "The data type of this tensor. For inputs, only `float32` is allowed and the consumer "
        "software needs to ensure that the correct data type is passed here. For outputs can be any of `float32, "
        "float64, (u)int8, (u)int16, (u)int32, (u)int64`. The data flow in bioimage.io models is explained "
        "[in this diagram.](https://docs.google.com/drawings/d/1FTw8-Rn6a6nXdkZ_SkMumtcjvur9mtIhRqLwnKqZNHM/edit).",
    )
    data_range = fields.Tuple(
        (fields.Float(allow_nan=True), fields.Float(allow_nan=True)),
        missing=(None, None),
        bioimageio_description=
        "Tuple `(minimum, maximum)` specifying the allowed range of the data in this tensor. "
        "If not specified, the full data range that can be expressed in `data_type` is allowed.",
    )
    shape: fields.Union

    processing_name: str

    @validates_schema
    def validate_processing_kwargs(self, data, **kwargs):
        axes = data["axes"]
        processing_list = data.get(self.processing_name, [])
        for processing in processing_list:
            name = processing.name
            kwargs = processing.kwargs or {}
            kwarg_axes = kwargs.get("axes", "")
            if any(a not in axes for a in kwarg_axes):
                raise PyBioValidationException(
                    "`kwargs.axes` needs to be subset of axes")
    class ZeroMeanUnitVariance(PyBioSchema):
        mode = fields.ProcMode(required=True)
        axes = fields.Axes(required=True, valid_axes="czyx")
        mean = fields.Array(
            fields.Float(), missing=None
        )  # todo: check if means match input axes (for mode 'fixed')
        std = fields.Array(fields.Float(), missing=None)
        eps = fields.Float(missing=1e-6)

        @validates_schema
        def mean_and_std_match_mode(self, data, **kwargs):
            if data["mode"] == "fixed" and (data["mean"] is None
                                            or data["std"] is None):
                raise PyBioValidationException(
                    "`kwargs` for 'zero_mean_unit_variance' preprocessing with `mode` 'fixed' require additional `kwargs`: `mean` and `std`."
                )
            elif data["mode"] != "fixed" and (data.get("mean") is not None
                                              or data.get("std") is not None):
                raise PyBioValidationException(
                    "`kwargs`: `mean` and `std` for 'zero_mean_unit_variance' preprocessing are only valid for `mode` 'fixed'."
                )
 class Clip(PyBioSchema):
     min = fields.Float(required=True)
     max = fields.Float(required=True)
 class Binarize(
         Schema
 ):  # do not inherit from PyBioSchema, return only a validated dict, no specific node
     threshold = fields.Float(required=True)