Skip to content

Lazily generate batches #112

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 4 commits into from
Oct 21, 2022
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
49 changes: 28 additions & 21 deletions xbatcher/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _iterate_through_dataset(ds, dims, overlap={}):

for slices in itertools.product(*dim_slices):
selector = {key: slice for key, slice in zip(dims, slices)}
yield ds.isel(**selector)
yield selector


def _drop_input_dims(ds, input_dims, suffix="_input"):
Expand Down Expand Up @@ -120,13 +120,11 @@ def __init__(
self.batch_dims = OrderedDict(batch_dims)
self.concat_input_dims = concat_input_dims
self.preload_batch = preload_batch

self._batches: Dict[int, Any] = self._gen_batches() # dict cache for batches
# in the future, we can make this a lru cache or similar thing (cachey?)

def __iter__(self) -> Iterator[xr.Dataset]:
for batch in self._batches.values():
yield batch
for idx in self._batches:
yield self[idx]

def __len__(self) -> int:
return len(self._batches)
Expand All @@ -142,7 +140,25 @@ def __getitem__(self, idx: int) -> xr.Dataset:
idx = list(self._batches)[idx]

if idx in self._batches:
return self._batches[idx]

if self.concat_input_dims:
new_dim_suffix = "_input"
all_dsets = [
_drop_input_dims(
self.ds.isel(**ds_input_select),
list(self.input_dims),
suffix=new_dim_suffix,
)
for ds_input_select in self._batches[idx]
]
dsc = xr.concat(all_dsets, dim="input_batch")
new_input_dims = [str(dim) + new_dim_suffix for dim in self.input_dims]
return _maybe_stack_batch_dims(dsc, new_input_dims)
else:

return _maybe_stack_batch_dims(
self.ds.isel(**self._batches[idx]), list(self.input_dims)
)
else:
raise IndexError("list index out of range")

Expand All @@ -151,26 +167,17 @@ def _gen_batches(self) -> dict:
# going the eager route for now is allowing me to fill out the loader api
# but it is likely to perform poorly.
batches = []
for ds_batch in self._iterate_batch_dims(self.ds):
for ds_batch_selector in self._iterate_batch_dims(self.ds):
ds_batch = self.ds.isel(**ds_batch_selector)
if self.preload_batch:
ds_batch.load()

input_generator = self._iterate_input_dims(ds_batch)

if self.concat_input_dims:
new_dim_suffix = "_input"
all_dsets = [
_drop_input_dims(
ds_input, list(self.input_dims), suffix=new_dim_suffix
)
for ds_input in input_generator
]
dsc = xr.concat(all_dsets, dim="input_batch")
new_input_dims = [str(dim) + new_dim_suffix for dim in self.input_dims]
batches.append(_maybe_stack_batch_dims(dsc, new_input_dims))
batches.append(list(input_generator))
else:
for ds_input in input_generator:
batches.append(
_maybe_stack_batch_dims(ds_input, list(self.input_dims))
)
batches += list(input_generator)

return dict(zip(range(len(batches)), batches))

Expand Down