Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
943d636
feat: display mesh and pointcloud
Feb 9, 2023
e9acd12
chore: update poetry
Feb 9, 2023
c91ab6c
fix: mypy
Feb 9, 2023
e979c32
fix: add display from param to mesh and pc display
Feb 9, 2023
21fb8ab
fix: clean up
Feb 9, 2023
75b33d8
fix: mypy
Feb 9, 2023
7890194
fix: move display from url to mesh and pc url classes
Feb 10, 2023
0129c3a
chore: remove pyglet dependency
Feb 10, 2023
322a718
chore: update pyproject toml
Feb 10, 2023
05d8461
refactor: copy is notebook function from hubble sdk
Feb 10, 2023
0244816
fix: introduce vertices and faces doc
Feb 10, 2023
255795c
fix: introduce points and colors class for point cloud
Feb 10, 2023
db42712
fix: mypy and tests
Feb 10, 2023
dca04ce
docs: add display example to docs
Feb 10, 2023
788f834
fix: apply johannes suggestion from review
Feb 10, 2023
57fb1e1
fix: apply samis suggestion
Feb 10, 2023
3a8dc5e
docs: update docstring
Feb 14, 2023
38f771d
fix: only display in notebook
Feb 15, 2023
8c31318
docs: update docstring
Feb 15, 2023
bbef411
chore: get poetry lock file from feat rewrite v2
Feb 15, 2023
b376ddd
docs: update docstrings
Feb 15, 2023
016760d
feat: display image from img url and img tensor
Feb 10, 2023
f55d811
fix: display from image url and from image tensor
Feb 13, 2023
0a23b52
fix: use is notebook from utils instead o f hubble
Feb 13, 2023
084921b
feat: audio from url
Feb 14, 2023
a949033
feat: display video and add pydub to pyproject toml
Feb 15, 2023
62b58ee
wip: remove non notebook
Feb 15, 2023
442dd29
fix: all except video tensor
Feb 16, 2023
b486b96
fix: mypy check for ipython display
Feb 16, 2023
f77d216
fix: mypy check for ipython display
Feb 16, 2023
ca948f9
feat: add videobytes
Feb 16, 2023
0dc606c
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-displ…
Feb 16, 2023
f2c0cff
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-displ…
Feb 16, 2023
48a85ba
chore: poetry lock
Feb 16, 2023
9df9df5
fix: clean up
Feb 16, 2023
e783c14
Merge branch 'feat-rewrite-v2' into feat-display-img-audio-vid
Feb 17, 2023
fe9d3bf
fix: mypy check
Feb 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: introduce vertices and faces doc
Signed-off-by: anna-charlotte <[email protected]>
  • Loading branch information
anna-charlotte committed Feb 15, 2023
commit 0244816584fbddf2a207e5f376d3ec0675014fbd
3 changes: 3 additions & 0 deletions docarray/documents/mesh/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from docarray.documents.mesh.mesh import Mesh3D

__all__ = ['Mesh3D']
64 changes: 29 additions & 35 deletions docarray/documents/mesh.py → docarray/documents/mesh/mesh.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any, Optional, Type, TypeVar, Union

from docarray.base_document import BaseDocument
from docarray.typing import AnyEmbedding, AnyTensor, Mesh3DUrl
from docarray.documents.mesh.vertices_and_faces import VerticesAndFaces
from docarray.typing.tensor.embedding import AnyEmbedding
from docarray.typing.url.url_3d.mesh_url import Mesh3DUrl

T = TypeVar('T', bound='Mesh3D')

Expand All @@ -17,9 +19,10 @@ class Mesh3D(BaseDocument):
tensor of shape (n_faces, 3). Each number in that tensor refers to an index of a
vertex in the tensor of vertices.

The Mesh3D Document can contain an Mesh3DUrl (`Mesh3D.url`), an AnyTensor of
vertices (`Mesh3D.vertices`), an AnyTensor of faces (`Mesh3D.faces`) and an
AnyEmbedding (`Mesh3D.embedding`).
The Mesh3D Document can contain an Mesh3DUrl (`Mesh3D.url`), a VerticesAndFaces
object containing an AnyTensor of vertices (`Mesh3D.tensors.vertices) and an
AnyTensor of faces (`Mesh3D.tensors.faces), and an AnyEmbedding
(`Mesh3D.embedding`).

EXAMPLE USAGE:

Expand All @@ -31,9 +34,9 @@ class Mesh3D(BaseDocument):

# use it directly
mesh = Mesh3D(url='https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj')
mesh.vertices, mesh.faces = mesh.url.load()
mesh.tensors = mesh.url.load()
model = MyEmbeddingModel()
mesh.embedding = model(mesh.vertices)
mesh.embedding = model(mesh.tensors.vertices)

