Skip to content

Commit

Permalink
cbor2 not always required
Browse files Browse the repository at this point in the history
  • Loading branch information
gcasadesus committed Jul 19, 2021
1 parent f68c847 commit 2b4e097
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions dislib/utils/saving.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import os
import numpy as np
import cbor2

from pycompss.runtime.management.classes import Future
from pycompss.api.api import compss_wait_on
Expand All @@ -25,6 +24,11 @@
_SkTreeWrapper,
)

try:
import cbor2
except ImportError:
cbor2 = None

# Dislib models with saving tested (model: str -> module: str)
_implemented_models = {
"KMeans": "cluster",
Expand Down Expand Up @@ -114,6 +118,8 @@ def save_model(model, filepath, overwrite=True, save_format="json"):
with open(filepath, "w") as f:
json.dump(model_metadata, f, default=_encode_helper)
elif save_format == "cbor":
if cbor2 is None:
raise ModuleNotFoundError("No module named 'cbor2'")
with open(filepath, "wb") as f:
cbor2.dump(model_metadata, f, default=_encode_helper_cbor)
else:
Expand Down Expand Up @@ -155,6 +161,8 @@ def load_model(filepath, load_format="json"):
with open(filepath, "r") as f:
model_metadata = json.load(f, object_hook=_decode_helper)
elif load_format == "cbor":
if cbor2 is None:
raise ModuleNotFoundError("No module named 'cbor2'")
with open(filepath, "rb") as f:
model_metadata = cbor2.load(f, object_hook=_decode_helper_cbor)
else:
Expand Down Expand Up @@ -188,12 +196,12 @@ def load_model(filepath, load_format="json"):


def _encode_helper_cbor(encoder, obj):
""" Special encoder wrapper for dislib using cbor2"""
""" Special encoder wrapper for dislib using cbor2."""
encoder.encode(_encode_helper(obj))


def _decode_helper_cbor(decoder, obj):
""" Special decoder wrapper for dislib using cbor2"""
""" Special decoder wrapper for dislib using cbor2."""
return _decode_helper(obj)


Expand Down

0 comments on commit 2b4e097

Please sign in to comment.