From 64da4f08531c9c00aaf2a510976da0c0acdfdc8b Mon Sep 17 00:00:00 2001 From: Henrik Finsberg Date: Thu, 4 Aug 2022 13:56:44 +0200 Subject: [PATCH] Better error message, try to convert object to string in json serialization and check it date object is allready a datetime object --- mps/load.py | 16 ++++++++++++++-- mps/utils.py | 6 +++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/mps/load.py b/mps/load.py index de53522..25715d2 100644 --- a/mps/load.py +++ b/mps/load.py @@ -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 @@ -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" ), ) @@ -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}") @@ -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 " diff --git a/mps/utils.py b/mps/utils.py index 8f16c52..81d9cc0 100644 --- a/mps/utils.py +++ b/mps/utils.py @@ -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"):