Skip to content

Commit cbc950a

Browse files
authored
Merge pull request #65 from MDAnalysis/more-tests-fixes
more tests/fixes
2 parents 6c47ad6 + 1d122a3 commit cbc950a

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ The rules for this file:
2626
* fix incorrect reading of ncstart and nrstart in CCP4 (#57)
2727
* fix that arithemtical operations broke inheritance (#56)
2828
* fix so that subclasses of ndarray are retained on input (#56)
29+
* fix Grid.save(filename) so that it respects the user's filename (#64)
30+
* fix grid equality test g == h (relied on old numpy behavior)
2931

3032
Changes (do not affect user)
3133

gridData/core.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,16 @@ def __init__(self, grid=None, edges=None, origin=None, delta=None,
106106
# file formats are guess from extension == lower case key
107107
self._exporters = {
108108
'DX': self._export_dx,
109-
'PICKLE': self._export_python,
109+
'PKL': self._export_python,
110+
'PICKLE': self._export_python, # compatibility
110111
'PYTHON': self._export_python, # compatibility
111112
}
112113
self._loaders = {
113114
'CCP4': self._load_cpp4,
114115
'DX': self._load_dx,
115116
'PLT': self._load_plt,
116-
'PICKLE': self._load_python,
117+
'PKL': self._export_python,
118+
'PICKLE': self._load_python, # compatibility
117119
'PYTHON': self._load_python, # compatibility
118120
}
119121

@@ -454,9 +456,6 @@ def _export_python(self, filename, **kwargs):
454456
The object is dumped as a dictionary with grid and edges: This
455457
is sufficient to recreate the grid object with __init__().
456458
"""
457-
root, ext = os.path.splitext(filename)
458-
filename = root + ".pickle"
459-
460459
data = dict(grid=self.grid, edges=self.edges, metadata=self.metadata)
461460
with open(filename, 'wb') as f:
462461
cPickle.dump(data, f, cPickle.HIGHEST_PROTOCOL)
@@ -603,8 +602,10 @@ def interpolatedF(*coordinates):
603602
def __eq__(self, other):
604603
if not isinstance(other, Grid):
605604
return False
606-
return numpy.all(other.grid == self.grid) and numpy.all(
607-
other.origin == self.origin) and other.edges == self.edges
605+
return numpy.all(other.grid == self.grid) and \
606+
numpy.all(other.origin == self.origin) and \
607+
numpy.all(numpy.all(other_edge == self_edge) for other_edge, self_edge in
608+
zip(other.edges, self.edges))
608609

609610
def __ne__(self, other):
610611
return not self.__eq__(other)

gridData/tests/test_grid.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ def test_resample_factor(self, data):
144144
assert_array_almost_equal(g.grid[::5, ::5, ::5],
145145
data['grid'].grid[::2, ::2, ::2])
146146

147+
def test_pickle(self, data, tmpdir):
148+
g = data['grid']
149+
fn = str(tmpdir.mkdir('grid').join('grid.pkl'))
150+
g.save(fn)
151+
152+
h = Grid()
153+
h.load(fn, file_format="pickle")
154+
155+
assert h == g
156+
157+
147158

148159
def test_inheritance(data):
149160
class DerivedGrid(Grid):

0 commit comments

Comments
 (0)