Skip to content

Fix #481 #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 1, 2025
Merged
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions bayesflow/datasets/disk_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
adapter: Adapter | None,
stage: str = "training",
augmentations: Mapping[str, Callable] | Callable = None,
shuffle: bool = True,
**kwargs,
):
"""
Expand Down Expand Up @@ -67,6 +68,8 @@

Note - augmentations are applied before the adapter is called and are generally
transforms that you only want to apply during training.
shuffle : bool, optional
Whether to shuffle the dataset at initialization and at the end of each epoch. Default is True.
**kwargs
Additional keyword arguments passed to the base `PyDataset`.
"""
Expand All @@ -79,8 +82,9 @@
self.stage = stage

self.augmentations = augmentations

self.shuffle()
self._shuffle = shuffle
if self._shuffle:
self.shuffle()

Check warning on line 87 in bayesflow/datasets/disk_dataset.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/datasets/disk_dataset.py#L85-L87

Added lines #L85 - L87 were not covered by tests

def __getitem__(self, item) -> dict[str, np.ndarray]:
if not 0 <= item < self.num_batches:
Expand Down Expand Up @@ -108,7 +112,8 @@
return batch

def on_epoch_end(self):
self.shuffle()
if self._shuffle:
self.shuffle()

Check warning on line 116 in bayesflow/datasets/disk_dataset.py

View check run for this annotation

Codecov / codecov/patch

bayesflow/datasets/disk_dataset.py#L115-L116

Added lines #L115 - L116 were not covered by tests

@property
def num_batches(self):
Expand Down
11 changes: 8 additions & 3 deletions bayesflow/datasets/offline_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(
*,
stage: str = "training",
augmentations: Mapping[str, Callable] | Callable = None,
shuffle: bool = True,
**kwargs,
):
"""
Expand Down Expand Up @@ -51,6 +52,8 @@ def __init__(

Note - augmentations are applied before the adapter is called and are generally
transforms that you only want to apply during training.
shuffle : bool, optional
Whether to shuffle the dataset at initialization and at the end of each epoch. Default is True.
**kwargs
Additional keyword arguments passed to the base `PyDataset`.
"""
Expand All @@ -69,8 +72,9 @@ def __init__(
self.indices = np.arange(self.num_samples, dtype="int64")

self.augmentations = augmentations

self.shuffle()
self._shuffle = shuffle
if self._shuffle:
self.shuffle()

def __getitem__(self, item: int) -> dict[str, np.ndarray]:
"""
Expand Down Expand Up @@ -122,7 +126,8 @@ def num_batches(self) -> int | None:
return int(np.ceil(self.num_samples / self.batch_size))

def on_epoch_end(self) -> None:
self.shuffle()
if self._shuffle:
self.shuffle()

def shuffle(self) -> None:
"""Shuffle the dataset in-place."""
Expand Down