Skip to content

Commit 799356a

Browse files
author
Michael Johansen
committed
Add remaining waveform converters
Signed-off-by: Michael Johansen <[email protected]>
1 parent 55e6e73 commit 799356a

File tree

6 files changed

+731
-204
lines changed

6 files changed

+731
-204
lines changed

examples/all_types/define_types.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import enum
55

66
import numpy as np
7+
from nitypes.complex import ComplexInt32DType
78
from nitypes.scalar import Scalar
8-
from nitypes.waveform import AnalogWaveform
9+
from nitypes.waveform import AnalogWaveform, ComplexWaveform, DigitalWaveform, Spectrum
910

1011

1112
class MyIntFlags(enum.IntFlag):
@@ -64,6 +65,11 @@ class MyMixedEnum(enum.Enum):
6465
VALUE3 = 3.0
6566

6667

68+
def _create_digital_waveform() -> DigitalWaveform[np.bool]:
69+
data = np.array([[0, 1, 0], [1, 0, 1]], dtype=np.bool)
70+
return DigitalWaveform.from_lines(data, dtype=np.bool, signal_count=3)
71+
72+
6773
all_types_with_values = {
6874
# supported scalar types
6975
"bool": True,
@@ -83,7 +89,12 @@ class MyMixedEnum(enum.Enum):
8389
"mixedenum": MyMixedEnum.VALUE2,
8490
# NI types
8591
"nitypes_Scalar": Scalar(42, "m"),
86-
"nitypes_AnalogWaveform": AnalogWaveform.from_array_1d(np.array([1.0, 2.0, 3.0])),
92+
"nitypes_DoubleAnalogWaveform": AnalogWaveform.from_array_1d(np.array([1.0, 2.0, 3.0])),
93+
"nitypes_I16AnalogWaveform": AnalogWaveform.from_array_1d(np.array([1, 2, 3]), dtype=np.int16),
94+
"nitypes_DoubleComplexWaveform": ComplexWaveform(2, np.complex128),
95+
"nitypes_I16ComplexWaveform": ComplexWaveform(2, ComplexInt32DType),
96+
"nitypes_DigitalWaveform": _create_digital_waveform(),
97+
"nitypes_Spectrum": Spectrum.from_array_1d(np.array([1.0, 2.0, 3.0])),
8798
# supported collection types
8899
"bool_collection": [True, False, True],
89100
"bytes_collection": [b"one", b"two", b"three"],

src/nipanel/_convert.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Any, Iterable
88

99
from google.protobuf import any_pb2
10+
from nitypes.waveform import AnalogWaveform, ComplexWaveform
1011

1112
from nipanel.converters import Converter
1213
from nipanel.converters.builtin import (
@@ -21,9 +22,14 @@
2122
from nipanel.converters.protobuf_types import (
2223
BoolCollectionConverter,
2324
BytesCollectionConverter,
25+
DigitalWaveformConverter,
2426
Double2DArrayConverter,
2527
DoubleAnalogWaveformConverter,
28+
DoubleComplexWaveformConverter,
29+
DoubleSpectrumConverter,
2630
FloatCollectionConverter,
31+
Int16AnalogWaveformConverter,
32+
Int16ComplexWaveformConverter,
2733
IntCollectionConverter,
2834
ScalarConverter,
2935
StrCollectionConverter,
@@ -44,9 +50,14 @@
4450
# Protobuf Types
4551
BoolCollectionConverter(),
4652
BytesCollectionConverter(),
53+
DigitalWaveformConverter(),
4754
Double2DArrayConverter(),
4855
DoubleAnalogWaveformConverter(),
56+
DoubleComplexWaveformConverter(),
57+
DoubleSpectrumConverter(),
4958
FloatCollectionConverter(),
59+
Int16AnalogWaveformConverter(),
60+
Int16ComplexWaveformConverter(),
5061
IntCollectionConverter(),
5162
StrCollectionConverter(),
5263
ScalarConverter(),
@@ -73,6 +84,7 @@ def to_any(python_value: object) -> any_pb2.Any:
7384

7485
def _get_best_matching_type(python_value: object) -> str:
7586
underlying_parents = type(python_value).mro() # This covers enum.IntEnum and similar
87+
additional_info_string = _get_additional_type_info_string(python_value)
7688

7789
container_types = []
7890
value_is_collection = any(_CONVERTIBLE_COLLECTION_TYPES.intersection(underlying_parents))
@@ -105,7 +117,9 @@ def _get_best_matching_type(python_value: object) -> str:
105117
best_matching_type = None
106118
candidates = _get_candidate_strings(underlying_parents)
107119
for candidate in candidates:
108-
python_typename = _create_python_typename(candidate, container_types)
120+
python_typename = _create_python_typename(
121+
candidate, container_types, additional_info_string
122+
)
109123
if python_typename not in _SUPPORTED_PYTHON_TYPES:
110124
continue
111125
best_matching_type = python_typename
@@ -115,7 +129,8 @@ def _get_best_matching_type(python_value: object) -> str:
115129
payload_type = underlying_parents[0]
116130
raise TypeError(
117131
f"Unsupported type: ({container_types}, {payload_type}) with parents "
118-
f"{underlying_parents}. Supported types are: {_SUPPORTED_PYTHON_TYPES}"
132+
f"{underlying_parents}.\n\nSupported types are: {_SUPPORTED_PYTHON_TYPES}"
133+
f"\n\nAdditional type info: {additional_info_string}"
119134
)
120135
_logger.debug(f"Best matching type for '{repr(python_value)}' resolved to {best_matching_type}")
121136
return best_matching_type
@@ -146,12 +161,24 @@ def _get_candidate_strings(candidates: Iterable[type]) -> list[str]:
146161
candidate_names = []
147162
for candidate in candidates:
148163
candidate_names.append(f"{candidate.__module__}.{candidate.__name__}")
149-
150164
return candidate_names
151165

152166

153-
def _create_python_typename(candidate_name: str, container_types: Iterable[type]) -> str:
167+
def _create_python_typename(
168+
candidate_name: str, container_types: Iterable[type], additional_info: str
169+
) -> str:
154170
name = candidate_name
171+
if additional_info:
172+
name = f"{name}[{additional_info}]"
155173
for container_type in container_types:
156174
name = f"{container_type.__module__}.{container_type.__name__}[{name}]"
157175
return name
176+
177+
178+
def _get_additional_type_info_string(python_value: object) -> str:
179+
if isinstance(python_value, AnalogWaveform):
180+
return str(python_value.dtype)
181+
elif isinstance(python_value, ComplexWaveform):
182+
return str(python_value.dtype)
183+
else:
184+
return ""

0 commit comments

Comments
 (0)