Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

silx.io.fioh5: Improved handling of missing data entries #4156

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/silx/io/fioh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@
"BOOLEAN": "?",
}

def _bytestobool (val):
"""Convert bytes of a truth value to bool.

Raises ValueError if 'val' is not supported.
"""
val = val.decode().lower()
t20100 marked this conversation as resolved.
Show resolved Hide resolved
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 +269,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 +383,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
Loading