Skip to content

Commit cadec83

Browse files
mikeprosserniMike Prosser
andauthored
Add start_index property to DigitalWaveform and NumericWaveform (#194)
* feat: add start_index property to DigitalWaveform and NumericWaveform classes * add start_index for spectrum also * fix comment --------- Co-authored-by: Mike Prosser <[email protected]>
1 parent f746eb9 commit cadec83

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

src/nitypes/waveform/_digital/_waveform.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,11 @@ def signal_count(self) -> int:
922922
shape: tuple[int, ...] = self._data.shape
923923
return shape[1]
924924

925+
@property
926+
def start_index(self) -> int:
927+
"""The sample index of the underlying array at which the waveform data begins."""
928+
return self._start_index
929+
925930
@property
926931
def capacity(self) -> int:
927932
"""The total capacity available for waveform data.

src/nitypes/waveform/_numeric.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ def sample_count(self, value: int) -> None:
466466
)
467467
self._sample_count = value
468468

469+
@property
470+
def start_index(self) -> int:
471+
"""The sample index of the underlying array at which the waveform data begins."""
472+
return self._start_index
473+
469474
@property
470475
def capacity(self) -> int:
471476
"""The total capacity available for waveform data.

src/nitypes/waveform/_spectrum.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ def sample_count(self) -> int:
542542
"""The number of samples in the spectrum."""
543543
return self._sample_count
544544

545+
@property
546+
def start_index(self) -> int:
547+
"""The sample index of the underlying array at which the spectrum data begins."""
548+
return self._start_index
549+
545550
@property
546551
def capacity(self) -> int:
547552
"""The total capacity available for spectrum data.

tests/unit/waveform/test_analog_waveform.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ def test___array_subset___from_array_2d___creates_waveform_with_array_subset(
580580
assert len(waveforms) == 2
581581
for i in range(len(waveforms)):
582582
assert waveforms[i].raw_data.tolist() == expected_data[i]
583+
assert waveforms[i].start_index == (start_index if start_index is not None else 0)
583584

584585

585586
@pytest.mark.parametrize(
@@ -1595,13 +1596,13 @@ def test___waveform_with_start_index___load_data___clears_start_index() -> None:
15951596
waveform = AnalogWaveform.from_array_1d(
15961597
np.array([0, 1, 2], np.int32), np.int32, copy=False, start_index=1, sample_count=1
15971598
)
1598-
assert waveform._start_index == 1
1599+
assert waveform.start_index == 1
15991600
array = np.array([3], np.int32)
16001601

16011602
waveform.load_data(array)
16021603

16031604
assert list(waveform.raw_data) == [3]
1604-
assert waveform._start_index == 0
1605+
assert waveform.start_index == 0
16051606

16061607

16071608
def test___ndarray_subset___load_data___overwrites_data() -> None:
@@ -1611,7 +1612,7 @@ def test___ndarray_subset___load_data___overwrites_data() -> None:
16111612
waveform.load_data(array, start_index=1, sample_count=1)
16121613

16131614
assert list(waveform.raw_data) == [4]
1614-
assert waveform._start_index == 0
1615+
assert waveform.start_index == 0
16151616
assert waveform.capacity == 3
16161617

16171618

tests/unit/waveform/test_digital_waveform.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ def test___array_subset___from_port___creates_waveform_with_array_subset() -> No
321321

322322
waveform = DigitalWaveform.from_port(array, start_index=2, sample_count=4)
323323

324+
assert waveform.start_index == 2
324325
assert waveform.sample_count == 4
325326
assert waveform.signal_count == 8
326327
assert waveform.data.tolist() == [
@@ -448,11 +449,13 @@ def test___array_subset___from_ports___creates_waveform_with_array_subset() -> N
448449
waveforms = DigitalWaveform.from_ports(array, start_index=1, sample_count=1)
449450

450451
assert len(waveforms) == 2
452+
assert waveforms[0].start_index == 1
451453
assert waveforms[0].sample_count == 1
452454
assert waveforms[0].signal_count == 8
453455
assert waveforms[0].data.tolist() == [
454456
[1, 0, 0, 0, 0, 0, 0, 0],
455457
]
458+
assert waveforms[1].start_index == 1
456459
assert waveforms[1].sample_count == 1
457460
assert waveforms[1].signal_count == 8
458461
assert waveforms[1].data.tolist() == [
@@ -1270,13 +1273,13 @@ def test___waveform_with_start_index___load_data___clears_start_index() -> None:
12701273
waveform = DigitalWaveform.from_lines(
12711274
np.array([[0], [1], [2]], np.uint8), np.uint8, copy=False, start_index=1, sample_count=1
12721275
)
1273-
assert waveform._start_index == 1
1276+
assert waveform.start_index == 1
12741277
array = np.array([[3]], np.uint8)
12751278

12761279
waveform.load_data(array)
12771280

12781281
assert waveform.data.tolist() == [[3]]
1279-
assert waveform._start_index == 0
1282+
assert waveform.start_index == 0
12801283

12811284

12821285
def test___ndarray_subset___load_data___overwrites_data() -> None:
@@ -1286,7 +1289,7 @@ def test___ndarray_subset___load_data___overwrites_data() -> None:
12861289
waveform.load_data(array, start_index=1, sample_count=1)
12871290

12881291
assert waveform.data.tolist() == [[4]]
1289-
assert waveform._start_index == 0
1292+
assert waveform.start_index == 0
12901293
assert waveform.capacity == 3
12911294

12921295

tests/unit/waveform/test_spectrum.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,13 @@ def test___spectrum_with_start_index___load_data___clears_start_index() -> None:
10711071
spectrum = Spectrum.from_array_1d(
10721072
np.array([0, 1, 2], np.int32), np.int32, copy=False, start_index=1, sample_count=1
10731073
)
1074-
assert spectrum._start_index == 1
1074+
assert spectrum.start_index == 1
10751075
array = np.array([3], np.int32)
10761076

10771077
spectrum.load_data(array)
10781078

10791079
assert list(spectrum.data) == [3]
1080-
assert spectrum._start_index == 0
1080+
assert spectrum.start_index == 0
10811081

10821082

10831083
def test___ndarray_subset___load_data___overwrites_data() -> None:
@@ -1087,7 +1087,7 @@ def test___ndarray_subset___load_data___overwrites_data() -> None:
10871087
spectrum.load_data(array, start_index=1, sample_count=1)
10881088

10891089
assert list(spectrum.data) == [4]
1090-
assert spectrum._start_index == 0
1090+
assert spectrum.start_index == 0
10911091
assert spectrum.capacity == 3
10921092

10931093

0 commit comments

Comments
 (0)