Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: fix issue ndarray of different dtype json compatibility#1874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
JoanFM wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromfix-ndarray-compatible
Draft
Show file tree
Hide file tree
Changes fromall commits
Commits
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
2 changes: 1 addition & 1 deletiondocarray/base_doc/docarray_response.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,7 @@ class DocArrayResponse(JSONResponse):
---

```python
from docarray.documets import Text
from docarray.documents import Text
from docarray.base_doc import DocResponse


Expand Down
6 changes: 2 additions & 4 deletionsdocarray/typing/tensor/ndarray.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -31,11 +31,9 @@
from docarray.typing.tensor.tensorflow_tensor import TensorFlowTensor # noqa: F401

if TYPE_CHECKING:

from docarray.computation.numpy_backend import NumpyCompBackend
from docarray.proto import NdArrayProto


T = TypeVar('T', bound='NdArray')
ShapeT = TypeVar('ShapeT')

Expand DownExpand Up@@ -150,12 +148,12 @@ def _docarray_from_native(cls: Type[T], value: np.ndarray) -> T:
return cast(T, value.view(cls.__unparametrizedcls__))
return value.view(cls)

def _docarray_to_json_compatible(self) -> np.ndarray:
def _docarray_to_json_compatible(self):
"""
Convert `NdArray` into a json compatible object
:return: a representation of the tensor compatible with orjson
"""
return self.unwrap()
return self.unwrap().tolist()

def unwrap(self) -> np.ndarray:
"""
Expand Down
21 changes: 21 additions & 0 deletionstests/units/array/test_array_from_to_json.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -149,6 +149,27 @@ class MyDoc(BaseDoc):
assert v_after._storage == v._storage


@pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
def test_from_to_json_doclist_different_dtype(dtype):
emb = np.random.rand(128).astype(dtype)
da = DocList[MyDoc](
[
MyDoc(embedding=emb, text='hello', image=ImageDoc(url='aux.png')),
MyDoc(embedding=emb, text='hello world', image=ImageDoc()),
]
)
json_da = da.to_json()
da2 = DocList[MyDoc].from_json(json_da)
assert len(da2) == 2
assert len(da) == len(da2)
for d1, d2 in zip(da, da2):
assert d1.embedding.tolist() == d2.embedding.tolist()
assert d1.text == d2.text
assert d1.image.url == d2.image.url
assert da[1].image.url is None
assert da2[1].image.url is None


def test_union_type():
from typing import Union

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp