How to use gsd - 10 common examples

To help you get started, we’ve selected a few gsd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github glotzerlab / gsd / tests / test_fl.py View on Github external
schema_long = 'ijklmnop' * 100
    chunk_long = '12345678' * 100

    with gsd.fl.open(name=tmp_path / 'test_namelen.gsd',
                     mode=open_mode.write,
                     application=app_long,
                     schema=schema_long,
                     schema_version=[1, 2]) as f:
        assert f.application == app_long[0:63]
        assert f.schema == schema_long[0:63]

        data = numpy.array([1, 2, 3, 4, 5, 10012], dtype=numpy.int64)
        f.write_chunk(name=chunk_long, data=data)
        f.end_frame()

    with gsd.fl.open(name=tmp_path / 'test_namelen.gsd',
                     mode=open_mode.read,
                     application=app_long,
                     schema=schema_long,
                     schema_version=[1, 2]) as f:
        data_read = f.read_chunk(0, name=chunk_long)
        numpy.testing.assert_array_equal(data, data_read)

    # test again with pygsd
    with gsd.pygsd.GSDFile(
            file=open(str(tmp_path / 'test_namelen.gsd'), mode='rb')) as f:
        data_read = f.read_chunk(0, name=chunk_long)
        numpy.testing.assert_array_equal(data, data_read)
github glotzerlab / gsd / tests / test_fl.py View on Github external
with gsd.fl.open(name=tmp_path / 'test_gsd_v1.gsd',
                     mode='rb+',
                     application='test_gsd_v1',
                     schema='none',
                     schema_version=[1, 2]) as f:

        assert f.gsd_version == (1, 0)

        f.upgrade()

        # check that we can read the file contents after the upgrade in memory
        check_v1_file_read(f)

    # and the same tests again after closing and opening the file
    with gsd.fl.open(name=tmp_path / 'test_gsd_v1.gsd',
                     mode=open_mode.read,
                     application='test_gsd_v1',
                     schema='none',
                     schema_version=[1, 2]) as f:

        assert f.gsd_version == (2, 0)

        check_v1_file_read(f)

    with gsd.pygsd.GSDFile(
            file=open(str(tmp_path / 'test_gsd_v1.gsd'), mode='rb')) as f:

        assert f.gsd_version == (2, 0)

        check_v1_file_read(f)
github glotzerlab / gsd / tests / test_fl.py View on Github external
def test_zero_size(tmp_path, open_mode):
    """Test that zero-size data chunks are allowed."""
    data = numpy.array([], dtype=numpy.float32)

    with gsd.fl.open(name=tmp_path / 'test_zero.gsd',
                     mode=open_mode.write,
                     application='test_zero',
                     schema='none',
                     schema_version=[1, 2]) as f:

        f.write_chunk(name='data', data=data)
        f.end_frame()

    with gsd.fl.open(name=tmp_path / 'test_zero.gsd',
                     mode=open_mode.read,
                     application='test_zero',
                     schema='none',
                     schema_version=[1, 2]) as f:
        assert f.nframes == 1
        data_read = f.read_chunk(frame=0, name='data')
        assert data_read.shape == (0,)
github glotzerlab / gsd / tests / test_fl.py View on Github external
def test_namelen(tmp_path, open_mode):
    """Test that long names are truncated as documented."""
    app_long = 'abcdefga' * 100
    schema_long = 'ijklmnop' * 100
    chunk_long = '12345678' * 100

    with gsd.fl.open(name=tmp_path / 'test_namelen.gsd',
                     mode=open_mode.write,
                     application=app_long,
                     schema=schema_long,
                     schema_version=[1, 2]) as f:
        assert f.application == app_long[0:63]
        assert f.schema == schema_long[0:63]

        data = numpy.array([1, 2, 3, 4, 5, 10012], dtype=numpy.int64)
        f.write_chunk(name=chunk_long, data=data)
        f.end_frame()

    with gsd.fl.open(name=tmp_path / 'test_namelen.gsd',
                     mode=open_mode.read,
                     application=app_long,
                     schema=schema_long,
                     schema_version=[1, 2]) as f:
github glotzerlab / gsd / tests / test_fl.py View on Github external
application='test_open',
                     schema='none',
                     schema_version=[1, 2]) as f:
        f.write_chunk(name='chunk1', data=data)
        f.end_frame()
        f.read_chunk(0, name='chunk1')

    with gsd.fl.open(name=tmp_path / 'test.gsd',
                     mode='ab',
                     application='test_open',
                     schema='none',
                     schema_version=[1, 2]) as f:
        f.write_chunk(name='chunk1', data=data)
        f.end_frame()

    with gsd.fl.open(name=tmp_path / 'test.gsd',
                     mode='rb',
                     application='test_open',
                     schema='none',
                     schema_version=[1, 2]) as f:
        f.read_chunk(0, name='chunk1')
        f.read_chunk(1, name='chunk1')

    with gsd.fl.open(name=tmp_path / 'test.gsd',
                     mode='rb+',
                     application='test_open',
                     schema='none',
                     schema_version=[1, 2]) as f:
        f.write_chunk(name='chunk1', data=data)
        f.end_frame()
        f.read_chunk(0, name='chunk1')
        f.read_chunk(1, name='chunk1')
