def perform(chunksize, stride):
            try:
                transformed_output = tica.get_output(chunk=chunksize, stride=stride)
                tica.write_to_hdf5(out, group=group, chunksize=chunksize, stride=stride)

                import h5py
                with h5py.File(out) as f:
                    assert len(f[group]) == len(data)
                    for (itraj, actual), desired in zip(f[group].items(), transformed_output):
                        np.testing.assert_equal(actual, desired, err_msg='failed for cs=%s, stride=%s'
                                                                         %(chunksize, stride))
            finally:
                os.remove(out)
    def test_write_h5(self):
        from pyemma.coordinates import tica
        dim = 10
        data = [
            np.random.random((np.random.randint(50, 150), dim))
            for _ in range(4)
        ]
        tica = tica(data, lag=1)
        import tempfile
        out = tempfile.mktemp()
        group = '/test'

        def perform(chunksize, stride):
            try:
                transformed_output = tica.get_output(chunk=chunksize,
                                                     stride=stride)
                tica.write_to_hdf5(out,
                                   group=group,
                                   chunksize=chunksize,
                                   stride=stride)
                assert os.path.exists(out), "File did not get created"
                import h5py
                with h5py.File(out, mode='r') as f:
                    assert len(f[group]) == len(data)
                    for (itraj,
                         actual), desired in zip(f[group].items(),
                                                 transformed_output):
                        np.testing.assert_equal(
                            actual,
                            desired,
                            err_msg='failed for cs=%s, stride=%s' %
                            (chunksize, stride))
            finally:
                if os.path.exists(out):
                    os.remove(out)

        for cs in [0, 1, 3, 10, 42, 50]:
            for s in [1, 2, 3, 10]:
                perform(cs, s)

        # test overwrite
        try:
            tica.write_to_hdf5(out, group=group)
            with self.assertRaises(ValueError):
                tica.write_to_hdf5(out, group=group)

            os.remove(out)
            tica.write_to_hdf5(out)
            with self.assertRaises(ValueError) as ctx:
                tica.write_to_hdf5(out)
            assert 'Refusing to overwrite data' in ctx.exception.args[0]

            os.remove(out)
            tica.write_to_hdf5(out, group=group)
            tica.write_to_hdf5(out, group=group, overwrite=True)

            os.remove(out)
            import h5py
            with h5py.File(out, mode='a') as f:
                f.create_group('empty').create_dataset('0000', shape=(1, 1))
            with self.assertRaises(ValueError):
                tica.write_to_hdf5(out, group='empty')
            tica.write_to_hdf5(out, group='empty', overwrite=True)
        finally:
            os.remove(out)