Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docarray/array/mixins/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def batch(
self,
batch_size: int,
shuffle: bool = False,
show_progress: bool = False,
) -> Generator['DocumentArray', None, None]:
"""
Creates a `Generator` that yields `DocumentArray` of size `batch_size` until `docs` is fully traversed along
Expand All @@ -51,8 +52,10 @@ def batch(

:param batch_size: Size of each generated batch (except the last one, which might be smaller, default: 32)
:param shuffle: If set, shuffle the Documents before dividing into minibatches.
:param show_progress: if set, show a progress bar when batching documents.
:yield: a Generator of `DocumentArray`, each in the length of `batch_size`
"""
from rich.progress import track

if not (isinstance(batch_size, int) and batch_size > 0):
raise ValueError('`batch_size` should be a positive integer')
Expand All @@ -64,7 +67,11 @@ def batch(
if shuffle:
random.shuffle(ix)

for i in range(n_batches):
for i in track(
range(n_batches),
description='Batching documents',
disable=not show_progress,
):
yield self[ix[i * batch_size : (i + 1) * batch_size]]

def batch_ids(
Expand Down