File: test_hdf5.py

package info (click to toggle)
python-pyqtgraph 0.13.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,072 kB
  • sloc: python: 54,043; makefile: 127; ansic: 40; sh: 2
file content (73 lines) | stat: -rw-r--r-- 2,091 bytes parent folder | download | duplicates (2)
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
import numpy as np
import pytest
from numpy.testing import assert_equal

import pyqtgraph as pg
from pyqtgraph.exporters import HDF5Exporter

h5py = pytest.importorskip("h5py")


@pytest.fixture
def tmp_h5(tmp_path):
    yield tmp_path / "data.h5"


@pytest.mark.parametrize("combine", [False, True])
def test_HDF5Exporter(tmp_h5, combine):
    # Basic test of functionality: multiple curves with shared x array. Tests
    # both options for stacking the data (columnMode).
    x = np.linspace(0, 1, 100)
    y1 = np.sin(x)
    y2 = np.cos(x)

    plt = pg.PlotWidget()
    plt.show()
    plt.plot(x=x, y=y1)
    plt.plot(x=x, y=y2)

    ex = HDF5Exporter(plt.plotItem)

    if combine:
        ex.parameters()['columnMode'] = '(x,y,y,y) for all plots'

    ex.export(fileName=tmp_h5)

    with h5py.File(tmp_h5, 'r') as f:
        # should be a single dataset with the name of the exporter
        dset = f[ex.parameters()['Name']]
        assert isinstance(dset, h5py.Dataset)

        if combine:
            assert_equal(np.array([x, y1, y2]), dset)
        else:
            assert_equal(np.array([x, y1, x, y2]), dset)


def test_HDF5Exporter_unequal_lengths(tmp_h5):
    # Test export with multiple curves of different size. The exporter should
    # detect this and create multiple hdf5 datasets under a group.
    x1 = np.linspace(0, 1, 10)
    y1 = np.sin(x1)
    x2 = np.linspace(0, 1, 100)
    y2 = np.cos(x2)

    plt = pg.PlotWidget()
    plt.show()
    plt.plot(x=x1, y=y1, name='plot0')
    plt.plot(x=x2, y=y2)

    ex = HDF5Exporter(plt.plotItem)
    ex.export(fileName=tmp_h5)

    with h5py.File(tmp_h5, 'r') as f:
        # should be a group with the name of the exporter
        group = f[ex.parameters()['Name']]
        assert isinstance(group, h5py.Group)

        # should be a dataset under the group with the name of the PlotItem
        assert_equal(np.array([x1, y1]), group['plot0'])

        # should be a dataset under the group with a default name that's the
        # index of the curve in the PlotItem
        assert_equal(np.array([x2, y2]), group['1'])