generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
waveform: Implement scaling #7
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 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d4b70a9
waveform: Scaling work-in-progress
bkeryan d733a0a
waveform: Move initialization of ScaleMode.none
bkeryan 61c2f42
waveform: Work around apparent bug in numpy type hints
bkeryan 4fe3410
waveform: Rework float parameter validation
bkeryan 8c5debf
tests: Add test for NoneScaleMode
bkeryan a4eca97
tests: Fix some type hints
bkeryan ef5d05c
waveform: Add a comment about float(x) accepting strings
bkeryan 02a13fa
waveform: Use repr in TypeError messages
bkeryan ce6bd69
waveform: Add doctests for arg_to_x
bkeryan c255cc7
waveform: Add AnalogWaveform.scale_mode
bkeryan 85bba73
waveform: Clean up some error messages
bkeryan 5cc89dc
waveform: Implement scaling
bkeryan 8a5d605
tests: Add waveform scaling tests
bkeryan d89c78e
waveform: Remove ScaleMode.none dependency cycle
bkeryan af04a78
waveform: Remove AnalogWaveform <-> ScaleMode dependency cycle
bkeryan 57afc2f
nitypes: Move utils to a more central location
bkeryan 6993792
waveform: Clarify get_scaled_data usage
bkeryan 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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| """Waveform scaling data types for NI Python APIs.""" | ||
|
|
||
| from nitypes.waveform._scaling._base import ScaleMode | ||
| from nitypes.waveform._scaling._none import NoneScaleMode | ||
|
|
||
bkeryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Defined here to avoid a circular dependency | ||
| ScaleMode.none = NoneScaleMode() | ||
bkeryan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """A scale mode that does not scale data.""" | ||
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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING, Any, ClassVar, SupportsIndex, TypeVar, overload | ||
|
|
||
| import numpy as np | ||
| import numpy.typing as npt | ||
|
|
||
| from nitypes.waveform._utils import validate_dtype | ||
|
|
||
| if TYPE_CHECKING: | ||
| # circular imports | ||
| from nitypes.waveform._analog_waveform import AnalogWaveform | ||
| from nitypes.waveform._scaling._none import NoneScaleMode | ||
|
|
||
| _TRaw = TypeVar("_TRaw", bound=np.generic) | ||
|
|
||
| # It might make sense to constrain this to np.float32 or np.float64 once | ||
| # https://github.com/numpy/numpy/issues/28805 is fixed, but bound=np.generic currently has more | ||
| # predictable results. | ||
| _TScaled = TypeVar("_TScaled", bound=np.generic) | ||
|
|
||
|
|
||
| _SCALED_DTYPES = ( | ||
| # Floating point | ||
| np.single, | ||
| np.double, | ||
| ) | ||
|
|
||
|
|
||
| class ScaleMode(ABC): | ||
| """An object that specifies how the waveform is scaled.""" | ||
|
|
||
| __slots__ = () | ||
|
|
||
| none: ClassVar[NoneScaleMode] | ||
|
|
||
| # If dtype is not specified, _ScaledDataType defaults to np.float64. | ||
| @overload | ||
| def get_scaled_data( # noqa: D107 - Missing docstring in __init__ (auto-generated noqa) | ||
| self, | ||
| waveform: AnalogWaveform[_TRaw], | ||
| dtype: None = ..., | ||
| *, | ||
| start_index: SupportsIndex | None = ..., | ||
| sample_count: SupportsIndex | None = ..., | ||
| ) -> npt.NDArray[np.float64]: ... | ||
|
|
||
| @overload | ||
| def get_scaled_data( # noqa: D107 - Missing docstring in __init__ (auto-generated noqa) | ||
| self, | ||
| waveform: AnalogWaveform[_TRaw], | ||
| dtype: type[_TScaled] | np.dtype[_TScaled] = ..., | ||
| *, | ||
| start_index: SupportsIndex | None = ..., | ||
| sample_count: SupportsIndex | None = ..., | ||
| ) -> npt.NDArray[_TScaled]: ... | ||
|
|
||
| @overload | ||
| def get_scaled_data( # noqa: D107 - Missing docstring in __init__ (auto-generated noqa) | ||
| self, | ||
| waveform: AnalogWaveform[Any], | ||
| dtype: npt.DTypeLike = ..., | ||
| *, | ||
| start_index: SupportsIndex | None = ..., | ||
| sample_count: SupportsIndex | None = ..., | ||
| ) -> npt.NDArray[Any]: ... | ||
|
|
||
| def get_scaled_data( | ||
| self, | ||
| waveform: AnalogWaveform[Any], | ||
| dtype: npt.DTypeLike = None, | ||
| *, | ||
| start_index: SupportsIndex | None = 0, | ||
| sample_count: SupportsIndex | None = None, | ||
| ) -> npt.NDArray[_TScaled]: | ||
| """Get scaled analog waveform data using the specified sample index and count. | ||
| Args: | ||
| waveform: The waveform to scale. | ||
| dtype: The NumPy data type for the analog waveform data. If not specified, the data | ||
| type defaults to np.float64. | ||
| start_index: The start index. | ||
| sample_count: The number of samples to scale. | ||
| Returns: | ||
| The scaled analog waveform data. | ||
| """ | ||
| from nitypes.waveform._analog_waveform import AnalogWaveform | ||
|
|
||
| if not isinstance(waveform, AnalogWaveform): | ||
bkeryan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| raise TypeError( | ||
| "The waveform must be an AnalogWaveform object.\n\n" f"Type: {type(waveform)}" | ||
| ) | ||
|
|
||
| if dtype is None: | ||
| dtype = np.float64 | ||
| validate_dtype(dtype, _SCALED_DTYPES) | ||
|
|
||
| raw_data = waveform.get_raw_data(start_index, sample_count) | ||
| return self._transform_data(raw_data.astype(dtype)) | ||
|
|
||
| @abstractmethod | ||
| def _transform_data(self, data: npt.NDArray[_TScaled]) -> npt.NDArray[_TScaled]: | ||
| raise NotImplementedError | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.