-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1953090
commit d39d2ad
Showing
5 changed files
with
71 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
"""Data parts""" | ||
from .utils import BatchSplitter |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
import torch | ||
from ocf_datapipes.utils.consts import BatchKey | ||
from torch.utils.data import functional_datapipe, IterDataPipe | ||
|
||
|
||
def copy_batch_to_device(batch, device): | ||
"""Moves a dict-batch of tensors to new device.""" | ||
batch_copy = {} | ||
for k in list(batch.keys()): | ||
if isinstance(batch[k], torch.Tensor): | ||
batch_copy[k] = batch[k].to(device) | ||
else: | ||
batch_copy[k] = batch[k] | ||
return batch_copy | ||
|
||
|
||
def batch_to_tensor(batch): | ||
"""Moves numpy batch to a tensor""" | ||
for k in list(batch.keys()): | ||
if isinstance(batch[k], np.ndarray) and np.issubdtype(batch[k].dtype, np.number): | ||
batch[k] = torch.as_tensor(batch[k]) | ||
return batch | ||
|
||
|
||
def split_batches(batch): | ||
"""Splits a single batch of data.""" | ||
n_samples = batch[BatchKey.sensor].shape[0] | ||
keys = list(batch.keys()) | ||
examples = [{} for _ in range(n_samples)] | ||
for i in range(n_samples): | ||
b = examples[i] | ||
for k in keys: | ||
if ("idx" in k.name) or ("channel_names" in k.name): | ||
b[k] = batch[k] | ||
else: | ||
b[k] = batch[k][i] | ||
return examples | ||
|
||
|
||
@functional_datapipe("split_batches") | ||
class BatchSplitter(IterDataPipe): | ||
"""Pipeline step to split batches of data and yield single examples""" | ||
|
||
def __init__(self, source_datapipe: IterDataPipe): | ||
"""Pipeline step to split batches of data and yield single examples""" | ||
self.source_datapipe = source_datapipe | ||
|
||
def __iter__(self): | ||
"""Opens the NWP data""" | ||
for batch in self.source_datapipe: | ||
for example in split_batches(batch): | ||
yield example |
This file contains 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
This file contains 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