github glotzerlab / gsd / tests / test_fl.py View on Github external
nframes = 1024

    with gsd.fl.open(name=tmp_path / 'test_append.gsd',
                     mode='ab',
                     application='test_append',
                     schema='none',
                     schema_version=[1, 2]) as f:
        assert f.mode == 'ab'
        for i in range(nframes):
            data[0] = i
            f.write_chunk(name='data1', data=data)
            data[0] = i * 10
            f.write_chunk(name='data10', data=data)
            f.end_frame()

    with gsd.fl.open(name=tmp_path / 'test_append.gsd',
                     mode=open_mode.read,
                     application='test_append',
                     schema='none',
                     schema_version=[1, 2]) as f:
        assert f.nframes == nframes
        for i in range(nframes):
            data1 = f.read_chunk(frame=i, name='data1')
            data10 = f.read_chunk(frame=i, name='data10')
            assert data1[0] == i
            assert data10[0] == i * 10

    # test again with pygsd
    with gsd.pygsd.GSDFile(
            file=open(str(tmp_path
                          / 'test_append.gsd'), mode=open_mode.read)) as f:
        assert f.nframes == nframes
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_create(tmp_path):
    """Test that gsd files can be created."""
    with gsd.hoomd.open(name=tmp_path / "test_create.gsd", mode='wb') as hf:
        assert hf.file.schema == 'hoomd'
        assert hf.file.schema_version >= (1, 0)
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
snap0 = gsd.hoomd.Snapshot()

    snap0.state['hpmc/sphere/radius'] = [2.0]
    snap0.state['hpmc/sphere/orientable'] = [1]

    snap1 = gsd.hoomd.Snapshot()

    snap1.state['hpmc/convex_polyhedron/N'] = [3]
    snap1.state['hpmc/convex_polyhedron/vertices'] = [[-1, -1, -1], [0, 1, 1],
                                                      [1, 0, 0]]

    with gsd.hoomd.open(name=tmp_path / "test_state.gsd",
                        mode=open_mode.write) as hf:
        hf.extend([snap0, snap1])

    with gsd.hoomd.open(name=tmp_path / "test_state.gsd",
                        mode=open_mode.read) as hf:
        assert len(hf) == 2
        s = hf.read_frame(0)

        numpy.testing.assert_array_equal(s.state['hpmc/sphere/radius'],
                                         snap0.state['hpmc/sphere/radius'])
        numpy.testing.assert_array_equal(s.state['hpmc/sphere/orientable'],
                                         snap0.state['hpmc/sphere/orientable'])

        s = hf.read_frame(1)

        numpy.testing.assert_array_equal(
            s.state['hpmc/convex_polyhedron/N'],
            snap1.state['hpmc/convex_polyhedron/N'])
        numpy.testing.assert_array_equal(
            s.state['hpmc/convex_polyhedron/vertices'],
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_view_slicing_and_iteration(tmp_path, open_mode):
    """Test that trajectories can be sliced."""
    with gsd.hoomd.open(name=tmp_path / "test_slicing.gsd",
                        mode=open_mode.write) as hf:
        hf.extend((create_frame(i) for i in range(40)))

    with gsd.hoomd.open(name=tmp_path / "test_slicing.gsd",
                        mode=open_mode.read) as hf:
        view = hf[::2]

        # Test len()-function on trajectory and sliced view.
        assert len(view) == 20
        assert len(view[:10]) == 10
        assert len(view[::2]) == 10

        # Test len()-function with explicit iterator.
        assert len(iter(view)) == len(view)
        assert len(iter(view[:10])) == len(view[:10])

        # Test iteration with implicit iterator.
        # All iterations are run twice to check for issues
        # with iterator exhaustion.
        assert len(list(view)) == len(view)
github glotzerlab / gsd / tests / test_hoomd.py View on Github external
def test_defaults(tmp_path, open_mode):
    """Test that the property defaults are properly set."""
    snap = gsd.hoomd.Snapshot()
    snap.particles.N = 2
    snap.bonds.N = 3
    snap.angles.N = 4
    snap.dihedrals.N = 5
    snap.impropers.N = 6
    snap.constraints.N = 4
    snap.pairs.N = 7

    with gsd.hoomd.open(name=tmp_path / "test_defaults.gsd",
                        mode=open_mode.write) as hf:
        hf.append(snap)

    with gsd.hoomd.open(name=tmp_path / "test_defaults.gsd",
                        mode=open_mode.read) as hf:
        s = hf.read_frame(0)

        assert s.configuration.step == 0
        assert s.configuration.dimensions == 3
        numpy.testing.assert_array_equal(
            s.configuration.box,
            numpy.array([1, 1, 1, 0, 0, 0], dtype=numpy.float32))
        assert s.particles.N == 2
        assert s.particles.types == ['A']
        assert s.particles.type_shapes == [{}]
        numpy.testing.assert_array_equal(