示例#1
0
def test_hdf5writer_dict():
    f = Frame()
    f.n = {1: 1}
    f.writer = writers.hdf5writer()
    with pytest.raises(NotImplementedError):
        f.writeoutput(0)
    shutil.rmtree(f.writer.datadir)
示例#2
0
def test_namespacewriter_simple():
    f = Frame()
    f.addfield("Y", 1.)
    f.addgroup("A")
    f.A.addfield("B", 0.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 1.

    f.x.updater = dx
    f.x.snapshots = [1.]
    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_1_euler, f.Y)]
    f.writer = writers.namespacewriter()
    f.writer.dumping = True
    f.run()
    Y = f.writer.read.sequence("Y")
    assert np.all(Y == [1., 0.])
    x = f.writer.read.sequence("x")
    assert np.all(x == [0., 1.])
    data = f.writer.read.all()
    assert np.all(data.Y == [1., 0.])
    assert np.all(data.x == [0., 1.])
    f.writer.reset()
示例#3
0
def test_namespacewriter_read_empty():
    f = Frame()
    f.addfield("Y", 1.)
    f.writer = writers.namespacewriter()
    with pytest.raises(RuntimeError):
        Y = f.writer.read.sequence("Y")
    with pytest.raises(RuntimeError):
        data = f.writer.read.all()
示例#4
0
def test_hdf5writer_skip():
    f = Frame()
    f.writer = writers.hdf5writer()
    f.addfield("x", 0., save=False)
    f.writeoutput(0)
    with pytest.raises(KeyError):
        x = f.writer.read.sequence("x")
    shutil.rmtree(f.writer.datadir)
示例#5
0
def test_frame_attributes():
    f = Frame()
    with pytest.raises(TypeError):
        f.integrator = 1
    with pytest.raises(TypeError):
        f.progressbar = 1
    f.progressbar = Progressbar()
    with pytest.raises(TypeError):
        f.verbosity = "_"
    with pytest.raises(TypeError):
        f.writer = "_"
示例#6
0
def test_namespacewriter_read_out_of_bounds():
    f = Frame()
    f.addfield("Y", 1.)
    f.writer = writers.namespacewriter()
    f.writer.verbosity = 0
    f.writer.dumping = False
    f.writer.write(f)
    with pytest.raises(RuntimeError):
        f.writer.read.output(1)
    data0000 = f.writer.read.output(0)
    assert data0000.Y == 1.
    f.writer.reset()
示例#7
0
def test_namespacewriter_read_sequence():
    f = Frame()
    f.addfield("Y", [1., 0])
    f.addfield("x", 0, save=False)
    f.writer = writers.namespacewriter()
    f.writer.write(f)
    with pytest.raises(TypeError):
        f.writer.read.sequence(1)
    with pytest.raises(RuntimeError):
        f.writer.read.sequence("x")
    Y = f.writer.read.sequence("Y")
    assert np.all(Y == [1., 0.])
    f.writer.reset()
示例#8
0
def test_hdf5writer_list():
    f = Frame()
    f.n = [1, 1]
    f.writer = writers.hdf5writer()
    f.writeoutput(0)
    f.writeoutput(1)
    data0000 = f.writer.read.output(0)
    assert np.all(data0000.n == [1, 1])
    n = f.writer.read.sequence("n")
    assert np.all(n == [[1, 1], [1, 1]])
    data = f.writer.read.all()
    assert np.all(data.n == [[1, 1], [1, 1]])
    shutil.rmtree(f.writer.datadir)
示例#9
0
def test_hdf5writer_single_value_array():
    f = Frame()
    f.n = np.array(1)
    f.writer = writers.hdf5writer()
    f.writeoutput(0)
    f.writeoutput(1)
    data0000 = f.writer.read.output(0)
    assert data0000.n == 1
    n = f.writer.read.sequence("n")
    assert np.all(n == [1, 1])
    data = f.writer.read.all()
    assert np.all(data.n == [1, 1])
    shutil.rmtree(f.writer.datadir)
示例#10
0
def test_hdf5writer_none():
    f = Frame()
    f.n = None
    f.writer = writers.hdf5writer()
    with pytest.raises(ValueError):
        f.writeoutput(0)
    f.n = [1, None]
    with pytest.raises(ValueError):
        f.writeoutput(1)
    f.n = (1, None)
    with pytest.raises(ValueError):
        f.writeoutput(2)
    shutil.rmtree(f.writer.datadir)
