Exemplo n.º 1
0
    def setup(self):
        self.file_v1 = 'v1.0.h5'
        self.file_v2 = 'v1.1.h5'
        self.teardown() # in case files already exist, remove it

        items, times, features = generate.full(20,10)
        h5f_1_0.write(self.file_v1, 'features', items, times, features)
        h5f_1_1.write(self.file_v2, 'features', items, times, features)
Exemplo n.º 2
0
    def test_bad_file2(self):
        with open(self.filename, 'w') as f:
            f.write('This is not a HDF5 file')

        with pytest.raises(IOError) as ioerror:
            h5f.write(self.filename, 'group', [], [], [])
        msg = str(ioerror.value)
        assert self.filename in msg
        assert 'not a HDF5 file' in msg
Exemplo n.º 3
0
    def test_bad_file2(self):
        with open(self.filename, 'w') as f:
            f.write('This is not a HDF5 file')

        data = generate.full(1)
        with pytest.raises(IOError) as ioerror:
            h5f.write(self.filename, 'group', data[0], data[1], data[2])
        msg = str(ioerror.value)
        assert self.filename in msg
        assert 'not a HDF5 file' in msg
Exemplo n.º 4
0
    def test_bad_file2(self):
        with open(self.filename, 'w') as f:
            f.write('This is not a HDF5 file')

        data = generate.full(1)
        with pytest.raises(IOError) as ioerror:
            h5f.write(self.filename, 'group', data[0], data[1], data[2])
        msg = str(ioerror.value)
        assert self.filename in msg
        assert 'not a HDF5 file' in msg
Exemplo n.º 5
0
    def test_write(self):
        files, times, features = generate.full(10, 2, 5)
        h5f.write(self.filename, 'f', files, times, features)

        with h5py.File(self.filename, 'r') as f:
            assert ['f'] == list(f.keys())
            g = f.get('f')
            assert list(g.keys()) == ['features', 'index', 'items', 'labels']
            assert g.get('features').shape[1] == 2
            assert g.get('index').shape == (10,)
            assert g.get('items').shape == (10,)
            assert g.get('features').shape[0] == g.get('labels').shape[0]
Exemplo n.º 6
0
    def test_write(self):
        files, times, features = generate.full(10, 2, 5)
        h5f.write(self.filename, 'f', files, times, features)

        with h5py.File(self.filename, 'r') as f:
            assert ['f'] == list(f.keys())
            g = f.get('f')
            assert list(g.keys()) == ['features', 'index', 'items', 'labels']
            assert g.get('features').shape[1] == 2
            assert g.get('index').shape == (10, )
            assert g.get('items').shape == (10, )
            assert g.get('features').shape[0] == g.get('labels').shape[0]
Exemplo n.º 7
0
    def _test_wr(self, time_format):
        """Test retrieving times and files after a write/read operation."""
        items, t_gold, feat = generate.full(self.nbitems, tformat=time_format)
        write(self.filename, self.group, items, t_gold, feat)
        t, _ = read(self.filename, self.group)

        assert len(t) == self.nbitems
        if time_format == 2:
            assert all([tt.shape[1] == time_format for tt in t.values()])

        # build a dict from gold to compare with t
        d = {}
        for k, v in zip(items, t_gold): d[k] = v
        # compare the two dicts
        for dd, tt in zip(d, t): assert tt == dd
Exemplo n.º 8
0
    def setup(self):
        # init default parameters
        self.filename = 'test.h5'
        self.group = 'features'

        # create a simple feature file
        items, times, feat = generate.full(10)
        self.features = Features(feat)
        self.times = Times(times)
        self.items = Items(items)
        self.items2 = Items([i+'2' for i in items])
        write(self.filename, self.group, items, times, feat)

        # read it with h5py
        self.f = h5py.File(self.filename, 'r')
        self.g = self.f.get(self.group)
Exemplo n.º 9
0
    def test_concat(self):
        """Concatenate new data to an existing item in an existing file."""

        # write a single item named 'item_0'
        data1 = generate.full(1, self.dim, 10, items_root='item')
        h5f.write(self.filename, 'group1', data1[0], data1[1], data1[2])
        h5f.write(self.filename, 'group3', data1[0], data1[1], data1[2])

        # concatenate new data to 'item_0'
        data2 = generate.full(1, self.dim, 20, items_root='item')
        h5f.write(self.filename, 'group2', data2[0], data2[1], data2[2])
        with pytest.raises(IOError):
            h5f.write(self.filename, 'group3', data2[0], data2[1], data2[2])