You can extend this Document:

Expand All @@ -49,7 +52,7 @@ class MyMesh3D(Mesh3D):


mesh = MyMesh3D(url='https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj')
mesh.vertices, mesh.faces = mesh.url.load()
mesh.tensors = mesh.url.load()
model = MyEmbeddingModel()
mesh.embedding = model(mesh.vertices)
mesh.name = 'my first mesh'
Expand All @@ -72,16 +75,32 @@ class MultiModalDoc(BaseDocument):
mesh=Mesh3D(url='https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj'),
text=Text(text='hello world, how are you doing?'),
)
mmdoc.mesh.vertices, mmdoc.mesh.faces = mmdoc.mesh.url.load()
mmdoc.mesh.tensors = mmdoc.mesh.url.load()

# or
mmdoc.mesh.bytes = mmdoc.mesh.url.load_bytes()


You can display your point cloud from either its url, or its tensors:

.. code-block:: python

from docarray.documents import Mesh3D

# display from url
mesh = Mesh3D(url='https://people.sc.fsu.edu/~jburkardt/data/obj/al.obj')
mesh.url.display()

# display from tensors
mesh.tensors = mesh.url.load()
model = MyEmbeddingModel()
mesh.embedding = model(mesh.tensors.vertices)


"""

url: Optional[Mesh3DUrl]
vertices: Optional[AnyTensor]
faces: Optional[AnyTensor]
tensors: Optional[VerticesAndFaces]
embedding: Optional[AnyEmbedding]
bytes: Optional[bytes]

Expand All @@ -93,28 +112,3 @@ def validate(
if isinstance(value, str):
value = cls(url=value)
return super().validate(value)

def display(self, display_from: str = 'url') -> None:
"""
Plot mesh consisting of vertices and faces.
:param display_from: display from either url or tensors (vertices and faces).
"""

if display_from not in ['tensor', 'url']:
raise ValueError(f'Expected one of ["tensor", "url"], got "{display_from}"')

if display_from == 'url':
if self.url is None:
raise ValueError('Can\'t display mesh from url when the url is None.')
self.url.display()
else:
import trimesh
from IPython.display import display

if self.vertices is None or self.faces is None:
raise ValueError(
'Can\'t display mesh from tensor when vertices and/or faces is None'
)

mesh = trimesh.Trimesh(vertices=self.vertices, faces=self.faces)
display(mesh.show())
40 changes: 40 additions & 0 deletions docarray/documents/mesh/vertices_and_faces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Any, Optional, Type, TypeVar, Union

from docarray.base_document import BaseDocument
from docarray.typing.proto_register import _register_proto
from docarray.typing.tensor.tensor import AnyTensor

T = TypeVar('T', bound='VerticesAndFaces')


@_register_proto(proto_type_name='vertices_and_faces')
class VerticesAndFaces(BaseDocument):
""" """

vertices: Optional[AnyTensor]
faces: Optional[AnyTensor]

@classmethod
def validate(
cls: Type[T],
value: Union[str, Any],
) -> T:
if isinstance(value, str):
value = cls(url=value)
return super().validate(value)

def display(self) -> None:
"""
Plot mesh consisting of vertices and faces.
"""
import trimesh
from IPython.display import display

if self.vertices is None or self.faces is None:
raise ValueError(
'Can\'t display mesh from tensors when the vertices and/or faces '
'are None.'
)

mesh = trimesh.Trimesh(vertices=self.vertices, faces=self.faces)
display(mesh.show())
27 changes: 13 additions & 14 deletions docarray/typing/url/url_3d/mesh_url.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import NamedTuple, TypeVar
from typing import TYPE_CHECKING, TypeVar

import numpy as np
from pydantic import parse_obj_as
Expand All @@ -7,12 +7,10 @@
from docarray.typing.tensor.ndarray import NdArray
from docarray.typing.url.url_3d.url_3d import Url3D

T = TypeVar('T', bound='Mesh3DUrl')

if TYPE_CHECKING:
from docarray.documents.mesh.vertices_and_faces import VerticesAndFaces

class Mesh3DLoadResult(NamedTuple):
vertices: NdArray
faces: NdArray
T = TypeVar('T', bound='Mesh3DUrl')


@_register_proto(proto_type_name='mesh_url')
Expand All @@ -22,9 +20,9 @@ class Mesh3DUrl(Url3D):
Can be remote (web) URL, or a local file path.
"""