示例#11
0
def test_write():
    f = Frame()
    f.writer = writers.hdf5writer
    f.writer.verbosity = 0
    filename = os.path.join(f.writer.datadir, "test.out")
    f.writer.write(f, 0, True, filename=filename)
    assert os.path.isfile(filename)
    with pytest.raises(RuntimeError):
        f.writer.write(f, 0, False, filename=filename)
    os.remove(filename)
    f.writer.overwrite = True
    f.writer.dumping = False
    f.writer.verbosity = 1
    f.writer.write(f, 0, False, filename=filename)
    assert os.path.isfile(filename)
    shutil.rmtree(f.writer.datadir)
示例#12
0
def test_simple_read_files():
    f = Frame()
    f.addgroup("A")
    f.A.addfield("B", [0., 0.])
    f.addfield("Y", 1.)

    def dYdx(f, x, Y):
        return -Y

    f.Y.differentiator = dYdx
    f.addintegrationvariable("x", 0.)

    def dx(f):
        return 1.

    f.x.updater = dx
    f.x.snapshots = [1.]
    f.integrator = Integrator(f.x)
    f.integrator.instructions = [Instruction(schemes.expl_1_euler, f.Y)]
    f.writer = writers.hdf5writer()
    f.run()
    x = f.writer.read.sequence("x")
    assert np.all(x == [0., 1.])
    Y = f.writer.read.sequence("Y")
    assert np.all(Y == [1., 0.])
    B = f.writer.read.sequence("A.B")
    assert np.all(B == [0., 0.])
    with pytest.raises(TypeError):
        f.writer.read.sequence(1)
    data = f.writer.read.all()
    assert np.all(data.x == [0., 1.])
    assert np.all(data.Y == [1., 0.])
    assert np.all(data.A.B == [0., 0.])
    data0000 = f.writer.read.output(0)
    assert np.all(data0000.x == 0.)
    assert np.all(data0000.Y == 1.)
    assert np.all(data0000.A.B == 0.)
    with pytest.raises(RuntimeError):
        f.writer.read.output(2)
    shutil.rmtree(f.writer.datadir)
    with pytest.raises(RuntimeError):
        f.writer.datadir = "temp"
        f.writer.read.all()
    with pytest.raises(RuntimeError):
        f.writer.read.sequence("x")
    f.writer.datadir = "data"
示例#13
0
def test_group_repr_str():
    f = Frame()
    assert isinstance(repr(f), str)
    assert isinstance(str(f), str)
    f.addintegrationvariable("x", 0)
    f.addfield("Y", 1.)
    f.addfield("abcdefghijklm", 0.)
    f.addgroup("A")
    f.addgroup("BCDEFGHIJKLMN")
    f.C = None
    f.abcdef1234567 = None
    f.A.addfield("k", 0.)
    assert isinstance(repr(f), str)
    assert isinstance(str(f), str)
    assert isinstance(repr(f.A), str)
    assert isinstance(str(f.A), str)
    f.integrator = Integrator(f.x)
    f.writer = writers.namespacewriter()
    assert isinstance(repr(f), str)
    assert isinstance(str(f), str)
示例#14
0
def test_hdf5writer_strings():
    string = "test"
    # When read from HDF5 the string will be a byte literal
    string_cmpr = string.encode()
    f = Frame()
    f.addfield("s", string)
    f.t = string
    f.writer = writers.hdf5writer()
    f.writeoutput(0)
    f.writeoutput(1)
    data0000 = f.writer.read.output(0)
    assert data0000.s[0] == string_cmpr
    assert data0000.t == string_cmpr
    s = f.writer.read.sequence("s")
    assert np.all(s == [string_cmpr, string_cmpr])
    t = f.writer.read.sequence("t")
    assert np.all(t == [string_cmpr, string_cmpr])
    data = f.writer.read.all()
    assert np.all(data.s == [string_cmpr, string_cmpr])
    assert np.all(data.s == [string_cmpr, string_cmpr])
    shutil.rmtree(f.writer.datadir)