Alexander Hirner <a.hir...@gmail.com> added the comment:

We construct a computational graph and need to validate (or search for possible 
new) edges at runtime. The data is specific to computer vision. One example is 
clipping geometry within 2D boundaries. A minimal implementation of this data 
would be:

@dataclass
class FramedGeometry:
    width: PositiveInt
    height: PositiveInt
    geometry: Geometry

However, different properties are manifest in different encodings. Height, 
width can be meta data from an image database or inherent to a decoded image 
(as np.ndarray.shape). The transform will then 
`dataclasses.replace(geometry=...)` its attribute(s).. If width and height are 
not implemented, another transition is needed to produce them whilst only 
looking into the image header, not fully decoding potentially large and many 
images.

The read-only interface ensures that transitions are generic wrt some forms of 
inputs. The replace interface preserves runtime types.

Inputs and outputs are annotated with @dataclass or tuples of them. Those 
dataclasses are a mixin of base dataclasses that declare concrete properties 
like a URI of an image and ABCs that declare accessors like get_width(self) -> 
PositiveInt. We use @pydantic.dataclass to parse, validate and deserialize 
concrete classes to great extent [0]. 

In order to not implement accessors on top of dataclasses, we'd want that 
abstract properties are compatible with dataclasses and issubclass works with 
data protocols (given the necessary constraints).

PS:
Polymorphism for computer-vision data is an interesting problem. Other 
approaches exist, each with their own struggle to model "traits" the right way 
[1]. E.g., scaling would be valid for `FramedGeometry` since no image data is 
included but invalid when images are referenced but cannot be resized, like:

class EncodedSizedAnnotatedFrame:
    width: PositiveInt
    height: PositiveInt
    image_bin: bytes
    geometry: Geometry

Thanks!

[0] https://pydantic-docs.helpmanual.io/usage/dataclasses/
[1] https://github.com/pytorch/vision/issues/1406

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39134>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to