-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #287 from markus-kreft/utils_from_pvnet
Add NumpyBatch utils from PVNet
- Loading branch information
Showing
6 changed files
with
111 additions
and
4 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
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,75 @@ | ||
"""Additional utils for working with batches""" | ||
import numpy as np | ||
import torch | ||
|
||
from ocf_datapipes.batch import NumpyBatch, TensorBatch | ||
|
||
|
||
def _copy_batch_to_device(batch: dict, device: torch.device) -> dict: | ||
""" | ||
Moves tensor leaves in a nested dict to a new device | ||
Args: | ||
batch: nested dict with tensors to move | ||
device: Device to move tensors to | ||
Returns: | ||
A dict with tensors moved to new device | ||
""" | ||
batch_copy = {} | ||
|
||
for k, v in batch.items(): | ||
if isinstance(v, dict): | ||
# Recursion to reach the nested NWP | ||
batch_copy[k] = _copy_batch_to_device(v, device) | ||
elif isinstance(v, torch.Tensor): | ||
batch_copy[k] = v.to(device) | ||
else: | ||
batch_copy[k] = v | ||
return batch_copy | ||
|
||
|
||
def copy_batch_to_device(batch: TensorBatch, device: torch.device) -> TensorBatch: | ||
""" | ||
Moves the tensors in a TensorBatch to a new device. | ||
Args: | ||
batch: TensorBatch with tensors to move | ||
device: Device to move tensors to | ||
Returns: | ||
TensorBatch with tensors moved to new device | ||
""" | ||
return _copy_batch_to_device(batch, device) | ||
|
||
|
||
def _batch_to_tensor(batch: dict) -> dict: | ||
""" | ||
Moves ndarrays in a nested dict to torch tensors | ||
Args: | ||
batch: nested dict with data in numpy arrays | ||
Returns: | ||
Nested dict with data in torch tensors | ||
""" | ||
for k, v in batch.items(): | ||
if isinstance(v, dict): | ||
# Recursion to reach the nested NWP | ||
batch[k] = _batch_to_tensor(v) | ||
elif isinstance(v, np.ndarray) and np.issubdtype(v.dtype, np.number): | ||
batch[k] = torch.as_tensor(v) | ||
return batch | ||
|
||
|
||
def batch_to_tensor(batch: NumpyBatch) -> TensorBatch: | ||
""" | ||
Moves data in a NumpyBatch to a TensorBatch | ||
Args: | ||
batch: NumpyBatch with data in numpy arrays | ||
Returns: | ||
TensorBatch with data in torch tensors | ||
""" | ||
return _batch_to_tensor(batch) |
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
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,30 @@ | ||
import numpy as np | ||
import torch | ||
|
||
from ocf_datapipes.batch import BatchKey, NumpyBatch, TensorBatch | ||
from ocf_datapipes.batch import copy_batch_to_device, batch_to_tensor | ||
|
||
|
||
def _create_test_batch() -> NumpyBatch: | ||
sample: NumpyBatch = {} | ||
sample[BatchKey.satellite_actual] = np.full((12, 10, 24, 24), 0) | ||
return sample | ||
|
||
|
||
def test_batch_to_tensor() -> None: | ||
batch: NumpyBatch = _create_test_batch() | ||
tensor_batch = batch_to_tensor(batch) | ||
assert isinstance(tensor_batch[BatchKey.satellite_actual], torch.Tensor) | ||
|
||
|
||
def test_copy_batch_to_device() -> None: | ||
batch = _create_test_batch() | ||
tensor_batch = batch_to_tensor(batch) | ||
device = torch.device("cpu") | ||
batch_copy: TensorBatch = copy_batch_to_device(tensor_batch, device) | ||
assert batch_copy[BatchKey.satellite_actual].device == device # type: ignore | ||
|
||
|
||
if __name__ == "__main__": | ||
test_batch_to_tensor() | ||
test_copy_batch_to_device() |