From b029b5e62c8b7a9b2f9fd0e92f8e0a5789b4428f Mon Sep 17 00:00:00 2001 From: Charlotte Gerhaher Date: Wed, 22 Mar 2023 15:22:47 +0100 Subject: [PATCH] refactor: bytes to bytes_ in predefined documents (#1273) * refactor: bytes to bytes_ Signed-off-by: anna-charlotte * refactor: missed fields Signed-off-by: anna-charlotte --------- Signed-off-by: anna-charlotte --- docarray/documents/audio.py | 4 ++-- docarray/documents/image.py | 6 ++++-- docarray/documents/mesh/mesh_3d.py | 4 ++-- docarray/documents/point_cloud/point_cloud_3d.py | 6 ++++-- docarray/documents/text.py | 4 ++-- docarray/documents/video.py | 4 ++-- tests/integrations/predefined_document/test_audio.py | 2 +- tests/integrations/predefined_document/test_image.py | 8 ++++---- tests/units/array/test_array_from_to_csv.py | 4 ++-- tests/units/array/test_array_from_to_pandas.py | 2 +- tests/units/test_helper.py | 2 +- 11 files changed, 25 insertions(+), 21 deletions(-) diff --git a/docarray/documents/audio.py b/docarray/documents/audio.py index 03b64c2c518..036d28c33ba 100644 --- a/docarray/documents/audio.py +++ b/docarray/documents/audio.py @@ -90,7 +90,7 @@ class MultiModalDoc(Document): # equivalent to - mmdoc.audio.bytes = mmdoc.audio.url.load_bytes() + mmdoc.audio.bytes_ = mmdoc.audio.url.load_bytes() mmdoc.audio.tensor, mmdoc.audio.frame_rate = mmdoc.audio.bytes.load() @@ -99,7 +99,7 @@ class MultiModalDoc(Document): url: Optional[AudioUrl] tensor: Optional[AudioTensor] embedding: Optional[AnyEmbedding] - bytes: Optional[AudioBytes] + bytes_: Optional[AudioBytes] frame_rate: Optional[int] @classmethod diff --git a/docarray/documents/image.py b/docarray/documents/image.py index ed5791ee1a3..e1def7b0470 100644 --- a/docarray/documents/image.py +++ b/docarray/documents/image.py @@ -47,6 +47,7 @@ class ImageDoc(BaseDocument): from docarray.typing import AnyEmbedding from typing import Optional + # extend it class MyImage(ImageDoc): second_embedding: Optional[AnyEmbedding] @@ -66,6 +67,7 @@ class MyImage(ImageDoc): from docarray import BaseDocument from docarray.documents import ImageDoc, TextDoc + # compose it class MultiModalDoc(BaseDocument): image: Image @@ -78,7 +80,7 @@ class MultiModalDoc(BaseDocument): ) mmdoc.image.tensor = mmdoc.image.url.load() # or - mmdoc.image.bytes = mmdoc.image.url.load_bytes() + mmdoc.image.bytes_ = mmdoc.image.url.load_bytes() mmdoc.image.tensor = mmdoc.image.bytes.load() """ @@ -86,7 +88,7 @@ class MultiModalDoc(BaseDocument): url: Optional[ImageUrl] tensor: Optional[ImageTensor] embedding: Optional[AnyEmbedding] - bytes: Optional[ImageBytes] + bytes_: Optional[ImageBytes] @classmethod def validate( diff --git a/docarray/documents/mesh/mesh_3d.py b/docarray/documents/mesh/mesh_3d.py index 6da315a0b51..a2706493423 100644 --- a/docarray/documents/mesh/mesh_3d.py +++ b/docarray/documents/mesh/mesh_3d.py @@ -80,7 +80,7 @@ class MultiModalDoc(BaseDocument): mmdoc.mesh.tensors = mmdoc.mesh.url.load() # or - mmdoc.mesh.bytes = mmdoc.mesh.url.load_bytes() + mmdoc.mesh.bytes_ = mmdoc.mesh.url.load_bytes() You can display your 3D mesh in a notebook from either its url, or its tensors: @@ -104,7 +104,7 @@ class MultiModalDoc(BaseDocument): url: Optional[Mesh3DUrl] tensors: Optional[VerticesAndFaces] embedding: Optional[AnyEmbedding] - bytes: Optional[bytes] + bytes_: Optional[bytes] @classmethod def validate( diff --git a/docarray/documents/point_cloud/point_cloud_3d.py b/docarray/documents/point_cloud/point_cloud_3d.py index 3a1b2918c36..52958a81ddb 100644 --- a/docarray/documents/point_cloud/point_cloud_3d.py +++ b/docarray/documents/point_cloud/point_cloud_3d.py @@ -54,6 +54,7 @@ class PointCloud3D(BaseDocument): from docarray.typing import AnyEmbedding from typing import Optional + # extend it class MyPointCloud3D(PointCloud3D): second_embedding: Optional[AnyEmbedding] @@ -73,6 +74,7 @@ class MyPointCloud3D(PointCloud3D): from docarray import BaseDocument from docarray.documents import PointCloud3D, Text + # compose it class MultiModalDoc(BaseDocument): point_cloud: PointCloud3D @@ -89,7 +91,7 @@ class MultiModalDoc(BaseDocument): # or - mmdoc.point_cloud.bytes = mmdoc.point_cloud.url.load_bytes() + mmdoc.point_cloud.bytes_ = mmdoc.point_cloud.url.load_bytes() You can display your point cloud from either its url, or its tensors: @@ -112,7 +114,7 @@ class MultiModalDoc(BaseDocument): url: Optional[PointCloud3DUrl] tensors: Optional[PointsAndColors] embedding: Optional[AnyEmbedding] - bytes: Optional[bytes] + bytes_: Optional[bytes] @classmethod def validate( diff --git a/docarray/documents/text.py b/docarray/documents/text.py index 98a335fb268..d88f0c4395b 100644 --- a/docarray/documents/text.py +++ b/docarray/documents/text.py @@ -78,7 +78,7 @@ class MultiModalDoc(BaseDocument): # or - mmdoc.text_doc.bytes = mmdoc.text_doc.url.load_bytes() + mmdoc.text_doc.bytes_ = mmdoc.text_doc.url.load_bytes() This Document can be compared against another Document of the same type or a string. @@ -101,7 +101,7 @@ class MultiModalDoc(BaseDocument): text: Optional[str] url: Optional[TextUrl] embedding: Optional[AnyEmbedding] - bytes: Optional[bytes] + bytes_: Optional[bytes] def __init__(self, text: Optional[str] = None, **kwargs): if 'text' not in kwargs: diff --git a/docarray/documents/video.py b/docarray/documents/video.py index 407d966fb3c..3e338b632db 100644 --- a/docarray/documents/video.py +++ b/docarray/documents/video.py @@ -93,7 +93,7 @@ class MultiModalDoc(BaseDocument): # or - mmdoc.video.bytes = mmdoc.video.url.load_bytes() + mmdoc.video.bytes_ = mmdoc.video.url.load_bytes() """ @@ -102,7 +102,7 @@ class MultiModalDoc(BaseDocument): tensor: Optional[VideoTensor] key_frame_indices: Optional[AnyTensor] embedding: Optional[AnyEmbedding] - bytes: Optional[bytes] + bytes_: Optional[bytes] @classmethod def validate( diff --git a/tests/integrations/predefined_document/test_audio.py b/tests/integrations/predefined_document/test_audio.py index 3edeb3aee77..e665f3d9d93 100644 --- a/tests/integrations/predefined_document/test_audio.py +++ b/tests/integrations/predefined_document/test_audio.py @@ -198,7 +198,7 @@ def test_audio_tensorflow(): def test_audio_bytes(): audio = parse_obj_as(AudioDoc, torch.zeros(10, 10, 3)) - audio.bytes = audio.tensor.to_bytes() + audio.bytes_ = audio.tensor.to_bytes() def test_audio_shortcut_doc(): diff --git a/tests/integrations/predefined_document/test_image.py b/tests/integrations/predefined_document/test_image.py index 31285dc98dd..b2b7077ad43 100644 --- a/tests/integrations/predefined_document/test_image.py +++ b/tests/integrations/predefined_document/test_image.py @@ -70,7 +70,7 @@ class MyDoc(BaseDocument): def test_byte(): img = ImageDoc(url=REMOTE_JPG) - img.bytes = img.url.load_bytes() + img.bytes_ = img.url.load_bytes() @pytest.mark.slow @@ -79,7 +79,7 @@ def test_byte_from_tensor(): img = ImageDoc(url=REMOTE_JPG) img.tensor = img.url.load() - img.bytes = img.tensor.to_bytes() + img.bytes_ = img.tensor.to_bytes() - assert isinstance(img.bytes, bytes) - assert len(img.bytes) > 0 + assert isinstance(img.bytes_, bytes) + assert len(img.bytes_) > 0 diff --git a/tests/units/array/test_array_from_to_csv.py b/tests/units/array/test_array_from_to_csv.py index f4c6e89b9ff..5fc81e0f40b 100644 --- a/tests/units/array/test_array_from_to_csv.py +++ b/tests/units/array/test_array_from_to_csv.py @@ -58,12 +58,12 @@ def test_from_csv_nested(nested_doc_cls): assert doc.image.__class__ == ImageDoc assert doc.image.tensor is None assert doc.image.embedding is None - assert doc.image.bytes is None + assert doc.image.bytes_ is None assert doc.image2.__class__ == ImageDoc assert doc.image2.tensor is None assert doc.image2.embedding is None - assert doc.image2.bytes is None + assert doc.image2.bytes_ is None assert da[0].image2.url == 'image_10.png' assert da[1].image2.url is None diff --git a/tests/units/array/test_array_from_to_pandas.py b/tests/units/array/test_array_from_to_pandas.py index a93726590a3..c6a54322efd 100644 --- a/tests/units/array/test_array_from_to_pandas.py +++ b/tests/units/array/test_array_from_to_pandas.py @@ -43,7 +43,7 @@ def test_to_from_pandas_df(nested_doc_cls): 'image__url', 'image__tensor', 'image__embedding', - 'image__bytes', + 'image__bytes_', ] ).all() diff --git a/tests/units/test_helper.py b/tests/units/test_helper.py index c31f7aab2cc..69649074bbd 100644 --- a/tests/units/test_helper.py +++ b/tests/units/test_helper.py @@ -62,7 +62,7 @@ class Painting(BaseDocument): 'img__url', 'img__tensor', 'img__embedding', - 'img__bytes', + 'img__bytes_', ]