Skip to content

Commit ffb33ad

Browse files
committed
make the file reader more robust for weird/malformed files
1 parent 48a675a commit ffb33ad

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/easyreflectometry/data/measurement.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def _load_txt(fname: Union[TextIO, str]) -> sc.DataGroup:
7575
:param fname: The path for the file to be read.
7676
"""
7777
# fname can have either a space or a comma as delimiter
78-
# Find out the delimiter first
78+
# Determine the delimiter used in the file
7979
delimiter = None
8080
with open(fname, 'r') as f:
8181
# find first non-comment and non-empty line
@@ -87,10 +87,29 @@ def _load_txt(fname: Union[TextIO, str]) -> sc.DataGroup:
8787
delimiter = ','
8888

8989
try:
90-
x, y, e, xe = np.loadtxt(fname, delimiter=delimiter, comments='#', unpack=True)
91-
except ValueError:
92-
x, y, e = np.loadtxt(fname, delimiter=delimiter, comments='#', unpack=True)
93-
xe = np.zeros_like(x)
90+
# First load only the data to check column count
91+
data = np.loadtxt(fname, delimiter=delimiter, comments='#')
92+
if data.ndim == 1:
93+
# Handle single row case
94+
num_columns = len(data)
95+
else:
96+
num_columns = data.shape[1]
97+
98+
# Verify minimum column requirement
99+
if num_columns < 3:
100+
raise ValueError(f"File must contain at least 3 columns (found {num_columns})")
101+
102+
# Now unpack the data based on column count
103+
if num_columns >= 4:
104+
x, y, e, xe = np.loadtxt(fname, delimiter=delimiter, comments='#', unpack=True)
105+
else: # 3 columns
106+
x, y, e = np.loadtxt(fname, delimiter=delimiter, comments='#', unpack=True)
107+
xe = np.zeros_like(x)
108+
109+
except (ValueError, IOError) as error:
110+
# Re-raise with more descriptive message
111+
raise ValueError(f"Failed to load data from {fname}: {str(error)}") from error
112+
94113
data = {'R_0': sc.array(dims=['Qz_0'], values=y, variances=np.square(e))}
95114
coords = {
96115
data['R_0'].dims[0]: sc.array(

0 commit comments

Comments
 (0)