1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
from __future__ import absolute_import, division
import numpy as np
from numpy.testing import assert_equal, assert_almost_equal
import pytest
import gridData.OpenDX
from gridData import Grid
from . import datafiles
@pytest.mark.parametrize("infile", [datafiles.DX, datafiles.DXGZ])
def test_read_dx(infile):
g = Grid(infile)
POINTS = 8
ref = np.ones(POINTS)
ref[4] = 1e-6
ref[5] = -1e+6
assert_equal(g.grid.flat, ref)
assert_equal(g.grid.size, POINTS)
assert_equal(g.delta, np.ones(3))
assert_equal(g.origin, np.array([20.1, 3., -10.]))
@pytest.mark.parametrize("outfile", ["grid.dx", "grid.dx.gz"])
@pytest.mark.parametrize("nptype,dxtype", [
("float16", "float"),
("float32", "float"),
("float64", "double"),
("int64", "int"),
("int32", "int"),
("uint32", "unsigned int"),
("uint64", "unsigned int"),
("int16", "short"),
("uint16", "unsigned short"),
("int8", "signed byte"),
("uint8", "byte"),
])
def test_write_dx(tmpdir, nptype, dxtype, outfile, counts=100, ndim=3):
# conversion from numpy array to DX file
h, edges = np.histogramdd(np.random.random((counts, ndim)), bins=10)
g = Grid(h, edges)
# hack the grid to be a different dtype
g.grid = g.grid.astype(nptype)
assert_equal(g.grid.sum(), counts)
with tmpdir.as_cwd():
g.export(outfile)
g2 = Grid(outfile)
# check that dxtype was written
dx = gridData.OpenDX.field(0)
dx.read(outfile)
data = dx.components['data']
out_dxtype = data.type
assert_almost_equal(g.grid, g2.grid,
err_msg="written grid does not match original")
assert_almost_equal(
g.delta, g2.delta,
decimal=6,
err_msg="deltas of written grid do not match original")
assert_equal(out_dxtype, dxtype)
@pytest.mark.parametrize("outfile", ["grid.dx", "grid.dx.gz"])
@pytest.mark.parametrize('nptype', ("complex64", "complex128", "bool_"))
@pytest.mark.filterwarnings("ignore:array dtype.name =")
def test_write_dx_ValueError(tmpdir, nptype, outfile, counts=100, ndim=3):
h, edges = np.histogramdd(np.random.random((counts, ndim)), bins=10)
g = Grid(h, edges)
# hack the grid to be a different dtype
g.grid = g.grid.astype(nptype)
with pytest.raises(ValueError):
with tmpdir.as_cwd():
g.export(outfile)
|