Skip to content

Commit

Permalink
Update helpers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kujaku11 committed May 26, 2022
1 parent 686eae3 commit baa7753
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions mth5/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ def validate_compression(compression, level):
msg = "compression type must be a string, not {0}".format(type(compression))
logger.error(msg)
raise TypeError(msg)

if not compression in COMPRESSION:
msg = (
f"Compression type {compression} not supported. "
+ f"Supported options are {COMPRESSION}"
)
logger.error(msg)
raise ValueError(msg)

if compression == "lzf":
level = COMPRESSION_LEVELS["lzf"][0]
elif compression == " gzip":
Expand All @@ -83,15 +81,13 @@ def validate_compression(compression, level):
) + " Options are {0}".format(COMPRESSION_LEVELS["szip"])
logger.error(msg)
raise TypeError(msg)

if not level in COMPRESSION_LEVELS[compression]:
msg = (
f"compression level {level} not supported for {compression}."
+ " Options are {0}".format(COMPRESSION_LEVELS[compression])
)
logger.error(msg)
raise ValueError(msg)

return compression, level


Expand Down Expand Up @@ -173,7 +169,6 @@ def to_numpy_type(value):
# For now turn references into a generic string
if isinstance(value, h5py.h5r.Reference):
value = str(value)

if isinstance(
value,
(
Expand All @@ -190,13 +185,11 @@ def to_numpy_type(value):
),
):
return value

if isinstance(value, Iterable):
if np.any([type(x) in [str, bytes, np.str_] for x in value]):
return np.array(value, dtype="S")
else:
return np.array(value)

else:
raise TypeError("Type {0} not understood".format(type(value)))

Expand All @@ -211,9 +204,10 @@ def validate_name(name):
:rtype: TYPE
"""

return name.replace(" ", "_").replace("/", "_")


def from_numpy_type(value):
"""
Need to make the attributes friendly with Numpy and HDF5.
Expand All @@ -232,7 +226,6 @@ def from_numpy_type(value):
# For now turn references into a generic string
if isinstance(value, h5py.h5r.Reference):
value = str(value)

if isinstance(
value,
(
Expand All @@ -249,13 +242,11 @@ def from_numpy_type(value):
),
):
return value

if isinstance(value, Iterable):
if np.any([type(x) in [bytes, np.bytes_] for x in value]):
return np.array(value, dtype="U").tolist()
else:
return np.array(value).tolist()

else:
raise TypeError("Type {0} not understood".format(type(value)))

Expand Down Expand Up @@ -285,4 +276,4 @@ def validate_name(name, pattern=None):
"""
if name is None:
return "unknown"
return name.replace(" ", "_")
return name.replace(" ", "_").replace(",", "")

0 comments on commit baa7753

Please sign in to comment.