|
2 | 2 |
|
3 | 3 | import numpy |
4 | 4 | from nitypes.bintime import DateTime |
5 | | -from nitypes.waveform import AnalogWaveform, NoneScaleMode, SampleIntervalMode, Timing |
| 5 | +from nitypes.waveform import ( |
| 6 | + AnalogWaveform, |
| 7 | + NoneScaleMode, |
| 8 | + SampleIntervalMode, |
| 9 | + Spectrum, |
| 10 | + Timing, |
| 11 | +) |
6 | 12 |
|
7 | | -from ni.protobuf.types.precision_timestamp_conversion import bintime_datetime_to_protobuf |
| 13 | +from ni.protobuf.types.precision_timestamp_conversion import ( |
| 14 | + bintime_datetime_to_protobuf, |
| 15 | +) |
8 | 16 | from ni.protobuf.types.waveform_conversion import ( |
9 | | - float64_analog_waveform_to_protobuf, |
10 | 17 | float64_analog_waveform_from_protobuf, |
| 18 | + float64_analog_waveform_to_protobuf, |
| 19 | + float64_spectrum_from_protobuf, |
| 20 | + float64_spectrum_to_protobuf, |
11 | 21 | ) |
12 | 22 | from ni.protobuf.types.waveform_pb2 import ( |
13 | 23 | DoubleAnalogWaveform, |
| 24 | + DoubleSpectrum, |
14 | 25 | WaveformAttributeValue, |
15 | 26 | ) |
16 | 27 |
|
@@ -139,3 +150,80 @@ def test___dbl_analog_wfm_with_timing_no_dt___convert___valid_python_object() -> |
139 | 150 | assert analog_waveform.timing.start_time == t0_dt._to_datetime_datetime() |
140 | 151 | assert not analog_waveform.timing.has_sample_interval |
141 | 152 | assert analog_waveform.timing.sample_interval_mode == SampleIntervalMode.NONE |
| 153 | + |
| 154 | + |
| 155 | +# ======================================================== |
| 156 | +# Spectrum to DoubleSpectrum |
| 157 | +# ======================================================== |
| 158 | +def test___default_spectrum___convert___valid_protobuf() -> None: |
| 159 | + spectrum = Spectrum() |
| 160 | + |
| 161 | + dbl_spectrum = float64_spectrum_to_protobuf(spectrum) |
| 162 | + |
| 163 | + assert not dbl_spectrum.attributes |
| 164 | + assert spectrum.start_frequency == 0.0 |
| 165 | + assert spectrum.frequency_increment == 0.0 |
| 166 | + assert list(dbl_spectrum.data) == [] |
| 167 | + |
| 168 | + |
| 169 | +def test___spectrum_with_data___convert___valid_protobuf() -> None: |
| 170 | + spectrum = Spectrum.from_array_1d(numpy.array([1.0, 2.0, 3.0])) |
| 171 | + spectrum.start_frequency = 100.0 |
| 172 | + spectrum.frequency_increment = 10.0 |
| 173 | + |
| 174 | + dbl_spectrum = float64_spectrum_to_protobuf(spectrum) |
| 175 | + |
| 176 | + assert list(dbl_spectrum.data) == [1.0, 2.0, 3.0] |
| 177 | + assert dbl_spectrum.start_frequency == 100.0 |
| 178 | + assert dbl_spectrum.frequency_increment == 10.0 |
| 179 | + |
| 180 | + |
| 181 | +def test___spectrum_with_extended_properties___convert___valid_protobuf() -> None: |
| 182 | + spectrum = Spectrum() |
| 183 | + spectrum.channel_name = "Dev1/ai0" |
| 184 | + spectrum.unit_description = "Volts" |
| 185 | + |
| 186 | + dbl_spectrum = float64_spectrum_to_protobuf(spectrum) |
| 187 | + |
| 188 | + assert dbl_spectrum.attributes["NI_ChannelName"].string_value == "Dev1/ai0" |
| 189 | + assert dbl_spectrum.attributes["NI_UnitDescription"].string_value == "Volts" |
| 190 | + |
| 191 | + |
| 192 | +# ======================================================== |
| 193 | +# DoubleSpectrum to Spectrum |
| 194 | +# ======================================================== |
| 195 | +def test___default_dbl_spectrum___convert___valid_python_object() -> None: |
| 196 | + dbl_spectrum = DoubleSpectrum() |
| 197 | + |
| 198 | + spectrum = float64_spectrum_from_protobuf(dbl_spectrum) |
| 199 | + |
| 200 | + assert not spectrum.extended_properties |
| 201 | + assert spectrum.start_frequency == 0.0 |
| 202 | + assert spectrum.frequency_increment == 0.0 |
| 203 | + assert spectrum.sample_count == 0 |
| 204 | + assert spectrum.data.size == 0 |
| 205 | + |
| 206 | + |
| 207 | +def test___dbl_spectrum_with_data___convert___valid_python_object() -> None: |
| 208 | + dbl_spectrum = DoubleSpectrum( |
| 209 | + data=[1.0, 2.0, 3.0], start_frequency=100.0, frequency_increment=10.0 |
| 210 | + ) |
| 211 | + |
| 212 | + spectrum = float64_spectrum_from_protobuf(dbl_spectrum) |
| 213 | + |
| 214 | + assert list(spectrum.data) == [1.0, 2.0, 3.0] |
| 215 | + assert spectrum.start_frequency == 100.0 |
| 216 | + assert spectrum.frequency_increment == 10.0 |
| 217 | + |
| 218 | + |
| 219 | +def test___dbl_spectrum_with_attributes___convert___valid_python_object() -> None: |
| 220 | + attributes = { |
| 221 | + "NI_ChannelName": WaveformAttributeValue(string_value="Dev1/ai0"), |
| 222 | + "NI_UnitDescription": WaveformAttributeValue(string_value="Volts"), |
| 223 | + } |
| 224 | + dbl_spectrum = DoubleSpectrum(attributes=attributes) |
| 225 | + |
| 226 | + spectrum = float64_spectrum_from_protobuf(dbl_spectrum) |
| 227 | + |
| 228 | + assert spectrum.channel_name == "Dev1/ai0" |
| 229 | + assert spectrum.unit_description == "Volts" |
0 commit comments