Exemplo n.º 10
0
    def test_concat(self):
        """Concatenate new data to an existing item in an existing file."""

        # write a single item named 'item_0'
        data1 = generate.full(1, self.dim, 10, items_root='item')
        h5f.write(self.filename, 'group1', data1[0], data1[1], data1[2])
        h5f.write(self.filename, 'group3', data1[0], data1[1], data1[2])

        # concatenate new data to 'item_0'
        data2 = generate.full(1, self.dim, 20, items_root='item')
        h5f.write(self.filename, 'group2', data2[0], data2[1], data2[2])
        with pytest.raises(IOError):
            h5f.write(self.filename, 'group3', data2[0], data2[1], data2[2])
Exemplo n.º 11
0
    def test_append(self):
        """Append a new item to an existing dataset."""
        i, t, f = generate.full(30, self.dim, 40, items_root='File')
        h5f.write(self.filename, 'group', i, t, f)

        # append new item to existing dataset
        features_added = np.zeros(shape=(1, self.dim))
        times_added = np.linspace(0, 2, 1)
        h5f.write(self.filename, 'group', ['File_31'],
                  [times_added], [features_added])

        with pytest.raises(IOError) as err:
            h5f.write(self.filename, 'group', ['File_3'],
                      [times_added], [features_added])
        assert 'data is not appendable to the group' in str(err.value)

        # read it
        times_r, features_r = h5f.read(self.filename, 'group')
        assert set(times_r.keys()) == set(i+['File_31'])
        assert set(features_r.keys()) == set(i+['File_31'])
        assert all(times_r['File_31'] == times_added)
        assert (features_r['File_31'] == features_added).all()
Exemplo n.º 12
0
    def test_append(self):
        """Append a new item to an existing dataset."""
        i, t, f = generate.full(30, self.dim, 40, items_root='File')
        h5f.write(self.filename, 'group', i, t, f)

        # append new item to existing dataset
        features_added = np.zeros(shape=(1, self.dim))
        times_added = np.linspace(0, 2, 1)
        h5f.write(self.filename, 'group', ['File_31'], [times_added],
                  [features_added])

        with pytest.raises(IOError) as err:
            h5f.write(self.filename, 'group', ['File_3'], [times_added],
                      [features_added])
        assert 'data is not appendable to the group' in str(err.value)

        # read it
        times_r, features_r = h5f.read(self.filename, 'group')
        assert set(times_r.keys()) == set(i + ['File_31'])
        assert set(features_r.keys()) == set(i + ['File_31'])
        assert all(times_r['File_31'] == times_added)
        assert (features_r['File_31'] == features_added).all()
Exemplo n.º 13
0
 def test_bad_file(self):
     a, b, c = generate.full(2)
     with pytest.raises(IOError) as ioerror:
         h5f.write('/silly/path/to/no/where.h5', 'group', a, b, c)
     assert all([s in str(ioerror.value)]
                for s in ['/silly/path', 'No such file'])
Exemplo n.º 14
0
 def setup(self):
     items, self.data, feats = generate.full(10,tformat=1)
     self.filename = 'test.h5'
     self.teardown()
     write(self.filename, 'group', items, self.data, feats)
     self.group = h5py.File(self.filename, 'a')['group']
Exemplo n.º 15
0
 def test_bad_file(self):
     a, b, c = generate.full(2)
     with pytest.raises(IOError) as ioerror:
         h5f.write('/silly/path/to/no/where.h5', 'group', a, b, c)
     assert all([s in str(ioerror.value)]
                for s in ['/silly/path', 'No such file'])
Exemplo n.º 16
0
 def test_bad_data(self):
     with pytest.raises(IOError) as ioerror:
         h5f.write('/silly/path/to/no/where.h5', 'group', [], [], [])
     assert 'data is empty' == str(ioerror.value)
Exemplo n.º 17
0
def test_raise_on_write_sparse():
    a, b, c = generate.full(1)
    with pytest.raises(NotImplementedError) as ioerror:
        h5f.write('test.h5', 'group', a, b, c, dformat='sparse')
    assert 'sparse' in str(ioerror.value)
Exemplo n.º 18
0
 def test_bad_data(self):
     with pytest.raises(IOError) as ioerror:
         h5f.write('/silly/path/to/no/where.h5', 'group', [], [], [])
     assert 'data is empty' == str(ioerror.value)
Exemplo n.º 19
0
def test_raise_on_write_sparse():
    a, b, c = generate.full(1)
    with pytest.raises(NotImplementedError) as ioerror:
        h5f.write('test.h5', 'group', a, b, c, dformat='sparse')
    assert 'sparse' in str(ioerror.value)