Skip to content

Commit

Permalink
WIP: fix failure to write OpenDX file (Issue #21)
Browse files Browse the repository at this point in the history
- change delta to DxD array if it is 1D
- added additional test (currently failing)
- use tempdir (added to travis and tests_require)
  • Loading branch information
orbeckst committed Dec 1, 2015
1 parent c6a7ddb commit 4a120eb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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} && \
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The rules for this file:
* accompany each entry with github issue/PR number (Issue #xyz)

------------------------------------------------------------------------------
??/??/??
??/??/?? orbeckst

* 0.3.1

Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions gridData/OpenDX.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 20 additions & 2 deletions gridData/tests/test_dx.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4a120eb

Please sign in to comment.