-
Notifications
You must be signed in to change notification settings - Fork 78
Fix OfflineDataset for non-array Simulator output
#364
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import numpy as np | ||
|
|
||
| from bayesflow.adapters import Adapter | ||
| from bayesflow.utils import logging | ||
|
|
||
|
|
||
| class OfflineDataset(keras.utils.PyDataset): | ||
|
|
@@ -11,12 +12,20 @@ | |
| See the `DiskDataset` class for handling large datasets that are split into multiple smaller files. | ||
| """ | ||
|
|
||
| def __init__(self, data: dict[str, np.ndarray], batch_size: int, adapter: Adapter | None, **kwargs): | ||
| def __init__( | ||
| self, data: dict[str, np.ndarray], batch_size: int, adapter: Adapter | None, num_samples: int = None, **kwargs | ||
| ): | ||
| super().__init__(**kwargs) | ||
| self.batch_size = batch_size | ||
| self.data = data | ||
| self.adapter = adapter | ||
| self.num_samples = next(iter(data.values())).shape[0] | ||
|
|
||
| if num_samples is None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do like that the user has both options (a) to automatically compute num_samples (default) or (b) to manually specify it, if for some reason, num_samples cannot be reliably inferred by the data. |
||
| self.num_samples = self._get_num_samples_from_data(data) | ||
| logging.debug(f"Automatically determined {self.num_samples} samples in data.") | ||
| else: | ||
| self.num_samples = num_samples | ||
|
|
||
| self.indices = np.arange(self.num_samples, dtype="int64") | ||
|
|
||
| self.shuffle() | ||
|
|
@@ -29,7 +38,10 @@ | |
| item = slice(item * self.batch_size, (item + 1) * self.batch_size) | ||
| item = self.indices[item] | ||
|
|
||
| batch = {key: np.take(value, item, axis=0) for key, value in self.data.items()} | ||
| batch = { | ||
| key: np.take(value, item, axis=0) if isinstance(value, np.ndarray) else value | ||
| for key, value in self.data.items() | ||
| } | ||
|
|
||
| if self.adapter is not None: | ||
| batch = self.adapter(batch) | ||
|
|
@@ -46,3 +58,13 @@ | |
| def shuffle(self) -> None: | ||
| """Shuffle the dataset in-place.""" | ||
| np.random.shuffle(self.indices) | ||
|
|
||
| @staticmethod | ||
| def _get_num_samples_from_data(data: dict) -> int: | ||
| for key, value in data.items(): | ||
| if hasattr(value, "shape"): | ||
| ndim = len(value.shape) | ||
| if ndim > 1: | ||
| return value.shape[0] | ||
|
|
||
| raise ValueError("Could not determine number of samples from data. Please pass it manually.") | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add docs about the arguments of the DataSet classes. But I guess this can be done independently of this bug fix.