Skip to content

Commit dfeeafc

Browse files
committed
fix inconsistent error handling for Grid.load()
- gOpenMol parser now captures its TypeError and raises ValueError if the header cannot be read; this makes it consistent with the other parsers - removed XFAILs from tests that check failure behavior for incorrectly specified file_format for Grid/Grid.load() - update CHANGELOG - update docs for Grid.load()
1 parent b29c1f4 commit dfeeafc

4 files changed

Lines changed: 40 additions & 12 deletions

File tree

CHANGELOG

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ The rules for this file:
1515
-------------------------------------------------------------------------------
1616
??/??/???? orbeckst
1717

18-
* 1.1.1
18+
* 1.2.0
1919

20-
Fixes
20+
Fixes
21+
22+
* Ensure that when Grid() or Grid.load() load a datafile with the
23+
wrong format a ValueError is raised consistently. (PR #165)
2124

2225

2326
01/22/2026 IAlibay, ollyfutur, conradolandia, orbeckst, PlethoraChutney,

gridData/core.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,29 @@ def load(self, filename, file_format=None, assume_volumetric=False):
558558
559559
The :meth:`load` method calls the class's constructor method and
560560
completely resets all values, based on the loaded data.
561+
562+
Parameters
563+
----------
564+
filename : str
565+
Name of the file.
566+
567+
file_format : str or None, optional
568+
Set the file format (e.g., "DX" or "MRC"). If ``None`` then
569+
try to guess the format.
570+
571+
assume_volumetric : bool, optional
572+
Optional keyword argument that is only taken into account by
573+
the :class:`gridData.mrc.MRC` file parser.
574+
575+
Raises
576+
------
577+
ValueError
578+
The underlying file parser raises an :exc:`ValueError` if it fails
579+
to parse the file.
580+
581+
582+
.. versionchanged:: 1.2.0
583+
Ensure that underlying parsers consistently raise ValueError.
561584
"""
562585
filename = str(filename)
563586
if not os.path.exists(filename):

gridData/gOpenMol.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,17 @@ def read(self, filename):
209209
from struct import calcsize, unpack
210210
if not filename is None:
211211
self.filename = str(filename)
212-
with open(self.filename, 'rb') as plt:
213-
h = self.header = self._read_header(plt)
214-
nentries = h['nx'] * h['ny'] * h['nz']
215-
# quick and dirty... slurp it all in one go
216-
datafmt = h['bsaflag']+str(nentries)+self._data_bintype
217-
a = numpy.array(unpack(datafmt, plt.read(calcsize(datafmt))))
212+
try:
213+
with open(self.filename, 'rb') as plt:
214+
h = self.header = self._read_header(plt)
215+
nentries = h['nx'] * h['ny'] * h['nz']
216+
# quick and dirty... slurp it all in one go
217+
datafmt = h['bsaflag'] + str(nentries) + self._data_bintype
218+
a = numpy.array(unpack(datafmt, plt.read(calcsize(datafmt))))
219+
except Exception as err:
220+
raise ValueError(f"gOpenMol PLT file {filename} could not be read. "
221+
"The error was\n"
222+
f" {err.__class__.__name__}: {err}")
218223
self.header['filename'] = self.filename
219224
self.array = a.reshape(h['nz'], h['ny'], h['nx']).transpose() # unpack plt in reverse!!
220225
self.delta = self._delta()

gridData/tests/test_grid.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,9 @@ def test_load_fileformat(self, data, pklfile, fileformat):
179179
h = Grid(pklfile, file_format="pkl")
180180
assert h == data['grid']
181181

182-
# At the moment, reading the file with the wrong parser does not give
183-
# good error messages.
184-
@pytest.mark.xfail
185182
@pytest.mark.parametrize("fileformat", ("ccp4", "plt", "dx"))
186183
def test_load_wrong_fileformat(self, data, pklfile, fileformat):
187-
with pytest.raises('ValueError'):
184+
with pytest.raises(ValueError):
188185
Grid(pklfile, file_format=fileformat)
189186

190187
# just check that we can export without stupid failures; detailed

0 commit comments

Comments
 (0)