def write_data(data, filename):
    """Write data in dict `data` to HDF5 file."""
    is_root = isinstance(filename, str)
    group = h5.File(filename, 'w') if is_root else filename
    for key, value in six.iteritems(data):
        if isinstance(value, dict):
            key_group = group.create_group(key)
            write_data(value, key_group)
        else:
            group[key] = value
    if is_root:
        group.close()