Skip to content

Commit

Permalink
Better error message, try to convert object to string in json seriali…
Browse files Browse the repository at this point in the history
…zation and check it date object is allready a datetime object
  • Loading branch information
finsberg committed Aug 4, 2022
1 parent c170e75 commit 64da4f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 14 additions & 2 deletions mps/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@

try:
import tifffile
except ImportError:
except ImportError as e:
_tifffile_msg = str(e)
has_tifffile = False
else:
_tifffile_msg = ""
has_tifffile = True


Expand Down Expand Up @@ -336,11 +338,12 @@ def load_zip(fname: os.PathLike) -> MPSData:
def load_stk(fname: os.PathLike) -> MPSData:

if not has_tifffile:

raise ImportError(
(
"tifffile is not installed. Please install "
"that if you want to load stk files. python -m"
" pip install tiffile"
" pip install tiffile\n\n"
),
)

Expand Down Expand Up @@ -420,6 +423,8 @@ def load_movie(fname: os.PathLike) -> MPSData:
def time2isoformat(s):
import datetime

if isinstance(s, datetime.datetime):
return s
date, time = s.split(" ")
return datetime.datetime.fromisoformat(f"{date[:4]}-{date[4:6]}-{date[6:]}T{time}")

Expand All @@ -444,6 +449,13 @@ def load_tiff_timestamps(f):
def load_tiff(fname):

if not has_tifffile:
if "_imagecodecs" in _tifffile_msg:
raise ImportError(
"lzma is not installed correctly. Please see "
"https://stackoverflow.com/questions/59690698/modulenotfounderror-no-module-named-lzma-when-building-python-using-pyenv-on "
"or https://github.com/pandas-dev/pandas/issues/27532",
)

raise ImportError(
(
"tifffile is not installed. Please install "
Expand Down
6 changes: 5 additions & 1 deletion mps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ def json_serial(obj):
return obj.isoformat()
elif isinstance(obj, (np.ndarray)):
return obj.tolist()
raise TypeError("Type %s not serializable" % type(obj))
else:
try:
return str(obj)
except Exception:
raise TypeError("Type %s not serializable" % type(obj))


def to_txt(data, path, header_list=None, delimiter=";", fmt="%10.6g"):
Expand Down

0 comments on commit 64da4f0

Please sign in to comment.