Skip to content
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 our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias "jpg" to "jpeg" #1073

Merged
merged 4 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion hub/api/tests/test_api_with_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _populate_compressed_samples(tensor: Tensor, cat_path, flower_path, count=1)

for _ in range(count):
tensor.append(hub.read(cat_path))
original_compressions.append("jpeg")
original_compressions.append("jpg")

tensor.append(hub.read(flower_path))
original_compressions.append("png")
Expand Down Expand Up @@ -120,6 +120,14 @@ def test_jpeg_bad_shapes(memory_ds: Dataset, bad_shape):
tensor.append(np.ones(bad_shape, dtype="uint8"))


def test_compression_aliases(memory_ds: Dataset):
tensor = memory_ds.create_tensor("jpeg_tensor", sample_compression="jpeg")
assert tensor.meta.sample_compression == "jpeg"

tensor = memory_ds.create_tensor("jpg_tensor", sample_compression="jpg")
assert tensor.meta.sample_compression == "jpeg"


@pytest.mark.xfail(raises=UnsupportedCompressionError, strict=True)
def test_unsupported_compression(memory_ds: Dataset):
memory_ds.create_tensor(TENSOR_KEY, sample_compression="bad_compression")
Expand Down
3 changes: 2 additions & 1 deletion hub/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

DEFAULT_HTYPE = "generic"

SUPPORTED_COMPRESSIONS = ["png", "jpeg", None]
SUPPORTED_COMPRESSIONS = ["png", "jpg", "jpeg", None]
COMPRESSION_ALIASES = {"jpg": "jpeg"}

# used for requiring the user to specify a value for htype properties. notates that the htype property has no default.
REQUIRE_USER_SPECIFICATION = "require_user_specification"
Expand Down
6 changes: 6 additions & 0 deletions hub/core/meta/tensor_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from hub.constants import (
REQUIRE_USER_SPECIFICATION,
SUPPORTED_COMPRESSIONS,
COMPRESSION_ALIASES,
UNSPECIFIED,
)
from hub.htypes import HTYPE_CONFIGURATIONS
Expand Down Expand Up @@ -251,8 +252,13 @@ def _format_values(htype_overwrite: dict):
if htype_overwrite["dtype"] is not None:
htype_overwrite["dtype"] = np.dtype(htype_overwrite["dtype"]).name

for key, value in COMPRESSION_ALIASES.items():
if htype_overwrite.get("sample_compression") == key:
htype_overwrite["sample_compression"] = value


def _validate_htype_exists(htype: str):
"""Raises errors if given an unrecognized htype."""
if htype not in HTYPE_CONFIGURATIONS:
raise TensorMetaInvalidHtype(htype, list(HTYPE_CONFIGURATIONS.keys()))

Expand Down