Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
FayazRahman committed Sep 8, 2023
1 parent 7ec75ff commit 9fd6885
Showing 1 changed file with 124 additions and 0 deletions.
124 changes: 124 additions & 0 deletions deeplake/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from deeplake.core.storage import GCSProvider
from deeplake.util.exceptions import (
GroupInfoNotSupportedError,
IncompatibleHtypeError,
InvalidOperationError,
SampleAppendError,
TensorDoesNotExistError,
Expand Down Expand Up @@ -2907,3 +2908,126 @@ def test_tensor_extend_ignore(local_ds, lfpw_links, compression_args):

# Commit should work
ds.commit()


def test_change_htype(local_ds_generator):
with local_ds_generator() as ds:
ds.create_tensor("images", sample_compression="jpg")
ds.images.extend(np.random.randint(0, 256, (10, 10, 3), dtype=np.uint8))

ds.create_tensor("labels")
ds.labels.extend([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

ds.create_tensor("boxes")
ds.boxes.extend(np.random.randn(10, 5, 4))

ds.create_tensor("boxes_3d")
ds.boxes_3d.extend(np.random.randn(10, 5, 8))

ds.create_tensor("embeddings")
ds.embeddings.extend(np.random.randn(10, 1536))

mask = np.zeros((10, 100, 100, 5), dtype=bool)
mask[:, :, :512, 1] = 1
ds.create_tensor("masks")
ds.masks.extend(mask)
ds.create_tensor("image_masks", htype="image", sample_compression=None)
ds.image_masks.extend(mask)

ds.create_tensor("keypoints")
ds.keypoints.extend(np.zeros((10, 9, 5)))

ds.create_tensor("points")
ds.points.extend(np.zeros((10, 5, 3)))

ds.images.htype = "image"
ds.labels.htype = "class_label"
ds.boxes.htype = "bbox"
ds.boxes_3d.htype = "bbox.3d"
ds.embeddings.htype = "embedding"
ds.masks.htype = "binary_mask"
ds.image_masks.htype = "binary_mask"
ds.keypoints.htype = "keypoints_coco"
ds.points.htype = "point"

with local_ds_generator() as ds:
assert ds.images.htype == "image"
assert ds.labels.htype == "class_label"
assert ds.boxes.htype == "bbox"
assert ds.boxes_3d.htype == "bbox.3d"
assert ds.embeddings.htype == "embedding"
assert ds.masks.htype == "binary_mask"
assert ds.image_masks.htype == "binary_mask"
assert ds.keypoints.htype == "keypoints_coco"
assert ds.points.htype == "point"


def test_change_htype_fail(local_ds_generator):
with local_ds_generator() as ds:
ds.create_tensor("images")
ds.images.extend(np.zeros((10, 5, 5, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.images.htype = "image"

ds.create_tensor("images2")
ds.images2.extend(np.zeros((10, 5, 5, 6)))
with pytest.raises(IncompatibleHtypeError):
ds.images2.htype = "image"

ds.create_tensor("labels")
ds.labels.extend(np.ones((10, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.labels.htype = "class_label"

ds.create_tensor("boxes")
ds.boxes.extend(np.zeros((10, 5, 5, 2)))
with pytest.raises(IncompatibleHtypeError):
ds.boxes.htype = "bbox"
with pytest.raises(IncompatibleHtypeError):
ds.boxes.htype = "bbox.3d"

ds.create_tensor("boxes2")
ds.boxes2.extend(np.zeros((10, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.boxes2.htype = "bbox"
with pytest.raises(IncompatibleHtypeError):
ds.boxes2.htype = "bbox.3d"

ds.create_tensor("masks")
ds.masks.extend(np.zeros((10, 5, 5, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.masks.htype = "binary_mask"

ds.create_tensor("keypoints")
ds.keypoints.extend(np.zeros((10, 5, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.keypoints.htype = "keypoints_coco"

ds.create_tensor("keypoints2")
ds.keypoints2.extend(np.zeros((10, 10, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.keypoints2.htype = "keypoints_coco"

ds.create_tensor("points")
ds.points.extend(np.zeros((10, 5, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.points.htype = "point"

ds.create_tensor("points2")
ds.points2.extend(np.zeros((10, 5, 5)))
with pytest.raises(IncompatibleHtypeError):
ds.points2.htype = "point"

with pytest.raises(ValueError):
ds.images.htype = "link[image]"

with pytest.raises(ValueError):
ds.images.htype = "sequence[image]"

ds.create_tensor("boxes3", htype="bbox")
ds.boxes3.extend(np.zeros((10, 5, 4), dtype=np.float32))
with pytest.raises(NotImplementedError):
ds.boxes3.htype = "embedding"

with pytest.raises(NotImplementedError):
ds.images.htype = "text"

0 comments on commit 9fd6885

Please sign in to comment.