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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pickle Image #160

Merged
merged 2 commits into from
Sep 13, 2022
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
32 changes: 28 additions & 4 deletions python/lance/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pickle
import platform

import numpy as np
import pyarrow as pa
import pytest

import lance
from lance.types import Box2dType, ImageType, LabelType, Point2dType
from lance.types.box import Box2dArray
from lance.types.label import LabelArray
from lance.types import (
Box2dArray,
Box2dType,
Image,
ImageBinary,
ImageType,
ImageUri,
LabelArray,
LabelType,
Point2dType,
)

if platform.system() != "Linux":
pytest.skip(allow_module_level=True)
Expand Down Expand Up @@ -123,3 +131,19 @@ def _test_extension_rt(tmp_path, ext_type, storage_arr):
assert table["ext"].type == ext_type
assert table["ext"].to_pylist() == arr.to_pylist()
return table["ext"]


def test_pickle(tmp_path):
img = Image.create("uri")
assert isinstance(img, ImageUri)
with (tmp_path / "image").open("wb") as fh:
pickle.dump(img, fh)
with (tmp_path / "image").open("rb") as fh:
assert img == pickle.load(fh)

img = Image.create(b"bytes")
assert isinstance(img, ImageBinary)
with (tmp_path / "image").open("wb") as fh:
pickle.dump(img, fh)
with (tmp_path / "image").open("rb") as fh:
assert img == pickle.load(fh)
13 changes: 10 additions & 3 deletions python/lance/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
from pyarrow import ArrowKeyError

from lance.types.base import Point2dType
from lance.types.box import Box2dType
from lance.types.image import ImageBinaryType, ImageType, ImageUriType
from lance.types.label import LabelType
from lance.types.box import Box2dArray, Box2dType
from lance.types.image import (
Image,
ImageBinary,
ImageBinaryType,
ImageType,
ImageUri,
ImageUriType,
)
from lance.types.label import LabelArray, LabelType


def register_extension_types():
Expand Down
10 changes: 5 additions & 5 deletions python/lance/types/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ class Image(ABC):
representations
"""

def __new__(cls, data: Union[bytes, str]):
@staticmethod
def create(data: Union[bytes, str]):
if isinstance(data, bytes):
img = object.__new__(ImageBinary)
img = ImageBinary(data)
elif isinstance(data, str):
img = object.__new__(ImageUri)
img = ImageUri(data)
else:
raise TypeError(
f"{cls.__name__} can only handle bytes or str " f"but got {type(data)}"
f"Image can only handle bytes or str " f"but got {type(data)}"
)
img.__init__(data)
return img

@classmethod
Expand Down