コード例 #1
0
    class scale_linear(SharedProcessingSchema):
        bioimageio_description = "Fixed linear scaling."
        axes = fields.Axes(
            required=True,
            valid_axes="czyx",
            bioimageio_description="The subset of axes to scale jointly. "
            "For example xy to scale the two image axes for 2d data jointly. "
            "The batch axis (b) is not valid here.",
        )
        gain = fields.Array(
            fields.Float(), missing=fields.Float(missing=1.0), bioimageio_description="multiplicative factor"
        )  # todo: check if gain match input axes
        offset = fields.Array(
            fields.Float(), missing=fields.Float(missing=0.0), bioimageio_description="additive term"
        )  # todo: check if offset match input axes

        @validates_schema
        def either_gain_or_offset(self, data, **kwargs):
            gain = data["gain"]
            if isinstance(gain, (float, int)):
                gain = [gain]

            offset = data["offset"]
            if isinstance(offset, (float, int)):
                offset = [offset]

            if all(g == 1.0 for g in gain) and all(off == 0 for off in offset):
                raise ValidationError("Specify gain!=1.0 or offset!=0.0")
コード例 #2
0
    class zero_mean_unit_variance(SharedProcessingSchema):
        bioimageio_description = "Subtract mean and divide by variance."
        mode = fields.ProcMode(required=True)
        axes = fields.Axes(
            required=True,
            valid_axes="czyx",
            bioimageio_description="The subset of axes to normalize jointly. For example xy to normalize the two image "
            "axes for 2d data jointly. The batch axis (b) is not valid here.",
        )
        mean = fields.Array(
            fields.Float(),
            bioimageio_description="The mean value(s) to use for `mode == fixed`. For example `[1.1, 2.2, 3.3]` in the "
            "case of a 3 channel image where the channels are not normalized jointly.",
        )  # todo: check if means match input axes (for mode 'fixed')
        std = fields.Array(
            fields.Float(),
            bioimageio_description="The standard deviation values to use for `mode == fixed`. Analogous to mean.",
        )
        eps = fields.Float(
            missing=1e-6,
            bioimageio_description="epsilon for numeric stability: `out = (tensor - mean) / (std + eps)`. "
            "Default value: 10^-6.",
        )

        @validates_schema
        def mean_and_std_match_mode(self, data, **kwargs):
            if data["mode"] == "fixed" and ("mean" not in data or "std" not in data):
                raise ValidationError(
                    "`kwargs` for 'zero_mean_unit_variance' preprocessing with `mode` 'fixed' require additional "
                    "`kwargs`: `mean` and `std`."
                )
            elif data["mode"] != "fixed" and ("mean" in data or "std" in data):
                raise ValidationError(
                    "`kwargs`: `mean` and `std` for 'zero_mean_unit_variance' preprocessing are only valid for `mode` 'fixed'."
                )
コード例 #3
0
 def test_wrong_dtype(self):
     data = [[1, 2], [3, 4.5]]
     with raises(ValidationError):
         fields.Array(fields.Integer(strict=True)).deserialize(data)
コード例 #4
0
 def test_2d(self):
     data = [[1, 2], [3, 4]]
     expected = numpy.array(data, dtype=int)
     actual = fields.Array(fields.Integer(strict=True)).deserialize(data)
     assert_equal(actual, expected)
コード例 #5
0
 def test_invalid_scalar(self):
     data = "invalid"
     with raises(ValidationError):
         fields.Array(fields.Integer(strict=True)).deserialize(data)
コード例 #6
0
 def test_scalar(self):
     data = 1
     expected = data
     actual = fields.Array(fields.Integer(strict=True)).deserialize(data)
     assert_equal(actual, expected)
コード例 #7
0
 def test_uneuqal_sublen(self):
     with raises(ValidationError):
         fields.Array(fields.Integer(strict=True)).deserialize([[1, 2],
                                                                [3]])
コード例 #8
0
 def test_unequal_nesting_depth(self):
     with raises(ValidationError):
         fields.Array(fields.Integer(strict=True)).deserialize([[1, 2], 3])