def load(self: T) -> Mesh3DLoadResult:
def load(self: T) -> 'VerticesAndFaces':
"""
Load the data from the url into a named tuple of two NdArrays containing
Load the data from the url into a VerticesAndFaces object containing
vertices and faces information.

EXAMPLE USAGE
Expand All @@ -34,7 +32,7 @@ def load(self: T) -> Mesh3DLoadResult:
from docarray import BaseDocument
import numpy as np

from docarray.typing import Mesh3DUrl
from docarray.typing import Mesh3DUrl, NdArray


class MyDoc(BaseDocument):
Expand All @@ -43,19 +41,20 @@ class MyDoc(BaseDocument):

doc = MyDoc(mesh_url="toydata/tetrahedron.obj")

vertices, faces = doc.mesh_url.load()
assert isinstance(vertices, np.ndarray)
assert isinstance(faces, np.ndarray)
tensors = doc.mesh_url.load()
assert isinstance(tensors.vertices, NdArray)
assert isinstance(tensors.faces, NdArray)

:return: named tuple of two NdArrays representing the mesh's vertices and faces
:return: VerticesAndFaces object containing vertices and faces information.
"""
from docarray.documents.mesh.vertices_and_faces import VerticesAndFaces

mesh = self._load_trimesh_instance(force='mesh')

vertices = parse_obj_as(NdArray, mesh.vertices.view(np.ndarray))
faces = parse_obj_as(NdArray, mesh.faces.view(np.ndarray))

return Mesh3DLoadResult(vertices=vertices, faces=faces)
return VerticesAndFaces(vertices=vertices, faces=faces)

def display(self) -> None:
"""
Expand Down
21 changes: 4 additions & 17 deletions tests/integrations/predefined_document/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from pydantic import parse_obj_as

from docarray import BaseDocument
from docarray.base_document.document import BaseDocument
from docarray.documents import Mesh3D
from tests import TOYDATA_DIR

Expand All @@ -17,10 +17,10 @@ def test_mesh(file_url):

mesh = Mesh3D(url=file_url)

mesh.vertices, mesh.faces = mesh.url.load()
mesh.tensors = mesh.url.load()

assert isinstance(mesh.vertices, np.ndarray)
assert isinstance(mesh.faces, np.ndarray)
assert isinstance(mesh.tensors.vertices, np.ndarray)
assert isinstance(mesh.tensors.faces, np.ndarray)


def test_str_init():
Expand All @@ -37,16 +37,3 @@ class MyDoc(BaseDocument):

assert doc.mesh1.url == 'http://hello.ply'
assert doc.mesh2.url == 'http://hello.ply'


def test_display_illegal_param():
mesh = Mesh3D(url='http://myurl.ply')
with pytest.raises(ValueError):
mesh.display(display_from='tensor')

mesh = Mesh3D(vertices=np.zeros((10, 3)), faces=np.ones((10, 3)))
with pytest.raises(ValueError):
mesh.display(display_from='url')

with pytest.raises(ValueError):
mesh.display(display_from='illegal')
17 changes: 8 additions & 9 deletions tests/units/typing/url/test_mesh_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from docarray.base_document.io.json import orjson_dumps
from docarray.typing import Mesh3DUrl, NdArray
from docarray.typing.url.url_3d.mesh_url import Mesh3DLoadResult
from tests import TOYDATA_DIR

MESH_FILES = {
Expand All @@ -28,14 +27,14 @@
)
def test_load(file_format, file_path):
url = parse_obj_as(Mesh3DUrl, file_path)
vertices, faces = url.load()
tensors = url.load()

assert isinstance(vertices, np.ndarray)
assert isinstance(vertices, NdArray)
assert isinstance(faces, np.ndarray)
assert isinstance(faces, NdArray)
assert vertices.shape[1] == 3
assert faces.shape[1] == 3
assert isinstance(tensors.vertices, np.ndarray)
assert isinstance(tensors.vertices, NdArray)
assert isinstance(tensors.faces, np.ndarray)
assert isinstance(tensors.faces, NdArray)
assert tensors.vertices.shape[1] == 3
assert tensors.faces.shape[1] == 3


@pytest.mark.slow
Expand All @@ -44,7 +43,7 @@ def test_load(file_format, file_path):
'file_path',
[*MESH_FILES.values(), REMOTE_OBJ_FILE],
)
@pytest.mark.parametrize('field', [f for f in Mesh3DLoadResult._fields])
@pytest.mark.parametrize('field', ['vertices', 'faces'])
def test_load_one_of_fields(file_path, field):
url = parse_obj_as(Mesh3DUrl, file_path)
field = getattr(url.load(), field)
Expand Down