-
Notifications
You must be signed in to change notification settings - Fork 2
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 #19 from Rohde-Schwarz/18-npz-loader-and-saver
Adding npz file format loader and saver
- Loading branch information
Showing
27 changed files
with
375 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""Iq tar loader and saver implementation.""" | ||
|
||
from .Load import Load | ||
from .Save import Save | ||
from .load import Load | ||
from .save import Save | ||
|
||
__all__ = ["Load", "Save"] |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
"""Iqw loader and saver implementation.""" | ||
|
||
from .Load import Load | ||
from .Save import Save | ||
from .load import Load | ||
from .save import Save | ||
|
||
__all__ = ["Load", "Save"] |
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
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,4 @@ | ||
# The Npz loader and saver | ||
|
||
The Npz loader and saver comes also with an extended interface providing a `dtype` parameter | ||
for setting the format of the output `i` and `q` values accordingly for loading and saving. |
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,6 @@ | ||
"""Npz waveform loader and saver implementation.""" | ||
|
||
from .load import Load | ||
from .save import Save | ||
|
||
__all__ = ["Load", "Save"] |
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,90 @@ | ||
"""Load implementation for generic waveform.""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import typing | ||
|
||
import numpy as np | ||
|
||
from ..load_interface import LoadInterface | ||
from ..meta import Meta | ||
from ..parent_storage import ParentStorage | ||
from ..storage import Storage | ||
from .npz_inteface import NpzInterface | ||
|
||
if typing.TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class Load(LoadInterface, NpzInterface): | ||
"""Load class for generic waveform.""" | ||
|
||
def load(self, file: typing.Union[str, typing.IO, Path]) -> ParentStorage: | ||
"""Load waveform data from npz file.""" | ||
content = np.load(file=file, allow_pickle=True) | ||
data_objs = content.get("storages") | ||
if not data_objs: | ||
raise ValueError(f"File {file} is not compliant to data schema.") | ||
parent = ParentStorage(number_of_storages=len(data_objs)) | ||
for idx, obj in enumerate(data_objs): | ||
i_values = obj.get("i") | ||
q_values = obj.get("q") | ||
meta = obj.get("meta", {}) | ||
if i_values is None: | ||
LOGGER.warning( | ||
( | ||
"Skipping index %s of file %s content because no i values are" | ||
" in it." | ||
), | ||
idx, | ||
str(file), | ||
) | ||
continue | ||
if q_values is None: | ||
q_values = np.zeros(i_values.size) | ||
if q_values is not None and len(i_values) != len(q_values): | ||
LOGGER.warning( | ||
( | ||
"Skipping index %s of file %s content because i value and" | ||
" q value lengths are not identical." | ||
), | ||
idx, | ||
str(file), | ||
) | ||
continue | ||
storage = Storage() | ||
storage.data = i_values.astype(self.dtype) + 1j * q_values.astype( | ||
self.dtype | ||
) | ||
storage.meta = Meta(no_defaults=True, **meta) | ||
parent.storages[idx] = storage | ||
return parent | ||
|
||
def load_in_chunks( | ||
self, | ||
file: typing.Union[str, typing.IO, Path], | ||
samples: int, | ||
offset: int, | ||
) -> ParentStorage: | ||
"""Load chunk waveform data from file.""" | ||
parent = self.load(file) | ||
for idx, storage in enumerate(parent.storages): | ||
if offset + samples > len(storage.data): | ||
raise ValueError( | ||
f"Could not load chunks for index {idx} of file {file} because " | ||
f"offset {offset} and sample count {samples} is greater than length" | ||
f" of available data {len(storage.data)}. " | ||
) | ||
storage.data = storage.data[offset : offset + samples] | ||
return parent | ||
|
||
def load_meta(self, file: typing.Union[str, typing.IO, Path]) -> ParentStorage: | ||
"""Load meta information only from wv file.""" | ||
parent = self.load(file) | ||
for storage in parent.storages: | ||
storage.data = np.empty((0, 0)) | ||
return parent |
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 @@ | ||
"""Npz interface.""" | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
|
||
|
||
class NpzInterface: | ||
"""Interface class for Npz.""" | ||
|
||
def __init__(self) -> None: | ||
"""Instantiate interface class.""" | ||
self._dtype: npt.DTypeLike = np.dtype(np.float16) | ||
|
||
@property | ||
def dtype(self) -> npt.DTypeLike: | ||
"""Dtype for loading and saving. | ||
Returns: | ||
DTypeLike: Numpy dtype | ||
""" | ||
return self._dtype | ||
|
||
@dtype.setter | ||
def dtype(self, value: npt.DTypeLike) -> None: | ||
"""Dtype for loading and saving. | ||
Args: | ||
value (DTypeLike): Numpy dtype | ||
""" | ||
self._dtype = value |
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,33 @@ | ||
"""Save implementation for generic waveform.""" | ||
|
||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
import numpy as np | ||
|
||
from ..parent_storage import ParentStorage | ||
from ..save_interface import SaveInterface | ||
from .npz_inteface import NpzInterface | ||
|
||
if typing.TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
|
||
class Save(SaveInterface, NpzInterface): | ||
"""Save class for generic waveform.""" | ||
|
||
def save( | ||
self, | ||
file: typing.Union[str, typing.IO, Path], | ||
datas: ParentStorage, | ||
scale: float = np.power(2, np.iinfo(np.int16).bits - 1), | ||
) -> None: | ||
"""Save waveform data to file.""" | ||
data_objs = [] | ||
for storage in datas.storages: | ||
i_values = np.real(storage.data).astype(self.dtype) | ||
q_values = np.imag(storage.data).astype(self.dtype) | ||
meta = storage.meta._items # pylint: disable=W0212 | ||
data_objs.append({"i": i_values, "q": q_values, "meta": meta}) | ||
np.savez_compressed(file=file, storages=data_objs) # type: ignore |
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
File renamed without changes.
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,6 +1,6 @@ | ||
"""Signal Generator Waveform loader and saver implementation.""" | ||
|
||
from .Load import Load | ||
from .Save import Save | ||
from .load import Load | ||
from .save import Save | ||
|
||
__all__ = ["Load", "Save"] |
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
Binary file not shown.
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
Oops, something went wrong.