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

Registered serializer for common classes of additional array-like objects #762

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions pydra/utils/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@
else:
HAVE_NUMPY = True

try:
import pandas
except ImportError:
HAVE_PANDAS = False
else:
HAVE_PANDAS = True

Check warning on line 51 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L51

Added line #L51 was not covered by tests

try:
import torch
except ImportError:
HAVE_PYTORCH = False
else:
HAVE_PYTORCH = True

Check warning on line 58 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L58

Added line #L58 was not covered by tests

try:
import tensorflow
except ImportError:
HAVE_TENSORFLOW = False
else:
HAVE_TENSORFLOW = True

Check warning on line 65 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L65

Added line #L65 was not covered by tests

__all__ = (
"hash_function",
"hash_object",
Expand Down Expand Up @@ -565,4 +586,28 @@
yield obj.tobytes(order="C")


if HAVE_PYTORCH:

@register_serializer(torch.Tensor)

Check warning on line 591 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L591

Added line #L591 was not covered by tests
def bytes_repr_torch(obj: torch.Tensor, cache: Cache) -> Iterator[bytes]:
yield f"{obj.__class__.__module__}{obj.__class__.__name__}:".encode()
yield from bytes_repr_numpy(obj.numpy(), cache)

Check warning on line 594 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L593-L594

Added lines #L593 - L594 were not covered by tests


if HAVE_TENSORFLOW:

@register_serializer(tensorflow.Tensor)

Check warning on line 599 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L599

Added line #L599 was not covered by tests
def bytes_repr_tensorflow(obj: tensorflow.Tensor, cache: Cache) -> Iterator[bytes]:
yield f"{obj.__class__.__module__}{obj.__class__.__name__}:".encode()
yield from bytes_repr_numpy(obj.numpy(), cache)

Check warning on line 602 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L601-L602

Added lines #L601 - L602 were not covered by tests


if HAVE_PANDAS:

@register_serializer(pandas.DataFrame)

Check warning on line 607 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L607

Added line #L607 was not covered by tests
def bytes_repr_pandas(obj: pandas.DataFrame, cache: Cache) -> Iterator[bytes]:
yield f"{obj.__class__.__module__}{obj.__class__.__name__}:".encode()
yield from bytes_repr_numpy(obj.to_numpy(), cache)

Check warning on line 610 in pydra/utils/hash.py

View check run for this annotation

Codecov / codecov/patch

pydra/utils/hash.py#L609-L610

Added lines #L609 - L610 were not covered by tests


NUMPY_CHUNK_LEN = 8192
Loading