diff --git a/.travis.yml b/.travis.yml index 3acebc0..bd917ea 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,10 +20,10 @@ install: - conda create --yes -q -n pyenv python=$TRAVIS_PYTHON_VERSION numpy=1.9.2 scipy=0.16.0 nose=1.3.7 six sphinx=1.3 - source activate pyenv - case "$TRAVIS_PYTHON_VERSION" in 2.*) SPECIAL_PACKAGES="mock";; 3.*) SPECIAL_PACKAGES="";; esac - - pip install coveralls $SPECIAL_PACKAGES + - pip install coveralls tempdir $SPECIAL_PACKAGES script: - - nosetests -v --with-coverage --cover-package gridData --process-timeout=120 gridData + - nosetests -v --with-coverage --cover-package gridData --process-timeout=120 --processes=2 gridData - | test ${TRAVIS_PULL_REQUEST} == "false" && \ test ${TRAVIS_BRANCH} == ${GH_DOC_BRANCH} && \ diff --git a/CHANGELOG b/CHANGELOG index 3df70cc..812d5ad 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -12,7 +12,7 @@ The rules for this file: * accompany each entry with github issue/PR number (Issue #xyz) ------------------------------------------------------------------------------ -??/??/?? +??/??/?? orbeckst * 0.3.1 @@ -22,6 +22,10 @@ The rules for this file: Fixes + * fixed writing of DX files failed with "NotImplementedError: Only + regularly spaced grids allowed." (issue #21 and + MDAnalysis/mdanalysis#544) + 09/10/22 kain88-de, holocronweavers, orbeckst * 0.3.0 diff --git a/gridData/OpenDX.py b/gridData/OpenDX.py index 4f34e3a..251052c 100644 --- a/gridData/OpenDX.py +++ b/gridData/OpenDX.py @@ -139,11 +139,19 @@ def __init__(self,classid,shape=None,origin=None,delta=None,**kwargs): self.component = 'positions' self.shape = numpy.asarray(shape) # D dimensional shape self.origin = numpy.asarray(origin) # D vector - self.delta = numpy.asarray(delta) # DxD array of grid spacings self.rank = len(self.shape) # D === rank - if self.delta.shape != (self.rank,self.rank): - # check OpenDX specs for irreg spacing - raise NotImplementedError('Only regularly spaced grids allowed.') + + self.delta = numpy.asarray(delta) # DxD array of grid spacings + # gridDataFormats actually provides a simple 1D array with the deltas because only + # regular grids are used but the following is a reminder that OpenDX should be able + # to handle more complicated volume elements + if len(self.delta.shape) == 1: + self.delta = numpy.diag(delta) + if self.delta.shape != (self.rank, self.rank): + # check OpenDX specs for irreg spacing if we want to implement + # anything more complicated + raise NotImplementedError('Only regularly spaced grids allowed, ' + 'not delta={}'.format(self.delta)) def write(self,file): DXclass.write(self,file, ('counts '+self.ndformat(' %d')) % tuple(self.shape)) diff --git a/gridData/tests/test_dx.py b/gridData/tests/test_dx.py index 615dd68..7f5a3de 100644 --- a/gridData/tests/test_dx.py +++ b/gridData/tests/test_dx.py @@ -1,14 +1,32 @@ +import tempdir +import os + import numpy as np -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, assert_array_almost_equal from nose.tools import assert_equal from gridData import Grid -def test_dx(): +def test_read_dx(): g = Grid('gridData/tests/test.dx') POINTS = 8 assert_array_equal(g.grid.flat, np.ones(POINTS)) assert_equal(g.grid.size, POINTS) assert_array_equal(g.delta, np.ones(3)) assert_array_equal(g.origin, np.zeros(3)) + +def test_write_dx(counts=100, ndim=3): + h, edges = np.histogramdd(np.random.random((counts, ndim)), bins=10) + g = Grid(h, edges) + assert_equal(g.grid.sum(), counts) + + with tempdir.in_tempdir(): + outfile = "grid.dx" + g.export(outfile) + g2 = Grid(outfile) + + assert_array_almost_equal(g.grid, g2.grid, + err_msg="written grid does not match original") + assert_array_almost_equal(g.delta, g2.delta, + err_msg="deltas of written grid do not match original") diff --git a/setup.py b/setup.py index 9789b54..8a1aa57 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ packages=find_packages(exclude=[]), package_data={}, install_requires=['numpy>=1.0.3', 'six'], + tests_require=['nose', 'tempdir', 'numpy'], # extras can be difficult to install through setuptools and/or # you might prefer to use the version available through your # packaging system