Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
bebc9d4
feat: add audio url class
Dec 14, 2022
6025c2f
fix: typos
Dec 14, 2022
9a599e5
test: add tests for audio and audio url
Dec 15, 2022
04abdae
feat: add audio url and audio predefined class
Dec 15, 2022
f8d700d
Merge remote-tracking branch 'origin/feat-rewrite-v2' into feat-add-a…
Dec 21, 2022
d58f804
chore: add types-request
Dec 22, 2022
bdf8e88
feat: add audio tensors torch and ndarray
Dec 22, 2022
6572df8
fix: mypy type hints
Dec 22, 2022
9cd4baa
test: empty test file
Dec 22, 2022
b3c1948
test: add more unit and integration tests
Dec 28, 2022
7774181
fix: update audio tensors and audio url
Dec 28, 2022
af840d4
fix: remove print statements
Dec 28, 2022
797f488
docs: add documentation
Dec 28, 2022
8b48a77
refactor: rename test audio py to test audio tensor py
Dec 28, 2022
e135438
fix: typo in torch tensor py
Dec 28, 2022
14fcf6b
feat: add proto stuff to audio tensors
Dec 28, 2022
c623a13
test: add tests for proto and set tensors
Dec 28, 2022
1be8e3f
fix: set tensor to tensor int, since no inplace change
Dec 28, 2022
17786eb
refactor: rename to save to wav file
Dec 28, 2022
97355f7
docs: fix typo
Dec 28, 2022
20e2344
docs: fix docs for save tensor to wav file
Dec 28, 2022
7fc06e1
Merge branch 'feat-rewrite-v2' into feat-add-audio-v2
Dec 28, 2022
b34d783
fix: apply suggestions from code review
Dec 28, 2022
130d8ab
fix: apply suggestions from code review
Dec 29, 2022
2954351
test: fix assertions
Dec 29, 2022
61cb103
fix: move max int multiplication to abstract class
Dec 29, 2022
5943c0f
feat: add ndim method to abstract tensor class and concrete classes
Dec 29, 2022
131c5ff
fix: ndim
Dec 29, 2022
83ef649
fix: revert ndim in abstract tensor and torch tensor and ndarray
Dec 29, 2022
eecca41
fix: mypy checks
Dec 29, 2022
4762c3c
docs: add docstring to n dim
Dec 29, 2022
6948122
refactor: move n dim to abstract tensor and subclasses
Dec 29, 2022
d174087
refactor: make to protobuf abstract, change node to protobuf signature
Dec 29, 2022
3a52303
fix: remove not needed methods
Dec 29, 2022
a0be12e
fix: change remote audio file to file from github
Dec 30, 2022
9623d29
fix: raw content from remote file
Dec 30, 2022
6efdcf2
fix: path to github remote file
Dec 30, 2022
5026543
refactor: tensor field name to proto field name
Jan 2, 2023
703de43
test: remove redundant test in test audio tensor
Jan 2, 2023
83ece31
fix: load audio url to audio ndarray instead of np ndarray
Jan 2, 2023
de079e2
refactor: move n dim to computational backend
Jan 2, 2023
2ef1350
docs: update docstrings for audio tensors
Jan 3, 2023
d51d38e
feat: make dtype in audiourl load optional
Jan 3, 2023
3901cfa
Merge branch 'feat-rewrite-v2' into feat-add-audio-v2
Jan 3, 2023
a571898
test: fix document refactor and ndarray import
Jan 3, 2023
71af630
fix: fix mypy check
Jan 3, 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: mypy checks
Signed-off-by: anna-charlotte <[email protected]>
  • Loading branch information
anna-charlotte committed Dec 29, 2022
commit eecca41a00f885c9e9e76370d9e421968ca024cd
27 changes: 7 additions & 20 deletions docarray/typing/tensor/audio/abstract_audio_tensor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import wave
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, BinaryIO, TypeVar, Union
from typing import BinaryIO, TypeVar, Union

from docarray.typing.tensor.abstract_tensor import AbstractTensor
from docarray.typing.url.audio_url import MAX_INT_16

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

