generated from ni/github-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Add XYData conversion methods #192
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
39 changes: 39 additions & 0 deletions
39
packages/ni.protobuf.types/src/ni/protobuf/types/xydata_conversion.py
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,39 @@ | ||
| """Methods to convert to and from DoubleXYData protobuf messages.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| from nitypes.xy_data import XYData | ||
|
|
||
| import ni.protobuf.types.xydata_pb2 as xydata_pb2 | ||
| from ni.protobuf.types.extended_property_conversion import ( | ||
| extended_properties_from_protobuf, | ||
| extended_properties_to_protobuf, | ||
| ) | ||
|
|
||
|
|
||
| def float64_xydata_to_protobuf(value: XYData[Any], /) -> xydata_pb2.DoubleXYData: | ||
| """Convert a XYData python object to a protobuf xydata_pb2.DoubleXYData.""" | ||
| attributes = extended_properties_to_protobuf(value.extended_properties) | ||
| xydata_message = xydata_pb2.DoubleXYData( | ||
| x_data=value.x_data, | ||
| y_data=value.y_data, | ||
| attributes=attributes, | ||
| ) | ||
| return xydata_message | ||
|
|
||
|
|
||
| def float64_xydata_from_protobuf(message: xydata_pb2.DoubleXYData, /) -> XYData[np.float64]: | ||
| """Convert the protobuf xydata_pb2.DoubleXYData to a Python XYData.""" | ||
| xydata = XYData.from_arrays_1d( | ||
| x_array=message.x_data, | ||
| y_array=message.y_data, | ||
| dtype=np.float64, | ||
| ) | ||
|
|
||
| # Transfer attributes to extended_properties | ||
| extended_properties_from_protobuf(message.attributes, xydata.extended_properties) | ||
|
|
||
| return xydata | ||
129 changes: 129 additions & 0 deletions
129
packages/ni.protobuf.types/tests/unit/test_xydata_conversion.py
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,129 @@ | ||
| import numpy as np | ||
| from nitypes.xy_data import XYData | ||
|
|
||
| import ni.protobuf.types.xydata_pb2 as xydata_pb2 | ||
| from ni.protobuf.types.attribute_value_pb2 import AttributeValue | ||
| from ni.protobuf.types.xydata_conversion import ( | ||
| float64_xydata_from_protobuf, | ||
| float64_xydata_to_protobuf, | ||
| ) | ||
|
|
||
|
|
||
| # ======================================================== | ||
| # XYData: Protobuf to Python | ||
| # ======================================================== | ||
| def test___doublexydata_protobuf___convert___valid_xydata_default_units() -> None: | ||
| expected_x_data = [1.0, 2.0, 3.0] | ||
| expected_y_data = [4.0, 5.0, 6.0] | ||
| protobuf_value = xydata_pb2.DoubleXYData(x_data=expected_x_data, y_data=expected_y_data) | ||
|
|
||
| python_value = float64_xydata_from_protobuf(protobuf_value) | ||
| assert isinstance(python_value, XYData) | ||
| assert python_value.dtype == np.float64 | ||
| assert list(python_value.x_data) == expected_x_data | ||
| assert list(python_value.y_data) == expected_y_data | ||
| assert python_value.x_units == "" | ||
| assert python_value.y_units == "" | ||
|
|
||
|
|
||
| def test___doublexydata_protobuf_with_units___convert___valid_xydata() -> None: | ||
| attributes = { | ||
| "NI_UnitDescription_X": AttributeValue(string_value="Volts"), | ||
| "NI_UnitDescription_Y": AttributeValue(string_value="Seconds"), | ||
| } | ||
| expected_x_data = [1.0, 2.0, 3.0] | ||
| expected_y_data = [4.0, 5.0, 6.0] | ||
| protobuf_value = xydata_pb2.DoubleXYData( | ||
| x_data=expected_x_data, | ||
| y_data=expected_y_data, | ||
| attributes=attributes, | ||
| ) | ||
|
|
||
| python_value = float64_xydata_from_protobuf(protobuf_value) | ||
|
|
||
| assert isinstance(python_value, XYData) | ||
| assert python_value.dtype == np.float64 | ||
| assert list(python_value.x_data) == expected_x_data | ||
| assert list(python_value.y_data) == expected_y_data | ||
| assert python_value.x_units == "Volts" | ||
| assert python_value.y_units == "Seconds" | ||
|
|
||
|
|
||
| def test___doublexydata_protobuf_with_other_attrs___convert___attrs_converted() -> None: | ||
| attributes = { | ||
| "NI_UnitDescription_X": AttributeValue(string_value="Volts"), | ||
| "NI_UnitDescription_Y": AttributeValue(string_value="Seconds"), | ||
| "Non_Units_Attribute": AttributeValue(double_value=1.1), | ||
| } | ||
| protobuf_value = xydata_pb2.DoubleXYData( | ||
| x_data=[1.0], | ||
| y_data=[2.0], | ||
| attributes=attributes, | ||
| ) | ||
|
|
||
| python_value = float64_xydata_from_protobuf(protobuf_value) | ||
|
|
||
| assert isinstance(python_value, XYData) | ||
| assert python_value.x_units == "Volts" | ||
| assert python_value.y_units == "Seconds" | ||
| assert python_value.extended_properties.get("Non_Units_Attribute") == 1.1 | ||
|
|
||
|
|
||
| # ======================================================== | ||
| # XYData: Python to Protobuf | ||
| # ======================================================== | ||
| def test___float64_xydata___convert___valid_doublexydata_protobuf() -> None: | ||
| expected_x_data = [1.0, 2.0, 3.0] | ||
| expected_y_data = [4.0, 5.0, 6.0] | ||
| python_value = XYData.from_arrays_1d( | ||
| x_array=expected_x_data, | ||
| y_array=expected_y_data, | ||
| dtype=np.float64, | ||
| ) | ||
|
|
||
| protobuf_value = float64_xydata_to_protobuf(python_value) | ||
|
|
||
| assert isinstance(protobuf_value, xydata_pb2.DoubleXYData) | ||
| assert list(protobuf_value.x_data) == expected_x_data | ||
| assert list(protobuf_value.y_data) == expected_y_data | ||
|
|
||
|
|
||
| def test___int16_xydata___convert___valid_doublexydata_protobuf() -> None: | ||
| python_value = XYData.from_arrays_1d( | ||
| x_array=[1, 2, 3], | ||
| y_array=[4, 5, 6], | ||
| dtype=np.int16, | ||
| ) | ||
|
|
||
| protobuf_value = float64_xydata_to_protobuf(python_value) | ||
|
|
||
| assert isinstance(protobuf_value, xydata_pb2.DoubleXYData) | ||
| # Data values converted to float. Is this OK? Or should we raise an error here? | ||
mjohanse-emr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert list(protobuf_value.x_data) == [1.0, 2.0, 3.0] | ||
| assert list(protobuf_value.y_data) == [4.0, 5.0, 6.0] | ||
|
|
||
|
|
||
| def test___xydata_with_extended_properties___convert___valid_doublexydata_protobuf() -> None: | ||
| expected_x_data = [1.0] | ||
| expected_y_data = [2.0] | ||
| python_value = XYData.from_arrays_1d( | ||
| x_array=expected_x_data, | ||
| y_array=expected_y_data, | ||
| dtype=np.float64, | ||
| extended_properties={ | ||
| "true": True, | ||
| "1": 1, | ||
| "1.0": 1.0, | ||
| "str": "str", | ||
| }, | ||
| ) | ||
|
|
||
| protobuf_value = float64_xydata_to_protobuf(python_value) | ||
|
|
||
| assert isinstance(protobuf_value, xydata_pb2.DoubleXYData) | ||
| assert list(protobuf_value.x_data) == expected_x_data | ||
| assert list(protobuf_value.y_data) == expected_y_data | ||
| assert protobuf_value.attributes["true"].bool_value is True | ||
| assert protobuf_value.attributes["1"].integer_value == 1 | ||
| assert protobuf_value.attributes["1.0"].double_value == 1.0 | ||
| assert protobuf_value.attributes["str"].string_value == "str" | ||
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.