Skip to content

Commit b910a14

Browse files
committed
Treat NaNs as missing values for gaps in ring buffer
Signed-off-by: cwasicki <[email protected]>
1 parent c5696b1 commit b910a14

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- A new class `Fuse` has been added to represent fuses. This class has a member variable `max_current` which represents the maximum current that can course through the fuse. If the current flowing through a fuse is greater than this limit, then the fuse will break the circuit.
2222

23+
- NaN values are treated as missing when gaps are determined in the `OrderedRingBuffer`.
2324

2425
## Bug Fixes
2526

src/frequenz/sdk/timeseries/_ringbuffer/buffer.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def gaps(self) -> List[Gap]:
108108
"""
109109
return self._gaps
110110

111+
def is_missing_value(self, value: QuantityT | None) -> bool:
112+
"""Check if a value should be considered as missing.
113+
114+
Args:
115+
value: quantity to check.
116+
117+
Returns:
118+
True if the value should be considered as missing.
119+
"""
120+
return (value is None) or value.isnan()
121+
111122
@property
112123
def maxlen(self) -> int:
113124
"""Get the max length.
@@ -154,10 +165,14 @@ def update(self, sample: Sample[QuantityT]) -> None:
154165
)
155166

156167
# Update data
157-
value: float = np.nan if sample.value is None else sample.value.base_value
168+
if self.is_missing_value(sample.value):
169+
value = np.nan
170+
else:
171+
assert sample.value is not None
172+
value = sample.value.base_value
158173
self._buffer[self.datetime_to_index(timestamp)] = value
159174

160-
self._update_gaps(timestamp, prev_newest, sample.value is None)
175+
self._update_gaps(timestamp, prev_newest, self.is_missing_value(sample.value))
161176

162177
@property
163178
def time_bound_oldest(self) -> datetime:

tests/timeseries/test_ringbuffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def test_gaps() -> None:
216216
buffer.update(Sample(dt(3), None))
217217
assert len(buffer.gaps) == 2
218218
buffer.update(Sample(dt(3), Quantity(np.nan)))
219-
assert len(buffer.gaps) == 1 # should be 2
219+
assert len(buffer.gaps) == 2
220220
buffer.update(Sample(dt(3), Quantity(3)))
221221
assert len(buffer.gaps) == 1
222222

0 commit comments

Comments
 (0)