Skip to content

Commit

Permalink
[coor/datasource] in case input files have different dimensions, give…
Browse files Browse the repository at this point in the history
… a useful msg. (markovmodel#822)
  • Loading branch information
marscher committed Jun 7, 2016
1 parent f766d3e commit ecd07bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 13 additions & 2 deletions pyemma/coordinates/data/_base/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,19 @@ def filenames(self, filename_list):

# ensure all trajs have same dim
if not np.unique(ndims).size == 1:
raise ValueError("input data has different dimensions!"
" Dimensions are = %s" % zip(filename_list, ndims))
# group files by their dimensions to give user indicator
ndims = np.array(ndims)
filename_list = np.asarray(filename_list)
sort_inds = np.argsort(ndims)
import itertools, operator
res = {}
for dim, files in itertools.groupby(zip(ndims[sort_inds], filename_list[sort_inds]),
operator.itemgetter(0)):
res[dim] = list(f[1] for f in files)

raise ValueError("Input data has different dimensions ({dims})!"
" Files grouped by dimensions: {groups}".format(dims=res.keys(),
groups=res))

self._ndim = ndims[0]
self._lengths = lengths
Expand Down
13 changes: 12 additions & 1 deletion pyemma/coordinates/tests/test_numpyfilereader.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,18 @@ def test_usecols(self):
with it:
for x in it:
np.testing.assert_equal(x, self.d2[:, cols])


def test_different_shapes_value_error(self):
with tempfile.NamedTemporaryFile(delete=False, suffix='.npy') as f:
x=np.zeros((3, 42))
np.save(f.name, x)
myfiles = self.files2d[:]
myfiles.insert(1, f.name)

with self.assertRaises(ValueError) as cm:
NumPyFileReader(myfiles)
self.assertIn("different dimensions", cm.exception.args[0])
print (cm.exception.args)


if __name__ == "__main__":
Expand Down

0 comments on commit ecd07bc

Please sign in to comment.