Esempio n. 1
0
    def zero_batch_step(self, data, **kwargs):
        axes = data["axes"]
        shape = data["shape"]
        if not isinstance(shape, MagicShapeValue):
            if axes is None:
                raise PyBioValidationException("Axes field required when shape is specified")

            assert isinstance(axes, str), type(axes)

            bidx = axes.find("b")
            if isinstance(shape, tuple):
                # exact shape (with batch size 1)
                if bidx != -1 and shape[bidx] != 1:
                    raise PyBioValidationException("Input shape has to be one in the batch dimension.")

            elif isinstance(shape, nodes.InputShape):
                step = shape.step
                if bidx != -1 and shape.min[bidx] != 1:
                    raise PyBioValidationException("Input shape has to be one in the batch dimension.")

                if bidx != -1 and step[bidx] != 0:
                    raise PyBioValidationException(
                        "Input shape step has to be zero in the batch dimension (the batch dimension can always be "
                        "increased, but `step` should specify how to increase the minimal shape to find the largest "
                        "single batch shape)"
                    )
            else:
                raise PyBioValidationException(f"Unknown shape type {type(shape)}")
Esempio n. 2
0
    def _deserialize(
        self,
        value: typing.Any,
        attr: typing.Optional[str],
        data: typing.Optional[typing.Mapping[str, typing.Any]],
        **kwargs,
    ):
        if isinstance(value, str):
            try:
                value = nodes.MagicShapeValue(value)
            except ValueError as e:
                raise PyBioValidationException(str(e)) from e

            if value in self.valid_magic_values:
                return value
            else:
                raise PyBioValidationException(
                    f"Invalid magic value: {value.value}")

        elif isinstance(value, list):
            if any(not isinstance(v, int) for v in value):
                raise PyBioValidationException(
                    "Encountered non-integers in shape")

            return tuple(value)
        elif isinstance(value, dict):
            return self._load(value, data)
        else:
            raise PyBioValidationException(
                f"Invalid input type: {type(value)}")
Esempio n. 3
0
    def _deserialize(
        self,
        value: typing.Any,
        attr: typing.Optional[str],
        data: typing.Optional[typing.Mapping[str, typing.Any]],
        **kwargs,
    ):
        if isinstance(value, str):
            try:
                value = nodes.MagicTensorsValue(value)
            except ValueError as e:
                raise PyBioValidationException(str(e)) from e

            if value in self.valid_magic_values:
                return value
            else:
                raise PyBioValidationException(
                    f"Invalid magic value: {value.value}")

        elif isinstance(value, list):
            return self._load(value, data, many=True)
            # if all(isinstance(v, str) for v in value):
            #     return namedtuple("CustomTensors", value)
            # else:
            #     return self._load(value, data, many=True)
        else:
            raise PyBioValidationException(
                f"Invalid input type: {type(value)}")
Esempio n. 4
0
    def _deserialize(self, *args, **kwargs) -> nodes.URI:
        uri_str = super()._deserialize(*args, **kwargs)
        uri = urlparse(uri_str)

        if uri.fragment:
            raise PyBioValidationException(
                f"Invalid URI: {uri}. Got URI fragment: {uri.fragment}")
        if uri.params:
            raise PyBioValidationException(
                f"Invalid URI: {uri}. Got URI params: {uri.params}")

        return nodes.URI(scheme=uri.scheme,
                         netloc=uri.netloc,
                         path=uri.path,
                         query=uri.query)
Esempio n. 5
0
    def _deserialize(self, *args, **kwargs) -> str:
        axes_str = super()._deserialize(*args, **kwargs)
        valid_axes = "bczyx"
        if any(a not in valid_axes for a in axes_str):
            raise PyBioValidationException(
                f"Invalid axes! Valid axes are: {valid_axes}")

        return axes_str
Esempio n. 6
0
    def _deserialize(self, value, attr, data, **kwargs):
        uri = urlparse(value)

        if uri.query:
            raise PyBioValidationException(
                f"Invalid URI: {uri}. Got URI query: {uri.query}")
        if uri.fragment:
            raise PyBioValidationException(
                f"Invalid URI: {uri}. Got URI fragment: {uri.fragment}")
        if uri.params:
            raise PyBioValidationException(
                f"Invalid URI: {uri}. Got URI params: {uri.params}")

        return nodes.SpecURI(spec_schema=self.schema,
                             scheme=uri.scheme,
                             netloc=uri.netloc,
                             path=uri.path,
                             query="")
Esempio n. 7
0
    def matching_lengths(self, data, **kwargs):
        min_ = data["min"]
        step = data["step"]
        if min_ is None or step is None:
            return

        if len(min_) != len(step):
            raise PyBioValidationException(
                f"'min' and 'step' have to have the same length! (min: {min_}, step: {step})"
            )
Esempio n. 8
0
 def matching_halo_length(self, data, **kwargs):
     shape = data["shape"]
     halo = data["halo"]
     if halo is None:
         return
     elif isinstance(shape, tuple) or isinstance(shape, nodes.OutputShape):
         if len(halo) != len(shape):
             raise PyBioValidationException(f"halo {halo} has to have same length as shape {shape}!")
     elif not isinstance(shape, MagicShapeValue):
         raise NotImplementedError
Esempio n. 9
0
 def axes_and_shape(self, data, **kwargs):
     axes = data["axes"]
     shape = data["shape"]
     if not isinstance(shape, MagicShapeValue) and shape and axes is None:
         raise PyBioValidationException("Axes may not be 'null', when shape is specified")
Esempio n. 10
0
 def matching_lengths(self, data, **kwargs):
     scale = data["scale"]
     offset = data["offset"]
     if len(scale) != len(offset):
         raise PyBioValidationException(f"scale {scale} has to have same length as offset {offset}!")