def test_predicate(): class Dummy: def _true(self): return True def _false(self): return False def _list(self): return [1, 2, 3] def _empty(self): return [] def _identity(self, arg): return arg d = Dummy() assert validate.Predicate("_true")(d) == d assert validate.Predicate("_list")(d) == d assert validate.Predicate("_identity", arg=True)(d) == d assert validate.Predicate("_identity", arg=1)(d) == d assert validate.Predicate("_identity", arg="abc")(d) == d with pytest.raises(ValidationError, match="Invalid input."): validate.Predicate("_false")(d) with pytest.raises(ValidationError): validate.Predicate("_empty")(d) with pytest.raises(ValidationError): validate.Predicate("_identity", arg=False)(d) with pytest.raises(ValidationError): validate.Predicate("_identity", arg=0)(d) with pytest.raises(ValidationError): validate.Predicate("_identity", arg="")(d)
def test_predicate_repr(): assert (repr(validate.Predicate(method='foo', error=None)) == '<Predicate(method={0!r}, kwargs={1!r}, error={2!r})>'.format( 'foo', {}, 'Invalid input.')) assert (repr(validate.Predicate(method='foo', error='bar', zoo=1)) == '<Predicate(method={0!r}, kwargs={1!r}, error={2!r})>'.format( 'foo', {str('zoo') if PY2 else 'zoo': 1}, 'bar'))
def test_predicate_repr(): assert repr( validate.Predicate(method="foo", error=None) ) == "<Predicate(method={!r}, kwargs={!r}, error={!r})>".format( "foo", {}, "Invalid input.") assert repr(validate.Predicate( method="foo", error="bar", zoo=1)) == "<Predicate(method={!r}, kwargs={!r}, error={!r})>".format( "foo", {"zoo": 1}, "bar")
def test_predicate_custom_message(): class Dummy(object): def _false(self): return False def __str__(self): return 'Dummy' d = Dummy() with pytest.raises(ValidationError) as excinfo: validate.Predicate('_false', error='{input}.{method} is invalid!')(d) assert 'Dummy._false is invalid!' in str(excinfo)
def test_predicate_custom_message(): class Dummy: def _false(self): return False def __str__(self): return "Dummy" d = Dummy() with pytest.raises(ValidationError, match="Dummy._false is invalid!"): validate.Predicate("_false", error="{input}.{method} is invalid!")(d)
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")
def test_predicate(): class Dummy(object): def _true(self): return True def _false(self): return False def _list(self): return [1, 2, 3] def _empty(self): return [] def _identity(self, arg): return arg d = Dummy() assert validate.Predicate('_true')(d) == d assert validate.Predicate('_list')(d) == d assert validate.Predicate('_identity', arg=True)(d) == d assert validate.Predicate('_identity', arg=1)(d) == d assert validate.Predicate('_identity', arg='abc')(d) == d with pytest.raises(ValidationError) as excinfo: validate.Predicate('_false')(d) assert 'Invalid input.' in str(excinfo) with pytest.raises(ValidationError): validate.Predicate('_empty')(d) with pytest.raises(ValidationError): validate.Predicate('_identity', arg=False)(d) with pytest.raises(ValidationError): validate.Predicate('_identity', arg=0)(d) with pytest.raises(ValidationError): validate.Predicate('_identity', arg='')(d)
class ScaleMeanVariance(PyBioSchema): mode = fields.ProcMode(required=True, valid_modes=("per_dataset", "per_sample")) reference_tensor: fields.String( required=True, validate=validate.Predicate("isidentifier"))
class ScaleRange(Preprocessing.ScaleRange): reference_tensor: fields.String( required=True, validate=validate.Predicate("isidentifier"))