-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1214 from activeloopai/fr_json
Json, List, Text
- Loading branch information
Showing
12 changed files
with
585 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import numpy as np | ||
import hub | ||
import pytest | ||
from hub.util.json import JsonValidationError | ||
from typing import Any, Optional, Union, List, Dict | ||
|
||
|
||
def test_json_basic(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("json", htype="json") | ||
items = [ | ||
{"x": [1, 2, 3], "y": [4, [5, 6]]}, | ||
{"x": [1, 2, 3], "y": [4, {"z": [0.1, 0.2, []]}]}, | ||
] | ||
with ds: | ||
for x in items: | ||
ds.json.append(x) | ||
ds.json.extend(items) | ||
assert ds.json.shape == (4, 1) | ||
for i in range(4): | ||
assert ds.json[i].data() == items[i % 2] | ||
|
||
|
||
def test_json_with_numpy(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("json", htype="json") | ||
items = [ | ||
{"x": np.array([1, 2, 3], dtype=np.float32), "y": [4, [5, 6]]}, | ||
{"x": np.array([1, 2, 3], dtype=np.uint8), "y": [4, {"z": [0.1, 0.2, []]}]}, | ||
] | ||
with ds: | ||
for x in items: | ||
ds.json.append(x) | ||
ds.json.extend(items) | ||
for i in range(4): | ||
assert ds.json[i].data()["y"] == items[i % 2]["y"] | ||
np.testing.assert_array_equal(ds.json[i].data()["x"], items[i % 2]["x"]) | ||
|
||
|
||
def test_json_with_hub_sample(memory_ds, compressed_image_paths): | ||
ds = memory_ds | ||
ds.create_tensor("json", htype="json") | ||
items = [ | ||
{ | ||
"x": [1, 2, 3], | ||
"y": [4, [5, 6]], | ||
"z": hub.read(compressed_image_paths["jpeg"][0]), | ||
}, | ||
{ | ||
"x": [1, 2, 3], | ||
"y": [4, {"z": [0.1, 0.2, []]}], | ||
"z": hub.read(compressed_image_paths["png"][0]), | ||
}, | ||
] | ||
with ds: | ||
for x in items: | ||
ds.json.append(x) | ||
ds.json.extend(items) | ||
assert ds.json.shape == (4, 1) | ||
for i in range(4): | ||
assert ds.json[i].data() == items[i % 2] | ||
|
||
|
||
def test_json_list_basic(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("list", htype="list") | ||
items = [ | ||
[{"x": [1, 2, 3], "y": [4, [5, 6]]}, [[]], [None, 0.1]], | ||
[[], [[[]]], {"a": [0.1, 1, "a", []]}], | ||
] | ||
with ds: | ||
for x in items: | ||
ds.list.append(x) | ||
ds.list.extend(items) | ||
assert ds.list.shape == (4, 3) | ||
for i in range(4): | ||
assert ds.list[i].data() == items[i % 2] | ||
for i, x in enumerate(ds.list.data()): | ||
assert x == items[i % 2] | ||
|
||
|
||
def test_list_with_numpy(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("list", htype="list") | ||
items = [ | ||
[ | ||
np.random.random((3, 4)), | ||
{"x": [1, 2, 3], "y": [4, [5, 6]]}, | ||
[[]], | ||
[None, 0.1], | ||
], | ||
[np.random.randint(0, 10, (4, 5)), [], [[[]]], {"a": [0.1, 1, "a", []]}], | ||
] | ||
with ds: | ||
for x in items: | ||
ds.list.append(x) | ||
ds.list.extend(items) | ||
assert ds.list.shape == (4, 4) | ||
for i in range(4): | ||
actual, expected = ds.list[i].data(), items[i % 2] | ||
np.testing.assert_array_equal(actual[0], expected[0]) | ||
assert actual[1:] == expected[1:] | ||
|
||
|
||
def test_list_with_hub_sample(memory_ds, compressed_image_paths): | ||
ds = memory_ds | ||
ds.create_tensor("list", htype="list") | ||
items = [ | ||
[ | ||
{ | ||
"x": [1, 2, 3], | ||
"y": [4, [5, 6, hub.read(compressed_image_paths["jpeg"][0])]], | ||
}, | ||
[[hub.read(compressed_image_paths["jpeg"][1])]], | ||
[None, 0.1], | ||
], | ||
[ | ||
[], | ||
[[[hub.read(compressed_image_paths["png"][0])]]], | ||
{"a": [0.1, 1, "a", hub.read(compressed_image_paths["png"][0])]}, | ||
], | ||
] | ||
with ds: | ||
for x in items: | ||
ds.list.append(x) | ||
ds.list.extend(items) | ||
assert ds.list.shape == (4, 3) | ||
for i in range(4): | ||
assert ds.list[i].data() == items[i % 2] | ||
|
||
|
||
def test_json_with_schema(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("json", htype="json", dtype=List[Dict[str, int]]) | ||
ds.json.append([{"x": 1, "y": 2}]) | ||
with pytest.raises(JsonValidationError): | ||
ds.json.append({"x": 1, "y": 2}) | ||
with pytest.raises(JsonValidationError): | ||
ds.json.append([{"x": 1, "y": "2"}]) | ||
|
||
assert ds.json.numpy()[0, 0] == [{"x": 1, "y": 2}] | ||
|
||
ds.create_tensor("json2", htype="json", dtype=Optional[List[Dict[str, int]]]) | ||
items = [ | ||
[{"x": 1, "y": 2}], | ||
None, | ||
[{"x": 2, "y": 3}], | ||
None, | ||
] | ||
ds.json2.extend(items) | ||
for i in range(len(items)): | ||
assert ds.json2[i].data() == ds.json2.data()[i] == items[i] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import hub | ||
import pytest | ||
|
||
|
||
def test_text(memory_ds): | ||
ds = memory_ds | ||
ds.create_tensor("text", htype="text") | ||
items = ["abcd", "efgh", "0123456"] | ||
with ds: | ||
for x in items: | ||
ds.text.append(x) | ||
ds.text.extend(items) | ||
assert ds.text.shape == (6, 1) | ||
for i in range(6): | ||
assert ds.text[i].numpy()[0] == items[i % 3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.