# Buggy code
def to_tensorflow(self, max_text_len=30):
    """
    Transforms into tensorflow dataset

    Parameters
    ----------
    max_text_len: integer
        the maximum length of text strings that would be stored. Strings longer than this would be snipped

    """
    try:
        import tensorflow as tf
    except ImportError:
        pass

    def tf_gen(step=4):
        with dask.config.set(scheduler="sync"):
            for index in range(0, len(self), step):
                arrs = [self[index : index + step].values() for i in range(1)]
                arrs = list(map(lambda x: x._array, _flatten(arrs)))
                arrs = dask.delayed(list, pure=False, nout=len(list(self.keys())))(arrs)
                arrs = arrs.compute()
                for ind, arr in enumerate(arrs):
                    if arr.dtype.type is np.str_:
                        arr = [
                            ([ord(x) for x in sample.tolist()[0:max_text_len]])
                            for sample in arr
                        ]
                        arr = np.array(
                            [
                                np.pad(
                                    sample,
                                    (0, max_text_len - len(sample)),
                                    "constant",
                                    constant_values=(32),
                                )
                                for sample in arr
                            ]
                        )
                        arrs[ind] = arr

                for i in range(step):
                    sample = {key: r[i] for key, r in zip(self[index].keys(), arrs)}
                    yield sample

    def tf_dtype(np_dtype):
        try:
            if "U" in np_dtype:
                return tf.dtypes.as_dtype("string")
            return tf.dtypes.as_dtype(np_dtype)
        except Exception as e:
            return tf.variant

    output_shapes = {}
    output_types = {}
    for key in self.keys():
        output_types[key] = tf_dtype(self._tensors[key].dtype)
        output_shapes[key] = self._tensors[key].shape[1:]

        # if this is a string, we change the type to int, as it's going to become ascii. shape is also set to None
        if output_types[key] == tf.dtypes.as_dtype("string"):
            output_types[key] = tf.dtypes.as_dtype("int8")
            output_shapes[key] = None

    # TODO use None for dimensions you don't know the length tf.TensorShape([None])
    # FIXME Dataset Generator is not very good with multiprocessing but its good for fast tensorflow support
    return tf.data.Dataset.from_generator(
        tf_gen,
        output_types=output_types,
        # output_shapes=output_shapes,
    )


# Traceback (ValueError)
Traceback (most recent call last):
File "hub/compute/tests/test_transform.py", line 284, in <module>
test_threaded()
File "hub/compute/tests/test_transform.py", line 88, in test_threaded
create_classification_dataset(ds_init).store(
File "/Users/davitb/Git/Hub/hub/compute/transform.py", line 246, in store
ds = self.upload(results, url=url, token=token, progressbar=progressbar)
File "/Users/davitb/Git/Hub/hub/compute/transform.py", line 177, in upload
list(self.map(upload_chunk, index_batched_values))
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/pathos/threading.py", line 134, in map
return _pool.map(star(f), zip(*args)) # chunksize
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/multiprocess/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/multiprocess/pool.py", line 771, in get
raise self._value
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/multiprocess/pool.py", line 125, in worker
result = (True, func(*args, **kwds))
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/multiprocess/pool.py", line 48, in mapstar
return list(map(*args))
File "/Users/davitb/Library/Python/3.8/lib/python/site-packages/pathos/helpers/mp_helper.py", line 15, in <lambda>
func = lambda args: f(*args)
File "/Users/davitb/Git/Hub/hub/compute/transform.py", line 167, in upload_chunk
ds[key, i * length + k] = el
File "/Users/davitb/Git/Hub/hub/api/dataset.py", line 316, in __setitem__
self._tensors[subpath][slice_list] = value
File "/Users/davitb/Git/Hub/hub/store/dynamic_tensor.py", line 207, in __setitem__
self._dynamic_tensor[slice_[0]]
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 571, in __getitem__
return self.get_basic_selection(selection, fields=fields)
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 696, in get_basic_selection
return self._get_basic_selection_nd(selection=selection, out=out,
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 739, in _get_basic_selection_nd
return self._get_selection(indexer=indexer, out=out, fields=fields)
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 1028, in _get_selection
self._chunk_getitem(chunk_coords, chunk_selection, out, out_selection,
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 1649, in _chunk_getitem
self._process_chunk(out, cdata, chunk_selection, drop_axes,
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 1592, in _process_chunk
chunk = self._decode_chunk(cdata)
File "/usr/local/lib/python3.8/site-packages/zarr/core.py", line 1802, in _decode_chunk
chunk = chunk.reshape(self._chunks, order=self._order)
ValueError: cannot reshape array of size 0 into shape (50,3)

# Fixed code
def to_tensorflow(self, max_text_len=30):
    """
    Transforms into tensorflow dataset

    Parameters
    ----------
    max_text_len: integer
        the maximum length of text strings that would be stored. Strings longer than this would be snipped

    """
    try:
        import tensorflow as tf
    except ImportError:
        pass

    def tf_gen(step=4):
        with dask.config.set(scheduler="sync"):
            for index in range(0, len(self), step):
                arrs = [self[index : index + step].values() for i in range(1)]
                arrs = list(map(lambda x: x._array, _flatten(arrs)))
                arrs = dask.delayed(list, pure=False, nout=len(list(self.keys())))(arrs)
                arrs = arrs.compute()
                for ind, arr in enumerate(arrs):
                    if arr.dtype.type is np.str_:
                        arr = [
                            ([ord(x) for x in sample.tolist()[0:max_text_len]])
                            for sample in arr
                        ]
                        arr = np.array(
                            [
                                np.pad(
                                    sample,
                                    (0, max_text_len - len(sample)),
                                    "constant",
                                    constant_values=(32),
                                )
                                for sample in arr
                            ]
                        )
                        arrs[ind] = arr

                for i in range(step):
                    sample = {key: r[i] for key, r in zip(self[index].keys(), arrs)}
                    yield sample

    def tf_dtype(np_dtype):
        try:
            if "U" in np_dtype:
                return tf.dtypes.as_dtype("string")
            return tf.dtypes.as_dtype(np_dtype)
        except Exception as e:
            logger.log(e)
            return tf.variant

    output_shapes = {}
    output_types = {}
    for key in self.keys():
        output_types[key] = tf_dtype(self._tensors[key].dtype)
        output_shapes[key] = self._tensors[key].shape[1:]

        # if this is a string, we change the type to int, as it's going to become ascii. shape is also set to None
        if output_types[key] == tf.dtypes.as_dtype("string"):
            output_types[key] = tf.dtypes.as_dtype("int8")
            output_shapes[key] = None

    # TODO use None for dimensions you don't know the length tf.TensorShape([None])
    # FIXME Dataset Generator is not very good with multiprocessing but its good for fast tensorflow support
    return tf.data.Dataset.from_generator(
        tf_gen,
        output_types=output_types,
        # output_shapes=output_shapes,
    )