Skip to content
1 change: 0 additions & 1 deletion src/nitypes/waveform/_analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ def __init__(
of this array. If not specified, an ndarray is created based on the specified dtype,
start index, sample count, and capacity.
start_index: The sample index at which the analog waveform data begins.
sample_count: The number of samples in the analog waveform.
capacity: The number of samples to allocate. Pre-allocating a larger buffer optimizes
appending samples to the waveform.
extended_properties: The extended properties of the analog waveform.
Expand Down
1 change: 0 additions & 1 deletion src/nitypes/waveform/_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ def __init__(
of this array. If not specified, an ndarray is created based on the specified dtype,
start index, sample count, and capacity.
start_index: The sample index at which the waveform data begins.
sample_count: The number of samples in the waveform.
capacity: The number of samples to allocate. Pre-allocating a larger buffer optimizes
appending samples to the waveform.
extended_properties: The extended properties of the waveform.
Expand Down
10 changes: 10 additions & 0 deletions src/nitypes/waveform/_digital/_waveform.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,16 @@ def sample_count(self) -> int:
"""The number of samples in the waveform."""
return self._sample_count

@sample_count.setter
def sample_count(self, value: int) -> None:
"""Set the number of samples in the waveform."""
value = arg_to_uint("sample count", value)
if self._start_index + value > self.capacity:
raise create_start_index_or_sample_count_too_large_error(
self._start_index, value, "capacity", self.capacity
)
self._sample_count = value

@property
def signal_count(self) -> int:
"""The number of signals in the waveform."""
Expand Down
10 changes: 10 additions & 0 deletions src/nitypes/waveform/_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ def sample_count(self) -> int:
"""The number of samples in the waveform."""
return self._sample_count

@sample_count.setter
def sample_count(self, value: int) -> None:
"""Set the number of samples in the waveform."""
value = arg_to_uint("sample count", value)
if self._start_index + value > self.capacity:
raise create_start_index_or_sample_count_too_large_error(
self._start_index, value, "capacity", self.capacity
)
self._sample_count = value

@property
def capacity(self) -> int:
"""The total capacity available for waveform data.
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/waveform/test_analog_waveform.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,32 @@ def test___array_subset___get_scaled_data___returns_array_subset() -> None:
assert list(scaled_data) == [2.5, 4.5]


###############################################################################
# sample count
###############################################################################
def test___waveform___set_sample_count___updates_sample_count() -> None:
waveform = AnalogWaveform(10, np.float64)

waveform.sample_count = 5

assert waveform.sample_count == 5
assert waveform.capacity == 10 # Capacity should remain unchanged
assert len(waveform.raw_data) == 5 # Raw data length should reflect new sample count


def test___waveform___set_sample_count_exceeds_capacity___raises_value_error() -> None:
waveform = AnalogWaveform(5, np.float64, capacity=10)

with pytest.raises(ValueError) as exc:
waveform.sample_count = 15

assert exc.value.args[0].startswith(
"The sum of the start index and sample count must be less than or equal to the capacity."
)

assert waveform.sample_count == 5 # Original sample count preserved


###############################################################################
# capacity
###############################################################################
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/waveform/test_digital_waveform.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,42 @@ def test___invalid_array_subset___get_data___returns_array_subset(
assert exc.value.args[0].startswith(expected_message)


###############################################################################
# sample_count
###############################################################################
def test___waveform___set_sample_count___updates_sample_count() -> None:
waveform = DigitalWaveform(10, 2, np.uint8)

waveform.sample_count = 5

assert waveform.sample_count == 5
assert waveform.capacity == 10 # Capacity should remain unchanged
assert waveform.signal_count == 2 # Signal count should remain unchanged
assert waveform.data.shape == (5, 2) # Data shape should reflect new sample count


def test___waveform___set_sample_count_to_zero___creates_empty_waveform() -> None:
waveform = DigitalWaveform.from_lines([[1, 2], [3, 4]], np.uint8)

waveform.sample_count = 0

assert waveform.sample_count == 0
assert waveform.data.tolist() == []
assert waveform.capacity == 2 # Capacity preserved


def test___waveform___set_sample_count_exceeds_capacity___raises_value_error() -> None:
waveform = DigitalWaveform(5, 2, np.uint8, capacity=10)

with pytest.raises(ValueError) as exc:
waveform.sample_count = 15

assert exc.value.args[0].startswith(
"The sum of the start index and sample count must be less than or equal to the capacity."
)
assert waveform.sample_count == 5 # Original sample count preserved


###############################################################################
# capacity
###############################################################################
Expand Down
Loading