if TYPE_CHECKING:
from docarray.proto import NodeProto


class AbstractAudioTensor(AbstractTensor, ABC):

Expand All @@ -18,7 +14,7 @@ class AbstractAudioTensor(AbstractTensor, ABC):
@abstractmethod
def to_audio_bytes(self):
"""
Convert tensor to bytes.
Convert audio tensor to bytes.
"""
raise NotImplementedError

Expand All @@ -36,23 +32,14 @@ def save_to_wav_file(
:param sample_rate: sampling frequency
:param sample_width: sample width in bytes
"""
n_channels = 2 if self.ndim() > 1 else 1
n_channels = 2 if self.n_dim() > 1 else 1

with wave.open(file_path, 'w') as f:
f.setnchannels(n_channels)
f.setsampwidth(sample_width)
f.setframerate(sample_rate)
f.writeframes((self * MAX_INT_16).to_audio_bytes())
f.writeframes(self.to_audio_bytes())

def _to_node_protobuf(self: T) -> 'NodeProto':
"""
Convert itself into a NodeProto protobuf message. This function should
be called when the Document is nested into another Document that need to be
converted into a protobuf
:param field: field in which to store the content in the node proto
:return: the nested item protobuf message
"""
from docarray.proto import NodeProto

nd_proto = self.to_protobuf()
return NodeProto(**{self.TENSOR_FIELD_NAME: nd_proto})
@abstractmethod
def n_dim(self) -> int:
...
25 changes: 22 additions & 3 deletions docarray/typing/tensor/audio/audio_ndarray.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import TypeVar
from typing import TYPE_CHECKING, TypeVar

from docarray.typing import NdArray
from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor
from docarray.typing.url.audio_url import MAX_INT_16

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

if TYPE_CHECKING:
from docarray.proto import NodeProto


class AudioNdArray(AbstractAudioTensor, NdArray):
"""
Expand Down Expand Up @@ -52,7 +56,22 @@ class MyAudioDoc(Document):

TENSOR_FIELD_NAME = 'audio_ndarray'

def to_audio_bytes(self):
tensor = self.astype('<h')
def _to_node_protobuf(self: T, field: str = TENSOR_FIELD_NAME) -> 'NodeProto':
"""
Convert itself into a NodeProto protobuf message. This function should
be called when the Document is nested into another Document that need to be
converted into a protobuf
:param field: field in which to store the content in the node proto
:return: the nested item protobuf message
"""
from docarray.proto import NodeProto

nd_proto = self.to_protobuf()
return NodeProto(**{field: nd_proto})

def to_audio_bytes(self):
tensor = (self * MAX_INT_16).astype('<h')
return tensor.tobytes()

def n_dim(self) -> int:
return self.ndim
24 changes: 22 additions & 2 deletions docarray/typing/tensor/audio/audio_torch_tensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import TypeVar
from typing import TYPE_CHECKING, TypeVar

from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor
from docarray.typing.tensor.torch_tensor import TorchTensor, metaTorchAndNode
from docarray.typing.url.audio_url import MAX_INT_16

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

if TYPE_CHECKING:
from docarray.proto import NodeProto


class AudioTorchTensor(AbstractAudioTensor, TorchTensor, metaclass=metaTorchAndNode):
"""
Expand Down Expand Up @@ -51,8 +55,24 @@ class MyAudioDoc(Document):

TENSOR_FIELD_NAME = 'audio_torch_tensor'

def _to_node_protobuf(self: T, field: str = TENSOR_FIELD_NAME) -> 'NodeProto':
"""
Convert itself into a NodeProto protobuf message. This function should
be called when the Document is nested into another Document that need to be
converted into a protobuf
:param field: field in which to store the content in the node proto
:return: the nested item protobuf message
"""
from docarray.proto import NodeProto

nd_proto = self.to_protobuf()
return NodeProto(**{field: nd_proto})

def to_audio_bytes(self):
import torch

tensor = self.to(dtype=torch.int16)
tensor = (self * MAX_INT_16).to(dtype=torch.int16)
return tensor.cpu().detach().numpy().tobytes()

def n_dim(self) -> int:
return self.ndim