Skip to content

Commit

Permalink
Merge pull request #4156 from tifuchs/master
Browse files Browse the repository at this point in the history
CI is fixed, I'll merge as it is and add a small test in a different PR
  • Loading branch information
t20100 authored Oct 7, 2024
2 parents 68c65f1 + e4f8546 commit 45bce37
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/silx/io/fioh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@
"BOOLEAN": "?",
}

def _bytestobool (val):
"""Convert bytes of a truth value to bool.
Raises ValueError if 'val' is not supported.
"""
if isinstance(val, bytes):
val = val.decode()
val.lower()
if val in ('y', 'yes', 't', 'true', 'on', '1'):
return True
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
return False
else:
raise ValueError("Invalid truth value %r" % val)


def is_fiofile(filename):
"""Test if a file is a FIO file, by checking if three consecutive lines
Expand Down Expand Up @@ -256,11 +271,22 @@ def __init__(self, filepath):
"Invalid fio file: Found no data "
"after %s lines" % ABORTLINENO
)
np_datatype = \
numpy.dtype([(n, t) for (n,t) in zip(self.names, self.dtypes)])

converter = {}
for i, t in enumerate(self.dtypes):
if t == dtypeConverter['BOOLEAN']:
converter[i] = _bytestobool

self.data = numpy.loadtxt(
self.data = numpy.genfromtxt(
fiof,
dtype={"names": tuple(self.names), "formats": tuple(self.dtypes)},
dtype=np_datatype,
comments="!",
invalid_raise=True,
names=None,
deletechars='',
converters=converter
)

# ToDo: read only last line of file,
Expand Down Expand Up @@ -359,7 +385,7 @@ def __init__(self, filename, order=1):
try:
fiof = FioFile(filename) # reads complete file
except Exception as e:
raise IOError("FIO file %s cannot be read.") from e
raise IOError("FIO file %s cannot be read." % filename) from e

attrs = {
"NX_class": to_h5py_utf8("NXroot"),
Expand Down

0 comments on commit 45bce37

Please sign in to comment.